diff --git "a/lcp/LCP 28. \351\207\207\350\264\255\346\226\271\346\241\210/README.md" "b/lcp/LCP 28. \351\207\207\350\264\255\346\226\271\346\241\210/README.md" index db74c352935f0..badc772c450f3 100644 --- "a/lcp/LCP 28. \351\207\207\350\264\255\346\226\271\346\241\210/README.md" +++ "b/lcp/LCP 28. \351\207\207\350\264\255\346\226\271\346\241\210/README.md" @@ -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 + } +} +``` + diff --git "a/lcp/LCP 28. \351\207\207\350\264\255\346\226\271\346\241\210/Solution.swift" "b/lcp/LCP 28. \351\207\207\350\264\255\346\226\271\346\241\210/Solution.swift" new file mode 100644 index 0000000000000..73add5edd350f --- /dev/null +++ "b/lcp/LCP 28. \351\207\207\350\264\255\346\226\271\346\241\210/Solution.swift" @@ -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 + } +} \ No newline at end of file