Skip to content

Commit d12ee96

Browse files
committed
Add the solution of 561. Array Partition I
1 parent a4db329 commit d12ee96

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@
277277
[Meeting Rooms II](https://leetcode.com/problems/meeting-rooms-ii/)| [Swift](./Sort/MeetingRoomsII.swift)| Medium| O(nlogn)| O(n)|
278278
[Merge Intervals](https://leetcode.com/problems/merge-intervals/)| [Swift](./Sort/MergeIntervals.swift)| Hard| O(nlogn)| O(n)|
279279
[Alien Dictionary](https://leetcode.com/problems/alien-dictionary/)| [Swift](./Sort/AlienDictionary.swift)| Hard| O(nm)| O(nm)|
280+
[Array Partition I](https://leetcode.com/problems/array-partition-i/description/)| [Swift](./Sort/ArrayPartitionI.swift)|Easy| O(nlogn)| O(n)|
280281

281282
## Union Find
282283
| Title | Solution | Difficulty | Time | Space |

Sort/ArrayPartitionI.swift

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution {
2+
func arrayPairSum(_ nums: [Int]) -> Int {
3+
var arr = nums
4+
arr = arr.sorted { $0 < $1 }
5+
var res = 0
6+
for i in 0 ..< arr.count {
7+
if i & 1 != 0 {
8+
res += min(arr[i], arr[i - 1])
9+
}
10+
}
11+
return res
12+
}
13+
}

0 commit comments

Comments
 (0)