Skip to content

Solve 373 #265

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FindKPairsWithSmallestSums.swift.swift?

| [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
Expand Down
25 changes: 25 additions & 0 deletions Search/FindKPairsWithSmallestSums.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
func kSmallestPairs(_ nums1: [Int], _ nums2: [Int], _ k: Int) -> [[Int]] {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add the question link and your idea to solve the question? You could reference other swift files for details.

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..<end {
answer.append([result[l][0], result[l][1]])
}
return answer
}