-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday9.cpp
More file actions
63 lines (47 loc) · 1.3 KB
/
day9.cpp
File metadata and controls
63 lines (47 loc) · 1.3 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
'''
Given an array of numbers N and an integer k, your task is to split N into k partitions such that
the maximum sum of any partition is minimized. Return this sum.
For example, given N = [5, 1, 2, 7, 3, 4] and k = 3, you should return 8, since the optimal partition is [5, 1, 2], [7], [3, 4].
'''
int solution(vector <int> arr, int k){
sort(arr.begin(), arr.end());
for (int i=0;i<arr.size();i++){
cout<<arr[arr.size()-1-i]<<" ";
}
cout<<endl;
int sum [k];
for (int i=0;i<k;i++){
sum[i]=0;
}
int min_index = 0;
int min_sum = 0;
for (int i=0;i<arr.size();i++){
for (int j=0;j<k;j++){
if(sum[j] <= min_sum){
min_sum = sum[j];
min_index = j;
}
}
sum[min_index]+=arr[arr.size()-1-i];
min_sum = sum[min_index];
}
int max = 0;
for (int i=0;i<k;i++){
if(sum[i]>max){
max = sum[i];
}
cout<< sum[i]<<" ";
}
cout<<endl;
return max;
}
int main(){
vector <int> arr = {5,1,2,7,3,4};
int k = 3;
cout << solution(arr, k) << endl;
return 0;
}