Skip to content

Commit 05daec6

Browse files
committed
README + 624
1 parent 51f740b commit 05daec6

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
Time spent : 20 min
2+
3+
GIVEN: a list of integer list
4+
5+
RETURNS: the maximum difference between two integers from two different arrays
6+
7+
8+
9+
EXAMPLES:
10+
11+
Input:
12+
13+
```
14+
[[1,2,3],
15+
[4,5],
16+
[1,2,3]]
17+
```
18+
19+
Output: 4
20+
21+
DESIGN STRATEGY:
22+
23+
Since the arrays are already sorted, we don't need to compare every element of the arrays.
24+
25+
The two points being considered for the distance calculation should not both belong to the same array.
26+
27+
Thus, for arrays a and b currently chosen, we can just find the maximum out of a[n - 1] - b[0] and b[m - 1] - a[0] to find the larger distance.
28+
29+
And we keep a track of the element with minimum value and the one with maximum value found so far.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public int maxDistance(List<List<Integer>> arrays) {
3+
int tailIndex = arrays.get(0).size() - 1;
4+
int max = arrays.get(0).get(tailIndex);
5+
int min = arrays.get(0).get(0);
6+
int res = 0;
7+
for (int i = 1; i < arrays.size(); i++) {
8+
tailIndex = arrays.get(i).size() - 1;
9+
int diff1 = Math.abs(max - arrays.get(i).get(0));
10+
int diff2 = Math.abs(arrays.get(i).get(tailIndex) - min);
11+
res = Math.max(res, Math.max(diff1, diff2));
12+
max = Math.max(max, arrays.get(i).get(tailIndex));
13+
min = Math.min(min, arrays.get(i).get(0));
14+
}
15+
return res;
16+
}
17+
}

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@
167167
| 163 | 326 | [Power of Three][326] | Java | Easy | 2017.11.27 | Math | |
168168
| 164 | 654 | [Maximum Binary Tree][654] | Java | Easy | 2017.11.27 | Tree | |
169169
| 165 | 191 | [Number of 1 Bits][191] | Java | Easy | 2017.11.27 | Bit manipulation | |
170+
| 166 | 624 | [Maximum Distance in Arrays][624] | Java | Easy | 2017.11.27 | Array | |
170171

171172

172173
[1]:<https://leetcode.com/problems/two-sum/> "Two Sum"
@@ -338,6 +339,7 @@
338339
[605]:<https://leetcode.com/problems/can-place-flowers/> "Can Place Flowers"
339340
[606]:<https://leetcode.com/problems/construct-string-from-binary-tree/> "Construct String from Binary Tree"
340341
[617]:<https://leetcode.com/problems/merge-two-binary-trees/> "Merge Two Binary Trees"
342+
[624]:<https://leetcode.com/problems/maximum-distance-in-arrays/> "Maximum Distance in Arrays"
341343
[628]:<https://leetcode.com/problems/maximum-product-of-three-numbers/> "Maximum Product of Three Numbers"
342344
[637]:<https://leetcode.com/problems/average-of-levels-in-binary-tree/> "Average of Levels in Binary tree"
343345
[643]:<https://leetcode.com/problems/maximum-average-subarray-i/> "Maximum Average Subarray I"

0 commit comments

Comments
 (0)