ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 방문길이
    코딩 테스트/Level 3 2020. 9. 22. 00:21
    반응형

    방문 길이
    Summer/Winter Coding(~2018)
    1364명 완료

    어렵지 않은데 유독 완료 수가 작다...

    아래 풀이를 보지 말고 자신 있게 풀어보시길 ...

    https://programmers.co.kr/learn/courses/30/lessons/49994

     

    코딩테스트 연습 - 방문 길이

     

    programmers.co.kr

    def solution(dirs):
        paths, x, y = set(), 0, 0
        for i in dirs:
            prev_x, prev_y = x, y
            if i == "U" and y + 1 < 6:
                y += 1
                paths.add((prev_x, prev_y, x, y) if prev_x > x or prev_y > y else (x, y, prev_x, prev_y))
            elif i == "D" and y - 1 > -6:
                y -= 1
                paths.add((prev_x, prev_y, x, y) if prev_x > x or prev_y > y else (x, y, prev_x, prev_y))
            elif i == "R" and x + 1 < 6:
                x += 1
                paths.add((prev_x, prev_y, x, y) if prev_x > x or prev_y > y else (x, y, prev_x, prev_y))
            elif i == "L" and x - 1 > -6:
                x -= 1
                paths.add((prev_x, prev_y, x, y) if prev_x > x or prev_y > y else (x, y, prev_x, prev_y))
        return len(paths)

     

    반응형
Designed by Tistory.