Untitled

 avatar
unknown
c_cpp
a year ago
641 B
6
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;
    int removed = 0;
    
    while (left <= right) {
        if (boxes[right] <= capacity * boxes[left]) {
            return removed;
        }
        
        // Calculate which removal would be more beneficial
        if (boxes[right] - boxes[right-1] > boxes[left+1] - boxes[left]) {
            right--;
        } else {
            left++;
        }
        
        removed++;
    }
    
    return removed;
}
Editor is loading...
Leave a Comment