feat: add swift implementation to lcp problem: No.28 (#3775)

This commit is contained in:
Lanre Adedara 2024-11-18 09:32:32 +01:00 committed by GitHub
parent 038e542aeb
commit 9430a95483
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 45 additions and 0 deletions

View File

@ -123,6 +123,31 @@ func purchasePlans(nums []int, target int) int {
}
```
#### Swift
```swift
class Solution {
func purchasePlans(_ nums: [Int], _ target: Int) -> Int {
let mod = 1_000_000_007
let nums = nums.sorted()
var res = 0
var i = 0
var j = nums.count - 1
while i < j {
if nums[i] + nums[j] > target {
j -= 1
} else {
res = (res + j - i) % mod
i += 1
}
}
return res
}
}
```
<!-- tabs:end -->
<!-- solution:end -->

View File

@ -0,0 +1,20 @@
class Solution {
func purchasePlans(_ nums: [Int], _ target: Int) -> Int {
let mod = 1_000_000_007
let nums = nums.sorted()
var res = 0
var i = 0
var j = nums.count - 1
while i < j {
if nums[i] + nums[j] > target {
j -= 1
} else {
res = (res + j - i) % mod
i += 1
}
}
return res
}
}