334 Increasing Triplet Subsequence 递增的三元子序列
给定一个未排序的数组,请判断这个数组中是否存在长度为3的递增的子序列。
正式的数学表达如下:
如果存在这样的 i, j, k, 且满足 0 ≤ i < j < k ≤ n-1,
使得 arr[i] < arr[j] < arr[k] ,返回 true ; 否则返回 false 。
要求算法时间复杂度为O(n),空间复杂度为O(1) 。
示例:
输入 [1, 2, 3, 4, 5],
输出 true.
输入 [5, 4, 3, 2, 1],
输出 false.
详见:https://leetcode.com/problems/increasing-triplet-subsequence/description/
C++:
方法一:超时
class Solution {
public:
bool increasingTriplet(vector<int>& nums) {
int n=nums.size();
vector<int> dp(n,1);
for(int i=0;i<n;++i)
{
for(int j=0;j<i;++j)
{
if(nums[j]<nums[i])
{
dp[i]=max(dp[i],dp[j]+1);
}
if(dp[i]>=3)
{
return true;
}
}
}
return false;
}
};
方法二:
使用两个指针m1和m2,初始化为整型最大值,遍历数组,如果m1大于等于当前数字,则将当前数字赋给m1;如果m1小于当前数字且m2大于等于当前数字,那么将当前数字赋给m2,一旦m2被更新了,说明一定会有一个数小于m2,那么就成功的组成了一个长度为2的递增子序列,所以一旦遍历到比m2还大的数,直接返回ture。如果遇到比m1小的数,还是要更新m1,有可能的话也要更新m2为更小的值,毕竟m2的值越小,能组成长度为3的递增序列的可能性越大。
class Solution {
public:
bool increasingTriplet(vector<int>& nums) {
int m1=INT_MAX,m2=INT_MAX;
for(int a:nums)
{
if(m1>=a)
{
m1=a;
}
else if(m2>=a)
{
m2=a;
}
else
{
return true;
}
}
return false;
}
};
参考:https://www.cnblogs.com/grandyang/p/5194599.html
334 Increasing Triplet Subsequence 递增的三元子序列的更多相关文章
- [LeetCode] Increasing Triplet Subsequence 递增的三元子序列
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...
- [LeetCode] 334. Increasing Triplet Subsequence 递增三元子序列
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...
- 【LeetCode】334. Increasing Triplet Subsequence 解题报告(Python)
[LeetCode]334. Increasing Triplet Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...
- 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(也可以使用dp动态规划)
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 ...
- LeetCode:递增的三元子序列【334】
LeetCode:递增的三元子序列[334] 题目描述 给定一个未排序的数组,判断这个数组中是否存在长度为 3 的递增子序列. 数学表达式如下: 如果存在这样的 i, j, k, 且满足 0 ≤ i ...
- Java实现 LeetCode 334 递增的三元子序列
334. 递增的三元子序列 给定一个未排序的数组,判断这个数组中是否存在长度为 3 的递增子序列. 数学表达式如下: 如果存在这样的 i, j, k, 且满足 0 ≤ i < j < k ...
- Leetcode 334.递增的三元子序列
递增的三元子序列 给定一个未排序的数组,判断这个数组中是否存在长度为 3 的递增子序列. 数学表达式如下: 如果存在这样的 i, j, k, 且满足 0 ≤ i < j < k ≤ n- ...
随机推荐
- spring 学习(二)
public interface BeanPostProcessor { @Nullable default Object postProcessBeforeInitialization(Object ...
- 欧拉回路输出(DFS,不用回溯!)Watchcow POJ 2230
Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 8109 Accepted: 3551 Special Judge D ...
- GRANT 授权
sys(管理员)身份登录.创建usernamezsta_new create user zsta_new identified by password default tablespace Z ...
- tomcat管理员password设置
打开tomcat安装文件夹下的conf,然后打开tomcat-users.xml 在标签中加入 <user name="" password="" rol ...
- struts2 与spring整合
要把struts2的action交给spring管理,这样spring才干帮struts2注入须要的的bean(一開始action是由struts初始化,所以想注入spring里面的bean是注入不了 ...
- linux安装jmeter
将jmeter安装包下载下来(部分linux版本),配置环境变量 jmeter下载地址:链接: https://pan.baidu.com/s/1_6-FmU6XjQH71Ngyh2Sx-A 提 ...
- willRotateToInterfaceOrientation 屏幕旋转
/* 1.屏幕发生旋转后, 这个旋转事件会先传递给window的rootViewController(窗口的根控制器) 2.rootViewController又会将这个旋转事件传递给它的子控制器 * ...
- 利用 gnuplot_i 在你的 c 程序中调用 GNUPLOT
这是一篇非常早曾经写的小文章,最初发表于我的搜狐博客(2008-09-23 22:55).由于自从转移到这里后,sohu 博客就不再维护了,所以把这篇文章也一起挪了过来. GNUPLOT 是一款功能强 ...
- LeetCode 9. Palindrome Number (回文数字)
Determine whether an integer is a palindrome. Do this without extra space. 题目标签:Math 题目给了我们一个int x, ...
- Project Perfect让Swift在server端跑起来-引言(一)
编者语:今天是大年初一.先和大家简单说一句猴年快乐! watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/ ...