Skip to content

Commit d277c23

Browse files
committed
0340 solved.
1 parent 97df423 commit d277c23

File tree

4 files changed

+104
-0
lines changed

4 files changed

+104
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
cmake_minimum_required(VERSION 3.13)
2+
project(cpp_0340)
3+
4+
set(CMAKE_CXX_STANDARD 14)
5+
6+
add_executable(cpp_0340 main.cpp)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/// Source : https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/
2+
/// Author : liuyubobobo
3+
/// Time : 2019-02-23
4+
5+
#include <iostream>
6+
#include <unordered_map>
7+
#include <cassert>
8+
9+
using namespace std;
10+
11+
12+
/// Sliding Window
13+
/// Time Complexity: O(n)
14+
/// Space Complexity: O(1)
15+
class Solution {
16+
public:
17+
int lengthOfLongestSubstringKDistinct(string s, int k) {
18+
19+
if(!k) return 0;
20+
21+
int l = 0, r = -1, res = 0;
22+
unordered_map<char, int> freq;
23+
while(r + 1 < s.size()){
24+
25+
while(r + 1 < s.size() && ok(freq, s[r + 1], k))
26+
freq[s[r + 1]] ++, r ++;
27+
// assert(freq.size() == k);
28+
res = max(res, r - l + 1);
29+
30+
while(l <= r && freq.size() >= k){
31+
freq[s[l]] --;
32+
if(freq[s[l]] == 0) freq.erase(s[l]);
33+
l ++;
34+
}
35+
}
36+
return res;
37+
}
38+
39+
private:
40+
bool ok(const unordered_map<char, int>& freq, char c, int k){
41+
42+
if(freq.size() <= k - 1) return true;
43+
44+
assert(freq.size() == k);
45+
return freq.count(c);
46+
}
47+
};
48+
49+
50+
int main() {
51+
52+
return 0;
53+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/// Source : https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/
2+
/// Author : liuyubobobo
3+
/// Time : 2019-02-23
4+
5+
#include <iostream>
6+
#include <unordered_map>
7+
#include <cassert>
8+
9+
using namespace std;
10+
11+
12+
/// Sliding Window (Another implement version)
13+
/// Time Complexity: O(n)
14+
/// Space Complexity: O(1)
15+
class Solution {
16+
public:
17+
int lengthOfLongestSubstringKDistinct(string s, int k) {
18+
19+
if(!k) return 0;
20+
21+
int l = 0, r = -1, res = 0;
22+
unordered_map<char, int> freq;
23+
while(r + 1 < s.size()){
24+
25+
if(freq.size() < k || freq.count(s[r + 1]))
26+
freq[s[r + 1]] ++, r ++;
27+
else{
28+
freq[s[l]] --;
29+
if(freq[s[l]] == 0) freq.erase(s[l]);
30+
l ++;
31+
}
32+
33+
res = max(res, r - l + 1);
34+
}
35+
return res;
36+
}
37+
};
38+
39+
40+
int main() {
41+
42+
return 0;
43+
}

readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,8 @@ email: [liuyubobobo@gmail.com](mailto:liuyubobobo@gmail.com)
299299
| | | | | | |
300300
| 337 | [House Robber III](https://leetcode.com/problems/house-robber-iii/description/) | [] | [C++](0337-House-Robber-III/cpp-0337/) | | |
301301
| | | | | | |
302+
| 340 | [Longest Substring with At Most K Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/) | [solution](https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/solution/) | [C++](0340-Longest-Substring-with-At-Most-K-Distinct-Characters/cpp-0340/) | | |
303+
| | | | | | |
302304
| 343 | [Integer Break](https://leetcode.com/problems/integer-break/description/) | [] | [C++](0343-Integer-Break/cpp-0343/) | [Java](0343-Integer-Break/java-0343/src/) | |
303305
| 344 | [Reverse String](https://leetcode.com/problems/reverse-string/description/) | [] | [C++](0344-Reverse-String/cpp-0344/) | | |
304306
| 345 | [Reverse Vowels of a String](https://leetcode.com/problems/reverse-vowels-of-a-string/description/) | [] | [C++](0345-Reverse-Vowels-of-a-String/cpp-0345/) | | |

0 commit comments

Comments
 (0)