【leetcode】Increasing Triplet Subsequence
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
.
解题思路:遍历数组,遍历过程中更新已经遍历的数组元素中的最小值和次小值,如果后续有元素比这两个值都大,说明存在这样的一个递增子序列。
class Solution(object):
def increasingTriplet(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
min =
smin =
for i in nums:
if i < min:
min = i
elif i<smin:
smin = i
else:
return True
return False
【leetcode】Increasing Triplet Subsequence的更多相关文章
- 【LeetCode】Increasing Triplet Subsequence(334)
1. Description Given an unsorted array return whether an increasing subsequence of length 3 exists o ...
- 【LeetCode】376. Wiggle Subsequence 解题报告(Python)
[LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...
- 【LeetCode】392. Is Subsequence 解题报告(Python)
[LeetCode]392. Is Subsequence 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/is-subseq ...
- [LeetCode] 334. Increasing Triplet Subsequence 递增三元子序列
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...
- 【leetcode】1081. Smallest Subsequence of Distinct Characters
题目如下: Return the lexicographically smallest subsequence of text that contains all the distinct chara ...
- 【Leetcode】376. Wiggle Subsequence
Description: A sequence of numbers is called a wiggle sequence if the differences between successive ...
- 【LeetCode】未分类(tag里面没有)(共题)
[334]Increasing Triplet Subsequence (2019年2月14日,google tag)(greedy) 给了一个数组 nums,判断是否有三个数字组成子序列,使得子序列 ...
- 【LeetCode】贪心 greedy(共38题)
[44]Wildcard Matching [45]Jump Game II (2018年11月28日,算法群衍生题) 题目背景和 55 一样的,问我能到达最后一个index的话,最少走几步. 题解: ...
- 【LeetCode】334. Increasing Triplet Subsequence 解题报告(Python)
[LeetCode]334. Increasing Triplet Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...
随机推荐
- 深入理解java:4.1. 框架编程之Spring MVC
说到java的mvc框架,struts2和springmvc想必大家都知道, Spring MVC是当前最优秀的MVC框架,自从Spring 2.5版本发布后,由于支持注解配置,易用性有了大幅度的提高 ...
- 利用Ansible模块copy和fetch进行主机间文件的传递
场景: java应用程序和Ansible不在同一台机子,要读取的文件又在另一台主机. 主机a不能保存文件,可以临时保存. 文件都在主机b上保存. 需求: 需要将文件从主机c传到主机b,再从主机b传到主 ...
- 未能加载文件或程序集“microsoft.Build.Engine, Version=3.5.0.0,...”或它的摸一个依赖项。
今天想打开IIS服务,然后点错了,不小心关掉了.net组件,结果vs就一直打不开项目,最后在网上查到了原因,打开 控制面板->程序和功能->打开或关闭功能 在里面勾选Microsoft . ...
- 小记---------网页采集之Jsoup
Jsoup是一款Java解析器,相当于httpClient解析器 功能:①:从一个URL,文件或字符串中解析HTML ②:使用DOM或CSS选择器来查找.取出数据 ...
- void *与id类型的相互转换
void *与id类型相互转换 在MRC下,void *与id类型相互转换完全没问题. id obj = [[NSObject alloc] init]; void *p = (void *)p; o ...
- idea工具
1. 使用IntelliJ IDEA 配置JDK(入门) https://blog.csdn.net/nobb111/article/details/77116259 2. idea 调试快捷键 ...
- linux-yum-downloadonly 下载rpm安装包到本地
注意 注意1:如果机器,本来就安装了相应的rpm包,则该rpm包不会下载. 参考 centos7离线安装rpm包自动解决依赖 查看linux系统版本信息(Oracle Linux.Centos Lin ...
- 解决IOS把数字渲染为电话号码,颜色为蓝色解决方案
可将telephone=no,则手机号码不被显示为拨号链接<meta name="format-detection" content="telephone=no&q ...
- vue项目报错1 Vue is a constructor and should be called with the `new` keyword && jquery.js?eedf:3850 Uncaught TypeError: this._init is not a function...
Vue is a constructor and should be called with the `new` keyword Uncaught TypeError: this._init is n ...
- JSP 自定义标签 生命周期
1. 2.