Skip to content

Practice K Items With the Maximum Sum

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Easy#2600
LeetCode Problem

K Items With the Maximum Sum

Problem Statement

A bag has numOnes items valued 1, numZeros items valued 0 and numNegOnes items valued -1. Select exactly k available items and return the largest possible sum of their values.

Source: K Items With the Maximum Sum on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
numOnes = 3, numZeros = 2, numNegOnes = 0, k = 2
Output
2

# Example 2

Input
numOnes = 3, numZeros = 2, numNegOnes = 0, k = 4
Output
3

Constraints

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

Related LeetCode Problems

Practice more problems similar to K Items With the Maximum Sum.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.