Skip to content

Commit 0fca351

Browse files
committed
feat: 0875.Koko-Eating-Bananas
1 parent 9f2ab52 commit 0fca351

File tree

3 files changed

+209
-0
lines changed

3 files changed

+209
-0
lines changed

Diff for: Leetcode/0875.Koko-Eating-Bananas/README.md

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
---
2+
title: 875. Koko Eating Bananas
3+
subtitle: "https://leetcode.com/problems/koko-eating-bananas/description/"
4+
date: 2024-03-28T18:03:00+08:00
5+
lastmod: 2024-03-28T18:03:00+08:00
6+
draft: false
7+
author: "Kimi.Tsai"
8+
authorLink: "https://kimi0230.github.io/"
9+
description: "0875.Koko-Eating-Bananas"
10+
license: ""
11+
images: []
12+
13+
tags: [LeetCode, Go, Medium, Koko Eating Bananas, Array, Binary Search]
14+
categories: [LeetCode]
15+
16+
featuredImage: ""
17+
featuredImagePreview: ""
18+
19+
hiddenFromHomePage: false
20+
hiddenFromSearch: false
21+
twemoji: false
22+
lightgallery: true
23+
ruby: true
24+
fraction: true
25+
fontawesome: true
26+
linkToMarkdown: false
27+
rssFullText: false
28+
29+
toc:
30+
enable: true
31+
auto: true
32+
code:
33+
copy: true
34+
maxShownLines: 200
35+
math:
36+
enable: false
37+
# ...
38+
mapbox:
39+
# ...
40+
share:
41+
enable: true
42+
# ...
43+
comment:
44+
enable: true
45+
# ...
46+
library:
47+
css:
48+
# someCSS = "some.css"
49+
# located in "assets/"
50+
# Or
51+
# someCSS = "https://cdn.example.com/some.css"
52+
js:
53+
# someJS = "some.js"
54+
# located in "assets/"
55+
# Or
56+
# someJS = "https://cdn.example.com/some.js"
57+
seo:
58+
images: []
59+
# ...
60+
---
61+
# [875. Koko Eating Bananas](https://leetcode.com/problems/koko-eating-bananas/description/)
62+
63+
## 題目
64+
65+
## 題目大意
66+
67+
68+
## 解題思路
69+
70+
## Big O
71+
72+
* 時間複雜 : `O(n log m)`,其中 n 是香蕉堆的數量,m 是香蕉堆中香蕉數量的最大值
73+
* 空間複雜 : `O(1)`
74+
75+
## 來源
76+
* https://leetcode.com/problems/koko-eating-bananas/description/
77+
* https://leetcode.cn/problems/koko-eating-bananas/description/
78+
79+
## 解答
80+
https://github.com/kimi0230/LeetcodeGolang/blob/master/Leetcode/0875.Koko-Eating-Bananas/main.go
81+
82+
```go
83+
package kokoeatingbananas
84+
85+
// 時間複雜 O(n log m), 空間複雜 O(1)
86+
func minEatingSpeed(piles []int, h int) int {
87+
// 找出最大的香蕉堆
88+
left, right := 1, maxPile(piles)
89+
for left < right {
90+
// mid 代表消耗香蕉的速度(k)
91+
mid := int(uint(left+right) >> 1)
92+
if executeTime(piles, mid) <= h {
93+
right = mid
94+
} else {
95+
left = mid + 1
96+
}
97+
}
98+
return left
99+
}
100+
101+
// 假設消耗速度k, 算出要花多少時間
102+
func executeTime(piles []int, k int) int {
103+
time := 0
104+
for _, pile := range piles {
105+
time += pile / k
106+
if pile%k > 0 {
107+
time++
108+
}
109+
// time += int(math.Ceil(float64(pile) / float64(k)))
110+
}
111+
return time
112+
}
113+
114+
func maxPile(piles []int) int {
115+
result := 0
116+
for _, pile := range piles {
117+
if result < pile {
118+
result = pile
119+
}
120+
}
121+
return result
122+
}
123+
124+
```
125+
126+
## Benchmark
127+
128+
```sh
129+
130+
```

Diff for: Leetcode/0875.Koko-Eating-Bananas/main.go

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package kokoeatingbananas
2+
3+
// 時間複雜 O(n log m), 空間複雜 O(1)
4+
func minEatingSpeed(piles []int, h int) int {
5+
// 找出最大的香蕉堆
6+
left, right := 1, maxPile(piles)
7+
for left < right {
8+
// mid 代表消耗香蕉的速度(k)
9+
mid := int(uint(left+right) >> 1)
10+
if executeTime(piles, mid) <= h {
11+
right = mid
12+
} else {
13+
left = mid + 1
14+
}
15+
}
16+
return left
17+
}
18+
19+
// 假設消耗速度k, 算出要花多少時間
20+
func executeTime(piles []int, k int) int {
21+
time := 0
22+
for _, pile := range piles {
23+
time += pile / k
24+
if pile%k > 0 {
25+
time++
26+
}
27+
// time += int(math.Ceil(float64(pile) / float64(k)))
28+
}
29+
return time
30+
}
31+
32+
func maxPile(piles []int) int {
33+
result := 0
34+
for _, pile := range piles {
35+
if result < pile {
36+
result = pile
37+
}
38+
}
39+
return result
40+
}

Diff for: Leetcode/0875.Koko-Eating-Bananas/main_test.go

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package kokoeatingbananas
2+
3+
import "testing"
4+
5+
var tests = []struct {
6+
piles []int
7+
h int
8+
want int
9+
}{
10+
{
11+
[]int{3, 6, 7, 11},
12+
8,
13+
4,
14+
},
15+
{
16+
[]int{30, 11, 23, 4, 20},
17+
5,
18+
30,
19+
},
20+
{
21+
[]int{30, 11, 23, 4, 20},
22+
6,
23+
23,
24+
},
25+
}
26+
27+
func TestMinEatingSpeed(t *testing.T) {
28+
for _, tt := range tests {
29+
if got := minEatingSpeed(tt.piles, tt.h); got != tt.want {
30+
t.Errorf("minEatingSpeed(%v, %v) = %v, want %v", tt.piles, tt.h, got, tt.want)
31+
}
32+
}
33+
}
34+
35+
func BenchmarkMinEatingSpeed(b *testing.B) {
36+
for i := 0; i < b.N; i++ {
37+
minEatingSpeed(tests[0].piles, tests[0].h)
38+
}
39+
}

0 commit comments

Comments
 (0)