贪心算法day3(最长递增序列问题)

目录

1.最长递增三元子序列

2.最长连续递增序列


1.最长递增三元子序列

题目链接:. - 力扣(LeetCode)

思路:我们只需要设置两个数进行比较就好。设a为nums[0],b 为一个无穷大的数,只要有比a小的数字就赋值a,比a大的数字就赋值b,如果有比b大的数字说明可以组成一个三元子序列直接返回true

代码如下:

复制代码
class Solution {
    public static   boolean increasingTriplet(int[] nums) {
        int a = nums[0],b = Integer.MAX_VALUE;
        for (int i = 0; i < nums.length; i++) {
            if(nums[i] > b){
                return true;
            } else if (nums[i] < a) {
                a = nums[i];
            }else if(nums[i] > a){ 
                b = nums[i];
            }
        }
        return false;
    }
}

2.最长连续递增序列

题目链接:. - 力扣(LeetCode)

思路:双指针遍历

代码:

复制代码
class Solution {
        public int findLengthOfLCIS(int[] nums) {
           int n = nums.length,ret = 0;
        for (int i = 0; i < n; ) {
            int j = i +1;
            while(j < n && nums[j] >nums[j -1])j++;
            ret = Math.max(ret,j-i);
            i = j;
        }
        return ret;
    }
}
相关推荐
IronMurphy6 小时前
【算法四十三】279. 完全平方数
算法
墨染天姬6 小时前
【AI】Hermes的GEPA算法
人工智能·算法
papership7 小时前
【入门级-数据结构-3、特殊树:完全二叉树的数组表示法】
数据结构·算法·链表
smj2302_796826527 小时前
解决leetcode第3911题.移除子数组元素后第k小偶数
数据结构·python·算法·leetcode
Beginner x_u7 小时前
链表专题:JS 实现原理与高频算法题总结
javascript·算法·链表
_深海凉_10 小时前
LeetCode热题100-寻找两个正序数组的中位数
算法·leetcode·职场和发展
旖-旎11 小时前
深搜练习(电话号码字母组合)(3)
c++·算法·力扣·深度优先遍历
谭欣辰11 小时前
C++快速幂完整实战讲解
算法·决策树·机器学习
Mr_pyx12 小时前
【LeetHOT100】随机链表的复制——Java多解法详解
算法·深度优先
AIFarmer12 小时前
【无标题】
开发语言·c++·算法