diff --git a/cs/maximum-number-of-integers-to-choose-from-a-range-i.cs b/cs/maximum-number-of-integers-to-choose-from-a-range-i.cs new file mode 100644 index 0000000..f1e364a --- /dev/null +++ b/cs/maximum-number-of-integers-to-choose-from-a-range-i.cs @@ -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; + } +}