Skip to content

Commit ccc1b6a

Browse files
committed
feat: add swift implementation to lcp problem: No.18
1 parent 84cd947 commit ccc1b6a

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Solution {
2+
func breakfastNumber(_ staple: [Int], _ drinks: [Int], _ x: Int) -> Int {
3+
let mod = 1000000007
4+
var result = 0
5+
let sortedDrinks = drinks.sorted()
6+
7+
for s in staple {
8+
let remaining = x - s
9+
if remaining >= sortedDrinks.first ?? 0 {
10+
var left = 0
11+
var right = sortedDrinks.count - 1
12+
13+
while left < right {
14+
let mid = (left + right + 1) / 2
15+
if sortedDrinks[mid] <= remaining {
16+
left = mid
17+
} else {
18+
right = mid - 1
19+
}
20+
}
21+
result = (result + left + 1) % mod
22+
}
23+
}
24+
return result
25+
}
26+
}

0 commit comments

Comments
 (0)