File tree 3 files changed +85
-3
lines changed
3 files changed +85
-3
lines changed Original file line number Diff line number Diff line change @@ -428,9 +428,10 @@ leetcode 题解,记录自己的 leetcode 解题之路。
428
428
- [ LCP 20. 快速公交] ( ./problems/lcp20.meChtZ.md ) 🆕
429
429
- [ LCP 21. 追逐游戏] ( ./problems/lcp21.Za25hA.md ) 🆕 👍
430
430
- [ Number Stream to Intervals] ( ./problems/Number-Stream-to-Intervals.md ) 🆕
431
- - [ Triple-Inversion] ( ./problems/Triple-Inversion.md ) 91
432
- - [ Kth-Pair-Distance] ( ./problems/Kth-Pair-Distance.md ) 91
433
- - [ Minimum-Light-Radius] ( ./problems/Minimum-Light-Radius.md ) 91
431
+ - [ Triple Inversion] ( ./problems/Triple-Inversion.md ) 91
432
+ - [ Kth Pair Distance] ( ./problems/Kth-Pair-Distance.md ) 91
433
+ - [ Minimum Light Radius] ( ./problems/Minimum-Light-Radius.md ) 91
434
+ - [ Largest Equivalent Set of Pairs] ( ./problems/Largest-Equivalent-Set-of-Pairs.md ) 🆕 👍
434
435
435
436
- [ 0004. 寻找两个正序数组的中位数] ( ./problems/4.median-of-two-sorted-arrays.md ) 👍
436
437
- [ 0023. 合并 K 个升序链表] ( ./problems/23.merge-k-sorted-lists.md )
Original file line number Diff line number Diff line change 256
256
- [ Triple-Inversion] ( ./problems/Triple-Inversion.md ) 91
257
257
- [ Kth-Pair-Distance] ( ./problems/Kth-Pair-Distance.md ) 91
258
258
- [ Minimum-Light-Radius] ( ./problems/Minimum-Light-Radius.md ) 91
259
+ - [ Largest Equivalent Set of Pairs] ( ./problems/Largest-Equivalent-Set-of-Pairs.md ) 🆕 👍
259
260
- [ 0004. 寻找两个正序数组的中位数] ( ./problems/4.median-of-two-sorted-arrays.md )
260
261
- [ 0023. 合并 K 个升序链表] ( ./problems/23.merge-k-sorted-lists.md )
261
262
- [ 0025. K 个一组翻转链表] ( ./problems/25.reverse-nodes-in-k-groups.md )
Original file line number Diff line number Diff line change
1
+ ## 题目地址(483. Largest Equivalent Set of Pairs)
2
+
3
+ https://binarysearch.com/problems/Largest-Equivalent-Set-of-Pairs
4
+
5
+ ## 题目描述
6
+
7
+ ```
8
+ Given a list of integers nums, find two sets such that their sums are equal and is maximized, and return one of the sets' sums.
9
+
10
+ Note that not all integers in nums need to be used and the two sets may be empty. A number cannot be in both of the two sets.
11
+
12
+ Constraints
13
+
14
+ n ≤ 30 where n is the length of nums
15
+ 0 ≤ nums[i] ≤ 100
16
+ Example 1
17
+ Input
18
+ nums = [1, 4, 3, 5]
19
+ Output
20
+ 5
21
+ Explanation
22
+ The two sets are [1, 4] and [5].
23
+ ```
24
+
25
+ ## 前置知识
26
+
27
+ - 动态规划
28
+
29
+ ## 思路
30
+
31
+ 假设题目要求我们找的两个子集分别为 A 和 B。 那么对于一个数来说,我们有三种选择:
32
+
33
+ - 将其加入 A
34
+ - 将其加入 B
35
+ - 既不加入 A,也不加入 B
36
+
37
+ > 不存在既加入 A 又加入 B 的情况。
38
+
39
+ 因此我们要做的就是枚举 nums,对于每个数组执行三种操作。最终枚举完所有的数字之后,如果集合 A 和 集合 B 的和一样的,那么就返回任意一个的和即可。
40
+
41
+ 一个简单的思路是分别维护两个集合的和。实际上,由于我们只关心 A 和 B 的和是否相等,而不关心其具体的值,因此我们可以维护 A 和 B 的差值。当 A 和 B 的差值为 0 的时候,说明 A 和 B 相等。
42
+
43
+ 代码上,我们可以将 A 和 B 的差值 diff 作为参数传进来,而集合 A (或者 B)的和作为返回值。由于我们需要集合 A 的和尽可能大,因此我们可以将上面三种情况的最大值进行返回即可。
44
+
45
+ 大家可以通过** 画递归树** 来直观感受这种算法。
46
+
47
+ ## 代码
48
+
49
+ 代码支持:Python3
50
+
51
+ Python3 Code:
52
+
53
+ ``` py
54
+ class Solution :
55
+ def solve (self , nums ):
56
+ n = len (nums)
57
+
58
+ @lru_cache (None )
59
+ def dp (i , diff ):
60
+ if i == n:
61
+ return 0 if diff == 0 else float (" -inf" )
62
+ return max (
63
+ dp(i + 1 , diff),
64
+ dp(i + 1 , diff - nums[i]),
65
+ dp(i + 1 , diff + nums[i]) + nums[i],
66
+ )
67
+
68
+ return dp(0 , 0 )
69
+ ```
70
+
71
+ ** 复杂度分析**
72
+
73
+ 令 m 为数组长度, n 为最终两个子集的长度的较大者。(因为最坏的情况,我们选取的子集就是较大的)
74
+
75
+ - 时间复杂度:$O(m * n)$
76
+ - 空间复杂度:$O(m * n)$
77
+
78
+ 力扣的小伙伴可以[ 关注我] ( https://leetcode-cn.com/u/fe-lucifer/ ) ,这样就会第一时间收到我的动态啦~
79
+
80
+ 以上就是本文的全部内容了。大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 40K star 啦。大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。
You can’t perform that action at this time.
0 commit comments