Skip to content

Commit 3d5f03f

Browse files
authored
feat: update solutions to lc problem: No.2786 (doocs#3096)
No.2786.Visit Array Positions to Maximize Score
1 parent b5c0a18 commit 3d5f03f

File tree

7 files changed

+54
-26
lines changed

7 files changed

+54
-26
lines changed

solution/2700-2799/2786.Visit Array Positions to Maximize Score/README.md

+22-9
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,18 @@ tags:
6767

6868
<!-- solution:start -->
6969

70-
### 方法一
70+
### 方法一:动态规划
71+
72+
根据题目描述,我们可以得到以下结论:
73+
74+
1. 从位置 $i$ 移动到位置 $j$ 时,如果 $nums[i]$ 和 $nums[j]$ 的奇偶性不同,那么会损失 $x$ 分;
75+
2. 从位置 $i$ 移动到位置 $j$ 时,如果 $nums[i]$ 和 $nums[j]$ 的奇偶性相同,那么不会损失分数。
76+
77+
因此,我们可以用一个长度为 $2$ 的数组 $f$ 来表示当前位置的奇偶性为 $0$ 和 $1$ 时的最大得分。初始时 $f$ 的值为 $-\infty$,然后我们再初始化 $f[nums[0] \& 1] = nums[0]$,表示初始位置的得分。
78+
79+
接下来,我们从位置 $1$ 开始遍历数组 $nums$,对于每个位置 $i$ 对应的值 $v$,我们更新 $f[v \& 1]$ 的值为 $f[v \& 1]$ 和 $f[v \& 1 \oplus 1] - x$ 的较大值再加上 $v$,即 $f[v \& 1] = \max(f[v \& 1], f[v \& 1 \oplus 1] - x) + v$。
80+
81+
答案为 $f[0]$ 和 $f[1]$ 中的较大值。
7182

7283
<!-- tabs:start -->
7384

@@ -79,7 +90,7 @@ class Solution:
7990
f = [-inf] * 2
8091
f[nums[0] & 1] = nums[0]
8192
for v in nums[1:]:
82-
f[v & 1] = max(f[v & 1] + v, f[v & 1 ^ 1] + v - x)
93+
f[v & 1] = max(f[v & 1], f[v & 1 ^ 1] - x) + v
8394
return max(f)
8495
```
8596

@@ -92,7 +103,8 @@ class Solution {
92103
Arrays.fill(f, -(1L << 60));
93104
f[nums[0] & 1] = nums[0];
94105
for (int i = 1; i < nums.length; ++i) {
95-
f[nums[i] & 1] = Math.max(f[nums[i] & 1] + nums[i], f[nums[i] & 1 ^ 1] + nums[i] - x);
106+
int v = nums[i];
107+
f[v & 1] = Math.max(f[v & 1], f[v & 1 ^ 1] - x) + v;
96108
}
97109
return Math.max(f[0], f[1]);
98110
}
@@ -110,7 +122,8 @@ public:
110122
f[nums[0] & 1] = nums[0];
111123
int n = nums.size();
112124
for (int i = 1; i < n; ++i) {
113-
f[nums[i] & 1] = max(f[nums[i] & 1] + nums[i], f[nums[i] & 1 ^ 1] + nums[i] - x);
125+
int v = nums[i];
126+
f[v & 1] = max(f[v & 1], f[v & 1 ^ 1] - x) + v;
114127
}
115128
return max(f[0], f[1]);
116129
}
@@ -125,7 +138,7 @@ func maxScore(nums []int, x int) int64 {
125138
f := [2]int{-inf, -inf}
126139
f[nums[0]&1] = nums[0]
127140
for _, v := range nums[1:] {
128-
f[v&1] = max(f[v&1]+v, f[v&1^1]+v-x)
141+
f[v&1] = max(f[v&1], f[v&1^1]-x) + v
129142
}
130143
return int64(max(f[0], f[1]))
131144
}
@@ -135,13 +148,13 @@ func maxScore(nums []int, x int) int64 {
135148

136149
```ts
137150
function maxScore(nums: number[], x: number): number {
138-
const inf = 1 << 30;
139-
const f: number[] = Array(2).fill(-inf);
151+
const f: number[] = Array(2).fill(-Infinity);
140152
f[nums[0] & 1] = nums[0];
141153
for (let i = 1; i < nums.length; ++i) {
142-
f[nums[i] & 1] = Math.max(f[nums[i] & 1] + nums[i], f[(nums[i] & 1) ^ 1] + nums[i] - x);
154+
const v = nums[i];
155+
f[v & 1] = Math.max(f[v & 1], f[(v & 1) ^ 1] - x) + v;
143156
}
144-
return Math.max(f[0], f[1]);
157+
return Math.max(...f);
145158
}
146159
```
147160

solution/2700-2799/2786.Visit Array Positions to Maximize Score/README_EN.md

+22-9
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,18 @@ The total score is: 2 + 4 + 6 + 8 = 20.
6767

6868
<!-- solution:start -->
6969

70-
### Solution 1
70+
### Solution 1: Dynamic Programming
71+
72+
Based on the problem description, we can draw the following conclusions:
73+
74+
1. Moving from position $i$ to position $j$, if $nums[i]$ and $nums[j]$ have different parities, then $x$ points will be lost;
75+
2. Moving from position $i$ to position $j$, if $nums[i]$ and $nums[j]$ have the same parity, then no points will be lost.
76+
77+
Therefore, we can use an array $f$ of length $2$ to represent the maximum score when the current position's parity is $0$ and $1$. Initially, the values of $f$ are $-\infty$, and then we initialize $f[nums[0] \& 1] = nums[0]$, indicating the score at the initial position.
78+
79+
Next, we start traversing the array $nums$ from position $1$. For each position $i$ corresponding to the value $v$, we update the value of $f[v \& 1]$ to be the larger value between $f[v \& 1]$ and $f[v \& 1 \oplus 1] - x$ plus $v$, i.e., $f[v \& 1] = \max(f[v \& 1], f[v \& 1 \oplus 1] - x) + v$.
80+
81+
The answer is the larger value between $f[0]$ and $f[1]$.
7182

7283
<!-- tabs:start -->
7384

@@ -79,7 +90,7 @@ class Solution:
7990
f = [-inf] * 2
8091
f[nums[0] & 1] = nums[0]
8192
for v in nums[1:]:
82-
f[v & 1] = max(f[v & 1] + v, f[v & 1 ^ 1] + v - x)
93+
f[v & 1] = max(f[v & 1], f[v & 1 ^ 1] - x) + v
8394
return max(f)
8495
```
8596

@@ -92,7 +103,8 @@ class Solution {
92103
Arrays.fill(f, -(1L << 60));
93104
f[nums[0] & 1] = nums[0];
94105
for (int i = 1; i < nums.length; ++i) {
95-
f[nums[i] & 1] = Math.max(f[nums[i] & 1] + nums[i], f[nums[i] & 1 ^ 1] + nums[i] - x);
106+
int v = nums[i];
107+
f[v & 1] = Math.max(f[v & 1], f[v & 1 ^ 1] - x) + v;
96108
}
97109
return Math.max(f[0], f[1]);
98110
}
@@ -110,7 +122,8 @@ public:
110122
f[nums[0] & 1] = nums[0];
111123
int n = nums.size();
112124
for (int i = 1; i < n; ++i) {
113-
f[nums[i] & 1] = max(f[nums[i] & 1] + nums[i], f[nums[i] & 1 ^ 1] + nums[i] - x);
125+
int v = nums[i];
126+
f[v & 1] = max(f[v & 1], f[v & 1 ^ 1] - x) + v;
114127
}
115128
return max(f[0], f[1]);
116129
}
@@ -125,7 +138,7 @@ func maxScore(nums []int, x int) int64 {
125138
f := [2]int{-inf, -inf}
126139
f[nums[0]&1] = nums[0]
127140
for _, v := range nums[1:] {
128-
f[v&1] = max(f[v&1]+v, f[v&1^1]+v-x)
141+
f[v&1] = max(f[v&1], f[v&1^1]-x) + v
129142
}
130143
return int64(max(f[0], f[1]))
131144
}
@@ -135,13 +148,13 @@ func maxScore(nums []int, x int) int64 {
135148

136149
```ts
137150
function maxScore(nums: number[], x: number): number {
138-
const inf = 1 << 30;
139-
const f: number[] = Array(2).fill(-inf);
151+
const f: number[] = Array(2).fill(-Infinity);
140152
f[nums[0] & 1] = nums[0];
141153
for (let i = 1; i < nums.length; ++i) {
142-
f[nums[i] & 1] = Math.max(f[nums[i] & 1] + nums[i], f[(nums[i] & 1) ^ 1] + nums[i] - x);
154+
const v = nums[i];
155+
f[v & 1] = Math.max(f[v & 1], f[(v & 1) ^ 1] - x) + v;
143156
}
144-
return Math.max(f[0], f[1]);
157+
return Math.max(...f);
145158
}
146159
```
147160

solution/2700-2799/2786.Visit Array Positions to Maximize Score/Solution.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ class Solution {
66
f[nums[0] & 1] = nums[0];
77
int n = nums.size();
88
for (int i = 1; i < n; ++i) {
9-
f[nums[i] & 1] = max(f[nums[i] & 1] + nums[i], f[nums[i] & 1 ^ 1] + nums[i] - x);
9+
int v = nums[i];
10+
f[v & 1] = max(f[v & 1], f[v & 1 ^ 1] - x) + v;
1011
}
1112
return max(f[0], f[1]);
1213
}

solution/2700-2799/2786.Visit Array Positions to Maximize Score/Solution.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ func maxScore(nums []int, x int) int64 {
33
f := [2]int{-inf, -inf}
44
f[nums[0]&1] = nums[0]
55
for _, v := range nums[1:] {
6-
f[v&1] = max(f[v&1]+v, f[v&1^1]+v-x)
6+
f[v&1] = max(f[v&1], f[v&1^1]-x) + v
77
}
88
return int64(max(f[0], f[1]))
99
}

solution/2700-2799/2786.Visit Array Positions to Maximize Score/Solution.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ public long maxScore(int[] nums, int x) {
44
Arrays.fill(f, -(1L << 60));
55
f[nums[0] & 1] = nums[0];
66
for (int i = 1; i < nums.length; ++i) {
7-
f[nums[i] & 1] = Math.max(f[nums[i] & 1] + nums[i], f[nums[i] & 1 ^ 1] + nums[i] - x);
7+
int v = nums[i];
8+
f[v & 1] = Math.max(f[v & 1], f[v & 1 ^ 1] - x) + v;
89
}
910
return Math.max(f[0], f[1]);
1011
}

solution/2700-2799/2786.Visit Array Positions to Maximize Score/Solution.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ def maxScore(self, nums: List[int], x: int) -> int:
33
f = [-inf] * 2
44
f[nums[0] & 1] = nums[0]
55
for v in nums[1:]:
6-
f[v & 1] = max(f[v & 1] + v, f[v & 1 ^ 1] + v - x)
6+
f[v & 1] = max(f[v & 1], f[v & 1 ^ 1] - x) + v
77
return max(f)
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
function maxScore(nums: number[], x: number): number {
2-
const inf = 1 << 30;
3-
const f: number[] = Array(2).fill(-inf);
2+
const f: number[] = Array(2).fill(-Infinity);
43
f[nums[0] & 1] = nums[0];
54
for (let i = 1; i < nums.length; ++i) {
6-
f[nums[i] & 1] = Math.max(f[nums[i] & 1] + nums[i], f[(nums[i] & 1) ^ 1] + nums[i] - x);
5+
const v = nums[i];
6+
f[v & 1] = Math.max(f[v & 1], f[(v & 1) ^ 1] - x) + v;
77
}
8-
return Math.max(f[0], f[1]);
8+
return Math.max(...f);
99
}

0 commit comments

Comments
 (0)