Untitled

 avatar
unknown
c_cpp
a year ago
597 B
4
Indexable
#include <vector>
#include <algorithm>

using namespace std;

int removeBoxes(vector<int> boxes, int capacity) {
    int n = boxes.size();
    sort(boxes.begin(), boxes.end());
    
    int left = 0;
    int right = n - 1;
    
    while (left <= right && boxes[right] > capacity * boxes[left]) {
        // Check if removing the largest box is better
        if ((right > left) && (boxes[right-1] <= capacity * boxes[left])) {
            right--;
        }
        // Otherwise, remove the smallest box
        else {
            left++;
        }
    }
    
    return n - (right - left + 1);
}
Editor is loading...
Leave a Comment