Leetcode Problem 2646. Minimize the Total Price of the Trips
2646. Minimize the Total Price of the Trips
Leetcode Solutions
Dynamic Programming with Tree Traversal
Create a graph representation of the tree from the edges list.
Traverse all trips using DFS or BFS to calculate the total number of visits to each node.
Multiply the price of each node by the number of visits to get the total contribution before halving.
Use dynamic programming to find the maximum contribution we can get by halving the prices of some non-adjacent nodes.
The state of the DP is defined by the current node and whether we have halved the price of the parent node.
For each node, we have two choices: either halve the price of the current node or not. If we halve the price, we cannot halve the price of its children. If we do not halve the price, we can choose to halve or not halve the price of its children.
The base case is when we reach a leaf node.
The DP transition is to take the maximum of halving or not halving the price for each node.
The answer is the total contribution minus half of the maximum contribution from halving.