LeetCode 2247: Maximum Cost of Trip With K Highways Solution

Master LeetCode problem 2247 (Maximum Cost of Trip With K Highways), a hard challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

2247. Maximum Cost of Trip With K Highways

Problem Explanation

Explanation:

This problem can be solved using Dijkstra's algorithm to find the shortest path in a graph with positive weights. We can modify Dijkstra's algorithm to keep track of the maximum cost while traversing the graph. The idea is to maintain a priority queue to store the vertices to be explored next based on their cost.

  1. Create an adjacency list to represent the graph where each edge has a weight representing the cost.
  2. Initialize an array to keep track of the maximum cost to reach each node, initially set to negative infinity except for the starting node which is set to 0.
  3. Initialize a priority queue to store vertices based on their maximum cost.
  4. Start with the starting node, update the maximum cost to reach neighboring nodes if the cost is greater and add them to the priority queue.
  5. Repeat the process until the priority queue is empty.

Time complexity: O(E log V), where E is the number of edges and V is the number of vertices. Space complexity: O(V), where V is the number of vertices.

: :

Solution Code

import java.util.*;

class Edge {
    int to;
    int cost;

    public Edge(int to, int cost) {
        this.to = to;
        this.cost = cost;
    }
}

public int maxCost(int n, int k, int[][] highways) {
    List<List<Edge>> graph = new ArrayList<>();
    for (int i = 0; i < n; i++) {
        graph.add(new ArrayList<>());
    }

    for (int[] highway : highways) {
        int from = highway[0];
        int to = highway[1];
        int cost = highway[2];
        graph.get(from).add(new Edge(to, cost));
    }

    PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> Integer.compare(b[1], a[1]));
    int[] maxCost = new int[n];
    Arrays.fill(maxCost, Integer.MIN_VALUE);
    maxCost[0] = 0;
    pq.offer(new int[]{0, 0});

    while (!pq.isEmpty()) {
        int[] curr = pq.poll();
        int node = curr[0];
        int cost = curr[1];

        if (cost < maxCost[node]) continue;

        for (Edge edge : graph.get(node)) {
            int neighbor = edge.to;
            int newCost = cost + edge.cost;

            if (newCost > maxCost[neighbor]) {
                maxCost[neighbor] = newCost;
                pq.offer(new int[]{neighbor, newCost});
            }
        }
    }

    return maxCost[n - 1];
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 2247 (Maximum Cost of Trip With K Highways)?

This page provides optimized solutions for LeetCode problem 2247 (Maximum Cost of Trip With K Highways) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 2247 (Maximum Cost of Trip With K Highways)?

The time complexity for LeetCode 2247 (Maximum Cost of Trip With K Highways) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 2247 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 2247 in Java, C++, or Python.

Back to LeetCode Solutions