力扣2874.有序三元组中的最大值 II
-
遍历j –> 找j左边最大数 和右边最大数
-
class Solution { public: long long maximumTripletValue(vector<int>& nums) { int n = nums.size(); vector<int> suf_max(n+1,0); //右边最大数 for(int i=n-1;i>1;i--) { suf_max[i] = max(suf_max[i+1] , nums[i]); } long long res=0; //左边最大数 int pre_max = nums[0]; for(int j=1;j<n-1;j++) { res = max(res, (long long)(pre_max - nums[j]) * suf_max[j + 1]); pre_max = max(pre_max,nums[j]); } return res; } };