Python | Greedy Algorithm
see the Successfully Accepted Submission\n\n# Code\npython3 []\nclass Solution:\n    def maxCount(self, banned: list[int], n: int, maxSum: int) -> int:\n       
[see the Successfully Accepted Submission](https://leetcode.com/problems/maximum-number-of-integers-to-choose-from-a-range-i/submissions/1471436407/?envType=daily-question&envId=2024-12-06)\n\n# Code\n```python3 []\nclass Solution:\n    def maxCount(self, banned: list[int], n: int, maxSum: int) -> int:\n        # Create a set of banned numbers for O(1) lookup\n        banned_set = set(banned)\n        \n        # Initialize the sum and count of chosen numbers\n        current_sum = 0\n        count = 0\n        \n        # Iterate through numbers from 1 to n\n        for num in range(1, n + 1):\n            # Skip if the number is banned or if adding it would exceed maxSum\n            if num in banned_set or current_sum + num > maxSum:\n                continue\n            \n            # Otherwise, include this number\n            current_sum += num\n            count += 1\n        \n        return count\n\n```\n\nExplanation:\n- **Banned Numbers Handling**: Convert the banned list to a set for faster lookups.\n- **Iterate Through Range**: Check every number in the range [1, n].\n- **Skip Invalid Numbers**: If the number is in the banned list or adding it would exceed `maxSum`, skip it.\n- **Add Valid Numbers**: If the number passes the checks, add it to `current_sum` and increment the count of chosen numbers.\n- **Return the Count**: After the loop, return the count of numbers successfully chosen.\n\n### Complexity:\n- **Time Complexity**:  \n  \\(O(n + k)\\), where \\(n\\) is the upper bound of the range and \\(k\\) is the length of the banned list (due to the set creation).\n- **Space Complexity**:  \n  \\(O(k)\\) for the `banned_set`.\n\n\n\n![image](https://assets.leetcode.com/users/images/b1e4af48-4da1-486c-bc97-b11ae02febd0_1696186577.992237.jpeg)
['Python3']