This repository was archived by the owner on Mar 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
[2023-09-22] wooyeol #240 #264
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| """ | ||
| 방문 길이 | ||
| https://school.programmers.co.kr/learn/courses/30/lessons/49994 | ||
|
|
||
| 풀이시간 | ||
| 10:50 ~ 11:33 (43분) | ||
|
|
||
| 문제 조건 | ||
| 1 <= len(dirs) : D <= 500 | ||
|
|
||
| 시간 복잡도 : | ||
| O(D * (D + D)) = O(D^2) | ||
|
|
||
| 접근법 | ||
| 무슨 알고리즘으로 풀이 할 수 있을까? -> 시뮬레이션(해쉬 테이블) | ||
|
|
||
| - 방문했던 장소를 기억해야하는 해쉬 테이블 설계 | ||
|
|
||
| 1. 좌표를 업데이트하는데 범위 밖으로 넘어가서 이동하지 않으면 거리 체크 X | ||
| 2. 이동한 좌표와 이동하기 전 좌표의 이동한 길을 방문한적 있는지 확인 후 없으면 중복이 제거된 이동한 거리 증가 | ||
| - 무방향성으로 길을 체크해야하기 때문에 (이동 전 좌표에서 이동 후 좌표)와 (이동 후 좌표에서 이동 전 좌표) 모두 체크 | ||
| """ | ||
| from collections import defaultdict | ||
|
|
||
| def solution(dirs): | ||
| # 중복이 제거된 이동한 거리 | ||
| answer = 0 | ||
|
|
||
| # 지나간 길 해쉬 테이블 | ||
| visited = defaultdict(list) | ||
|
|
||
| # 현재 위치 초기화 | ||
| cur_x, cur_y = 0, 0 | ||
|
|
||
| # 주어진 방향에 따라 이동 시뮬레이션 | ||
| for d in dirs: | ||
| prev_y, prev_x = cur_y, cur_x | ||
|
|
||
| # 좌표 업데이트 | ||
| if d == "U": | ||
| if -5 <= cur_y+1 <= 5: | ||
| cur_y += 1 | ||
| elif d == "D": | ||
| if -5 <= cur_y-1 <= 5: | ||
| cur_y -= 1 | ||
| elif d == "R": | ||
| if -5 <= cur_x + 1 <= 5: | ||
| cur_x += 1 | ||
| elif d == "L": | ||
| if -5 <= cur_x - 1 <= 5: | ||
| cur_x -= 1 | ||
|
|
||
| # 이동했는지 여부 체크 | ||
| if (prev_x, prev_y) == (cur_x, cur_y): | ||
| continue | ||
|
|
||
| # 길 체크 | ||
| if ((prev_x, prev_y) not in visited[(cur_x, cur_y)]) and ((cur_x, cur_y) not in visited[(prev_x, prev_y)]): | ||
|
Contributor
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. 우열님 아래에서 양방향 처리를 해주기 때문에 부분은 필요가 없는 것 같습니다!!
Contributor
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. 도현님 말씀처럼 앞의 조건이나 뒤의 조건 중 하나만 써줘도 될 것 같아요 우열님!! 고생하셨습니다~~!!👍👍 |
||
| visited[(cur_x, cur_y)].append((prev_x, prev_y)) | ||
| visited[(prev_x, prev_y)].append((cur_x, cur_y)) | ||
| answer += 1 | ||
|
|
||
| return answer | ||
|
|
||
|
|
||
| case1 = "ULURRDLLU" # 7 | ||
| case2 = "LULLLLLLU" # 7 | ||
| case3 = "UDU" # 7 | ||
|
|
||
| # print(solution(case1)) | ||
| # print(solution(case2)) | ||
| print(solution(case3)) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
위 if-else 구절을 조금만 리팩토링 하면 이 친구도 끼어넣을 수 있을 것 같아요! 그 편이 더 깔끔하다는 생각힙니다!