Skip to content

Commit 2a1e3e7

Browse files
committedApr 25, 2025
Apr-25
1 parent 2a397b1 commit 2a1e3e7

6 files changed

+275
-6
lines changed
 

‎README.md

Lines changed: 8 additions & 5 deletions
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) | 15/128 | 3 vips |
58-
| [🔲] | [endlesscheng.md](./list/endlesscheng.md) | 92/2210 | 4 vips |
58+
| [🔲] | [endlesscheng.md](./list/endlesscheng.md) | 93/2210 | 4 vips |
5959

60-
**Solved**: 675 problems
60+
**Solved**: 677 problems
6161

6262
## 类型/Category
6363

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

554554
## Hash Table
555555

556-
| Link | Problem(120) | Solution | Tag | Time | Space | Ref |
556+
| Link | Problem(121) | Solution | Tag | Time | Space | Ref |
557557
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
558558
| [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\) | - |
559559
| [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\) | - |
@@ -570,6 +570,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
570570
| [Leetcode-1399](https://leetcode.com/problems/count-largest-group/) | Count Largest Group | [c++](./leetcode/1399.count-largest-group.cpp), [python3](./leetcode/1399.count-largest-group.py) | Hash Table | O\(N\) | O\(N\) | - |
571571
| [Leetcode-2364](https://leetcode.com/problems/count-number-of-bad-pairs/) | Count Number Of Bad Pairs | [c++](./leetcode/2364.count-number-of-bad-pairs.cpp), [python3](./leetcode/2364.count-number-of-bad-pairs.py) | Hash Table | O\(N\) | O\(N\) | - |
572572
| [Leetcode-1248](https://leetcode.com/problems/count-number-of-nice-subarrays/) | Count Number Of Nice Subarrays | [c++](./leetcode/1248.count-number-of-nice-subarrays.cpp), [python3](./leetcode/1248.count-number-of-nice-subarrays.py) | Hash Table | O\(N\) | O\(1\) | - |
573+
| [Leetcode-2845](https://leetcode.com/problems/count-of-interesting-subarrays/) | Count Of Interesting Subarrays | [c++](./leetcode/2845.count-of-interesting-subarrays.cpp), [python3](./leetcode/2845.count-of-interesting-subarrays.py) | Hash Table | O\(N\) | O\(N\) | - |
573574
| [Leetcode-3306](https://leetcode.com/problems/count-of-substrings-containing-every-vowel-and-k-consonants-ii/) | Count Of Substrings Containing Every Vowel And K Consonants II | [c++](./leetcode/3306.count-of-substrings-containing-every-vowel-and-k-consonants-ii.cpp), [python3](./leetcode/3306.count-of-substrings-containing-every-vowel-and-k-consonants-ii.py) | Hash Table | O\(N\) | O\(N\) | - |
574575
| [Leetcode-3298](https://leetcode.com/problems/count-substrings-that-can-be-rearranged-to-contain-a-string-ii/) | Count Substrings That Can Be Rearranged To Contain A String II | [c++](./leetcode/3298.count-substrings-that-can-be-rearranged-to-contain-a-string-ii.cpp), [python3](./leetcode/3298.count-substrings-that-can-be-rearranged-to-contain-a-string-ii.py) | Hash Table | O\(N\) | O\(M\) | - |
575576
| [Leetcode-3325](https://leetcode.com/problems/count-substrings-with-k-frequency-characters-i/) | Count Substrings With K Frequency Characters I | [c++](./leetcode/3325.count-substrings-with-k-frequency-characters-i.cpp), [python3](./leetcode/3325.count-substrings-with-k-frequency-characters-i.py) | Hash Table | O\(N\) | O\(N\) | - |
@@ -686,14 +687,15 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
686687

687688
## Prefix Sum
688689

689-
| Link | Problem(45) | Solution | Tag | Time | Space | Ref |
690+
| Link | Problem(46) | Solution | Tag | Time | Space | Ref |
690691
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
691692
| [Leetcode-2968](https://leetcode.com/problems/apply-operations-to-maximize-frequency-score/) | Apply Operations To Maximize Frequency Score | [c++](./leetcode/2968.apply-operations-to-maximize-frequency-score.cpp), [python3](./leetcode/2968.apply-operations-to-maximize-frequency-score.py) | Prefix Sum | O\(NlogN\) | O\(1\) | - |
692693
| [Leetcode-930](https://leetcode.com/problems/binary-subarrays-with-sum/) | Binary Subarrays With Sum | [c++](./leetcode/930.binary-subarrays-with-sum.cpp), [python3](./leetcode/930.binary-subarrays-with-sum.py) | Prefix Sum | O\(N\) | O\(1\) | - |
693694
| [Leetcode-1094](https://leetcode.com/problems/car-pooling/) | Car Pooling | [c++](./leetcode/1094.car-pooling.cpp), [python3](./leetcode/1094.car-pooling.py) | Prefix Sum | O\(NlogN\) | O\(N\) | - |
694695
| [Leetcode-1893](https://leetcode.com/problems/check-if-all-the-integers-in-a-range-are-covered/) | Check If All The Integers In A Range Are Covered | [c++](./leetcode/1893.check-if-all-the-integers-in-a-range-are-covered.cpp), [python3](./leetcode/1893.check-if-all-the-integers-in-a-range-are-covered.py) | Prefix Sum | O\(N\) | O\(N\) | - |
695696
| [Leetcode-525](https://leetcode.com/problems/contiguous-array/) | Contiguous Array | [c++](./leetcode/525.contiguous-array.cpp), [python3](./leetcode/525.contiguous-array.py) | Prefix Sum | O\(N\) | O\(N\) | - |
696697
| [Leetcode-1248](https://leetcode.com/problems/count-number-of-nice-subarrays/) | Count Number Of Nice Subarrays | [c++](./leetcode/1248.count-number-of-nice-subarrays.cpp), [python3](./leetcode/1248.count-number-of-nice-subarrays.py) | Prefix Sum | O\(N\) | O\(1\) | - |
698+
| [Leetcode-2845](https://leetcode.com/problems/count-of-interesting-subarrays/) | Count Of Interesting Subarrays | [c++](./leetcode/2845.count-of-interesting-subarrays.cpp), [python3](./leetcode/2845.count-of-interesting-subarrays.py) | Prefix Sum | O\(N\) | O\(N\) | - |
697699
| [Leetcode-2302](https://leetcode.com/problems/count-subarrays-with-score-less-than-k/) | Count Subarrays With Score Less Than K | [c++](./leetcode/2302.count-subarrays-with-score-less-than-k.cpp), [python3](./leetcode/2302.count-subarrays-with-score-less-than-k.py) | Prefix Sum | O\(N\) | O\(1\) | - |
698700
| [Leetcode-3261](https://leetcode.com/problems/count-substrings-that-satisfy-k-constraint-ii/) | Count Substrings That Satisfy K Constraint II | [c++](./leetcode/3261.count-substrings-that-satisfy-k-constraint-ii.cpp), [python3](./leetcode/3261.count-substrings-that-satisfy-k-constraint-ii.py) | Prefix Sum | O\(N \+ Q\) | O\(N\) | - |
699701
| [Leetcode-2145](https://leetcode.com/problems/count-the-hidden-sequences/) | Count The Hidden Sequences | [c++](./leetcode/2145.count-the-hidden-sequences.cpp), [python3](./leetcode/2145.count-the-hidden-sequences.py) | Prefix Sum | O\(N\) | O\(1\) | - |
@@ -843,7 +845,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
843845

844846
## Two Pointers
845847

846-
| Link | Problem(45) | Solution | Tag | Time | Space | Ref |
848+
| Link | Problem(46) | Solution | Tag | Time | Space | Ref |
847849
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
848850
| [Leetcode-15](https://leetcode.com/problems/3sum/) | 3Sum | [c++](./leetcode/15.3sum.cpp), [python3](./leetcode/15.3sum.py) | Two Pointers | O\(N^2\) | O\(1\) | - |
849851
| [Leetcode-18](https://leetcode.com/problems/4sum/) | 4Sum | [c++](./leetcode/18.4sum.cpp), [python3](./leetcode/18.4sum.py) | Two Pointers | O\(N^3\) | O\(1\) | - |
@@ -866,6 +868,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
866868
| [Leetcode-88](https://leetcode.com/problems/merge-sorted-array/) | Merge Sorted Array | [c++](./leetcode/88.merge-sorted-array.cpp), [python3](./leetcode/88.merge-sorted-array.py) | Two Pointers | O\(M \+ N\) | O\(1\) | - |
867869
| [Leetcode-2570](https://leetcode.com/problems/merge-two-2d-arrays-by-summing-values/) | Merge Two 2D Arrays By Summing Values | [c++](./leetcode/2570.merge-two-2d-arrays-by-summing-values.cpp), [python3](./leetcode/2570.merge-two-2d-arrays-by-summing-values.py) | Two Pointers | O\(N\) | O\(1\) | - |
868870
| [Leetcode-876](https://leetcode.com/problems/middle-of-the-linked-list/) | Middle Of The Linked List | [c++](./leetcode/876.middle-of-the-linked-list.cpp), [python3](./leetcode/876.middle-of-the-linked-list.py) | Two Pointers | O\(N\) | O\(1\) | - |
871+
| [Leetcode-1750](https://leetcode.com/problems/minimum-length-of-string-after-deleting-similar-ends/) | Minimum Length Of String After Deleting Similar Ends | [c++](./leetcode/1750.minimum-length-of-string-after-deleting-similar-ends.cpp), [python3](./leetcode/1750.minimum-length-of-string-after-deleting-similar-ends.py) | Two Pointers | O\(N\) | O\(1\) | - |
869872
| [Leetcode-1040](https://leetcode.com/problems/moving-stones-until-consecutive-ii/) | Moving Stones Until Consecutive II | [c++](./leetcode/1040.moving-stones-until-consecutive-ii.cpp), [python3](./leetcode/1040.moving-stones-until-consecutive-ii.py) | Two Pointers | O\(NlogN\) | O\(1\) | - |
870873
| [Leetcode-234](https://leetcode.com/problems/palindrome-linked-list/) | Palindrome Linked List | [c++](./leetcode/234.palindrome-linked-list.cpp), [python3](./leetcode/234.palindrome-linked-list.py) | Two Pointers | O\(N\) | O\(1\) | - |
871874
| [Leetcode-647](https://leetcode.com/problems/palindromic-substrings/) | Palindromic Substrings | [c++](./leetcode/647.palindromic-substrings.cpp), [python3](./leetcode/647.palindromic-substrings.py) | Two Pointers | O\(N^2\) | O\(1\) | - |
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Tag: Two Pointers, String
2+
// Time: O(N)
3+
// Space: O(1)
4+
// Ref: -
5+
// Note: -
6+
7+
// Given a string s consisting only of characters 'a', 'b', and 'c'. You are asked to apply the following algorithm on the string any number of times:
8+
//
9+
// Pick a non-empty prefix from the string s where all the characters in the prefix are equal.
10+
// Pick a non-empty suffix from the string s where all the characters in this suffix are equal.
11+
// The prefix and the suffix should not intersect at any index.
12+
// The characters from the prefix and suffix must be the same.
13+
// Delete both the prefix and the suffix.
14+
//
15+
// Return the minimum length of s after performing the above operation any number of times (possibly zero times).
16+
//  
17+
// Example 1:
18+
//
19+
// Input: s = "ca"
20+
// Output: 2
21+
// Explanation: You can't remove any characters, so the string stays as is.
22+
//
23+
// Example 2:
24+
//
25+
// Input: s = "cabaabac"
26+
// Output: 0
27+
// Explanation: An optimal sequence of operations is:
28+
// - Take prefix = "c" and suffix = "c" and remove them, s = "abaaba".
29+
// - Take prefix = "a" and suffix = "a" and remove them, s = "baab".
30+
// - Take prefix = "b" and suffix = "b" and remove them, s = "aa".
31+
// - Take prefix = "a" and suffix = "a" and remove them, s = "".
32+
// Example 3:
33+
//
34+
// Input: s = "aabccabba"
35+
// Output: 3
36+
// Explanation: An optimal sequence of operations is:
37+
// - Take prefix = "aa" and suffix = "a" and remove them, s = "bccabb".
38+
// - Take prefix = "b" and suffix = "bb" and remove them, s = "cca".
39+
//
40+
//  
41+
// Constraints:
42+
//
43+
// 1 <= s.length <= 105
44+
// s only consists of characters 'a', 'b', and 'c'.
45+
//
46+
//
47+
48+
class Solution {
49+
public:
50+
int minimumLength(string s) {
51+
int l = 0;
52+
int r = s.size() - 1;
53+
while (l < r && s[l] == s[r]) {
54+
while (l < r && s[l] == s[l + 1]) {
55+
l += 1;
56+
}
57+
58+
while (l < r && s[r] == s[r - 1]) {
59+
r -= 1;
60+
}
61+
l += 1;
62+
r -= 1;
63+
}
64+
return max(0, r - l + 1);
65+
}
66+
};
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# Tag: Two Pointers, String
2+
# Time: O(N)
3+
# Space: O(1)
4+
# Ref: -
5+
# Note: -
6+
7+
# Given a string s consisting only of characters 'a', 'b', and 'c'. You are asked to apply the following algorithm on the string any number of times:
8+
#
9+
# Pick a non-empty prefix from the string s where all the characters in the prefix are equal.
10+
# Pick a non-empty suffix from the string s where all the characters in this suffix are equal.
11+
# The prefix and the suffix should not intersect at any index.
12+
# The characters from the prefix and suffix must be the same.
13+
# Delete both the prefix and the suffix.
14+
#
15+
# Return the minimum length of s after performing the above operation any number of times (possibly zero times).
16+
#  
17+
# Example 1:
18+
#
19+
# Input: s = "ca"
20+
# Output: 2
21+
# Explanation: You can't remove any characters, so the string stays as is.
22+
#
23+
# Example 2:
24+
#
25+
# Input: s = "cabaabac"
26+
# Output: 0
27+
# Explanation: An optimal sequence of operations is:
28+
# - Take prefix = "c" and suffix = "c" and remove them, s = "abaaba".
29+
# - Take prefix = "a" and suffix = "a" and remove them, s = "baab".
30+
# - Take prefix = "b" and suffix = "b" and remove them, s = "aa".
31+
# - Take prefix = "a" and suffix = "a" and remove them, s = "".
32+
# Example 3:
33+
#
34+
# Input: s = "aabccabba"
35+
# Output: 3
36+
# Explanation: An optimal sequence of operations is:
37+
# - Take prefix = "aa" and suffix = "a" and remove them, s = "bccabb".
38+
# - Take prefix = "b" and suffix = "bb" and remove them, s = "cca".
39+
#
40+
#  
41+
# Constraints:
42+
#
43+
# 1 <= s.length <= 105
44+
# s only consists of characters 'a', 'b', and 'c'.
45+
#
46+
#
47+
48+
class Solution:
49+
def minimumLength(self, s: str) -> int:
50+
l = 0
51+
r = len(s) - 1
52+
while l < r and s[l] == s[r]:
53+
54+
while l < r and s[l] == s[l + 1]:
55+
l += 1
56+
while l < r and s[r] == s[r - 1]:
57+
r -= 1
58+
59+
l += 1
60+
r -= 1
61+
62+
return max(0, r - l + 1)
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Tag: Array, Hash Table, Prefix Sum
2+
// Time: O(N)
3+
// Space: O(N)
4+
// Ref: -
5+
// Note: -
6+
// Video: https://youtu.be/hvHvsnEvbuw
7+
8+
// You are given a 0-indexed integer array nums, an integer modulo, and an integer k.
9+
// Your task is to find the count of subarrays that are interesting.
10+
// A subarray nums[l..r] is interesting if the following condition holds:
11+
//
12+
// Let cnt be the number of indices i in the range [l, r] such that nums[i] % modulo == k. Then, cnt % modulo == k.
13+
//
14+
// Return an integer denoting the count of interesting subarrays.
15+
// Note: A subarray is a contiguous non-empty sequence of elements within an array.
16+
//  
17+
// Example 1:
18+
//
19+
// Input: nums = [3,2,4], modulo = 2, k = 1
20+
// Output: 3
21+
// Explanation: In this example the interesting subarrays are:
22+
// The subarray nums[0..0] which is [3].
23+
// - There is only one index, i = 0, in the range [0, 0] that satisfies nums[i] % modulo == k.
24+
// - Hence, cnt = 1 and cnt % modulo == k.
25+
// The subarray nums[0..1] which is [3,2].
26+
// - There is only one index, i = 0, in the range [0, 1] that satisfies nums[i] % modulo == k.
27+
// - Hence, cnt = 1 and cnt % modulo == k.
28+
// The subarray nums[0..2] which is [3,2,4].
29+
// - There is only one index, i = 0, in the range [0, 2] that satisfies nums[i] % modulo == k.
30+
// - Hence, cnt = 1 and cnt % modulo == k.
31+
// It can be shown that there are no other interesting subarrays. So, the answer is 3.
32+
// Example 2:
33+
//
34+
// Input: nums = [3,1,9,6], modulo = 3, k = 0
35+
// Output: 2
36+
// Explanation: In this example the interesting subarrays are:
37+
// The subarray nums[0..3] which is [3,1,9,6].
38+
// - There are three indices, i = 0, 2, 3, in the range [0, 3] that satisfy nums[i] % modulo == k.
39+
// - Hence, cnt = 3 and cnt % modulo == k.
40+
// The subarray nums[1..1] which is [1].
41+
// - There is no index, i, in the range [1, 1] that satisfies nums[i] % modulo == k.
42+
// - Hence, cnt = 0 and cnt % modulo == k.
43+
// It can be shown that there are no other interesting subarrays. So, the answer is 2.
44+
//  
45+
// Constraints:
46+
//
47+
// 1 <= nums.length <= 105
48+
// 1 <= nums[i] <= 109
49+
// 1 <= modulo <= 109
50+
// 0 <= k < modulo
51+
//
52+
//
53+
54+
class Solution {
55+
public:
56+
long long countInterestingSubarrays(vector<int>& nums, int modulo, int k) {
57+
unordered_map<int, int> counter;
58+
counter[0] = 1;
59+
int prefix = 0;
60+
long long res = 0;
61+
for (auto &x: nums) {
62+
prefix = (prefix + (x % modulo == k ? 1 : 0)) % modulo;
63+
int find = (prefix - k + modulo) % modulo;
64+
res += counter[find];
65+
counter[prefix] += 1;
66+
}
67+
return res;
68+
}
69+
};
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Tag: Array, Hash Table, Prefix Sum
2+
# Time: O(N)
3+
# Space: O(N)
4+
# Ref: -
5+
# Note: -
6+
# Video: https://youtu.be/hvHvsnEvbuw
7+
8+
# You are given a 0-indexed integer array nums, an integer modulo, and an integer k.
9+
# Your task is to find the count of subarrays that are interesting.
10+
# A subarray nums[l..r] is interesting if the following condition holds:
11+
#
12+
# Let cnt be the number of indices i in the range [l, r] such that nums[i] % modulo == k. Then, cnt % modulo == k.
13+
#
14+
# Return an integer denoting the count of interesting subarrays.
15+
# Note: A subarray is a contiguous non-empty sequence of elements within an array.
16+
#  
17+
# Example 1:
18+
#
19+
# Input: nums = [3,2,4], modulo = 2, k = 1
20+
# Output: 3
21+
# Explanation: In this example the interesting subarrays are:
22+
# The subarray nums[0..0] which is [3].
23+
# - There is only one index, i = 0, in the range [0, 0] that satisfies nums[i] % modulo == k.
24+
# - Hence, cnt = 1 and cnt % modulo == k.
25+
# The subarray nums[0..1] which is [3,2].
26+
# - There is only one index, i = 0, in the range [0, 1] that satisfies nums[i] % modulo == k.
27+
# - Hence, cnt = 1 and cnt % modulo == k.
28+
# The subarray nums[0..2] which is [3,2,4].
29+
# - There is only one index, i = 0, in the range [0, 2] that satisfies nums[i] % modulo == k.
30+
# - Hence, cnt = 1 and cnt % modulo == k.
31+
# It can be shown that there are no other interesting subarrays. So, the answer is 3.
32+
# Example 2:
33+
#
34+
# Input: nums = [3,1,9,6], modulo = 3, k = 0
35+
# Output: 2
36+
# Explanation: In this example the interesting subarrays are:
37+
# The subarray nums[0..3] which is [3,1,9,6].
38+
# - There are three indices, i = 0, 2, 3, in the range [0, 3] that satisfy nums[i] % modulo == k.
39+
# - Hence, cnt = 3 and cnt % modulo == k.
40+
# The subarray nums[1..1] which is [1].
41+
# - There is no index, i, in the range [1, 1] that satisfies nums[i] % modulo == k.
42+
# - Hence, cnt = 0 and cnt % modulo == k.
43+
# It can be shown that there are no other interesting subarrays. So, the answer is 2.
44+
#  
45+
# Constraints:
46+
#
47+
# 1 <= nums.length <= 105
48+
# 1 <= nums[i] <= 109
49+
# 1 <= modulo <= 109
50+
# 0 <= k < modulo
51+
#
52+
#
53+
54+
from collections import defaultdict
55+
class Solution:
56+
def countInterestingSubarrays(self, nums: List[int], modulo: int, k: int) -> int:
57+
nums = [1 if x % modulo == k else 0 for x in nums]
58+
59+
counter = defaultdict(int)
60+
counter[0] = 1
61+
prefix = 0
62+
res = 0
63+
for x in nums:
64+
prefix = (prefix + x) % modulo
65+
find = (prefix - k + modulo) % modulo
66+
res += counter[find]
67+
counter[prefix] += 1
68+
69+
return res

‎list/endlesscheng.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@
114114

115115
96. https://leetcode.com/problems/reverse-string/
116116
97. https://leetcode.com/problems/valid-palindrome/
117-
- https://leetcode.com/problems/minimum-length-of-string-after-deleting-similar-ends/
117+
98. https://leetcode.com/problems/minimum-length-of-string-after-deleting-similar-ends/
118118
- https://leetcode.com/problems/watering-plants-ii/
119119
- https://leetcode.com/problems/squares-of-a-sorted-array/
120120
- https://leetcode.com/problems/find-k-closest-elements/

0 commit comments

Comments
 (0)
Please sign in to comment.