vault backup: 2022-09-04 15:03:27

This commit is contained in:
juan 2022-09-04 15:03:27 +08:00
parent 64455a46e4
commit 5d97fd5b35
8 changed files with 136 additions and 20 deletions

View file

@ -12,9 +12,7 @@
"command-palette",
"editor-status",
"starred",
"markdown-importer",
"random-note",
"outline",
"word-count",
"file-recovery"
"word-count"
]

View file

@ -39,6 +39,6 @@
"repelStrength": 10,
"linkStrength": 1,
"linkDistance": 250,
"scale": 0.9328381822372834,
"scale": 0.7878344292720599,
"close": true
}

View file

@ -19,6 +19,6 @@
"refreshSourceControl": true,
"basePath": "",
"differentIntervalCommitAndPush": true,
"changedFilesInStatusBar": false,
"changedFilesInStatusBar": true,
"username": ""
}

View file

@ -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<int> &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;
}
};
```

View file

@ -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;
}
};
```

View file

@ -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;
}
};
```
```

3
_tools/prettify.sh Executable file
View file

@ -0,0 +1,3 @@
#/bin/sh
find . -type f -iname "*.md" | xargs -0 -d "\n" prettier -w