Middle-题目44:334. Increasing Triplet Subsequence

原创 2016年05月31日 16:05:17

题目原文:
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%

版权声明:完整版Leetcode题解请出门左转https://github.com/cmershen1/leetcode/tree/master/docs

相关文章推荐

leedCode 334 Increasing Triplet Subsequence

问题描述: Given an unsorted array return whether an increasing sub sequence of length 3 exists or not...

LeetCode#334. Increasing Triplet Subsequence

334. Increasing Triplet Subsequence Given an unsorted array return whether an increasing subseq...

334. Increasing Triplet Subsequence

Task: Given an unsorted array return whether an increasing subsequence of length 3 exists or not in...

LeetCode 334. Increasing Triplet Subsequence

前言写这篇文章的主要目的是为了熟悉一下MarkDown这个编辑方式。因为昨天有道云笔记发布对MarkDown的支持,后知后觉的我才发现了这个编辑器,因此打算写一篇小算法来练练手。这个算法也不算难,虽然...

LeetCode 334. Increasing Triplet Subsequence

LeetCode 334. Increasing Triplet Subsequence 时间复杂度为O(n)

334. Increasing Triplet Subsequence

334. Increasing Triplet Subsequence Given an unsorted array return whether an increasing subseq...

Middle-题目33:300. Longest Increasing Subsequence

题目原文: Given an unsorted array of integers, find the length of longest increasing subsequence.For ex...

Increasing Triplet Subsequence

题目描述: Given an unsorted array return whether an increasing subsequence of length 3 exists or no...

图(深度优先搜索)491. Increasing Subsequences[Middle]03-18

题目: Given an integer array, your task is to find all the different possible increasing subsequenc...
  • xiegr
  • xiegr
  • 2017-03-18 22:32
  • 48

leetcode_middle_44_386. Lexicographical Numbers

题意: 给一个数n,按照字典顺序返回1-n。 分析: 什么是字典顺序呢?观察例子,分析是按位比较,先比较最高位。 我们按照人脑思维,是应该怎么选出排序呢?首先1,10,11,12......
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)