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 n nodes (cities) numbered from 0 to n-1
  • A scores array where scores[i] represents the popularity score of city i
  • An edges array where each edges[i] = [a, b] represents a direct road between cities a and b

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
4-Restaurant Food Tour OptimizationAโ˜… 9.2Bโ˜… 7.8Cโ˜… 8.5Dโ˜… 9.7BRIDGEAlgorithm Strategy1. Bridge Focus: Every 4-restaurant tour has a middle connection (Bโ†’C)2. Smart Search: Try each street as potential bridge connection3. Optimal Endpoints: Find highest-rated restaurants at each end4. Efficiency: Sort connections by rating for quick selectionResult: A(9.2) โ†’ B(7.8) โ†’ C(8.5) โ†’ D(9.7) = Total Score: 35.2 โญTime Complexity: O(nยฒ + m) vs Brute Force: O(nโด)
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

n
2n
โš  Quadratic Growth
Space Complexity
O(nยฒ)

Space for adjacency lists containing all graph connections

n
2n
โš  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
Asked in
Google 45 Meta 38 Amazon 32 Microsoft 28
48.3K Views
High Frequency
~25 min Avg. Time
1.3K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen