LeetCode:递增的三元子序列【334】

LeetCode:递增的三元子序列【334】

题目描述

给定一个未排序的数组,判断这个数组中是否存在长度为 3 的递增子序列。

数学表达式如下:

如果存在这样的 i, j, k,  且满足 0 ≤ i < j < k ≤ n-1,
使得 arr[i] < arr[j] < arr[k] ,返回 true ; 否则返回 false 。

说明: 要求算法的时间复杂度为 O(n),空间复杂度为 O(1) 。

示例 1:

输入: [1,2,3,4,5]
输出: true

示例 2:

输入: [5,4,3,2,1]
输出: false

题目分析

Java题解

class Solution {
    public boolean increasingTriplet(int[] nums) {
        if(nums.length<3)
            return false;
        int min1 = Integer.MAX_VALUE;
        int min2 = Integer.MAX_VALUE;
        for(int i=0;i<nums.length;i++)
        {
            //CASE3
            if(nums[i]>min2)
                return true;
            if(nums[i]<min1)
                min1=nums[i];
            if(nums[i]>min1&&nums[i]<min2)
                min2=nums[i];
        }
        return false;
    }
}

原文地址:https://www.cnblogs.com/MrSaver/p/9499233.html

时间: 2024-08-03 15:09:26

LeetCode:递增的三元子序列【334】的相关文章

【LeetCode】334#递增的三元子序列

题目描述 给定一个未排序的数组,判断这个数组中是否存在长度为 3 的递增子序列. 数学表达式如下: 如果存在这样的 i, j, k, 且满足 0 ≤ i < j < k ≤ n-1, 使得 arr[i] < arr[j] < arr[k] ,返回 true ; 否则返回 false . 说明: 要求算法的时间复杂度为 O(n),空间复杂度为 O(1) . 示例 1: 输入: [1,2,3,4,5] 输出: true 示例 2: 输入: [5,4,3,2,1] 输出: false 解

递增的三元子序列

给定一个未排序的数组,请判断这个数组中是否存在长度为3的递增的子序列. 正式的数学表达如下: 如果存在这样的 i, j, k,  且满足 0 ≤ i < j < k ≤ n-1,使得 arr[i] < arr[j] < arr[k] ,返回 true ; 否则返回 false . 要求算法时间复杂度为O(n),空间复杂度为O(1) . 示例:输入 [1, 2, 3, 4, 5],输出 true. 输入 [5, 4, 3, 2, 1],输出 false. 致谢:特别感谢 @Djang

(Java) LeetCode 334. Increasing Triplet Subsequence —— 递增的三元子序列

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

[LeetCode] Increasing Triplet Subsequence 递增的三元子序列

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

【中级算法】5.递增的三元子序列

题目: 给定一个未排序的数组,请判断这个数组中是否存在长度为3的递增的子序列. 正式的数学表达如下: 如果存在这样的 i, j, k, 且满足 0 ≤ i < j < k ≤ n-1, 使得 arr[i] < arr[j] < arr[k] ,返回 true ; 否则返回 false . 要求算法时间复杂度为O(n),空间复杂度为O(1) . 示例: 输入 [1, 2, 3, 4, 5], 输出 true. 输入 [5, 4, 3, 2, 1], 输出 false. 解法: cla

[Swift]LeetCode334. 递增的三元子序列 | Increasing Triplet Subsequence

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

[LeetCode] 334. Increasing Triplet Subsequence 递增三元子序列

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

leetcode-递增的三元子序列

给定一个未排序的数组,判断这个数组中是否存在长度为 3 的递增子序列. 数学表达式如下: 如果存在这样的 i, j, k,  且满足 0 ≤ i < j < k ≤ n-1,使得 arr[i] < arr[j] < arr[k] ,返回 true ; 否则返回 false . 说明: 要求算法的时间复杂度为 O(n),空间复杂度为 O(1) . 示例 1: 输入: [1,2,3,4,5] 输出: true 示例 2: 输入: [5,4,3,2,1] 输出: false 原文地址:h

[LeetCode]152. 乘积最大子序列(DP)

题目 给定一个整数数组 nums?,找出一个序列中乘积最大的连续子序列(该序列至少包含一个数). 示例 1: 输入: [2,3,-2,4] 输出: 6 解释:?子数组 [2,3] 有最大乘积 6. 示例 2: 输入: [-2,0,-1] 输出: 0 解释:?结果不能为 2, 因为 [-2,-1] 不是子数组. 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/maximum-product-subarray 著作权归领扣网络所有.商业转载请联