Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
luzhipeng committed Apr 28, 2019
1 parent aac59ff commit badd4e0
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 66 deletions.
65 changes: 0 additions & 65 deletions 334.increasing-triplet-subsequence.js

This file was deleted.

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ leetcode 题解,记录自己的 leetcode 解题之路。
- [240.search-a-2-d-matrix-ii](./problems/240.search-a-2-d-matrix-ii.md)
- 🖊 [279.perfect-squares](./problems/279.perfect-squares.md)
- [322.coin-change](./problems/322.coin-change.md)
- 🆕 [334.increasing-triplet-subsequence](./problems/334.increasing-triplet-subsequence.md)
- [328.odd-even-linked-list](./problems/328.odd-even-linked-list.md)
- [416.partition-equal-subset-sum](./problems/416.partition-equal-subset-sum.md)
- [445.add-two-numbers-ii](./problems/445.add-two-numbers-ii.md)
Expand Down
1 change: 1 addition & 0 deletions assets/drawio/152.maximum-product-subarray .drawio
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>
1 change: 1 addition & 0 deletions assets/drawio/334.increasing-triplet-subsequence.drawio
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/qA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huvMoBuqtiLxBP0PBuBqeXsIxDNsu4JousJ/dZ5FQ8MRu1JdP58ZvpnOXMlm7oqiqHr7jDeNr2R6IZea0rbOnWCd6Ku8jFCeUVbhO686wQE9NGdhbmicvD9++1GVHXxDBxT8=</diagram></mxfile>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion problems/152.maximum-product-subarray.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ var maxProduct = function(nums) {

![152.maximum-product-subarray](../assets/problems/152.maximum-product-subarray.png)

这种思路的解法由于只需要遍历依次,其时间复杂度是O(n),代码见下方代码区。
这种思路的解法由于只需要遍历一次,其时间复杂度是O(n),代码见下方代码区。
## 关键点

- 同时记录乘积最大值和乘积最小值
Expand Down
106 changes: 106 additions & 0 deletions problems/334.increasing-triplet-subsequence.md
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;
};
```

0 comments on commit badd4e0

Please sign in to comment.