Leetcode 334. Increasing Triplet Subsequence

该篇文章介绍了如何使用动态规划方法解决一个编程问题,判断给定整数数组中是否存在三个元素满足nums[i]<nums[j]<nums[k]的条件。代码展示了从数组前端开始扫描,利用最小前缀值更新dp数组的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem

Given an integer array nums, return true if there exists a triple of indices (i, j, k) such that i < j < k and nums[i] < nums[j] < nums[k]. If no such indices exists, return false.

Algorithm

Dynamic Programming (DP). Following the increasing length of tuples, scan the array from front to back each time, saving the position of the smallest prefix of length L-1: if the data is larger than the smallest prefix, a new sequence of length L is found, otherwise update the prefix.

Code

class Solution:
    def increasingTriplet(self, nums: List[int]) -> bool:
        nLen = len(nums)
        dp = [1] * nLen
        for i in range(1, 3):
            min_value_index = 0
            while min_value_index < nLen and dp[min_value_index] < i:
                min_value_index += 1
            if min_value_index >= nLen:
                return False
            
            j = min_value_index+1
            while j < nLen:
                if dp[j] == i:
                    if nums[min_value_index] < nums[j]:
                        dp[j] = i+1
                    else:
                        min_value_index = j
                j += 1
        
        return max(dp) >= 3
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值