File tree Expand file tree Collapse file tree 3 files changed +48
-0
lines changed
Array/624. Maximum Distance in Arrays Expand file tree Collapse file tree 3 files changed +48
-0
lines changed Original file line number Diff line number Diff line change
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.
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 167
167
| 163 | 326 | [ Power of Three] [ 326 ] | Java | Easy | 2017.11.27 | Math | |
168
168
| 164 | 654 | [ Maximum Binary Tree] [ 654 ] | Java | Easy | 2017.11.27 | Tree | |
169
169
| 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 | |
170
171
171
172
172
173
[ 1 ] :< https://leetcode.com/problems/two-sum/ > " Two Sum "
338
339
[ 605 ] :< https://leetcode.com/problems/can-place-flowers/ > " Can Place Flowers "
339
340
[ 606 ] :< https://leetcode.com/problems/construct-string-from-binary-tree/ > " Construct String from Binary Tree "
340
341
[ 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 "
341
343
[ 628 ] :< https://leetcode.com/problems/maximum-product-of-three-numbers/ > " Maximum Product of Three Numbers "
342
344
[ 637 ] :< https://leetcode.com/problems/average-of-levels-in-binary-tree/ > " Average of Levels in Binary tree "
343
345
[ 643 ] :< https://leetcode.com/problems/maximum-average-subarray-i/ > " Maximum Average Subarray I "
You can’t perform that action at this time.
0 commit comments