// ==================================================================================
// Problem: Piles of Boxes
// ==================================================================================
// Given an array where each element represents the height of a pile of boxes,
// reduce the height of the largest pile to match the next smaller pile until all
// piles are of equal height. Count the number of operations needed to do so.
//
// Approach:
// - Greedy Approach: At each step, we reduce the largest pile to the next smaller pile.
// - This approach minimizes the number of operations required by always reducing to the next smallest pile.
// - The array is sorted first to efficiently compare adjacent piles.
//
// Time Complexity: O(n log n) — The time complexity is dominated by the sorting step (Arrays.sort(arr)).
// Space Complexity: O(1) — No extra space is used except for a few variables.
//
// By Avinash Sinha
// ==================================================================================
import java.util.*;
class ideone
{
public static void main
(String[] args
) { int[] arr = {5, 2, 1}; // Sample input
System.
out.
println(pilesOfBoxes
(arr
)); // Output: 3 }
public static int pilesOfBoxes(int[] arr) {
Arrays.
sort(arr
); // Sort the array in ascending order to facilitate comparison
int n = arr.length;
int count = 0; // To track the number of operations
int uniqueHeights = 0; // To track the number of unique heights encountered
// Traverse from the end of the array to the beginning, comparing each element with the previous
for (int i = n - 1; i > 0; i--) {
if (arr[i] != arr[i - 1]) {
// When we encounter a new unique height, increment the counter for unique heights
uniqueHeights++;
// Add the number of operations needed to reduce the piles above to the new height
count += uniqueHeights;
}
}
return count; // Return the total number of operations
}
}
/*
* Dry Run Example:
* Input: {5, 2, 1}
* After sorting: {1, 2, 5}
*
* First iteration:
* arr = {1, 2, 2} -> 5 is reduced to 2, count = 1
*
* Second iteration:
* arr = {1, 1, 2} -> 2 is reduced to 1, count = 2
*
* Final result: arr = {1, 1, 1}, count = 3
* Output: 3
*/
/*
* Approach:
* 1. **Greedy Strategy**: We always reduce the largest pile to the next smaller pile.
* 2. We use the sorted array to efficiently check for unique heights and count how many operations are needed to make the piles equal.
* 3. The idea behind the greedy approach is that reducing a pile to match a smaller one minimizes the number of operations required, ensuring an optimal solution.
* 4. The algorithm counts the unique heights while iterating backward through the sorted array, which gives the total number of operations required to make all piles the same height.
*
* Time Complexity: O(n log n)
* - The sorting step `Arrays.sort(arr)` dominates the time complexity with O(n log n).
* - The loop runs for n iterations, but since sorting is the most expensive operation, the overall time complexity is O(n log n).
* Space Complexity: O(1)
* - No extra space is used for data structures beyond a few variables (`count`, `uniqueHeights`), making the space complexity constant.
*/