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 ...
随机推荐
- 协同开发功能——Github团队协作
最近需要写一个HoloLens开发的简明介绍,其中要测试几个demo.用到github以团队协作,像下面是简单的事件记录. 一.创建项目 1. 2.项目设置 名称 描述description Init ...
- 【转载】opencvVS2019配置方法
环境: 系统:win10系统截至2020920版本 opencv版本:3.0.0版本 IDE:宇宙最强IDEA最新版本2019社区版 教程: 1.下载opencv安装包官网下载链接:https://o ...
- sublime python配置运行
1.安装python环境 安装完成时,Win+R → 输入cmd → Enter → 调出来命令行,输入python确认安装是否成功. 2.安装sublime 3.打开sublime,选择工具--编译 ...
- 【线段树分治】Dash Speed
代码的美妙 #include <bits/stdc++.h> %:pragma GCC optimize(3) using namespace std; const int maxn=7e ...
- 机器分配----线性dp难题(对于我来说)
题目: 总公司拥有高效设备M台, 准备分给下属的N个分公司.各分公司若获得这些设备,可以为国家提供一定的盈利.问:如何分配这M台设备才能使国家得到的盈利最大?求出最大盈利值.其中M <= 15, ...
- Web调优之IBM JDK+liberty(一): Jmeter pod里压力,50个线程并发测试,调整 -Xms -Xms, Log原来是大问题
1.运行环境 k8s Web服务器: Liberty(IBM J9 JDK),base image : FROM websphere-liberty:20.0.0.3-kernel-java8-ibm ...
- Java8新特性--Base64转换
1.简介 在Java8中,Base64编码已经成为Java类库的标准.Java 8 内置了 Base64 编码的编码器和解码器. Base64工具类提供了一套静态方法获取下面三种BASE64编解码器: ...
- 关于Python的面相对象编程
Python 其实不是面向对象的语言,更像是C语言的面向过程编程的语言 但 Python 也支持 class 关键字来实现类的声明与创建 但 Python 的对象更像是 JavaScript 的函数 ...
- git 常用命令大全2
查看.添加.提交.删除.找回,重置修改文件 git help <command> # 显示command的help git show # 显示某次提交的内容 git show $id gi ...
- Django、haystack、whoosh实现全局搜索
Django.haystack.whoosh实现全局搜索 关注公众号"轻松学编程"了解更多. [参考:https://blog.csdn.net/zhaogeno1/article ...