Question

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.

Solution

数学逻辑。

Code

class Solution {
public:
bool increasingTriplet(vector<int>& nums) {
int c1 = INT_MAX, c2 = INT_MAX;
for (int i : nums) {
if (i <= c1) // 当前最小的数
c1 = i;
else if (i <= c2) // 第二或者第三大的数
c2 = i;
else // 如果存在一个比当前最小的大,且比第二大的数大(能够成为第二大,说明它之前有比它小的),那么这个数前面肯定有两个比它小的
return true;
}
return false; }
};

LeetCode——Increasing Triplet Subsequence的更多相关文章

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

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

  2. Leetcode: Increasing Triplet Subsequence

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

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

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

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

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

  5. 【LeetCode】Increasing Triplet Subsequence(334)

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

  6. 【leetcode】Increasing Triplet Subsequence

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

  7. [Swift]LeetCode334. 递增的三元子序列 | Increasing Triplet Subsequence

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

  8. Increasing Triplet Subsequence

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

  9. LeetCode-334. Increasing Triplet Subsequence

    Description: Given an unsorted array return whether an increasing subsequence of length 3 exists or ...

随机推荐

  1. ASP.NET Identity 2集成到MVC5项目--笔记01

    Identiry2是微软推出的Identity的升级版本,较之上一个版本更加易于扩展,总之更好用.如果需要具体细节.网上具体参考Identity2源代码下载 参考文章 在项目中,是不太想直接把这一堆堆 ...

  2. 拼团商品列表页 分析 js代码行位置对执行的影响和window.onload的原理 setTimeout传参

    w TypeError : Cannot set property 'innerHTML' of nullTypeError : Cannot set property 'value' of null ...

  3. LINUX内核分析20133201

    实验:通过汇编一个简单的C程序,分析汇编代码理解计算机是如何工作的 学号:20133201 姓名:李冬辉 注: 原创作品转载请注明出处 +<Linux内核分析>MOOC课程http://m ...

  4. Linux进程虚拟地址空间管理2

    2017-04-12 前篇文章对Linux进程地址空间的布局以及各个部分的功能做了简要介绍,本文主要对各个部分的具体使用做下简要分析,主要涉及三个方面:1.MMAP文件的映射过程 2.用户 内存的动态 ...

  5. 3类数据库的联动:mysql、mongodb、redis

    3类数据库的联动:mysql.mongodb.redis from pymysql import * from pymongo import * from redis import * class M ...

  6. JS根据userAgent值来判断浏览器的类型及版本【转】

    转自:http://blog.csdn.net/sunlovefly2012/article/details/22384255 JavaScript是前端开发的主要语言,我们可以通过编写JavaScr ...

  7. OOM问题分析

    一.背景 在实际的开发中,性能问题的分析一直是运维团队的痛点,无论是缓慢内存溢出还是迅速的内存爆炸,对系统和业务的破坏都是快速而巨大的,此贴分享下简单的分析内存问题的经验. 二.相关名词 分代:根据对 ...

  8. boost::any 用法

    boost::any可以存放任何类型的C++类型,也可以是用户自定义的类型.非常方便,可以很方便的满足在运行过程中判断数据类型,从而进行相关的操作. 函数原型: // In header: <b ...

  9. PKU 2506 Tiling(递推+高精度||string应用)

    题目大意:原题链接有2×1和2×2两种规格的地板,现要拼2×n的形状,共有多少种情况,首先要做这道题目要先对递推有一定的了解.解题思路:1.假设我们已经铺好了2×(n-1)的情形,则要铺到2×n则只能 ...

  10. maven install jdk版本自动降为1.7

    开发过程中遇到了一个奇怪的现象. IDEA中所有的设置都改成了1.8,但是在执行maven install时却自动降为1.7,报错提示: [ERROR] Failed to execute goal ...