当前位置:网站首页>leetcode:334. 递增的三元子序列
leetcode:334. 递增的三元子序列
2022-08-10 10:09:00 【OceanStar的学习笔记】
题目来源
题目描述


题目解析
本质是问最长递增子序列长度能否超过2而已
一次遍历
因为我们需要求解的长度仅仅为3,所以我们可以直接将这个三元子序列的值记录下来;

使用两个指针m1和m2,初始化为INT_MAX,然后遍历数组:
- 如果m1 >= curr,则令m1 = curr
- 如果m1 < curr && m2 >= curr,则令m2 = curr
一旦m2被更新了,说明一定会有一个数小于m2,也就是我们成功组成了一个长度为2的递增子序列。所以我们一旦遍历到比m2还大的数,我们直接返回ture。
如果我们遇到比m1小的数,还是要更新m1,有可能的话也要更新m2为更小的值,毕竟m2的值越小,能组成长度为3的递增序列的可能性越大
class Solution {
public:
bool increasingTriplet(vector<int>& nums) {
int m1 = INT32_MAX, m2 =INT32_MAX;
for(auto a : nums){
if(m1 >= a){
m1 = a;
}else if(m2 >= a){
m2 = a;
}else{
return true;
}
}
return false;
}
};
暴力超时
class Solution {
bool process(vector<int>& nums, int idx, std::vector<int>&path){
if(idx == nums.size()){
return path.size() >= 3;
}
bool p1 = process(nums, idx + 1, path);
bool p2 = false;
if(path.empty() || path.back() < nums[idx] ){
path.emplace_back(nums[idx]);
p2 = process(nums, idx + 1, path);
path.pop_back();
}
return p1 || p2;
}
public:
bool increasingTriplet(vector<int>& nums) {
std::vector<int>path;
return process(nums, 0, path);
}
};
类似题目
边栏推荐
- C语言空白符、空格符 与转义字符题点总结
- 效率开发目录
- bus事件总线 使用
- The web project accesses static resources inside the reference jar
- "Data Strategy" Results-Driven Enterprise Data Strategy: Organization and Governance
- vs2012创建WCF应用程序
- 腾讯云校园大使开始招募啦,内推名额和奖金等你来拿
- Dalian University of Technology & Pengcheng & UAE propose a mixed-scale triple network ZoomNet for camouflaged target detection, with SOTA performance!
- 交换 生成树 知识总结
- 干货!ASSANet:让PointNet++更快更强
猜你喜欢

大连理工&鹏城&UAE提出用于伪装目标检测的混合尺度三重网络ZoomNet,性能SOTA!

String interception function in SQL

CentOS和Ubantu的Mysql主从配置
![[Microservice Architecture] Microservices and SOA Architecture (2)](/img/62/70e5cd1640bc7a78525cdb662adffe.png)
[Microservice Architecture] Microservices and SOA Architecture (2)

「应用架构」六边型架构:三个原则和一个实现示例

【Azure云】服务端点和私有链接有什么区别?观点(1)

因子分析(SPSS)

「数据架构」数据模型,数据字典,数据库模式 和ERD的比较

《MySQL高级篇》六、索引的创建与设计原则

Dialogue with Chen Ciliang: Nezha wants to popularize high-end products
随机推荐
LeetCode Algorithm 914. 卡牌分组
lua初学
Excel绘制统计图
[Concept of Theory of Knowledge] "Progress in the Theory of Reason" University of Leuven 2022 latest 220-page doctoral dissertation
C语言空白符、空格符 与转义字符题点总结
LiveNVR操作日志页面快速筛选上级国标平台的调用记录直播观看录像回看等操作
Chapter 3 Search and Graph Theory (3)
2022.8.8-----leetcode.761
LCD模块如何建立联系分析
定时任务Quartz
Text selection rounded style border-radius
VBA: 遍历文件抓取指定条件的数据
dos环境下操作mysql
Redis6(一)——NoSQL数据库简介与Redis的安装
俄罗斯宣布临时禁止进口摩尔多瓦植物产品
Situation丨The intrusion of hackers intensifies, and the shooting range sets up a "defense shield" for network security
2022.8.9-----leetcode.1413
「时序数据库」使用cassandra进行时间序列数据扫描
兼容移动和PC的loading加载和toast消息插件
「业务架构」TAGAF建模:业务服务/信息图