Skip to content

Commit 595e34b

Browse files
committed
May-17
1 parent 4ea016f commit 595e34b

10 files changed

+322
-32
lines changed

README.md

Lines changed: 10 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) | 16/128 | 3 vips |
58-
| [🔲] | [endlesscheng.md](./list/endlesscheng.md) | 117/2210 | 5 vips |
58+
| [🔲] | [endlesscheng.md](./list/endlesscheng.md) | 118/2210 | 5 vips |
5959

60-
**Solved**: 708 problems
60+
**Solved**: 710 problems
6161

6262
## 类型/Category
6363

@@ -252,11 +252,12 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
252252

253253
## Enumeration
254254

255-
| Link | Problem(10) | Solution | Tag | Time | Space | Ref |
255+
| Link | Problem(11) | Solution | Tag | Time | Space | Ref |
256256
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
257257
| [Leetcode-1534](https://leetcode.com/problems/count-good-triplets/) | Count Good Triplets | [c++](./leetcode/1534.count-good-triplets.cpp), [python3](./leetcode/1534.count-good-triplets.py) | Enumeration | O\(N^3\) | O\(1\) | - |
258258
| [Leetcode-204](https://leetcode.com/problems/count-primes/) | Count Primes | [c++](./leetcode/204.count-primes.cpp), [python3](./leetcode/204.count-primes.py) | Enumeration | O\(NlogN\) | O\(N\) | - |
259259
| [Leetcode-2843](https://leetcode.com/problems/count-symmetric-integers/) | Count Symmetric Integers | [c++](./leetcode/2843.count-symmetric-integers.cpp), [python3](./leetcode/2843.count-symmetric-integers.py) | Enumeration | O\(N\) | O\(1\) | - |
260+
| [Leetcode-2970](https://leetcode.com/problems/count-the-number-of-incremovable-subarrays-i/) | Count The Number Of Incremovable Subarrays I | [c++](./leetcode/2970.count-the-number-of-incremovable-subarrays-i.cpp), [python3](./leetcode/2970.count-the-number-of-incremovable-subarrays-i.py) | Enumeration | O\(N\) | O\(1\) | - |
260261
| [Leetcode-3272](https://leetcode.com/problems/find-the-count-of-good-integers/) | Find The Count Of Good Integers | [c++](./leetcode/3272.find-the-count-of-good-integers.cpp), [python3](./leetcode/3272.find-the-count-of-good-integers.py) | Enumeration | O\(10\*\*\(N/2\) \* NlogN\) | O\(10\*\*\(N/2\)\) | - |
261262
| [Leetcode-2094](https://leetcode.com/problems/finding-3-digit-even-numbers/) | Finding 3 Digit Even Numbers | [c++](./leetcode/2094.finding-3-digit-even-numbers.cpp), [python3](./leetcode/2094.finding-3-digit-even-numbers.py) | Enumeration | O\(1\) | O\(1\) | - |
262263
| [Leetcode-3411](https://leetcode.com/problems/maximum-subarray-with-equal-products/) | Maximum Subarray With Equal Products | [c++](./leetcode/3411.maximum-subarray-with-equal-products.cpp), [python3](./leetcode/3411.maximum-subarray-with-equal-products.py) | Enumeration | O\(N \* sqrt\(Num\)\) | O\(N\) | - |
@@ -466,7 +467,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
466467

467468
## Binary Search
468469

469-
| Link | Problem(84) | Solution | Tag | Time | Space | Ref |
470+
| Link | Problem(86) | Solution | Tag | Time | Space | Ref |
470471
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
471472
| [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) | Binary Search | O\(NlogN\) | O\(1\) | - |
472473
| [Leetcode-704](https://leetcode.com/problems/binary-search/) | Binary Search | [c++](./leetcode/704.binary-search.cpp), [python3](./leetcode/704.binary-search.py) | Binary Search | O\(logN\) | O\(1\) | - |
@@ -476,6 +477,8 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
476477
| [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) | Binary Search | O\(N\) | O\(1\) | - |
477478
| [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) | Binary Search | O\(N \+ Q\) | O\(N\) | - |
478479
| [Leetcode-2563](https://leetcode.com/problems/count-the-number-of-fair-pairs/) | Count The Number Of Fair Pairs | [c++](./leetcode/2563.count-the-number-of-fair-pairs.cpp), [python3](./leetcode/2563.count-the-number-of-fair-pairs.py) | Binary Search | O\(NlogN\) | O\(1\) | - |
480+
| [Leetcode-2970](https://leetcode.com/problems/count-the-number-of-incremovable-subarrays-i/) | Count The Number Of Incremovable Subarrays I | [c++](./leetcode/2970.count-the-number-of-incremovable-subarrays-i.cpp), [python3](./leetcode/2970.count-the-number-of-incremovable-subarrays-i.py) | Binary Search | O\(N\) | O\(1\) | - |
481+
| [Leetcode-2972](https://leetcode.com/problems/count-the-number-of-incremovable-subarrays-ii/) | Count The Number Of Incremovable Subarrays II | [c++](./leetcode/2972.count-the-number-of-incremovable-subarrays-ii.cpp), [python3](./leetcode/2972.count-the-number-of-incremovable-subarrays-ii.py) | Binary Search | O\(N\) | O\(1\) | - |
479482
| [Leetcode-3048](https://leetcode.com/problems/earliest-second-to-mark-indices-i/) | Earliest Second To Mark Indices I | [c++](./leetcode/3048.earliest-second-to-mark-indices-i.cpp), [python3](./leetcode/3048.earliest-second-to-mark-indices-i.py) | Binary Search | O\(NlogN\) | O\(N\) | - |
480483
| [Leetcode-1901](https://leetcode.com/problems/find-a-peak-element-ii/) | Find A Peak Element II | [c++](./leetcode/1901.find-a-peak-element-ii.cpp), [python3](./leetcode/1901.find-a-peak-element-ii.py) | Binary Search | O\(N \* logM\) | O\(1\) | - |
481484
| [Leetcode-34](https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/) | Find First And Last Position Of Element In Sorted Array | [c++](./leetcode/34.find-first-and-last-position-of-element-in-sorted-array.cpp), [python3](./leetcode/34.find-first-and-last-position-of-element-in-sorted-array.py) | Binary Search | O\(logN\) | O\(1\) | - |
@@ -882,7 +885,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
882885

883886
## Two Pointers
884887

885-
| Link | Problem(63) | Solution | Tag | Time | Space | Ref |
888+
| Link | Problem(65) | Solution | Tag | Time | Space | Ref |
886889
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
887890
| [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\) | - |
888891
| [Leetcode-16](https://leetcode.com/problems/3sum-closest/) | 3Sum Closest | [c++](./leetcode/16.3sum-closest.cpp), [python3](./leetcode/16.3sum-closest.py) | Two Pointers | O\(N^2\) | O\(1\) | - |
@@ -897,6 +900,8 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
897900
| [Leetcode-1782](https://leetcode.com/problems/count-pairs-of-nodes/) | Count Pairs Of Nodes | [c++](./leetcode/1782.count-pairs-of-nodes.cpp), [python3](./leetcode/1782.count-pairs-of-nodes.py) | Two Pointers | O\(VlogV \+ E\) | O\(V \+ E\) | - |
898901
| [Leetcode-2824](https://leetcode.com/problems/count-pairs-whose-sum-is-less-than-target/) | Count Pairs Whose Sum Is Less Than Target | [c++](./leetcode/2824.count-pairs-whose-sum-is-less-than-target.cpp), [python3](./leetcode/2824.count-pairs-whose-sum-is-less-than-target.py) | Two Pointers | O\(NlogN\) | O\(1\) | - |
899902
| [Leetcode-2563](https://leetcode.com/problems/count-the-number-of-fair-pairs/) | Count The Number Of Fair Pairs | [c++](./leetcode/2563.count-the-number-of-fair-pairs.cpp), [python3](./leetcode/2563.count-the-number-of-fair-pairs.py) | Two Pointers | O\(NlogN\) | O\(1\) | - |
903+
| [Leetcode-2970](https://leetcode.com/problems/count-the-number-of-incremovable-subarrays-i/) | Count The Number Of Incremovable Subarrays I | [c++](./leetcode/2970.count-the-number-of-incremovable-subarrays-i.cpp), [python3](./leetcode/2970.count-the-number-of-incremovable-subarrays-i.py) | Two Pointers | O\(N\) | O\(1\) | - |
904+
| [Leetcode-2972](https://leetcode.com/problems/count-the-number-of-incremovable-subarrays-ii/) | Count The Number Of Incremovable Subarrays II | [c++](./leetcode/2972.count-the-number-of-incremovable-subarrays-ii.cpp), [python3](./leetcode/2972.count-the-number-of-incremovable-subarrays-ii.py) | Two Pointers | O\(N\) | O\(1\) | - |
900905
| [Leetcode-295](https://leetcode.com/problems/find-median-from-data-stream/) | Find Median From Data Stream | [c++](./leetcode/295.find-median-from-data-stream.cpp), [python3](./leetcode/295.find-median-from-data-stream.py) | Two Pointers | O\(logN\) | O\(N\) | - |
901906
| [Leetcode-287](https://leetcode.com/problems/find-the-duplicate-number/) | Find The Duplicate Number | [c++](./leetcode/287.find-the-duplicate-number.cpp), [python3](./leetcode/287.find-the-duplicate-number.py) | Two Pointers | O\(N\) | O\(1\) | - |
902907
| [Leetcode-28](https://leetcode.com/problems/find-the-index-of-the-first-occurrence-in-a-string/) | Find The Index Of The First Occurrence In A String | [c++](./leetcode/28.find-the-index-of-the-first-occurrence-in-a-string.cpp), [python3](./leetcode/28.find-the-index-of-the-first-occurrence-in-a-string.py) | Two Pointers | O\(M \+ N\) | O\(N\) | - |

leetcode/2901.longest-unequal-adjacent-groups-subsequence-ii.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// Space: O(N)
44
// Ref: -
55
// Note: -
6+
// Video: https://youtu.be/Mu70Pw_6Ey8
67

78
// You are given a string array words, and an array groups, both arrays having length n.
89
// The hamming distance between two strings of equal length is the number of positions at which the corresponding characters are different.

leetcode/2901.longest-unequal-adjacent-groups-subsequence-ii.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# Space: O(N)
44
# Ref: -
55
# Note: -
6+
# Video: https://youtu.be/Mu70Pw_6Ey8
67

78
# You are given a string array words, and an array groups, both arrays having length n.
89
# The hamming distance between two strings of equal length is the number of positions at which the corresponding characters are different.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Tag: Array, Two Pointers, Binary Search, Enumeration
2+
// Time: O(N)
3+
// Space: O(1)
4+
// Ref: -
5+
// Note: -
6+
7+
// You are given a 0-indexed array of positive integers nums.
8+
// A subarray of nums is called incremovable if nums becomes strictly increasing on removing the subarray. For example, the subarray [3, 4] is an incremovable subarray of [5, 3, 4, 6, 7] because removing this subarray changes the array [5, 3, 4, 6, 7] to [5, 6, 7] which is strictly increasing.
9+
// Return the total number of incremovable subarrays of nums.
10+
// Note that an empty array is considered strictly increasing.
11+
// A subarray is a contiguous non-empty sequence of elements within an array.
12+
//  
13+
// Example 1:
14+
//
15+
// Input: nums = [1,2,3,4]
16+
// Output: 10
17+
// Explanation: The 10 incremovable subarrays are: [1], [2], [3], [4], [1,2], [2,3], [3,4], [1,2,3], [2,3,4], and [1,2,3,4], because on removing any one of these subarrays nums becomes strictly increasing. Note that you cannot select an empty subarray.
18+
//
19+
// Example 2:
20+
//
21+
// Input: nums = [6,5,7,8]
22+
// Output: 7
23+
// Explanation: The 7 incremovable subarrays are: [5], [6], [5,7], [6,5], [5,7,8], [6,5,7] and [6,5,7,8].
24+
// It can be shown that there are only 7 incremovable subarrays in nums.
25+
//
26+
// Example 3:
27+
//
28+
// Input: nums = [8,7,6,6]
29+
// Output: 3
30+
// Explanation: The 3 incremovable subarrays are: [8,7,6], [7,6,6], and [8,7,6,6]. Note that [8,7] is not an incremovable subarray because after removing [8,7] nums becomes [6,6], which is sorted in ascending order but not strictly increasing.
31+
//
32+
//  
33+
// Constraints:
34+
//
35+
// 1 <= nums.length <= 50
36+
// 1 <= nums[i] <= 50
37+
//
38+
//
39+
40+
class Solution {
41+
public:
42+
int incremovableSubarrayCount(vector<int>& nums) {
43+
int n = nums.size();
44+
int l = 0;
45+
int r = n - 1;
46+
while (l + 1 < n && nums[l] < nums[l + 1]) {
47+
l += 1;
48+
}
49+
while (r > 0 && nums[r - 1] < nums[r]) {
50+
r -= 1;
51+
}
52+
53+
if (l >= r) {
54+
return (n + 1) * n / 2;
55+
}
56+
57+
int res= l + 1 + n - r + 1;
58+
int i = 0;
59+
int j = r;
60+
while (i <= l && j <= n - 1) {
61+
if (nums[i] < nums[j]) {
62+
res += n - j;
63+
i += 1;
64+
} else {
65+
j += 1;
66+
}
67+
}
68+
69+
return res;
70+
}
71+
};
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Tag: Array, Two Pointers, Binary Search, Enumeration
2+
# Time: O(N)
3+
# Space: O(1)
4+
# Ref: -
5+
# Note: -
6+
7+
# You are given a 0-indexed array of positive integers nums.
8+
# A subarray of nums is called incremovable if nums becomes strictly increasing on removing the subarray. For example, the subarray [3, 4] is an incremovable subarray of [5, 3, 4, 6, 7] because removing this subarray changes the array [5, 3, 4, 6, 7] to [5, 6, 7] which is strictly increasing.
9+
# Return the total number of incremovable subarrays of nums.
10+
# Note that an empty array is considered strictly increasing.
11+
# A subarray is a contiguous non-empty sequence of elements within an array.
12+
#  
13+
# Example 1:
14+
#
15+
# Input: nums = [1,2,3,4]
16+
# Output: 10
17+
# Explanation: The 10 incremovable subarrays are: [1], [2], [3], [4], [1,2], [2,3], [3,4], [1,2,3], [2,3,4], and [1,2,3,4], because on removing any one of these subarrays nums becomes strictly increasing. Note that you cannot select an empty subarray.
18+
#
19+
# Example 2:
20+
#
21+
# Input: nums = [6,5,7,8]
22+
# Output: 7
23+
# Explanation: The 7 incremovable subarrays are: [5], [6], [5,7], [6,5], [5,7,8], [6,5,7] and [6,5,7,8].
24+
# It can be shown that there are only 7 incremovable subarrays in nums.
25+
#
26+
# Example 3:
27+
#
28+
# Input: nums = [8,7,6,6]
29+
# Output: 3
30+
# Explanation: The 3 incremovable subarrays are: [8,7,6], [7,6,6], and [8,7,6,6]. Note that [8,7] is not an incremovable subarray because after removing [8,7] nums becomes [6,6], which is sorted in ascending order but not strictly increasing.
31+
#
32+
#  
33+
# Constraints:
34+
#
35+
# 1 <= nums.length <= 50
36+
# 1 <= nums[i] <= 50
37+
#
38+
#
39+
40+
class Solution:
41+
def incremovableSubarrayCount(self, nums: List[int]) -> int:
42+
n = len(nums)
43+
res = 0
44+
45+
l = 0
46+
while l + 1 < n and nums[l] < nums[l + 1]:
47+
l += 1
48+
49+
r = n - 1
50+
while r > 0 and nums[r - 1] < nums[r]:
51+
r -= 1
52+
53+
if l >= r:
54+
return n * (n + 1) // 2
55+
56+
res = l + 1 + n - r + 1
57+
58+
i = 0
59+
j = r
60+
while i <= l and j <= n - 1:
61+
if nums[i] < nums[j]:
62+
res += n - j
63+
i += 1
64+
else:
65+
j += 1
66+
67+
return res
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Tag: Array, Two Pointers, Binary Search
2+
// Time: O(N)
3+
// Space: O(1)
4+
// Ref: -
5+
// Note: -
6+
7+
// You are given a 0-indexed array of positive integers nums.
8+
// A subarray of nums is called incremovable if nums becomes strictly increasing on removing the subarray. For example, the subarray [3, 4] is an incremovable subarray of [5, 3, 4, 6, 7] because removing this subarray changes the array [5, 3, 4, 6, 7] to [5, 6, 7] which is strictly increasing.
9+
// Return the total number of incremovable subarrays of nums.
10+
// Note that an empty array is considered strictly increasing.
11+
// A subarray is a contiguous non-empty sequence of elements within an array.
12+
//  
13+
// Example 1:
14+
//
15+
// Input: nums = [1,2,3,4]
16+
// Output: 10
17+
// Explanation: The 10 incremovable subarrays are: [1], [2], [3], [4], [1,2], [2,3], [3,4], [1,2,3], [2,3,4], and [1,2,3,4], because on removing any one of these subarrays nums becomes strictly increasing. Note that you cannot select an empty subarray.
18+
//
19+
// Example 2:
20+
//
21+
// Input: nums = [6,5,7,8]
22+
// Output: 7
23+
// Explanation: The 7 incremovable subarrays are: [5], [6], [5,7], [6,5], [5,7,8], [6,5,7] and [6,5,7,8].
24+
// It can be shown that there are only 7 incremovable subarrays in nums.
25+
//
26+
// Example 3:
27+
//
28+
// Input: nums = [8,7,6,6]
29+
// Output: 3
30+
// Explanation: The 3 incremovable subarrays are: [8,7,6], [7,6,6], and [8,7,6,6]. Note that [8,7] is not an incremovable subarray because after removing [8,7] nums becomes [6,6], which is sorted in ascending order but not strictly increasing.
31+
//
32+
//  
33+
// Constraints:
34+
//
35+
// 1 <= nums.length <= 105
36+
// 1 <= nums[i] <= 109
37+
//
38+
//
39+
40+
class Solution {
41+
public:
42+
long long incremovableSubarrayCount(vector<int>& nums) {
43+
int n = nums.size();
44+
int l = 0;
45+
int r = n - 1;
46+
while (l + 1 < n && nums[l] < nums[l + 1]) {
47+
l += 1;
48+
}
49+
while (r > 0 && nums[r - 1] < nums[r]) {
50+
r -= 1;
51+
}
52+
53+
if (l >= r) {
54+
return (n + 1LL) * n / 2;
55+
}
56+
57+
long long res= l + 1 + n - r + 1;
58+
int i = 0;
59+
int j = r;
60+
while (i <= l && j <= n - 1) {
61+
if (nums[i] < nums[j]) {
62+
res += n - j;
63+
i += 1;
64+
} else {
65+
j += 1;
66+
}
67+
}
68+
69+
return res;
70+
}
71+
};
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# Tag: Array, Two Pointers, Binary Search
2+
# Time: O(N)
3+
# Space: O(1)
4+
# Ref: -
5+
# Note: -
6+
7+
# You are given a 0-indexed array of positive integers nums.
8+
# A subarray of nums is called incremovable if nums becomes strictly increasing on removing the subarray. For example, the subarray [3, 4] is an incremovable subarray of [5, 3, 4, 6, 7] because removing this subarray changes the array [5, 3, 4, 6, 7] to [5, 6, 7] which is strictly increasing.
9+
# Return the total number of incremovable subarrays of nums.
10+
# Note that an empty array is considered strictly increasing.
11+
# A subarray is a contiguous non-empty sequence of elements within an array.
12+
#  
13+
# Example 1:
14+
#
15+
# Input: nums = [1,2,3,4]
16+
# Output: 10
17+
# Explanation: The 10 incremovable subarrays are: [1], [2], [3], [4], [1,2], [2,3], [3,4], [1,2,3], [2,3,4], and [1,2,3,4], because on removing any one of these subarrays nums becomes strictly increasing. Note that you cannot select an empty subarray.
18+
#
19+
# Example 2:
20+
#
21+
# Input: nums = [6,5,7,8]
22+
# Output: 7
23+
# Explanation: The 7 incremovable subarrays are: [5], [6], [5,7], [6,5], [5,7,8], [6,5,7] and [6,5,7,8].
24+
# It can be shown that there are only 7 incremovable subarrays in nums.
25+
#
26+
# Example 3:
27+
#
28+
# Input: nums = [8,7,6,6]
29+
# Output: 3
30+
# Explanation: The 3 incremovable subarrays are: [8,7,6], [7,6,6], and [8,7,6,6]. Note that [8,7] is not an incremovable subarray because after removing [8,7] nums becomes [6,6], which is sorted in ascending order but not strictly increasing.
31+
#
32+
#  
33+
# Constraints:
34+
#
35+
# 1 <= nums.length <= 105
36+
# 1 <= nums[i] <= 109
37+
#
38+
#
39+
40+
class Solution:
41+
def incremovableSubarrayCount(self, nums: List[int]) -> int:
42+
n = len(nums)
43+
res = 0
44+
45+
l = 0
46+
while l + 1 < n and nums[l] < nums[l + 1]:
47+
l += 1
48+
49+
r = n - 1
50+
while r > 0 and nums[r - 1] < nums[r]:
51+
r -= 1
52+
53+
if l >= r:
54+
return n * (n + 1) // 2
55+
56+
res = l + 1 + n - r + 1
57+
58+
i = 0
59+
j = r
60+
while i <= l and j <= n - 1:
61+
if nums[i] < nums[j]:
62+
res += n - j
63+
i += 1
64+
else:
65+
j += 1
66+
67+
return res

0 commit comments

Comments
 (0)