力扣2874.有序三元组中的最大值 II

力扣2874.有序三元组中的最大值 II

  • 遍历j --> 找j左边最大数 和右边最大数

cpp 复制代码
  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;
      }
  };
相关推荐
风筝在晴天搁浅4 小时前
代码随想录 718.最长重复子数组
算法
kyle~4 小时前
算法---回溯算法
算法
star _chen4 小时前
C++实现完美洗牌算法
开发语言·c++·算法
hzxxxxxxx4 小时前
1234567
算法
Sylvia-girl4 小时前
数据结构之复杂度
数据结构·算法
CQ_YM5 小时前
数据结构之队列
c语言·数据结构·算法·
VekiSon5 小时前
数据结构与算法——树和哈希表
数据结构·算法
xu_yule6 小时前
数据结构与算法(1)(第一章复杂度知识点)(大O渐进表示法)
数据结构
大江东去浪淘尽千古风流人物6 小时前
【DSP】向量化操作的误差来源分析及其经典解决方案
linux·运维·人工智能·算法·vr·dsp开发·mr
fish_xk6 小时前
数据结构之排序
数据结构