【贪心算法】334. 递增的三元子序列

334. 递增的三元子序列

解题思路

  • 找到的递增序列 不一定是连续的
  • 固定第一个数first 然后开始向后找第二个数second
  • 要求second 大于 first 找到之后 向后找第三个数third 找到 返回true
  • 如果third < first 那么更新first = third 重新找
  • 如果只是third > first 更新second
java 复制代码
 class Solution {
    public boolean increasingTriplet(int[] nums) {
        // 找到的递增序列 不一定是连续的
        // 固定第一个数first  然后开始向后找第二个数second
        // 要求second 大于 first 找到之后 向后找第三个数third 找到 返回true
        // 如果third < first 那么更新first = third 重新找
        // 如果只是third > first 更新second  

        int first = nums[0];
        int second = Integer.MAX_VALUE;
        for(int i = 1; i < nums.length; i++){
            if(nums[i] > second){
                // 说明找到了
                return true;
            }

            if(nums[i] < first){
                first = nums[i];
            }else if(nums[i] > first){
                second = nums[i];
            }
        }


        return false;
    }
}
相关推荐
大熊背12 小时前
树莓派IspPipeline LSC模块原理详解
算法·lsc·isppipeline·mesh lsc
zander25813 小时前
LeetCode 300. 最长递增子序列
算法·leetcode·深度优先
祖力5514 小时前
Linux应用软件编程:目录IO与framebuffer
linux·运维·算法·framebuffer·目录io
名字还没想好☜14 小时前
Go 用 slices/maps 标准库泛型函数:告别手写 Contains、Sort、去重(Go 1.21)
开发语言·后端·算法·golang·go
地平线开发者14 小时前
【模型轻量化专题】深度学习模型为什么需要轻量化
算法
_Narcissus_14 小时前
链表算法题和静态链表
数据结构·c++·笔记·算法·链表·ai·力扣
Lyyaoo.14 小时前
【普通数组】【中等】除了自身以外数组的乘积
数据结构·算法·leetcode
threerocks15 小时前
《The AI-Native SDLC Playbook》万字拆解
算法·aigc·ai编程
夏玉林的学习之路15 小时前
算法8.环形队列
算法
O。O蛋黄酥啊15 小时前
GraphRAG 和 LightRAG 详解与对比
人工智能·python·算法·rag·graphrag·lightrag