Sort by

recency

|

880 Discussions

|

  • + 0 comments

    Branded lanyards can serve as a useful tool while exploring the concept of Roads and Libraries. As both are integral to connectivity, roads symbolize physical infrastructure, while libraries represent hubs of knowledge. Using lanyards, you can keep your IDs, keys, or event passes handy while navigating these essential spaces. Whether for educational trips or professional events, these lanyards are practical and serve as subtle promotional items to showcase your brand.

  • + 0 comments

    Guys , its a Basic disjoint set implemtation using krushkal;

    The catch is it will pass all except 7 testcase due to typedef ,

    we need to change all the int to long long , cause we multiply

    So, we need to even change the long long inside main loop also instead of typecasting at result!!

  • + 0 comments

    I try very hard, buts for now I dont know whats is wrong with my code, can somebody help please?

    class Result {

    private static class Graph {
    
         private final int size;
    
        private long nodes = 0;
    
        private final long costForRoad;
    
        private final long costForLibrary;
    
        private final int[][] cities;
    
        public Graph(int size, final long costForRoad, final long costForLibrary) {
            this.size = size;
            this.costForRoad = costForRoad;
            this.costForLibrary = costForLibrary;
            this.cities = new int[size][];
            for (int i = 0; i < size; i++) {
                this.cities[i] = new int[size];
            }
        }
    
        public void addEdge(
                final int origin,
                final int destiny
        ) {
            this.cities[origin-1][destiny-1] = 1;
            this.cities[destiny-1][origin-1] = 1;
        }
    
       public long calcCost() {
            long cost = 0;
            final boolean[] visited = new boolean[this.size];
            for (int i = 0; i < this.size; i++) {
                if(!visited[i]) {
                    nodes = 0;
                    this.dfs(i, visited);
                      if(nodes == 1) {
                        cost += nodes * costForLibrary;
                    } else {
                        cost += (nodes - 1) * costForRoad + costForLibrary;
                    }
                }
            }
    
            return cost;
        }
    
        private void dfs(
                final int vertex,
                final boolean[] visited
        ) {
            nodes += 1;
            visited[vertex] = true;
            for (int i = 0; i < this.cities[vertex].length; i++) {
                if(this.cities[vertex][i] == 1 && !visited[i]) {
                    dfs(i, visited);
                }
            }
        }
    }
    
    /*
     * Complete the 'roadsAndLibraries' function below.
     *
     * The function is expected to return a LONG_INTEGER.
     * The function accepts following parameters:
     *  1. INTEGER n
     *  2. INTEGER c_lib
     *  3. INTEGER c_road
     *  4. 2D_INTEGER_ARRAY cities
     */
    
    public static long roadsAndLibraries(int n, int c_lib, int c_road, List<List<Integer>> cities) {
    // Write your code here
        if(c_lib < c_road) {
            return n * c_lib;
        }
    
        final Graph graph = new Graph(n, c_road, c_lib);
        for(List<Integer> connections : cities) {
            graph.addEdge(connections.get(0), connections.get(1));
        }
        return graph.calcCost();
    }
    

    }

  • + 0 comments

    with custom input 1 5 3 6 1 1 2 1 3 1 4 it returns an expected answer of 15 when infact its 9. ie c_lib = 6 c_road = 1 1 library + 3 roads = 9 not 15 1 libraries + 3 roads = 15

  • + 3 comments

    Compiler Message: Wrong Answer

    Input (stdin) 1 5 3 6 1 1 2 1 3 1 4 Your Output (stdout) 9

    Any ideas why my answer is wrong? 4 cities in one aglomeration, cost of library 6 and cost of road 1. It's simply one library (6) and 3 roads (3) what all gives 9. Do I misunderstand something, or hackerrank has a bug?