贪心算法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;
    }
}
相关推荐
qq_334903155 分钟前
高性能网络协议栈
开发语言·c++·算法
光电笑映7 分钟前
STL 源码解剖系列:map/set 的底层复用与红黑树封装
c语言·数据结构·c++·算法
阿贵---9 分钟前
模板编译期循环展开
开发语言·c++·算法
2601_954023669 分钟前
Beyond the Hype: Deconstructing the 2025 High-Performance Stack for Agencies
java·开发语言·算法·seo·wordpress·gpl
沉鱼.449 分钟前
滑动窗口问题
数据结构·算法
ysa05103015 分钟前
二分+前缀(预处理神力2)
数据结构·c++·笔记·算法
2401_8331977316 分钟前
嵌入式C++电源管理
开发语言·c++·算法
灰色小旋风20 分钟前
力扣22 括号生成(C++)
开发语言·数据结构·c++·算法·leetcode
寒月小酒21 分钟前
3.23 OJ
数据结构·c++·算法
2501_9249526922 分钟前
模板编译期哈希计算
开发语言·c++·算法