← All problems

2467. Most Profitable Path in a Tree

MediumOpen on LeetCodeProblem statement

Problem Statement

2467. Most Profitable Path in a Tree

Medium


There is an undirected tree with n nodes labeled from 0 to n - 1, rooted at node 0. You are given a 2D integer array edges of length n - 1 where edges[i] = [ai, bi] indicates that there is an edge between nodes ai and bi in the tree.

At every node i, there is a gate. You are also given an array of even integers amount, where amount[i] represents:

The game goes on as follows:

Return the maximum net income Alice can have if she travels towards the optimal leaf node.

 

Example 1:

Input: edges = [[0,1],[1,2],[1,3],[3,4]], bob = 3, amount = [-2,4,2,-4,6]
Output: 6
Explanation: 
The above diagram represents the given tree. The game goes as follows:
- Alice is initially on node 0, Bob on node 3. They open the gates of their respective nodes.
  Alice's net income is now -2.
- Both Alice and Bob move to node 1. 
  Since they reach here simultaneously, they open the gate together and share the reward.
  Alice's net income becomes -2 + (4 / 2) = 0.
- Alice moves on to node 3. Since Bob already opened its gate, Alice's income remains unchanged.
  Bob moves on to node 0, and stops moving.
- Alice moves on to node 4 and opens the gate there. Her net income becomes 0 + 6 = 6.
Now, neither Alice nor Bob can make any further moves, and the game ends.
It is not possible for Alice to get a higher net income.

Example 2:

Input: edges = [[0,1]], bob = 1, amount = [-7280,2350]
Output: -7280
Explanation: 
Alice follows the path 0->1 whereas Bob follows the path 1->0.
Thus, Alice opens the gate at node 0 only. Hence, her net income is -7280. 

 

Constraints:

Java

Source file
class Solution {
    int maxProfit = Integer.MIN_VALUE;

    public int mostProfitablePath(int[][] edges, int bob, int[] amount) {
        int n = edges.length + 1;
        List<Integer>[] adj = new List[n];
        boolean[] visA = new boolean[n], visB = new boolean[n];
        for (int i = 0; i < n; i++)
            adj[i] = new ArrayList<>();
        for (int[] e : edges) {
            adj[e[0]].add(e[1]);
            adj[e[1]].add(e[0]);
        }
        dfs(adj, amount, visA, visB, 0, bob, 0);
        return maxProfit;
    }

    private void dfs(List<Integer>[] adj, int[] amount, boolean[] visA, boolean[] visB, int a, int b, int profit) {
        System.out.println(Arrays.toString(amount) + profit);
        profit += a == b ? amount[a] / 2 : amount[a];
        if (a != 0 && adj[a].size() == 1) {
            maxProfit = Math.max(maxProfit, profit);
            return;
        }
        int amtA = amount[a], amtB = amount[b];
        amount[a] = 0;
        amount[b] = 0;
        for (int nbrA : adj[a]) {
            if (visA[nbrA])
                continue;
            visA[nbrA] = true;
            if (b == 0) {
                dfs(adj, amount, visA, visB, nbrA, 0, profit);
            } else {
                for (int nbrB : adj[b]) {
                    if (visB[nbrB])
                        continue;
                    visB[nbrB] = true;
                    dfs(adj, amount, visA, visB, nbrA, nbrB, profit);
                }
            }
        }
        amount[a] = amtA;
        amount[b] = amtB;
    }
}