# Untitled

unknown
c_cpp
24 days ago
1.5 kB
2
Indexable
Never
```#include <vector>
#include <algorithm>

int removeBoxes(std::vector<int> boxes, int capacity) {
int n = boxes.size();
std::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;
}#include <vector>
#include <algorithm>

int removeBoxes(std::vector<int> boxes, int capacity) {
int n = boxes.size();
std::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;
}```
Leave a Comment