forked from piyush01123/Daily-Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsol.cpp
32 lines (28 loc) · 736 Bytes
/
sol.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/*
My solution is this: First sort the weights smallest to largest.
Then take largest weight w. Find cap-w. and find largest item less than cap-w.
Take that weight.
Then repeat.
*/
#include <iostream>
#include <vector>
#include <algorithm>
int minBoats(std::vector<int> weights, int capacity){
std::sort(weights.begin(), weights.end());
int ctr = 0;
while (weights.size()>0){
int w_max = weights.back();
weights.pop_back();
int i=-1;
while (weights[i+1]<capacity-w_max) i++;
if (i>=0) weights.erase(weights.begin()+i);
ctr++;
}
return ctr;
}
int main(){
std::vector<int> v {100, 200, 150, 80};
int cap = 200;
std::cout << "Min # Boats Required = " << minBoats(v, cap) << '\n';
return 0;
}