【中级算法】5.递增的三元子序列

题目:

给定一个未排序的数组,请判断这个数组中是否存在长度为3的递增的子序列。

正式的数学表达如下:

如果存在这样的 i, j, k,  且满足 0 ≤ i < j < k ≤ n-1,
使得 arr[i] < arr[j] < arr[k] ,返回 true ; 否则返回 false 。
要求算法时间复杂度为O(n),空间复杂度为O(1) 。

示例:
输入 [1, 2, 3, 4, 5],
输出 true.

输入 [5, 4, 3, 2, 1],
输出 false.

 解法:

class Solution {
public:
    bool increasingTriplet(vector<int>& nums) {
       int len = nums.size();
        if(len <= 2){
            return false;
        }

        vector<int> minleft(len,nums[0]);
        vector<int> maxright(len,nums[len-1]);

       for(int i = 1;i < len;++i){
           minleft[i] = min(nums[i],minleft[i-1]);
       }

       for(int i = len-2;i >= 0;--i){
           maxright[i] = max(nums[i],maxright[i+1]);
       }

       for(int i = 1; i < len-1; ++i){
           if(minleft[i-1] < nums[i] && nums[i] < maxright[i+1]){
               return true;
           }
       }

       return false;
    }
};

  

原文地址:https://www.cnblogs.com/mikemeng/p/9206955.html

时间: 2024-08-30 06:39:41

【中级算法】5.递增的三元子序列的相关文章

LeetCode:递增的三元子序列【334】

LeetCode:递增的三元子序列[334] 题目描述 给定一个未排序的数组,判断这个数组中是否存在长度为 3 的递增子序列. 数学表达式如下: 如果存在这样的 i, j, k,  且满足 0 ≤ i < j < k ≤ n-1,使得 arr[i] < arr[j] < arr[k] ,返回 true ; 否则返回 false . 说明: 要求算法的时间复杂度为 O(n),空间复杂度为 O(1) . 示例 1: 输入: [1,2,3,4,5] 输出: true 示例 2: 输入:

【LeetCode】334#递增的三元子序列

题目描述 给定一个未排序的数组,判断这个数组中是否存在长度为 3 的递增子序列. 数学表达式如下: 如果存在这样的 i, j, k, 且满足 0 ≤ i < j < k ≤ n-1, 使得 arr[i] < arr[j] < arr[k] ,返回 true ; 否则返回 false . 说明: 要求算法的时间复杂度为 O(n),空间复杂度为 O(1) . 示例 1: 输入: [1,2,3,4,5] 输出: true 示例 2: 输入: [5,4,3,2,1] 输出: false 解

递增的三元子序列

给定一个未排序的数组,请判断这个数组中是否存在长度为3的递增的子序列. 正式的数学表达如下: 如果存在这样的 i, j, k,  且满足 0 ≤ i < j < k ≤ n-1,使得 arr[i] < arr[j] < arr[k] ,返回 true ; 否则返回 false . 要求算法时间复杂度为O(n),空间复杂度为O(1) . 示例:输入 [1, 2, 3, 4, 5],输出 true. 输入 [5, 4, 3, 2, 1],输出 false. 致谢:特别感谢 @Djang

(Java) LeetCode 334. Increasing Triplet Subsequence —— 递增的三元子序列

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

[Swift]LeetCode334. 递增的三元子序列 | Increasing Triplet Subsequence

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

[LeetCode] Increasing Triplet Subsequence 递增的三元子序列

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

FreeCodeCamp( FCC)前端工程师 中级算法练习 分析与解答(全)(精)

[TOC] 说在前面 这是要一篇非常简单的新手能看懂的文章,希望你喜欢.由于在 freecodecamp 中貌似!?无法使用 ES6 的某些语法,未测试具体.所以基本上用古老?!的ES5,4写成,谢谢.在写本博文前没有参考过别人的做法,纯手打,我的方法肯定不是最好,只是以我自己喜欢的方式在写而已. 纯原创,转载请联系作者https//:[email protected].[email protected]. freecodecamp China 不明白API请参考MDN给出的解释 个别题目没有判

[LeetCode] 334. Increasing Triplet Subsequence 递增三元子序列

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

字符串算法之最长公共子序列

最长公共子序列,即 longest common subsequence,LCS.一个字符串删掉任意字符后所形成的字符串,不要求连续,注意和最长公共子串的区别. LCS的应用:论文查重,图形相似度比较,基因序列比较等. 暴力求解: 分别求出X.Y串的子序列,而后进行搜索比较,容易得到该算法复杂度为O(2^m · 2^n ),显然不可取 动态规划: 设有两个字符串X[1....m],Y[1....n],求其最长公共子串 假设Xi Yi为两个字符串从1开始数的第i个字符,若xm=yn,则xm必在最长