본문 바로가기
알고리즘

[LeetCode] 455. Assign Cookies (golang)

by 참새는 짹짹 2021. 6. 1.

문제


Assign Cookies - LeetCode
Assume you are an awesome parent and want to give your children some cookies. But, you should give each child at most one cookie. Each child i has a greed factor g[i], which is the minimum size of a cookie that the child will be content with; and each cookie j has a size s[j].
https://leetcode.com/problems/assign-cookies/
  • 쿠키를 나눠주어 만족시킬 수 있는 아이들의 최대수

구현


  • 정렬 ( NlogN? )
  • 아이에게 줄 수 있는 쿠키 전체 탐색 ( M )

시간 복잡도
  • O( NlogN + M )?

  • 코드
    func findContentChildren(g []int, s []int) int {
        sort.Ints(g)
        sort.Ints(s)
        cnt := 0
        i := 0
        for j:=0;j<len(s) && i<len(g);j++ {
            if (g[i] <= s[j]) {
                i++
                cnt++
            }
        }
        return cnt
    }

댓글