Maximum Score of a Node Sequence - Problem
Imagine you're exploring a network of interconnected cities, where each city has a popularity score. Your goal is to plan the perfect 4-city tour that maximizes the total score!
You're given:
- An undirected graph with
nnodes (cities) numbered from 0 to n-1 - A
scoresarray wherescores[i]represents the popularity score of cityi - An
edgesarray where eachedges[i] = [a, b]represents a direct road between citiesaandb
Your tour must follow these rules:
- Connected Path: You can only travel between directly connected cities
- No Revisits: Each city can only be visited once in your tour
- Exactly 4 Cities: Your tour must visit exactly 4 different cities
Goal: Find the tour of 4 cities with the maximum total popularity score. If no valid 4-city tour exists, return -1.
Input & Output
example_1.py โ Basic Connected Path
$
Input:
scores = [5,2,9,8,4], edges = [[0,1],[1,2],[2,3],[0,2],[1,3],[2,4]]
โบ
Output:
24
๐ก Note:
The optimal 4-node path is 0 โ 2 โ 1 โ 3 with scores [5,9,2,8] totaling 24. This path follows edges: (0,2), (2,1), (1,3), creating a valid sequence where each consecutive pair is connected.
example_2.py โ Linear Chain
$
Input:
scores = [9,20,6,4,11,12], edges = [[0,3],[5,3],[2,4],[1,3]]
โบ
Output:
-1
๐ก Note:
No valid 4-node sequence exists. The graph has disconnected components and no path of length 4 where all consecutive nodes are connected by edges. Node 3 is a hub connected to 0,1,5 but these don't form a 4-node path.
example_3.py โ Multiple Valid Paths
$
Input:
scores = [1,1,1,1,1], edges = [[0,1],[1,2],[2,3],[3,4]]
โบ
Output:
4
๐ก Note:
The graph forms a linear path 0-1-2-3-4. The only valid 4-node sequences are consecutive segments like 0โ1โ2โ3 or 1โ2โ3โ4, each with total score 1+1+1+1 = 4.
Visualization
Tap to expand
Understanding the Visualization
1
Map the Connections
First, map out which restaurants are directly connected by walkable streets
2
Identify Bridge Points
Realize that any 4-restaurant tour has two middle restaurants that act as a bridge
3
Find Best Endpoints
For each potential bridge connection, find the highest-rated restaurants on each end
4
Calculate Optimal Route
Compare all possible bridge-based routes to find the maximum total rating
Key Takeaway
๐ฏ Key Insight: By recognizing that every 4-node path has a middle edge structure, we can dramatically reduce the search space from O(nโด) to O(nยฒ + m), making the algorithm efficient enough for large graphs while guaranteeing the optimal solution.
Time & Space Complexity
Time Complexity
O(nยฒ + m)
O(nยฒ) for sorting all adjacency lists, O(m) for iterating through all edges where m is number of edges
โ Quadratic Growth
Space Complexity
O(nยฒ)
Space for adjacency lists containing all graph connections
โ Quadratic Space
Constraints
- n == scores.length
- 4 โค n โค 5 ร 104
- 1 โค scores[i] โค 108
- 0 โค edges.length โค 5 ร 104
- edges[i].length == 2
- 0 โค ai, bi โค n - 1
- ai โ bi
- No duplicate edges exist
- The graph may be disconnected
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code