Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] master from wisdompeak:master #354

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 4 commits into AlgorithmAndLeetCode:master from wisdompeak:master
Aug 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Readme.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -1704,6 +1704,7 @@
[2952.Minimum-Number-of-Coins-to-be-Added](https://github.com/wisdompeak/LeetCode/tree/master/Thinking/2952.Minimum-Number-of-Coins-to-be-Added) (H-)
[3609.Minimum-Moves-to-Reach-Target-in-Grid](https://github.com/wisdompeak/LeetCode/tree/master/Thinking/3609.Minimum-Moves-to-Reach-Target-in-Grid) (H)
[3644.Maximum-K-to-Sort-a-Permutation](https://github.com/wisdompeak/LeetCode/tree/master/Thinking/3644.Maximum-K-to-Sort-a-Permutation) (H)
[3660.Jump-Game-IX](https://github.com/wisdompeak/LeetCode/tree/master/Thinking/3660.Jump-Game-IX) (H)

#### [LeetCode Cup](https://github.com/wisdompeak/LeetCode/tree/master/LCCUP)
[LCP23.魔术排列](https://github.com/wisdompeak/LeetCode/tree/master/LCCUP/2020Fall/LCP23.%E9%AD%94%E6%9C%AF%E6%8E%92%E5%88%97)
Expand Down
23 changes: 23 additions & 0 deletions Thinking/3660.Jump-Game-IX/3660.Jump-Game-IX.cpp
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution {
public:
vector<int> maxValue(vector<int>& nums) {
int n = nums.size();
vector<int>preMax(n);
vector<int>sufMin(n);
for (int i=0; i<n; i++)
preMax[i] = max(i==0?0:preMax[i-1], nums[i]);
for (int i=n-1; i>=0; i--)
sufMin[i] = min((i==n-1)?INT_MAX:sufMin[i+1], nums[i]);

vector<int>rets(n);
rets[n-1] = preMax[n-1];
for (int i=n-2; i>=0; i--) {
if (preMax[i]>sufMin[i+1])
rets[i] = rets[i+1];
else
rets[i] = preMax[i];
}

return rets;
}
};
11 changes: 11 additions & 0 deletions Thinking/3660.Jump-Game-IX/Readme.md
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
### 3660.Jump-Game-IX

此题乍看是个图论的问题,彼此可以跳转的点可以认为是联通的。但是构建所有的边需要n^2的复杂度。

我们定义preMax[i]表示前i个元素(包括自身)里的最大值。考察任意的rets[i],如果答案只在左边的话,那么答案就是preMax[i]。但是也有可能答案在右边。从i能往右跳转到哪些地方呢?我们势必会先借助于preMax[i],因为从preMax[i]跳转的话可以最大范围地覆盖到[i+1:n-1]里的可跳转区域。此时两种情况:

1. 如果`preMax[i]<sufMin[i+1]`,也就是说从preMax[i]也无法跳转到i右边的任何位置,于是rets[i]只能是preMax[i].

2. 如果`preMax[i]>sufMin[i+1]`,那么我们可以有这样一条跳转路径:i->preMax[i]->sufMin[i]->i+1. 最后一步跳转的依据是:根据定义,sufMin[i]是[i+1:n-1]里的最小值,必然小于等于nums[i+1].由此可知i与i+1是联通的,必然有`rets[i]=rets[i+1]`.

由此我们发现了rets从后往前的递推关系。因为rets[n-1]必然等于preMax[n-1],由此可以根据以上的结论,依次推出i=n-2,n-1,...,0的答案。

AltStyle によって変換されたページ (->オリジナル) /