LeetCode 334 Increasing Triplet
这个题是说看一个没有排序的数组里面有没有三个递增的子序列,也即:
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.
大家都知道这个题有很多解法,然而题主丧心病狂地说要O(n)的时间复杂度和O(1)的空间复杂度。
我当时考虑的是找三个递增的数,中间那个数比较重要,所以我们可以遍历该数组,检查每个元素是不是递增序列的中间那个数,假设我们叫它为p。
那要成为p有什么条件呢?召唤画面感。
p把整个数组划分成两部分。如果在前面有比p小的,且在后面有比p大的,那么成了。反之,(1)如果前面最小的数都比p大,(2)或者后面最大的数都比p小,那么p肯定不是”中间那个数“,对吧?
那么我们从第二个数开始,检查它是不是p。满足(1),其实可以通过求一个数组最小值来做到,从左到右,如果一个元素是当前最小的,那么肯定就满足(1)了。我们就可以把它从数组里面排除了。同理,从右到左,如果一个元素是当前最大的,那么满足(2)了,排除完了还有剩下的,就是说明有戏了嘛。但是怎么排除呢。。。?人家又不许有临时数组啊。。。O(1)的时间复杂度啊。只有耍机灵了。直接在数组里面吧排除了的数设置成一个invalid number...OMG。玛德智障啊。。。
bool increasingTriplet(vector<int>& nums)
{ vector<int>::iterator it;
int min = INT_MAX;
for(it = nums.begin(); it < nums.end(); it++) {
if(*it <= min) {
min = *it;
*it = INT_MIN;//i feel there should not be such element...
}
}
vector<int>::reverse_iterator rit = nums.rbegin();
int max = INT_MIN;
for(; rit < nums.rend(); rit++) {
if (*rit >= max && *rit != INT_MIN) {
max = *rit;
} else if (*rit != INT_MIN){
return true;
}
}
return false;
}
捂脸。。居然过了。
但是时间就。。。
于是好奇的猫看了下讨论。天。。好简单的答案。
if (numsSize < ) return false;
int l = nums[], m = 0x7fffffff;
for (int i = ; i < numsSize; i++) {
int a = nums[i];
if (a <= l) l = a;
else if (a < m) m = a;
else if (a > m) return true;
}
return false;
你萌看懂了伐?
其实他也认为”中间“那个数是很重要的。所以就是用m来代替。m之前始终有个比他小的数(l,或曾经的l)。所以如果当前遍历到的元素大于了m,那么就return true。
LeetCode 334 Increasing Triplet的更多相关文章
- [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 解题报告(Python)
[LeetCode]334. Increasing Triplet Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...
- 【LeetCode】Increasing Triplet Subsequence(334)
1. Description Given an unsorted array return whether an increasing subsequence of length 3 exists o ...
- 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 My Submissions Question--Avota
问题描述: Given an unsorted array return whether an increasing subsequence of length 3 exists or not in ...
- 334 Increasing Triplet Subsequence 递增的三元子序列
给定一个未排序的数组,请判断这个数组中是否存在长度为3的递增的子序列.正式的数学表达如下: 如果存在这样的 i, j, k, 且满足 0 ≤ i < j < k ≤ n-1, ...
- 【leetcode】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 ...
- [LeetCode] Longest Increasing Subsequence 最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
随机推荐
- MongoDB 的 GridFS 详细分析
GridFS简介 GridFS是MongoDB中的一个内置功能,可以用于存放大量小文件. http://www.mongodb.org/display/DOCS/GridFS http://www.m ...
- 三种另外的循环 while{} 和do{}while{}还有switch case
while的写法 var i=0; while(i<5){ document.write("12378<br />"); i++;} while(true)-- ...
- 如何正大光明的使用 google 进行搜索
对于程序猿来说,不能使用google,是一大痛所在,今天在使用 百度网盘 搜索时,突然发现 ,他能同时使用 baidu和 google进行搜索,于是想到了这个正大光明的使用google 的方法,不需要 ...
- 用shell脚本写一个for循环
一.输出十遍北京 for((i=1;i<10;i++))> do> echo '北京';> done 二.死循环 for((;;))do#java -jar producer. ...
- oc-基本语法
一.第一个oc程序 #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { NSLog(@& ...
- mybatis的#{}和${}的区别以及order by注入问题
前言略,直奔主题.. #{}相当于jdbc中的preparedstatement ${}是输出变量的值 你可能说不明所以,不要紧我们看2段代码: String sql = "select * ...
- linux command intro2 vi
vi cusor : 0 : to the beginning of the current line $ : to the end of the current line G : to the la ...
- linux sed命令
一.初识sed 在部署openstack的过程中,会接触到大量的sed命令,比如 # Bind MySQL service to all network interfaces. sed -i 's/1 ...
- 边工作边刷题:70天一遍leetcode: day 72
Missing Range 要点:题简单,这类题的特点都是记录上一步的状态,比如这题是end 错误点: 三种情况:一是连续的,即和上一个end差1,而是中间只差1个数,没有'->',最后是大于1 ...
- UVALive 6255 Kingdoms --状态搜索
题意:n个国家,给出国家间相互的债务关系,每个国家如果债务>收入就要破产,破产后该国的所有债务关系全部清除,第一个破产的国家不同有可能造成最后的没破产的国家的不同,问哪些国家有可能成为独自存活的 ...