Skip to content

Commit e81e547

Browse files
committed
Array partition.
1 parent 19380a6 commit e81e547

File tree

8 files changed

+121
-2
lines changed

8 files changed

+121
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
Given an array of distinct integers candidates and a target integer target, return a list of all unique combinations of candidates where the chosen numbers sum to target. You may return the combinations in any order.
3+
4+
The same number may be chosen from candidates an unlimited number of times. Two combinations are unique if the frequency of at least one of the chosen numbers is different.
5+
6+
It is guaranteed that the number of unique combinations that sum up to target is less than 150 combinations for the given input.
7+
8+
 
9+
10+
Example 1:
11+
Input: candidates = [2,3,6,7], target = 7
12+
Output: [[2,2,3],[7]]
13+
Explanation:
14+
2 and 3 are candidates, and 2 + 2 + 3 = 7. Note that 2 can be used multiple times.
15+
7 is a candidate, and 7 = 7.
16+
These are the only two combinations.
17+
18+
Example 2:
19+
Input: candidates = [2,3,5], target = 8
20+
Output: [[2,2,2,2],[2,3,3],[3,5]]
21+
22+
Example 3:
23+
Input: candidates = [2], target = 1
24+
Output: []
25+
 
26+
27+
Constraints:
28+
- 1 <= candidates.length <= 30
29+
- 1 <= candidates[i] <= 200
30+
- All elements of candidates are distinct.
31+
- 1 <= target <= 500
32+
*/
33+
class Solution {
34+
func combinationSum(_ candidates: [Int], _ target: Int) -> [[Int]] {
35+
let sortedCandidates = candidates.sorted(by: <)
36+
var result = [[Int]]()
37+
for element in candidates {
38+
if element == target {
39+
result.append([target])
40+
break
41+
}
42+
if element > target {
43+
break
44+
}
45+
if target % element == 0 {
46+
let times = target / element
47+
var arr = [Int]()
48+
for i in 0..<times {
49+
arr.append(element)
50+
}
51+
result.append(arr)
52+
}
53+
}
54+
return result
55+
}
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

Easy/39.Combination Sum.playground/playground.xcworkspace/contents.xcworkspacedata

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
Given an integer array nums of 2n integers, group these integers into n pairs (a1, b1), (a2, b2), ..., (an, bn) such that the sum of min(ai, bi) for all i is maximized. Return the maximized sum.
3+
4+
 
5+
6+
Example 1:
7+
Input: nums = [1,4,3,2]
8+
Output: 4
9+
Explanation: All possible pairings (ignoring the ordering of elements) are:
10+
1. (1, 4), (2, 3) -> min(1, 4) + min(2, 3) = 1 + 2 = 3
11+
2. (1, 3), (2, 4) -> min(1, 3) + min(2, 4) = 1 + 2 = 3
12+
3. (1, 2), (3, 4) -> min(1, 2) + min(3, 4) = 1 + 3 = 4
13+
So the maximum possible sum is 4.
14+
15+
Example 2:
16+
Input: nums = [6,2,6,5,1,2]
17+
Output: 9
18+
Explanation: The optimal pairing is (2, 1), (2, 5), (6, 6). min(2, 1) + min(2, 5) + min(6, 6) = 1 + 2 + 6 = 9.
19+
 
20+
21+
Constraints:
22+
- 1 <= n <= 104
23+
- nums.length == 2 * n
24+
- -104 <= nums[i] <= 104
25+
*/
26+
class Solution {
27+
func arrayPairSum(_ nums: [Int]) -> Int {
28+
let sortedNums = nums.sorted(by: <)
29+
var i = 0
30+
var sum = 0
31+
while i <= nums.count - 2 {
32+
sum += sortedNums[i]
33+
i += 2
34+
}
35+
return sum
36+
}
37+
}
38+
39+
let s = Solution()
40+
let r = s.arrayPairSum([6,2,6,5,1,2])
41+
print(r)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

Easy/561.Array Partition.playground/playground.xcworkspace/contents.xcworkspacedata

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Medium/38.Count and Say.playground/Contents.swift

+1-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
*/
3333
class Solution {
3434
func countAndSay(_ n: Int) -> String {
35-
if n == 1 { return "1" }
3635
var sumArr = ["1"]
3736
var count = 0
3837
var number = Character("1")
@@ -65,5 +64,5 @@ class Solution {
6564
}
6665

6766
let s = Solution()
68-
let r = s.countAndSay(30)
67+
let r = s.countAndSay(1)
6968
print(r)

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@
110110
106. [Student Attendance Record I](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/551.Student%20Attendance%20Record%20I.playground/Contents.swift)
111111
107. [Reverse Words in a String III](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/557.Reverse%20Words%20in%20a%20String%20III.playground/Contents.swift)
112112
108. [Maximum Depth of N-ary Tree](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/559.Maximum%20Depth%20of%20N-ary%20Tree.playground/Contents.swift)
113+
109. [Array Partition](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/561.Array%20Partition.playground/Contents.swift)
113114

114115
#### Medium
115116

0 commit comments

Comments
 (0)