【LeetCode】334. Increasing Triplet Subsequence 解题报告(Python)
【LeetCode】334. Increasing Triplet Subsequence 解题报告(Python)
标签(空格分隔): LeetCode
题目地址:https://leetcode.com/problems/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的递增的序列。
解题方法
用LIS的解法一定能做出来的,但是不符合题目给出的O(n)的时间复杂度。看了别人的解法发现真的很巧妙。我们完全可以抛弃什么DP啊,dfs啊,老夫写代码就是一把梭,抓起键盘就是干!
既然要求我们从前到后遍历,那么在遍历的时候保存已经看到的最小值和次小值,然后再发现比这两个值大的的第3小的值存在的时候,那么就说明有长度为3的递增的子序列了。
当然,对于这种情况:
4 5 1 2 6
长度为3递增子序列有两种,但是由于我们保存的是最小的优先,所以最后的结果求得的是1 2 6这组。
整体的思想其实是很灵活的,保存的是遍历时见到的最小和次小,因此千万不要使用一成不变的min和max函数。
代码:
class Solution(object):
def increasingTriplet(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
first, second = float('inf'), float('inf')
for num in nums:
if num <= first:
first = num
elif num <= second:
second = num
else:
return True
return False
日期
2018 年 4 月 5 日 ———— 清明节假期开始,小长假真好~~
【LeetCode】334. Increasing Triplet Subsequence 解题报告(Python)的更多相关文章
- [LeetCode] 334. Increasing Triplet Subsequence 递增三元子序列
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...
- 【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】Increasing Triplet Subsequence(334)
1. Description Given an unsorted array return whether an increasing subsequence of length 3 exists o ...
- 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
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 ...
- 【LeetCode】62. Unique Paths 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...
随机推荐
- 面向Web应用的并发压力测试工具——Locust实用攻略
1. 概述 该方案写作目的在于描述一个基于Locust实现的压力测试,文中详细地描述了如何利用locustfile.py文件定义期望达成的测试用例,并利用Locust对目标站点进行并发压力测试. 特别 ...
- SQLite is 35% Faster Than The Filesystem
比方说你要在C++/PHP里实现一个函数Image get_image(string id),不同的图片有1万张(用户头像),你可以把它们存在一个目录/文件夹里,然后fopen()再fread. 你也 ...
- Android 开源框架Universal-Image-Loader加载https图片
解决方案就是 需要 android https HttpsURLConnection 这个类忽略证书 1,找到 Universal-Image-Loader的library依赖包下面com.nostr ...
- restful接口文档
1.先理清业务bai流程 2.定义前后端开发的接口规范.比如json的格dao式,url的格式 3.定内义接口文容档,这里的接口文档一般就是对应后台的实体reqVo(调用后台接口<控制器> ...
- Oracle中IS TABLE OF的使用
IS TABLE OF :指定是一个集合的表的数组类型,简单的来说就是一个可以存储一列多行的数据类型. INDEX BY BINARY_INTEGER:指索引组织类型 BULK COLLECT :指是 ...
- nodejs代码初探之nodejs启动
nodejs启动 入口在node_main.cc,解析参数后进入node.cc 中的node::Start() V8::Initialize() //初始化v8SetupProcessObject() ...
- @RestController和@Controller的区别与作用
在springMvc中controller层类上的要使用@Controller来注明该类属于控制层,在controller层常返回的数据形式有以下几种: 页面:静态页面 ModelAndView:返回 ...
- java客户端的elasticSearch索引库的相关操作
package com.hope.es;import org.elasticsearch.client.transport.TransportClient;import org.elasticsear ...
- 机器学习——可视化绘图matplotlib和seaborn
安装matplotlib和seaborn https://blog.csdn.net/Jia_jinjin/article/details/80428598 seaborn pairplot:特征两两 ...
- VSCode上发布第一篇博客
在VSCode上发布到博客园的第一篇博客 前段时间在VSCode安装好插件WriteCnblog,多次检查writeCnblog configuration配置信息也是完全正确的,但是一直没能在VSC ...