[LeetCode] Increasing Triplet Subsequence

題目:Increasing Triplet Subsequence
難度:Medium

題目是要在矩陣中找到三個遞增的數字 N1、N2、N3,如果存在則回傳 true,反之回傳 false。
解題想法是:

  1. 先定義 N1、N2 為 int.MaxValue
  2. 然後開始迭代,如果當前數字 num 小於 N1,則 N1 = num
  3. 如果當前數字 num 大於 N1 且 小於 N2,則 N2 = num
  4. 如果當前數字 num 大於 N1 且 大於 N2,則 N3 = num,即滿足題目需求

在討論串看到,如果 input 是 [2, 5, 0, 6],得到的答案會是 [0, 5, 6],但應該是 [2, 5, 6]
這不影響最後的答案,因為問題是要找是否存在三個遞增的數字,以上面的 input 為例,只要有數字小於 5 即可,不管它是 2 還是 0

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
public class Solution {
public bool IncreasingTriplet(int[] nums) {
// 如果長度小於 3,則直接 return false
if (nums.Length < 3)
{
return false;
}

int smallest = int.MaxValue;
int secondSmallest = int.MaxValue;

foreach(int num in nums)
{
// 如果當前數字小於 smallest
if (num <= smallest)
{
smallest = num;
}
// 如果當前數字大於 smallest 且小於 secondSmallest
else if (num <= secondSmallest)
{
secondSmallest = num;
}
// 如果當前數字大於 smallest 且大於 secondSmallest
// 滿足題目需求,找到三個遞增的數字
else
{
return true;
}
}

return false;
}
}

Comments