-
Notifications
You must be signed in to change notification settings - Fork 126
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
[Chaedie] Week 7 #937
[Chaedie] Week 7 #937
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.
안녕하세요 Chaedie님 :)
이번 한 주도 수고하셨습니다
|
||
class Solution: | ||
def lengthOfLongestSubstring(self, s: str) -> int: | ||
window = set() |
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으로 이미 방문한 문자들을 관리하는 대신에, dictionary로 { 방문한 문자: 해당 문자의 인덱스 }
를 관리할 수도 있습니다 :)
참고하시라고 gist 남깁니다 https://gist.github.com/obzva/5db9d02a3e6c97f45589dab34f0f8603
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.
감사합니다.참고하겠습니다..!
prev = None | ||
cur = head | ||
while cur: | ||
next = cur.next |
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.
개인적인 생각입니다, 변수명을 next
로 지으니까 좀 헷갈리는 것 같아요
지금은 간단한 함수라 괜찮지만, 더 복잡해지면 좀 어지러울 수 있을 것 같아요
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.
node.next 와 next 라는 변수명이 겹쳐서 헷갈린다는 의미시죠? temp 로 수정하겠습니다. 👍
dp = [] | ||
for i in range(m): | ||
dp.append([0] * n) | ||
print(dp) |
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.
앗 ㅎㅎㅎㅎ
up_value = 0 if i - 1 < 0 or i - 1 >= m else dp[i - 1][j] | ||
left_value = 0 if j - 1 < 0 or j - 1 >= n else dp[i][j - 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.
이렇게 하시는 것보다 17번 줄 근처에서 애초에 dp를 m x n 크기의 2차원 배열로 초기화시키고 시작하는게 더 깔끔할 것 같아요
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 배열 만들 때 1로 초기화 시키고, 배열 인덱스 경계 체크 없이 1부터 m, 1부터 n 계산하도록 했습니다. 리뷰 주신대로 훨씬 깔끔해졌네요 감사해요 항상 👍 (반영이 늦어서 다음 PR 에 포함될 예정입니다 🙏)
def uniquePaths(self, m: int, n: int) -> int:
dp = [[1] * n] * m
for i in range(1, m):
for j in range(1, n):
dp[i][j] = dp[i - 1][j] + dp[i][j - 1]
return dp[m - 1][n - 1]
1) for iteration 을 도는 동안 hash set 을 이용해 처음 발견한 원소들을 window set에 넣는다. | ||
2) 중복되는 원소를 발견할 경우 해당 원소의 중복이 사라질때까지 left side 의 원소들을 하나씩 제거한다. | ||
|
||
Time: O(n^2) = O(n) (for iteration) * O(n) 최악의 경우 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.
각 문자마다 최대 2번의 연산만 실행됩니다
- window에 추가하는 경우
- window에서 삭제하는 경우
그럼 시간 복잡도는 O(2N) = 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.
O(2N) => O(N) 이 맞는것 같습니다.
loop 내부에서 연산을 한다는 것만으로 관성적으로 곱셈으로 생각한것 같습니다. 감사합니다..!
답안 제출 문제
체크 리스트
In Review
로 설정해주세요.