Leetcode: Increasing Triplet Subsequence

时间:2022-10-29 12:48:01
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.

Naive Solution: use DP, Time O(N^2), Space O(N)

  dp[i] represents the length of longest increasing subsequence till i including element i in nums array. dp[i] is initialized to be 1.

  dp[i] = max(dp[i], dp[j]+1), where j is an index before i

 public class Solution {
public boolean increasingTriplet(int[] nums) {
int[] dp = new int[nums.length];
for (int i=0; i<nums.length; i++) {
dp[i] = 1;
for (int j=0; j<i; j++) {
if (nums[j] < nums[i]) {
dp[i] = Math.max(dp[i], dp[j]+1);
}
if (dp[i] == 3) return true;
}
}
return false;
}
}

Better Solution: keep two values. Once find a number bigger than both, while both values have been updated, return true.

small: is the minimum value ever seen untill now

big: the smallest value that has something before it that is even smaller. That 'something before it that is even smaller' does not have to be the current min value.

Example:
3,2,1,4,0,5

When you see 5, min value is 0, and the smallest second value is 4, which is not after the current min value.

 public class Solution {
public boolean increasingTriplet(int[] nums) {
int small = Integer.MAX_VALUE, big = Integer.MAX_VALUE;
for (int n : nums) {
if (n <= small) {
small = n;
}
else if (n <= big) {
big = n;
}
else return true;
}
return false;
}
}

Leetcode: Increasing Triplet Subsequence的更多相关文章

  1. &lbrack;LeetCode&rsqb; Increasing Triplet Subsequence 递增的三元子序列

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

  2. LeetCode——Increasing Triplet Subsequence

    Question Given an unsorted array return whether an increasing subsequence of length 3 exists or not ...

  3. 【LeetCode】334&period; Increasing Triplet Subsequence 解题报告(Python)

    [LeetCode]334. Increasing Triplet Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...

  4. &lbrack;LeetCode&rsqb; 334&period; Increasing Triplet Subsequence 递增三元子序列

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

  5. 【LeetCode】Increasing Triplet Subsequence(334)

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

  6. 【leetcode】Increasing Triplet Subsequence

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

  7. &lbrack;Swift&rsqb;LeetCode334&period; 递增的三元子序列 &vert; Increasing Triplet Subsequence

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

  8. Increasing Triplet Subsequence

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

  9. LeetCode-334&period; Increasing Triplet Subsequence

    Description: Given an unsorted array return whether an increasing subsequence of length 3 exists or ...

随机推荐

  1. webpack-dev-server轻量级js高速打包、热部署服务器

    webpack是一个打包web项目的工具 ,可以实现css,js,less,cass,html的混淆加密,minify,结合webpack-dev-server热部署,非常方便前端页面和Nodejs的 ...

  2. java&period;net&period;SocketException&colon; No buffer space available &lpar;maximum connections reached&quest;&rpar;&colon; JVM&lowbar;Bind

    1. 启动注册表编辑器. HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services/Tcpip/Parameters 2. 新建 值名称:MaxUser ...

  3. PHP Deprecated&colon; Comments starting with &&num;39&semi;&num;&&num;39&semi; are deprecated in &ast;&period;ini 警告解决办法

    新装的ubuntu 10.04系统,使用新立得装的PHP,但是每次我在命令行下执行php脚本时都会出如下的警告信息: PHP Deprecated:  Comments starting with ' ...

  4. Ant学习---第二节:Ant添加文件夹和文件夹集的使用

    一.创建 java 项目(Eclipse 中),结构图如下: 1.创建 .java 文件,代码如下: package com.learn.ant; public class HelloWorld { ...

  5. ActiveX控件

    什么是ActiveX控件:一个进程内服务器,支持多种的COM接口.(可以理解为,一个COM接口是一个纯抽象基类,你实现了它,并且它支持自注册,就是一个ActiveX控件了)可以把ActiveX控件看做 ...

  6. 为 Devops 和系统管理员提供的 400&plus; 免费资源

    014年,谷歌索引的数据量大约为200TB(1T等于1024 GB).而且,据估计,谷歌的200TB只占到整个互联网的0.004%.基本上,互联网是一个拥有无限的信息的地方. 因此,为了努力降低搜索和 ...

  7. Spring Boot&comma;Spring Data JPA多数据源支持

    1 配置文件 wisely.primary.datasource.driverClassName=oracle.jdbc.OracleDriver wisely.primary.datasource. ...

  8. jaxb和dozer简介

    一.jaxb是什么     JAXB是Java Architecture for XML Binding的缩写.可以将一个Java对象转变成为XML格式,反之亦然.     我们把对象与关系数据库之间 ...

  9. 集美大学网络1413第十四次作业成绩(团队九) -- 测试与发布&amp&semi;博客展示(Beta版本)

    题目 团队作业9--测试与发布(Beta版本) 团队作业9成绩  团队/分值 Beta版本测试报告 Beta版本发布说明       总分  Bug类别. 数量 场景测试 测试结果 测试矩阵 出口条件 ...

  10. 使用python找出nginx访问日志中访问次数最多的10个ip排序生成网页

    使用python找出nginx访问日志中访问次数最多的10个ip排序生成网页 方法1:linux下使用awk命令 # cat access1.log | awk '{print $1" &q ...