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- ...
随机推荐
- eduroam WIFI on Ubuntu OS
- 【codeforces 707C】Pythagorean Triples
[题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...
- noip模拟赛 蒜头君打地鼠
分析:直接一个一个地去暴力枚举分数比较少,我们需要一种比较快的统计一定空间内1的数量,标准做法是前缀和,但是二维前缀和维护的是一个矩形内的值,这个是旋转过的该怎么办?可以把图旋转45°,不过这样比较考 ...
- CodeForces - 459C - Pashmak and Buses
先上题目+: C. Pashmak and Buses time limit per test 1 second memory limit per test 256 megabytes input s ...
- BIV+CSS网页的标准化布局
DIV用于搭建网站结构(框架),CSS用于创建网站表现(样式/美化) DIV+CSS模式设计网站的优势: 1.表现和内容分离. 2代码简洁,提高网页浏览速度. 3.易于维护,改版. 4.提高搜索引擎对 ...
- innodb-internals
https://www.pythian.com/blog/exposing-innodb-internals-via-system-variables-part-1-memory/
- 【python】蛋疼的中文乱码解决方案
转自: http://yooooo.us/2013/python-encoding-decoding?variant=zh-cn
- LINQ体验(11)——LINQ to SQL语句之Null语义和String/DateTime方法
在本系列中.主要介绍LINQ to SQL基础的东西,由于LINQ太强大了,它对我们寻常使用不同的数据源有着不同的内容,其包含对于SQL Server 数据库的LINQ to SQL:对于XML 文档 ...
- hdu 4193 单调队列
题意是给你n个数 组成的环 求以一个数开头 的数列全部前缀都为非负数的数列的个数: 思路: 先扩展成2*n的数列 然后求出sum[i]表示前i项的和 对每一个i>.=n结尾的数 ...
- C# 生成pdf文件客户端下载
itextsharp.dll 下载:http://sourceforge.net/projects/itextsharp/ 程序需引用:itextsharp.dll,itextsharp.pdfa.d ...