-
-
Notifications
You must be signed in to change notification settings - Fork 195
[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
base: main
Are you sure you want to change the base?
Conversation
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.
이번 한 주도 고생 많으셨습니다~! 🍀
for (let right = 0; right < s.length; right++) { | ||
while (seen.has(s[right])) { | ||
seen.delete(s[left]); | ||
left++; |
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.
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) { |
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.
반복문으로 푸는 코드가 정말 깔끔하네요 🤩 문제 하단에 follow up 조건으로 재귀 방식으로도 풀어보는 걸 권하고 있어서, 재귀로도 한 번 더 풀어보셔도 좋을 것 같습니다!
* - 첫번째 행과 열에 0이 존재하는지 확인 후 변수로 따로 뺀다 | ||
* - 나머지 셀 중에서 0이 존재하는지 순회하고 있으면 첫번째 행/열에 표시한다 | ||
* - 나머지 셀을 다시 순회하면서 첫번째 열을 참고하여 0으로 치환한다 | ||
* - 첫번째 행과 열을 0으로 바꿔아한다면 처리한다. |
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.
아이디어를 코멘트로 정리/비교해주셔서 리뷰에 도움이 되는 것 같습니다! 실제로 코딩 인터뷰를 볼 때에도 좋은 인상을 남길 수 있는 습관인 것 같아요!!
|
||
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]; |
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.
2D DP에서 dp[r][c]
를 구할 때 dp[r - 1][c]
와 dp[r][c - 1]
만 사용하기 때문에, rolling array 기법(참고 링크)을 이용해 1D DP로 공간 복잡도를 최적화 할 수 있을 것 같아요~!
이 기법은 row 만큼 순회하면서 1D 리스트인 dp[c]
에 이전 값을 더해가며 업데이트하는 방식인데요, DP 문제에서 최적화 용도로 자주 사용되는 기법이라고 합니다! 한 번 찾아보셔도 재밌으실 것 같아요 ㅎㅎㅎ
답안 제출 문제
작성자 체크 리스트
In Review
로 설정해주세요.검토자 체크 리스트
Important
본인 답안 제출 뿐만 아니라 다른 분 PR 하나 이상을 반드시 검토를 해주셔야 합니다!