这个题是说看一个没有排序的数组里面有没有三个递增的子序列,也即:

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的更多相关文章

  1. [LeetCode] 334. Increasing Triplet Subsequence 递增三元子序列

    Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...

  2. 【LeetCode】334. Increasing Triplet Subsequence 解题报告(Python)

    [LeetCode]334. Increasing Triplet Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...

  3. 【LeetCode】Increasing Triplet Subsequence(334)

    1. Description Given an unsorted array return whether an increasing subsequence of length 3 exists o ...

  4. 334. Increasing Triplet Subsequence

    Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...

  5. 334. Increasing Triplet Subsequence My Submissions Question--Avota

    问题描述: Given an unsorted array return whether an increasing subsequence of length 3 exists or not in ...

  6. 334 Increasing Triplet Subsequence 递增的三元子序列

    给定一个未排序的数组,请判断这个数组中是否存在长度为3的递增的子序列.正式的数学表达如下:    如果存在这样的 i, j, k,  且满足 0 ≤ i < j < k ≤ n-1,    ...

  7. 【leetcode】Increasing Triplet Subsequence

    Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...

  8. 334. Increasing Triplet Subsequence(也可以使用dp动态规划)

    Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...

  9. [LeetCode] Longest Increasing Subsequence 最长递增子序列

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

随机推荐

  1. 手动删除webapps下项目,导致Document base %TOMCAT_HOME%\webapps\XXX does not exist or is not a readable directory

    删除 %TOMCAT_HOME%\conf\XXX.xml , 再次eclipse中重新启动tomcat,错误就会消失.

  2. Effective Java 21 Use function objects to represent strategies

    Theory In the Java the function pointers is implemented by the declaring an interface to represent s ...

  3. tomcat支持多少并发

    作者:孟男男 来源:https://zhidao.baidu.com/question/1445941399668603020.html Tomcat的最大并发数是可以配置的,实际运用中,最大并发数与 ...

  4. Windows7下安装MongoDB(转)

    1.下载 地址:http://www.mongodb.org/downloads(32位还是64位自行选择). 我下载的是:mongodb-win32-x86_64-2.4.5.zip 2.解压 把m ...

  5. 在安装SqlServer2008时,有一项安装程序支持规则,为什么重新启动计算机那一项总是失败

    1.运行 regedit 打开注册表编辑器. 2.依次展开HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager目录,找 ...

  6. Linux gcc命令

    一.简介 GCC 的意思也只是 GNU C Compiler 而已.经过了这么多年的发展,GCC 已经不仅仅能支持 C 语言:它现在还支持 Ada 语言.C++ 语言.Java 语言.Objectiv ...

  7. 常用的Linux可插拔认证模块(PAM)应用举例(一)

    pam_access.so模块 pam_access.so模块主要的功能和作用是根据主机名(包括普通主机名或者FQDN).IP地址和用户实现全面的访问控制.pam_access.so模块的具体工作行为 ...

  8. Ubuntu安装Eclips for C/C++及相关配置

    1,安装JDK: sudo apt-get install aptitude sudo aptitude search openjava sudo aptitude install openjdk-7 ...

  9. Hive UDF 实验1

    项目中使用的hive版本低于0.11,无法使用hive在0.11中新加的开窗分析函数. 在项目中需要使用到row_number()函数的地方,有人写了udf来实现这个功能. new java proj ...

  10. 怎么运用好ZBrush中Magnify膨胀笔刷

    Magnify膨胀笔刷是ZBrush笔刷中经常使用的,利用该笔刷可绘制中心向四周膨胀的效果.本文内容向大家介绍ZBrush®中膨胀笔刷以便大家熟悉它的用法和特性. 查看更多内容请直接前往:http:/ ...