Stealth Interview
  • Features
  • Pricing
  • Blog
  • Login
  • Sign up

Leetcode #2247: Maximum Cost of Trip With K Highways

In this guide, we solve Leetcode #2247 Maximum Cost of Trip With K Highways in Python and focus on the core idea that makes the solution efficient.

You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Leetcode

Problem Statement

A series of highways connect n cities numbered from 0 to n - 1. You are given a 2D integer array highways where highways[i] = [city1i, city2i, tolli] indicates that there is a highway that connects city1i and city2i, allowing a car to go from city1i to city2i and vice versa for a cost of tolli.

Quick Facts

  • Difficulty: Hard
  • Premium: Yes
  • Tags: Bit Manipulation, Graph, Dynamic Programming, Bitmask

Intuition

The problem breaks into overlapping subproblems, so caching results prevents exponential repetition.

A carefully chosen DP state captures exactly what we need to build the final answer.

Approach

Define the DP state and recurrence, then compute states in the correct order.

Optionally compress space once the recurrence is clear.

Steps:

  • Choose a DP state definition.
  • Write the recurrence and base cases.
  • Compute states in the correct order.

Example

Input: n = 5, highways = [[0,1,4],[2,1,3],[1,4,11],[3,2,3],[3,4,2]], k = 3 Output: 17 Explanation: One possible trip is to go from 0 -> 1 -> 4 -> 3. The cost of this trip is 4 + 11 + 2 = 17. Another possible trip is to go from 4 -> 1 -> 2 -> 3. The cost of this trip is 11 + 3 + 3 = 17. It can be proven that 17 is the maximum possible cost of any valid trip. Note that the trip 4 -> 1 -> 0 -> 1 is not allowed because you visit the city 1 twice.

Python Solution

class Solution: def maximumCost(self, n: int, highways: List[List[int]], k: int) -> int: if k >= n: return -1 g = defaultdict(list) for a, b, cost in highways: g[a].append((b, cost)) g[b].append((a, cost)) f = [[-inf] * n for _ in range(1 << n)] for i in range(n): f[1 << i][i] = 0 ans = -1 for i in range(1 << n): for j in range(n): if i >> j & 1: for h, cost in g[j]: if i >> h & 1: f[i][j] = max(f[i][j], f[i ^ (1 << j)][h] + cost) if i.bit_count() == k + 1: ans = max(ans, f[i][j]) return ans

Complexity

The time complexity is O(2n×n2)O(2^n \times n^2)O(2n×n2), and the space complexity is O(2n×n)O(2^n \times n)O(2n×n), where nnn represents the number of cities. The space complexity is O(2n×n)O(2^n \times n)O(2n×n), where nnn represents the number of cities.

Edge Cases and Pitfalls

Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.

Summary

This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.


Ace your next coding interview

We're here to help you ace your next coding interview.

Subscribe
Stealth Interview
© 2026 Stealth Interview®Stealth Interview is a registered trademark. All rights reserved.
Product
  • Blog
  • Pricing
Company
  • Terms of Service
  • Privacy Policy