Minimize the Total Price of the Trips — LeetCode 2646 Python Solution

HardTreeDepth-First SearchGraphArrayDynamic Programming
Problem
#2646
Reading time
6 min

The problem

There exists an undirected and unrooted tree with n nodes indexed from 0 to n - 1. You are given the integer n and 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.

Example

Input
n = 4, edges = [[0,1],[1,2],[1,3]], price = [2,2,10,6], trips = [[0,3],[2,1],[2,3]]
Output
23
Explanation
The diagram above denotes the tree after rooting it at node 2. The first part shows the initial tree and the second part shows the tree after choosing nodes 0, 2, and 3, and making their price half.

Python solution

Python
class Solution:
    def minimumTotalPrice(
        self, n: int, edges: List[List[int]], price: List[int], trips: List[List[int]]
    ) -> int:
        def dfs(i: int, fa: int, k: int) -> bool:
            cnt[i] += 1
            if i == k:
                return True
            ok = any(j != fa and dfs(j, i, k) for j in g[i])
            if not ok:
                cnt[i] -= 1
            return ok

        def dfs2(i: int, fa: int) -> (int, int):
            a = cnt[i] * price[i]
            b = a // 2
            for j in g[i]:
                if j != fa:
                    x, y = dfs2(j, i)
                    a += min(x, y)
                    b += x
            return a, b

        g = [[] for _ in range(n)]
        for a, b in edges:
            g[a].append(b)
            g[b].append(a)
        cnt = Counter()
        for start, end in trips:
            dfs(start, -1, end)
        return min(dfs2(0, -1))

Complexity

MeasureComplexity
TimeO(m \times n), where m and n are the lengths of nums and divisors respectively
SpaceO(1) auxiliary

Pattern: Tree Traversal

Choose the order — preorder, inorder, postorder, level — and the problem solves itself. LeetCode 2646. Minimize the Total Price of the Trips is filed here because LeetCode tags it Tree, which is the vocabulary this hub collects.

The tree traversal guide has the Python template for the pattern and the 225 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 2646. Minimize the Total Price of the Trips?
LeetCode 2646. Minimize the Total Price of the Trips is rated Hard on LeetCode.
What is the time complexity of LeetCode 2646. Minimize the Total Price of the Trips?
The Python solution on this page runs in O(m \times n), where m and n are the lengths of nums and divisors respectively.
What is the space complexity of LeetCode 2646. Minimize the Total Price of the Trips?
The Python solution on this page uses O(1) auxiliary space.
What topics does LeetCode 2646. Minimize the Total Price of the Trips cover?
LeetCode 2646. Minimize the Total Price of the Trips is tagged Tree, Depth-First Search, Graph, Array and Dynamic Programming on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview