From 5d97fd5b35a548233c6d77084a302c9c1f42e049 Mon Sep 17 00:00:00 2001 From: juan Date: Sun, 4 Sep 2022 15:03:27 +0800 Subject: [PATCH] vault backup: 2022-09-04 15:03:27 --- .obsidian/core-plugins.json | 4 +- .obsidian/graph.json | 2 +- .obsidian/plugins/obsidian-git/data.json | 2 +- ...Leetcode Increasing-Triplet-Subsequence.md | 114 ++++++++++++++++++ .../Leetcode Non-Overlapping-Intervals.md | 15 +-- .../pages/Leetcode Search-A-2D-Matrix-II.md | 16 +-- OJ notes/pages/Untitled.md | 0 _tools/prettify.sh | 3 + 8 files changed, 136 insertions(+), 20 deletions(-) create mode 100644 OJ notes/pages/Leetcode Increasing-Triplet-Subsequence.md delete mode 100644 OJ notes/pages/Untitled.md create mode 100755 _tools/prettify.sh diff --git a/.obsidian/core-plugins.json b/.obsidian/core-plugins.json index 936173e..1bbb163 100644 --- a/.obsidian/core-plugins.json +++ b/.obsidian/core-plugins.json @@ -12,9 +12,7 @@ "command-palette", "editor-status", "starred", - "markdown-importer", "random-note", "outline", - "word-count", - "file-recovery" + "word-count" ] \ No newline at end of file diff --git a/.obsidian/graph.json b/.obsidian/graph.json index e8aea86..04297f8 100644 --- a/.obsidian/graph.json +++ b/.obsidian/graph.json @@ -39,6 +39,6 @@ "repelStrength": 10, "linkStrength": 1, "linkDistance": 250, - "scale": 0.9328381822372834, + "scale": 0.7878344292720599, "close": true } \ No newline at end of file diff --git a/.obsidian/plugins/obsidian-git/data.json b/.obsidian/plugins/obsidian-git/data.json index e2ab79d..d360cd6 100644 --- a/.obsidian/plugins/obsidian-git/data.json +++ b/.obsidian/plugins/obsidian-git/data.json @@ -19,6 +19,6 @@ "refreshSourceControl": true, "basePath": "", "differentIntervalCommitAndPush": true, - "changedFilesInStatusBar": false, + "changedFilesInStatusBar": true, "username": "" } \ No newline at end of file diff --git a/OJ notes/pages/Leetcode Increasing-Triplet-Subsequence.md b/OJ notes/pages/Leetcode Increasing-Triplet-Subsequence.md new file mode 100644 index 0000000..0cf15a8 --- /dev/null +++ b/OJ notes/pages/Leetcode Increasing-Triplet-Subsequence.md @@ -0,0 +1,114 @@ +# Leetcode Increasing-Triplet-Subsequence + +2022-09-04 14:23 + +> ##### Algorithms: +> +> #algorithm #greedy +> +> ##### Data structures: +> +> #DS #array +> +> ##### Difficulty: +> +> #coding_problem #difficulty-medium +> +> ##### Additional tags: +> +> #leetcode #CS_list_need_understanding +> +> ##### Revisions: +> +> N/A + +##### Links: + +- [Link to problem](https://leetcode.com/problems/increasing-triplet-subsequence/) +- [Solution with explanation](https://leetcode.com/problems/increasing-triplet-subsequence/discuss/78993/Clean-and-short-with-comments-C%2B%2B) + +--- + +### Problem + +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`. + +#### Examples + +**Example 1:** + +**Input:** nums = [1,2,3,4,5] +**Output:** true +**Explanation:** Any triplet where i < j < k is valid. + +**Example 2:** + +**Input:** nums = [5,4,3,2,1] +**Output:** false +**Explanation:** No triplet exists. + +**Example 3:** + +**Input:** nums = [2,1,5,0,4,6] +**Output:** true +**Explanation:** The triplet (3, 4, 5) is valid because nums[3] == 0 < nums[4] == 4 < nums[5] == 6. + +#### Constraints + +- `1 <= nums.length <= 5 * 105` +- `-231 <= nums[i] <= 231 - 1` + +### Thoughts + +> [!summary] +> This is a #greedy problem. + +Use two variables: `small` and `big` to keep track of stuff. + +For each element, there are three possibilities: + +- the element is smaller than `small`, assign the element + to `small` + +> `small` and `big` doesn't represent `i` and `j`, +> accurately, they get updated lazily. +> And because we don't need to show `i`, `j`, `k`, this +> will be just fine + +- the element is greater than `small`: + - the element is smaller than `big`, change `big`, this + allows more possibilities + - the element is bigger than `big`, found, exit loop. + +> #### Why not #stack ? +> +> Similar to [[Leetcode Next-Greater-Element-I]], the property +> of stack can be used for **continuity** related problem, but +> here we don't need this. + +### Solution + +```cpp +class Solution { +public: + bool increasingTriplet(vector &nums) { + // greedy algorithm + int small = INT_MAX, big = INT_MAX; + + for (int i = 0, size = nums.size(); i < size; i++) { + if (nums[i] <= small) { + // Use equal sign here, to avoid small and big being same. + small = nums[i]; + } else if (nums[i] <= big) { + // Use equal sign here, to avoid three consecutive + // same numbers + big = nums[i]; + } else { + return true; + } + } + + return false; + } +}; +``` \ No newline at end of file diff --git a/OJ notes/pages/Leetcode Non-Overlapping-Intervals.md b/OJ notes/pages/Leetcode Non-Overlapping-Intervals.md index 9941e7f..1cf391a 100644 --- a/OJ notes/pages/Leetcode Non-Overlapping-Intervals.md +++ b/OJ notes/pages/Leetcode Non-Overlapping-Intervals.md @@ -4,15 +4,15 @@ > ##### Algorithms: > -> #algorithm #greedy +> #algorithm #greedy > > ##### Data structures: > -> #DS +> #DS #array > > ##### Difficulty: > -> #coding_problem #difficulty-medium +> #coding_problem #difficulty-medium > > ##### Additional tags: > @@ -54,9 +54,9 @@ Given an array of intervals `intervals` where `intervals[i] = [starti, endi]`, r #### Constraints -- `1 <= intervals.length <= 105` -- `intervals[i].length == 2` -- `-5 * 104 <= starti < endi <= 5 * 104` +- `1 <= intervals.length <= 105` +- `intervals[i].length == 2` +- `-5 * 104 <= starti < endi <= 5 * 104` ### Thoughts @@ -70,7 +70,7 @@ first sort the intervals. pick the intervals with smallest end, which will allow us to make more room for following ones. -#tip: Use `&` to reference vectors in comp.function to save +#tip: Use `&` to reference vectors in comp.function to save time. ### Solution @@ -100,3 +100,4 @@ public: return ans; } }; +``` diff --git a/OJ notes/pages/Leetcode Search-A-2D-Matrix-II.md b/OJ notes/pages/Leetcode Search-A-2D-Matrix-II.md index 0d8105b..3cb7cd0 100644 --- a/OJ notes/pages/Leetcode Search-A-2D-Matrix-II.md +++ b/OJ notes/pages/Leetcode Search-A-2D-Matrix-II.md @@ -57,13 +57,13 @@ Write an efficient algorithm that searches for a value `target` in an `m x n` in #### Constraints -- `m == matrix.length` -- `n == matrix[i].length` -- `1 <= n, m <= 300` -- `-109 <= matrix[i][j] <= 109` -- All the integers in each row are **sorted** in ascending order. -- All the integers in each column are **sorted** in ascending order. -- `-109 <= target <= 109` +- `m == matrix.length` +- `n == matrix[i].length` +- `1 <= n, m <= 300` +- `-109 <= matrix[i][j] <= 109` +- All the integers in each row are **sorted** in ascending order. +- All the integers in each column are **sorted** in ascending order. +- `-109 <= target <= 109` ### Thoughts @@ -99,4 +99,4 @@ public: return false; } }; -``` \ No newline at end of file +``` diff --git a/OJ notes/pages/Untitled.md b/OJ notes/pages/Untitled.md deleted file mode 100644 index e69de29..0000000 diff --git a/_tools/prettify.sh b/_tools/prettify.sh new file mode 100755 index 0000000..83885b0 --- /dev/null +++ b/_tools/prettify.sh @@ -0,0 +1,3 @@ +#/bin/sh + +find . -type f -iname "*.md" | xargs -0 -d "\n" prettier -w