Skip to content

Commit 4cc20bf

Browse files
committed
Apr-20
1 parent ddf6c8e commit 4cc20bf

8 files changed

+215
-48
lines changed

README.md

+8-5
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
5555
| [] | [leetcode-top-100-liked.md](./list/leetcode-top-100-liked.md) | 100/100 | - |
5656
| [] | [leetcode101.md](./list/leetcode101.md) | 183/184 | 1 vip |
5757
| [🔲] | [9c-basic.md](./list/9c-basic.md) | 14/128 | 3 vips |
58-
| [🔲] | [endlesscheng.md](./list/endlesscheng.md) | 87/2209 | 4 vips |
58+
| [🔲] | [endlesscheng.md](./list/endlesscheng.md) | 88/2209 | 4 vips |
5959

60-
**Solved**: 669 problems
60+
**Solved**: 670 problems
6161

6262
## 类型/Category
6363

@@ -103,7 +103,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
103103

104104
## Math
105105

106-
| Link | Problem(48) | Solution | Tag | Time | Space | Ref |
106+
| Link | Problem(49) | Solution | Tag | Time | Space | Ref |
107107
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
108108
| [Leetcode-415](https://leetcode.com/problems/add-strings/) | Add Strings | [c++](./leetcode/415.add-strings.cpp), [python3](./leetcode/415.add-strings.py) | Math | O\(N\) | O\(1\) | - |
109109
| [Leetcode-2](https://leetcode.com/problems/add-two-numbers/) | Add Two Numbers | [c++](./leetcode/2.add-two-numbers.cpp), [python3](./leetcode/2.add-two-numbers.py) | Math | O\(N \+ M\) | O\(1\) | - |
@@ -144,6 +144,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
144144
| [Leetcode-326](https://leetcode.com/problems/power-of-three/) | Power Of Three | [c++](./leetcode/326.power-of-three.cpp), [python3](./leetcode/326.power-of-three.py) | Math | O\(1\) | O\(1\) | - |
145145
| [Leetcode-50](https://leetcode.com/problems/powx-n/) | Powx N | [c++](./leetcode/50.powx-n.cpp), [python3](./leetcode/50.powx-n.py) | Math | O\(logN\) | O\(1\) | - |
146146
| [Leetcode-1352](https://leetcode.com/problems/product-of-the-last-k-numbers/) | Product Of The Last K Numbers | [c++](./leetcode/1352.product-of-the-last-k-numbers.cpp), [python3](./leetcode/1352.product-of-the-last-k-numbers.py) | Math | O\(1\) | O\(N\) | - |
147+
| [Leetcode-781](https://leetcode.com/problems/rabbits-in-forest/) | Rabbits In Forest | [c++](./leetcode/781.rabbits-in-forest.cpp), [python3](./leetcode/781.rabbits-in-forest.py) | Math | O\(N\) | O\(N\) | - |
147148
| [Leetcode-836](https://leetcode.com/problems/rectangle-overlap/) | Rectangle Overlap | [c++](./leetcode/836.rectangle-overlap.cpp), [python3](./leetcode/836.rectangle-overlap.py) | Math | O\(1\) | O\(1\) | - |
148149
| [Leetcode-13](https://leetcode.com/problems/roman-to-integer/) | Roman To Integer | [c++](./leetcode/13.roman-to-integer.cpp), [python3](./leetcode/13.roman-to-integer.py) | Math | O\(N\) | O\(1\) | - |
149150
| [Leetcode-189](https://leetcode.com/problems/rotate-array/) | Rotate Array | [c++](./leetcode/189.rotate-array.cpp), [python3](./leetcode/189.rotate-array.py) | Math | O\(N\) | O\(1\) | - |
@@ -290,7 +291,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
290291

291292
## Greedy
292293

293-
| Link | Problem(41) | Solution | Tag | Time | Space | Ref |
294+
| Link | Problem(42) | Solution | Tag | Time | Space | Ref |
294295
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
295296
| [Leetcode-870](https://leetcode.com/problems/advantage-shuffle/) | Advantage Shuffle | [c++](./leetcode/870.advantage-shuffle.cpp), [python3](./leetcode/870.advantage-shuffle.py) | Greedy | O\(NlogN\) | O\(N\) | - |
296297
| [Leetcode-2818](https://leetcode.com/problems/apply-operations-to-maximize-score/) | Apply Operations To Maximize Score | [c++](./leetcode/2818.apply-operations-to-maximize-score.cpp), [python3](./leetcode/2818.apply-operations-to-maximize-score.py) | Greedy | O\(\(N \+ K\) \* logN\) | O\(Max\) | - |
@@ -321,6 +322,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
321322
| [Leetcode-435](https://leetcode.com/problems/non-overlapping-intervals/) | Non Overlapping Intervals | [c++](./leetcode/435.non-overlapping-intervals.cpp), [python3](./leetcode/435.non-overlapping-intervals.py) | Greedy | O\(NlogN\) | O\(1\) | - |
322323
| [Leetcode-763](https://leetcode.com/problems/partition-labels/) | Partition Labels | [c++](./leetcode/763.partition-labels.cpp), [python3](./leetcode/763.partition-labels.py) | Greedy | O\(N\) | O\(N\) | - |
323324
| [Leetcode-2551](https://leetcode.com/problems/put-marbles-in-bags/) | Put Marbles In Bags | [c++](./leetcode/2551.put-marbles-in-bags.cpp), [python3](./leetcode/2551.put-marbles-in-bags.py) | Greedy | O\(N\) | O\(N\) | - |
325+
| [Leetcode-781](https://leetcode.com/problems/rabbits-in-forest/) | Rabbits In Forest | [c++](./leetcode/781.rabbits-in-forest.cpp), [python3](./leetcode/781.rabbits-in-forest.py) | Greedy | O\(N\) | O\(N\) | - |
324326
| [Leetcode-3282](https://leetcode.com/problems/reach-end-of-array-with-max-score/) | Reach End Of Array With Max Score | [c++](./leetcode/3282.reach-end-of-array-with-max-score.cpp), [python3](./leetcode/3282.reach-end-of-array-with-max-score.py) | Greedy | O\(N\) | O\(1\) | - |
325327
| [Leetcode-3439](https://leetcode.com/problems/reschedule-meetings-for-maximum-free-time-i/) | Reschedule Meetings For Maximum Free Time I | [c++](./leetcode/3439.reschedule-meetings-for-maximum-free-time-i.cpp), [python3](./leetcode/3439.reschedule-meetings-for-maximum-free-time-i.py) | Greedy | O\(N\) | O\(N\) | - |
326328
| [Leetcode-632](https://leetcode.com/problems/smallest-range-covering-elements-from-k-lists/) | Smallest Range Covering Elements From K Lists | [c++](./leetcode/632.smallest-range-covering-elements-from-k-lists.cpp), [python3](./leetcode/632.smallest-range-covering-elements-from-k-lists.py) | Greedy | O\(NlogN\) | O\(N\) | - |
@@ -546,7 +548,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
546548

547549
## Hash Table
548550

549-
| Link | Problem(118) | Solution | Tag | Time | Space | Ref |
551+
| Link | Problem(119) | Solution | Tag | Time | Space | Ref |
550552
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
551553
| [Leetcode-721](https://leetcode.com/problems/accounts-merge/) | Accounts Merge | [c++](./leetcode/721.accounts-merge.cpp), [python3](./leetcode/721.accounts-merge.py) | Hash Table | O\(MlogM\) | O\(N\) | - |
552554
| [Leetcode-863](https://leetcode.com/problems/all-nodes-distance-k-in-binary-tree/) | All Nodes Distance K In Binary Tree | [c++](./leetcode/863.all-nodes-distance-k-in-binary-tree.cpp), [python3](./leetcode/863.all-nodes-distance-k-in-binary-tree.py) | Hash Table | O\(N\) | O\(N\) | - |
@@ -632,6 +634,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
632634
| [Leetcode-763](https://leetcode.com/problems/partition-labels/) | Partition Labels | [c++](./leetcode/763.partition-labels.cpp), [python3](./leetcode/763.partition-labels.py) | Hash Table | O\(N\) | O\(N\) | - |
633635
| [Leetcode-567](https://leetcode.com/problems/permutation-in-string/) | Permutation In String | [c++](./leetcode/567.permutation-in-string.cpp), [python3](./leetcode/567.permutation-in-string.py) | Hash Table | O\(N\+M\) | O\(N\) | - |
634636
| [Leetcode-2848](https://leetcode.com/problems/points-that-intersect-with-cars/) | Points That Intersect With Cars | [c++](./leetcode/2848.points-that-intersect-with-cars.cpp), [python3](./leetcode/2848.points-that-intersect-with-cars.py) | Hash Table | O\(N\) | O\(N\) | - |
637+
| [Leetcode-781](https://leetcode.com/problems/rabbits-in-forest/) | Rabbits In Forest | [c++](./leetcode/781.rabbits-in-forest.cpp), [python3](./leetcode/781.rabbits-in-forest.py) | Hash Table | O\(N\) | O\(N\) | - |
635638
| [Leetcode-383](https://leetcode.com/problems/ransom-note/) | Ransom Note | [c++](./leetcode/383.ransom-note.cpp), [python3](./leetcode/383.ransom-note.py) | Hash Table | O\(N\) | O\(N\) | - |
636639
| [Leetcode-13](https://leetcode.com/problems/roman-to-integer/) | Roman To Integer | [c++](./leetcode/13.roman-to-integer.cpp), [python3](./leetcode/13.roman-to-integer.py) | Hash Table | O\(N\) | O\(1\) | - |
637640
| [Leetcode-73](https://leetcode.com/problems/set-matrix-zeroes/) | Set Matrix Zeroes | [c++](./leetcode/73.set-matrix-zeroes.cpp), [python3](./leetcode/73.set-matrix-zeroes.py) | Hash Table | O\(NM\) | O\(N \+ M\) | - |

leetcode/424.longest-repeating-character-replacement.cpp

+25-17
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,23 @@ class Solution {
3333
public:
3434
int characterReplacement(string s, int k) {
3535
int n = s.size();
36-
int j = 0;
36+
int i = 0;
3737
vector<int> counter(26, 0);
38-
int mas_freq = 0;
38+
int max_freq = 0;
3939
int res = 0;
4040

41-
for (int i = 0; i < n; i++) {
42-
counter[s[i] - 'A'] += 1;
43-
mas_freq = max(mas_freq, counter[s[i] - 'A']);
41+
for (int j = 0; j < n; j++) {
42+
counter[s[j] - 'A'] += 1;
43+
max_freq = max(max_freq, counter[s[j] - 'A']);
4444

4545
// length - most_frequent <= k
4646
// kepp most_frequent unchanged since smaller frequnt won't contribute to the result
47-
while (i - j + 1 - mas_freq > k) {
48-
counter[s[j] - 'A']--;
49-
j++;
47+
while (j - i + 1 - max_freq > k) {
48+
counter[s[i] - 'A']--;
49+
i++;
5050
}
5151

52-
res = max(res, i - j + 1);
52+
res = max(res, j - i + 1);
5353
}
5454

5555
return res;
@@ -59,19 +59,27 @@ class Solution {
5959
class Solution {
6060
public:
6161
int characterReplacement(string s, int k) {
62+
int res = 0;
63+
for (int i = 0; i < 26; i++) {
64+
res = max(res, count(s, 'A' + i, k));
65+
}
66+
return res;
67+
}
68+
69+
int count(string &s, char target, int k) {
6270
int n = s.size();
63-
int j = 0;
64-
vector<int> counter(26, 0);
71+
int i = 0;
72+
int count = 0;
6573
int res = 0;
6674

67-
for (int i = 0; i < n; i++) {
68-
counter[s[i] - 'A'] += 1;
69-
while (i - j + 1 - *max_element(counter.begin(), counter.end()) > k) {
70-
counter[s[j] - 'A']--;
71-
j++;
75+
for (int j = 0; j < n; j++) {
76+
count += s[j] != target;
77+
while (count > k) {
78+
count -= s[i] != target;
79+
i++;
7280
}
7381

74-
res = max(res, i - j + 1);
82+
res = max(res, j - i + 1);
7583
}
7684

7785
return res;

leetcode/424.longest-repeating-character-replacement.py

+20-24
Original file line numberDiff line numberDiff line change
@@ -34,44 +34,40 @@ class Solution:
3434
def characterReplacement(self, s: str, k: int) -> int:
3535
counter = defaultdict(int)
3636
n = len(s)
37-
j = 0
37+
i = 0
3838
res = 0
3939
most_frequent = 0
4040

41-
for i in range(n):
42-
counter[s[i]] += 1
43-
most_frequent = max(most_frequent, counter[s[i]])
41+
for j in range(n):
42+
counter[s[j]] += 1
43+
most_frequent = max(most_frequent, counter[s[j]])
4444

4545
# length - most_frequent <= k
4646
# kepp most_frequent unchanged since smaller frequnt won't contribute to the result
47-
while i - j + 1 - most_frequent > k:
48-
counter[s[j]] -= 1
49-
j += 1
47+
while j - i + 1 - most_frequent > k:
48+
counter[s[i]] -= 1
49+
i += 1
5050

51-
res = max(res, i - j + 1)
51+
res = max(res, j - i + 1)
5252

5353
return res
5454

55-
56-
from collections import defaultdict
5755
class Solution:
5856
def characterReplacement(self, s: str, k: int) -> int:
59-
counter = defaultdict(int)
60-
n = len(s)
61-
j = 0
6257
res = 0
63-
for i in range(n):
64-
counter[s[i]] += 1
65-
while i - j + 1 - self.most_frequent(counter) > k:
66-
counter[s[j]] -= 1
67-
j += 1
68-
69-
res = max(res, i - j + 1)
70-
58+
for ch in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ':
59+
res = max(res, self.count(s, ch, k))
7160
return res
7261

73-
def most_frequent(self, counter: dict) -> int:
62+
def count(self, s: str, target: str, k: int) -> int:
63+
n = len(s)
64+
i = 0
7465
res = 0
75-
for key in counter:
76-
res = max(res, counter[key])
66+
count = 0
67+
for j in range(n):
68+
count += s[j] != target
69+
while count > k:
70+
count -= s[i] != target
71+
i += 1
72+
res = max(res, j - i + 1)
7773
return res

leetcode/438.find-all-anagrams-in-a-string.cpp

+39
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,45 @@ class Solution {
6868
}
6969
}
7070

71+
return res;
72+
}
73+
};
74+
75+
class Solution {
76+
public:
77+
vector<int> findAnagrams(string s, string p) {
78+
int n = s.size();
79+
int k = p.size();
80+
unordered_map<char, int> counter;
81+
for (char &c : p) {
82+
counter[c]++;
83+
}
84+
85+
int count = 0;
86+
vector<int> res;
87+
for (int i = 0; i < n; ++i) {
88+
if (counter.find(s[i]) != counter.end()) {
89+
counter[s[i]]--;
90+
if (counter[s[i]] >= 0) {
91+
count++;
92+
}
93+
}
94+
95+
if (i >= k - 1) {
96+
if (count == k) {
97+
res.push_back(i - k + 1);
98+
}
99+
100+
char left = s[i - k + 1];
101+
if (counter.find(left) != counter.end()) {
102+
counter[left]++;
103+
if (counter[left] > 0) {
104+
count--;
105+
}
106+
}
107+
}
108+
}
109+
71110
return res;
72111
}
73112
};

leetcode/438.find-all-anagrams-in-a-string.py

+26
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,30 @@ def findAnagrams(self, s: str, p: str) -> List[int]:
5656
if hit == k:
5757
res.append(i - k + 1)
5858

59+
return res
60+
61+
from collections import Counter
62+
class Solution:
63+
def findAnagrams(self, s: str, p: str) -> List[int]:
64+
n = len(s)
65+
k = len(p)
66+
counter = Counter(p)
67+
count = 0
68+
res = []
69+
for i in range(n):
70+
if s[i] in counter:
71+
counter[s[i]] -= 1
72+
if counter[s[i]] >= 0:
73+
count += 1
74+
75+
if i >= k - 1:
76+
if count == k:
77+
res.append(i - k + 1)
78+
79+
left = s[i - k + 1]
80+
if left in counter:
81+
counter[left] += 1
82+
if counter[left] >= 1:
83+
count -= 1
84+
5985
return res

leetcode/781.rabbits-in-forest.cpp

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Tag: Array, Hash Table, Math, Greedy
2+
// Time: O(N)
3+
// Space: O(N)
4+
// Ref: -
5+
// Note: -
6+
// Video: https://youtu.be/nOAC3RimLUI
7+
8+
// There is a forest with an unknown number of rabbits. We asked n rabbits "How many rabbits have the same color as you?" and collected the answers in an integer array answers where answers[i] is the answer of the ith rabbit.
9+
// Given the array answers, return the minimum number of rabbits that could be in the forest.
10+
//  
11+
// Example 1:
12+
//
13+
// Input: answers = [1,1,2]
14+
// Output: 5
15+
// Explanation:
16+
// The two rabbits that answered "1" could both be the same color, say red.
17+
// The rabbit that answered "2" can't be red or the answers would be inconsistent.
18+
// Say the rabbit that answered "2" was blue.
19+
// Then there should be 2 other blue rabbits in the forest that didn't answer into the array.
20+
// The smallest possible number of rabbits in the forest is therefore 5: 3 that answered plus 2 that didn't.
21+
//
22+
// Example 2:
23+
//
24+
// Input: answers = [10,10,10]
25+
// Output: 11
26+
//
27+
//  
28+
// Constraints:
29+
//
30+
// 1 <= answers.length <= 1000
31+
// 0 <= answers[i] < 1000
32+
//
33+
//
34+
35+
class Solution {
36+
public:
37+
int numRabbits(vector<int>& answers) {
38+
unordered_map<int, int> counter;
39+
for (auto &x: answers) {
40+
counter[x] += 1;
41+
}
42+
int res = 0;
43+
for (auto &[x, count]: counter) {
44+
int size = x + 1;
45+
int group = (count + size - 1) / size;
46+
res += group * size;
47+
}
48+
49+
return res;
50+
}
51+
};

leetcode/781.rabbits-in-forest.py

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Tag: Array, Hash Table, Math, Greedy
2+
# Time: O(N)
3+
# Space: O(N)
4+
# Ref: -
5+
# Note: -
6+
# Video: https://youtu.be/nOAC3RimLUI
7+
8+
# There is a forest with an unknown number of rabbits. We asked n rabbits "How many rabbits have the same color as you?" and collected the answers in an integer array answers where answers[i] is the answer of the ith rabbit.
9+
# Given the array answers, return the minimum number of rabbits that could be in the forest.
10+
#  
11+
# Example 1:
12+
#
13+
# Input: answers = [1,1,2]
14+
# Output: 5
15+
# Explanation:
16+
# The two rabbits that answered "1" could both be the same color, say red.
17+
# The rabbit that answered "2" can't be red or the answers would be inconsistent.
18+
# Say the rabbit that answered "2" was blue.
19+
# Then there should be 2 other blue rabbits in the forest that didn't answer into the array.
20+
# The smallest possible number of rabbits in the forest is therefore 5: 3 that answered plus 2 that didn't.
21+
#
22+
# Example 2:
23+
#
24+
# Input: answers = [10,10,10]
25+
# Output: 11
26+
#
27+
#  
28+
# Constraints:
29+
#
30+
# 1 <= answers.length <= 1000
31+
# 0 <= answers[i] < 1000
32+
#
33+
#
34+
35+
from collections import Counter
36+
class Solution:
37+
def numRabbits(self, answers: List[int]) -> int:
38+
counter = Counter(answers)
39+
res = 0
40+
for x in counter:
41+
size = x + 1
42+
group = (counter[x] + size - 1) // size
43+
res += group * size
44+
return res

list/endlesscheng.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@
105105
89. https://leetcode.com/problems/friends-of-appropriate-ages/ +Mark
106106
90. https://leetcode.com/problems/longest-nice-subarray/
107107
91. https://leetcode.com/problems/swap-for-longest-repeated-character-substring/
108-
- https://leetcode.com/problems/longest-repeating-character-replacement/
109-
- https://leetcode.com/problems/find-all-anagrams-in-a-string/ +duplicate
108+
92. https://leetcode.com/problems/longest-repeating-character-replacement/
109+
93. https://leetcode.com/problems/find-all-anagrams-in-a-string/ +duplicate
110110
- https://leetcode.com/problems/ways-to-split-array-into-three-subarrays/
111111
- https://leetcode.com/problems/kth-smallest-subarray-sum/
112112

0 commit comments

Comments
 (0)