-
Notifications
You must be signed in to change notification settings - Fork 3
/
1793.cpp
35 lines (33 loc) · 974 Bytes
/
1793.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
class Solution {
public:
int maximumScore(vector<int>& nums, int k) {
int n = nums.size();
int minVal = nums[k];
int res = 0;
int left = k;
int right = k;
while (left >= 0 || right <= n - 1) {
res = max(res, minVal * (right - left + 1));
if (left == 0 && right == n - 1) break;
if (left == 0) {
right++;
minVal = min(minVal, nums[right]);
}
else if (right == n - 1) {
left--;
minVal = min(minVal, nums[left]);
}
else {
if (nums[left - 1] < nums[right + 1]) {
right++;
minVal = min(minVal, nums[right]);
}
else {
left--;
minVal = min(minVal, nums[left]);
}
}
}
return res;
}
};