力扣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;
      }
  };
相关推荐
踩坑记录13 小时前
leetcode hot100 74. 搜索二维矩阵 二分查找 medium
leetcode
TracyCoder12313 小时前
LeetCode Hot100(60/100)——55. 跳跃游戏
算法·leetcode
月挽清风13 小时前
代码随想录第35天:动态规划
算法·动态规划
岛雨QA13 小时前
链表「Java数据结构与算法学习笔记3」
数据结构·算法
Sunsets_Red14 小时前
P8277 [USACO22OPEN] Up Down Subsequence P 题解
c语言·c++·算法·c#·学习方法·洛谷·信息学竞赛
菜鸟小九14 小时前
redis原理篇(基本数据结构)
数据结构·数据库·redis
汉克老师14 小时前
GESP2023年12月认证C++二级( 第三部分编程题(2) 小杨的H字矩阵)
c++·算法·矩阵·循环结构·gesp二级·gesp2级
_Li.14 小时前
Simulink-螺旋桨动力模块
人工智能·算法·机器学习
Charlie_lll14 小时前
力扣解题-438. 找到字符串中所有字母异位词
后端·算法·leetcode
奶茶树14 小时前
【数据结构】红黑树
数据结构·c++·算法