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

  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】376. Wiggle Subsequence 解题报告(Python)

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

  3. 【LeetCode】392. Is Subsequence 解题报告(Python)

    [LeetCode]392. Is Subsequence 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/is-subseq ...

  4. 【LeetCode】Increasing Triplet Subsequence(334)

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

  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

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

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

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

  9. 【LeetCode】62. Unique Paths 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...

随机推荐

  1. UE4打包启动失败:RunUAT.bat ERROR: AutomationTool failed to compile.

    打包配置正常的情况下,出现下面Log: RunUAT.bat ERROR: AutomationTool failed to compile. 基本上,可以先排查下任务管理器中是不是有UE4Edito ...

  2. 学习java的第十天

    一.今日收获 1.java完全学习手册第二章2.9程序流程控制中的选择结构与顺序结构的例题 2.观看哔哩哔哩上的教学视频 二.今日问题 1.例题的问题不大,需要注意大小写,新的语句记忆不牢 2.哔哩哔 ...

  3. 《Scala编程》课程作业

    第一题.百元喝酒 作业要求:每瓶啤酒2元,3个空酒瓶或者5个瓶盖可换1瓶啤酒.100元最多可喝多少瓶啤酒?(不允许借啤酒) 思路:利用递归算法,一次性买完,然后递归算出瓶盖和空瓶能换的啤酒数 /** ...

  4. Spark基础:(五)Spark编程进阶

    共享变量 (1)累加器:是用来对信息进行聚合的,同时也是Spark中提供的一种分布式的变量机制,其原理类似于mapreduce,即分布式的改变,然后聚合这些改变.累加器的一个常见用途是在调试时对作业执 ...

  5. 大数据学习day15----第三阶段----scala03--------1.函数(“_”的使用, 函数和方法的区别)2. 数组和集合常用的方法(迭代器,并行集合) 3. 深度理解函数 4 练习(用java实现类似Scala函数式编程的功能(不能使用Lambda表达式))

    1. 函数 函数就是一个非常灵活的运算逻辑,可以灵活的将函数传入方法中,前提是方法中接收的是类型一致的函数类型 函数式编程的好处:想要做什么就调用相应的方法(fliter.map.groupBy.so ...

  6. 零基础学习java------25--------jdbc

    jdbc开发步骤图 以下要用到的products表 一. JDBC简介 补充    JDBC本质:其实是官方(sun公司)定义的一套操作所有关系型数据库的规则,即接口,各个数据库厂商趋势线这个接口,提 ...

  7. vue3 使用 data、computed、methods

    简单数据ref复杂数据reactive 使用方法: // useCount.js import {ref,reactive,computed} from 'vue' export default fu ...

  8. Struts 2 基础篇【转】

    转载至 : http://www.blogjava.net/huamengxing/archive/2009/10/21/299153.html Struts2架构图 有视频讲解百度一下就可以找到了 ...

  9. Oracle—表、约束、索引、表空间、分区、序列、统计信息

    表.约束.索引.表空间.分区.序列.统计信息 一.表及其操作 1.创建表 create table 表名 ( 字段名1 字段类型 默认值 是否为空 , 字段名2 字段类型 默认值 是否为空, 字段名3 ...

  10. Shell学习(二)——变量和基本数据类型

    参考博客: [1]LinuxShell脚本--变量和数据类型 [2]shell只读变量删除 一.变量 定义变量的语法 定义变量时,变量名和变量值之间使用"="分隔,并且等号两边不能 ...