From 90a2ad817101334c583d81e6d0c2f6e3bc2f2819 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Fri, 6 Dec 2024 22:57:34 +0100 Subject: [PATCH] =?UTF-8?q?cs:=20add=20=C2=AB2554.=20Maximum=20Number=20of?= =?UTF-8?q?=20Integers=20to=20Choose=20From=20a=20Range=20I=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit URL: https://leetcode.com/problems/maximum-number-of-integers-to-choose-from-a-range-i/ Signed-off-by: Matej Focko --- ...er-of-integers-to-choose-from-a-range-i.cs | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 cs/maximum-number-of-integers-to-choose-from-a-range-i.cs 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; + } +}