Answers for "piles of boxes leetcode solution"

0

piles of boxes leetcode solution

private static int minStepsToMakePilesHeightEqual(int[] piles) {

        int minHeight = getMinHeight(piles);
        PriorityQueue<Integer> maxHeap = new PriorityQueue<>((a,b)->b-a);


        int minSteps = 0;

        Map<Integer, Integer> pileMap = new HashMap<>();


        for(int i=0; i<piles.length; i++){

            int height = piles[i];

            if(height == minHeight)
                continue;

            if(pileMap.get(height) == null)
            {
                maxHeap.add(height);
                pileMap.put(height, 1);
            }
            else{
                pileMap.put(height, pileMap.get(height)+1);
            }
            
        }

        while(!maxHeap.isEmpty()){
            int max = maxHeap.poll();
            int stepsRequired = maxHeap.size()+1; // plus one for not putting minimum in heap
            int freq = pileMap.get(max);

            minSteps += freq*stepsRequired;
        }

        return minSteps;
    }
Posted by: Guest on August-08-2021

Browse Popular Code Answers by Language