Increasing Triplet Subsequence
1. 解析
题目大意,求解子序列是否存在一个长度为3的递增子序列。
2. 分析
涉及子序列问题,一般都是动态规划。与Longest Increasing Subsequence最大的不同在于,只要求长度为3即可,思路是一样的。subsequence[i]表示[0, i]内递增子序列的最大长度,每次向后检索,然后更新当前的最大长度即可,前面依赖的状态之前已经求解过,不过题目要求O(1)的空间复杂度,所以这种解题思路不是正确的。
class Solution {
public:bool increasingTriplet(vector<int>& nums) {vector<int> subsequence(nums.size(), 1);int res = 0;for (int i = 0; i < nums.size(); ++i){for (int j = i - 1; j >= 0; j--){if (nums[i] > nums[j] && subsequence[i] < subsequence[j] + 1){ //更新当前的状态subsequence[i] = subsequence[j] + 1;}}res = max(res, subsequence[i]);if (res >= 3) return true;}return false;}
};
3. 核心解法
参考@Grandyang的解法,思路很巧妙,即建立两个指针,left指向当前最小的数,right指向比left略微大的数,所以我们的目标就是检测是否存在大于right指针的数,left始终更新的是最小值,right更新的是稍微比left大一点的数。
例如:5 4 9 3 2 11
left = 5——> 4——>3——>2
right = 9
11 return true
class Solution {
public:bool increasingTriplet(vector<int>& nums){int left = INT_MAX, right = INT_MAX;for (int num : nums){if (left >= num) left = num; //更新left,使其始终指向最小的值else if (right >= num) right = num; //更新right,使其始终指向比left大的最小值else return true;}return false;}
};
4. 类似的题目
Longest Increasing Subsequence
Largest Divisible Subset
[1]https://www.cnblogs.com/grandyang/p/5194599.html
Increasing Triplet Subsequence相关推荐
- LeetCode-334. Increasing Triplet Subsequence
Description: Given an unsorted array return whether an increasing subsequence of length 3 exists or ...
- leetcode 334. Increasing Triplet Subsequence | 334. 递增的三元子序列(一种较trick的解法)
题目 https://leetcode.com/problems/increasing-triplet-subsequence/ 题解 看到这题,首先,没有思路- 然后,看了下面的 Related Q ...
- 334. 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 ...
- [Swift]LeetCode334. 递增的三元子序列 | Increasing Triplet Subsequence
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...
- LeetCode Increasing Triplet Subsequence(动态规划)
问题:问数组中是否存在一个上升的三元子序列 思路:方法一是使用动态规划求解上升子序列,如果子序列长度等于3,说明存在 方法二是使用small,mid分别记录最小值,第二小值,如果当前值比small小则 ...
- LintCode Longest Increasing Continuous Subsequence
原题链接在这里:http://www.lintcode.com/en/problem/longest-increasing-continuous-subsequence/ 题目: Give an in ...
- 继续过中等难度.0309
. 8 String to Integer (atoi) 13.9% Medium . 151 Reverse Words in a String 15.7% Mediu ...
- [leetcode] 数字游戏
169. Majority Element Given an array of size n, find the majority element. The majority element is t ...
- Leetcode重点250题
LeetCode重点250题 这个重点题目是把LeetCode前400题进行精简.精简方法如下: 删除不常考,面试低频出现题目 删除重复代码题目(例:链表反转206题,代码在234题出现过) 删除过于 ...
最新文章
- 项目的简单总结二--可拉伸的头视图
- SpringDataJpa开发--继承JpaRepository实现简单条件查询
- 业界 | 如何达到Kaggle竞赛top 2%?这里有一篇特征探索经验帖
- VTK:深度优先搜索动画用法实战
- Oracle数据库案例整理-Oracle系统执行时故障-Shared Pool内存不足导致数据库响应缓慢...
- java编译程序的基本命令是什么,【填空题】Java中编译java 程序的命令是 1 ,执行java程序的命令是 java 。...
- Android 4 学习(19):Services
- 一些Select检索高级用法
- java查询和添加客户信息_4.从零点五开始的Java之路(增删改查-客户)
- python切片输出_Python语言之详解切片
- 21天Jenkins打卡Day15项目复制
- CCF NOI1056 表达式
- Java Web开发入门 - 第3章 Tomcat单元测试
- python vimdiff_Vim实践与学习-09其他
- 证件OCR识别360度全面解析
- jquery点击事件写法
- Nginx 静态压缩/缓存
- C 语言 数据类型及所占字节数
- 《Intriguing Properties of Contrastive Losses》阅读笔记
- 为什么程序员用笛卡尔心形曲线告白的人,都还是单身?
热门文章
- MBR15200FAC-ASEMI插件肖特基二极管MBR15200FAC
- 锋利的jQuery读书笔记-第1章 认识jQuery
- python爬虫之以腾讯招聘为例,爬取动态页面
- K210(SiPEED MaixBit)MicroPython使用参考(一、软硬件环境)
- android MediaPlayer SurfaceView 网络视频播放器
- 基于JAVA的葫芦娃救爷爷游戏
- linux环境下常用的查找命令find、which、grep
- 17zwd获得17网商品详情接口 API 返回值说明,API接口获取方案
- 电脑如何连接无线打印服务器,电脑怎么联接无线路由器打印机
- 我的世界java版怎么加整合包_我的世界java如何下载安装optifine和forge及整合包和常见问题[纯小白教程]...