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 ...
随机推荐
- MySQL5.7版本sql_mode=only_full_group_by问题解决办法
原因分析:MySQL5.7版本默认设置了 mysql sql_mode = only_full_group_by 属性,导致报错. 1.查看sql_mode SELECT @@sql_mode; 2. ...
- eclipse 配置opencv
1 准备 eclipse 2017 JDK1.8 opencv 4.40 2 配置 新建java工程 添加jar包 选择opencv-xxx.jar包 加入原生库 选择原生库位置 确认即可,测试 新建 ...
- python程序整理(1)
''' 用户登录验证 要求: 1. 系统⾃动⽣成4位随机数. 作为登录验证码. 直接用就好. 这里不用纠结 提示. 生成随机数的办法. from random import randint num = ...
- wine实用经验教程
本篇讲类unix系统下的用以模拟运行Windows程序的wine.会从普通使用者的比较实用的角度去讲.有专为国内用户准备的内容. 本篇面向有Linux经验但对wine不熟悉的人. wine可靠吗?该不 ...
- Jmeter入门(1)- 什么是Jmeter以及Jmeter的安装和环境配置
一. Jmeter简介 Jmeter时Apacha公司使用Java平台开发的一款测试工具 二. Jmeter可以做什么 Jmeter可以用来做接口测试.性能测试.压力测试.数据库测试.Java程序测试 ...
- Fur 是 .NET 5 平台下企业应用开发最佳实践框架。
Fur 是 .NET 5 平台下企业应用开发最佳实践框架. 立即尝鲜 Fur 是基于最新的 .NET 5 RC2 构建,目的是为了尽早体验新功能,对即将到来的 .NET 5 正式版做出最快的响应. 所 ...
- IntentService下载任务
onHandleIntent开启一个线程按顺序处理任务,不适合做大量任务 public class MainActivity extends AppCompatActivity { protected ...
- origin把点图和线图放在一起
首先分别做好点图和线图在两个graph中,然后选中其中一副图(点图或者线图),然后按下图选择: 如果有多个图,可以在弹出窗口中选择多个sheet.
- Graph-GraphSage
MPNN很好地概括了空域卷积的过程,但定义在这个框架下的所有模型都有一个共同的缺陷: 1. 卷积操作针对的对象是整张图,也就意味着要将所有结点放入内存/显存中,才能进行卷积操作.但对实际场景中的大规模 ...
- 开源项目bootdo的实战开发笔记
开源项目bootdo 源码地址:https://github.com/lcg0124/bootdo 技术选型 1.后端 核心框架:Spring Boot 安全框架:Apache Shiro 模板引擎: ...