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 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的更多相关文章
- [LeetCode] Increasing Triplet Subsequence 递增的三元子序列
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...
- LeetCode——Increasing Triplet Subsequence
Question Given an unsorted array return whether an increasing subsequence of length 3 exists or not ...
- 【LeetCode】334. Increasing Triplet Subsequence 解题报告(Python)
[LeetCode]334. Increasing Triplet Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...
- [LeetCode] 334. Increasing Triplet Subsequence 递增三元子序列
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...
- 【LeetCode】Increasing Triplet Subsequence(334)
1. Description Given an unsorted array return whether an increasing subsequence of length 3 exists o ...
- 【leetcode】Increasing Triplet Subsequence
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...
- [Swift]LeetCode334. 递增的三元子序列 | Increasing Triplet Subsequence
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...
- Increasing Triplet Subsequence
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...
- LeetCode-334. Increasing Triplet Subsequence
Description: Given an unsorted array return whether an increasing subsequence of length 3 exists or ...
随机推荐
- poj1979
#include<stdio.h>int map[4][4]={'.','.','.','.', '#','.','.','.', '.','#','.','.', ...
- JAVA Callable
Listing -. Calculating Euler’s Number e import java.math.BigDecimal; import java.math.MathContext; i ...
- Asp.net forms认证注意事项
1.N台服务器配置文件的相关配置要一致 <authentication mode="Forms"> <forms timeout="3600" ...
- c 生成随机不重复的整数序列
#include <stdio.h> #include <malloc.h> #include <stdlib.h> #include <time.h> ...
- oracle常用的SQL语句
一些常用的SQL语句: --建表 create table adolph (id number(10,0), name varchar2(20), ...
- [LeetCode]题解(python):083 - Remove Duplicates from Sorted List
题目来源 https://leetcode.com/problems/remove-duplicates-from-sorted-list/ Given a sorted linked list, d ...
- Selenium2学习-002-Selenium2 Web 元素定位及 XPath 编写演示示例
此文主要对 Selenium2 的 Web 元素定位及 XPath 编写示例,敬请各位亲们参阅,共同探讨.若有不足之处,敬请各位大神指正,不胜感激! 通过 Firefox(火狐)浏览器的插件 Fire ...
- JAVA并发编程的艺术
CAS有两个特点: 1.for循环 2.compareAndSet(可能别的线程先改变然后又重置,此时CAS是成功的,也就是CAS执行的过程中,可能多个线程对此变量做了修改,而不是各个线程互斥的修改) ...
- Java的浮点数和整数的进制转换
整数的表达 –原码:第一位为符号位(0为正数,1为负数) –反码:符号位不动,原码取反 –负数补码:符号位不动,反码加1 –正数补码:和原码相同 -6 5 原码 10000110 0 ...
- Android studio关于真机调试DDMS中的data文件夹打不开的解决方法
由于做开发的时候想打开查看数据库存放的内容,在eclipse中数据库文件默认就在/data/data/应用包名/databases/数据库名,而用Android studio打开DDMS下面找时发现点 ...