Skip to content

Commit ed0d722

Browse files
solves #2600: K Items With the Maximum Sum in java
1 parent 9e449f7 commit ed0d722

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -809,7 +809,7 @@
809809
| 2586 | [Count the Number of Vowel Strings in Range](https://leetcode.com/problems/count-the-number-of-vowel-strings-in-range) | [![Java](assets/java.png)](src/CountTheNumberOfVowelStringsInRange.java) | |
810810
| 2591 | [Distribute Money to Maximum Children](https://leetcode.com/problems/distribute-money-to-maximum-children) | [![Java](assets/java.png)](src/DistributeMoneyToMaximumChildren.java) | |
811811
| 2595 | [Number of Even and Odd Bits](https://leetcode.com/problems/number-of-even-and-odd-bits) | [![Java](assets/java.png)](src/NumberOfEvenAndOddBits.java) | |
812-
| 2600 | [K Items With the Maximum Sum](https://leetcode.com/problems/k-items-with-the-maximum-sum) | | |
812+
| 2600 | [K Items With the Maximum Sum](https://leetcode.com/problems/k-items-with-the-maximum-sum) | [![Java](assets/java.png)](src/KItemsWithTheMaximumSum.java) | |
813813
| 2605 | [Form Smallest Number From Two Digit Arrays](https://leetcode.com/problems/form-smallest-number-from-two-digit-arrays) | | |
814814
| 2605 | [Form Smallest Number From Two Digit Arrays](https://leetcode.com/problems/form-smallest-number-from-two-digit-arrays) | | |
815815
| 2609 | [Find the Longest Balanced Substring of a Binary String](https://leetcode.com/problems/find-the-longest-balanced-substring-of-a-binary-string) | | |

src/KItemsWithTheMaximumSum.java

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// https://leetcode.com/problems/k-items-with-the-maximum-sum
2+
// T: O(1)
3+
// S: O(1)
4+
5+
public class KItemsWithTheMaximumSum {
6+
public int kItemsWithMaximumSum(int numOnes, int numZeros, int numNegOnes, int k) {
7+
int maxSum = Math.min(numOnes, k);
8+
k -= numOnes;
9+
if (k <= 0) return maxSum;
10+
11+
k -= numZeros;
12+
if (k <= 0) return maxSum;
13+
14+
return maxSum - k;
15+
}
16+
}

0 commit comments

Comments
 (0)