본문 바로가기
python-algorithm

leetcode 1266. Minimum Time Visiting All Points

by 무적김두칠 2024. 4. 27.

https://leetcode.com/problems/minimum-time-visiting-all-points/

 

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
class Solution:
    def minTimeToVisitAllPoints(self, points: List[List[int]]) -> int:
        def sol(point1, point2):
            cost = 0
 
            def move1():
                nonlocal cost
                while point1[0!= point2[0and point1[1!= point2[1]:
                    if point1[0< point2[0and point1[1< point2[1]:
                        point1[0+= 1
                        point1[1+= 1
 
                    elif point1[0< point2[0and point1[1> point2[1]:
                        point1[0+= 1
                        point1[1-= 1
                    elif point1[0> point2[0and point1[1> point2[1]:
                        point1[0-= 1
                        point1[1-= 1
                    elif point1[0> point2[0and point1[1< point2[1]:
                        point1[0-= 1
                        point1[1+= 1
 
                    cost += 1
 
            def move2():
                nonlocal cost
                if point1[0== point2[0]:
                    cost += abs(point1[1- point2[1])
                    point1[1= point2[1]
                elif point1[1== point2[1]:
                    cost += abs(point1[0- point2[0])
                    point1[0= point2[0]
 
            move1()
            move2()
            return cost
        answer = 0
 
 
        for i in range(1len(points)):
            answer += sol(points[i - 1], points[i])
        
        return answer
cs
반응형

댓글