-
Notifications
You must be signed in to change notification settings - Fork 1
[2023-09-20] dohyun #239 #257
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
""" | ||
|
||
풀이시간 | ||
- 약 15분 | ||
|
||
접근법 | ||
- N이 1만 -> O(N^2) ~ O(NlogN) | ||
- 유저가 나중에 닉변하는 경우를 생각해서 결과를 순회할 때 배열에 저장하는 행위를 한번 해야함 | ||
- 고유 아이디는 그대로 -> 이거를 딕셔너리로 활용하자 | ||
|
||
회고 | ||
- 시간복잡도나 코드를 조금 더 간결하게 할 수 있을 것 같다는 생각 | ||
- 여러분들의 코드를 잘 공부해보겠습니다!! | ||
|
||
""" | ||
|
||
from collections import defaultdict | ||
|
||
def solution(records): | ||
users = defaultdict() | ||
answer = [] | ||
|
||
for record in records: | ||
behavior, code, *user = record.split() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. 오 저도 몰랐던 내용이네요! 하지만 협업할 때 이렇게 사용하기 위해서는 따로 명시를 해줘야 할 것 같아요 ㅎㅎ;; |
||
|
||
if user: # 닉네임이 비어있지 않은 경우 닉네임 갱신(Leave 가 아닌 경우) | ||
users[code] = user # {유저의 고유 아이디 : 닉네임} | ||
|
||
if behavior == "Enter": | ||
answer.append([code, "님이 들어왔습니다."]) | ||
elif behavior == "Leave": | ||
answer.append([code, "님이 나갔습니다."]) | ||
|
||
for i in range(len(answer)): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. 현재 순회중인 인덱스 이전의 원소만 업데이트 하는 건 크게 문제가 안될 것 같지만(dp문제에서 그러듯이), 만약 다시 현재 인덱스를 방문해야 하거나, 리스트 원소의 개수가 바뀌어 버리는 경우 안전하지 않은 게 맞습니다! 실제로 순회 중에 리스트 원소 개수가 바뀌면 에러납니다! 참고하시면 좋을 것 같아요. |
||
code, printer = answer[i] | ||
answer[i] = users[code][0] + printer # 유저의 고유 아이디를 닉네임으로 변경하며 완전한 문장형태로 변환 | ||
|
||
return answer |
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.
굳이 defaultdict가 아니라 그냥 dict로 써줘도 될 것 같아요!