Last active
June 15, 2020 05:15
-
-
Save idiotleon/f5ad0ab4cd02592d53a609f22a3b0e6b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution{ | |
public cutPiles(int[] piles, int target){ | |
int max = 0; | |
for(int pile : piles){ | |
max = Math.max(max, pile); | |
} | |
int lo = 1, hi = max; | |
while(lo < hi){ | |
int mid = lo + (hi - lo) / 2; | |
int cuts = getCounts(piles, mid); | |
if(target < cuts) hi = mid; | |
else lo = mid + 1; | |
} | |
return hi - 1; | |
} | |
private int getCounts(int[] piles, int cut){ | |
int cuts = 0; | |
for(int pile : piles) | |
if(pile >= cut) | |
cuts += pile / cut; | |
return cuts; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment