Untitled
unknown
c_cpp
a year ago
1.5 kB
13
Indexable
#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;
}Editor is loading...
Leave a Comment