aboutsummaryrefslogtreecommitdiff
path: root/other/max_bags_rocks.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'other/max_bags_rocks.cpp')
-rw-r--r--other/max_bags_rocks.cpp22
1 files changed, 22 insertions, 0 deletions
diff --git a/other/max_bags_rocks.cpp b/other/max_bags_rocks.cpp
new file mode 100644
index 0000000..35c66bc
--- /dev/null
+++ b/other/max_bags_rocks.cpp
@@ -0,0 +1,22 @@
+#include "leetcode.h"
+
+class Solution {
+public:
+ int maximumBags(vector<int> &capacity, vector<int> &rocks,
+ int additionalRocks) {
+ vector<int> count;
+ int n = rocks.size(), ans;
+ for (int i = 0; i < n; ++i)
+ count.push_back(capacity[i] - rocks[i]);
+ sort(count.begin(), count.end());
+ for (ans = 0; ans < n && additionalRocks >= count[ans]; ++ans)
+ additionalRocks -= count[ans];
+ return ans;
+ }
+};
+
+int main() {
+ Solution obj;
+ vector<int> capacity = {2, 3, 4, 5}, rocks = {1, 2, 4, 4};
+ cout << obj.maximumBags(capacity, rocks, 2);
+}