Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 63cadcb

Browse files
committed
Solution added.
1 parent f481e50 commit 63cadcb

File tree

1 file changed

+24
-0
lines changed
  • 30 Days of October Challange/Week 1/8. Maximum Distance in Arrays

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
"""
2+
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.
3+
4+
Example 1:
5+
Input:
6+
[[1,2,3],
7+
[4,5],
8+
[1,2,3]]
9+
Output: 4
10+
Explanation:
11+
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.
12+
Note:
13+
Each given array will have at least 1 number. There will be at least two non-empty arrays.
14+
The total number of the integers in all the m arrays will be in the range of [2, 10000].
15+
The integers in the m arrays will be in the range of [-10000, 10000].
16+
"""
17+
class Solution:
18+
def maxDistance(self, arrays: List[List[int]]) -> int:
19+
diff = 0
20+
min_val, max_val = arrays[0][0], arrays[0][-1]
21+
for array in arrays[1:]:
22+
diff = max(diff, max(abs(array[0] - max_val), abs(array[-1] - min_val)))
23+
max_val, min_val = max(max_val, array[-1]), min(min_val, array[0])
24+
return diff

0 commit comments

Comments
 (0)