2920. Maximum Points After Collecting Coins From All Nodes Hard
1/**
2 * [2920] Maximum Points After Collecting Coins From All Nodes
3 *
4 * There exists an undirected tree rooted at node 0 with n nodes labeled from 0 to n - 1. 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. You are also given a 0-indexed array coins of size n where coins[i] indicates the number of coins in the vertex i, and an integer k.
5 * Starting from the root, you have to collect all the coins such that the coins at a node can only be collected if the coins of its ancestors have been already collected.
6 * Coins at nodei can be collected in one of the following ways:
7 *
8 * Collect all the coins, but you will get coins[i] - k points. If coins[i] - k is negative then you will lose abs(coins[i] - k) points.
9 * Collect all the coins, but you will get floor(coins[i] / 2) points. If this way is used, then for all the nodej present in the subtree of nodei, coins[j] will get reduced to floor(coins[j] / 2).
10 *
11 * Return the maximum points you can get after collecting the coins from all the tree nodes.
12 *
13 * <strong class="example">Example 1:
14 * <img alt="" src="https://assets.leetcode.com/uploads/2023/09/18/ex1-copy.png" style="width: 60px; height: 316px; padding: 10px; background: rgb(255, 255, 255); border-radius: 0.5rem;" />
15 * Input: edges = [[0,1],[1,2],[2,3]], coins = [10,10,3,3], k = 5
16 * Output: 11
17 * Explanation:
18 * Collect all the coins from node 0 using the first way. Total points = 10 - 5 = 5.
19 * Collect all the coins from node 1 using the first way. Total points = 5 + (10 - 5) = 10.
20 * Collect all the coins from node 2 using the second way so coins left at node 3 will be floor(3 / 2) = 1. Total points = 10 + floor(3 / 2) = 11.
21 * Collect all the coins from node 3 using the second way. Total points = 11 + floor(1 / 2) = 11.
22 * It can be shown that the maximum points we can get after collecting coins from all the nodes is 11.
23 *
24 * <strong class="example">Example 2:
25 * <strong class="example"> <img alt="" src="https://assets.leetcode.com/uploads/2023/09/18/ex2.png" style="width: 140px; height: 147px; padding: 10px; background: #fff; border-radius: .5rem;" />
26 *
27 * Input: edges = [[0,1],[0,2]], coins = [8,4,4], k = 0
28 * Output: 16
29 * Explanation:
30 * Coins will be collected from all the nodes using the first way. Therefore, total points = (8 - 0) + (4 - 0) + (4 - 0) = 16.
31 *
32 *
33 * Constraints:
34 *
35 * n == coins.length
36 * 2 <= n <= 10^5
37 * <font face="monospace">0 <= coins[i] <= 10^4</font>
38 * edges.length == n - 1
39 * <font face="monospace">0 <= edges[i][0], edges[i][1] < n</font>
40 * <font face="monospace">0 <= k <= 10^4</font>
41 *
42 */
43pub struct Solution {}
44
45// problem: https://leetcode.com/problems/maximum-points-after-collecting-coins-from-all-nodes/
46// discuss: https://leetcode.com/problems/maximum-points-after-collecting-coins-from-all-nodes/discuss/?currentPage=1&orderBy=most_votes&query=
47
48// submission codes start here
49
50impl Solution {
51 pub fn maximum_points(edges: Vec<Vec<i32>>, coins: Vec<i32>, k: i32) -> i32 {
52 0
53 }
54}
55
56// submission codes end
57
58#[cfg(test)]
59mod tests {
60 use super::*;
61
62 #[test]
63 fn test_2920() {
64 }
65}
66
Back
© 2025 bowen.ge All Rights Reserved.