# [LeetCode 75] DAY 8 - Increasing Triplet Subsequence ## 題目: Given an integer array `nums`, return `true` if there exists a triple of indices `(i, j, k)` such that `i < j < k` and `nums[i] < nums[j] < nums[k]`. If no such indices exists, return `false`. ## 解題思考 - 沒邏輯的我先嘗試寫出一個連續三位數字遞增的版本 - 只要比對 <u>當前位置+2</u> 是否符合即可 - 如果要修改成不連續的版本我該怎麼做? - 將宣告兩個變數儲存**最小值**&**次小值**,並宣告為 `Infinity` - 迭代 `nums` - if < 最小值:替換掉`num1` - else if < 次小值:替換掉 `num2` - else return true :既 > num1 也 > num2 - 脫離迴圈 return false ## 程式碼 **連續** ```javascript function increasingTriplet(nums) { for (let i = 0; i < nums.length - 2; i++) { if (nums[i] < nums[i + 1] && nums[i + 1] < nums[i + 2]) { return true; } } return false; }; ``` **不連續的版本:** ```javascript function increasingTriplet(nums) { let num1 = Infinity; let num2 = Infinity; for (let num of nums) { if (num <= num1) { num1 = num; } else if (num <= num2) { num2 = num; } else return true; } return false; } ``` ## 延伸思考 ## 相關資源 - [334. Increasing Triplet Subsequence](https://leetcode.com/problems/increasing-triplet-subsequence/description/)
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up