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

[kayden] Week 05 Solutions #448

Merged
merged 10 commits into from
Sep 14, 2024
Merged

[kayden] Week 05 Solutions #448

merged 10 commits into from
Sep 14, 2024

Conversation

kjb512
Copy link
Contributor

@kjb512 kjb512 commented Sep 9, 2024

답안 제출 문제

체크 리스트

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

@kjb512 kjb512 self-assigned this Sep 9, 2024
@github-actions github-actions bot added the py label Sep 9, 2024
@kjb512 kjb512 requested a review from obzva September 9, 2024 15:55
group-anagrams/kayden.py Outdated Show resolved Hide resolved
groups = {}
for anagram in strs:
key = str(sorted(anagram))
groups.setdefault(key, []).append(anagram)
Copy link
Contributor

Choose a reason for hiding this comment

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

setdefault 잘 배워갑니다 :)

Copy link
Contributor

Choose a reason for hiding this comment

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

@obzva 같이 읽어보시면 도움이 될 만한 글 추천드려요.
https://www.daleseo.com/python-collections-defaultdict/

Copy link
Contributor

Choose a reason for hiding this comment

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

이런 좋은 블로그가 있네요 ^~^

3sum/kayden.py Outdated
continue
for j in range(i + 1, n):
target = -(nums[i] + nums[j])
if not check.get(target, None):
Copy link
Contributor

Choose a reason for hiding this comment

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

단순 질문

if target not in check이랑 if not check.get(target, None)의 차이가 있나요?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

없습니다! 전자가 훨씬 읽기 좋네요! get(key, default)가 습관이 돼서 이렇게 작성했네요


answer = set()
for i in range(n-2):
if nums[i] > 0:
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

Comment on lines +46 to +49
if nums[l] + nums[r] == -nums[i]:
answer.add((nums[i], nums[l], nums[r]))
l += 1
r -= 1
Copy link
Contributor

Choose a reason for hiding this comment

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

'이렇게 하면 answer에 중복되는 triplet이 많이 쌓이고 answer를 list로 변환할 때 비효율적이지 않을까?' 싶어서 대안 코드도 써보고 검색도 해봤는데, 결과적으론 도긴개긴이었어요 ㅎㅎ

if nums[l] + nums[r] == -nums[i]:
    answer.add((nums[i], nums[l], nums[r]))
    while True:
        l += 1
        if nums[l - 1] != nums[l]:
            break
    while True:
        r -= 1
        if nums[r + 1] != nums[r]:
            break

https://stackoverflow.com/questions/62679314/algorithmic-complexity-to-convert-a-set-to-a-list-in-python

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.

도긴개긴 추가 ㅎㅎ 😂

if nums[l] + nums[r] == -nums[i]:
    while l < r and nums[l] == nums[l + 1]:
        l += 1
    while low < high and nums[r] == nums[r - 1]:
        r -= 1
    l, r = l + 1, r - 1

Copy link
Contributor Author

Choose a reason for hiding this comment

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

읽기 편해졌네요 👏

@kjb512 kjb512 marked this pull request as ready for review September 12, 2024 13:12
@kjb512 kjb512 requested a review from a team as a code owner September 12, 2024 13:12
Comment on lines +46 to +49
if nums[l] + nums[r] == -nums[i]:
answer.add((nums[i], nums[l], nums[r]))
l += 1
r -= 1
Copy link
Contributor

Choose a reason for hiding this comment

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

도긴개긴 추가 ㅎㅎ 😂

if nums[l] + nums[r] == -nums[i]:
    while l < r and nums[l] == nums[l + 1]:
        l += 1
    while low < high and nums[r] == nums[r - 1]:
        r -= 1
    l, r = l + 1, r - 1

groups = {}
for anagram in strs:
key = str(sorted(anagram))
groups.setdefault(key, []).append(anagram)
Copy link
Contributor

Choose a reason for hiding this comment

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

@obzva 같이 읽어보시면 도움이 될 만한 글 추천드려요.
https://www.daleseo.com/python-collections-defaultdict/

@@ -0,0 +1,25 @@
# 시간복잡도: ?
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.

@kjb512 시간이 괜찮으시다면 시각화하면서 각 idx가 dfs에 의해 어떻게 호출되는지 살펴보면 금방 계산하실 수 있을 것 같아요

Copy link
Contributor Author

@kjb512 kjb512 Sep 13, 2024

Choose a reason for hiding this comment

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

감사합니다 좋은 방법이네요! 반복문들 시각화를 해봐야겠네요!

@@ -0,0 +1,25 @@
# 시간복잡도: ?
# 공간복잡도: O(S)
Copy link
Contributor

Choose a reason for hiding this comment

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

S는 주어진 문자열 s의 길이인가요?

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

@DaleSeo DaleSeo 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 +1 to +3
# 시간복잡도: O(S*W)
# S: s의 길이 300 W: worDict 각 단어의 총 길이 20*1000
# 300 * 20*1000 = 6*1e6 (600만)
Copy link
Contributor

Choose a reason for hiding this comment

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

혹시 17번째 줄의 s[idx:idx + l] == word 부분이 상수 시간이 걸린다고 보신건가요?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

리뷰 감사합니다! s[idx:idx + l] == word 슬라이싱 과정이 wordDict 단어들의 총길이 만큼 연산된다고 판단하여 20*1000으로 하였습니다!

@kjb512 kjb512 merged commit c7afa4d into DaleStudy:main Sep 14, 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.

3 participants