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的更多相关文章

  1. [LeetCode] 334. Increasing Triplet Subsequence 递增三元子序列

    Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...

  2. 【LeetCode】334. Increasing Triplet Subsequence 解题报告(Python)

    [LeetCode]334. Increasing Triplet Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...

  3. 【LeetCode】Increasing Triplet Subsequence(334)

    1. Description Given an unsorted array return whether an increasing subsequence of length 3 exists o ...

  4. 334. Increasing Triplet Subsequence

    Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...

  5. 334. Increasing Triplet Subsequence My Submissions Question--Avota

    问题描述: Given an unsorted array return whether an increasing subsequence of length 3 exists or not in ...

  6. 【leetcode】Increasing Triplet Subsequence

    Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...

  7. 334. Increasing Triplet Subsequence(也可以使用dp动态规划)

    Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...

  8. 334 Increasing Triplet Subsequence 递增的三元子序列

    给定一个未排序的数组,请判断这个数组中是否存在长度为3的递增的子序列.正式的数学表达如下:    如果存在这样的 i, j, k,  且满足 0 ≤ i < j < k ≤ n-1,    ...

  9. LeetCode 334 Increasing Triplet

    这个题是说看一个没有排序的数组里面有没有三个递增的子序列,也即: Return true if there exists i, j, k such that arr[i] < arr[j] &l ...

  10. [LeetCode] Increasing Triplet Subsequence 递增的三元子序列

    Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...

随机推荐

  1. Java反序列化漏洞执行命令回显实现及Exploit下载

    原文地址:http://www.freebuf.com/tools/88908.html 本文原创作者:rebeyond 文中提及的部分技术.工具可能带有一定攻击性,仅供安全学习和教学用途,禁止非法使 ...

  2. 如何编写一个PHP的C扩展

    为什么要用C扩展 C是静态编译的,执行效率比PHP代码高很多.同样的运算代码,使用C来开发,性能会比PHP要提升数百倍.IO操作如CURL,因为耗时主要在IOWait上,C扩展没有明显优势. 另外C扩 ...

  3. xcode中嵌入framework(接入快用最新SDK遇到的问题)

      但xcode设置中并没有找到Embed Frameworks这个选项,使用以下方式添加 当遇到dyld: Library not loaded:xxxx  Reason: image not fo ...

  4. 【转】iOS超全开源框架、项目和学习资料汇总

    iOS超全开源框架.项目和学习资料汇总(1)UI篇iOS超全开源框架.项目和学习资料汇总(2)动画篇iOS超全开源框架.项目和学习资料汇总(3)网络和Model篇iOS超全开源框架.项目和学习资料汇总 ...

  5. AYUI快速开发2016-6-29 ,全部免费,WPF普遍之路梦想开启

    下载开发模板:下载 AYUI 6月29日起,免费使用,无需授权,去除所有限制,关爱开发者,不求捐赠,只要你们能私活挣到钱就行,你们没有欠我的.我希望所有人都能开发WPF的东西 使用教程,上面的下载文件 ...

  6. 第六章 - 图像变换 - 图像拉伸、收缩、扭曲、旋转[2] - 透视变换(cvWarpPerspective)

    透视变换(单应性?)能提供更大的灵活性,但是一个透视投影并不是线性变换,因此所采用的映射矩阵是3*3,且控点变为4个,其他方面与仿射变换完全类似,下面的例程是针对密集变换,稀疏图像变换则采用cvPer ...

  7. AndroidStudio小技巧--依赖库

    同步发表于http://avenwu.net/2015/02/12/androidstudio_library_dependency Fork on github https://github.com ...

  8. 【转帖】自助式BI的崛起:三张图看清商业智能和大数据分析市场趋势

    自助式BI的崛起:三张图看清商业智能和大数据分析市场趋势 大数据时代,商业智能和数据分析软件市场正在经历一场巨变,那些强调易用性的,人人都能使用的分析软件正在取代传统复杂的商业智能和分析软件成为市场的 ...

  9. Python: 收集所有命名参数

    有时候把Python函数调用的命名参数都收集到一个dict中可以更方便地做参数检查,或者直接由参数创建attribute等.更简单的理解就是def foo(*args, **kwargs): pass ...

  10. 解决cxf+spring发布的webservice,types,portType和message以import方式导入

    用cxf+spring发布了webservice,发现生成的wsdl的types,message和portType都以import的方式导入的.. 原因:命名空间问题 我想要生成的wsdl在同个文件中 ...