diff --git a/Readme.md b/Readme.md index 70938ab70..53da52feb 100644 --- a/Readme.md +++ b/Readme.md @@ -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) diff --git a/Thinking/3660.Jump-Game-IX/3660.Jump-Game-IX.cpp b/Thinking/3660.Jump-Game-IX/3660.Jump-Game-IX.cpp new file mode 100644 index 000000000..ce95f215a --- /dev/null +++ b/Thinking/3660.Jump-Game-IX/3660.Jump-Game-IX.cpp @@ -0,0 +1,23 @@ +class Solution { +public: + vector maxValue(vector& nums) { + int n = nums.size(); + vectorpreMax(n); + vectorsufMin(n); + for (int i=0; i=0; i--) + sufMin[i] = min((i==n-1)?INT_MAX:sufMin[i+1], nums[i]); + + vectorrets(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; + } +}; diff --git a/Thinking/3660.Jump-Game-IX/Readme.md b/Thinking/3660.Jump-Game-IX/Readme.md new file mode 100644 index 000000000..31b1a3df6 --- /dev/null +++ b/Thinking/3660.Jump-Game-IX/Readme.md @@ -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]`,那么我们可以有这样一条跳转路径: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 によって変換されたページ (->オリジナル) /