Skip to content

Commit 59533ed

Browse files
committed
Apr-26
1 parent 2a1e3e7 commit 59533ed

File tree

3 files changed

+122
-3
lines changed

3 files changed

+122
-3
lines changed

README.md

+5-3
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
5757
| [🔲] | [9c-basic.md](./list/9c-basic.md) | 15/128 | 3 vips |
5858
| [🔲] | [endlesscheng.md](./list/endlesscheng.md) | 93/2210 | 4 vips |
5959

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

6262
## 类型/Category
6363

@@ -793,9 +793,10 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
793793

794794
## Monotonic Queue
795795

796-
| Link | Problem(3) | Solution | Tag | Time | Space | Ref |
796+
| Link | Problem(4) | Solution | Tag | Time | Space | Ref |
797797
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
798798
| [Leetcode-2762](https://leetcode.com/problems/continuous-subarrays/) | Continuous Subarrays | [c++](./leetcode/2762.continuous-subarrays.cpp), [python3](./leetcode/2762.continuous-subarrays.py) | Monotonic Queue | O\(N\) | O\(N\) | - |
799+
| [Leetcode-2444](https://leetcode.com/problems/count-subarrays-with-fixed-bounds/) | Count Subarrays With Fixed Bounds | [c++](./leetcode/2444.count-subarrays-with-fixed-bounds.cpp), [python3](./leetcode/2444.count-subarrays-with-fixed-bounds.py) | Monotonic Queue | O\(N\) | O\(1\) | - |
799800
| [Leetcode-1438](https://leetcode.com/problems/longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit/) | Longest Continuous Subarray With Absolute Diff Less Than Or Equal To Limit | [c++](./leetcode/1438.longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit.cpp), [python3](./leetcode/1438.longest-continuous-subarray-with-absolute-diff-less-than-or-equal-to-limit.py) | Monotonic Queue | O\(N\) | O\(N\) | - |
800801
| [Leetcode-239](https://leetcode.com/problems/sliding-window-maximum/) | Sliding Window Maximum | [c++](./leetcode/239.sliding-window-maximum.cpp), [python3](./leetcode/239.sliding-window-maximum.py) | Monotonic Queue | O\(N\) | O\(K\) | - |
801802

@@ -896,7 +897,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
896897

897898
## Sliding Window
898899

899-
| Link | Problem(88) | Solution | Tag | Time | Space | Ref |
900+
| Link | Problem(89) | Solution | Tag | Time | Space | Ref |
900901
| ----- | ----- | ----- | ----- | ----- | ----- | ----- |
901902
| [Leetcode-3206](https://leetcode.com/problems/alternating-groups-i/) | Alternating Groups I | [c++](./leetcode/3206.alternating-groups-i.cpp), [python3](./leetcode/3206.alternating-groups-i.py) | Sliding Window | O\(N\) | O\(1\) | - |
902903
| [Leetcode-3208](https://leetcode.com/problems/alternating-groups-ii/) | Alternating Groups II | [c++](./leetcode/3208.alternating-groups-ii.cpp), [python3](./leetcode/3208.alternating-groups-ii.py) | Sliding Window | O\(N\) | O\(1\) | - |
@@ -909,6 +910,7 @@ python problem.py https://www.lintcode.com/problem/92 -l cpp
909910
| [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) | Sliding Window | O\(N\) | O\(1\) | - |
910911
| [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) | Sliding Window | O\(N\) | O\(N\) | - |
911912
| [Leetcode-2962](https://leetcode.com/problems/count-subarrays-where-max-element-appears-at-least-k-times/) | Count Subarrays Where Max Element Appears At Least K Times | [c++](./leetcode/2962.count-subarrays-where-max-element-appears-at-least-k-times.cpp), [python3](./leetcode/2962.count-subarrays-where-max-element-appears-at-least-k-times.py) | Sliding Window | O\(N\) | O\(1\) | - |
913+
| [Leetcode-2444](https://leetcode.com/problems/count-subarrays-with-fixed-bounds/) | Count Subarrays With Fixed Bounds | [c++](./leetcode/2444.count-subarrays-with-fixed-bounds.cpp), [python3](./leetcode/2444.count-subarrays-with-fixed-bounds.py) | Sliding Window | O\(N\) | O\(1\) | - |
912914
| [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) | Sliding Window | O\(N\) | O\(1\) | - |
913915
| [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) | Sliding Window | O\(N\) | O\(M\) | - |
914916
| [Leetcode-3258](https://leetcode.com/problems/count-substrings-that-satisfy-k-constraint-i/) | Count Substrings That Satisfy K Constraint I | [c++](./leetcode/3258.count-substrings-that-satisfy-k-constraint-i.cpp), [python3](./leetcode/3258.count-substrings-that-satisfy-k-constraint-i.py) | Sliding Window | O\(N\) | O\(1\) | - |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Tag: Array, Queue, Sliding Window, Monotonic Queue
2+
// Time: O(N)
3+
// Space: O(1)
4+
// Ref: -
5+
// Note: -
6+
// Video: https://youtu.be/N1kAIbVPb3Q
7+
8+
// You are given an integer array nums and two integers minK and maxK.
9+
// A fixed-bound subarray of nums is a subarray that satisfies the following conditions:
10+
//
11+
// The minimum value in the subarray is equal to minK.
12+
// The maximum value in the subarray is equal to maxK.
13+
//
14+
// Return the number of fixed-bound subarrays.
15+
// A subarray is a contiguous part of an array.
16+
//  
17+
// Example 1:
18+
//
19+
// Input: nums = [1,3,5,2,7,5], minK = 1, maxK = 5
20+
// Output: 2
21+
// Explanation: The fixed-bound subarrays are [1,3,5] and [1,3,5,2].
22+
//
23+
// Example 2:
24+
//
25+
// Input: nums = [1,1,1,1], minK = 1, maxK = 1
26+
// Output: 10
27+
// Explanation: Every subarray of nums is a fixed-bound subarray. There are 10 possible subarrays.
28+
//
29+
//  
30+
// Constraints:
31+
//
32+
// 2 <= nums.length <= 105
33+
// 1 <= nums[i], minK, maxK <= 106
34+
//
35+
//
36+
37+
class Solution {
38+
public:
39+
long long countSubarrays(vector<int>& nums, int minK, int maxK) {
40+
int n = nums.size();
41+
long long res = 0;
42+
int i = 0;
43+
int minj = -1;
44+
int maxj = -1;
45+
for (int j = 0; j < n; j++) {
46+
if (nums[j] < minK || nums[j] > maxK) {
47+
i = j + 1;
48+
}
49+
50+
if (nums[j] == minK) {
51+
minj = j;
52+
}
53+
54+
if (nums[j] == maxK) {
55+
maxj = j;
56+
}
57+
res += max(0, min(minj, maxj) - i + 1);
58+
}
59+
return res;
60+
}
61+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Tag: Array, Queue, Sliding Window, Monotonic Queue
2+
# Time: O(N)
3+
# Space: O(1)
4+
# Ref: -
5+
# Note: -
6+
# Video: https://youtu.be/N1kAIbVPb3Q
7+
8+
# You are given an integer array nums and two integers minK and maxK.
9+
# A fixed-bound subarray of nums is a subarray that satisfies the following conditions:
10+
#
11+
# The minimum value in the subarray is equal to minK.
12+
# The maximum value in the subarray is equal to maxK.
13+
#
14+
# Return the number of fixed-bound subarrays.
15+
# A subarray is a contiguous part of an array.
16+
#  
17+
# Example 1:
18+
#
19+
# Input: nums = [1,3,5,2,7,5], minK = 1, maxK = 5
20+
# Output: 2
21+
# Explanation: The fixed-bound subarrays are [1,3,5] and [1,3,5,2].
22+
#
23+
# Example 2:
24+
#
25+
# Input: nums = [1,1,1,1], minK = 1, maxK = 1
26+
# Output: 10
27+
# Explanation: Every subarray of nums is a fixed-bound subarray. There are 10 possible subarrays.
28+
#
29+
#  
30+
# Constraints:
31+
#
32+
# 2 <= nums.length <= 105
33+
# 1 <= nums[i], minK, maxK <= 106
34+
#
35+
#
36+
37+
class Solution:
38+
def countSubarrays(self, nums: List[int], minK: int, maxK: int) -> int:
39+
n = len(nums)
40+
res = 0
41+
i = 0
42+
jmin = jmax = -1
43+
for j in range(n):
44+
if nums[j] < minK or nums[j] > maxK:
45+
i = j + 1
46+
47+
if nums[j] == minK:
48+
jmin = j
49+
50+
if nums[j] == maxK:
51+
jmax = j
52+
53+
res += max(0, min(jmin, jmax) - i + 1)
54+
55+
return res
56+

0 commit comments

Comments
 (0)