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. apache2.2版本 configuration error: couldn't perform authentication. AuthType not set!: /

    configuration error:  couldn't perform authentication. AuthType not set!: /  500服务器错误 解决方案: <Dire ...

  2. Python 之父谈放弃 Python:我对核心成员们失望至极!

    Python 之父讲述退位原因,以及 Python 的未来将何去何从. ​ 在 Python 社区,Python 的发明者 Guido Van Rossum 被称为 “仁慈的终生独裁者”(BDFL,B ...

  3. Apache Lucene初探

    讲解之前,先来分享一些资料 首先,学习任何一门新的亦或是旧的开源技术,百度其中一二是最简单的办法,先了解其中的大概,思想等等.这里就贡献一个讲解很到位的ppt 这是Lucene4.0的官网文档:htt ...

  4. ROC与AUC学习

    全文转自:https://www.cnblogs.com/gatherstars/p/6084696.html#commentform 这篇真的讲的清楚明白!要多复习!加深记忆! 1.概述 AUC(A ...

  5. selenium 模块

    介绍 selenium最初是一个自动化测试工具,而爬虫中使用它主要是为了解决requests无法直接执行JavaScript代码的问题 selenium本质是通过驱动浏览器,完全模拟浏览器的操作,比如 ...

  6. SSH查看Linux系统是32位还是64位?

    Linux下如何明确地查看操作系统的位数   如何知晓操作系统是32位还是64位?这里介绍一种简单的方式: [root@localhost mysql-5.1.57]# getconf LONG_BI ...

  7. 微信小程序组件navigator

    导航navigator:官方文档 Demo Code: // redirect.js navigator.js Page({ onLoad: function(options) { this.setD ...

  8. Log4net 自定义字段 写入Oracle 使用ODP.NET Managed驱动

    一.环境说明: 开发工具:vs2010   ,数据库:oracle 11g ,版本:log4net的目前最新版本1.2.13.0    :  Oracle.ManagedDataAccess.dll ...

  9. 跨域问题-cors

    什么是跨域如大家所知,出于安全考虑,浏览器会限制脚本中发起的跨站请求.比如,使用 XMLHttpRequest 对象发起 HTTP 请求就必须遵守同源策略(same-origin policy). 具 ...

  10. ionic简介

    CordovaCordova是贡献给Apache后的开源项目,是从PhoneGap中抽出的核心代码,是驱动PhoneGap的核心引擎.提供了一组设备相关的API,通过这组API,移动应用能够以Java ...