vault backup: 2022-07-14 10:02:54

This commit is contained in:
juan 2022-07-14 10:02:54 +08:00
parent 1084d88309
commit ae73cbe6b8
2 changed files with 41 additions and 6 deletions

View file

@ -3,7 +3,7 @@
#### 2022-07-14 09:33
> ##### Algorithms:
> #algorithm
> #algorithm #Kadane_s_algorithm
> ##### Data structures:
> #DS #string
> ##### Difficulty:
@ -15,11 +15,13 @@
##### Related topics:
```expander
tag:#<INSERT_TAG_HERE>
tag:#Kadane_s_algorithm
```
##### Links:
- [Link to problem](https://leetcode.com/problems/longest-substring-without-repeating-characters/)
___
@ -56,12 +58,45 @@ Notice that the answer must be a substring, "pwke" is a subsequence and not a su
### Thoughts
> [!summary]
> This is a #
> This is a #Kadane_s_algorithm
Initially, I thought of a lineal algorithm
Initially, I thought of a kadane's algo, but implemented wrong.
Then I figured out kadane's algorithm.
similar to this one.
[[Leetcode Best-Time-To-Buy-And-Sell-Stock]]
The goal is making cur as small as possible, without duplicating
### Solution
Kadane's algorithm
```cpp
class Solution {
public:
int lengthOfLongestSubstring(string s) {
// Kadane's algorithm
vector<int> hMap(255, -1);
int cur = -1;
int localMax = 0;
for (int i = 0; i < s.length(); i++) {
if (hMap[s[i]] > cur) {
// If the element occurs again, reset
cur = hMap[s[i]];
}
hMap[s[i]] = i;
localMax = max(localMax, i - cur);
}
return localMax;
}
};
```
Initial solution
```cpp

View file