vault backup: 2022-07-14 10:02:54
This commit is contained in:
parent
1084d88309
commit
ae73cbe6b8
|
@ -3,7 +3,7 @@
|
||||||
#### 2022-07-14 09:33
|
#### 2022-07-14 09:33
|
||||||
|
|
||||||
> ##### Algorithms:
|
> ##### Algorithms:
|
||||||
> #algorithm
|
> #algorithm #Kadane_s_algorithm
|
||||||
> ##### Data structures:
|
> ##### Data structures:
|
||||||
> #DS #string
|
> #DS #string
|
||||||
> ##### Difficulty:
|
> ##### Difficulty:
|
||||||
|
@ -15,10 +15,12 @@
|
||||||
|
|
||||||
##### Related topics:
|
##### Related topics:
|
||||||
```expander
|
```expander
|
||||||
tag:#<INSERT_TAG_HERE>
|
tag:#Kadane_s_algorithm
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
##### Links:
|
##### Links:
|
||||||
- [Link to problem](https://leetcode.com/problems/longest-substring-without-repeating-characters/)
|
- [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
|
### Thoughts
|
||||||
|
|
||||||
> [!summary]
|
> [!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
|
### 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
|
Initial solution
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
|
|
0
OJ notes/pages/Leetcode.md
Normal file
0
OJ notes/pages/Leetcode.md
Normal file
Loading…
Reference in a new issue