Skip to content

[doitduri] Week01 Solutions #1154

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
Apr 5, 2025
Merged

[doitduri] Week01 Solutions #1154

merged 5 commits into from
Apr 5, 2025

Conversation

doitduri
Copy link
Contributor

@doitduri doitduri commented Mar 31, 2025

답안 제출 문제

작성자 체크 리스트

  • 우측 메뉴에서 PR을 Projects에 추가해주세요.
  • Projects의 오른쪽 버튼(▼)을 눌러 확장한 뒤, Week를 현재 주차로 설정해주세요.
  • 문제를 모두 푸시면 프로젝트에서 StatusIn Review로 설정해주세요.
  • 코드 검토자 1분 이상으로부터 승인을 받으셨다면 PR을 병합해주세요.

Important

본인 답안 제출 뿐만 아니라 다른 분 PR 하나 이상을 반드시 검토를 해주셔야 합니다!

  • 바로 이전에 올라온 PR에 본인을 코드 리뷰어로 추가해주세요.
  • 본인이 검토해야하는 PR의 답안 코드에 피드백을 주세요.
  • 토요일 전까지 PR을 병합할 수 있도록 승인해주세요.

@@ -0,0 +1,16 @@
class Solution {
func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
let dict = nums.enumerated().reduce(into: [Int: Int]()) { initialValue, num in
Copy link
Contributor

Choose a reason for hiding this comment

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

reduce 를 사용해서 Dictionary를 만들 수 있는건 처음 알았네요!
기억이 맞다면 map으로는 불가능 했어서 무조건 반복문 돌리면서 dictionary를 만들었는데,,,

새로운거 배워갑니다!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

허허.. 저는 반복문이 그렇게 싫더라구요(?)
다만 알고리즘 문제 풀이에서 고차 함수를 사용 할 때 시간복잡도를 유의해서 사용해야할 것 같아요~

@doitduri doitduri moved this from Solving to In Review in 리트코드 스터디 4기 Apr 2, 2025
class Solution {
func containsDuplicate(_ nums: [Int]) -> Bool {
let numbericSet = Set(nums)
return numbericSet.count < nums.count
Copy link
Contributor

Choose a reason for hiding this comment

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

Set에 nums를 먼저 담고, 길이를 비교하는 솔루션은 생각 못해 봤는데, 좋은 방법인 것 같습니다.

저는 Set에 nums를 하나씩 담으면서 이미 담긴 num이 있다면, 그 때 return하도록 했는데요!
시간복잡도는 O(N)으로 같겠지만, 처음에 거를 수도 있다는 부분도 참고해 보시면 좋을 것 같습니다!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

오오 early return 할 수도 있겠네요 ㅎㅎ 좋은 의견 감사합니다 🥰

Copy link
Contributor

Choose a reason for hiding this comment

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

early return 명칭 너무 좋네요.
새로운거 알아갑니다.ㅎㅎ

Copy link
Contributor

Choose a reason for hiding this comment

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

DP로 풀이해서 직관적으로 잘 풀으신 것 같아요!
직관적인 풀이법으로 풀면 시간/공간 복잡도는 O(N)/O(N) 인데, 공간 복잡도를 더 최적화하는 풀이법이 있더라구요!
공간 복잡도를 더 최적화한 솔루션은 코드가 직관적이진 않지만, 이 방법도 보시고 고민해보는 것도 좋을 것 같아요!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

네! 한번 살펴보겠습니다 😁👀

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

Choose a reason for hiding this comment

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

👍
(heap을 사용해서 시간복잡도를 좀 더 최적화한 방법도 풀어보시면 좋을 것 같습니다.)

Copy link
Contributor

Choose a reason for hiding this comment

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

👍
(dict 없이 더 최적화 할 수 있는 방법은 없을까요??)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

딕셔너리는 key-value 자료구조로 조회 시 O(1)인 장점을 이용해서 풀었는데요~,
지금 당장 생각나는 다른 풀이는. .. nums의 최대값 만큼의 array를 생성해서 동일한 로직으로 접근해볼 것 같습니다🙄 ㅠ
대신 공간복잡도가 증가한다는 단점이 있을 것 같습니다. 다양한 관점에서 의견 주셔서 감사합니다 ㅎㅎ

@doitduri doitduri merged commit 09a509c into DaleStudy:main Apr 5, 2025
1 check passed
@github-project-automation github-project-automation bot moved this from In Review to Completed in 리트코드 스터디 4기 Apr 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Completed
Development

Successfully merging this pull request may close these issues.

3 participants