Skip to content

Instantly share code, notes, and snippets.

@idiotleon
Last active June 15, 2020 05:15
Show Gist options
  • Save idiotleon/f5ad0ab4cd02592d53a609f22a3b0e6b to your computer and use it in GitHub Desktop.
Save idiotleon/f5ad0ab4cd02592d53a609f22a3b0e6b to your computer and use it in GitHub Desktop.
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