File tree 1 file changed +49
-0
lines changed
1 file changed +49
-0
lines changed Original file line number Diff line number Diff line change
1
+ // 時間複雜度:
2
+ // 空間複雜度:
3
+ /*
4
+ * @lc app=leetcode.cn id=875 lang=golang
5
+ *
6
+ * [875] 爱吃香蕉的珂珂
7
+ */
8
+
9
+ // @lc code=start
10
+ func minEatingSpeed (piles []int , h int ) int {
11
+ // 找出最大的香蕉堆
12
+ left , right := 1 , maxPile (piles )
13
+ for left < right {
14
+ // mid 代表消耗香蕉的速度(k)
15
+ mid := int (uint (left + right ) >> 1 )
16
+ if executeTime (piles , mid ) <= h {
17
+ right = mid
18
+ } else {
19
+ left = mid + 1
20
+ }
21
+ }
22
+ return left
23
+ }
24
+
25
+ // 假設消耗速度k, 算出要花多少時間
26
+ func executeTime (piles []int , k int ) int {
27
+ time := 0
28
+ for _ , pile := range piles {
29
+ time += pile / k
30
+ if pile % k > 0 {
31
+ time ++
32
+ }
33
+ // time += int(math.Ceil(float64(pile) / float64(k)))
34
+ }
35
+ return time
36
+ }
37
+
38
+ func maxPile (piles []int ) int {
39
+ result := 0
40
+ for _ , pile := range piles {
41
+ if result < pile {
42
+ result = pile
43
+ }
44
+ }
45
+ return result
46
+ }
47
+
48
+ // @lc code=end
49
+
You can’t perform that action at this time.
0 commit comments