diff --git a/334.increasing-triplet-subsequence.js b/334.increasing-triplet-subsequence.js deleted file mode 100644 index afac973eb7d32488e381b2035731db69d90e2d13..0000000000000000000000000000000000000000 --- a/334.increasing-triplet-subsequence.js +++ /dev/null @@ -1,65 +0,0 @@ -/* - * @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; -}; - diff --git a/README.md b/README.md index e9edc7427978483f3a2ecb2f9b8eff78ee908eda..ae100f6e11478c30aac4aa9aac9bc20e50faeead 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/assets/drawio/152.maximum-product-subarray .drawio b/assets/drawio/152.maximum-product-subarray .drawio new file mode 100644 index 0000000000000000000000000000000000000000..6ae7f16dbbfad5d5038a4547d295ddfe68272c48 --- /dev/null +++ b/assets/drawio/152.maximum-product-subarray .drawio @@ -0,0 +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/ \ No newline at end of file diff --git a/assets/drawio/334.increasing-triplet-subsequence.drawio b/assets/drawio/334.increasing-triplet-subsequence.drawio new file mode 100644 index 0000000000000000000000000000000000000000..96874f889dd65c0cb1b80ca1033663327b2ecd7d --- /dev/null +++ b/assets/drawio/334.increasing-triplet-subsequence.drawio @@ -0,0 +1 @@ +1Zphb6MgGMc/jS/XWFBrX7aubrlsyXK75JJ7szCllSuKh2zt7tMfWGyrdNkuc8UtS9QHBP3x8PB/qA6M8u0VR2V2y1JMHeCmWwdeOgCM4TSUB2V52VkmwXhnWHGS6koHwz35i7XR1dYnkuKqVVEwRgUp28aEFQVORMuGOGebdrUlo+1eS7TChuE+QdS0/iSpyHbWEEwO9mtMVlnT8ziY7kpy1FTWb1JlKGWbIxNcODDijIndWb6NMFXwGi67++JXSvcPxnEh3nPD5u76z292fQPEt5/w3otcEP260K08I/rUfuFKvDQIOHsqUqxacR0432RE4PsSJap0Iwdd2jKRU3k1lqe6PcwF3r76oOP960u/wSzHgr/IKnun0cS0y0z15ebA39em7Ah9Y0N6xFf7hg9Q5Inm8h+MgMEI2GcUDosRNBhB64zAwPzIMxh59hkNzI98g5FvnREcmB8FA2TUjdnexDKkyQAnWzdoW4cUfoGobR3S1IBkf/nvhm3rkBq5Pigh2Q3c9imZcttZBM40cuR4Lnxn7jphbGCTDctsB7+NDFXlLgVakq3C3AfDIGgzHAcmQ3iCIfw0hqYcdxYTZxap/4XnzGdOGA0Vpu+3YALXN2B6J2B6nwbT1O1tmKETTgcLs+2ZwDdhnnd2mwLffgwM3M789W3HQFPi219Pu5ROTczzUjJFvn1pZlA6sRacl5Kp8g1IVYZKdUryeqfvGImCQRJEZ5SsCmkTrDyy3qBHTO9YRQRhqvSRCcFyWYGqgjlK1qt6ACJGGa/7gsv6T1apO5s1Qc89FQH181xmQqitzJkiAeIkLeCIJKxYEjm0fJTIHkGcIoHkQdkreVxSJC5Qop6ruqiNSqTGvloa4x8kWT/cIr6WK2U4KotVDwPvTUBHBIyNgZe9mSO/N/Y/9GbuMngpNenIUetSykxtvo6UCtpSyrctpYCZAX0dKdUR+b5tKQXMwP7BPa4UVVldt6e10GukczOZbesqYAbED+549Y4sGBoyM/59UGT1jcwfmpdBM8p9UL33jmxoXgaH+BtrMDjHAuby2YVUMlIIzBfP8iUrzWL/q7p77EtuG1glOFvjRqgXrMCNjD/S9wlWjb8nATidNyB9tW+nyTzy7Up9GzHKqwThUYpLjhMkcDoqWSVrPtSfKcj6S0LpUTLhuvMoBuqtiLxBP0PBuBqeXsIxDNsu4JousJ/dZ5FQ8MRu1JdP58ZvpnOXMlm7oqiqHr7jDeNr2R6IZea0rbOnWCd6Ku8jFCeUVbhO686wQE9NGdhbmicvD9++1GVHXxDBxT8= \ No newline at end of file diff --git a/assets/problems/334.increasing-triplet-subsequence.png b/assets/problems/334.increasing-triplet-subsequence.png new file mode 100644 index 0000000000000000000000000000000000000000..553cbe0e40a46bec09bab2ccdcc4af53615167c6 Binary files /dev/null and b/assets/problems/334.increasing-triplet-subsequence.png differ diff --git a/problems/152.maximum-product-subarray.md b/problems/152.maximum-product-subarray.md index 3aee557ef8e34c44cb1b8d09c315c3aae6770bb4..ec4704cd12990277049d96473d2e133790b57725 100644 --- a/problems/152.maximum-product-subarray.md +++ b/problems/152.maximum-product-subarray.md @@ -51,7 +51,7 @@ var maxProduct = function(nums) { ![152.maximum-product-subarray](../assets/problems/152.maximum-product-subarray.png) -这种思路的解法由于只需要遍历依次,其时间复杂度是O(n),代码见下方代码区。 +这种思路的解法由于只需要遍历一次,其时间复杂度是O(n),代码见下方代码区。 ## 关键点 - 同时记录乘积最大值和乘积最小值 diff --git a/problems/334.increasing-triplet-subsequence.md b/problems/334.increasing-triplet-subsequence.md new file mode 100644 index 0000000000000000000000000000000000000000..7c359e3517cdc32cc5771113668b6263e111fd73 --- /dev/null +++ b/problems/334.increasing-triplet-subsequence.md @@ -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; +}; +```