[LeetCode] 2279. Maximum Bags With Full Capacity of Rocks

김민우·2022년 11월 24일
0

알고리즘

목록 보기
73/189

- Problem

2279. Maximum Bags With Full Capacity of Rocks

- 내 풀이

class Solution:
    def maximumBags(self, capacity: List[int], rocks: List[int], additionalRocks: int) -> int:
        answer = 0
        capacity_bag = []
        
        for i in range(len(capacity)):
            if capacity[i] == rocks[i]:
                answer += 1
                continue
            capacity_bag.append(capacity[i] - rocks[i])
            
        capacity_bag.sort()
            
        for x in capacity_bag:
            if additionalRocks < x:
                break
            additionalRocks -= x
            answer += 1
        
        return answer

- 결과


- refactoring code

class Solution:
    def maximumBags(self, capacity: List[int], rocks: List[int], additionalRocks: int) -> int:
        remain_capacity = sorted([c - r for c, r in zip(capacity, rocks)])
        answer = 0

        for x in remain_capacity:
            if additionalRocks < x:
                break
            additionalRocks -= x
            answer += 1
        
        return answer

- 결과

profile
Pay it forward.

0개의 댓글