返回介绍

solution / 2600-2699 / 2600.K Items With the Maximum Sum / README_EN

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

2600. K Items With the Maximum Sum

中文文档

Description

There is a bag that consists of items, each item has a number 1, 0, or -1 written on it.

You are given four non-negative integers numOnes, numZeros, numNegOnes, and k.

The bag initially contains:

  • numOnes items with 1s written on them.
  • numZeroes items with 0s written on them.
  • numNegOnes items with -1s written on them.

We want to pick exactly k items among the available items. Return _the maximum possible sum of numbers written on the items_.

 

Example 1:

Input: numOnes = 3, numZeros = 2, numNegOnes = 0, k = 2
Output: 2
Explanation: We have a bag of items with numbers written on them {1, 1, 1, 0, 0}. We take 2 items with 1 written on them and get a sum in a total of 2.
It can be proven that 2 is the maximum possible sum.

Example 2:

Input: numOnes = 3, numZeros = 2, numNegOnes = 0, k = 4
Output: 3
Explanation: We have a bag of items with numbers written on them {1, 1, 1, 0, 0}. We take 3 items with 1 written on them, and 1 item with 0 written on it, and get a sum in a total of 3.
It can be proven that 3 is the maximum possible sum.

 

Constraints:

  • 0 <= numOnes, numZeros, numNegOnes <= 50
  • 0 <= k <= numOnes + numZeros + numNegOnes

Solutions

Solution 1

class Solution:
  def kItemsWithMaximumSum(
    self, numOnes: int, numZeros: int, numNegOnes: int, k: int
  ) -> int:
    if numOnes >= k:
      return k
    if numZeros >= k - numOnes:
      return numOnes
    return numOnes - (k - numOnes - numZeros)
class Solution {
  public int kItemsWithMaximumSum(int numOnes, int numZeros, int numNegOnes, int k) {
    if (numOnes >= k) {
      return k;
    }
    if (numZeros >= k - numOnes) {
      return numOnes;
    }
    return numOnes - (k - numOnes - numZeros);
  }
}
class Solution {
public:
  int kItemsWithMaximumSum(int numOnes, int numZeros, int numNegOnes, int k) {
    if (numOnes >= k) {
      return k;
    }
    if (numZeros >= k - numOnes) {
      return numOnes;
    }
    return numOnes - (k - numOnes - numZeros);
  }
};
func kItemsWithMaximumSum(numOnes int, numZeros int, numNegOnes int, k int) int {
  if numOnes >= k {
    return k
  }
  if numZeros >= k-numOnes {
    return numOnes
  }
  return numOnes - (k - numOnes - numZeros)
}
function kItemsWithMaximumSum(
  numOnes: number,
  numZeros: number,
  numNegOnes: number,
  k: number,
): number {
  if (numOnes >= k) {
    return k;
  }
  if (numZeros >= k - numOnes) {
    return numOnes;
  }
  return numOnes - (k - numOnes - numZeros);
}
impl Solution {
  pub fn k_items_with_maximum_sum(
    num_ones: i32,
    num_zeros: i32,
    num_neg_ones: i32,
    k: i32
  ) -> i32 {
    if num_ones > k {
      return k;
    }

    if num_ones + num_zeros > k {
      return num_ones;
    }

    num_ones - (k - num_ones - num_zeros)
  }
}
public class Solution {
  public int KItemsWithMaximumSum(int numOnes, int numZeros, int numNegOnes, int k) {
    if (numOnes >= k) {
      return k;
    }
    if (numZeros >= k - numOnes) {
      return numOnes;
    }
    return numOnes - (k - numOnes - numZeros);
  }
}

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

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

发布评论

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