Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit e320a9f

Browse files
authored
feat: add ts solution to lc problem: No.1793 (doocs#1913)
No.1793.Maximum Score of a Good Subarray
1 parent 4a5c1c5 commit e320a9f

File tree

4 files changed

+117
-3
lines changed

4 files changed

+117
-3
lines changed

solution/1700-1799/1793.Maximum Score of a Good Subarray/README.md

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@
4444

4545
**方法一:单调栈**
4646

47-
我们可以枚举 `nums` 中的每个元素 $nums[i]$ 作为子数组的最小值,利用单调栈找出其左边第一个小于 $nums[i]$ 的位置 $left[i]$ 和右边第一个小于等于 $nums[i]$ 的位置 $right[i]$,则以 $nums[i]$ 为最小值的子数组的分数为 $nums[i] \times (right[i] - left[i] - 1)$。
47+
我们可以枚举 $nums$ 中的每个元素 $nums[i]$ 作为子数组的最小值,利用单调栈找出其左边第一个小于 $nums[i]$ 的位置 $left[i]$ 和右边第一个小于等于 $nums[i]$ 的位置 $right[i]$,则以 $nums[i]$ 为最小值的子数组的分数为 $nums[i] \times (right[i] - left[i] - 1)$。
4848

4949
需要注意的是,只有当左右边界 $left[i]$ 和 $right[i]$ 满足 $left[i]+1 \leq k \leq right[i]-1$ 时,答案才有可能更新。
5050

51-
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 `nums` 的长度。
51+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为数组 $nums$ 的长度。
5252

5353
<!-- tabs:start -->
5454

@@ -212,6 +212,43 @@ func maximumScore(nums []int, k int) (ans int) {
212212
}
213213
```
214214

215+
### **TypeScript**
216+
217+
```ts
218+
function maximumScore(nums: number[], k: number): number {
219+
const n = nums.length;
220+
const left: number[] = Array(n).fill(-1);
221+
const right: number[] = Array(n).fill(n);
222+
const stk: number[] = [];
223+
for (let i = 0; i < n; ++i) {
224+
while (stk.length && nums[stk.at(-1)] >= nums[i]) {
225+
stk.pop();
226+
}
227+
if (stk.length) {
228+
left[i] = stk.at(-1);
229+
}
230+
stk.push(i);
231+
}
232+
stk.length = 0;
233+
for (let i = n - 1; ~i; --i) {
234+
while (stk.length && nums[stk.at(-1)] > nums[i]) {
235+
stk.pop();
236+
}
237+
if (stk.length) {
238+
right[i] = stk.at(-1);
239+
}
240+
stk.push(i);
241+
}
242+
let ans = 0;
243+
for (let i = 0; i < n; ++i) {
244+
if (left[i] + 1 <= k && k <= right[i] - 1) {
245+
ans = Math.max(ans, nums[i] * (right[i] - left[i] - 1));
246+
}
247+
}
248+
return ans;
249+
}
250+
```
251+
215252
### **...**
216253

217254
```

solution/1700-1799/1793.Maximum Score of a Good Subarray/README_EN.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@
3838

3939
## Solutions
4040

41+
**Solution 1: Monotonic Stack**
42+
43+
We can enumerate each element $nums[i]$ in $nums$ as the minimum value of the subarray, and use a monotonic stack to find the first position $left[i]$ on the left that is less than $nums[i]$ and the first position $right[i]$ on the right that is less than or equal to $nums[i]$. Then, the score of the subarray with $nums[i]$ as the minimum value is $nums[i] \times (right[i] - left[i] - 1)$.
44+
45+
It should be noted that the answer can only be updated when the left and right boundaries $left[i]$ and $right[i]$ satisfy $left[i]+1 \leq k \leq right[i]-1$.
46+
47+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $nums$.
48+
4149
<!-- tabs:start -->
4250

4351
### **Python3**
@@ -196,6 +204,43 @@ func maximumScore(nums []int, k int) (ans int) {
196204
}
197205
```
198206

207+
### **TypeScript**
208+
209+
```ts
210+
function maximumScore(nums: number[], k: number): number {
211+
const n = nums.length;
212+
const left: number[] = Array(n).fill(-1);
213+
const right: number[] = Array(n).fill(n);
214+
const stk: number[] = [];
215+
for (let i = 0; i < n; ++i) {
216+
while (stk.length && nums[stk.at(-1)] >= nums[i]) {
217+
stk.pop();
218+
}
219+
if (stk.length) {
220+
left[i] = stk.at(-1);
221+
}
222+
stk.push(i);
223+
}
224+
stk.length = 0;
225+
for (let i = n - 1; ~i; --i) {
226+
while (stk.length && nums[stk.at(-1)] > nums[i]) {
227+
stk.pop();
228+
}
229+
if (stk.length) {
230+
right[i] = stk.at(-1);
231+
}
232+
stk.push(i);
233+
}
234+
let ans = 0;
235+
for (let i = 0; i < n; ++i) {
236+
if (left[i] + 1 <= k && k <= right[i] - 1) {
237+
ans = Math.max(ans, nums[i] * (right[i] - left[i] - 1));
238+
}
239+
}
240+
return ans;
241+
}
242+
```
243+
199244
### **...**
200245

201246
```
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
function maximumScore(nums: number[], k: number): number {
2+
const n = nums.length;
3+
const left: number[] = Array(n).fill(-1);
4+
const right: number[] = Array(n).fill(n);
5+
const stk: number[] = [];
6+
for (let i = 0; i < n; ++i) {
7+
while (stk.length && nums[stk.at(-1)] >= nums[i]) {
8+
stk.pop();
9+
}
10+
if (stk.length) {
11+
left[i] = stk.at(-1);
12+
}
13+
stk.push(i);
14+
}
15+
stk.length = 0;
16+
for (let i = n - 1; ~i; --i) {
17+
while (stk.length && nums[stk.at(-1)] > nums[i]) {
18+
stk.pop();
19+
}
20+
if (stk.length) {
21+
right[i] = stk.at(-1);
22+
}
23+
stk.push(i);
24+
}
25+
let ans = 0;
26+
for (let i = 0; i < n; ++i) {
27+
if (left[i] + 1 <= k && k <= right[i] - 1) {
28+
ans = Math.max(ans, nums[i] * (right[i] - left[i] - 1));
29+
}
30+
}
31+
return ans;
32+
}

solution/1700-1799/1794.Count Pairs of Equal Substrings With Minimum Difference/README.md

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

5353
题目实际上要我们找到一个最小的下标 $i$ 和一个最大的下标 $j$,使得 $firstString[i]$ 与 $secondString[j]$ 相等,且 $i - j$ 的值是所有满足条件的下标对中最小的。
5454

55-
因此,我们先用哈希表 `last` 记录 $secondString$ 中每个字符最后一次出现的下标,然后遍历 $firstString$,对于每个字符 $c$,如果 $c$ 在 $secondString$ 中出现过,则计算 $i - last[c]$,如果 $i - last[c]$ 的值小于当前最小值,则更新最小值,同时更新答案为 1;如果 $i - last[c]$ 的值等于当前最小值,则答案加 1。
55+
因此,我们先用哈希表 $last$ 记录 $secondString$ 中每个字符最后一次出现的下标,然后遍历 $firstString$,对于每个字符 $c$,如果 $c$ 在 $secondString$ 中出现过,则计算 $i - last[c]$,如果 $i - last[c]$ 的值小于当前最小值,则更新最小值,同时更新答案为 1;如果 $i - last[c]$ 的值等于当前最小值,则答案加 1。
5656

5757
时间复杂度 $O(m + n)$,空间复杂度 $O(C)$。其中 $m$ 和 $n$ 分别是 $firstString$ 和 $secondString$ 的长度,而 $C$ 是字符集的大小。本题中 $C = 26$。
5858

0 commit comments

Comments
 (0)