Skip to content

38. Count and Say / Medium / JavaScript #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

yeongrok-jeong
Copy link
Contributor

설명

  1. 재귀

    countAndSay(1) = "1"
    countAndSay(2) = say "1" = one 1 = "11" // one of `one`
    countAndSay(3) = say "11" = two 1's = "21" // two of `one`s
    countAndSay (4) = say "21" = one 2 + one 1 = "12" + "11" = "1211" // one of `one`, one of `two`, two of `one`s
    

    문제에서 설명하듯
    countAndsay(n)countAndSay(n-1)의 리턴값을 input으로 받아서
    특정한 방식으로 읽은 다음
    그걸 output으로 내놓아야 함

    즉 재귀를 사용함
    재귀 탈출 조건은 'n=1'일 때 outout 이 1'이라는 것

  2. "특정한 방식으로 읽은 것을 output으로"

    "1211" // one of `one`, one of `two`, two of `one`s
    

    "1211"을 count of num의 형식으로 읽는다고 하면
    같은 숫자(num)가 몇 개가 나오는지를 카운트(count)한 다음
    output 문자열에 ${count}${num}을 붙여주면 됨

Time Complexity: O(1) to O(3410)

n의 범위는 1 <= n <= 30
n이 늘어남에 따라 return value의 길이는 아래와 같이 변함
[1, 2, 2, 4, 6, 6, 8, 10, 14, 20, 26, 34, 46, 62, 78, 102, 134, 176, 226, 302, 408, 528, 678, 904, 1182, 1540, 2012, 2606, 3410, 4462]

countAndsay(n)countAndSay(n-1)의 리턴값을 순회하므로
countAndSay(n-1)의 리턴값의 길이만큼 연산을 수행함

즉 Time Complexity의 최솟값은 O(1), 최댓값은 O(3410)

Space Complexity: O(1)

리턴할 문자열을 저장하는 용도의 변수 외에는 따로 공간을 요하는 것이 없음

@yeongrok-jeong yeongrok-jeong requested a review from lynn0506 May 15, 2023 14:47
@yeongrok-jeong yeongrok-jeong self-assigned this May 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant