Skip to content

feat: update solutions to lc problem: No.2786 #3096

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
merged 1 commit into from
Jun 12, 2024
Merged
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
Original file line number Diff line number Diff line change
@@ -67,7 +67,18 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:动态规划

根据题目描述,我们可以得到以下结论:

1. 从位置 $i$ 移动到位置 $j$ 时,如果 $nums[i]$ 和 $nums[j]$ 的奇偶性不同,那么会损失 $x$ 分;
2. 从位置 $i$ 移动到位置 $j$ 时,如果 $nums[i]$ 和 $nums[j]$ 的奇偶性相同,那么不会损失分数。

因此,我们可以用一个长度为 $2$ 的数组 $f$ 来表示当前位置的奇偶性为 $0$ 和 $1$ 时的最大得分。初始时 $f$ 的值为 $-\infty$,然后我们再初始化 $f[nums[0] \& 1] = nums[0]$,表示初始位置的得分。

接下来,我们从位置 $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$。

答案为 $f[0]$ 和 $f[1]$ 中的较大值。

<!-- tabs:start -->

@@ -79,7 +90,7 @@ class Solution:
f = [-inf] * 2
f[nums[0] & 1] = nums[0]
for v in nums[1:]:
f[v & 1] = max(f[v & 1] + v, f[v & 1 ^ 1] + v - x)
f[v & 1] = max(f[v & 1], f[v & 1 ^ 1] - x) + v
return max(f)
```

@@ -92,7 +103,8 @@ class Solution {
Arrays.fill(f, -(1L << 60));
f[nums[0] & 1] = nums[0];
for (int i = 1; i < nums.length; ++i) {
f[nums[i] & 1] = Math.max(f[nums[i] & 1] + nums[i], f[nums[i] & 1 ^ 1] + nums[i] - x);
int v = nums[i];
f[v & 1] = Math.max(f[v & 1], f[v & 1 ^ 1] - x) + v;
}
return Math.max(f[0], f[1]);
}
@@ -110,7 +122,8 @@ public:
f[nums[0] & 1] = nums[0];
int n = nums.size();
for (int i = 1; i < n; ++i) {
f[nums[i] & 1] = max(f[nums[i] & 1] + nums[i], f[nums[i] & 1 ^ 1] + nums[i] - x);
int v = nums[i];
f[v & 1] = max(f[v & 1], f[v & 1 ^ 1] - x) + v;
}
return max(f[0], f[1]);
}
@@ -125,7 +138,7 @@ func maxScore(nums []int, x int) int64 {
f := [2]int{-inf, -inf}
f[nums[0]&1] = nums[0]
for _, v := range nums[1:] {
f[v&1] = max(f[v&1]+v, f[v&1^1]+v-x)
f[v&1] = max(f[v&1], f[v&1^1]-x) + v
}
return int64(max(f[0], f[1]))
}
@@ -135,13 +148,13 @@ func maxScore(nums []int, x int) int64 {

```ts
function maxScore(nums: number[], x: number): number {
const inf = 1 << 30;
const f: number[] = Array(2).fill(-inf);
const f: number[] = Array(2).fill(-Infinity);
f[nums[0] & 1] = nums[0];
for (let i = 1; i < nums.length; ++i) {
f[nums[i] & 1] = Math.max(f[nums[i] & 1] + nums[i], f[(nums[i] & 1) ^ 1] + nums[i] - x);
const v = nums[i];
f[v & 1] = Math.max(f[v & 1], f[(v & 1) ^ 1] - x) + v;
}
return Math.max(f[0], f[1]);
return Math.max(...f);
}
```

Original file line number Diff line number Diff line change
@@ -67,7 +67,18 @@ The total score is: 2 + 4 + 6 + 8 = 20.

<!-- solution:start -->

### Solution 1
### Solution 1: Dynamic Programming

Based on the problem description, we can draw the following conclusions:

1. Moving from position $i$ to position $j$, if $nums[i]$ and $nums[j]$ have different parities, then $x$ points will be lost;
2. Moving from position $i$ to position $j$, if $nums[i]$ and $nums[j]$ have the same parity, then no points will be lost.

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.

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$.

The answer is the larger value between $f[0]$ and $f[1]$.

<!-- tabs:start -->

@@ -79,7 +90,7 @@ class Solution:
f = [-inf] * 2
f[nums[0] & 1] = nums[0]
for v in nums[1:]:
f[v & 1] = max(f[v & 1] + v, f[v & 1 ^ 1] + v - x)
f[v & 1] = max(f[v & 1], f[v & 1 ^ 1] - x) + v
return max(f)
```

@@ -92,7 +103,8 @@ class Solution {
Arrays.fill(f, -(1L << 60));
f[nums[0] & 1] = nums[0];
for (int i = 1; i < nums.length; ++i) {
f[nums[i] & 1] = Math.max(f[nums[i] & 1] + nums[i], f[nums[i] & 1 ^ 1] + nums[i] - x);
int v = nums[i];
f[v & 1] = Math.max(f[v & 1], f[v & 1 ^ 1] - x) + v;
}
return Math.max(f[0], f[1]);
}
@@ -110,7 +122,8 @@ public:
f[nums[0] & 1] = nums[0];
int n = nums.size();
for (int i = 1; i < n; ++i) {
f[nums[i] & 1] = max(f[nums[i] & 1] + nums[i], f[nums[i] & 1 ^ 1] + nums[i] - x);
int v = nums[i];
f[v & 1] = max(f[v & 1], f[v & 1 ^ 1] - x) + v;
}
return max(f[0], f[1]);
}
@@ -125,7 +138,7 @@ func maxScore(nums []int, x int) int64 {
f := [2]int{-inf, -inf}
f[nums[0]&1] = nums[0]
for _, v := range nums[1:] {
f[v&1] = max(f[v&1]+v, f[v&1^1]+v-x)
f[v&1] = max(f[v&1], f[v&1^1]-x) + v
}
return int64(max(f[0], f[1]))
}
@@ -135,13 +148,13 @@ func maxScore(nums []int, x int) int64 {

```ts
function maxScore(nums: number[], x: number): number {
const inf = 1 << 30;
const f: number[] = Array(2).fill(-inf);
const f: number[] = Array(2).fill(-Infinity);
f[nums[0] & 1] = nums[0];
for (let i = 1; i < nums.length; ++i) {
f[nums[i] & 1] = Math.max(f[nums[i] & 1] + nums[i], f[(nums[i] & 1) ^ 1] + nums[i] - x);
const v = nums[i];
f[v & 1] = Math.max(f[v & 1], f[(v & 1) ^ 1] - x) + v;
}
return Math.max(f[0], f[1]);
return Math.max(...f);
}
```

Original file line number Diff line number Diff line change
@@ -6,7 +6,8 @@ class Solution {
f[nums[0] & 1] = nums[0];
int n = nums.size();
for (int i = 1; i < n; ++i) {
f[nums[i] & 1] = max(f[nums[i] & 1] + nums[i], f[nums[i] & 1 ^ 1] + nums[i] - x);
int v = nums[i];
f[v & 1] = max(f[v & 1], f[v & 1 ^ 1] - x) + v;
}
return max(f[0], f[1]);
}
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ func maxScore(nums []int, x int) int64 {
f := [2]int{-inf, -inf}
f[nums[0]&1] = nums[0]
for _, v := range nums[1:] {
f[v&1] = max(f[v&1]+v, f[v&1^1]+v-x)
f[v&1] = max(f[v&1], f[v&1^1]-x) + v
}
return int64(max(f[0], f[1]))
}
Original file line number Diff line number Diff line change
@@ -4,7 +4,8 @@ public long maxScore(int[] nums, int x) {
Arrays.fill(f, -(1L << 60));
f[nums[0] & 1] = nums[0];
for (int i = 1; i < nums.length; ++i) {
f[nums[i] & 1] = Math.max(f[nums[i] & 1] + nums[i], f[nums[i] & 1 ^ 1] + nums[i] - x);
int v = nums[i];
f[v & 1] = Math.max(f[v & 1], f[v & 1 ^ 1] - x) + v;
}
return Math.max(f[0], f[1]);
}
Original file line number Diff line number Diff line change
@@ -3,5 +3,5 @@ def maxScore(self, nums: List[int], x: int) -> int:
f = [-inf] * 2
f[nums[0] & 1] = nums[0]
for v in nums[1:]:
f[v & 1] = max(f[v & 1] + v, f[v & 1 ^ 1] + v - x)
f[v & 1] = max(f[v & 1], f[v & 1 ^ 1] - x) + v
return max(f)
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
function maxScore(nums: number[], x: number): number {
const inf = 1 << 30;
const f: number[] = Array(2).fill(-inf);
const f: number[] = Array(2).fill(-Infinity);
f[nums[0] & 1] = nums[0];
for (let i = 1; i < nums.length; ++i) {
f[nums[i] & 1] = Math.max(f[nums[i] & 1] + nums[i], f[(nums[i] & 1) ^ 1] + nums[i] - x);
const v = nums[i];
f[v & 1] = Math.max(f[v & 1], f[(v & 1) ^ 1] - x) + v;
}
return Math.max(f[0], f[1]);
return Math.max(...f);
}