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 ...
随机推荐
- BZOJ1666: [Usaco2006 Oct]Another Cow Number Game 奶牛的数字游戏
1666: [Usaco2006 Oct]Another Cow Number Game 奶牛的数字游戏 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 5 ...
- Defining as a "long" or "int" type throws an error on startup
solr启动时候,报如下异常: [java] view plaincopy SEVERE: org.apache.solr.common.SolrException at org.a ...
- 如何查看jar包的版本号?
jar包根目录里的META-INF目录下的MANIFEST.MF文件里一般有会记录版本信息,可以到这个文件里查看 打开Java的JAR文件我们经常可以看到文件中包含着一个META-INF目录,这个 ...
- 【转】蓝牙物理链路类型:SCO和ACL链路
原文网址:http://blog.chinaunix.net/uid-23193900-id-3272233.html 蓝牙物理链路ACL(Asynchronous Connectionless), ...
- Java---基于TCP协议的相互即时通讯小程序
这是几年前,新浪的一个面试题~要求是3天之内实现~ 通过TCP 协议,建立一个服务器端. 通过配置服务器端的IP和端口: 客户端之间就可以相互通讯~ 上线了全部在线用户会收到你上线的通知. 下线了全部 ...
- UVa 10294 Arif in Dhaka (First Love Part 2)(置换)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=35397 [思路] Polya定理. 旋转:循环节为gcd(i,n) ...
- JavaScript高级程序设计19.pdf
注册处理程序 navigator.registerContentHandler("applicat/rss+xml","http://www.somereader.com ...
- 《Numerical Methods》-chaper7-解线性方程组的直接方法和最小二乘问题
基于我们在线性代数中学习过的知识,我们知道解线性方程组本质上就是Gauss消元,也就是基于增广矩阵A的矩阵初等变换.关于数学层面的内容这里不做过多的介绍,这里的侧重点是从数值计算的角度来看这些常见的问 ...
- eclipse运行项目特别慢,出现Java heap space溢出
在eclipse中可用为JVM设置参数:Window-->Preferences-->Java-->Installed JREs然后选中你安装的jre-->Edit--> ...
- Linux 搭建SVN server
一. SVN 简单介绍 Subversion(SVN) 是一个开源的版本号控制系統, 也就是说 Subversion 管理着随时间改变的数据. 这些数据放置在一个中央资料档案库 (repository ...