LeetCode solutions

2640. Maximum Number of Integers to Choose From a Range I

My accepted Python solution to LeetCode problem 2640, Maximum Number of Integers to Choose From a Range I, running in 49ms.

  • Difficulty: Medium
  • Python
  • Runtime 49ms
  • Memory 18.9MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 49ms, memory 18.9MB, accepted 2025-12-31.

python
class Solution:
    def maxCount(self, banned: List[int], n: int, maxSum: int) -> int:
        banned_set = set(banned)
        count = 0
        current_sum = 0
        
        for i in range(1, n + 1):
            if i not in banned_set:
                if current_sum + i <= maxSum:
                    current_sum += i
                    count += 1
                else:
                    break
        
        return count

Source