[LeetCode] 334. Increasing Triplet Subsequence 递增三元子序列
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.
Note: Your algorithm should run in O(n) time complexity and O(1) space complexity.
Example 1:
Input: [1,2,3,4,5]
Output: true
Example 2:
Input: [5,4,3,2,1]
Output: false
给一个非排序的数组,判断是否存在一个长度为3的递增子序列。 要求:T: O(n) S: O(1)
解法:由于时间和空间复杂度的要求,不能用排序或者DP的方法。遍历数组,用两个变量分别记录当前的最小值和第二小的值,如果发现一个比这两个都大的数,则组成了一个长度为3的子序列,返回True。如果遍历结束,没找到返回False。
Java:
public boolean increasingTriplet(int[] nums) {
// start with two largest values, as soon as we find a number bigger than both, while both have been updated, return true.
int small = Integer.MAX_VALUE, big = Integer.MAX_VALUE;
for (int n : nums) {
if (n <= small) { small = n; } // update small if n is smaller than both
else if (n <= big) { big = n; } // update big only if greater than small but smaller than big
else return true; // return if you find a number bigger than both
}
return false;
}
Python:
def increasingTriplet(nums):
first = second = float('inf')
for n in nums:
if n <= first:
first = n
elif n <= second:
second = n
else:
return True
return False
C++:
bool increasingTriplet(vector<int>& nums) {
int c1 = INT_MAX, c2 = INT_MAX;
for (int x : nums) {
if (x <= c1) {
c1 = x; // c1 is min seen so far (it's a candidate for 1st element)
} else if (x <= c2) { // here when x > c1, i.e. x might be either c2 or c3
c2 = x; // x is better than the current c2, store it
} else { // here when we have/had c1 < c2 already and x > c2
return true; // the increasing subsequence of 3 elements exists
}
}
return false;
}
All LeetCode Questions List 题目汇总
[LeetCode] 334. Increasing Triplet Subsequence 递增三元子序列的更多相关文章
- 334 Increasing Triplet Subsequence 递增的三元子序列
给定一个未排序的数组,请判断这个数组中是否存在长度为3的递增的子序列.正式的数学表达如下: 如果存在这样的 i, j, k, 且满足 0 ≤ i < j < k ≤ n-1, ...
- 【LeetCode】334. Increasing Triplet Subsequence 解题报告(Python)
[LeetCode]334. Increasing Triplet Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...
- [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
这个题是说看一个没有排序的数组里面有没有三个递增的子序列,也即: Return true if there exists i, j, k such that arr[i] < arr[j] &l ...
- 【LeetCode】Increasing Triplet Subsequence(334)
1. Description Given an unsorted array return whether an increasing subsequence of length 3 exists o ...
- 334. Increasing Triplet Subsequence My Submissions Question--Avota
问题描述: Given an unsorted array return whether an increasing subsequence of length 3 exists or not in ...
- 【leetcode】Increasing Triplet Subsequence
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...
- 334. Increasing Triplet Subsequence(也可以使用dp动态规划)
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...
- 334. Increasing Triplet Subsequence
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...
随机推荐
- Java动态代理-JDK自带实现
上篇文章讲解了什么是静态代理,但是静态代理有一个问题就是需要建立很多的代理类,这样我们需要修改代理的方法的时候,需要在每个类中都要修改,这对于我们来说:当代理类很多的时候工作量就会成倍的增加. 于是针 ...
- PHP数组操作类
class ArrayHelper{ /** * 从数组中删除空白的元素(包括只有空白字符的元素) * * 用法: * @code php ...
- 输入一个表示整数的字符串,把该字符串转换成整数并输出(实现atoi函数功能)
例如输入字符串"345",则输出整数345.-----------------------------此题一点也不简单.不信,你就先不看一下的代码,你自己先写一份,然后再对比一下, ...
- 到底该不该用RTOS——rtos的优点
我现在要不要学习RTOS? 学习RTOS有什么好处? 我的项目要不要跑RTOS? ······等等一些关于RTOS的问题,其实归根结底还是你对RTOS了解的不够,项目开发的经验还不足等. 针对这部分朋 ...
- PHP:ThinkCMFX任意文件包含漏洞
前言:最近爆出来的漏洞,ThinkCmfX版本应该是通杀的,基于3.X Thinkphp开发的 代码下载地址:https://gitee.com/thinkcmf/ThinkCMFX/releases ...
- Javascript搞笑图,哈哈哈哈
- Install Python3.6 on Amazon Linux/EC2 在Amazon Linux实例中安装使用Python3.6
本文转载自 https://gist.github.com/niranjv/f80fc1f488afc49845e2ff3d5df7f83b 由于Amazon Linux中预装的Python版本为2. ...
- 字符串Hash学习笔记
[toc] # 以下内容作废,太多错误了,等我有时间重写 说一下什么是Hash,说白了就是把一大坨字符用一些神奇的数来表示,可以说是把字符加密了. 简单一点就是一个像函数一样的东西,你放进去一个值,它 ...
- 树形DP入门题目推荐以及解析
关于树形DP几道入门题目 今天恶补树形DP,感觉海星. 其实挺简单的. 介绍几道例题,我会的. 1.洛谷P1352 没有上司的舞会 我的一篇题解 我们可以考虑每一个节点都是有两种情况. 一个是被邀请: ...
- 【luoguP2986】[USACO10MAR]伟大的奶牛聚集Great Cow Gathering
题目链接 先把\(1\)作为根求每个子树的\(size\),算出把\(1\)作为集会点的代价,不难发现把集会点移动到\(u\)的儿子\(v\)上后的代价为原代价-\(v\)的\(size\)*边权+( ...