Advertisement
Guest User

Untitled

a guest
Aug 28th, 2016
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.74 KB | None | 0 0
  1.  
  2. static long minFloors(long[] buildings, int m) {
  3. //sort buildings
  4. Arrays.sort(buildings);
  5. //maintain a window of size m, compute the minCost of each window, update minCost along the way as the final result
  6. long minCost = Long.MAX_VALUE;
  7. for(int i = 0; i <= buildings.length-m; i++){
  8. long heightToMatch = buildings[i+m-1];
  9. 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
  10. long thisCost = 0;
  11. for(int j = i+m-1; j >= i; j--){
  12. thisCost += heightToMatch - buildings[j];
  13. }
  14. minCost = Math.min(minCost, thisCost);
  15. }
  16. return minCost;
  17. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement