2908. Minimum Sum of Mountain Triplets I
You are given a 0-indexed array nums
of integers.
A triplet of indices (i, j, k)
is a mountain if:
i < j < k
nums[i] < nums[j]
and nums[k] < nums[j]
Return the minimum possible sum of a mountain triplet of nums
. If no such triplet exists, return -1
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| class Solution { public: int minimumSum(vector<int>& nums) { int n = nums.size(), lmi = 1e9, rmi = 1e9; vector<long long> dpl(n,1e9), dpr(n,1e9); for(int i = 0; i < n; i++) { if(nums[i] > lmi) dpl[i] = lmi; lmi = min(nums[i], lmi); } for(int i = n - 1; i >= 0; i--) { if(nums[i] > rmi) dpr[i] = rmi; rmi = min(nums[i], rmi); } long long res = 1e9; for(int i = 0; i < n; i++) { res = min(res, dpl[i] + dpr[i] + nums[i]); } return res >= 1e9 ? -1:res; } };
|