贪心算法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;
    }
}
相关推荐
DoraBigHead17 分钟前
小哆啦解题记——映射的背叛
算法
Heartoxx27 分钟前
c语言-指针与一维数组
c语言·开发语言·算法
孤狼warrior1 小时前
灰色预测模型
人工智能·python·算法·数学建模
京东云开发者1 小时前
京东零售基于国产芯片的AI引擎技术
算法
chao_7892 小时前
回溯题解——子集【LeetCode】二进制枚举法
开发语言·数据结构·python·算法·leetcode
十盒半价2 小时前
从递归到动态规划:手把手教你玩转算法三剑客
javascript·算法·trae
GEEK零零七2 小时前
Leetcode 1070. 产品销售分析 III
sql·算法·leetcode
凌肖战2 小时前
力扣网编程274题:H指数之普通解法(中等)
算法·leetcode
秋说2 小时前
【PTA数据结构 | C语言版】将数组中元素反转存放
c语言·数据结构·算法
WebInfra2 小时前
如何在程序中嵌入有大量字符串的 HashMap
算法·设计模式·架构