当前位置:网站首页>【LeetCode】Day105-递增的三元子序列
【LeetCode】Day105-递增的三元子序列
2022-07-29 12:55:00 【倒过来是圈圈】
题目
题解
双向遍历
维护数组 nums 中的每个元素左边的最小值和右边的最大值,只要出现leftMin[i] < nums[i] < rightMax[i],即返回true
class Solution {
public boolean increasingTriplet(int[] nums) {
int n=nums.length;
if(n<3)
return false;
int[] leftMin=new int[n];
leftMin[0]=nums[0];
for(int i=1;i<n;i++){
leftMin[i]=Math.min(leftMin[i-1],nums[i]);
}
int[] rightMax=new int[n];
rightMax[n-1]=nums[n-1];
for(int i=n-2;i>=0;i--){
rightMax[i]=Math.max(rightMax[i+1],nums[i]);
}
//遍历中间值nums[i]
for(int i=1;i<n-1;i++){
if(nums[i]>leftMin[i]&&nums[i]<rightMax[i])
return true;
}
return false;
}
}
时间复杂度: O ( n ) O(n) O(n),遍历数组三次
空间复杂度: O ( n ) O(n) O(n),三个数组
贪心
使用贪心策略将空间复杂度降到O(1),其核心思想为:为了找到递增的三元子序列,三元组第一个元素 first 和 第二个元素 second 应该尽可能地小,此时找到递增的三元子序列的可能性更大。
具体算法如下:赋初始值,first=nums[0],second=+∞,已经满足second > first了,现在找第三个数third
(1) 如果third比second大,那就是找到了,直接返回true
(2) 如果third比second小,但是比first大,那就把second的值设为third,然后继续遍历找third
(3) 如果third比first还小,那就把first的值设为third,然后继续遍历找third
class Solution {
public boolean increasingTriplet(int[] nums) {
int n=nums.length;
if(n<3)
return false;
int first=nums[0],second=Integer.MAX_VALUE;
for(int i=1;i<n;i++){
int third=nums[i];
if(third>second)//1<2<3
return true;
else if(third>first)//1<3<2
second=third;
else//3<1<2
first=third;
}
return false;
}
}
时间复杂度: O ( n ) O(n) O(n),遍历数组一次
空间复杂度: O ( 1 ) O(1) O(1)
边栏推荐
- String.split()最详细源码解读及注意事项
- asyncawait和promise的区别
- The whole process of installing Oracle database on CentOS7
- 如何把Netflix数据集转换成Movielens格式?
- 轻松学Pytorch-Pytorch可视化
- The meaning of "last in first out" in stack and "first in first out" in queue
- 【kaggle】Spaceship Titanic - 预测哪些乘客被运送到另一个维度【CatBoost - 10%】
- 人脸合成效果媲美StyleGAN,而它是个自编码器
- Sql file import database - nanny level tutorial
- 用支持LaTex的Markdown语句编辑一个数学公式
猜你喜欢

IJCAI 2022杰出论文公布,大陆作者中稿298篇拿下两项第一

Nacos hierarchical storage model - the cluster configuration and NacosRule load balance

npm出现报错 npm WARN config global `--global`, `--local` are deprecated. Use `--location=global

MLX90640 infrared thermal imaging temperature measuring sensor module development notes (9)

为什么用了大牌工具后报表开发依然头痛

snap软件中哨兵2A数据预处理及六种常用植被指数的计算
Go简单实现协程池

AutoAlignV2:多模态3D目标检测新SOTA!(ECCV2022)
![[WeChat applet] One article to solve button, input, image components](/img/a4/56df6c530c41f6cff865053f9f8f2c.png)
[WeChat applet] One article to solve button, input, image components

年轻人开始“反大牌”,有钱也不买
随机推荐
Draw boxes of WPF screenshots controls and ellipse (4) "imitation WeChat"
【kaggle】Spaceship Titanic - 预测哪些乘客被运送到另一个维度【CatBoost - 10%】
Go - reading (7), CopySheet Excelize API source code (the from and to the int)
[网鼎杯 2020 半决赛]AliceWebsite
基于对象的实时空间音频渲染丨Dev for Dev 专栏
What is the difference between the legendary server GOM engine and the GEE engine?
MySQL 视图(详解)
pycharm专业版使用
[WeChat applet] WXSS and global, page configuration
超年轻!34岁教授,任985王牌学院副院长!
Bika LIMS 开源LIMS集—— SENAITE的使用(用户、角色、部门)
SIP系统组成格式
MySQL database installation (detailed)
The strategy pattern replacement if the else
【云原生】开源数据分析 SPL 轻松应对 T+0
mysql数据库安装(详细)
MySQL 安装报错的解决方法
C language game ------ greedy snake ---- for Xiaobai
kotlin协程与线程池
从KEIL仿真界面导出数据的技巧