-
Notifications
You must be signed in to change notification settings - Fork 0
/
Question15.cpp
43 lines (38 loc) · 887 Bytes
/
Question15.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
38
39
40
41
42
43
15 -> Maximum Points You Can Obtain from Cards
Solution :
class Solution {
public:
int maxScore(vector<int>& cardPoints, int k) {
int n=cardPoints.size();
vector<int>pref1;
vector<int>pref2(n,0);
int sum=0;
for(auto x:cardPoints){
sum+=x;
pref1.push_back(sum);
}
sum=0;
for(int i=n-1;i>=0;i--){
sum+=cardPoints[i];
pref2[i]=sum;
}
int ans=pref1[k-1];
int i=k-2;
int j=n-1;
while(i>=0){
ans=max(ans,pref1[i]+pref2[j]);
cout<<ans<<endl;
i--;
j--;
}
ans=max(ans,pref2[n-k]);
i=n-k+1;
j=0;
while(i<n){
ans=max(ans,pref2[i]+pref1[j]);
i++;
j++;
}
return ans;
}
};