-
-
Notifications
You must be signed in to change notification settings - Fork 195
[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
Conversation
@@ -0,0 +1,16 @@ | |||
class Solution { | |||
func twoSum(_ nums: [Int], _ target: Int) -> [Int] { | |||
let dict = nums.enumerated().reduce(into: [Int: Int]()) { initialValue, num in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reduce 를 사용해서 Dictionary를 만들 수 있는건 처음 알았네요!
기억이 맞다면 map으로는 불가능 했어서 무조건 반복문 돌리면서 dictionary를 만들었는데,,,
새로운거 배워갑니다!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
허허.. 저는 반복문이 그렇게 싫더라구요(?)
다만 알고리즘 문제 풀이에서 고차 함수를 사용 할 때 시간복잡도를 유의해서 사용해야할 것 같아요~
class Solution { | ||
func containsDuplicate(_ nums: [Int]) -> Bool { | ||
let numbericSet = Set(nums) | ||
return numbericSet.count < nums.count |
There was a problem hiding this comment.
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)으로 같겠지만, 처음에 거를 수도 있다는 부분도 참고해 보시면 좋을 것 같습니다!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
오오 early return 할 수도 있겠네요 ㅎㅎ 좋은 의견 감사합니다 🥰
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
early return 명칭 너무 좋네요.
새로운거 알아갑니다.ㅎㅎ
There was a problem hiding this comment.
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) 인데, 공간 복잡도를 더 최적화하는 풀이법이 있더라구요!
공간 복잡도를 더 최적화한 솔루션은 코드가 직관적이진 않지만, 이 방법도 보시고 고민해보는 것도 좋을 것 같아요!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
네! 한번 살펴보겠습니다 😁👀
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
(이 방법으로는 못 풀어 봤는데, 다시 풀어 보겠습니다!)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
(heap을 사용해서 시간복잡도를 좀 더 최적화한 방법도 풀어보시면 좋을 것 같습니다.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
(dict 없이 더 최적화 할 수 있는 방법은 없을까요??)
There was a problem hiding this comment.
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를 생성해서 동일한 로직으로 접근해볼 것 같습니다🙄 ㅠ
대신 공간복잡도가 증가한다는 단점이 있을 것 같습니다. 다양한 관점에서 의견 주셔서 감사합니다 ㅎㅎ
답안 제출 문제
작성자 체크 리스트
In Review
로 설정해주세요.Important
본인 답안 제출 뿐만 아니라 다른 분 PR 하나 이상을 반드시 검토를 해주셔야 합니다!