Middle-题目44:334. Increasing Triplet Subsequence

寻找长度3递增子序列
本文介绍了一种高效算法,用于判断一个未排序数组中是否存在长度为3的递增子序列。该算法仅需遍历一次数组,并使用常数级别的额外空间,实现了O(n)的时间复杂度。

题目原文:
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the array.

Formally the function should:
Return true if there exists i, j, k
such that arr[i] < arr[j] < arr[k] given 0 ≤ i < j < k ≤ n-1 else return false.
Your algorithm should run in O(n) time complexity and O(1) space complexity.
题目大意:
给出一个数组,判断是否存在一个递增的长度为3的子序列。
题目分析:
(1) 朴素解法: O(n3) 暴力搜索;
(2) 最长递增子列:引用Middle-题目33的方法判断最长递增子列的长度是否>=3,最好的时间复杂度是O(nlogn)。
(3) O(n)算法:扫描一遍数组,令a1是当前最小值,a2是a1以后次小值,则如果当前的数比a2还大,就存在。
源码:(language:java)

public class Solution {
    public boolean increasingTriplet(int[] nums) {
        int a1 = Integer.MAX_VALUE,a2 = Integer.MAX_VALUE;
        for(int num : nums) {
            if(num<=a1) 
                a1=num;
            else if(num<=a2)
                a2=num;
            else
                return true;
        }
        return false;
    }
}

成绩:
1ms,beats 34.32%,众数1ms,65.68%

2025-10-11 14:20:13.571 [reactor-http-nio-3] DEBUG reactor.netty.http.server.HttpServerOperations - [0ec6d5d5, L:/172.16.1.39:8088 - R:/172.16.2.61:52947] New http connection, requesting read 2025-10-11 14:20:13.571 [reactor-http-nio-3] DEBUG reactor.netty.transport.TransportConfig - [0ec6d5d5, L:/172.16.1.39:8088 - R:/172.16.2.61:52947] Initialized pipeline DefaultChannelPipeline{(reactor.left.httpCodec = io.netty.handler.codec.http.HttpServerCodec), (reactor.left.httpTrafficHandler = reactor.netty.http.server.HttpTrafficHandler), (reactor.right.reactiveBridge = reactor.netty.channel.ChannelOperationsHandler)} 2025-10-11 14:20:13.573 [reactor-http-nio-3] DEBUG reactor.netty.http.server.HttpServerOperations - [0ec6d5d5, L:/172.16.1.39:8088 - R:/172.16.2.61:52947] Increasing pending responses, now 1 2025-10-11 14:20:13.573 [reactor-http-nio-3] DEBUG reactor.netty.http.server.HttpServer - [0ec6d5d5-1, L:/172.16.1.39:8088 - R:/172.16.2.61:52947] Handler is being applied: org.springframework.http.server.reactive.ReactorHttpHandlerAdapter@1078821d 2025-10-11 14:20:13.573 [reactor-http-nio-3] DEBUG o.s.web.server.adapter.HttpWebHandlerAdapter - [0ec6d5d5-2] HTTP POST "/system/session/login?account=admin&password=123456" 2025-10-11 14:20:13.574 [reactor-http-nio-3] DEBUG o.s.w.r.r.m.a.RequestMappingHandlerMapping - [0ec6d5d5-2] Mapped to com.hvlink.controller.SessionController#login(String, String) 2025-10-11 14:20:13.575 [reactor-http-nio-3] DEBUG org.mybatis.spring.SqlSessionUtils - Creating a new SqlSession 2025-10-11 14:20:13.575 [reactor-http-nio-3] DEBUG org.mybatis.spring.SqlSessionUtils - SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6445501b] was not registered for synchronization because synchronization is not active 2025-10-11 14:20:13.577 [reactor-http-nio-3] DEBUG o.springframework.jdbc.datasource.DataSourceUtils - Fetching JDBC Connection from DataSource
10-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值