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 ...
随机推荐
- storm-kafka-0.8-plus 源码解析
https://github.com/wurstmeister/storm-kafka-0.8-plus http://blog.csdn.net/xeseo/article/details/1861 ...
- ThinkPad告别蓝快,自己使用VHD安 WIN8.1并成功激活
写在前面:本文WIN8.1激活适合于中国大陆地区国行预装WIN8系统(bios写入WIN8授权)是可激活的,享受正版WIN8系统(同样可以安装WIN8.1系统).比如联想的Y400.Y500.Y480 ...
- 【ECshop错误集锦】解决ECShop发送邮件提示:Error: need RCPT command
ECShop发送邮件报错Error: need RCPT command,经检测,邮件服务器返回的真实错误是501 mail from address must be same as authoriz ...
- TWICImage.SaveToStream内存泄漏的解决办法
这个BUG从2010到XE5一直没改.....只能自己写个函数来搞了 uses ActiveX; procedure WICImageSaveToStream(AWICImage: TWICImage ...
- eclipse Maven -->web project
http://blog.chinaunix.net/uid-26959955-id-3248053.html http://blog.csdn.net/wilsonke/article/details ...
- 备份mysql
#!/bin/bash # 要备份的数据库名,多个数据库用空格分开USER=rootPASSWORD=rootdatabases=("shopnc") # 备份文件要保存的目录ba ...
- nodejs net模块实现socket
var net = require('net'); var client = net.connect({port: 8080}, function() { console.log('连接到服务器!') ...
- python socket 选项
一.int socket(int domain, int type, int protocol) 1.domain -- 指定使用何种的地址类型 PF_INET, AF_INET: Ipv4网络协议P ...
- Servlet Threading Model
Servlet Threading Model The scalability issues of Java servlets are caused mainly by the server thre ...
- FastReport的交叉表实际使用的一个例子
计算发行-->定义份数月表(打开)出现 PosFraisPaysInput选择时间段后,点击“打印”.这个设计表格,就是交叉表. 交叉表的特点是:数据库是一条一条并列的但是出来的结果却是:横向是 ...