Skip to content

Commit 4bd6a33

Browse files
authored
feat: add solutions to lc problem: No.3205 (doocs#3201)
No.3205.Maximum Array Hopping Score I
1 parent 3dc80c3 commit 4bd6a33

File tree

7 files changed

+327
-0
lines changed

7 files changed

+327
-0
lines changed

solution/3200-3299/3205.Maximum Array Hopping Score I/README.md

+124
Original file line numberDiff line numberDiff line change
@@ -285,4 +285,128 @@ function maxScore(nums: number[]): number {
285285

286286
<!-- solution:end -->
287287

288+
<!-- solution:start -->
289+
290+
### 方法三:单调栈
291+
292+
我们观察发现,对于当前位置 $i$,我们应该跳到下一个值最大的位置 $j$,这样才能获得最大的分数。
293+
294+
因此,我们遍历数组 $\text{nums}$,维护一个从栈底到栈顶单调递减的栈 $\text{stk}$。对于当前遍历到的位置 $i$,如果栈顶元素对应的值小于等于 $\text{nums}[i]$,我们就不断地弹出栈顶元素,直到栈为空或者栈顶元素对应的值大于 $\text{nums}[i]$,然后将 $i$ 入栈。
295+
296+
然后,我们初始化答案 $\text{ans}$ 和当前位置 $i = 0$,遍历栈中的元素,每次取出栈顶元素 $j$,更新答案 $\text{ans} += \text{nums}[j] \times (j - i)$,然后更新 $i = j$。
297+
298+
最后返回答案 $\text{ans}$。
299+
300+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组的长度。
301+
302+
<!-- tabs:start -->
303+
304+
#### Python3
305+
306+
```python
307+
class Solution:
308+
def maxScore(self, nums: List[int]) -> int:
309+
stk = []
310+
for i, x in enumerate(nums):
311+
while stk and nums[stk[-1]] <= x:
312+
stk.pop()
313+
stk.append(i)
314+
ans = i = 0
315+
for j in stk:
316+
ans += nums[j] * (j - i)
317+
i = j
318+
return ans
319+
```
320+
321+
#### Java
322+
323+
```java
324+
class Solution {
325+
public int maxScore(int[] nums) {
326+
Deque<Integer> stk = new ArrayDeque<>();
327+
for (int i = 0; i < nums.length; ++i) {
328+
while (!stk.isEmpty() && nums[stk.peek()] <= nums[i]) {
329+
stk.pop();
330+
}
331+
stk.push(i);
332+
}
333+
int ans = 0, i = 0;
334+
while (!stk.isEmpty()) {
335+
int j = stk.pollLast();
336+
ans += (j - i) * nums[j];
337+
i = j;
338+
}
339+
return ans;
340+
}
341+
}
342+
```
343+
344+
#### C++
345+
346+
```cpp
347+
class Solution {
348+
public:
349+
int maxScore(vector<int>& nums) {
350+
vector<int> stk;
351+
for (int i = 0; i < nums.size(); ++i) {
352+
while (stk.size() && nums[stk.back()] <= nums[i]) {
353+
stk.pop_back();
354+
}
355+
stk.push_back(i);
356+
}
357+
int ans = 0, i = 0;
358+
for (int j : stk) {
359+
ans += (j - i) * nums[j];
360+
i = j;
361+
}
362+
return ans;
363+
}
364+
};
365+
```
366+
367+
#### Go
368+
369+
```go
370+
func maxScore(nums []int) (ans int) {
371+
stk := []int{}
372+
for i, x := range nums {
373+
for len(stk) > 0 && nums[stk[len(stk)-1]] <= x {
374+
stk = stk[:len(stk)-1]
375+
}
376+
stk = append(stk, i)
377+
}
378+
i := 0
379+
for _, j := range stk {
380+
ans += (j - i) * nums[j]
381+
i = j
382+
}
383+
return
384+
}
385+
```
386+
387+
#### TypeScript
388+
389+
```ts
390+
function maxScore(nums: number[]): number {
391+
const stk: number[] = [];
392+
for (let i = 0; i < nums.length; ++i) {
393+
while (stk.length && nums[stk.at(-1)!] <= nums[i]) {
394+
stk.pop();
395+
}
396+
stk.push(i);
397+
}
398+
let ans = 0;
399+
let i = 0;
400+
for (const j of stk) {
401+
ans += (j - i) * nums[j];
402+
i = j;
403+
}
404+
return ans;
405+
}
406+
```
407+
408+
<!-- tabs:end -->
409+
410+
<!-- solution:end -->
411+
288412
<!-- problem:end -->

solution/3200-3299/3205.Maximum Array Hopping Score I/README_EN.md

+124
Original file line numberDiff line numberDiff line change
@@ -285,4 +285,128 @@ function maxScore(nums: number[]): number {
285285

286286
<!-- solution:end -->
287287

288+
<!-- solution:start -->
289+
290+
### Solution 3: Monotonic Stack
291+
292+
We observe that for the current position $i$, we should jump to the next position $j$ with the maximum value to obtain the maximum score.
293+
294+
Therefore, we traverse the array $\text{nums}$, maintaining a stack $\text{stk}$ that is monotonically decreasing from the bottom to the top of the stack. For the current position $i$ being traversed, if the value corresponding to the top element of the stack is less than or equal to $\text{nums}[i]$, we continuously pop the top element of the stack until the stack is empty or the value corresponding to the top element of the stack is greater than $\text{nums}[i]$, and then push $i$ into the stack.
295+
296+
Next, we initialize the answer $\text{ans}$ and the current position $i = 0$, traverse the elements in the stack, each time taking out the top element $j$, updating the answer $\text{ans} += \text{nums}[j] \times (j - i)$, and then updating $i = j$.
297+
298+
Finally, return the answer $\text{ans}$.
299+
300+
The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array.
301+
302+
<!-- tabs:start -->
303+
304+
#### Python3
305+
306+
```python
307+
class Solution:
308+
def maxScore(self, nums: List[int]) -> int:
309+
stk = []
310+
for i, x in enumerate(nums):
311+
while stk and nums[stk[-1]] <= x:
312+
stk.pop()
313+
stk.append(i)
314+
ans = i = 0
315+
for j in stk:
316+
ans += nums[j] * (j - i)
317+
i = j
318+
return ans
319+
```
320+
321+
#### Java
322+
323+
```java
324+
class Solution {
325+
public int maxScore(int[] nums) {
326+
Deque<Integer> stk = new ArrayDeque<>();
327+
for (int i = 0; i < nums.length; ++i) {
328+
while (!stk.isEmpty() && nums[stk.peek()] <= nums[i]) {
329+
stk.pop();
330+
}
331+
stk.push(i);
332+
}
333+
int ans = 0, i = 0;
334+
while (!stk.isEmpty()) {
335+
int j = stk.pollLast();
336+
ans += (j - i) * nums[j];
337+
i = j;
338+
}
339+
return ans;
340+
}
341+
}
342+
```
343+
344+
#### C++
345+
346+
```cpp
347+
class Solution {
348+
public:
349+
int maxScore(vector<int>& nums) {
350+
vector<int> stk;
351+
for (int i = 0; i < nums.size(); ++i) {
352+
while (stk.size() && nums[stk.back()] <= nums[i]) {
353+
stk.pop_back();
354+
}
355+
stk.push_back(i);
356+
}
357+
int ans = 0, i = 0;
358+
for (int j : stk) {
359+
ans += (j - i) * nums[j];
360+
i = j;
361+
}
362+
return ans;
363+
}
364+
};
365+
```
366+
367+
#### Go
368+
369+
```go
370+
func maxScore(nums []int) (ans int) {
371+
stk := []int{}
372+
for i, x := range nums {
373+
for len(stk) > 0 && nums[stk[len(stk)-1]] <= x {
374+
stk = stk[:len(stk)-1]
375+
}
376+
stk = append(stk, i)
377+
}
378+
i := 0
379+
for _, j := range stk {
380+
ans += (j - i) * nums[j]
381+
i = j
382+
}
383+
return
384+
}
385+
```
386+
387+
#### TypeScript
388+
389+
```ts
390+
function maxScore(nums: number[]): number {
391+
const stk: number[] = [];
392+
for (let i = 0; i < nums.length; ++i) {
393+
while (stk.length && nums[stk.at(-1)!] <= nums[i]) {
394+
stk.pop();
395+
}
396+
stk.push(i);
397+
}
398+
let ans = 0;
399+
let i = 0;
400+
for (const j of stk) {
401+
ans += (j - i) * nums[j];
402+
i = j;
403+
}
404+
return ans;
405+
}
406+
```
407+
408+
<!-- tabs:end -->
409+
410+
<!-- solution:end -->
411+
288412
<!-- problem:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
int maxScore(vector<int>& nums) {
4+
vector<int> stk;
5+
for (int i = 0; i < nums.size(); ++i) {
6+
while (stk.size() && nums[stk.back()] <= nums[i]) {
7+
stk.pop_back();
8+
}
9+
stk.push_back(i);
10+
}
11+
int ans = 0, i = 0;
12+
for (int j : stk) {
13+
ans += (j - i) * nums[j];
14+
i = j;
15+
}
16+
return ans;
17+
}
18+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
func maxScore(nums []int) (ans int) {
2+
stk := []int{}
3+
for i, x := range nums {
4+
for len(stk) > 0 && nums[stk[len(stk)-1]] <= x {
5+
stk = stk[:len(stk)-1]
6+
}
7+
stk = append(stk, i)
8+
}
9+
i := 0
10+
for _, j := range stk {
11+
ans += (j - i) * nums[j]
12+
i = j
13+
}
14+
return
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public int maxScore(int[] nums) {
3+
Deque<Integer> stk = new ArrayDeque<>();
4+
for (int i = 0; i < nums.length; ++i) {
5+
while (!stk.isEmpty() && nums[stk.peek()] <= nums[i]) {
6+
stk.pop();
7+
}
8+
stk.push(i);
9+
}
10+
int ans = 0, i = 0;
11+
while (!stk.isEmpty()) {
12+
int j = stk.pollLast();
13+
ans += (j - i) * nums[j];
14+
i = j;
15+
}
16+
return ans;
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def maxScore(self, nums: List[int]) -> int:
3+
stk = []
4+
for i, x in enumerate(nums):
5+
while stk and nums[stk[-1]] <= x:
6+
stk.pop()
7+
stk.append(i)
8+
ans = i = 0
9+
for j in stk:
10+
ans += nums[j] * (j - i)
11+
i = j
12+
return ans
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function maxScore(nums: number[]): number {
2+
const stk: number[] = [];
3+
for (let i = 0; i < nums.length; ++i) {
4+
while (stk.length && nums[stk.at(-1)!] <= nums[i]) {
5+
stk.pop();
6+
}
7+
stk.push(i);
8+
}
9+
let ans = 0;
10+
let i = 0;
11+
for (const j of stk) {
12+
ans += (j - i) * nums[j];
13+
i = j;
14+
}
15+
return ans;
16+
}

0 commit comments

Comments
 (0)