Skip to content

[Hyun] Week 5 Solution Explanation #108

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

Merged
merged 5 commits into from
Jun 4, 2024

Conversation

WhiteHyun
Copy link
Member

@WhiteHyun WhiteHyun commented Jun 1, 2024

347. Top K Frequent Elements

Complexities 📈

  • Time Complexity: $O(n log n)$
  • Space Complexity: $O(n)$

Explanation 📝

  1. 각 배열마다 빈도수를 구하는 Counter 딕셔너리를 만듦
  2. 빈도를 기준으로 내림차순 정렬
  3. 0부터 k만큼의 subarray를 가져옴
  4. 해당 빈도를 갖는 숫자 (딕셔너리의 key)를 가져옴

271. Encode and Decode Strings

  • Swift를 지원하지 않고, 기존 온라인 저지의 문제 풀이방법과는 달라서 제대로 풀이할 수 없었습니다 🥹

238. Product of Array Except Self

Complexities 📈

  • Time Complexity: $O(n)$
  • Space Complexity: $O(1)$

Explanation 📝

  1. 처음에 바로 떠올랐던 해법은 전부 곱한 값에서 각 자신의 요소를 나누는 것이었음
    • You must write an algorithm that runs in O(n) time and without using the division operation.를 보고 좌절함
  2. 좀 알아본 결과 두 번의 패스를 통해 왼쪽 부분곱, 오른쪽 부분 곱을 할당하여 풀이할 수 있었음

P.S. Space Complexity를 계산할 때 정답 배열에 관한 크기는 따로 계산하지 않는다는 것을 몰랐습니다.. 그래서 nums의 데이터를 업데이트하며 정답을 어떻게 반환할 수 있을지 계속 고민만 했네요. 😅

128. Longest Consecutive Sequence

Complexities 📈

  • Time Complexity: $O(n)$
  • Space Complexity: $O(n)$

Explanation 📝

  1. 집합으로 한 번 묶은 뒤 요소를 반복. 각 요소는 n이라고 칭함
    1. n-1가 집합에 존재하는지 확인(연속적인 수열을 구할 때 자신이 최솟값인지 확인하기 위한 여부)
      1. n+1이 집합에 존재하는지 확인, 그 이후 집합에 존재하기 전까지 해당 요소의 값을 1씩 더함. 이를 m이라고 칭함
        2.m이 구해졌다면, n...m-1이 최종 연속 수열의 길이가 됨. 따라서 이러한 수열의 길이를 구하고, 최댓값을 저장하여 리턴하였음

15. 3Sum

Complexities 📈

  • Time Complexity: $O(n^2)$
  • Space Complexity: $O(n)$

Explanation 📝

저에게는 너무 어려운 문제였습니다..
처음에는 3중 for문이 바로 떠올랐으나, 시간초과가 날 것 같아 시도하지 않았고, 시간복잡도를 줄일 수 있는 방법이 무엇일지 계속 고민하다가 답지를 찾아보게 되었어요 ㅎㅎ..

@WhiteHyun WhiteHyun self-assigned this Jun 1, 2024
Copy link
Contributor

@Invidam Invidam left a comment

Choose a reason for hiding this comment

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

코드가 엄청 깔끔하시네요! 부럽습니다~

Comment on lines +17 to +22
return counter
.sorted { lhs, rhs in
lhs.value > rhs.value // 빈도 수를 기준으로 내림차순 정렬
}
.prefix(k) // 앞에서부터 k만큼 Subarray로 가져옴
.map(\.key) // 제일 빈도가 많았던 것의 숫자를 가져옴 (Dictionary의 Key값)
Copy link
Contributor

Choose a reason for hiding this comment

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

스트림(스위프트에서도 해당 용어가 맞을까요?)로 해결하면 깔끔하겠다 생각했는데 역시나 멋있네요! 👍

@DaleSeo
Copy link
Member

DaleSeo commented Jun 2, 2024

@WhiteHyun 님, 아직 PR이 Draft 상태인데, 남은 두 문제 푸실 계획이신가요?

@WhiteHyun
Copy link
Member Author

@WhiteHyun 님, 아직 PR이 Draft 상태인데, 남은 두 문제 푸실 계획이신가요?

넵 마저 풀고 Draft 풀겠습니다.

@WhiteHyun WhiteHyun marked this pull request as ready for review June 3, 2024 16:34
@WhiteHyun WhiteHyun requested a review from DaleSeo June 3, 2024 16:34
@leokim0922 leokim0922 merged commit c890b73 into DaleStudy:main Jun 4, 2024
@WhiteHyun WhiteHyun deleted the whitehyun/week5 branch June 5, 2024 03:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Status: Done
Development

Successfully merging this pull request may close these issues.

4 participants