[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 false.

Note: Your algorithm should run in O(n) time complexity and O(1) space complexity.

Example 1:

Input: [1,2,3,4,5]
Output: true

Example 2:

Input: [5,4,3,2,1]
Output: false


给定一个未排序的数组,判断这个数组中是否存在长度为 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

16ms
 1 class Solution {
 2     func increasingTriplet(_ nums: [Int]) -> Bool {
 3         guard nums.count >= 3 else {
 4             return false
 5         }
 6
 7         var first = Int.max
 8         var second = Int.max
 9
10         for num in nums {
11             if num <= first {
12                 first = num
13             } else if num <= second {
14                 second = num
15             } else {
16                 return true
17             }
18         }
19         return false
20     }
21 }


20ms

 1 class Solution {
 2        func increasingTriplet(_ nums: [Int]) -> Bool {
 3
 4     guard nums.count >= 3 else {
 5
 6       return false
 7     }
 8
 9     var minvalue = nums[0]
10
11     var middle = Int.max
12
13     var minvalue1 = nums[0]
14
15     for i in 1..<(nums.count - 1) {
16
17       if nums[i] <= minvalue1 {
18
19         if middle != Int.max {
20
21           minvalue1 = nums[i]
22         }
23         else {
24
25           minvalue = nums[i]
26
27           minvalue1 = minvalue
28
29           middle = Int.max
30         }
31       }
32       else if nums[i] <= middle {
33
34         middle = nums[i]
35
36         minvalue = minvalue1
37       }
38       else {
39
40         return true
41       }
42     }
43
44     return minvalue < middle && middle < nums.last!
45   }
46 }


48ms

 1 class Solution {
 2     func increasingTriplet(_ nums: [Int]) -> Bool {
 3         if nums.count < 3 {
 4             return false
 5         }
 6         var minI = nums[0]
 7         var bigMin : Int? = nil
 8
 9         for i in 1..<nums.count {
10             if bigMin != nil && nums[i] > bigMin! {
11                 return true
12             }
13
14             if nums[i] < minI {
15                 minI = nums[i]
16             }
17             if nums[i] > minI {
18                 if bigMin == nil {
19                     bigMin = nums[i]
20                 }else {
21                     bigMin = min(nums[i], bigMin!)
22                 }
23             }
24         }
25
26         return false
27     }
28 }


56ms

 1 class Solution {
 2     func increasingTriplet(_ nums: [Int]) -> Bool {
 3         var minInt:Int = Int.max
 4         var maxInt:Int = Int.max
 5         for item in nums {
 6             if minInt >= item {
 7                 minInt = item
 8             }else if maxInt >= item {
 9                 maxInt = item
10             }else{
11                 return true
12             }
13         }
14         return false
15     }
16 }


56ms

 1 class Solution {
 2     func increasingTriplet(_ nums: [Int]) -> Bool {
 3         guard nums.count > 2 else {
 4             return false
 5         }
 6
 7         var n1 = Int.max
 8         var n2 = Int.max - 1
 9
10         for n in nums {
11             if n <= n1 {
12                 n1 = n
13             } else if n < n2 {
14                 n2 = n
15             } else if n1 < n2 && n2 < n {
16                 return true
17             }
18         }
19
20         return false
21     }
22 }

原文地址:https://www.cnblogs.com/strengthen/p/10261970.html

时间: 2024-08-01 03:56:05

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

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 解

[LeetCode][JavaScript]Increasing Triplet Subsequence

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 

递增的三元子序列

给定一个未排序的数组,请判断这个数组中是否存在长度为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

[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

[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

LeetCode-334. 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-

leetcode-334 Increasing Triplet Subsequence 解题记录

题目链接: https://leetcode.com/problems/increasing-triplet-subsequence/ 题目大意: 找出一个三元的上升子序列.返回bool值,表示是否存在. O(n)时间,O(1)空间 解题思路: 维护第一小数,第二小数. 一但出现数大于第二小数,即可break宣布找到. 扫一遍,维护第一小数和第二小数即可. 1 class Solution { 2 public: 3 bool increasingTriplet(vector<int>&