leetcode解题之 Combination Sum java 版(组合求和)

39. Combination Sum

Given a set of candidate numbers (C)(without duplicates) and a target number (T), find all unique combinations inC where the candidate numbers sums to T.

The same repeated number may be chosen fromC unlimited number of times.

Note:

  • All numbers (including target) will be positive integers.
  • The solution set must not contain duplicate combinations.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
题目描述: 给定一个由 n 个节点组成的树,每个节点都有一个权值。定义一个节点的权值为其子树中所有节点的权值之和。请你返回所有满足下列条件的节点的权值之和: 该节点位于树的重心以上,即如果将该节点删除后,剩余各个连通分量的节点权值最大值最小。 如果节点数为偶数,则要求上述节点在剩余节点中,左右两部分节点数目相同;如果节点数为奇数,则要求左部分节点数目比右部分节点数目多一。 示例 1: 输入:edges = [[1,2],[1,3],[1,4],[4,5]], weight = [2,3,1,4] 输出:15 解释:树上的节点权值为 [,2,3,1,4] 。重心是节点 1 ,删除后为两个子树 [2,3,4] 和 [5] 。剩余节点权值分别为 9 和 4,均最小化。 示例 2: 输入:edges = [[1,2],[2,3],[3,4],[4,5],[5,6],[6,7],[7,8],[8,9]], weight = [1,2,3,4,5,6,7,8,9] 输出:31 解释:树上的节点权值为 [,1,2,3,4,5,6,7,8,9] 。重心是节点 5 ,删除后为两个子树 [1,2,3,4,6,7,8,9] 和 [] 。剩余节点权值分别为 33 和 ,均最小化。 提示: 1 <= n <= 10^5 edges.length == n - 1 edges[i].length == 2 1 <= edges[i][], edges[i][1] <= n 1 <= weight.length <= n 1 <= weight[i] <= 10^5 解题思路: 题目要求我们找到树的重心,然后删除重心以上的节点,使得剩余各个连通分量的节点权值最大值最小。 首先,我们需要知道什么是树的重心。树的重心是指树上的一个节点,如果将该节点删除后,剩余各个连通分量的节点数最大值最小,那么这个节点就是树的重心。 我们可以使用两次 DFS 来解决这个问题。第一次 DFS 用来求出树的重心,第二次 DFS 用来计算删除重心以上的节点后,剩余各个连通分量的节点权值之和。 具体来说,我们可以先从任意一个节点开始,进行一次 DFS,求出以该节点为根的子树中的节点数和子树中所有节点的权值之和。然后,我们可以再进行一次 DFS,求出以该节点为根的子树中,删除该节点后,剩余各个连通分量的节点数最大值。我们可以使用一个数组 subsize 来记录每个节点的子树大小,使用一个数组 sum 来记录每个节点的子树中所有节点的权值之和。我们可以使用一个变量 ans 来记录删除重心以上的节点后,剩余各个连通分量的节点权值之和的最小值。 在第一次 DFS 中,我们可以使用一个变量 maxsubsize 来记录以当前节点为根的子树中,最大的子树大小。我们可以使用一个变量 totsize 来记录以当前节点为根的子树中,所有节点的总数。我们可以使用一个变量 cursum 来记录以当前节点为根的子树中,所有节点的权值之和。我们可以使用一个变量 curans 来记录删除当前节点后,剩余各个连通分量的节点数最大值。具体来说,我们可以枚举当前节点的每个子节点,然后递归地计算以该子节点为根的子树中,最大的子树大小。我们可以使用一个变量 cursize 来记录以该子节点为根的子树中,所有节点的总数。我们可以使用一个变量 subsum 来记录以该子节点为根的子树中,所有节点的权值之和。然后,我们可以使用 maxsubsize 来更新以当前节点为根的子树中,最大的子树大小。我们可以使用 totsize 来更新以当前节点为根的子树中,所有节点的总数。我们可以使用 cursum 来更新以当前节点为根的子树中,所有节点的权值之和。最后,我们可以使用一个变量 maxsize 来记录当前节点的父节点到当前节点这条路径上,最大的子树大小。我们可以使用一个变量 parentsize 来记录当前节点的父节点的子树大小。然后,我们可以使用 maxsize 和 totsize - cursize 来计算删除当前节点后,剩余各个连通分量的节点数最大值。最后,我们可以使用 curans 来更新 ans。 在第二次 DFS 中,我们可以使用一个变量 maxsubsize 来记录以当前节点为根的子树中,最大的子树大小。我们可以使用一个变量 totsize 来记录以当前节点为根的子树中,所有节点的总数。我们可以使用一个变量 cursum 来记录以当前节点为根的子树中,所有节点的权值之和。我们可以使用一个变量 parentsize 来记录当前节点的父节点的子树大小。具体来说,我们可以枚举当前节点的每个子节点,然后递归地计算以该子节点为根的子树中,最大的子树大小。我们可以使用一个变量 cursize 来记录以该子节点为根的子树中,所有节点的总数。我们可以使用一个变量 subsum 来记录以该子节点为根的子树中,所有节点的权值之和。然后,我们可以使用 maxsubsize 来更新以当前节点为根的子树中,最大的子树大小。我们可以使用 totsize 来更新以当前节点为根的子树中,所有节点的总数。我们可以使用 cursum 来更新以当前节点为根的子树中,所有节点的权值之和。最后,我们可以使用 parentsize 和 totsize - cursize 来计算删除当前节点后,剩余各个连通分量的节点数最大值。如果当前节点不是树的重心,那么我们可以使用 ans 来更新剩余各个连通分量的节点权值之和的最小值。 最后,我们可以返回 ans。 Java 代码: class Solution { int[] subsize; int[] sum; int ans = Integer.MAX_VALUE; public int getCenter(int[][] edges, int[] weight) { int n = weight.length; subsize = new int[n]; sum = new int[n]; dfs1(, -1, edges, weight); dfs2(, -1, edges, weight); return ans; } private void dfs1(int u, int p, int[][] edges, int[] weight) { subsize[u] = 1; sum[u] = weight[u]; int maxsubsize = ; int totsize = 1; int cursum = weight[u]; int curans = ; for (int v : edges[u]) { if (v == p) { continue; } dfs1(v, u, edges, weight); int cursize = subsize[v]; int subsum = sum[v]; subsize[u] += cursize; sum[u] += subsum; maxsubsize = Math.max(maxsubsize, cursize); totsize += cursize; cursum += subsum; int maxsize = Math.max(cursize, subsize[u] - cursize); int parentsize = totsize - cursize; curans = Math.max(curans, Math.min(maxsize, parentsize)); } int maxsize = Math.max(maxsubsize, totsize - maxsubsize); if (maxsize < ans) { ans = maxsize; } } private void dfs2(int u, int p, int[][] edges, int[] weight) { subsize[u] = 1; sum[u] = weight[u]; int maxsubsize = ; int totsize = 1; int cursum = weight[u]; int parentsize = p == -1 ? : subsize[p]; for (int v : edges[u]) { if (v == p) { continue; } int cursize = subsize[v]; int subsum = sum[v]; subsize[u] += cursize; sum[u] += subsum; maxsubsize = Math.max(maxsubsize, cursize); totsize += cursize; cursum += subsum; int maxsize = Math.max(cursize, subsize[u] - cursize); int childsize = totsize - cursize; int curans = Math.max(Math.min(maxsize, parentsize + childsize), Math.min(subsize[v], totsize - subsize[v])); if (curans < ans) { ans = curans; } dfs2(v, u, edges, weight); } } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值