2600. K Items With the Maximum Sum LeetCode Solution

In this guide, you will get 2600. K Items With the Maximum Sum LeetCode Solution with the best time and space complexity. The solution to K Items With the Maximum Sum problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. K Items With the Maximum Sum solution in C++
  4. K Items With the Maximum Sum solution in Java
  5. K Items With the Maximum Sum solution in Python
  6. Additional Resources
2600. K Items With the Maximum Sum LeetCode Solution image

Problem Statement of K Items With the Maximum Sum

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.

See also  1942. The Number of the Smallest Unoccupied Chair LeetCode Solution

Constraints:

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

Complexity Analysis

  • Time Complexity: O(1)
  • Space Complexity: O(1)

2600. K Items With the Maximum Sum LeetCode Solution in C++

class Solution {
 public:
  int kItemsWithMaximumSum(int numOnes, int numZeros, int numNegOnes, int k) {
    if (k <= numOnes)
      return k;
    if (k <= numOnes + numZeros)
      return numOnes;
    return numOnes - (k - numOnes - numZeros);
  }
};
/* code provided by PROGIEZ */

2600. K Items With the Maximum Sum LeetCode Solution in Java

class Solution {
  public int kItemsWithMaximumSum(int numOnes, int numZeros, int numNegOnes, int k) {
    if (k <= numOnes)
      return k;
    if (k <= numOnes + numZeros)
      return numOnes;
    return numOnes - (k - numOnes - numZeros);
  }
}
// code provided by PROGIEZ

2600. K Items With the Maximum Sum LeetCode Solution in Python

class Solution:
  def kItemsWithMaximumSum(
      self,
      numOnes: int,
      numZeros: int,
      numNegOnes: int,
      k: int,
  ) -> int:
    if k <= numOnes:
      return k
    if k <= numOnes + numZeros:
      return numOnes
    return numOnes - (k - numOnes - numZeros)
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.