diff --git a/solution/3200-3299/3205.Maximum Array Hopping Score I/README.md b/solution/3200-3299/3205.Maximum Array Hopping Score I/README.md index a19821d463494..da3729f76276c 100644 --- a/solution/3200-3299/3205.Maximum Array Hopping Score I/README.md +++ b/solution/3200-3299/3205.Maximum Array Hopping Score I/README.md @@ -285,4 +285,128 @@ function maxScore(nums: number[]): number { + + +### 方法三:单调栈 + +我们观察发现,对于当前位置 $i$,我们应该跳到下一个值最大的位置 $j$,这样才能获得最大的分数。 + +因此,我们遍历数组 $\text{nums}$,维护一个从栈底到栈顶单调递减的栈 $\text{stk}$。对于当前遍历到的位置 $i$,如果栈顶元素对应的值小于等于 $\text{nums}[i]$,我们就不断地弹出栈顶元素,直到栈为空或者栈顶元素对应的值大于 $\text{nums}[i]$,然后将 $i$ 入栈。 + +然后,我们初始化答案 $\text{ans}$ 和当前位置 $i = 0$,遍历栈中的元素,每次取出栈顶元素 $j$,更新答案 $\text{ans} += \text{nums}[j] \times (j - i)$,然后更新 $i = j$。 + +最后返回答案 $\text{ans}$。 + +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组的长度。 + + + +#### Python3 + +```python +class Solution: + def maxScore(self, nums: List[int]) -> int: + stk = [] + for i, x in enumerate(nums): + while stk and nums[stk[-1]] <= x: + stk.pop() + stk.append(i) + ans = i = 0 + for j in stk: + ans += nums[j] * (j - i) + i = j + return ans +``` + +#### Java + +```java +class Solution { + public int maxScore(int[] nums) { + Deque stk = new ArrayDeque<>(); + for (int i = 0; i < nums.length; ++i) { + while (!stk.isEmpty() && nums[stk.peek()] <= nums[i]) { + stk.pop(); + } + stk.push(i); + } + int ans = 0, i = 0; + while (!stk.isEmpty()) { + int j = stk.pollLast(); + ans += (j - i) * nums[j]; + i = j; + } + return ans; + } +} +``` + +#### C++ + +```cpp +class Solution { +public: + int maxScore(vector& nums) { + vector stk; + for (int i = 0; i < nums.size(); ++i) { + while (stk.size() && nums[stk.back()] <= nums[i]) { + stk.pop_back(); + } + stk.push_back(i); + } + int ans = 0, i = 0; + for (int j : stk) { + ans += (j - i) * nums[j]; + i = j; + } + return ans; + } +}; +``` + +#### Go + +```go +func maxScore(nums []int) (ans int) { + stk := []int{} + for i, x := range nums { + for len(stk) > 0 && nums[stk[len(stk)-1]] <= x { + stk = stk[:len(stk)-1] + } + stk = append(stk, i) + } + i := 0 + for _, j := range stk { + ans += (j - i) * nums[j] + i = j + } + return +} +``` + +#### TypeScript + +```ts +function maxScore(nums: number[]): number { + const stk: number[] = []; + for (let i = 0; i < nums.length; ++i) { + while (stk.length && nums[stk.at(-1)!] <= nums[i]) { + stk.pop(); + } + stk.push(i); + } + let ans = 0; + let i = 0; + for (const j of stk) { + ans += (j - i) * nums[j]; + i = j; + } + return ans; +} +``` + + + + + diff --git a/solution/3200-3299/3205.Maximum Array Hopping Score I/README_EN.md b/solution/3200-3299/3205.Maximum Array Hopping Score I/README_EN.md index c7f720ca8534a..43195076335af 100644 --- a/solution/3200-3299/3205.Maximum Array Hopping Score I/README_EN.md +++ b/solution/3200-3299/3205.Maximum Array Hopping Score I/README_EN.md @@ -285,4 +285,128 @@ function maxScore(nums: number[]): number { + + +### Solution 3: Monotonic Stack + +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. + +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. + +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$. + +Finally, return the answer $\text{ans}$. + +The time complexity is $O(n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array. + + + +#### Python3 + +```python +class Solution: + def maxScore(self, nums: List[int]) -> int: + stk = [] + for i, x in enumerate(nums): + while stk and nums[stk[-1]] <= x: + stk.pop() + stk.append(i) + ans = i = 0 + for j in stk: + ans += nums[j] * (j - i) + i = j + return ans +``` + +#### Java + +```java +class Solution { + public int maxScore(int[] nums) { + Deque stk = new ArrayDeque<>(); + for (int i = 0; i < nums.length; ++i) { + while (!stk.isEmpty() && nums[stk.peek()] <= nums[i]) { + stk.pop(); + } + stk.push(i); + } + int ans = 0, i = 0; + while (!stk.isEmpty()) { + int j = stk.pollLast(); + ans += (j - i) * nums[j]; + i = j; + } + return ans; + } +} +``` + +#### C++ + +```cpp +class Solution { +public: + int maxScore(vector& nums) { + vector stk; + for (int i = 0; i < nums.size(); ++i) { + while (stk.size() && nums[stk.back()] <= nums[i]) { + stk.pop_back(); + } + stk.push_back(i); + } + int ans = 0, i = 0; + for (int j : stk) { + ans += (j - i) * nums[j]; + i = j; + } + return ans; + } +}; +``` + +#### Go + +```go +func maxScore(nums []int) (ans int) { + stk := []int{} + for i, x := range nums { + for len(stk) > 0 && nums[stk[len(stk)-1]] <= x { + stk = stk[:len(stk)-1] + } + stk = append(stk, i) + } + i := 0 + for _, j := range stk { + ans += (j - i) * nums[j] + i = j + } + return +} +``` + +#### TypeScript + +```ts +function maxScore(nums: number[]): number { + const stk: number[] = []; + for (let i = 0; i < nums.length; ++i) { + while (stk.length && nums[stk.at(-1)!] <= nums[i]) { + stk.pop(); + } + stk.push(i); + } + let ans = 0; + let i = 0; + for (const j of stk) { + ans += (j - i) * nums[j]; + i = j; + } + return ans; +} +``` + + + + + diff --git a/solution/3200-3299/3205.Maximum Array Hopping Score I/Solution3.cpp b/solution/3200-3299/3205.Maximum Array Hopping Score I/Solution3.cpp new file mode 100644 index 0000000000000..f0024ae262344 --- /dev/null +++ b/solution/3200-3299/3205.Maximum Array Hopping Score I/Solution3.cpp @@ -0,0 +1,18 @@ +class Solution { +public: + int maxScore(vector& nums) { + vector stk; + for (int i = 0; i < nums.size(); ++i) { + while (stk.size() && nums[stk.back()] <= nums[i]) { + stk.pop_back(); + } + stk.push_back(i); + } + int ans = 0, i = 0; + for (int j : stk) { + ans += (j - i) * nums[j]; + i = j; + } + return ans; + } +}; \ No newline at end of file diff --git a/solution/3200-3299/3205.Maximum Array Hopping Score I/Solution3.go b/solution/3200-3299/3205.Maximum Array Hopping Score I/Solution3.go new file mode 100644 index 0000000000000..af7fdb8f760f8 --- /dev/null +++ b/solution/3200-3299/3205.Maximum Array Hopping Score I/Solution3.go @@ -0,0 +1,15 @@ +func maxScore(nums []int) (ans int) { + stk := []int{} + for i, x := range nums { + for len(stk) > 0 && nums[stk[len(stk)-1]] <= x { + stk = stk[:len(stk)-1] + } + stk = append(stk, i) + } + i := 0 + for _, j := range stk { + ans += (j - i) * nums[j] + i = j + } + return +} \ No newline at end of file diff --git a/solution/3200-3299/3205.Maximum Array Hopping Score I/Solution3.java b/solution/3200-3299/3205.Maximum Array Hopping Score I/Solution3.java new file mode 100644 index 0000000000000..47b0b306bfa4d --- /dev/null +++ b/solution/3200-3299/3205.Maximum Array Hopping Score I/Solution3.java @@ -0,0 +1,18 @@ +class Solution { + public int maxScore(int[] nums) { + Deque stk = new ArrayDeque<>(); + for (int i = 0; i < nums.length; ++i) { + while (!stk.isEmpty() && nums[stk.peek()] <= nums[i]) { + stk.pop(); + } + stk.push(i); + } + int ans = 0, i = 0; + while (!stk.isEmpty()) { + int j = stk.pollLast(); + ans += (j - i) * nums[j]; + i = j; + } + return ans; + } +} \ No newline at end of file diff --git a/solution/3200-3299/3205.Maximum Array Hopping Score I/Solution3.py b/solution/3200-3299/3205.Maximum Array Hopping Score I/Solution3.py new file mode 100644 index 0000000000000..02a773ddd21f7 --- /dev/null +++ b/solution/3200-3299/3205.Maximum Array Hopping Score I/Solution3.py @@ -0,0 +1,12 @@ +class Solution: + def maxScore(self, nums: List[int]) -> int: + stk = [] + for i, x in enumerate(nums): + while stk and nums[stk[-1]] <= x: + stk.pop() + stk.append(i) + ans = i = 0 + for j in stk: + ans += nums[j] * (j - i) + i = j + return ans diff --git a/solution/3200-3299/3205.Maximum Array Hopping Score I/Solution3.ts b/solution/3200-3299/3205.Maximum Array Hopping Score I/Solution3.ts new file mode 100644 index 0000000000000..a5411d1b49c0a --- /dev/null +++ b/solution/3200-3299/3205.Maximum Array Hopping Score I/Solution3.ts @@ -0,0 +1,16 @@ +function maxScore(nums: number[]): number { + const stk: number[] = []; + for (let i = 0; i < nums.length; ++i) { + while (stk.length && nums[stk.at(-1)!] <= nums[i]) { + stk.pop(); + } + stk.push(i); + } + let ans = 0; + let i = 0; + for (const j of stk) { + ans += (j - i) * nums[j]; + i = j; + } + return ans; +}