cs: add «2554. Maximum Number of Integers to Choose From a Range I»

URL:	https://leetcode.com/problems/maximum-number-of-integers-to-choose-from-a-range-i/
Signed-off-by: Matej Focko <me@mfocko.xyz>
This commit is contained in:
Matej Focko 2024-12-06 22:57:34 +01:00
parent 97dc8014b2
commit 90a2ad8171
Signed by: mfocko
SSH key fingerprint: SHA256:icm0fIOSJUpy5+1x23sfr+hLtF9UhY8VpMC7H4WFJP8

View file

@ -0,0 +1,20 @@
public class Solution {
public int MaxCount(int[] banned, int n, int maxSum) {
Array.Sort(banned);
int idx = 0, used = 0;
for (int i = 1; i <= n && maxSum > 0; ++i) {
var skip = idx < banned.Length && banned[idx] == i;
while (idx < banned.Length && banned[idx] == i) {
++idx;
}
if (!skip && maxSum - i >= 0) {
maxSum -= i;
++used;
}
}
return used;
}
}