Skip to content

Commit

Permalink
feat: 0875.Koko-Eating-Bananas
Browse files Browse the repository at this point in the history
  • Loading branch information
kimi0230 committed Mar 28, 2024
1 parent 9f2ab52 commit 0fca351
Show file tree
Hide file tree
Showing 3 changed files with 209 additions and 0 deletions.
130 changes: 130 additions & 0 deletions Leetcode/0875.Koko-Eating-Bananas/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
---
title: 875. Koko Eating Bananas
subtitle: "https://leetcode.com/problems/koko-eating-bananas/description/"
date: 2024-03-28T18:03:00+08:00
lastmod: 2024-03-28T18:03:00+08:00
draft: false
author: "Kimi.Tsai"
authorLink: "https://kimi0230.github.io/"
description: "0875.Koko-Eating-Bananas"
license: ""
images: []

tags: [LeetCode, Go, Medium, Koko Eating Bananas, Array, Binary Search]
categories: [LeetCode]

featuredImage: ""
featuredImagePreview: ""

hiddenFromHomePage: false
hiddenFromSearch: false
twemoji: false
lightgallery: true
ruby: true
fraction: true
fontawesome: true
linkToMarkdown: false
rssFullText: false

toc:
enable: true
auto: true
code:
copy: true
maxShownLines: 200
math:
enable: false
# ...
mapbox:
# ...
share:
enable: true
# ...
comment:
enable: true
# ...
library:
css:
# someCSS = "some.css"
# located in "assets/"
# Or
# someCSS = "https://cdn.example.com/some.css"
js:
# someJS = "some.js"
# located in "assets/"
# Or
# someJS = "https://cdn.example.com/some.js"
seo:
images: []
# ...
---
# [875. Koko Eating Bananas](https://leetcode.com/problems/koko-eating-bananas/description/)

## 題目

## 題目大意


## 解題思路

## Big O

* 時間複雜 : `O(n log m)`,其中 n 是香蕉堆的數量,m 是香蕉堆中香蕉數量的最大值
* 空間複雜 : `O(1)`

## 來源
* https://leetcode.com/problems/koko-eating-bananas/description/
* https://leetcode.cn/problems/koko-eating-bananas/description/

## 解答
https://github.com/kimi0230/LeetcodeGolang/blob/master/Leetcode/0875.Koko-Eating-Bananas/main.go

```go
package kokoeatingbananas

// 時間複雜 O(n log m), 空間複雜 O(1)
func minEatingSpeed(piles []int, h int) int {
// 找出最大的香蕉堆
left, right := 1, maxPile(piles)
for left < right {
// mid 代表消耗香蕉的速度(k)
mid := int(uint(left+right) >> 1)
if executeTime(piles, mid) <= h {
right = mid
} else {
left = mid + 1
}
}
return left
}

// 假設消耗速度k, 算出要花多少時間
func executeTime(piles []int, k int) int {
time := 0
for _, pile := range piles {
time += pile / k
if pile%k > 0 {
time++
}
// time += int(math.Ceil(float64(pile) / float64(k)))
}
return time
}

func maxPile(piles []int) int {
result := 0
for _, pile := range piles {
if result < pile {
result = pile
}
}
return result
}

```

## Benchmark

```sh

```
40 changes: 40 additions & 0 deletions Leetcode/0875.Koko-Eating-Bananas/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package kokoeatingbananas

// 時間複雜 O(n log m), 空間複雜 O(1)
func minEatingSpeed(piles []int, h int) int {
// 找出最大的香蕉堆
left, right := 1, maxPile(piles)
for left < right {
// mid 代表消耗香蕉的速度(k)
mid := int(uint(left+right) >> 1)
if executeTime(piles, mid) <= h {
right = mid
} else {
left = mid + 1
}
}
return left
}

// 假設消耗速度k, 算出要花多少時間
func executeTime(piles []int, k int) int {
time := 0
for _, pile := range piles {
time += pile / k
if pile%k > 0 {
time++
}
// time += int(math.Ceil(float64(pile) / float64(k)))
}
return time
}

func maxPile(piles []int) int {
result := 0
for _, pile := range piles {
if result < pile {
result = pile
}
}
return result
}
39 changes: 39 additions & 0 deletions Leetcode/0875.Koko-Eating-Bananas/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package kokoeatingbananas

import "testing"

var tests = []struct {
piles []int
h int
want int
}{
{
[]int{3, 6, 7, 11},
8,
4,
},
{
[]int{30, 11, 23, 4, 20},
5,
30,
},
{
[]int{30, 11, 23, 4, 20},
6,
23,
},
}

func TestMinEatingSpeed(t *testing.T) {
for _, tt := range tests {
if got := minEatingSpeed(tt.piles, tt.h); got != tt.want {
t.Errorf("minEatingSpeed(%v, %v) = %v, want %v", tt.piles, tt.h, got, tt.want)
}
}
}

func BenchmarkMinEatingSpeed(b *testing.B) {
for i := 0; i < b.N; i++ {
minEatingSpeed(tests[0].piles, tests[0].h)
}
}

0 comments on commit 0fca351

Please sign in to comment.