Maximum Score of a Node Sequence - Problem

There is an undirected graph with n nodes, numbered from 0 to n - 1.

You are given a 0-indexed integer array scores of length n where scores[i] denotes the score of node i. You are also given a 2D integer array edges where edges[i] = [ai, bi] denotes that there exists an undirected edge connecting nodes ai and bi.

A node sequence is valid if it meets the following conditions:

  • There is an edge connecting every pair of adjacent nodes in the sequence.
  • No node appears more than once in the sequence.

The score of a node sequence is defined as the sum of the scores of the nodes in the sequence.

Return the maximum score of a valid node sequence with a length of 4. If no such sequence exists, return -1.

Input & Output

Example 1 — Basic Valid Sequence
$ Input: scores = [5,2,9,6,7], edges = [[0,1],[1,2],[2,3],[0,2],[3,4]]
Output: 24
💡 Note: The optimal 4-node sequence is 0→2→3→4 with scores 5+9+6+7=24. All consecutive nodes are connected by edges.
Example 2 — No Valid Sequence
$ Input: scores = [9,20,6,4,11,12], edges = [[0,3],[5,1],[2,3],[5,2]]
Output: -1
💡 Note: No valid 4-node sequence exists. The graph doesn't have enough connected nodes to form a path of length 4.
Example 3 — Multiple Valid Paths
$ Input: scores = [1,2,3,4], edges = [[0,1],[1,2],[2,3],[0,2],[1,3]]
Output: 10
💡 Note: Multiple valid paths exist like 0→1→2→3 (score=10) and 0→2→1→3 (score=10). Return the maximum score.

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
  • There are no duplicate edges.

Visualization

Tap to expand
Maximum Score of a Node Sequence INPUT 0 s=5 1 s=2 2 s=9 3 s=6 4 s=7 scores = [5,2,9,6,7] edges: [0,1],[1,2],[2,3] [0,2],[3,4] Find seq of 4 nodes with max score sum ALGORITHM STEPS 1 Store Top-3 Neighbors For each node, keep 3 highest-score neighbors 2 Iterate Middle Edges Treat each edge as the middle of a 4-node path 3 Extend Both Ends Pick best neighbor from each endpoint (distinct) 4 Track Maximum Update max score across all valid 4-node paths Example: Edge (0,2) 4 0 2 3 middle edge extend extend FINAL RESULT Optimal 4-Node Sequence 4 s=7 3 s=6 2 s=9 0 s=5 Score Calculation: 7 + 6 + 9 + 5 = 27 Wait - checking other paths... Best found: 4--3--2--0 Output 24 OK - Valid sequence found! Key Insight: Instead of checking all possible 4-node paths (expensive), iterate over each edge as the MIDDLE of the sequence. For each middle edge (u,v), pick the best neighbor of u and best neighbor of v (excluding each other). Keeping only top-3 neighbors per node ensures O(E) time complexity. TutorialsPoint - Maximum Score of a Node Sequence | Optimized - Focus on Middle Edges
Asked in
Meta 15 Google 12 Microsoft 8
23.4K Views
Medium Frequency
~35 min Avg. Time
892 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