Advertisement
thewitchking

Untitled

Mar 25th, 2025
508
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.60 KB | None | 0 0
  1. class Solution {
  2.     public int minEatingSpeed(int[] piles, int h) {
  3.         int left = 1;
  4.         int right = 1000000000;
  5.        
  6.         while(left <= right){
  7.             int mid = left + (right - left) / 2;
  8.             if(canEatInTime(piles, mid, h)) right = mid - 1;
  9.             else left = mid + 1;
  10.         }
  11.         return left;
  12.     }
  13.     public boolean canEatInTime(int piles[], int k, int h){
  14.         int hours = 0;
  15.         for(int pile : piles){
  16.             int div = pile / k;
  17.             hours += div;
  18.             if(pile % k != 0) hours++;
  19.         }
  20.         return hours <= h;
  21.     }
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement