Skip to content

Commit

Permalink
feat: add swift implementation to lcp problem: No.28 (#3775)
Browse files Browse the repository at this point in the history
  • Loading branch information
klever34 authored Nov 18, 2024
1 parent 038e542 commit 9430a95
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
25 changes: 25 additions & 0 deletions lcp/LCP 28. 采购方案/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 -->
Expand Down
20 changes: 20 additions & 0 deletions lcp/LCP 28. 采购方案/Solution.swift
Original file line number Diff line number Diff line change
@@ -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
}
}

0 comments on commit 9430a95

Please sign in to comment.