<p>You are given an integer array <code>banned</code> and two integers <code>n</code> and <code>maxSum</code>. You are choosing some number of integers following the below rules:</p>

<ul>
	<li>The chosen integers have to be in the range <code>[1, n]</code>.</li>
	<li>Each integer can be chosen <strong>at most once</strong>.</li>
	<li>The chosen integers should not be in the array <code>banned</code>.</li>
	<li>The sum of the chosen integers should not exceed <code>maxSum</code>.</li>
</ul>

<p>Return <em>the <strong>maximum</strong> number of integers you can choose following the mentioned rules</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> banned = [1,6,5], n = 5, maxSum = 6
<strong>Output:</strong> 2
<strong>Explanation:</strong> You can choose the integers 2 and 4.
2 and 4 are from the range [1, 5], both did not appear in banned, and their sum is 6, which did not exceed maxSum.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> banned = [1,2,3,4,5,6,7], n = 8, maxSum = 1
<strong>Output:</strong> 0
<strong>Explanation:</strong> You cannot choose any integer while following the mentioned conditions.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> banned = [11], n = 7, maxSum = 50
<strong>Output:</strong> 7
<strong>Explanation:</strong> You can choose the integers 1, 2, 3, 4, 5, 6, and 7.
They are from the range [1, 7], all did not appear in banned, and their sum is 28, which did not exceed maxSum.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= banned.length &lt;= 10<sup>4</sup></code></li>
	<li><code>1 &lt;= banned[i], n &lt;= 10<sup>4</sup></code></li>
	<li><code>1 &lt;= maxSum &lt;= 10<sup>9</sup></code></li>
</ul>

from bisect import bisect_right

class Solution:
    def maxCount(self, banned: List[int], n: int, maxSum: int) -> int:
        banned = self.bucket_sort(banned)
        ans = 0
        l, r = 0, n
        while l <= r:
            mid = (l + r) // 2
            ind = bisect_right(banned, mid)
            total = mid * (mid + 1) // 2 - sum(banned[:bisect_right(banned, mid)])
            if total == maxSum:
                return mid - ind
            elif total < maxSum:
                l = mid + 1
                ans = max(ans, mid - ind)
            else:
                r = mid - 1
        return ans
        

    def bucket_sort(self, arr):
        n = 10**4 + 1
        buckets = [[] for _ in range(n)]

        # Put array elements in different buckets
        for num in arr:
            if len(buckets[num]) == 0:
                buckets[num].append(num)

        # Concatenate all buckets into arr[]
        index = 0
        sum_array = [0 for _ in range(n)]
        banned_array = []
        for bucket in buckets:
            for num in bucket:
                banned_array.append(num)
        return banned_array
1093