334. Increasing Triplet Subsequence(也可以使用dp动态规划)
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动态规划)的更多相关文章
- 【LeetCode】334. Increasing Triplet Subsequence 解题报告(Python)
[LeetCode]334. Increasing Triplet Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...
- [LeetCode] 334. 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
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...
- 334. Increasing Triplet Subsequence My Submissions Question--Avota
问题描述: Given an unsorted array return whether an increasing subsequence of length 3 exists or not in ...
- 334 Increasing Triplet Subsequence 递增的三元子序列
给定一个未排序的数组,请判断这个数组中是否存在长度为3的递增的子序列.正式的数学表达如下: 如果存在这样的 i, j, k, 且满足 0 ≤ i < j < k ≤ n-1, ...
- 【LeetCode】Increasing Triplet Subsequence(334)
1. Description Given an unsorted array return whether an increasing subsequence of length 3 exists o ...
- [LeetCode] Increasing Triplet Subsequence 递增的三元子序列
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...
- Leetcode: Increasing Triplet Subsequence
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...
- Increasing Triplet Subsequence
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...
随机推荐
- Docker(Docker Toolbox)配置镜像加速更换国内源
自己当时装的是Win10专业工作室版本,不知道为什么不支持window for docker, 所以选择了Docker Toolbox 的方式,主要是为了学习,虽然这种方式是不建议安装的,但是基础的学 ...
- GIS和视频监控的集成
本文讨论了使用增强现实(AR)技术的三维(3D)地理信息系统(GIS)和视频监视系统的集成.进行这种集成的动机是要克服常规视频监视系统面临的问题.关于哪个摄像机当前监视此类系统中哪个区域的明确信息:因 ...
- kubernetes:用kubeadm管理token(kubernetes 1.18.3)
一,token的用途: 1,token是node节点用来连接master节点的令牌字串, 它和ca证书的hash值是把一台node节点加入到kubernetes集群时要使用的凭证 2, 通过kubea ...
- centos8平台yum无法安装一些常用软件的解决,如:screen,iftop,nethogs
一,例如:安装screen时报错: [root@localhost liuhongdi]# yum install screen 上次元数据过期检查:17:39:58 前,执行于 2020年03月18 ...
- 自动化遍历-appcrawler
下载appclawler 下载地址:https://pan.baidu.com/s/1dE0JDCH#list/path=%2F 查看帮助文档: java -jar appcrawler-2.4.0- ...
- salesforce零基础学习(九十六)项目中的零碎知识点小总结(四)
本篇参考: https://developer.salesforce.com/docs/atlas.en-us.216.0.apexcode.meta/apexcode/apex_classes_ke ...
- solr之functionQuery(函数查询)【转】
函数查询 让我们可以利用 numeric域的值 或者 与域相关的的某个特定的值的函数,来对文档进行评分. 怎样使用函数查询 这里主要有两种方法可以使用函数查询,这两种方法都是通过solr http 接 ...
- XSS攻击 js 脚本注入
原文地址:http://www.cnblogs.com/robot/archive/2009/04/15/1436107.html 1.不要相信Request.QueryString: 相信在asp时 ...
- hbase的Java基本操作
hbase的Java基本操作 建表,建列簇操作 private static Connection connection; private static Admin admin; public sta ...
- Linux配置阿里epl源
去阿里云 有源仓库 阿里云镜像官方站点 https://developer.aliyun.com/mirror/ 先备份本机上的源 mv /etc/yum.repos.d/CentOS-Base.re ...