Advertisement

Most Profitable Path in a Tree - LeetCode 2467 Solution

Most Profitable Path in a Tree - Complete Solution Guide

Most Profitable Path in a Tree is LeetCode problem 2467, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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] = [a i , b i ] indicates that there is an edge between nodes a i and b i 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 price needed to open the gate at node i , if amount[i] is negative, or, the cash reward obtained on opening the gate at node i , otherwis

Detailed Explanation

The problem presents an undirected tree where nodes represent locations with associated prices (negative) or rewards (positive). Alice starts at node 0 and wants to maximize her income by moving to a leaf node. Bob starts at a specified node `bob` and moves towards node 0. Both Alice and Bob move simultaneously each second. If they arrive at a node at the same time, they split the price/reward. If Bob arrives before Alice, Alice gets nothing at that node (Bob has already opened the gate). If Alice arrives before Bob, she gets the full price/reward. The goal is to determine the maximum possible net income Alice can achieve by choosing the optimal path to a leaf node.

Solution Approach

The solution involves three main steps: building the adjacency list representation of the tree, using BFS to calculate the distance of each node from the root (Alice's time), and then determining Bob's path from `bob` to 0 and adjusting the `amount` array based on who reaches the node first. Finally, DFS is used to traverse the tree from the root to all leaf nodes, calculating Alice's profit along each path, and identifying the maximum profit she can make.

Step-by-Step Algorithm

  1. Step 1: **Build Adjacency List:** Construct an adjacency list from the input `edges` to represent the tree structure. This allows easy access to neighboring nodes.
  2. Step 2: **BFS for Alice's Time and Parent Pointers:** Perform a BFS from node 0. Calculate `dist_from_0[i]` for each node `i`, which stores the distance from node 0. This represents the time Alice takes to reach node `i`. Also, construct the `parent` array which stores the parent node of each node. This is crucial for reconstructing Bob's path.
  3. Step 3: **Determine Bob's Path and Adjust Amounts:** Trace Bob's path back to node 0 using the `parent` array. For each node on Bob's path, compare his arrival time (`bob_time`) with Alice's arrival time (`alice_time`). If `bob_time < alice_time`, Alice receives nothing (amount[curr] = 0). If `bob_time == alice_time`, the reward/price is divided (amount[curr] /= 2).
  4. Step 4: **DFS for Maximum Profit:** Perform a DFS starting from node 0 to explore all paths to leaf nodes. At each node, add the adjusted `amount[u]` to the current profit. When a leaf node is reached, update `max_profit` if the current profit is greater.

Key Insights

  • Insight 1: Represent the tree as an adjacency list to efficiently traverse its structure.
  • Insight 2: Calculate the distance of each node from the root (node 0) using Breadth-First Search (BFS). This represents Alice's arrival time at each node.
  • Insight 3: Determine the path Bob takes to reach the root and the time he takes to reach each node on his path. Compare Bob's arrival time with Alice's arrival time to determine how the price/reward at each node should be divided or assigned.
  • Insight 4: Use Depth-First Search (DFS) to explore all possible paths from the root to leaf nodes and compute Alice's net income for each path. Keep track of the maximum income found so far.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(n)

Topics

This problem involves: Array, Tree, Depth-First Search, Breadth-First Search, Graph.

Companies

Asked at: Intuit, Snowflake.