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

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. This is usually caused by using Struts tags without the associated filter. Struts tags are only usable when the request has p

    2014-09-16 15:47:51.590:WARN:oejs.ErrorPageErrorHandler:EXCEPTION org.apache.jasper.JasperException: ...

  2. MongoDB 初识1: 启动MongoDB

    Mongodb可作为网络服务器运行,客户端可连接到该服务器并执行操作. 1. 首先,用户必须下载Mongodb并解压,运行mongod命令启动实例 下载地址:http://www.mongodb.or ...

  3. C#操作XML小结(转)

    一.简单介绍 using System.Xml; //初始化一个xml实例 XmlDocument xml=new XmlDocument(); //导入指定xml文件 xml.Load(path); ...

  4. 在MFC中添加用户自定义消息

    1.定义一个宏 (用户自定义消息从WM_USER开始) #define WM_XXXXX WM_USER+数值 2.声明一个函数并实现 afx_msg LRESULT OnXXXXX( WPARAM ...

  5. 一个自定义 HBase Filter -“通过RowKeys来高性能获取数据”

    摘要: 大家在使用HBase和Solr搭建系统中经常遇到的一个问题就是:“我通过SOLR得到了RowKeys后,该怎样去HBase上取数据”.使用现有的Filter性能差劲,网上也没有现成的自定义Fi ...

  6. 本地Git仓库与Github远程仓库同步

    在本地创建了一个Git仓库后,还想在Github创建一个Git仓库,并使其远程同步.1.在电脑的用户主目录下有无.ssh目录,若有看是否有id_rsa和id_rsa.pub文件.若无,则创建SSH K ...

  7. openfire+asmack搭建的安卓即时通讯(二) 15.4.9

    上期没有放成果图呢!忘了=-=,这就是上次的成果图,textview里面会显示登陆的名字(这个是默认管理员帐号=-=) 好吧,登陆了服务器我们就有了交互的功能啦可以说是前进了一大步呢!下面能我们就要试 ...

  8. JSON对象和字符串之间的相互转换JSON.stringify(obj)和JSON.parse(string)

    在Firefox,chrome,opera,safari,ie9,ie8等高级浏览器直接可以用JSON对象的stringify()和parse()方法. JSON.stringify(obj)将JSO ...

  9. ZOJ 1111 Poker Hands --复杂模拟

    昨天晚上写的,写了一个多小时,9000+B,居然1A了,爽. 题意:玩扑克,比大小.规则如下: 题意很简单,看过赌神的人都知道,每人手中5张排,比牌面大小,牌面由大到小分别是(这里花色无大小),级别从 ...

  10. 2014 Super Training #8 B Consecutive Blocks --排序+贪心

    当时不知道怎么下手,后来一看原来就是排个序然后乱搞就行了. 解法不想写了,可见:http://blog.csdn.net/u013368721/article/details/28071241 其实就 ...