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. Examples:
Given [1, 2, 3, 4, 5],
return true. Given [5, 4, 3, 2, 1],
return false.

Naive Solution: use DP, Time O(N^2), Space O(N)

  dp[i] represents the length of longest increasing subsequence till i including element i in nums array. dp[i] is initialized to be 1.

  dp[i] = max(dp[i], dp[j]+1), where j is an index before i

 public class Solution {
public boolean increasingTriplet(int[] nums) {
int[] dp = new int[nums.length];
for (int i=0; i<nums.length; i++) {
dp[i] = 1;
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;
}
}

Better Solution: keep two values. Once find a number bigger than both, while both values have been updated, return true.

small: is the minimum value ever seen untill now

big: the smallest value that has something before it that is even smaller. That 'something before it that is even smaller' does not have to be the current min value.

Example:
3,2,1,4,0,5

When you see 5, min value is 0, and the smallest second value is 4, which is not after the current min value.

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

Leetcode: Increasing Triplet Subsequence的更多相关文章

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

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

  2. LeetCode——Increasing Triplet Subsequence

    Question Given an unsorted array return whether an increasing subsequence of length 3 exists or not ...

  3. 【LeetCode】334. Increasing Triplet Subsequence 解题报告(Python)

    [LeetCode]334. Increasing Triplet Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...

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

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

  5. 【LeetCode】Increasing Triplet Subsequence(334)

    1. Description Given an unsorted array return whether an increasing subsequence of length 3 exists o ...

  6. 【leetcode】Increasing Triplet Subsequence

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

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

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

  8. Increasing Triplet Subsequence

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

  9. LeetCode-334. Increasing Triplet Subsequence

    Description: Given an unsorted array return whether an increasing subsequence of length 3 exists or ...

随机推荐

  1. 资源(1)----封装类(连接数据库mysql,分页)

    一,链接MYSQL数据库 class DBDA{ public $host="localhost";//服务器地址 public $uid="root";//数 ...

  2. Natural Language Toolkit

    http://www.nltk.org/ >>> import nltk >>> nltk.download()

  3. backbone-todo案例分析

    todo案例可以到这个地址下载 https://github.com/jashkenas/backbone 添加数据后 todo案例不涉及Router,仅有Model.Collection.View的 ...

  4. 【转】android中最好的瀑布流控件PinterestLikeAdapterView

    [源地址]http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/0919/1696.html 之前我们介绍过一个开源的瀑布流控件Stag ...

  5. 【转】在C#用HttpWebRequest中发送GET/HTTP/HTTPS请求

    http://zhoufoxcn.blog.51cto.com/792419/561934 这个需求来自于我最近练手的一个项目,在项目中我需要将一些自己发表的和收藏整理的网文集中到一个地方存放,如果全 ...

  6. Bluetooth ATT介绍

    目录 1 介绍 2 详细内容 2.1 Attribute Type 2.2 Attribute Handle 2.3 Attribute Handle Grouping 2.4 Attribute V ...

  7. java JDK8 学习笔记——第17章 反射与类加载器

    第十七章 反射与类加载器 17.1 运用反射 反射:.class文档反映了类基本信息,从Class等API取得类信息的方式称为反射. 17.1.1 Class与.class文档 1.java.lang ...

  8. C++ 字符串操作常见函数

    //字符串拷贝,排除指定字符 char *strcpy_exclude_char(char *dst, const int dst_len, const char *src, const char * ...

  9. strcpy之代码的健壮性与可维护性

    strcpy   函数的原型是: char * strcpy(char * strDest,const char * strSrc);    功能:把从strSrc地址开始且含有NULL结束符的字符串 ...

  10. Windows Server 2008标准证书使用记录

    Windows Server 2008标准证书使用记录   近期准备将单位的服务器全部升级到Windows Server 2008,但有一些“遗留”问题需要解决: (1)现在单位还有一台Windows ...