-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcookies.cpp
More file actions
37 lines (28 loc) · 724 Bytes
/
Copy pathcookies.cpp
File metadata and controls
37 lines (28 loc) · 724 Bytes
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
33
34
35
36
37
#include<bits/stdc++.h>
using namespace std;
//vector<int> c = {8,15,10,20,8};
//int child = 2;
int distributeCookies(vector<int>& cookies, int k) {
vector<int> child(k,0);
sort(cookies.begin(), cookies.end());
int j = 1;
for(int i = 0 ; i < cookies.size() ; i++){
if(j < k){
if(child[j-1]+cookies[i] < child[j]){
child[j-1] += cookies[i];
}else{
child[j] += cookies[i];
}
j++;
}else{
j = 0;
}
}
sort(child.begin(), child.end());
return child[k-1];
}
int main(){
vector<int> c = {8,15,10,20,8};
int child = 2;
cout << distributeCookies(c, child);
}