Skip to content

Commit 2322a22

Browse files
add 624
1 parent 4785e2b commit 2322a22

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Your ideas/fixes/algorithms are more than welcome!
2020

2121
| # | Title | Solutions | Time | Space | Difficulty | Tag | Notes
2222
|-----|----------------|---------------|---------------|---------------|-------------|--------------|-----
23+
|624|[Maximum Distance in Arrays](https://leetcode.com/problems/maximum-distance-in-arrays/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_624.java) | O(nlogn) |O(1) | Easy | Sort, Array
2324
|617|[Merge Two Binary Trees](https://leetcode.com/problems/merge-two-binary-trees/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_617.java) | O(n) |O(h) | Easy | Tree, Recursion
2425
|616|[Add Bold Tag in String](https://leetcode.com/problems/add-bold-tag-in-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_616.java) | O(n*k) (n is length of string, k is size of dict) |O(n) | Medium | String
2526
|611|[Valid Triangle Number](https://leetcode.com/problems/valid-triangle-number/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_611.java) | O(n^2logn) |O(logn) | Medium | Binary Search
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.fishercoder.solutions;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.List;
6+
7+
/**
8+
* 624. Maximum Distance in Arrays
9+
*
10+
* Given m arrays, and each array is sorted in ascending order.
11+
* Now you can pick up two integers from two different arrays (each array picks one)
12+
* and calculate the distance. We define the distance between two
13+
* integers a and b to be their absolute difference |a-b|. Your task is to find the maximum distance.
14+
15+
Example 1:
16+
Input:
17+
[[1,2,3],
18+
[4,5],
19+
[1,2,3]]
20+
21+
Output: 4
22+
23+
Explanation:
24+
25+
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.
26+
27+
Note:
28+
Each given array will have at least 1 number. There will be at least two non-empty arrays.
29+
The total number of the integers in all the m arrays will be in the range of [2, 10000].
30+
The integers in the m arrays will be in the range of [-10000, 10000].
31+
*/
32+
public class _624 {
33+
34+
public int maxDistance(int[][] arrays) {
35+
List<Integer> max = new ArrayList<>();
36+
for (int[] array : arrays) {
37+
max.add(array[array.length-1]);
38+
}
39+
Collections.sort(max);
40+
int ans = Integer.MIN_VALUE;
41+
for (int[] array : arrays) {
42+
int big = array[array.length-1] == max.get(max.size()-1) ? max.get(max.size()-2) : max.get(max.size()-1);
43+
ans = Math.max(ans, big - array[0]);
44+
}
45+
return ans;
46+
}
47+
48+
}

0 commit comments

Comments
 (0)