diff --git a/README.md b/README.md index 6c438a3a..aeb9e217 100644 --- a/README.md +++ b/README.md @@ -512,7 +512,7 @@ | | 376 | [Wiggle Subsequence](https://leetcode.com/problems/wiggle-subsequence/) | Medium | [Swift](./DP/GuessNumberHigherOrLowerII.swift) | 375 | [Guess Number Higher or Lower II](https://leetcode.com/problems/guess-number-higher-or-lower-ii/) | Medium | | 374 | [Guess Number Higher or Lower](https://leetcode.com/problems/guess-number-higher-or-lower/) | Easy -| | 373 | [Find K Pairs with Smallest Sums](https://leetcode.com/problems/find-k-pairs-with-smallest-sums/) | Medium +|[Swift](./Search/FindKPairsWithSmallestSums.swift.swift)| 373 | [Find K Pairs with Smallest Sums](https://leetcode.com/problems/find-k-pairs-with-smallest-sums/) | Medium | [Swift](./Math/SuperPow.swift) | 372 | [Super Pow](https://leetcode.com/problems/super-pow/) | Medium | [Swift](./Math/SumTwoIntegers.swift) | 371 | [Sum of Two Integers](https://leetcode.com/problems/sum-of-two-integers/) | Easy | | 370 | [Range Addition](https://leetcode.com/problems/range-addition/) ♥ | Medium diff --git a/Search/FindKPairsWithSmallestSums.swift b/Search/FindKPairsWithSmallestSums.swift new file mode 100644 index 00000000..d489769b --- /dev/null +++ b/Search/FindKPairsWithSmallestSums.swift @@ -0,0 +1,25 @@ +func kSmallestPairs(_ nums1: [Int], _ nums2: [Int], _ k: Int) -> [[Int]] { + var combinations: [[Int]] = [] + + for n in nums1 { + for m in nums2 { + combinations.append([n, m, n+m]) + } + } + + let result: [[Int]] = combinations.sorted(by: { + $0[2] < $1[2] + }) + + var end = k + if combinations.count < k { + end = combinations.count + } + + var answer: [[Int]] = [] + + for l in 0..