Skip to content
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

[Flynn] week 3 #385

Merged
merged 6 commits into from
Aug 28, 2024
Merged

[Flynn] week 3 #385

merged 6 commits into from
Aug 28, 2024

Conversation

obzva
Copy link
Contributor

@obzva obzva commented Aug 26, 2024

제출 문제

체크 리스트

  • PR을 프로젝트에 추가하고 Week를 현재 주차로 설정해주세요.
  • 바로 앞에 PR을 열어주신 분을 코드 검토자로 지정해주세요.
  • 문제를 모두 푸시면 프로젝트에서 Status를 In Review로 설정해주세요.
  • 코드 검토자 1분 이상으로부터 승인을 받으셨다면 PR을 병합해주세요.

설명

Two Sum

풀이

key: num, value: index를 저장할 hashmap을 선언합니다.

배열 nums의 첫번째 원소를 hashmap에 저장합니다. (nums[0]: 0)

배열 nums를 두번째 원소부터 조회하여 target - nums[i]가 hashmap에 존재하는지 판단합니다.

만약 target - nums[i]가 hashmap에 존재한다면 정답 배열을 반환하고, 그렇지 않다면 hashmap에 새로운 쌍을 추가합니다.

Big-O

주어진 배열 nums의 크기 N에 대해,

Time complexity: O(N)

  • 배열 nums를 순회하기 때문에 O(N)의 시간 복잡도를 가집니다.

Space complexity: O(N)

  • hashmap의 크기가 배열 nums의 크기에 가깝게 커질 수 있으므로 O(N)의 공간복잡도를 가집니다.

Climbing Stairs

풀이

다이나믹 프로그래밍을 이용하여 풀 수 있습니다.

아래와 같이 memo 배열을 정의했을 때,

memo[0] = 1
memo[1] = 1
memo[i] = distinct ways to climb to the i-th stair

다음과 같은 점화식이 성립합니다.

memo[i] = memo[i - 2] + memo[i - 1] (i > 1)

Big-O

Time complexity: O(N)

Space complexity: O(N)


Product of Array Except Self

풀이

한 칸씩 밀린 상태로 누적곱을 배열에 기록해주는 것을 두 번 진행해주면 원하는 바를 얻을 수 있습니다.

index 0 1 2 3
value 1 2 3 4
acc-> 1 1 x 2 1 x 2 x 3
<-acc 2 x 3 x 4 3 x 4 4
res 2 x 3 x 4 1 x 3 x 4 1 x 2 x 4 1 x 2 x 3

Big-O

Time complexity: O(N)

Space complexity: O(N)


Coin Change

풀이

DP를 이용하여 풀이할 수 있습니다.

배열 memo를 아래와 같이 정의합니다.

memo[i] = i원을 만들기 위해 필요한 동전의 최소 개수
각 원소의 값은 초기값은 10^4 + 1로 설정함 (max(amount) / min(coin) + 1)

앞서 정의한 배열 memo를 이용하면 아래 점화식이 성립합니다.

memo[i] = min(memo[i], memo[i - coin] + 1) if i - coin >= 0

Big-O

배열 coins의 크기를 N, 정수 amount의 크기를 K라고 했을 때,

Time complexity: O(N * M)

Space complexity: O(M)


Combination Sum

풀이

queue를 이용한 BFS로 주어진 candidates의 조합을 만듭니다.

조합의 합 S의 크기에 따라 아래와 같이 연산을 진행합니다.

S < target: 조합에 새로운 수를 추가하여 queue에 다시 push
S == target: 정답 배열 res에 해당 조합을 push
S > target: 더 이상 queue에 조합을 등록하지 않음

Big-O

candidates 배열의 크기를 N, target의 크기를 T, candidates 배열의 원소 중 가장 작은 원소의 크기를 K라고 했을 때,

Time complexity: O(N ^ (T / K))

  • queue에 담긴 각 조합은 다음 단계에서 �새 후보 숫자를 한 번씩 추가하여 최대 N개의 새로운 조합을 생성할 수 있습니다.
  • 이걸 Tree에 빗대어 생각해보면 각 nodeN개의 자식들을 갖는다고 볼 수 있습니다
  • Tree의 깊이는 T / K에 비례합니다

Space complexity: O((T / K) * (N ^ (T / K)))

  • queue의 크기는 앞서 말한 Treenode 개수만큼 늘어날 수 있습니다
  • node가 지닌 조합 배열의 크기는 T / K 까지 커질 수 있습니다

@obzva obzva requested a review from HC-kang August 26, 2024 09:35
@obzva obzva added the c++ label Aug 26, 2024
@obzva obzva marked this pull request as ready for review August 26, 2024 15:30
@obzva obzva requested a review from a team as a code owner August 26, 2024 15:30
climbing-stairs/flynn.md Outdated Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 풀이 설명까지 감사합니다! ㅎㅎ 간결해서 보기 좋네요~!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

감사합니다

@obzva obzva requested a review from DaleSeo August 27, 2024 11:27

}

while (!q.empty()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이전 풀이도 그렇고, 큐와 스택을 정말 잘 쓰시네요

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

감사합니다
재귀보다는 반복문 사용하는 풀이를 좀 더 선호합니다 :D

@HC-kang
Copy link
Contributor

HC-kang commented Aug 27, 2024

중요한 부분은 아니지만, Combination Sum 문제의
queue에 담긴 각 조합들은 최대 N개의 새로운 조합들을 만들어 낼 수 있습니다 부분을
queue에 담긴 각 조합은 다음 단계에서 모든 후보 숫자를 한 번씩 추가하여 최대 N개의 새로운 조합을 생성할 수 있습니다.
와 같이 좀 더 이해하기 쉽게 표현 할 수 있지 않을까 싶습니다!

좋은 풀이를 알기 쉽게 잘 써 주셨네요. 고생하셨습니다!

@obzva
Copy link
Contributor Author

obzva commented Aug 28, 2024

중요하고 좋은 코멘트 감사합니다!
희찬님께서 말씀해주신 방향대로 수정하는게 더 잘 읽히네요 :D

@obzva obzva merged commit 862141b into DaleStudy:main Aug 28, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
No open projects
Status: Completed
Development

Successfully merging this pull request may close these issues.

4 participants