forked from azl397985856/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
luzhipeng
committed
Apr 28, 2019
1 parent
aac59ff
commit badd4e0
Showing
7 changed files
with
110 additions
and
66 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<mxfile modified="2019-04-28T04:04:43.390Z" host="www.draw.io" agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36" etag="x7taIV4aV3HK_th4ormZ" version="10.6.5" type="device"><diagram id="YXMyPOv_ULgBjCf9Ohy7" name="第 1 页">7Znfb5swEMf/mjy2wja/8rik6TYpk6Z20qS+TA5cgjXAyDgN3V8/EwyBOG02iQRU9SlwPoz53Nd3R5iQeVJ8FjSLvvEQ4gm2wmJC7iYYIzL11U9peaksnosqw0awUDsdDI/sD2ijpa1bFkLecZScx5JlXWPA0xQC2bFRIfiu67bmcfeuGd2AYXgMaGxaf7JQRpXVx97B/gXYJqrvjNxpNZLQ2lk/SR7RkO9aJrKYkLngXFZHSTGHuIRXc6muu39ltFmYgFT+ywV2ePf08LB6Xn79sbSe7ueLmYxusF1N80zjrX5ivVr5UiMQfJuGUM6CJmS2i5iEx4wG5ehOBV3ZIpnEejiXgv+GOY+52F9NVr5jO5YaWbM4btnXfgBBoOx6ASAkFK8+GmqAKaUBT0CKF+WiLyBEM9Yiw65W3e4QMle7RK1oOY4WihbJppn5wFEdaJT/g5W8B6zNJtRY0dQbFisyqL6F1TqPtQdIyDmCVJ+fgWRbl9KeAYkMDslQ0tCQzP15M7yUsDcySmZxsAeHdJzrB4fknk/1GWepBLF4Vs+Y1xm9bgdKbCHNo4bhq2k/5amacBbTFcTfec4k46kyB1BOrgZKrky1LssjhxWXkicth08x25QDkpfxofqsmUctLStXnhSbsqm7TfKAwm0ImYCASghvM54rz1/7/sosQZY1m9/jnrbEUbSnZrDxqQp0qWBPjWAntFCG/bHlGpFXU6pOFc7vDJpnVfu6ZkWphGOoIQV/HZzqBNzAh9W6rzztdHBjy+TtXJN3XTfawFnaADe3Wn/AHfBD+xRwH6+I614IuDc0cDPntxXeJ/Am6aGh5I6cscnduZrc36R/Fe0b9AfXfq+lHJ15g2vK5Kjrue45+kh103GVcuR9RPty0SbOyKLtv1nXTrz99ZRa+9g53tjK1Ik2uFWmbtCwfdnF+Q9dqPCJrrglZvMlfTxaJmRkWsbmn4odLdv+u9KywX9wLeOPMny5Moy8a5VhdXr4lrUfa30RJIu/</diagram></mxfile> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<mxfile modified="2019-04-28T05:44:04.589Z" host="www.draw.io" agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36" etag="PODMzlxkKwJ-VLBAo4pq" version="10.6.5" type="device"><diagram id="a3jdPwh5aMc9FGEKUp8T" name="第 1 页">1Zphb6MgGMc/jS/XWFBrX7aubrlsyXK75JJ7szCllSuKh2zt7tMfWGyrdNkuc8UtS9QHBP3x8PB/qA6M8u0VR2V2y1JMHeCmWwdeOgCM4TSUB2V52VkmwXhnWHGS6koHwz35i7XR1dYnkuKqVVEwRgUp28aEFQVORMuGOGebdrUlo+1eS7TChuE+QdS0/iSpyHbWEEwO9mtMVlnT8ziY7kpy1FTWb1Jl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> |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
|
||
## 题目地址 | ||
https://leetcode.com/problems/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-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 | ||
``` | ||
|
||
## 思路 | ||
这道题是求解顺序数字是否有三个递增的排列, 注意这里没有要求连续的,因此诸如滑动窗口的思路是不可以的。 | ||
题目要求O(n)的时间复杂度和O(1)的空间复杂度,因此暴力的做法就不用考虑了。 | ||
|
||
我们的目标就是`依次`找到三个数字,其顺序是递增的。因此我们的做法可以是依次遍历, | ||
然后维护三个变量,分别记录最小值,第二小值,第三小值。只要我们能够填满这三个变量就返回true,否则返回false。 | ||
|
||
![334.increasing-triplet-subsequence](../assets/problems/334.increasing-triplet-subsequence.png) | ||
## 关键点解析 | ||
|
||
- 维护三个变量,分别记录最小值,第二小值,第三小值。只要我们能够填满这三个变量就返回true,否则返回false | ||
|
||
## 代码 | ||
```js | ||
|
||
|
||
/* | ||
* @lc app=leetcode id=334 lang=javascript | ||
* | ||
* [334] Increasing Triplet Subsequence | ||
* | ||
* https://leetcode.com/problems/increasing-triplet-subsequence/description/ | ||
* | ||
* algorithms | ||
* Medium (39.47%) | ||
* Total Accepted: 89.6K | ||
* Total Submissions: 226.6K | ||
* Testcase Example: '[1,2,3,4,5]' | ||
* | ||
* 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 | ||
* | ||
* | ||
* | ||
*/ | ||
/** | ||
* @param {number[]} nums | ||
* @return {boolean} | ||
*/ | ||
var increasingTriplet = function(nums) { | ||
if (nums.length < 3) return false; | ||
let n1 = Number.MAX_VALUE; | ||
let n2 = Number.MAX_VALUE; | ||
|
||
for(let i = 0; i < nums.length; i++) { | ||
if (nums[i] <= n1) { | ||
n1 = nums[i] | ||
} else if (nums[i] <= n2) { | ||
n2 = nums[i] | ||
} else { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
}; | ||
``` |