mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-05 08:27:30 +08:00
添加 problem 875
This commit is contained in:
@ -0,0 +1,34 @@
|
||||
package leetcode
|
||||
|
||||
import "math"
|
||||
|
||||
func minEatingSpeed(piles []int, H int) int {
|
||||
low, high := 1, maxInArr(piles)
|
||||
for low < high {
|
||||
mid := low + (high-low)>>1
|
||||
if !isPossible(piles, mid, H) {
|
||||
low = mid + 1
|
||||
} else {
|
||||
high = mid
|
||||
}
|
||||
}
|
||||
return low
|
||||
}
|
||||
|
||||
func isPossible(piles []int, h, H int) bool {
|
||||
res := 0
|
||||
for _, p := range piles {
|
||||
res += int(math.Ceil(float64(p) / float64(h)))
|
||||
}
|
||||
return res <= H
|
||||
}
|
||||
|
||||
func maxInArr(xs []int) int {
|
||||
res := 0
|
||||
for _, x := range xs {
|
||||
if res < x {
|
||||
res = x
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question875 struct {
|
||||
para875
|
||||
ans875
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para875 struct {
|
||||
piles []int
|
||||
H int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans875 struct {
|
||||
one int
|
||||
}
|
||||
|
||||
func Test_Problem875(t *testing.T) {
|
||||
|
||||
qs := []question875{
|
||||
|
||||
question875{
|
||||
para875{[]int{3, 6, 7, 11}, 8},
|
||||
ans875{4},
|
||||
},
|
||||
|
||||
question875{
|
||||
para875{[]int{30, 11, 23, 4, 20}, 5},
|
||||
ans875{30},
|
||||
},
|
||||
|
||||
question875{
|
||||
para875{[]int{30, 11, 23, 4, 20}, 6},
|
||||
ans875{23},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 875------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans875, q.para875
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, minEatingSpeed(p.piles, p.H))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
59
Algorithms/0875. Koko Eating Bananas/README.md
Executable file
59
Algorithms/0875. Koko Eating Bananas/README.md
Executable file
@ -0,0 +1,59 @@
|
||||
# [875. Koko Eating Bananas](https://leetcode.com/problems/koko-eating-bananas/)
|
||||
|
||||
|
||||
## 题目:
|
||||
|
||||
Koko loves to eat bananas. There are `N` piles of bananas, the `i`-th pile has `piles[i]` bananas. The guards have gone and will come back in `H` hours.
|
||||
|
||||
Koko can decide her bananas-per-hour eating speed of `K`. Each hour, she chooses some pile of bananas, and eats K bananas from that pile. If the pile has less than `K` bananas, she eats all of them instead, and won't eat any more bananas during this hour.
|
||||
|
||||
Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back.
|
||||
|
||||
Return the minimum integer `K` such that she can eat all the bananas within `H` hours.
|
||||
|
||||
**Example 1:**
|
||||
|
||||
Input: piles = [3,6,7,11], H = 8
|
||||
Output: 4
|
||||
|
||||
**Example 2:**
|
||||
|
||||
Input: piles = [30,11,23,4,20], H = 5
|
||||
Output: 30
|
||||
|
||||
**Example 3:**
|
||||
|
||||
Input: piles = [30,11,23,4,20], H = 6
|
||||
Output: 23
|
||||
|
||||
**Note:**
|
||||
|
||||
- `1 <= piles.length <= 10^4`
|
||||
- `piles.length <= H <= 10^9`
|
||||
- `1 <= piles[i] <= 10^9`
|
||||
|
||||
|
||||
## 题目大意
|
||||
|
||||
|
||||
珂珂喜欢吃香蕉。这里有 N 堆香蕉,第 i 堆中有 piles[i] 根香蕉。警卫已经离开了,将在 H 小时后回来。
|
||||
|
||||
珂珂可以决定她吃香蕉的速度 K (单位:根/小时)。每个小时,她将会选择一堆香蕉,从中吃掉 K 根。如果这堆香蕉少于 K 根,她将吃掉这堆的所有香蕉,然后这一小时内不会再吃更多的香蕉。
|
||||
|
||||
珂珂喜欢慢慢吃,但仍然想在警卫回来前吃掉所有的香蕉。
|
||||
|
||||
返回她可以在 H 小时内吃掉所有香蕉的最小速度 K(K 为整数)。
|
||||
|
||||
提示:
|
||||
|
||||
- 1 <= piles.length <= 10^4
|
||||
- piles.length <= H <= 10^9
|
||||
- 1 <= piles[i] <= 10^9
|
||||
|
||||
|
||||
|
||||
## 解题思路
|
||||
|
||||
|
||||
- 给出一个数组,数组里面每个元素代表的是每个香蕉🍌串上香蕉的个数。koko 以 `k 个香蕉/小时`的速度吃这些香蕉。守卫会在 `H 小时`以后回来。问 k 至少为多少,能在守卫回来之前吃完所有的香蕉。当香蕉的个数小于 k 的时候,这个小时只能吃完这些香蕉,不能再吃其他串上的香蕉了。
|
||||
- 这一题可以用二分搜索来解答。在 `[0 , max(piles)]` 的范围内搜索,二分的过程都是常规思路。判断是否左右边界如果划分的时候需要注意题目中给的限定条件。当香蕉个数小于 k 的时候,那个小时不能再吃其他香蕉了。
|
Reference in New Issue
Block a user