问题描述:

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.

题意:

给定一无序数组,判断其中是否存在长度为3的递增子序列。

解题思路:

此问题可转化为先找长度为2的递增序列(记为first, second)然后再判断后面是否有比second更大的数。此时我们只需维护first, second,但可能出现如3,4,1这种情况,这时我们就需要另一个数temp表示位于递增序列后面但比first更小的数。之后对于数组中的每一个数nums[i],只需根据其在temp,first,second三者之间的位置来判断结果并维护这三个数,其中包括[负无穷, temp],(temp, first],(first, second],(second, 正无穷)四个区间

示例代码:

class Solution {
public:
bool increasingTriplet(vector<int>& nums) {
int len = nums.size();
if(len < 3){
return false;
}
int first = nums[0], second = INT_MAX, temp = nums[0];
for(int i = 1; i < len; i++){
if(nums[i] > second){
return true;
}
if(nums[i] > first){
second = nums[i];
}
else if(nums[i] <= temp){
temp = nums[i];
}
else{
first = temp;
second = nums[i];
}
}
return false;
}
};

334. Increasing Triplet Subsequence My Submissions Question--Avota的更多相关文章

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

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

  2. 334. 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 递增三元子序列

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

  4. 334. Increasing Triplet Subsequence(也可以使用dp动态规划)

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

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

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

  6. 【LeetCode】Increasing Triplet Subsequence(334)

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

  7. LeetCode——Increasing Triplet Subsequence

    Question Given an unsorted array return whether an increasing subsequence of length 3 exists or not ...

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

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

  9. Increasing Triplet Subsequence

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

随机推荐

  1. Spark(Hive) SQL中UDF的使用(Python)

    相对于使用MapReduce或者Spark Application的方式进行数据分析,使用Hive SQL或Spark SQL能为我们省去不少的代码工作量,而Hive SQL或Spark SQL本身内 ...

  2. MYSQL删除以数字开头的字段

    例子: // 删除以0开头的字段 DELETE FROM `week_energy_copy` WHERE openid like '0%'; // 删除以数字开头的字段 DELETE FROM `w ...

  3. 关于将客户端移植到Lua的解决方案设想。

    现在发行商都需要cp们做热更新,而对于unity制作的游戏来讲,这个恐怕是个噩梦,而项目已经进行到中后期,确实很麻烦,有UniLua,但是如果全部手动解决恐怕上不了线了工作量太大,初步设想如果做一个基 ...

  4. HDOJ/HDU 2566 统计硬币(公式~遍历~)

    Problem Description 假设一堆由1分.2分.5分组成的n个硬币总面值为m分,求一共有多少种可能的组合方式(某种面值的硬币可以数量可以为0). Input 输入数据第一行有一个正整数T ...

  5. HDOJ 2030 汉字统计

    Problem Description 统计给定文本文件中汉字的个数. Input 输入文件首先包含一个整数n,表示测试实例的个数,然后是n段文本. Output 对于每一段文本,输出其中的汉字的个数 ...

  6. spring框架DI(IOC)和AOP 原理及方案

    http://www.blogjava.net/killme2008/archive/2007/04/20/112160.html http://www.oschina.net/code/snippe ...

  7. js判断是否为手机浏览器

    JS判断手机浏览器 判断原理: JavaScript是前端开发的主要语言,我们可以通过 编写JavaScript程序来判断浏览器的类型及版本.JavaScript判断浏览器类型一般有两种办法,一种是根 ...

  8. list的三种遍历方法

    1.最简单的for循环遍历 for(int i = 0; i < list.size(); i++){        list.get(i);        } 2.最方便的foreach循环遍 ...

  9. C#类型 分类: C# 2015-03-09 08:44 202人阅读 评论(0) 收藏

    C# 类型 引言 本文之初的目的是讲述设计模式中的 Prototype(原型)模式,但是如果想较清楚地弄明白这个模式,需要了解对象克隆(Object Clone),Clone其实也就是对象复制.复制又 ...

  10. python小程序——购物

    流程图  代码程序 saving = int(input('请输入你的工资:'))shopping = [['iphone',5800],['mx6',2000],['pythonbook',80], ...