Sort by

recency

|

95 Discussions

|

  • + 0 comments

    Java solution using Dijkstra's algo

    class Result {
    
        /*
         * Complete the 'getCost' function below.
         * The function accepts a weighted graph defined by:
         * 1. gNodes: Number of nodes
         * 2. gFrom, gTo: Lists of nodes defining edges
         * 3. gWeight: Weights of the edges
         */
    
        // Edge class to store destination and weight
        static class Edge {
            int dest, weight;
            public Edge(int dest, int weight) {
                this.dest = dest;
                this.weight = weight;
            }
        }
    
        // State class for priority queue (Dijkstra's)
        static class State {
            int node, cost;
            public State(int node, int cost) {
                this.node = node;
                this.cost = cost;
            }
        }
    
        public static void getCost(int gNodes, List<Integer> gFrom, List<Integer> gTo, List<Integer> gWeight) {
            // Step 1: Build the graph using adjacency list
            List<List<Edge>> graph = new ArrayList<>();
            for (int i = 0; i <= gNodes; i++) {
                graph.add(new ArrayList<>());
            }
    
            for (int i = 0; i < gFrom.size(); i++) {
                int u = gFrom.get(i);
                int v = gTo.get(i);
                int weight = gWeight.get(i);
    
                graph.get(u).add(new Edge(v, weight));
                graph.get(v).add(new Edge(u, weight));
            }
    
            // Step 2: Implement Dijkstra's algorithm to find the minimum "maximum edge weight"
            PriorityQueue<State> pq = new PriorityQueue<>(Comparator.comparingInt(s -> s.cost));
            boolean[] visited = new boolean[gNodes + 1];
            int[] minCosts = new int[gNodes + 1];
            Arrays.fill(minCosts, Integer.MAX_VALUE);
            minCosts[1] = 0; // Start from node 1
            pq.offer(new State(1, 0));
    
            while (!pq.isEmpty()) {
                State current = pq.poll();
    
                // Skip already visited nodes
                if (visited[current.node]) continue;
                visited[current.node] = true;
    
                // Explore neighbors
                for (Edge edge : graph.get(current.node)) {
                    int maxCost = Math.max(current.cost, edge.weight); // Update the max edge cost along the path
                    if (maxCost < minCosts[edge.dest]) {
                        minCosts[edge.dest] = maxCost;
                        pq.offer(new State(edge.dest, maxCost));
                    }
                }
            }
    
            // Step 3: Print the result
            if (minCosts[gNodes] == Integer.MAX_VALUE) {
                System.out.println("NO PATH EXISTS");
            } else {
                System.out.println(minCosts[gNodes]);
            }
        }
    }
    
  • + 0 comments

    Emergency locksmith services offer peace of mind during unexpected situations! When Jack goes to Rapture, a thrilling world filled with mystery, he encounters challenges that require quick thinking. Just like an emergency locksmith responding to a lockout, Jack must adapt, solve problems, and navigate through the complex environment. Whether facing security challenges or unlocking secrets, both Jack and locksmiths understand the importance of quick, reliable solutions when under pressure.

  • + 0 comments

    for two failing testcases, print "NO PATH EXISTS"

  • + 1 comment

    i am not getting the point for the test case 2 , answer is showing 1196 as min fair but is the fair for going from 1-6-7 only not reaching 10 though 7- 10 has fair 1974...?

  • + 0 comments

    Join Jack on his adventure to Rapture in our latest immersive experience. Discover the captivating world and intricate storylines that await. To celebrate this thrilling journey, we’re offering a selection of promotional business products with every experience purchase. From branded notebooks to custom pens, these items will help you stay organized and inspired while exploring Rapture. Enhance your adventure with practical and stylish business products that reflect your excitement."