关闭

LeetCode:Increasing Triplet Subsequence

标签: LeetCodeIncreasing Triplet S
2631人阅读 评论(0) 收藏 举报
分类:

Increasing Triplet Subsequence




Total Accepted: 14818 Total Submissions: 43443 Difficulty: Medium

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.

Credits:
Special thanks to @DjangoUnchained for adding this problem and creating all test cases.

Subscribe to see which companies asked this question

Hide Similar Problems
 (M) Longest Increasing Subsequence































java code:

public class Solution {
    public boolean increasingTriplet(int[] nums) {
        
        int max = (1<<31)-1;
        int mid = (1<<31)-1;
        
        for(int num : nums) {
            if(num <= mid)
                mid = num;
            else if(num <= max)
                max = num;
            else
                return true;
        }
        
        return false;
    }
}


1
0
查看评论
发表评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场

最长递增和递减子序列详解(longest increasing subsequence)

对于动态规划问题,往往存在递推解决方法,这个问题也不例外。要求长度为i的序列的Ai{a1,a2,……,ai}最长递增子序列,需要先求出序列Ai-1{a1,a2,……,ai-1}中以各元素(a1,a2,...
  • seekcreation
  • seekcreation
  • 2014-10-09 21:45
  • 668

leetcode -- 392. Is Subsequence 【贪心算法 + 双指针 + 无后效性】

题目 Given a string s and a string t, check if s is subsequence of t. You may assume that there ...
  • TheSnowBoy_2
  • TheSnowBoy_2
  • 2017-06-17 23:04
  • 435

动态规划之最长递增子序列(Longest Increasing Subsequence)

原文地址:http://www.geeksforgeeks.org/dynamic-programming-set-3-longest-increasing-subsequence/ We have...
  • sinat_36246371
  • sinat_36246371
  • 2016-10-13 12:12
  • 930

leetcode小白解题记录——第三题

3. Longest Substring Without Repeating Characters Given a string, find the length of the long...
  • lin_tuer
  • lin_tuer
  • 2016-08-18 16:14
  • 445

LeetCode 300.Longest Increasing Subsequence 在一维数组中找最长序列(好题)

题目 Given an unsorted array of integers, find the length of longest increasing subsequence. For...
  • gddxmmxf
  • gddxmmxf
  • 2017-04-13 02:45
  • 110

三元组Triplet的Java实现(数据结构 例1-6)

/*实现三元组*/ public class Triplet { int[] e=new int[3]; public Triplet(int e1,int e2,int e3){//相当于Ini...
  • xky1306102chenhong
  • xky1306102chenhong
  • 2015-11-10 22:36
  • 448

基于Triplet loss 函数训练人脸识别深度网络

基于Triplet loss 函数训练人脸识别深度网络 在FaceNet中,作者提出了基于度量学习的误差函数Triplet loss,其思想来源如下: ∥∥xai−xpi∥∥22+thresh...
  • kklots
  • kklots
  • 2015-10-08 11:53
  • 12218

leetcode之392. Is Subsequence(C++解法 动态规划 贪心 模式匹配)

模式匹配,字符串查找,动态规划
  • haimianxiaodao
  • haimianxiaodao
  • 2016-09-21 18:31
  • 1194

Caffe中增加新的layer以及Caffe中triplet loss layer的实现

【如何在caffe中增加新的layer】【caffe中实现Triplettloss layer】【caffe中新增加的layer的编译和测试】
  • tangwei2014
  • tangwei2014
  • 2015-07-09 12:21
  • 6301

动态规划求最长递增子序列(longest increasing subsequence)

1,什么是动态规划? 在现实生活中,有一类活动的过程,由于它的特殊性,可将过程分成若干个互相联系的阶段,在它的每一阶段都需要作出决策,从而使整个过程达到最好的活动效果。当然,各个阶段决策的选取不是任意...
  • zzuchengming
  • zzuchengming
  • 2015-11-04 22:00
  • 871
    个人资料
    • 访问:378493次
    • 积分:6670
    • 等级:
    • 排名:第4149名
    • 原创:318篇
    • 转载:1篇
    • 译文:0篇
    • 评论:33条
    博客专栏
    最新评论