Skip to content

[Chaewon-Park-STUDY] WEEK 02 Solutions - #2934

Open
Chaewon-Park-STUDY wants to merge 9 commits into
DaleStudy:mainfrom
Chaewon-Park-STUDY:week2
Open

Chaewon-Park-STUDY wants to merge 9 commits into
DaleStudy:mainfrom
Chaewon-Park-STUDY:week2

Conversation

@Chaewon-Park-STUDY

@Chaewon-Park-STUDY Chaewon-Park-STUDY commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

답안 제출 문제

작성자 체크 리스트

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

검토자 체크 리스트

Important

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

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

Implement isAnagram method to check if two strings are anagrams using sorting.
@github-actions github-actions Bot added the py label Nov 16, 2026
@dalestudy

dalestudy Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

🤖 AI 리뷰(알고리즘 패턴, 시간/공간 복잡도, 학습 현황) 결과는 Checks 탭에서 볼 수 있어요.

Comment thread valid-anagram/Chaewon-Park-STUDY.py Outdated
# 정렬을 해서 anagram이면 정렬된 리스트의 원소가 동일
# Time:O(n log n), Space:O(n)
# 시간 복잡도: O(n log n) - 정렬, 공간 복잡도: O(n) - sorted로 list 생성
# 정렬 말고 다른 효율적 풀이가 있을까요?? 코멘트 많이 부탁드립니다~!!

@novacat25 novacat25 Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

저도 이 문제에서 꽤 절었던 관계로 힌트 남기고 갑니다...!

  • 두 문자열이 Anagram이라는 것은 결국 어떤 정보를 공유한다는 의미입니다. (ex: anagram, naargam은 a 3번, n/g/r/m 1번씩 똑같습니다) 여기서는 문자열의 순서를 일치시키는 정렬 연산이 필요 없어집니다.
  • 문제에서 소문자 알파벳(a-z)으로 조건이 고정이 되어 있습니다. 이걸 멋지게 쓸 수 있는 방법이 있을 거에요!
  • 입력할 문자열이 아무리 길어져도 관리해야 할 문자는 일관적으로 같습니다. 입력 문자열에 따라 공간 복잡도가 O(k)를 갖는 Map 자료 구조와 달리, OO OO을 이용할 경우 input value = k의 값에 변하지 않고 공간 복잡도를 O(1)로 유지할 수 있습니다.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

힌트 감사합니다:) 빈칸에 들어갈 말은 '정적 배열'이군요..!
파이썬에서는 인덱스 배열을 만들고 ord(문자) - ord('a')로 인덱스를 구해 개수를 세면 정렬 없이 해결하고 복잡도도 줄일 수 있을 것 같네요~
도움 주셔서 감사합니다💪
이 풀이로 다시 커밋해보겠습니다

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

네, TS/JS에서는 charCodeAt()으로 인덱스를 계산할 수 있었더라고요.
그리고 a-z까지 연속된 값으로 코드를 딸 수 있으니, 아마 이걸로 카운트 배열을 만드시면 직빵일 거에요..!

@Yu-Jaeyoung
Yu-Jaeyoung self-requested a review November 16, 2026 05:05
Comment thread valid-anagram/Chaewon-Park-STUDY.py Outdated
Comment on lines +3 to +5
# 정렬을 해서 anagram이면 정렬된 리스트의 원소가 동일
# Time:O(n log n), Space:O(n)
# 시간 복잡도: O(n log n) - 정렬, 공간 복잡도: O(n) - sorted로 list 생성

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

저도 처음에는 정렬로 접근하였는데,
문제에서 s와 t의 길이는 반드시 같다 라는 제약조건이 없어서, 이 경우에 대한 처리를 하나 추가하였습니다.

그 다음 더 효율적인 방법을 고민할 때는,
공간 복잡도 1로 유지가능한 자료구조를 사용하는 방법이 있을 것 같습니다.

PS: @novacat25 님이 답을 유추할 수 있도록 남기신것 같아서, 정답을 남기지는 않겠습니다 😆

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

안녕하세요 @Yu-Jaeyoung 님 :)
리뷰 남겨주셔서 감사합니다!
길이가 다른 경우는 고려하지 못했는데, 처음에 길이를 비교해서 다르면 바로 종료하는 조건을 추가해야겠네요.
감사합니다 💪

Comment on lines +12 to +13
count_s = [0] * 26
count_t = [0] * 26

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

좋은 접근입니다!
문자 종류가 26개로 제한되어 있기 때문에 고정 크기 배열을 활용하면 공간 복잡도를 O(1)로 유지할 수 있었거든요.

두 개의 카운팅 배열을 사용하셔서 접근하셨군요..!
저는 두 문자열의 빈도수를 하나의 배열에서 증가/감소시키는 방식으로도 구현해서, 이 부분도 스타일 차를 느꼈습니다.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

정적배열 접근은 처음에 생각 못한 부분이었는데 문제 조건들도 꼼꼼히 살펴야겠어요.!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

이 문제 다른 풀이가 매우 궁금하네요 ㅎㅎ

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

💬 접근 방식이 저랑 완전 비슷해서 신기하네요.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

저도 리뷰 받고 투포인터 기법을 최종 풀이로 제출했는데 다른 분들도 대부분 투포인터기법을 사용하셨군요!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@Chaewon-Park-STUDY 투포인터가 역시 생각해내기 쉬운 방법인거 같아요!

Comment thread 3sum/Chaewon-Park-STUDY.py Outdated
if i > 0 and nums[i] == nums[i-1]: #중복 제거
continue
candid=set() #target이 candid안에 있으면 세 수의 합이 "0"
for j in range(i+1,k): #i를 고정

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

정렬 후 첫 번째 숫자를 고정하고, Hash Set을 활용하여 Two Sum 형태로 접근하신 점이 좋았습니다!
더불어, 정렬된 배열 기준으로 nums[i] > 0인 경우에는 탐색 자체가 불필요해지기에 이 부분 과감하게 쳐내신 부분도 배워갑니다.

개선 부분에서 힌트를 드리자면...

현재는 j를 한 방향(i+1 -> k)으로 순차 탐색하면서 Hash Set으로 필요한 숫자를 찾고 있는데,
정렬된 배열의 특성을 활용하면 현재 합의 크기에 따라 탐색 방향을 조절할 수도 있습니다.

(ex) nums[j] + nums[k] = SUM_TARGET일 때,

  • 값이 SUM_TARGET보다 작다면? j idx(nums[j])만 올려서 탐색치에 근접할 수 있게 해줍니다.
  • 값이 SUM_TARGET보다 크다면? k idx(nums[k])만 내려서 탐색치에 근접할 수 있게 해줍니다.

이런 관점에서 Two Pointer 기법을 함께 살펴보시면,
Hash Set 없이도 중복을 효율적으로 처리하는 방법을 이해하는 데 도움이 되실 것 같습니다!

@Chaewon-Park-STUDY Chaewon-Park-STUDY Nov 19, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

오호!
투 포인터 기법으로 한번 접근해보겠습니다 🫡재풀이 후 리뷰 다시 남길게요~감사합니다
저도 처음에 break, continue파트를 아예 작성하지 않았었는데 TLE가 날 것 같아 추가했네요 ㅎㅎ
지난번에 Hash Set에 대해 블로그에 정리해주셨는데, 말씀 나온 김에
Hash set, two pointer로 찾는 방식의 차이에 대해 다시 복기해야겠어요..!

@Chaewon-Park-STUDY Chaewon-Park-STUDY moved this from Solving to In Review in 리트코드 스터디 9기 Nov 20, 2026
return n
first, second = 1, 2 # 1번째, 2번째 계단 오르는 방법 수
for i in range(3, n + 1):
first, second = second, first + second

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

👍 저는 배열을 사용했는데, 계속 갱신해나가면 별도의 공간을 차지않고 풀 수 있군요. 참고가 되었습니다.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

dp[n] = dp[n-1] + dp[n-2] 수식을 손으로 전개하다 보니, let 변수나 가변 변수로 해결이 되는 것 같더라고요..!
그래도 처음부터 Space Optimisation 기법을 적용시키는 게 쉽지 않아서요, 처음에는 배열에 눈이 가게 되는 것 같습니다.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

코멘트 감사합니다!
저도 처음엔 모두 dp 배열로 풀었는데, 공간복잡도가 O(n)이라 줄여보고자 시도하였습니다.
점화식에서 바로 직전 두 값만 쓰이니, 두 변수만 갱신하는 방식으로 공간을 O(1)로 줄여 봤습니다!

@novacat25
novacat25 marked this pull request as draft November 21, 2026 05:44
@novacat25
novacat25 marked this pull request as ready for review November 21, 2026 05:44

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

깔끔한 풀이 잘 보고 갑니다!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: In Review

Development

Successfully merging this pull request may close these issues.

5 participants