Skip to content

Commit b009e9c

Browse files
authored
feat: add solutions to lc problems: No.3113~3115 (#2585)
* No.3113.Find the Number of Subarrays Where Boundary Elements Are Maximum * No.3114.Latest Time You Can Obtain After Replacing Characters * No.3115.Maximum Prime Difference
1 parent 9a067fc commit b009e9c

File tree

21 files changed

+872
-24
lines changed

21 files changed

+872
-24
lines changed

solution/3100-3199/3113.Find the Number of Subarrays Where Boundary Elements Are Maximum/README.md

+96-4
Original file line numberDiff line numberDiff line change
@@ -85,24 +85,116 @@
8585

8686
## 解法
8787

88-
### 方法一
88+
### 方法一:单调栈
89+
90+
我们考虑以数组 $nums$ 中的每个元素 $x$ 作为子数组的边界元素且最大值的情况。
91+
92+
每个长度为 $1$ 的子数组都满足条件,而对于长度大于 $1$ 的子数组,子数组中的所有元素都不能大于边界元素 $x$,我们可以用单调栈来实现。
93+
94+
我们维护一个从栈底到栈顶单调递减的栈,单调栈的每个元素是一个二元组 $[x, cnt]$,表示元素 $x$ 且以 $x$ 为边界元素且最大值的子数组的个数为 $cnt$。
95+
96+
我们从左到右遍历数组 $nums$,对于每个元素 $x$,我们不断地将栈顶元素弹出,直到栈为空或者栈顶元素的第一个元素大于等于 $x$。如果栈为空,或者栈顶元素的第一个元素大于 $x$,说明当前遇到第一个边界元素为 $x$ 且最大值的子数组,该子数组的长度为 $1$,所以我们将 $[x, 1]$ 入栈。如果栈顶元素的第一个元素等于 $x$,说明当前遇到的边界元素为 $x$ 且最大值的子数组,我们将栈顶元素的第二个元素加 $1$。然后,我们将栈顶元素的第二个元素加到答案中。
97+
98+
遍历结束后,返回答案即可。
99+
100+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $nums$ 的长度。
89101

90102
<!-- tabs:start -->
91103

92104
```python
93-
105+
class Solution:
106+
def numberOfSubarrays(self, nums: List[int]) -> int:
107+
stk = []
108+
ans = 0
109+
for x in nums:
110+
while stk and stk[-1][0] < x:
111+
stk.pop()
112+
if not stk or stk[-1][0] > x:
113+
stk.append([x, 1])
114+
else:
115+
stk[-1][1] += 1
116+
ans += stk[-1][1]
117+
return ans
94118
```
95119

96120
```java
97-
121+
class Solution {
122+
public long numberOfSubarrays(int[] nums) {
123+
Deque<int[]> stk = new ArrayDeque<>();
124+
long ans = 0;
125+
for (int x : nums) {
126+
while (!stk.isEmpty() && stk.peek()[0] < x) {
127+
stk.pop();
128+
}
129+
if (stk.isEmpty() || stk.peek()[0] > x) {
130+
stk.push(new int[] {x, 1});
131+
} else {
132+
stk.peek()[1]++;
133+
}
134+
ans += stk.peek()[1];
135+
}
136+
return ans;
137+
}
138+
}
98139
```
99140

100141
```cpp
101-
142+
class Solution {
143+
public:
144+
long long numberOfSubarrays(vector<int>& nums) {
145+
vector<pair<int, int>> stk;
146+
long long ans = 0;
147+
for (int x : nums) {
148+
while (!stk.empty() && stk.back().first < x) {
149+
stk.pop_back();
150+
}
151+
if (stk.empty() || stk.back().first > x) {
152+
stk.push_back(make_pair(x, 1));
153+
} else {
154+
stk.back().second++;
155+
}
156+
ans += stk.back().second;
157+
}
158+
return ans;
159+
}
160+
};
102161
```
103162
104163
```go
164+
func numberOfSubarrays(nums []int) (ans int64) {
165+
stk := [][2]int{}
166+
for _, x := range nums {
167+
for len(stk) > 0 && stk[len(stk)-1][0] < x {
168+
stk = stk[:len(stk)-1]
169+
}
170+
if len(stk) == 0 || stk[len(stk)-1][0] > x {
171+
stk = append(stk, [2]int{x, 1})
172+
} else {
173+
stk[len(stk)-1][1]++
174+
}
175+
ans += int64(stk[len(stk)-1][1])
176+
}
177+
return
178+
}
179+
```
105180

181+
```ts
182+
function numberOfSubarrays(nums: number[]): number {
183+
const stk: number[][] = [];
184+
let ans = 0;
185+
for (const x of nums) {
186+
while (stk.length > 0 && stk.at(-1)![0] < x) {
187+
stk.pop();
188+
}
189+
if (stk.length === 0 || stk.at(-1)![0] > x) {
190+
stk.push([x, 1]);
191+
} else {
192+
stk.at(-1)![1]++;
193+
}
194+
ans += stk.at(-1)![1];
195+
}
196+
return ans;
197+
}
106198
```
107199

108200
<!-- tabs:end -->

solution/3100-3199/3113.Find the Number of Subarrays Where Boundary Elements Are Maximum/README_EN.md

+96-4
Original file line numberDiff line numberDiff line change
@@ -81,24 +81,116 @@
8181

8282
## Solutions
8383

84-
### Solution 1
84+
### Solution 1: Monotonic Stack
85+
86+
We consider each element $x$ in the array $nums$ as the boundary element and the maximum value of the subarray.
87+
88+
Each subarray of length $1$ meets the condition, and for subarrays with length greater than $1$, all elements in the subarray cannot be greater than the boundary element $x$. We can implement this with a monotonic stack.
89+
90+
We maintain a stack that decreases monotonically from the bottom to the top. Each element in the monotonic stack is a pair $[x, cnt]$, representing the element $x$ and the count $cnt$ of subarrays with $x$ as the boundary element and the maximum value.
91+
92+
We traverse the array $nums$ from left to right. For each element $x$, we continuously pop the top element of the stack until the stack is empty or the first element of the top element of the stack is greater than or equal to $x$. If the stack is empty, or the first element of the top element of the stack is greater than $x$, it means that we have encountered the first subarray with $x$ as the boundary element and the maximum value, and the length of this subarray is $1$, so we push $[x, 1]$ into the stack. If the first element of the top element of the stack is equal to $x$, it means that we have encountered a subarray with $x$ as the boundary element and the maximum value, and we add $1$ to the second element of the top element of the stack. Then, we add the second element of the top element of the stack to the answer.
93+
94+
After the traversal, we return the answer.
95+
96+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the array $nums$.
8597

8698
<!-- tabs:start -->
8799

88100
```python
89-
101+
class Solution:
102+
def numberOfSubarrays(self, nums: List[int]) -> int:
103+
stk = []
104+
ans = 0
105+
for x in nums:
106+
while stk and stk[-1][0] < x:
107+
stk.pop()
108+
if not stk or stk[-1][0] > x:
109+
stk.append([x, 1])
110+
else:
111+
stk[-1][1] += 1
112+
ans += stk[-1][1]
113+
return ans
90114
```
91115

92116
```java
93-
117+
class Solution {
118+
public long numberOfSubarrays(int[] nums) {
119+
Deque<int[]> stk = new ArrayDeque<>();
120+
long ans = 0;
121+
for (int x : nums) {
122+
while (!stk.isEmpty() && stk.peek()[0] < x) {
123+
stk.pop();
124+
}
125+
if (stk.isEmpty() || stk.peek()[0] > x) {
126+
stk.push(new int[] {x, 1});
127+
} else {
128+
stk.peek()[1]++;
129+
}
130+
ans += stk.peek()[1];
131+
}
132+
return ans;
133+
}
134+
}
94135
```
95136

96137
```cpp
97-
138+
class Solution {
139+
public:
140+
long long numberOfSubarrays(vector<int>& nums) {
141+
vector<pair<int, int>> stk;
142+
long long ans = 0;
143+
for (int x : nums) {
144+
while (!stk.empty() && stk.back().first < x) {
145+
stk.pop_back();
146+
}
147+
if (stk.empty() || stk.back().first > x) {
148+
stk.push_back(make_pair(x, 1));
149+
} else {
150+
stk.back().second++;
151+
}
152+
ans += stk.back().second;
153+
}
154+
return ans;
155+
}
156+
};
98157
```
99158
100159
```go
160+
func numberOfSubarrays(nums []int) (ans int64) {
161+
stk := [][2]int{}
162+
for _, x := range nums {
163+
for len(stk) > 0 && stk[len(stk)-1][0] < x {
164+
stk = stk[:len(stk)-1]
165+
}
166+
if len(stk) == 0 || stk[len(stk)-1][0] > x {
167+
stk = append(stk, [2]int{x, 1})
168+
} else {
169+
stk[len(stk)-1][1]++
170+
}
171+
ans += int64(stk[len(stk)-1][1])
172+
}
173+
return
174+
}
175+
```
101176

177+
```ts
178+
function numberOfSubarrays(nums: number[]): number {
179+
const stk: number[][] = [];
180+
let ans = 0;
181+
for (const x of nums) {
182+
while (stk.length > 0 && stk.at(-1)![0] < x) {
183+
stk.pop();
184+
}
185+
if (stk.length === 0 || stk.at(-1)![0] > x) {
186+
stk.push([x, 1]);
187+
} else {
188+
stk.at(-1)![1]++;
189+
}
190+
ans += stk.at(-1)![1];
191+
}
192+
return ans;
193+
}
102194
```
103195

104196
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
long long numberOfSubarrays(vector<int>& nums) {
4+
vector<pair<int, int>> stk;
5+
long long ans = 0;
6+
for (int x : nums) {
7+
while (!stk.empty() && stk.back().first < x) {
8+
stk.pop_back();
9+
}
10+
if (stk.empty() || stk.back().first > x) {
11+
stk.push_back(make_pair(x, 1));
12+
} else {
13+
stk.back().second++;
14+
}
15+
ans += stk.back().second;
16+
}
17+
return ans;
18+
}
19+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
func numberOfSubarrays(nums []int) (ans int64) {
2+
stk := [][2]int{}
3+
for _, x := range nums {
4+
for len(stk) > 0 && stk[len(stk)-1][0] < x {
5+
stk = stk[:len(stk)-1]
6+
}
7+
if len(stk) == 0 || stk[len(stk)-1][0] > x {
8+
stk = append(stk, [2]int{x, 1})
9+
} else {
10+
stk[len(stk)-1][1]++
11+
}
12+
ans += int64(stk[len(stk)-1][1])
13+
}
14+
return
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public long numberOfSubarrays(int[] nums) {
3+
Deque<int[]> stk = new ArrayDeque<>();
4+
long ans = 0;
5+
for (int x : nums) {
6+
while (!stk.isEmpty() && stk.peek()[0] < x) {
7+
stk.pop();
8+
}
9+
if (stk.isEmpty() || stk.peek()[0] > x) {
10+
stk.push(new int[] {x, 1});
11+
} else {
12+
stk.peek()[1]++;
13+
}
14+
ans += stk.peek()[1];
15+
}
16+
return ans;
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
class Solution:
2+
def numberOfSubarrays(self, nums: List[int]) -> int:
3+
stk = []
4+
ans = 0
5+
for x in nums:
6+
while stk and stk[-1][0] < x:
7+
stk.pop()
8+
if not stk or stk[-1][0] > x:
9+
stk.append([x, 1])
10+
else:
11+
stk[-1][1] += 1
12+
ans += stk[-1][1]
13+
return ans
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function numberOfSubarrays(nums: number[]): number {
2+
const stk: number[][] = [];
3+
let ans = 0;
4+
for (const x of nums) {
5+
while (stk.length > 0 && stk.at(-1)![0] < x) {
6+
stk.pop();
7+
}
8+
if (stk.length === 0 || stk.at(-1)![0] > x) {
9+
stk.push([x, 1]);
10+
} else {
11+
stk.at(-1)![1]++;
12+
}
13+
ans += stk.at(-1)![1];
14+
}
15+
return ans;
16+
}

0 commit comments

Comments
 (0)