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[0] and point1[1] != point2[1]:
if point1[0] < point2[0] and point1[1] < point2[1]:
point1[0] += 1
point1[1] += 1
elif point1[0] < point2[0] and point1[1] > point2[1]:
point1[0] += 1
point1[1] -= 1
elif point1[0] > point2[0] and point1[1] > point2[1]:
point1[0] -= 1
point1[1] -= 1
elif point1[0] > point2[0] and 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(1, len(points)):
answer += sol(points[i - 1], points[i])
return answer
|
cs |
반응형
'python-algorithm' 카테고리의 다른 글
leetcode 2000. Reverse Prefix of Word (0) | 2024.05.02 |
---|---|
leetcode 3131. Find the Integer Added to Array I (0) | 2024.04.30 |
leetcode 2855. Minimum Right Shifts to Sort the Array (0) | 2024.04.26 |
leetcode 3074. Apple Redistribution into Boxes (0) | 2024.04.25 |
leetcode 1137. N-th Tribonacci Number (0) | 2024.04.25 |
댓글