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 递增三元子序列的更多相关文章

  1. 334 Increasing Triplet Subsequence 递增的三元子序列

    给定一个未排序的数组,请判断这个数组中是否存在长度为3的递增的子序列.正式的数学表达如下:    如果存在这样的 i, j, k,  且满足 0 ≤ i < j < k ≤ n-1,    ...

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

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

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

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

  4. LeetCode 334 Increasing Triplet

    这个题是说看一个没有排序的数组里面有没有三个递增的子序列,也即: Return true if there exists i, j, k such that arr[i] < arr[j] &l ...

  5. 【LeetCode】Increasing Triplet Subsequence(334)

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

  6. 334. Increasing Triplet Subsequence My Submissions Question--Avota

    问题描述: Given an unsorted array return whether an increasing subsequence of length 3 exists or not in ...

  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. 334. Increasing Triplet Subsequence

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

随机推荐

  1. JQuery系列(8) - JQuery插件开发

    所谓“插件”,就是用户自己新增的jQuery实例对象的方法.由于该方法要被所有实例共享,所以只能定义在jQuery构造函数的原型对象(prototype)之上.对于用户来说,把一些常用的操作封装成插件 ...

  2. Java-Modifier类常用方法详解

    一.Modifier类的定义 Modifier类 (修饰符工具类) 位于 java.lang.reflect 包中,用于判断和获取某个类.变量或方法的修饰符Modifier类将各个修饰符表示为相对应的 ...

  3. PureComponent & shouldComponentUpdate

    Called to determine whether the change in props and state should trigger a re-render. Component alwa ...

  4. React Tutorial: Basic Concept Of React Component---babel, a translator

    Getting started with react.js: basic concept of React component 1 What is React.js React, or React.j ...

  5. python--基于socket网络编程

    Python 提供了两个基本的 socket 模块. 第一个是 Socket,它提供了标准的 BSD Sockets API. 第二个是 SocketServer, 它提供了服务器中心类,可以简化网络 ...

  6. LeetCode 422. Valid Word Square

    原题链接在这里:https://leetcode.com/problems/valid-word-square/ 题目: Given a sequence of words, check whethe ...

  7. re.sub 实现多处替换

    1   | 表示或的意思 将所有字母替换掉 result_content = re.sub('a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z', ...

  8. ES6 String和Number扩展

    一.String 扩展 ①传统上,JavaScript 只有indexOf方法,可以用来确定一个字符串是否包含在另一个字符串中.ES6 又提供了三种新方法. includes():返回布尔值,表示是否 ...

  9. UE4的多线程

    1. 源代码 AsyncWork.h 2. 多线程的使用 参考文档:https://wiki.unrealengine.com/Using_AsyncTasks 当我们需要执行一个需要很长时间的任务时 ...

  10. markdown转html

    今天临时要写接口文档,然后发现部门给的文档是markdown文件的,而接口文档是要html格式的,因此想直接把markdown转为html 这里我使用的是marked 首先初始化一个node项目 np ...