From ccc1b6a43d77ffa084ab5d0b322c8b168195a90d Mon Sep 17 00:00:00 2001 From: Lanre Adedara Date: Tue, 12 Nov 2024 07:49:03 +0100 Subject: [PATCH 1/3] feat: add swift implementation to lcp problem: No.18 --- .../Solution.swift" | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 "lcp/LCP 18. \346\227\251\351\244\220\347\273\204\345\220\210/Solution.swift" diff --git "a/lcp/LCP 18. \346\227\251\351\244\220\347\273\204\345\220\210/Solution.swift" "b/lcp/LCP 18. \346\227\251\351\244\220\347\273\204\345\220\210/Solution.swift" new file mode 100644 index 0000000000000..f126c14fafca0 --- /dev/null +++ "b/lcp/LCP 18. \346\227\251\351\244\220\347\273\204\345\220\210/Solution.swift" @@ -0,0 +1,26 @@ +class Solution { + func breakfastNumber(_ staple: [Int], _ drinks: [Int], _ x: Int) -> Int { + let mod = 1000000007 + var result = 0 + let sortedDrinks = drinks.sorted() + + for s in staple { + let remaining = x - s + if remaining >= sortedDrinks.first ?? 0 { + var left = 0 + var right = sortedDrinks.count - 1 + + while left < right { + let mid = (left + right + 1) / 2 + if sortedDrinks[mid] <= remaining { + left = mid + } else { + right = mid - 1 + } + } + result = (result + left + 1) % mod + } + } + return result + } +} \ No newline at end of file From 352577a17f75aea8c312bbd5b02a597b6a0571dd Mon Sep 17 00:00:00 2001 From: Lanre Adedara Date: Tue, 12 Nov 2024 07:49:47 +0100 Subject: [PATCH 2/3] feat: add swift implementation to lcp problem: No.18 --- .../README.md" | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git "a/lcp/LCP 18. \346\227\251\351\244\220\347\273\204\345\220\210/README.md" "b/lcp/LCP 18. \346\227\251\351\244\220\347\273\204\345\220\210/README.md" index c04eb85dcb523..ad73635384d40 100644 --- "a/lcp/LCP 18. \346\227\251\351\244\220\347\273\204\345\220\210/README.md" +++ "b/lcp/LCP 18. \346\227\251\351\244\220\347\273\204\345\220\210/README.md" @@ -187,6 +187,37 @@ func breakfastNumber(staple []int, drinks []int, x int) int { } ``` +#### Swift + +```swift +class Solution { + func breakfastNumber(_ staple: [Int], _ drinks: [Int], _ x: Int) -> Int { + let mod = 1000000007 + var result = 0 + let sortedDrinks = drinks.sorted() + + for s in staple { + let remaining = x - s + if remaining >= sortedDrinks.first ?? 0 { + var left = 0 + var right = sortedDrinks.count - 1 + + while left < right { + let mid = (left + right + 1) / 2 + if sortedDrinks[mid] <= remaining { + left = mid + } else { + right = mid - 1 + } + } + result = (result + left + 1) % mod + } + } + return result + } +} +``` + From 7c52a5fa9ef1d7077ea9eedb242e8e930006cb78 Mon Sep 17 00:00:00 2001 From: klever34 Date: Tue, 12 Nov 2024 06:52:10 +0000 Subject: [PATCH 3/3] style: format code and docs with prettier --- .../README.md" | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git "a/lcp/LCP 18. \346\227\251\351\244\220\347\273\204\345\220\210/README.md" "b/lcp/LCP 18. \346\227\251\351\244\220\347\273\204\345\220\210/README.md" index ad73635384d40..f57903fb90c8f 100644 --- "a/lcp/LCP 18. \346\227\251\351\244\220\347\273\204\345\220\210/README.md" +++ "b/lcp/LCP 18. \346\227\251\351\244\220\347\273\204\345\220\210/README.md" @@ -195,13 +195,13 @@ class Solution { let mod = 1000000007 var result = 0 let sortedDrinks = drinks.sorted() - + for s in staple { let remaining = x - s if remaining >= sortedDrinks.first ?? 0 { var left = 0 var right = sortedDrinks.count - 1 - + while left < right { let mid = (left + right + 1) / 2 if sortedDrinks[mid] <= remaining {