LeetCode //C - 875. Koko Eating Bananas

875. Koko Eating Bananas

Koko loves to eat bananas. There are n piles of bananas, the i t h i^{th} ith 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 will not eat any more bananas during this hour.

Koko likes to eat slowly but still wants to finish eating all the bananas before the guards return.

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

Constraints:
  • 1 < = p i l e s . l e n g t h < = 1 0 4 1 <= piles.length <= 10^4 1<=piles.length<=104
  • p i l e s . l e n g t h < = h < = 1 0 9 piles.length <= h <= 10^9 piles.length<=h<=109
  • 1 < = p i l e s [ i ] < = 1 0 9 1 <= piles[i] <= 10^9 1<=piles[i]<=109

From: LeetCode
Link: 875. Koko Eating Bananas


Solution:

Ideas:
  • Left Bound (low): Start with 1 because Koko cannot eat at a speed slower than 1 banana per hour.

  • Right Bound (high): Start with the maximum number of bananas in any pile. This is because Koko does not need to eat faster than the largest pile to finish all bananas within h hours.

  • Binary Search: Calculate the mid-point mid between low and high. For each mid, calculate the total hours Koko takes to eat all bananas at this speed. If the total hours exceed h, Koko needs to eat faster, so we move the low up to mid + 1. Otherwise, we can potentially slow down, so we adjust high to mid.

  • Termination Condition: The search continues until low meets high, which will be the minimum speed Koko can eat all the bananas within h hours.

Caode:
// Helper function to calculate the total hours taken to eat all bananas at speed k
int hoursNeeded(int* piles, int pilesSize, int k) {
   
    int hours = 0;
    for (int i = 0; i < pilesSize; i++) {
   
        hours += piles[i] / k;
        if (piles[i] % k != 0) {
    // If there are leftovers, it takes an extra hour
            hours++;
        }
    }
    return hours;
}

int minEatingSpeed(int* piles, int pilesSize, int h) {
   
    int low = 1, high = 0;
    // Find the maximum pile to set as the initial high bound
    for (int i = 0; i < pilesSize; i++) {
   
        if (piles[i] > high) {
   
            high = piles[i];
        }
    }
    
    while (low < high) {
   
        int mid = low + (high - low) / 2;
        if (hoursNeeded(piles, pilesSize, mid) > h) {
   
            low = mid + 1; // Koko needs to eat faster
        } else {
   
            high = mid; // Koko can afford to eat slower
        }
    }
    
    return low; // low and high converge to the minimum speed
}

相关推荐

  1. 面试经典150题(85-87)

    2024-02-07 14:54:02       52 阅读
  2. LeetCode //C - 875. Koko Eating Bananas

    2024-02-07 14:54:02       49 阅读
  3. ug871 Lab4

    2024-02-07 14:54:02       71 阅读
  4. Acwing845 八数码

    2024-02-07 14:54:02       56 阅读
  5. H12-821_895

    2024-02-07 14:54:02       38 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-07 14:54:02       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-07 14:54:02       101 阅读
  3. 在Django里面运行非项目文件

    2024-02-07 14:54:02       82 阅读
  4. Python语言-面向对象

    2024-02-07 14:54:02       91 阅读

热门阅读

  1. C语言---—————— 快速创建指定个数的文件

    2024-02-07 14:54:02       43 阅读
  2. 2 进程(上)

    2024-02-07 14:54:02       48 阅读
  3. LeetCode树总结

    2024-02-07 14:54:02       54 阅读
  4. mysql order by 排序原理

    2024-02-07 14:54:02       41 阅读
  5. 每月AI科研动向(2024年1月)

    2024-02-07 14:54:02       42 阅读
  6. 如何快速上手Vue框架

    2024-02-07 14:54:02       63 阅读
  7. 解决 python CV2 imread读取中文文件名的问题

    2024-02-07 14:54:02       54 阅读
  8. 达梦数据库适配Springboot+MybatisPlus+达梦数据库

    2024-02-07 14:54:02       56 阅读
  9. 渗透测试工具库总结(全网最全)

    2024-02-07 14:54:02       80 阅读
  10. redis相关问题

    2024-02-07 14:54:02       43 阅读
  11. UI自动化之Poco常用断言方式

    2024-02-07 14:54:02       53 阅读
  12. 踩坑实录(Second Day)

    2024-02-07 14:54:02       51 阅读
  13. 蓝桥杯(Web大学组)2022国赛真题:新鲜的蔬菜

    2024-02-07 14:54:02       52 阅读