-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathLv4_쿠키구입.cpp
37 lines (33 loc) · 903 Bytes
/
Lv4_쿠키구입.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
33
34
35
36
37
#include <string>
#include <vector>
using namespace std;
int solution(vector<int> cookie) {
int answer = 0;
int size = cookie.size();
vector<int> sum; // 1 2 4 7
sum.push_back(0);
sum.push_back(cookie[0]);
for (int i = 1; i < size; i++)
sum.push_back(sum[i] + cookie[i]);
size = sum.size();
int start_sum = 0;
for (int start = 0; start < size; start++) {
for (int end = size-1; end > start; end--) {
int total = sum[end] - sum[start];
if (total % 2 != 0) // 쿠키가 홀수개이면 패스
continue;
if (total / 2 < answer) // 나눠 가져도 이전 답보다 작을 경우 패스
continue;
for (int index = end; index > start; index-- ) {
if (sum[index] - sum[start] == total / 2) {
if (total / 2 > answer)
answer = total / 2;
break;
}
else if (sum[index] - sum[start] < total / 2)
break;
}
}
}
return answer;
}