1. 题意
求 m i n ( a i + a j + a k ) , a j > a k , a j > a i , i < j < k min(a_i + a_j + a_k), a_j >a_k,a_j > a_i,i \lt j \lt k min(ai+aj+ak),aj>ak,aj>ai,i<j<k
元素和最小的山形三元组
2. 题解
枚举 j j j,求出 P r e M i n ( j − 1 ) PreMin(j - 1) PreMin(j−1)与 S u f M i n ( j + 1 ) SufMin(j +1) SufMin(j+1)
- 代码
cpp
class Solution {
public:
int minimumSum(vector<int>& nums) {
int sz = nums.size();
vector<int> suf(sz);
suf[sz - 1] = nums[sz - 1];
for ( int i = sz - 2; ~i; --i ) {
suf[i] = min(suf[i + 1], nums[i]);
}
int ans = -1;
int pre = nums[0];
for ( int i = 1; i < sz - 1; ++i ) {
if ( nums[i] > pre && nums[i] > suf[i + 1]) {
int sum = nums[i] + pre + suf[i + 1];
if ( ans == -1)
ans = sum;
else
ans = min(ans, sum);
}
pre = min(pre, nums[i]);
}
return ans;
}
};