【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. ...
随机推荐
- 浏览器访问ipv6站点(未绑定主机的ipv6站点)
我们在浏览器直接输入ipv6地址敲回车,一般情况下浏览器会跳转到搜索引擎进行搜索. 我们需要在浏览器器中输入: http://[::1] 或者 [::1]
- vue2创建webpack项目build之后无法正常显示页面的问题
最近在做vue项目的时候,项目正常运行,但是当我打包上线之后,却出现无法出现页面空白的情况,打开控制台,发现无法加载到css和js文件. 仔细观察发现路径中少了一个dis文件夹,于是我加上dist文件 ...
- jquery实现分页效果
通过jq实现分页的原理如下:将所有条数加载到页面中,根据每页放特定条数(例如 5 条)获取jquery对象索引,使部分条数显示,其他条数隐藏. 前提:引入jquery.js 代码 <!DOCTY ...
- Docker 镜像的操作
1. 从docker 镜像仓库获取镜像 docker pull [镜像名] 从 docker hub 中查看版本 获取 特定版本 docker pull centos 7.6.1801 2. ...
- nginx源码包安装
一.下载码源包 1. 获得源码包途径官方网站,可以获得最新的软件包 Nginx: www.nginx.org 2.具体实例展示(tengine) 下载源码包,准备软件包 准备编译环境如编译器gcc.m ...
- PEP8-python编码规范(下)
1.结尾逗号 结尾的逗号通常是可选的,除了在构成一个元素的元组时是强制性需要的(在Python 2 中,它们对 print 语句有语义).为了清晰起见,建议将后者用括号括起来(在技术上是多余的). Y ...
- Python_Onlineh_Hmework(基础篇,持续更新中...)
1 递归 1.1 定义一个函数,求一个数的阶乘 def func(x): if x == 2: return 2 else: return x*func(x-1) a = func(4) print( ...
- MySQL -2- 体系结构-多实例模块
0.在已创建mysql,port=3306的数据库前提下 需要删除/etc/.my.cnf再做以下操作 cp /etc/my.cnf /etc/my.cnf.3306 1.mkdir -p /u02/ ...
- Windows Forms和WPF在Net Core 3.0框架下并不会支持跨平台
Windows Forms和WPF在Net Core 3.0框架下并不会支持跨平台 微软将WinForms和WPF带到.NET Core 3.0这一事实,相信大家都有所了解,这是否意味着它在Linux ...
- priority_queue的常见用法
priority_queue的常见用法 priority_queue是什么? 优先队列 底层实现用堆来实现 每次队首的优先级最大 priority_queue的定义 引入头文件 # include & ...