-
Notifications
You must be signed in to change notification settings - Fork 43
/
maximum-distance-in-arrays.py
74 lines (64 loc) · 2.28 KB
/
maximum-distance-in-arrays.py
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"""
Given m arrays, and each array is sorted in ascending order. Now you can pick up two integers from two different arrays (each array picks one) and calculate the distance. We define the distance between two integers a and b to be their absolute difference |a-b|. Your task is to find the maximum distance.
Example 1:
Input:
[[1,2,3],
[4,5],
[1,2,3]]
Output: 4
Explanation:
One way to reach the maximum distance 4 is to pick 1 in the first or third array and pick 5 in the second array.
Note:
Each given array will have at least 1 number. There will be at least two non-empty arrays.
The total number of the integers in all the m arrays will be in the range of [2, 10000].
The integers in the m arrays will be in the range of [-10000, 10000].
"""
# V0
# V1
# https://blog.csdn.net/github_39261590/article/details/73655796
class Solution(object):
def maxDistance(self, arrays):
"""
:type arrays: List[List[int]]
:rtype: int
"""
len_arrays=len(arrays)
res=0
min_first=arrays[0][0]
max_last=arrays[0][len(arrays[0])-1]
for i in range(1,len_arrays):
array_i=arrays[i]
res=max(abs(array_i[len(array_i)-1]-min_first),abs(array_i[0]-max_last),res)
min_first=min(array_i[0],min_first)
max_last=max(array_i[len(array_i)-1],max_last)
return res
# V1'
# https://www.jianshu.com/p/8cdf02a4d30a
class Solution:
def maxDistance(self, arrays):
"""
:type arrays: List[List[int]]
:rtype: int
"""
res, curMin, curMax = 0, 10000, -10000
for a in arrays :
res = max(res, max(a[-1]-curMin, curMax-a[0]))
curMin, curMax = min(curMin, a[0]), max(curMax, a[-1])
return res
# V2
# Time: O(n)
# Space: O(1)
class Solution(object):
def maxDistance(self, arrays):
"""
:type arrays: List[List[int]]
:rtype: int
"""
result, min_val, max_val = 0, arrays[0][0], arrays[0][-1]
for i in range(1, len(arrays)):
result = max(result, \
max(max_val - arrays[i][0], \
arrays[i][-1] - min_val))
min_val = min(min_val, arrays[i][0])
max_val = max(max_val, arrays[i][-1])
return result