334. Increasing Triplet Subsequence My Submissions Question--Avota
问题描述:
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.
题意:
给定一无序数组,判断其中是否存在长度为3的递增子序列。
解题思路:
此问题可转化为先找长度为2的递增序列(记为first, second)然后再判断后面是否有比second更大的数。此时我们只需维护first, second,但可能出现如3,4,1这种情况,这时我们就需要另一个数temp表示位于递增序列后面但比first更小的数。之后对于数组中的每一个数nums[i],只需根据其在temp,first,second三者之间的位置来判断结果并维护这三个数,其中包括[负无穷, temp],(temp, first],(first, second],(second, 正无穷)四个区间
示例代码:
class Solution {
public:
bool increasingTriplet(vector<int>& nums) {
int len = nums.size();
if(len < 3){
return false;
}
int first = nums[0], second = INT_MAX, temp = nums[0];
for(int i = 1; i < len; i++){
if(nums[i] > second){
return true;
}
if(nums[i] > first){
second = nums[i];
}
else if(nums[i] <= temp){
temp = nums[i];
}
else{
first = temp;
second = nums[i];
}
}
return false;
}
};
334. Increasing Triplet Subsequence My Submissions Question--Avota的更多相关文章
- 【LeetCode】334. Increasing Triplet Subsequence 解题报告(Python)
[LeetCode]334. Increasing Triplet Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...
- 334. 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 递增三元子序列
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...
- 334. Increasing Triplet Subsequence(也可以使用dp动态规划)
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...
- 334 Increasing Triplet Subsequence 递增的三元子序列
给定一个未排序的数组,请判断这个数组中是否存在长度为3的递增的子序列.正式的数学表达如下: 如果存在这样的 i, j, k, 且满足 0 ≤ i < j < k ≤ n-1, ...
- 【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
Question Given an unsorted array return whether an increasing subsequence of length 3 exists or not ...
- [LeetCode] 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 ...
随机推荐
- luoguP2266 爱的距离
题目:http://www.luogu.org/problem/show?pid=2266 题解:感觉题意不清,就去瞅题解了T_T 然后发现好水... 类似于MST,我们把边从小到大加进去就可以了. ...
- Linux Shell编程(27)——子shell
运行一个shell脚本时会启动另一个命令解释器. 就好像你的命令是在命令行提示下被解释的一样, 类似于批处理文件里的一系列命令.每个shell脚本有效地运行在父shell(parent shell)的 ...
- 【转】VC的MFC中重绘函数的使用总结(整理)
原文网址:http://www.cnblogs.com/x8023z/archive/2008/12/09/mfc33.html 在刷新窗口时经常要调用重绘函数MFC提供了三个函数用于窗口重绘Inva ...
- 数据导出到Excel中
自己修改后的一个数据导出到Excel的方法,粘出来与大家共享. 只需要将 System.Web.HttpContext.Current.Response.Charset = ...
- poj 1704 Georgia and Bob(阶梯博弈)
Georgia and Bob Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8656 Accepted: 2751 D ...
- iOS设备隐藏StateBar
//隐藏StateBar - (BOOL)prefersStatusBarHidden { returnYES; }
- 怎么把GPUImageFIlter处理过的图像保存成UIImage
总共有两种方法能够把GPUImage处理过的图片转化成UIImage 方法一: UIImage *inputImage = [UIImage imageNamed:@"Lambeau ...
- RTP, RTCP, RTSP 协议介绍
流媒体是边下载边播放的方式, 是视频会议.IP电话等应用场合的技术基础. 为什么TCP/IP协议就不能满足多媒体通信的要求呢?因为TCP有以下4个特点:1.TCP重传机制2.TCP ...
- 自主架设VOIP系统
my.oschina.net/fcboys/blog/2695 FXS (Foreign Exchange Station) FXS is an interface which drives a te ...
- 开源跨平台的3D渲染软件
http://www.blender.org/ KVM是Kernel-based Virtual Machine的缩写; http://kiwik.github.io/openstack/2013/1 ...