LeetCode——Increasing Triplet Subsequence
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的更多相关文章
- [LeetCode] Increasing Triplet Subsequence 递增的三元子序列
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...
- Leetcode: Increasing Triplet Subsequence
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...
- 【LeetCode】334. Increasing Triplet Subsequence 解题报告(Python)
[LeetCode]334. Increasing Triplet Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...
- [LeetCode] 334. Increasing Triplet Subsequence 递增三元子序列
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...
- 【LeetCode】Increasing Triplet Subsequence(334)
1. Description Given an unsorted array return whether an increasing subsequence of length 3 exists o ...
- 【leetcode】Increasing Triplet Subsequence
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...
- [Swift]LeetCode334. 递增的三元子序列 | Increasing Triplet Subsequence
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...
- Increasing Triplet Subsequence
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...
- LeetCode-334. Increasing Triplet Subsequence
Description: Given an unsorted array return whether an increasing subsequence of length 3 exists or ...
随机推荐
- 【Python数据挖掘】回归模型与应用
线性回归 ( Linear Regression ) 线性回归中,只包括一个自变量和一个因变量,且二者的关系可用一条直线近似表示,这种回归称为一元线性回归. 如果回归分析中包括两个或两个以上的自变量, ...
- [转载]Elasticsearch、MongoDB和Hadoop比较
IT界在过去几年中出现了一个有趣的现象.很多新的技术出现并立即拥抱了“大数据”.稍微老一点的技术也会将大数据添进自己的特性,避免落大部队太远,我们看到了不同技术之间的边际的模糊化.假如你有诸如Elas ...
- Bootstrap CSS组组件架构的设计思想
w AO模式 Append Overwrite 附加重写
- 【转】Keepalived无法绑定VIP故障排查经历
一 故障描述 我在台湾合作方给定的两台虚拟机上部署HAProxy+Keepalived负载均衡高可用方案.在配置完Keepalived后,重新启动Keepalived,Keepalived没有绑定VI ...
- 用Python爬虫爬取广州大学教务系统的成绩(内网访问)
用Python爬虫爬取广州大学教务系统的成绩(内网访问) 在进行爬取前,首先要了解: 1.什么是CSS选择器? 每一条css样式定义由两部分组成,形式如下: [code] 选择器{样式} [/code ...
- LeetCode_Add Two Numbers
题目: You are given two linked lists representing two non-negative numbers. The digits are stored in r ...
- Linux IPC之管道通信
2017-04-07 管道通信在linux中使用较为频繁的进程通信机制.基于unix一切皆文件的传统,管道也是一种文件.所以可以使用一般的VFS接口对管道进行读写操作,如read.write.具体管道 ...
- HTML5游戏开发系列教程5(译)
原文地址:http://www.script-tutorials.com/html5-game-development-lesson-5/ 最终我决定准备下一篇游戏开发系列的文章,我们将继续使用can ...
- Get started on your own KD 8 custom colorway
The 2009 Summer time Nike Basketball revealed the Cheap KD 8 and revealed three MVP-inspired colors ...
- PL/SQL编程—存储过程
SQL> create or replace procedure sp_pro3(name_in varchar2,id_in varchar2) is begin update mytest ...