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. localStorage使用小结

    一.什么是localStorage.sessionStorage 在HTML5中,新加入了一个localStorage特性,这个特性主要是用来作为本地存储来使用的,解决了cookie存储空间不足的问题 ...

  2. 打印一个N*N的方阵,N为每边字符的个数( 3〈N〈20 ),写出来真是泪牛满面啊。

    本程序可以完美输出,效果如下: 相信你已经很期待代码实现了吧,其实代码真的不难,关键是思想.我说说我的想法吧,首先我构造了一个数组,保存了上面图片中字符一行的一半.聪明的你肯定看出来了,上面多数字符都 ...

  3. 【xenomai内核解析】系列文章大纲

    xenomai内核解析 本博客为本人学习linux实时操作系统框架xenomai的一些记录,主要剖析xenomai内核实现,以及与linux相关的知识.方便读者定位具体文章,现列出本博客大纲,后续会陆 ...

  4. CentOS安装MySQL详解 转

      引言 最近某云搞活动,买了个服务器作为平时学习和测试用,新机器啥也没有,一些常用软件的安装是免不了的,于是乎想着把安装过程都详细记录下来,一是做个备忘,二是给有需要的同学作个参考. Linux上安 ...

  5. Linux系统安装MySQL(2020最新最详细)

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

  6. Helium文档1-WebUI自动化-环境准备与入门

    前言 Helium 是一款 Web 端自动化开源框架,全称是:Selenium-Python-Helium,从名字上就可以看出,Helium 似乎和 Selenium 息息相关,基于Selenium的 ...

  7. JAVA递归实现线索化二叉树

    JAVA递归实现线索化二叉树 基础理论 首先,二叉树递归遍历分为先序遍历.中序遍历和后序遍历. 先序遍历为:根节点+左子树+右子树 中序遍历为:左子树+根节点+右子树 后序遍历为:左子树+右子树+根节 ...

  8. git学习(十一) idea git pull 解决冲突

    测试如下: 先将远程的代码修改,之后更新: 之后将工作区修改的代码(这里修改的代码跟远程修改的位置一样)提交到本地,之后拉取远程的代码,会发现有冲突: Accept Yours 就是直接选取本地的代码 ...

  9. vue-cli 工程目录结构介绍 详细介绍

    vue-cli目录结构: vue-cli目录解析: build 文件夹:用于存放 webpack 相关配置和脚本.开发中仅 偶尔使用 到此文件夹下 webpack.base.conf.js 用于配置 ...

  10. Java学习的第五十四天

    1.例13.1二次函数的解 import java.util.Scanner; public class Cjava { public static void main(String[]args) { ...