【LeetCode】334. Increasing ternary subsequence

1. Topic

Give you an integer array nums to determine whether there is an increasing subsequence of length 3 in this array.

If there is such a triple subscript (i, j, k) and satisfies i <j <k such that nums[i] <nums[j] <nums[k], return true; otherwise, return false.

Example 1 :

输入:nums = [1,2,3,4,5]
输出:true
解释:任何 i < j < k 的三元组都满足题意

Example 2 :

输入:nums = [5,4,3,2,1]
输出:false
解释:不存在满足题意的三元组

Example 3 :

输入:nums = [2,1,5,0,4,6]
输出:true
解释:三元组 (3, 4, 5) 满足题意,因为 nums[3] == 0 < nums[4] == 4 < nums[5] == 6

Tips :

  • 1 <= nums.length <= 105
  • -231 <= nums[i] <= 231 - 1

Advanced : Can you implement a solution with a time complexity of O(n) and a space complexity of O(1)?

Two, solve

1. Dynamic planning

Ideas :

Find the number of elements smaller than the current number each time, to be improved...

Code :

class Solution {
    
    
    public boolean increasingTriplet(int[] nums) {
    
    
        int n = nums.length;
        int[] dp = new int[n];
        Arrays.fill(dp, 1);
        for (int i = 0; i < n; ++i) {
    
    
            for (int j = 0; j < i; ++j) {
    
    
                if (nums[j] < nums[i]) {
    
    
                    dp[i] = Math.max(dp[i], dp[j] + 1);
                }
                if (dp[i] >= 3) return true;
            }
        }
        return false;
    }
}

Time complexity : O (n 2) O(n^2)O ( n2 )
Time complexity:O (n) O(n)O ( n )

2. Traverse back and forth

Ideas :

定义:
forward[i]:从前向后遍历,保存[0, i]之间最小元素值。
backward[i]:从后向前遍历,保存[i, n - 1]间最大元素值。

例如:
nums[i]: 8 3 5 1 6
forwa[i]: 8 3 3 1 1
backw[i]:8 6 6 6 6

Code :

class Solution {
    
    
    public boolean increasingTriplet(int[] nums) {
    
    
        int n = nums.length;
        if (n < 3) return false;
        int[] f = new int[n]; f[0] = nums[0];
        int[] b = new int[n]; b[n-1] = nums[n-1];
        for (int i = 1; i < n; i++) {
    
    
            f[i] = Math.min(f[i - 1], nums[i]);
        }
        for (int i = n - 2; i >= 0; i--) {
    
    
            b[i] = Math.max(b[i + 1], nums[i]);
        }
        for (int i = 0; i < n; i++) {
    
    
            if (f[i] < nums[i] && nums[i] < b[i]) return true;
        }
        return false;
    }
}

Time complexity : O (n) O(n)O ( n )
time complexity:O (n) O(n)O ( n )

3. Double pointer

Ideas :

Question: When an increasing sequence of length 2 has been found, and a smallsmaller number comes, why can it be replaced directly small, smalland the sum is midnot incremented by index?

Note: If the current small=3 and mid=5, then a 1 comes, if you don’t replace small with 1, then when the next number is 2, and a 3 is followed, there will be no way to find [1,2, 3] Increment the array! In other words, the replacement of the minimum value is to better update the intermediate value later.

In addition, even if a small update, behind this small mid, did not strictly comply with ascending order, but it implies the truth is that there is a number num, small < num && num < midthe minimum in midfront. Accordingly, when the subsequent occurrence is greater than the mid value, the current through smalland midinference, there are indeed increasing sequence length of 3. Therefore, this replacement will not interfere with subsequent calculations!

Code :

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

Time complexity : O (n) O(n)O ( n )
time complexity:O (1) O(1)O ( 1 )

Three, reference

1. Detailed analysis of C++ linear time complexity, defeating 98%
2. [334. Incremental ternary subsequence] Three ideas (dynamic programming + double pointer + forward and backward traversal)
3. Python3 is extended to the general situation to find whether there is a length Ascending subsequence of n
4. My way to approach such a problem. How to think about it? Explanation of my think flow.
5. My java solution works for not just triplet

Guess you like

Origin blog.csdn.net/HeavenDan/article/details/115015624