LeetCode-334. Increasing Triplet Subsequence
Description:
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the array.
Formally the function should:
Return true if there exists i, j, k
such that arr[i] < arr[j] < arr[k] given 0 ≤ i < j < k ≤ n-1 else return false.
Your algorithm should run in O(n) time complexity and O(1) space complexity.
Examples:
Given [1, 2, 3, 4, 5],
return true.
Given [5, 4, 3, 2, 1],
return false.
题目大意是判断所给的数组中是否存在长度为3的递增序列。
最常规的办法应该是两层循环,逐个判断。时间复杂度较高。还有一种比较巧妙的办法,维护两个距离最近的递增序列,如果能找到第三个则返回true。设置一个small,一个big,small<big。如果能找到一个不小于small和big的就返回true。时间复杂度为O(n)
实现代码:
public class Solution {
public boolean increasingTriplet(int[] nums) {
if(nums == null || nums.length < 3)
return false;
int small = Integer.MAX_VALUE, big = Integer.MAX_VALUE;
for(int value : nums) {
if(value <= small)
small = value;
else if(value <= big)
big = value;
else
return true;
}
return false;
}
}
LeetCode-334. Increasing Triplet Subsequence的更多相关文章
- [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. ...
- 【LeetCode】Increasing Triplet Subsequence(334)
1. Description Given an unsorted array return whether an increasing subsequence of length 3 exists o ...
- 334. Increasing Triplet Subsequence
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...
- 334. Increasing Triplet Subsequence My Submissions Question--Avota
问题描述: Given an unsorted array return whether an increasing subsequence of length 3 exists or not in ...
- 【leetcode】Increasing Triplet Subsequence
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...
- 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 递增的三元子序列
给定一个未排序的数组,请判断这个数组中是否存在长度为3的递增的子序列.正式的数学表达如下: 如果存在这样的 i, j, k, 且满足 0 ≤ i < j < k ≤ n-1, ...
- LeetCode 334 Increasing Triplet
这个题是说看一个没有排序的数组里面有没有三个递增的子序列,也即: Return true if there exists i, j, k such that arr[i] < arr[j] &l ...
- [LeetCode] Increasing Triplet Subsequence 递增的三元子序列
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...
随机推荐
- Python学习笔记(4):自定义时间类
Python的时间我实在无法接受,太难用了.我觉得C#的时间就非常完美,简单.好用.所以,自定义了自己的时间类: 用法: 一个小小的应用,我需要取出每天股市交易的分钟段,开始是这样的: 稍微改进一下, ...
- 利用Mahout实现在Hadoop上运行K-Means算法
利用Mahout实现在Hadoop上运行K-Means算法 一.介绍Mahout Mahout是Apache下的开源机器学习软件包,目前实现的机器学习算法主要包含有协同过滤/推荐引擎,聚类和分类三个部 ...
- .net core 使用Autofac依赖注入
Startup中: public IContainer ApplicationContainer { get; private set; } // This method gets called by ...
- DropDownList 获取不了选择的值 这种错误
有时候做项目的时候 发现DropDownList 获取不了选择的值 这个原因很可能是 你初始化DropDownList的时候 没有进行 ispostback的判断 导致提交的时候 又初始化了一次... ...
- VC++ 学习笔记(序):神一样的语言
总的来说,我觉得VC++是一门神一样的语言——它是公认最强大.最复杂的:它一切以效率为第一要务,却又不肯落伍,拼命兼容现在的新的语言设计特点.本来在别的语言很容与就避开的问题,在这里要用很高的技巧去设 ...
- IOS CopyPNGFile 异常问题解决
今天突然出现一个编译问题,搞了我好久,这里不多说了.直接整理出来,记录下来! 问题描述如下图: 出现:CopyPNGFile 异常错误,Command /Applications/Xcode.app/ ...
- jdbc无法连接数据解析
1.网络原因 2.账户权限问题 账户是否赋予以下的权限: grant connect, resource to ADM_BI; grant read, write on directory BACKU ...
- SAP顾问发展
关于SAP顾问发展的话题也不仅仅是一次的谈起,但是我想对于自己的规划很多人是否有没有深刻的考虑过.这对于你我来说都非常的重要,那么作为我来说,我仅仅把自己的观点阐述以供大家思考,希望对大家能有所帮助. ...
- AIDL示例
背景 最近在考虑项目重构的时候,考虑将项目拆分成两个APK,一个用于数据服务,一个用于UI展示. 数据服务APK向自己编写APK提供数据,同时也可以向第三方提供数据.考虑使用这样的方式代替向第三方提供 ...
- 一段时间没上来了,看到有很多网友对OWA感兴趣,因为所在公司发展方向的原因,没有太多时间继续深入研究OWA,敬请见谅
一段时间没上来了,看到有很多网友对OWA感兴趣,因为所在公司发展方向的原因,没有太多时间继续深入研究OWA,敬请见谅