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 ...
随机推荐
- Linux Shell编程(11)——退出和退出状态
exit命令一般用于结束一个脚本,就像C语言的exit一样.它也能返回一个值给父进程.每一个命令都能返回一个退出状态(有时也看做返回状态).一个命令执行成功返回0,一个执行不成功的命令则返回一个非零值 ...
- Delphi实现WebService带身份认证的数据传输
WebService使得不同开发工具开发出来的程序可以在网络连通的环境下相互通信,它最大的特点就是标准化(基于XML的一系列标准)带来的跨平台.跨开发工具的通用性,基于HTTP带来的畅通无阻的能力(跨 ...
- std::min 与std::max 的 Compiler Error C2780
代码 #include<iostream>#include <algorithm> // std::min#undef minint main(){ float a =15.0 ...
- udev:renamed network interface eth0 to eth1
删除/etc/udev/rules.d/70-persistent-net.rules这个文件,重启
- 2014年河南省第七届ACM大赛总结
虽然大赛已经结束了两天,不过比赛的场景还是不断地在眼前回放,一遍遍,这次的比赛给了我很深刻的感悟还有教训. 刚开始比赛选择了贩卖武器那道题,也是全场到最后唯一没有被人做出来的一道题,策略的严重错误,大 ...
- hdu 2824 The Euler function(欧拉函数)
如果打表的话会超内存,我想到了一种方法解决这个问题.题目给出的数据时3000000,我将三百万分成300个数据,将整万的数据存储下来,计算的时候,先计算x和y之间整万的数据,然后再计算零散数据. 想法 ...
- IAAS云计算产品畅想-公有云主机产品优势
关于云计算的优势介绍真是太多太多了,但是说实话准确性欠妥. 云计算也是有很多细分的: 公有云.私有云.混合云 IAAS.PAAS.SAAS 园区云.行业云(医疗云.教育云等等) 说起优点来,绝对不能一 ...
- ASP.NET常用导出Excel方法汇总
本文转载:http://mattberseth.com/blog/2007/04/export_gridview_to_excel_1.html http://geekswithblogs.net/a ...
- [Node.js] Scraping Dynamic JavaScript Websites with Nightmare
Many websites have more than just simple static content. Dynamic content which is rendered by JavaSc ...
- iOS 百度地图 小的特点demo
先上图的样子 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMDEyMzIwOA==/font/5a6L5L2T/fontsize/400/fill ...