Untitled
unknown
c_cpp
a year ago
739 B
10
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 the difference between removing from left and right
int diff_left = boxes[left + 1] - boxes[left];
int diff_right = boxes[right] - boxes[right - 1];
if (diff_left <= diff_right) {
left++;
} else {
right--;
}
removed++;
}
return removed;
}Editor is loading...
Leave a Comment