Skip to content

[soobing] WEEK07 Solutions #1465

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open

[soobing] WEEK07 Solutions #1465

wants to merge 6 commits into from

Conversation

soobing
Copy link
Contributor

@soobing soobing commented May 12, 2025

답안 제출 문제

작성자 체크 리스트

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

검토자 체크 리스트

Important

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

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

@soobing soobing moved this from Solving to In Review in 리트코드 스터디 4기 May 15, 2025
Copy link
Contributor

@seungriyou seungriyou left a comment

Choose a reason for hiding this comment

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

이번 한 주도 고생 많으셨습니다~! 🍀

for (let right = 0; right < s.length; right++) {
while (seen.has(s[right])) {
seen.delete(s[left]);
left++;
Copy link
Contributor

Choose a reason for hiding this comment

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

seen을 set이 아닌 hash map으로 관리하며 가장 최근에 등장했던 인덱스까지 함께 저장한다면, left를 1씩 증가시키며 확인하는 것이 아닌 곧바로 가장 최근에 등장했던 인덱스의 다음 위치(= seen[s[right]] + 1)로 옮길 수 있어 최적화가 가능하다고 합니다. (단, 이때 seen[s[right]] >= left인지 추가로 확인해야 합니다!)

저도 처음에 soobing님과 동일하게 투 포인터와 set을 이용해 left를 1씩 증가시키면서 확인하는 방식으로 풀었는데요, 솔루션 탭에서 더 최적화 할 수 있는 풀이법을 알게 되어 공유드려요! 😆

function reverseList(head: ListNode | null): ListNode | null {
let prev: ListNode | null = null;

while (head) {
Copy link
Contributor

Choose a reason for hiding this comment

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

반복문으로 푸는 코드가 정말 깔끔하네요 🤩 문제 하단에 follow up 조건으로 재귀 방식으로도 풀어보는 걸 권하고 있어서, 재귀로도 한 번 더 풀어보셔도 좋을 것 같습니다!

* - 첫번째 행과 열에 0이 존재하는지 확인 후 변수로 따로 뺀다
* - 나머지 셀 중에서 0이 존재하는지 순회하고 있으면 첫번째 행/열에 표시한다
* - 나머지 셀을 다시 순회하면서 첫번째 열을 참고하여 0으로 치환한다
* - 첫번째 행과 열을 0으로 바꿔아한다면 처리한다.
Copy link
Contributor

Choose a reason for hiding this comment

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

아이디어를 코멘트로 정리/비교해주셔서 리뷰에 도움이 되는 것 같습니다! 실제로 코딩 인터뷰를 볼 때에도 좋은 인상을 남길 수 있는 습관인 것 같아요!!


for (let r = 1; r < m; r++) {
for (let c = 1; c < n; c++) {
dp[r][c] = dp[r - 1][c] + dp[r][c - 1];
Copy link
Contributor

@seungriyou seungriyou May 16, 2025

Choose a reason for hiding this comment

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

2D DP에서 dp[r][c]를 구할 때 dp[r - 1][c]dp[r][c - 1]만 사용하기 때문에, rolling array 기법(참고 링크)을 이용해 1D DP로 공간 복잡도를 최적화 할 수 있을 것 같아요~!

이 기법은 row 만큼 순회하면서 1D 리스트인 dp[c]에 이전 값을 더해가며 업데이트하는 방식인데요, DP 문제에서 최적화 용도로 자주 사용되는 기법이라고 합니다! 한 번 찾아보셔도 재밌으실 것 같아요 ㅎㅎㅎ

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.

2 participants