Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a245b03

Browse files
feat: add swift implementation to lcof2 problem: No.073 (doocs#3306)
1 parent 21bf14e commit a245b03

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

โ€Žlcof2/ๅ‰‘ๆŒ‡ Offer II 073. ็‹’็‹’ๅƒ้ฆ™่•‰/README.mdโ€Ž

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,34 @@ public class Solution {
182182
}
183183
```
184184

185+
#### Swift
186+
187+
```swift
188+
class Solution {
189+
func minEatingSpeed(_ piles: [Int], _ h: Int) -> Int {
190+
var left = 1
191+
var right = piles.max() ?? 0
192+
193+
while left < right {
194+
let mid = (left + right) / 2
195+
var hours = 0
196+
197+
for pile in piles {
198+
hours += (pile + mid - 1) / mid
199+
}
200+
201+
if hours <= h {
202+
right = mid
203+
} else {
204+
left = mid + 1
205+
}
206+
}
207+
208+
return left
209+
}
210+
}
211+
```
212+
185213
<!-- tabs:end -->
186214

187215
<!-- solution:end -->
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
func minEatingSpeed(_ piles: [Int], _ h: Int) -> Int {
3+
var left = 1
4+
var right = piles.max() ?? 0
5+
6+
while left < right {
7+
let mid = (left + right) / 2
8+
var hours = 0
9+
10+
for pile in piles {
11+
hours += (pile + mid - 1) / mid
12+
}
13+
14+
if hours <= h {
15+
right = mid
16+
} else {
17+
left = mid + 1
18+
}
19+
}
20+
21+
return left
22+
}
23+
}

0 commit comments

Comments
(0)

AltStyle ใซใ‚ˆใฃใฆๅค‰ๆ›ใ•ใ‚ŒใŸใƒšใƒผใ‚ธ (->ใ‚ชใƒชใ‚ธใƒŠใƒซ) /