Advertisement
lazar955

Untitled

Oct 23rd, 2023
754
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.62 KB | None | 0 0
  1.  
  2. func minEatingSpeed(piles []int, h int) int {
  3.     l,r := 1, Max(piles)
  4.     res := r
  5.  
  6.     for l<=r {
  7.       k:=(l+r)/2
  8.       hours:=0
  9.       for _,p:= range piles {
  10.           hours+= int(math.Ceil(float64(p)/float64(k)))
  11.  
  12.       }
  13.       if hours <= h {
  14.           res = min(res,k)
  15.           r = k - 1
  16.       }else {
  17.           l = k + 1
  18.       }
  19.     }
  20.     return res
  21. }
  22.  
  23. func Max(arr []int) int {
  24.     max := arr[0]
  25.    
  26.     for _, val := range(arr) {
  27.         if max < val {
  28.             max = val
  29.         }
  30.     }
  31.     return max
  32. }
  33.  
  34. func min(v,w int) int {
  35.     if v > w{
  36.         return w
  37.     }
  38.     return v
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement