[리트코드] 쿠키 부여

박형진·2022년 1월 8일
0

https://leetcode.com/problems/assign-cookies/submissions/


1. 전체 코드

1-1. 그리디

class Solution:
    def findContentChildren(self, g: List[int], s: List[int]) -> int:
        g.sort()
        s.sort()
        g = deque(g)
        s = deque(s)
        ans = 0
        # g = [7, 8, 9, 10]
        # s = [5, 6, 7, 8]
        while g and s:
            cookie = s.popleft()
            if cookie >= g[0]:
               ans += 1
               person = g.popleft()
        return ans

2-2. 이진 탐색

class Solution:
    def findContentChildren(self, g: List[int], s: List[int]) -> int:
        g.sort()
        s.sort()
        result = 0
        for i in s:
            idx = bisect_right(g, i)
            # 이미 idx로 뽑혔던 인덱스보다 큰 인덱스만 가능하다.
            if idx > result:
                result += 1
        return result
profile
안녕하세요!

0개의 댓글