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

class Solution {
public:
//直接找出这个最长公共子串
//用一个数组保存最长公共子串,
//遍历原始数组,若nums[i] 小于最长公共子串数组开头数,则替换
//若nums[i]大于最长公共子串数组结尾数,则加入数组
//若nums[i]在最长公共子串数组中间位置,那么找到有序数组中第一个大于nums[i]的数,替换。
bool increasingTriplet(vector<int>& nums) {
if(nums.size()<3)
return false;
//存放最长递增子序列。
vector<int> dp;
dp.push_back(nums[0]);
for(int i=1;i<nums.size();i++){
if(nums[i] > dp.back()){
dp.push_back(nums[i]);
}else if(nums[i] < dp[0]){
dp[0] = nums[i];
}else{
//二分查找。查找所有大于key的元素中最左的元素。
int left = 0,right=dp.size()-1,target=nums[i];
while(left<=right){
int mid = left+(right-left)/2;
if(dp[mid] < target) left=mid+1;
else right=mid-1;
}
dp[left]=nums[i];
}
}
return dp.size() >= 3 ? true:false;
}
};

法二:

class Solution {
public:
//维护更新最小值和次小值。
bool increasingTriplet(vector<int>& nums) {
if(nums.size()<3) return false;
int smallest = INT_MAX;
int smaller = INT_MAX;
for(int i=0;i<nums.size();i++){
if(nums[i] <= smallest) smallest = nums[i];
else if(nums[i] <= smaller) smaller = nums[i];
else return true;
}
return false;
}
};


334. Increasing Triplet Subsequence(也可以使用dp动态规划)的更多相关文章

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

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

  2. [LeetCode] 334. Increasing Triplet Subsequence 递增三元子序列

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

  3. 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 My Submissions Question--Avota

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

  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 递增的三元子序列

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

  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. Linux系统安装JDK1.8

    2020最新Linux系统发行版ContOS7演示安装JDK. 为防止操作权限不足,建议切换root用户,当然如果你对Linux命令熟悉,能够自主完成权限更新操作,可以不考虑此推荐. 更多命令学习推荐 ...

  2. 怎样学习C语言(献给迷茫的C爱好者)!

    一 .怎样学习C语言 很多人对学习C语言感到无从下手,经常问我同一个问题:究竟怎样学习C语言?我是一个教师,已经开发了很多年的程序,和很多刚刚起步的人一样,学习的第一个计算机语言就是C语言. 经过这些 ...

  3. 自定义常用input表单元素二:纯css实现自定义radio单选按钮

    这是接着上一篇纯css自定义复选框checkbox的第二篇,自定义一个radio单选按钮,同样,采用css伪类和"+"css选择器为思路,下面是预览图: 下面直入主题放代码:HTM ...

  4. 后羿:我射箭了快上—用MotionLayout实现王者荣耀团战

    前言 昨晚跟往常一样,饭后开了一局王者荣耀,前中期基本焦灼,到了后期一波决定胜负的时候,我果断射箭,射中对面,配合队友直接秒杀,打赢团战一波推完基地.那叫一个精彩,队友都发出了666666的称赞,我酷 ...

  5. Python列表的增删改查

    列表的增: li = ['libai','sushi','dufu','sushi',"白居易"] 第一种: append():向列表末尾追加元素 li.append('diaoc ...

  6. 在centos下启动nginx出现Failed to start nginx.service:unit not found

    错误的原因就是没有添加nginx服务,所以启动失败. 解决方法: 1.    在/root/etc/init.d/目录下新建文件,文件名为nginx 或者用命令在根目录下执行:# vim /etc/i ...

  7. IDEA安装IDEA阿里Java规范插件

    插件安装方式有两种: 1.通过在线方式安装,搜索后找到,点击Install安装即可: 2.去官网plugins下载对应插件离线包,地址:https://plugins.jetbrains.com/pl ...

  8. 初学 Python 需要安装哪些软件?

    自动配置.有效求助.协作编程.版本控制.一站式解决 Python 新手练习中的痛点. 痛点 这个学期,我在北得克萨斯大学(University of North Texas)教 INFO 5731: ...

  9. vivo 基于原生 RabbitMQ 的高可用架构实践

    一.背景说明 vivo 在 2016 年引入 RabbitMQ,基于开源 RabbitMQ 进行扩展,向业务提供消息中间件服务. 2016~2018年,所有业务均使用一个集群,随着业务规模的增长,集群 ...

  10. Spring boot ConditionalOnClass原理解析

    Spring boot如何自动加载 对于Springboot的ConditionalOnClass注解一直非常好奇,原因是我们的jar包里面可能没有对应的class,而使用ConditionalOnC ...