-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlintcode1850.cpp
36 lines (34 loc) · 1.19 KB
/
lintcode1850.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
// 用隔板法
// 分兩種情況討論:左邊k右邊l / 左邊l右邊k
// 這邊在自己寫的getApple裡面用sliding window求最大窗口,所以還要傳入window size
int getApple(vector<int> &a, int winsize, int left, int right) {
int maxApple = 0;
if(winsize > right - left)
return -1;
for(int i = left; i < left + winsize; i++)
maxApple += a[i];
int apples = maxApple;
for(int i = left + 1; i <= right - winsize; i++) {
apples = apples - a[i - 1] + a[i + winsize - 1];
maxApple = max(maxApple, apples);
}
return maxApple;
}
int PickApples(vector<int> &A, int K, int L) {
int maxApple = INT_MIN;
for(int i = 0; i < A.size(); i++) {
// k left l right
int leftk = getApple(A, K, 0, i);
int rightl = getApple(A, L, i, A.size());
// l left k right
int leftl = getApple(A, L, 0, i);
int rightk = getApple(A, K, i, A.size());
if(leftk != -1 && rightl != -1)
maxApple = max(maxApple, leftk + rightl);
if(leftl != -1 && rightk != -1)
maxApple = max(maxApple, leftl + rightk);
}
if(maxApple == INT_MIN)
return -1;
return maxApple;
}