BackhardDynamic ProgrammingSwiggyMeta

Hyper-Dimensional Grid Optimizer 2 Solution

Problem Statement

You are given a tree with N nodes (1 ≤ N ≤ 10^5). Each node i has an integer weight w_i (−10^9 ≤ w_i ≤ 10^9). A subset of nodes is called independent if no two nodes in the subset are connected by an edge. Your task is to find an independent subset with the maximum possible sum of weights. If all weights are negative, the empty subset is allowed and the maximum sum is 0. The input consists of the number of nodes, the list of weights, and the N−1 edges that form the tree. The output is a single integer: the maximum achievable sum.

Example 1
Input
5 1 2 3 4 5 1 2 1 3 3 4 3 5
Output
11

Explanation: The tree structure: - Node 1 connects to 2 and 3 - Node 3 connects to 4 and 5 Choosing nodes 2, 4, and 5 gives weights 2 + 4 + 5 = 11. No two chosen nodes share an edge, and 11 is the largest possible sum.

Example 2
Input
4 -1 -2 -3 -4 1 2 2 3 3 4
Output
0

Explanation: All weights are negative. Selecting any node would decrease the sum, so the optimal independent set is empty, yielding a sum of 0.

Example 3
Input
6 10 -5 20 -1 5 3 1 2 1 3 2 4 2 5 3 6
Output
17

Explanation: Tree layout: - 1 connects to 2 and 3 - 2 connects to 4 and 5 - 3 connects to 6 Choosing nodes 1, 4, 5, and 6 gives 10 + (−1) + 5 + 3 = 17. This set is independent and yields the maximum sum.

Example 4
Input
3 5 5 5 1 2 1 3
Output
10

Explanation: The tree has node 1 connected to nodes 2 and 3. Selecting nodes 2 and 3 gives 5 + 5 = 10, which is the largest sum without selecting node 1.

Constraints

  • 1 ≤ N ≤ 10^5
  • −10^9 ≤ w_i ≤ 10^9 for all i
  • The graph is a tree: it is connected and has exactly N−1 edges
  • All input values are integers
  • The answer fits in a 64‑bit signed integer
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

🚀 Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Hyper-Dimensional Grid Optimizer 2 — Problem Statement & Solution Guide

Dynamic ProgrammingHardTree DP
TimeO(N)
|
SpaceO(N)

Problem Description

You are given a tree with N nodes (1 ≤ N ≤ 10^5). Each node i has an integer weight w_i (−10^9 ≤ w_i ≤ 10^9). A subset of nodes is called *independent* if no two nodes in the subset are connected by an edge. Your task is to find an independent subset with the maximum possible sum of weights. If all weights are negative, the empty subset is allowed and the maximum sum is 0. The input consists of the number of nodes, the list of weights, and the N−1 edges that form the tree. The output is a single integer: the maximum achievable sum.

DSA Pattern Breakdown

DSA Pattern Breakdown

"Hyper-Dimensional Grid Optimizer 2"

hard

WHY DOES IT MATTER?

Tree DP is a fundamental pattern for problems where global constraints decompose into local decisions along a hierarchy. Mastering it unlocks efficient solutions for many hierarchical optimization tasks, from resource allocation in organizational charts to compiler register allocation on abstract syntax trees.

OPTIMIZATION CHALLENGE

The breakthrough is recognizing that each node only needs two aggregated values (taken vs. not taken). This reduces the exponential subset space to O(N) by collapsing all possible configurations of a subtree into these two numbers.

REAL-WORLD CONNECTION

Think of a corporate hierarchy where you want to award bonuses to employees such that no direct manager and subordinate both receive a bonus. The optimal bonus distribution mirrors the independent set DP: each manager decides to take a bonus (excluding subordinates) or skip it (allowing subordinates to decide).

During the interview, write the DFS skeleton first, then immediately define the two DP arrays. Fill the recurrence in a few lines, and remember to handle the all‑negative edge case by taking max with zero at the final step.

COMPLEXITY AT A GLANCE

⏱ Time:O(N)
💾 Space:O(N)

Core Theory — Why This Approach?

The problem is a classic instance of the Maximum Weight Independent Set on a tree, which can be solved optimally using tree DP. For each node we maintain two DP states: dp0[u] – the best sum when u is excluded, and dp1[u] – the best sum when u is included. The recurrence is straightforward: if we include u, we must exclude all its children, so dp1[u] = w_u + Σ dp0[v] for each child v; if we exclude u, each child may be either included or excluded, giving dp0[u] = Σ max(dp0[v], dp1[v]). A post‑order DFS computes these values in linear time. The answer is max(dp0[root], dp1[root]), and we clamp it to zero to handle the all‑negative case.

A naive approach that enumerates all subsets would be O(2^N) and quickly becomes infeasible for N up to 10^5. Even a DP that treats the tree as a generic graph without exploiting the hierarchical structure would require exponential state space. The optimal paradigm leverages the tree’s acyclic nature, allowing us to decompose the global constraint (no adjacent selected nodes) into local decisions that propagate upward, achieving O(N) time and O(N) memory.

The key insight is that the independence constraint is local to edges, so the optimal solution for a subtree depends only on whether its root is taken or not. This binary choice per node yields a linear‑size DP table, turning an otherwise combinatorial explosion into a tractable linear scan.

Interview Questions on This Problem

Q1How would you modify the DP if the tree were rooted and you needed to return the actual set of nodes achieving the maximum sum?

During the DFS, store a boolean flag for each node indicating whether dp1[u] > dp0[u]. After the DP, perform a second top‑down traversal: if a node is marked as taken, skip its children (force them to be excluded); otherwise, follow the choice that gave max(dp0, dp1) for each child. Collect all nodes marked taken to reconstruct the optimal independent set.

Q2What changes are required if the problem asks for the maximum sum of an independent set where the empty set is not allowed?

Compute the same DP but at the end return max(dp0[root], dp1[root]) without clamping to zero. If both values are negative, the answer will be the least negative value, meaning you must pick at least one node; the DP already captures this because dp1 includes the node's weight.

Q3Can this DP be extended to graphs with cycles, such as a general undirected graph? If not, why?

No. The recurrence relies on the tree property that removing a node separates the graph into independent subtrees. In a cyclic graph, children are not independent after removing a node, leading to overlapping subproblems and requiring exponential state (e.g., treewidth‑based DP). Hence the linear DP only works on trees or graphs with bounded treewidth.

Examples

Example 1

Input

5
1 2 3 4 5
1 2
1 3
3 4
3 5

Output

11

Explanation: The tree structure: - Node 1 connects to 2 and 3 - Node 3 connects to 4 and 5 Choosing nodes 2, 4, and 5 gives weights 2 + 4 + 5 = 11. No two chosen nodes share an edge, and 11 is the largest possible sum.

Example 2

Input

4
-1 -2 -3 -4
1 2
2 3
3 4

Output

0

Explanation: All weights are negative. Selecting any node would decrease the sum, so the optimal independent set is empty, yielding a sum of 0.

Example 3

Input

6
10 -5 20 -1 5 3
1 2
1 3
2 4
2 5
3 6

Output

17

Explanation: Tree layout: - 1 connects to 2 and 3 - 2 connects to 4 and 5 - 3 connects to 6 Choosing nodes 1, 4, 5, and 6 gives 10 + (−1) + 5 + 3 = 17. This set is independent and yields the maximum sum.

Example 4

Input

3
5 5 5
1 2
1 3

Output

10

Explanation: The tree has node 1 connected to nodes 2 and 3. Selecting nodes 2 and 3 gives 5 + 5 = 10, which is the largest sum without selecting node 1.

Constraints

  • 1 ≤ N ≤ 10^5
  • −10^9 ≤ w_i ≤ 10^9 for all i
  • The graph is a tree: it is connected and has exactly N−1 edges
  • All input values are integers
  • The answer fits in a 64‑bit signed integer

Optimal Approach & Strategy

Perform a DFS and compute two DP values per node (include/exclude) using the recurrence dp1 = w + Σ child.dp0, dp0 = Σ max(child.dp0, child.dp1).

Brute Force Approach

Enumerate every subset of nodes, check if it is independent, and compute its weight sum; keep the maximum.

Verified Code Solutions

JavaScript Solution
Time: O(N)
function solution(grid) {
   const N = grid.length;
   const dp = Array(N).fill(0).map(() => Array(N).fill(0));
   for (let i = 0; i < N; i++) {
       for (let j = 0; j < N; j++) {
           if (i === 0 && j === 0) {
               dp[i][j] = grid[i][j];
           } else if (i === 0) {
               dp[i][j] = Math.max(dp[i][j-1], grid[i][j]);
           } else if (j === 0) {
               dp[i][j] = Math.max(dp[i-1][j], grid[i][j]);
           } else {
               dp[i][j] = Math.max(Math.max(dp[i-1][j], dp[i][j-1]), dp[i-1][j-1]) + grid[i][j];
           }
       }
   }
   return dp[N-1][N-1];
}

Asked in Top Tech Interviews

SwiggyMeta

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.