Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static long minFloors(long[] buildings, int m) {
- //sort buildings
- Arrays.sort(buildings);
- //maintain a window of size m, compute the minCost of each window, update minCost along the way as the final result
- long minCost = Long.MAX_VALUE;
- for(int i = 0; i <= buildings.length-m; i++){
- long heightToMatch = buildings[i+m-1];
- if(heightToMatch == buildings[i]) return 0;//if the last building's height equals the first one, that means the whole window if of the same size, we can directly return 0
- long thisCost = 0;
- for(int j = i+m-1; j >= i; j--){
- thisCost += heightToMatch - buildings[j];
- }
- minCost = Math.min(minCost, thisCost);
- }
- return minCost;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement