Skip to content
This repository was archived by the owner on Mar 18, 2024. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions Programmers/방문 길이/wooyeol.py
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):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

위 if-else 구절을 조금만 리팩토링 하면 이 친구도 끼어넣을 수 있을 것 같아요! 그 편이 더 깔끔하다는 생각힙니다!

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)]):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

우열님 아래에서 양방향 처리를 해주기 때문에

and ((cur_x, cur_y) not in visited[(prev_x, prev_y)])

부분은 필요가 없는 것 같습니다!!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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))