返回介绍

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

发布于 2024-06-17 01:03:00 字数 4242 浏览 0 评论 0 收藏 0

2786. Visit Array Positions to Maximize Score

中文文档

Description

You are given a 0-indexed integer array nums and a positive integer x.

You are initially at position 0 in the array and you can visit other positions according to the following rules:

  • If you are currently in position i, then you can move to any position j such that i < j.
  • For each position i that you visit, you get a score of nums[i].
  • If you move from a position i to a position j and the parities of nums[i] and nums[j] differ, then you lose a score of x.

Return _the maximum total score you can get_.

Note that initially you have nums[0] points.

 

Example 1:

Input: nums = [2,3,6,1,9,2], x = 5
Output: 13
Explanation: We can visit the following positions in the array: 0 -> 2 -> 3 -> 4.
The corresponding values are 2, 6, 1 and 9. Since the integers 6 and 1 have different parities, the move 2 -> 3 will make you lose a score of x = 5.
The total score will be: 2 + 6 + 1 + 9 - 5 = 13.

Example 2:

Input: nums = [2,4,6,8], x = 3
Output: 20
Explanation: All the integers in the array have the same parities, so we can visit all of them without losing any score.
The total score is: 2 + 4 + 6 + 8 = 20.

 

Constraints:

  • 2 <= nums.length <= 105
  • 1 <= nums[i], x <= 106

Solutions

Solution 1

class Solution:
  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)
    return max(f)
class Solution {
  public long maxScore(int[] nums, int x) {
    long[] f = new long[2];
    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);
    }
    return Math.max(f[0], f[1]);
  }
}
class Solution {
public:
  long long maxScore(vector<int>& nums, int x) {
    const long long inf = 1LL << 60;
    vector<long long> f(2, -inf);
    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);
    }
    return max(f[0], f[1]);
  }
};
func maxScore(nums []int, x int) int64 {
  const inf int = 1 << 40
  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)
  }
  return int64(max(f[0], f[1]))
}
function maxScore(nums: number[], x: number): number {
  const inf = 1 << 30;
  const f: number[] = Array(2).fill(-inf);
  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);
  }
  return Math.max(f[0], f[1]);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文