Leetcode # 2554. Maximum Number of Integers to Choose From a Range I

Problem

https://leetcode.com/problems/maximum-number-of-integers-to-choose-from-a-range-i

Solution

Time Complexity: O(max(len(banned), n))
Space Complexity: O(len(banned))
(The input and output generally do not count towards the space complexity.)

class Solution:
  def maxCount(self, banned: List[int], n: int, maxSum: int) -> int:
    cur_sum = chosen_n = 0
    banned_set = set(banned)
    for _n in range(1, n + 1):
      if _n not in banned_set:
        if cur_sum + _n > maxSum: break
        cur_sum += _n
        chosen_n += 1

    return chosen_n
   

 

Last Updated on 2024/12/06 by A1go

目錄
Bitnami