国产xxxx99真实实拍_久久不雅视频_高清韩国a级特黄毛片_嗯老师别我我受不了了小说

資訊專欄INFORMATION COLUMN

[LeetCode] Increasing Triplet Subsequence

cooxer / 3142人閱讀

摘要:題目不要求連續(xù)的三個(gè)增長數(shù),所以只需要更新其中較小的兩個(gè)數(shù),并在第三個(gè)數(shù)滿足條件的情況下返回即可。

Problem

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.
Note

題目不要求連續(xù)的三個(gè)增長數(shù),所以只需要更新其中較小的兩個(gè)數(shù),并在第三個(gè)數(shù)滿足條件的情況下返回true即可。
怎么更新較小的兩個(gè)數(shù)呢,先判斷新數(shù)是否最小的數(shù),如果是便更新,如果不是,再判斷是否第二小的數(shù),如果是便更新,如果不是,那么一定是最大的數(shù),則符合題意,返回TRUE。

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

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://specialneedsforspecialkids.com/yun/64775.html

相關(guān)文章

  • [Leetcode] Increasing Triplet Subsequence 遞增的三元子序列

    摘要:如果右面能碰到一個(gè)數(shù)大于,說明必然存在一個(gè)遞增的三元組。復(fù)雜度空間時(shí)間測試代碼結(jié)果 Given an unsorted array return whether an increasing subsequence oflength 3 exists or not in the array. More specifically, if there exists i , j , k suc...

    coordinate35 評(píng)論0 收藏0
  • LeetCode 334. Increasing Triplet Subsequence

    摘要:描述給定一個(gè)未排序的數(shù)組,判斷這個(gè)數(shù)組中是否存在長度為的遞增子序列。說明要求算法的時(shí)間復(fù)雜度為,空間復(fù)雜度為。示例輸入輸出示例輸入輸出思路聲明三個(gè)變量,,用于表示首先遍歷數(shù)組,找到第一對(duì)滿足的數(shù)。此時(shí)依然有但是,不影響判斷的邏輯。 Description Given an unsorted array return whether an increasing subsequence o...

    saucxs 評(píng)論0 收藏0
  • leetcode334. Increasing Triplet Subsequence

    摘要:題目假設(shè)有一個(gè)無序的數(shù)組,如果數(shù)組中從左到右存在三個(gè)由小到大的數(shù)字,則返回。這個(gè)思路實(shí)在是非常的獨(dú)特,而且精煉這里它用兩個(gè)變量分別記錄了已經(jīng)遍歷過的數(shù)字中最小的數(shù)字和第二小的數(shù)字,一旦找到比這兩個(gè)數(shù)字都大的數(shù)字就證明一定存在一個(gè)升序。 題目 Given an unsorted array return whether an increasing subsequence of lengt...

    ASCH 評(píng)論0 收藏0
  • LeetCode[300] Longest Increasing Subsequence

    摘要:再用二分法找當(dāng)前值應(yīng)該在排好序的數(shù)組中的插入位置。因?yàn)橐业氖亲铋L的序列,所以每次將排好序的數(shù)組中替換成已經(jīng)排好序的,會(huì)能保證得到的結(jié)果是最長的。保證升序相等也要替換這個(gè)值 LeetCode[300] Longest Increasing Subsequence Given an unsorted array of integers, find the length of longe...

    blankyao 評(píng)論0 收藏0
  • [LeetCode] 300. Longest Increasing Subsequence

    Problem Given an unsorted array of integers, find the length of longest increasing subsequence. Example: Input: [10,9,2,5,3,7,101,18]Output: 4 Explanation: The longest increasing subsequence is [2,3,7...

    luckyyulin 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<