일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- programmers
- Stack
- 조합
- coding test
- coding
- lv4
- lambda
- 데이터분석
- import re
- 코딩
- level4
- 코테
- python
- 연습문제
- sql
- time complexity
- join
- counter
- itertools
- mysql
- 완전탐색
- Level2
- BFS
- Queue
- collections
- CodingTest
- 코딩테스트
- 시간복잡도
- 파이썬
- 프로그래머스
- Today
- Total
ror_coding
[Programmers] 방문 길이 - 49994 본문
728x90
set()을 이용해서 중복은 자동으로 안 겹치게 하기.
list로 풀었는데 이 방법이 더 좋은 것 같다 !
Question
명령어가 매개변수 dirs로 주어질 때, 게임 캐릭터가 처음 걸어본 길의 길이를 구하여 return 하는 solution 함수를 완성해 주세요.
Code
def solution(dirs):
x, y, paths = 0, 0, set()
ways = {'U':[0,1], 'D':[0,-1], 'L':[-1,0], 'R':[1,0]}
for i in dirs:
dx = x + ways[i][0]
dy = y + ways[i][1]
if ((-5 <= dx <= 5) and (-5 <= dy <= 5)):
paths.add((x,y,dx,dy))
paths.add((dx,dy,x,y))
x, y = dx, dy
return len(paths)//2
![](https://t1.daumcdn.net/keditor/emoticon/friends1/large/012.gif)
now me
On my github
728x90
'Algorithm > Python' 카테고리의 다른 글
[Programmers] 게임 맵 최단거리 - 1844 (0) | 2024.12.08 |
---|---|
[Programmers] 뒤에 있는 큰 수 찾기 - 154539 (0) | 2024.12.07 |
[Programmers] [3차] n진수 게임 - 17687 (0) | 2024.12.07 |
[Programmers] 모음 사전 - 84512 (0) | 2024.12.05 |
[Programmers] 롤케이크 자르기 - 132265 (1) | 2024.12.05 |