Description
Submission
class Solution {
public:
int maximumScore(vector<int>& nums, int k) {
int n = nums.size();
int l = k, r = k;
vector<int> mins(nums.begin(), nums.end());
for(int i = k-1; i >= 0; --i) {
mins[i] = min(mins[i+1], mins[i]);
}
for(int i = k+1; i < n; ++i) {
mins[i] = min(mins[i-1], mins[i]);
}
int ret = mins[k] * 1;
for(int len = 2; len <= n; ++len) {
if(l == 0 && r == n - 1) break;
if(l == 0) r++;
else if(r == n - 1) l--;
else if(mins[l-1] < mins[r+1]) r++;
else l--;
ret = max(min(mins[l], mins[r])*len, ret);
}
return ret;
}
};