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 new file mode 100644 index 0000000000000..a19821d463494 --- /dev/null +++ b/solution/3200-3299/3205.Maximum Array Hopping Score I/README.md @@ -0,0 +1,288 @@ +--- +comments: true +difficulty: 中等 +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3205.Maximum%20Array%20Hopping%20Score%20I/README.md +--- + + + +# [3205. Maximum Array Hopping Score I 🔒](https://leetcode.cn/problems/maximum-array-hopping-score-i) + +[English Version](/solution/3200-3299/3205.Maximum%20Array%20Hopping%20Score%20I/README_EN.md) + +## 题目描述 + + + +

Given an array nums, you have to get the maximum score starting from index 0 and hopping until you reach the last element of the array.

+ +

In each hop, you can jump from index i to an index j > i, and you get a score of (j - i) * nums[j].

+ +

Return the maximum score you can get.

+ +

 

+

Example 1:

+ +
+

Input: nums = [1,5,8]

+ +

Output: 16

+ +

Explanation:

+ +

There are two possible ways to reach the last element:

+ + +
+ +

Example 2:

+ +
+

Input: nums = [4,5,2,8,9,1,3]

+ +

Output: 42

+ +

Explanation:

+ +

We can do the hopping 0 -> 4 -> 6 with a score of (4 - 0) * 9 + (6 - 4) * 3 = 42.

+
+ +

 

+

Constraints:

+ + + + + +## 解法 + + + +### 方法一:记忆化搜索 + +我们设计一个函数 $\text{dfs}(i)$,表示从下标 $i$ 出发,能够获得的最大分数。那么答案就是 $\text{dfs}(0)$。 + +函数 $\text{dfs}(i)$ 的执行过程如下: + +我们枚举下一个跳跃的位置 $j$,那么从下标 $i$ 出发,能够获得的分数就是 $(j - i) \times \text{nums}[j]$,加上从下标 $j$ 出发,能够获得的最大分数,总分数就是 $(j - i) \times \text{nums}[j] + \text{dfs}(j)$。我们枚举所有的 $j$,取分数的最大值即可。 + +为了避免重复计算,我们使用记忆化搜索的方法,将已经计算过的 $\text{dfs}(i)$ 的值保存起来,下次直接返回即可。 + +时间复杂度 $O(n^2)$,空间复杂度 $O(n)$。其中 $n$ 是数组的长度。 + + + +#### Python3 + +```python +class Solution: + def maxScore(self, nums: List[int]) -> int: + @cache + def dfs(i: int) -> int: + return max( + [(j - i) * nums[j] + dfs(j) for j in range(i + 1, len(nums))] or [0] + ) + + return dfs(0) +``` + +#### Java + +```java +class Solution { + private Integer[] f; + private int[] nums; + private int n; + + public int maxScore(int[] nums) { + n = nums.length; + f = new Integer[n]; + this.nums = nums; + return dfs(0); + } + + private int dfs(int i) { + if (f[i] != null) { + return f[i]; + } + f[i] = 0; + for (int j = i + 1; j < n; ++j) { + f[i] = Math.max(f[i], (j - i) * nums[j] + dfs(j)); + } + return f[i]; + } +} +``` + +#### C++ + +```cpp +class Solution { +public: + int maxScore(vector& nums) { + int n = nums.size(); + vector f(n); + auto dfs = [&](auto&& dfs, int i) -> int { + if (f[i]) { + return f[i]; + } + for (int j = i + 1; j < n; ++j) { + f[i] = max(f[i], (j - i) * nums[j] + dfs(dfs, j)); + } + return f[i]; + }; + return dfs(dfs, 0); + } +}; +``` + +#### Go + +```go +func maxScore(nums []int) int { + n := len(nums) + f := make([]int, n) + var dfs func(int) int + dfs = func(i int) int { + if f[i] > 0 { + return f[i] + } + for j := i + 1; j < n; j++ { + f[i] = max(f[i], (j-i)*nums[j]+dfs(j)) + } + return f[i] + } + return dfs(0) +} +``` + +#### TypeScript + +```ts +function maxScore(nums: number[]): number { + const n = nums.length; + const f: number[] = Array(n).fill(0); + const dfs = (i: number): number => { + if (f[i]) { + return f[i]; + } + for (let j = i + 1; j < n; ++j) { + f[i] = Math.max(f[i], (j - i) * nums[j] + dfs(j)); + } + return f[i]; + }; + return dfs(0); +} +``` + + + + + + + +### 方法二:动态规划 + +我们可以将方法一中的记忆化搜索转换为动态规划。 + +定义 $f[j]$ 表示从下标 $0$ 出发,到下标 $j$ 结束,能够获得的最大分数。那么答案就是 $f[n - 1]$。 + +状态转移方程为: + +$$ +f[j] = \max_{0 \leq i < j} \{ f[i] + (j - i) \times \text{nums}[j] \} +$$ + +时间复杂度 $O(n^2)$,空间复杂度 $O(n)$。其中 $n$ 是数组的长度。 + + + +#### Python3 + +```python +class Solution: + def maxScore(self, nums: List[int]) -> int: + n = len(nums) + f = [0] * n + for j in range(1, n): + for i in range(j): + f[j] = max(f[j], f[i] + (j - i) * nums[j]) + return f[n - 1] +``` + +#### Java + +```java +class Solution { + public int maxScore(int[] nums) { + int n = nums.length; + int[] f = new int[n]; + for (int j = 1; j < n; ++j) { + for (int i = 0; i < j; ++i) { + f[j] = Math.max(f[j], f[i] + (j - i) * nums[j]); + } + } + return f[n - 1]; + } +} +``` + +#### C++ + +```cpp +class Solution { +public: + int maxScore(vector& nums) { + int n = nums.size(); + vector f(n); + for (int j = 1; j < n; ++j) { + for (int i = 0; i < j; ++i) { + f[j] = max(f[j], f[i] + (j - i) * nums[j]); + } + } + return f[n - 1]; + } +}; +``` + +#### Go + +```go +func maxScore(nums []int) int { + n := len(nums) + f := make([]int, n) + for j := 1; j < n; j++ { + for i := 0; i < j; i++ { + f[j] = max(f[j], f[i]+(j-i)*nums[j]) + } + } + return f[n-1] +} +``` + +#### TypeScript + +```ts +function maxScore(nums: number[]): number { + const n = nums.length; + const f: number[] = Array(n).fill(0); + for (let j = 1; j < n; ++j) { + for (let i = 0; i < j; ++i) { + f[j] = Math.max(f[j], f[i] + (j - i) * nums[j]); + } + } + return f[n - 1]; +} +``` + + + + + + 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 new file mode 100644 index 0000000000000..c7f720ca8534a --- /dev/null +++ b/solution/3200-3299/3205.Maximum Array Hopping Score I/README_EN.md @@ -0,0 +1,288 @@ +--- +comments: true +difficulty: Medium +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3205.Maximum%20Array%20Hopping%20Score%20I/README_EN.md +--- + + + +# [3205. Maximum Array Hopping Score I 🔒](https://leetcode.com/problems/maximum-array-hopping-score-i) + +[中文文档](/solution/3200-3299/3205.Maximum%20Array%20Hopping%20Score%20I/README.md) + +## Description + + + +

Given an array nums, you have to get the maximum score starting from index 0 and hopping until you reach the last element of the array.

+ +

In each hop, you can jump from index i to an index j > i, and you get a score of (j - i) * nums[j].

+ +

Return the maximum score you can get.

+ +

 

+

Example 1:

+ +
+

Input: nums = [1,5,8]

+ +

Output: 16

+ +

Explanation:

+ +

There are two possible ways to reach the last element:

+ +
    +
  • 0 -> 1 -> 2 with a score of (1 - 0) * 5 + (2 - 1) * 8 = 13.
  • +
  • 0 -> 2 with a score of (2 - 0) * 8 = 16.
  • +
+
+ +

Example 2:

+ +
+

Input: nums = [4,5,2,8,9,1,3]

+ +

Output: 42

+ +

Explanation:

+ +

We can do the hopping 0 -> 4 -> 6 with a score of (4 - 0) * 9 + (6 - 4) * 3 = 42.

+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= nums.length <= 103
  • +
  • 1 <= nums[i] <= 105
  • +
+ + + +## Solutions + + + +### Solution 1: Memoization Search + +We design a function $\text{dfs}(i)$, which represents the maximum score that can be obtained starting from index $i$. Therefore, the answer is $\text{dfs}(0)$. + +The execution process of the function $\text{dfs}(i)$ is as follows: + +We enumerate the next jump position $j$. Thus, the score that can be obtained starting from index $i$ is $(j - i) \times \text{nums}[j]$, plus the maximum score that can be obtained starting from index $j$, making the total score $(j - i) \times \text{nums}[j] + \text{dfs}(j)$. We enumerate all possible $j$ and take the maximum score. + +To avoid redundant calculations, we use memoization search. We save the calculated value of $\text{dfs}(i)$, so it can be directly returned next time. + +The time complexity is $O(n^2)$, 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: + @cache + def dfs(i: int) -> int: + return max( + [(j - i) * nums[j] + dfs(j) for j in range(i + 1, len(nums))] or [0] + ) + + return dfs(0) +``` + +#### Java + +```java +class Solution { + private Integer[] f; + private int[] nums; + private int n; + + public int maxScore(int[] nums) { + n = nums.length; + f = new Integer[n]; + this.nums = nums; + return dfs(0); + } + + private int dfs(int i) { + if (f[i] != null) { + return f[i]; + } + f[i] = 0; + for (int j = i + 1; j < n; ++j) { + f[i] = Math.max(f[i], (j - i) * nums[j] + dfs(j)); + } + return f[i]; + } +} +``` + +#### C++ + +```cpp +class Solution { +public: + int maxScore(vector& nums) { + int n = nums.size(); + vector f(n); + auto dfs = [&](auto&& dfs, int i) -> int { + if (f[i]) { + return f[i]; + } + for (int j = i + 1; j < n; ++j) { + f[i] = max(f[i], (j - i) * nums[j] + dfs(dfs, j)); + } + return f[i]; + }; + return dfs(dfs, 0); + } +}; +``` + +#### Go + +```go +func maxScore(nums []int) int { + n := len(nums) + f := make([]int, n) + var dfs func(int) int + dfs = func(i int) int { + if f[i] > 0 { + return f[i] + } + for j := i + 1; j < n; j++ { + f[i] = max(f[i], (j-i)*nums[j]+dfs(j)) + } + return f[i] + } + return dfs(0) +} +``` + +#### TypeScript + +```ts +function maxScore(nums: number[]): number { + const n = nums.length; + const f: number[] = Array(n).fill(0); + const dfs = (i: number): number => { + if (f[i]) { + return f[i]; + } + for (let j = i + 1; j < n; ++j) { + f[i] = Math.max(f[i], (j - i) * nums[j] + dfs(j)); + } + return f[i]; + }; + return dfs(0); +} +``` + + + + + + + +### Solution 2: Dynamic Programming + +We can transform the memoization search from Solution 1 into dynamic programming. + +Define $f[j]$ as the maximum score that can be obtained starting from index $0$ and ending at index $j$. Therefore, the answer is $f[n - 1]$. + +The state transition equation is: + +$$ +f[j] = \max_{0 \leq i < j} \{ f[i] + (j - i) \times \text{nums}[j] \} +$$ + +The time complexity is $O(n^2)$, 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: + n = len(nums) + f = [0] * n + for j in range(1, n): + for i in range(j): + f[j] = max(f[j], f[i] + (j - i) * nums[j]) + return f[n - 1] +``` + +#### Java + +```java +class Solution { + public int maxScore(int[] nums) { + int n = nums.length; + int[] f = new int[n]; + for (int j = 1; j < n; ++j) { + for (int i = 0; i < j; ++i) { + f[j] = Math.max(f[j], f[i] + (j - i) * nums[j]); + } + } + return f[n - 1]; + } +} +``` + +#### C++ + +```cpp +class Solution { +public: + int maxScore(vector& nums) { + int n = nums.size(); + vector f(n); + for (int j = 1; j < n; ++j) { + for (int i = 0; i < j; ++i) { + f[j] = max(f[j], f[i] + (j - i) * nums[j]); + } + } + return f[n - 1]; + } +}; +``` + +#### Go + +```go +func maxScore(nums []int) int { + n := len(nums) + f := make([]int, n) + for j := 1; j < n; j++ { + for i := 0; i < j; i++ { + f[j] = max(f[j], f[i]+(j-i)*nums[j]) + } + } + return f[n-1] +} +``` + +#### TypeScript + +```ts +function maxScore(nums: number[]): number { + const n = nums.length; + const f: number[] = Array(n).fill(0); + for (let j = 1; j < n; ++j) { + for (let i = 0; i < j; ++i) { + f[j] = Math.max(f[j], f[i] + (j - i) * nums[j]); + } + } + return f[n - 1]; +} +``` + + + + + + diff --git a/solution/3200-3299/3205.Maximum Array Hopping Score I/Solution.cpp b/solution/3200-3299/3205.Maximum Array Hopping Score I/Solution.cpp new file mode 100644 index 0000000000000..69edd5d87fff1 --- /dev/null +++ b/solution/3200-3299/3205.Maximum Array Hopping Score I/Solution.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + int maxScore(vector& nums) { + int n = nums.size(); + vector f(n); + auto dfs = [&](auto&& dfs, int i) -> int { + if (f[i]) { + return f[i]; + } + for (int j = i + 1; j < n; ++j) { + f[i] = max(f[i], (j - i) * nums[j] + dfs(dfs, j)); + } + return f[i]; + }; + return dfs(dfs, 0); + } +}; \ No newline at end of file diff --git a/solution/3200-3299/3205.Maximum Array Hopping Score I/Solution.go b/solution/3200-3299/3205.Maximum Array Hopping Score I/Solution.go new file mode 100644 index 0000000000000..ea7f273d8ecd7 --- /dev/null +++ b/solution/3200-3299/3205.Maximum Array Hopping Score I/Solution.go @@ -0,0 +1,15 @@ +func maxScore(nums []int) int { + n := len(nums) + f := make([]int, n) + var dfs func(int) int + dfs = func(i int) int { + if f[i] > 0 { + return f[i] + } + for j := i + 1; j < n; j++ { + f[i] = max(f[i], (j-i)*nums[j]+dfs(j)) + } + return f[i] + } + return dfs(0) +} \ No newline at end of file diff --git a/solution/3200-3299/3205.Maximum Array Hopping Score I/Solution.java b/solution/3200-3299/3205.Maximum Array Hopping Score I/Solution.java new file mode 100644 index 0000000000000..1b7ef3a6f93d1 --- /dev/null +++ b/solution/3200-3299/3205.Maximum Array Hopping Score I/Solution.java @@ -0,0 +1,23 @@ +class Solution { + private Integer[] f; + private int[] nums; + private int n; + + public int maxScore(int[] nums) { + n = nums.length; + f = new Integer[n]; + this.nums = nums; + return dfs(0); + } + + private int dfs(int i) { + if (f[i] != null) { + return f[i]; + } + f[i] = 0; + for (int j = i + 1; j < n; ++j) { + f[i] = Math.max(f[i], (j - i) * nums[j] + dfs(j)); + } + return f[i]; + } +} \ No newline at end of file diff --git a/solution/3200-3299/3205.Maximum Array Hopping Score I/Solution.py b/solution/3200-3299/3205.Maximum Array Hopping Score I/Solution.py new file mode 100644 index 0000000000000..f5b58bcf0293d --- /dev/null +++ b/solution/3200-3299/3205.Maximum Array Hopping Score I/Solution.py @@ -0,0 +1,9 @@ +class Solution: + def maxScore(self, nums: List[int]) -> int: + @cache + def dfs(i: int) -> int: + return max( + [(j - i) * nums[j] + dfs(j) for j in range(i + 1, len(nums))] or [0] + ) + + return dfs(0) diff --git a/solution/3200-3299/3205.Maximum Array Hopping Score I/Solution.ts b/solution/3200-3299/3205.Maximum Array Hopping Score I/Solution.ts new file mode 100644 index 0000000000000..2df1725aecc56 --- /dev/null +++ b/solution/3200-3299/3205.Maximum Array Hopping Score I/Solution.ts @@ -0,0 +1,14 @@ +function maxScore(nums: number[]): number { + const n = nums.length; + const f: number[] = Array(n).fill(0); + const dfs = (i: number): number => { + if (f[i]) { + return f[i]; + } + for (let j = i + 1; j < n; ++j) { + f[i] = Math.max(f[i], (j - i) * nums[j] + dfs(j)); + } + return f[i]; + }; + return dfs(0); +} diff --git a/solution/3200-3299/3205.Maximum Array Hopping Score I/Solution2.cpp b/solution/3200-3299/3205.Maximum Array Hopping Score I/Solution2.cpp new file mode 100644 index 0000000000000..a9dca8a2fb2d1 --- /dev/null +++ b/solution/3200-3299/3205.Maximum Array Hopping Score I/Solution2.cpp @@ -0,0 +1,13 @@ +class Solution { +public: + int maxScore(vector& nums) { + int n = nums.size(); + vector f(n); + for (int j = 1; j < n; ++j) { + for (int i = 0; i < j; ++i) { + f[j] = max(f[j], f[i] + (j - i) * nums[j]); + } + } + return f[n - 1]; + } +}; \ No newline at end of file diff --git a/solution/3200-3299/3205.Maximum Array Hopping Score I/Solution2.go b/solution/3200-3299/3205.Maximum Array Hopping Score I/Solution2.go new file mode 100644 index 0000000000000..a4282a64617f6 --- /dev/null +++ b/solution/3200-3299/3205.Maximum Array Hopping Score I/Solution2.go @@ -0,0 +1,10 @@ +func maxScore(nums []int) int { + n := len(nums) + f := make([]int, n) + for j := 1; j < n; j++ { + for i := 0; i < j; i++ { + f[j] = max(f[j], f[i]+(j-i)*nums[j]) + } + } + return f[n-1] +} \ No newline at end of file diff --git a/solution/3200-3299/3205.Maximum Array Hopping Score I/Solution2.java b/solution/3200-3299/3205.Maximum Array Hopping Score I/Solution2.java new file mode 100644 index 0000000000000..36047cdc61596 --- /dev/null +++ b/solution/3200-3299/3205.Maximum Array Hopping Score I/Solution2.java @@ -0,0 +1,12 @@ +class Solution { + public int maxScore(int[] nums) { + int n = nums.length; + int[] f = new int[n]; + for (int j = 1; j < n; ++j) { + for (int i = 0; i < j; ++i) { + f[j] = Math.max(f[j], f[i] + (j - i) * nums[j]); + } + } + return f[n - 1]; + } +} \ No newline at end of file diff --git a/solution/3200-3299/3205.Maximum Array Hopping Score I/Solution2.py b/solution/3200-3299/3205.Maximum Array Hopping Score I/Solution2.py new file mode 100644 index 0000000000000..c9f1f06f96aad --- /dev/null +++ b/solution/3200-3299/3205.Maximum Array Hopping Score I/Solution2.py @@ -0,0 +1,8 @@ +class Solution: + def maxScore(self, nums: List[int]) -> int: + n = len(nums) + f = [0] * n + for j in range(1, n): + for i in range(j): + f[j] = max(f[j], f[i] + (j - i) * nums[j]) + return f[n - 1] diff --git a/solution/3200-3299/3205.Maximum Array Hopping Score I/Solution2.ts b/solution/3200-3299/3205.Maximum Array Hopping Score I/Solution2.ts new file mode 100644 index 0000000000000..f4b69806e5e5f --- /dev/null +++ b/solution/3200-3299/3205.Maximum Array Hopping Score I/Solution2.ts @@ -0,0 +1,10 @@ +function maxScore(nums: number[]): number { + const n = nums.length; + const f: number[] = Array(n).fill(0); + for (let j = 1; j < n; ++j) { + for (let i = 0; i < j; ++i) { + f[j] = Math.max(f[j], f[i] + (j - i) * nums[j]); + } + } + return f[n - 1]; +} diff --git a/solution/README.md b/solution/README.md index b5919310582be..7417457340a65 100644 --- a/solution/README.md +++ b/solution/README.md @@ -3215,6 +3215,7 @@ | 3202 | [找出有效子序列的最大长度 II](/solution/3200-3299/3202.Find%20the%20Maximum%20Length%20of%20Valid%20Subsequence%20II/README.md) | `数组`,`动态规划` | 中等 | 第 404 场周赛 | | 3203 | [合并两棵树后的最小直径](/solution/3200-3299/3203.Find%20Minimum%20Diameter%20After%20Merging%20Two%20Trees/README.md) | `树`,`深度优先搜索`,`广度优先搜索`,`图` | 困难 | 第 404 场周赛 | | 3204 | [按位用户权限分析](/solution/3200-3299/3204.Bitwise%20User%20Permissions%20Analysis/README.md) | `数据库` | 中等 | 🔒 | +| 3205 | [Maximum Array Hopping Score I](/solution/3200-3299/3205.Maximum%20Array%20Hopping%20Score%20I/README.md) | | 中等 | 🔒 | ## 版权 diff --git a/solution/README_EN.md b/solution/README_EN.md index 9e444079ca766..91a4e5e61a125 100644 --- a/solution/README_EN.md +++ b/solution/README_EN.md @@ -3213,6 +3213,7 @@ Press Control + F(or Command + F on | 3202 | [Find the Maximum Length of Valid Subsequence II](/solution/3200-3299/3202.Find%20the%20Maximum%20Length%20of%20Valid%20Subsequence%20II/README_EN.md) | `Array`,`Dynamic Programming` | Medium | Weekly Contest 404 | | 3203 | [Find Minimum Diameter After Merging Two Trees](/solution/3200-3299/3203.Find%20Minimum%20Diameter%20After%20Merging%20Two%20Trees/README_EN.md) | `Tree`,`Depth-First Search`,`Breadth-First Search`,`Graph` | Hard | Weekly Contest 404 | | 3204 | [Bitwise User Permissions Analysis](/solution/3200-3299/3204.Bitwise%20User%20Permissions%20Analysis/README_EN.md) | `Database` | Medium | 🔒 | +| 3205 | [Maximum Array Hopping Score I](/solution/3200-3299/3205.Maximum%20Array%20Hopping%20Score%20I/README_EN.md) | | Medium | 🔒 | ## Copyright