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

题目描述

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

数学表达式如下:

如果存在这样的 i, j, k, 且满足 0 ≤ i < j < kn-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

解题思路

1、暴力破解

使用三层循环,先找到二元上升序列,再在二元上升序列的基础上,找三元上升序列,时间复杂度为O(N^3)

源代码

public boolean increasingTriplet (int[] nums) {
    if (nums.length < 3) return false;
    for (int i = 0; i < nums.length-2; i++) {
        for (int j = i+1; j < nums.length - 1; j++) {
            if (nums[j] > nums[i]) {
                for (int k = j+1; k < nums.length; k++) {
                    if (nums[k] > nums[j]) {
                        return true;
                    }
                }
            }
        }
    }
    return false;
}

2、一次遍历法

维护两个常量:minsecond_min,对数组进行遍历。

其中,min表示遍历到当前位置最小的元素,second_min表示从min的位置开始一直到当前位置的第二小元素(也就是比min大的元素中最小的那一个)。

确定这两个元素后,再在后续的元素中找有没有比second_min大的元素,如果有,就表示存在递增的三元子序列。

这样只需要遍历一次数组,时间复杂度为O(N)

示意图

源代码

public boolean increasingTriplet (int[] nums) {
    int min = Integer.MAX_VALUE;
    int second_min = Integer.MAX_VALUE;
    for (int num : nums) {
        if (num<=min) min = num;
        else if (num < second_min) second_min = num;
        else if (num > second_min) return true;
    }
    return false;
}

心得体会

一次遍历法的巧妙就在于设置了两个变量(或者叫指针)来保存递增二元子序列,并实时更新,避免了许多重复的判断。

原文地址:https://www.cnblogs.com/yuzhenzero/p/10237821.html

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

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

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: 输入:

递增的三元子序列

给定一个未排序的数组,请判断这个数组中是否存在长度为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 | 0392. Is Subsequence判断子序列【Python】

LeetCode 0392. Is Subsequence判断子序列[Easy][Python][双指针] Problem LeetCode Given a string s and a string t, check if s is subsequence of t. You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length

[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 491. 递增子序列

给定一个整型数组, 你的任务是找到所有该数组的递增子序列,递增子序列的长度至少是2. 示例: 输入: [4, 6, 7, 7] 输出: [[4, 6], [4, 7], [4, 6, 7], [4, 6, 7, 7], [6, 7], [6, 7, 7], [7,7], [4,7,7]] 说明: 给定数组的长度不会超过15. 数组中的整数范围是 [-100,100]. 给定数组中可能包含重复数字,相等的数字应该被视为递增的一种情况. 这种算法的复杂度O(n^2) 通过dfs的方法找到第i位数右边