首页 > 其他分享 >LeetCode/递增的三元子序列

LeetCode/递增的三元子序列

时间:2022-09-03 01:55:06浏览次数:51  
标签:return nums int 递增 vector 三元 LeetCode s1 size

给你一个整数数组nums ,判断这个数组中是否存在长度为 3 的递增子序列

1. 贪心法

贪心更新两个最左端端点

class Solution {
public:
  bool increasingTriplet(vector<int>& nums) {
    int len = nums.size();
    if (len < 3) return false;
    int small = INT_MAX, mid = INT_MAX;//初始皆赋最大值
    for (auto num : nums) {
      if (num <= small) small = num;//贪心选择左端点
      else if (num <= mid) mid = num;//贪心更新中间端点
      else if (num > mid) return true;
    }
    return false;    
  }
};

2. 动态规划

求最长递增子序列

class Solution {
public:
  bool increasingTriplet(vector<int>& nums) {
        int n = nums.size();
        if(n==0) return 0;
        vector<int> dp(n); //dp[i]记录i长度序列的最后元素值
        int res = 0;
        for(int k=0;k<n;k++){
           int i =0;int j =res;//res 为当前最长长度
           while(i<j){
               int m = (i+j)/2;
               if(dp[m]<nums[k]) i=m+1;//移动左边界,最终移到恰满足条件的下一位置
               else j=m; //移动右边界
           }
        dp[i] = nums[k];//把该元素作为最小元素替换下一序列长度
        if(j==res) res++;//如果该位置拓宽了最大长度,最大长度更新
        if(res>=3) return true;
    }
    return false; 
  }
};

3. 双单调栈

记录每个位置左侧(右侧)是否有更小值(更大值)

class Solution {
public:
    bool increasingTriplet(vector<int>& nums) {
        if(nums.size()<3) return 0;
        vector<bool> left(nums.size());
        vector<bool> right(nums.size());
        stack<int> s1,s2;
        for(int i=0, j=nums.size()-1;i<nums.size();i++,j--){
            while(!s1.empty()&&nums[i]>nums[s1.top()]){
                right[s1.top()] = true;
                s1.pop(); 
            }
            while(!s2.empty()&&nums[j]<nums[s2.top()]){
                left[s2.top()] = true;
                s2.pop(); 
            }
            s1.push(i);
            s2.push(j);
        }
        for(int i=0;i<nums.size();i++)
            if(left[i]&&right[i]) return true;
        return false;
  }
};

标签:return,nums,int,递增,vector,三元,LeetCode,s1,size
From: https://www.cnblogs.com/929code/p/16651838.html

相关文章

  • [Leetcode 189]轮转数组
    Leetocde189轮转数组这题能被用做mid题是因为一题多解,其中基于双指针的轮状数组解法是比较难的1.使用新数组__直接把第i个元素移到第(i+k)%numsize位置,类似循环队列voi......
  • LeetCode617 合并二叉树
    LeetCode617合并二叉树#Definitionforabinarytreenode.#classTreeNode:#def__init__(self,val=0,left=None,right=None):#self.val=val......
  • [Google] LeetCode 778 Swim in Rising Water 优先队列
    Youaregivenannxnintegermatrixgridwhereeachvaluegrid[i][j]representstheelevationatthatpoint(i,j).Therainstartstofall.Attimet,thed......
  • leetcode1502-判断能否形成等差数列
      我的原始代码class Solution {public:    bool canMakeArithmeticProgression(vector<int>& arr) {        sort(arr.begin(),arr.end()); ......
  • leetcode191-位1的个数
    1.循环检查二进制位把题目给出的数不断对2取余,余数为1则计数class Solution {public:    int hammingWeight(uint32_t n) {        int count=0;......
  • leetcode976-三角形的最大周长
    第一反应是排序,然后瞎想了很多什么双指针、三指针的东西。看了评论区才豁然开朗。“为什么排序遍历相邻元素可行,有没有可能最优解为非相邻元素?(不会)证明:反证假设a,b,......
  • LeetCode 51 n皇后
    constintN=20;classSolution{public:vector<vector<string>>res;boolcol[N],dg[N],udg[N];voiddfs(intu,intn,vector<string>&pa......
  • 220902_leetcode 21. Merge Two Sorted Lists 合并两个有序链表(简单).md
    一、题目大意将两个升序链表合并为一个新的升序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。示例1:输入:l1=[1,2,4],l2=[1,3,4]输出:[1,1,2,......
  • LeetCode 35. 搜索插入位置
    题目题目链接:https://leetcode.cn/problems/search-insert-position/给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将......
  • leetcode394-字符串解码
    字符串解码递归classSolution{publicStringdecodeString(Strings){StringBuildersb=newStringBuilder();inti=0,n=s.length()......