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

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. 阿里云ECS服务器(ubuntu)下基本配置以及升级git

    最近需要在阿里云服务器上远程搭建调试环境,这里把遇到的问题做一下记录: 1.ECS Linux解决SSH会话连接超时问题 用SSH客户端(我使用的Xshell)连接linux服务器时,经常会出现与服务 ...

  2. redis高可用之REDIS SENTINEL

    1. Redis主从配置 1.1. 设置主从复制 Master <= Salve 10.24.6.5:6379 <= 10.24.6.7:6379 1.2.   取消主从复制 1.3.   ...

  3. 敏捷软件开发:原则、模式与实践——第12章 ISP:接口隔离原则

    第12章 ISP:接口隔离原则 不应该强迫客户程序依赖并未使用的方法. 这个原则用来处理“胖”接口所存在的缺点.如果类的接口不是内敛的,就表示该类具有“胖”接口.换句话说,类的“胖”接口可以分解成多组 ...

  4. 奇怪的cab_xxxx_x文件

    最近一段时间发现C盘老是提示空间紧张(显示为红色),之前清理了一次系统盘的\Windows\Temp文件夹,发现了很多文件名类似cab_xxxx_x的cab文件,大概占用了五六个G的空间,当时没太在意 ...

  5. 使用ExposedObject对Asp.net MVC中匿名类型的JsonResult做单元测试

    返回JsonResult是MVC中的常见返回值类型,而且简单方便的方式是结合匿名类型一起使用. 比如: public ActionResult PreviewEmail() { …… return J ...

  6. Effective Java 11 Override clone judiciously

    Principles If you override the clone method in a nonfinal class, you should return an object obtaine ...

  7. MongoDB学习笔记——数据库操作

    使用use数据库名称来创建数据库,如果该数据库已经存在则返回这个数据库 语句格式:use DATABASE_NAME >use mynewdb switched to db mynewdb 使用 ...

  8. java使用httpcomponents发送get请求

    一.适用场景 在ESTful webservice中,get方法一般都是用来获取数据.我们可以使用httpcomponents组件来完成调用. 如我们需要发起get请求,携带的参数都是附加到请求的ur ...

  9. 安装SQL Server2008,要重启机器,解决办法

    安装SQL Server2008时,总提示有挂起,要重启机器:重启之后还是有相应的提示,该怎么办呢? 其实只要删除一个注册表项就可以了: 1.  打开注册表编辑器 开始菜单—>运行->re ...

  10. linux: 获取监听指定端口的进程PID

    在 linux 下经常需要杀死(重启)监听某端口的进程, 因此就写了一个小脚本, 通过 ss 命令获取监听制定端口的进程 PID, 然后通过 kill 命令结束掉进程: #!/bin/sh # set ...