【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. centos 7的命令变化

    1.service -> systemctl命令 2.ifconfig -> ip 命令 3.netstat -> ss命令 4.route -> ip route命令 5.t ...

  2. 使用SpringBoot实现文件的上传

    使用SpringBoot实现文件的上传 springboot可以直接使用 org.springframework.web.multipart.MultipartFile 所以非常容易实现 一.首先是简 ...

  3. HDFS05 NameNode和SecondaryNameNode

    NameNode和SecondaryNameNode(了解) 目录 NameNode和SecondaryNameNode(了解) NN 和 2NN 工作机制 NameNode工作机制 Secondar ...

  4. 日常Java 2021/9/19

    Math类方法 package m; public class m { public static void main(String args[]) { //计算平方根 System.out.prin ...

  5. aboard, abolish

    aboard board做动词有上车/船/飞机的意思,boarding就是正在上.board做名词有板的意思,车厢地板的板. a是个词根,有三种意思:1. 以某种状态或方式,如: ablaze, af ...

  6. 零基础学习java------day4------流程控制结构

    1. 顺序结构 代码从上往下依次执行 2. 选择结构 也叫分支结构,其会根据执行的结果选择不同的代码执行,有以下两种形式: if  语句 switch  语句 2.1 if 语句 2.1.1  if语 ...

  7. Oracle中的DBMS_LOCK包的使用

    一.DBMS_LOCK相关知识介绍 锁模式: 名字 描述 数据类型 值 nl_mode Null INTEGER 1 ss_mode Sub Shared: used on an aggregate ...

  8. @Value("#{}")与@Value("${}")

    开发过程中,有些配置信息是根据环境的不同而有不同的值.这个时候,我们需要获取配置文件中的值或者spring容器中的值,可以通过@value注解获取相关的值. @Value("#{}" ...

  9. 将图片打印到word中

    1.生成模板文件 工具类: package com.sfec.snmgr.track.utils;import com.alibam.core.wechat.util.QRCodeUtil;impor ...

  10. Declarative Pipeline 基础语法

    Declarative Pipeline(声明式)核心概念 核心概念用来组织pipeline的运行流程 1.pipeline :声明其内容为一个声明式的pipeline脚本 2.agent:执行节点( ...