-
Notifications
You must be signed in to change notification settings - Fork 0
/
MaximumAbsoluteDifference.java
84 lines (76 loc) · 2.14 KB
/
MaximumAbsoluteDifference.java
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
75
76
77
78
79
80
81
82
83
84
// https://www.interviewbit.com/problems/maximum-absolute-difference/
// #array
public class MaximumAbsoluteDifference {
public int maxArr(int[] A) {
/*
idea: break the expression to two case, and generate new Array for each case to calculate.
i < j: two case:
|A[i] - A[j]| + |i - j|
Case 1: A[i] > A[j]
-> A[i] - A[j] + j - i = (A[i] - i) - (A[j] - j)
=> new Array with element: A[i] - i
Case 2: A[i] < A[j]
-> A[j] - A[i] + j - i = -(A[i] + i) - (-(A[j] + j))
=> new Array with element: -(A[i] + i)
*/
// return solution1(A);
return solution2(A);
}
/*
- Time: 0(n)
- Space: 0(n)
*/
private int solution1(int[] A) {
// case 1
int[] case1_arr = new int[A.length];
for (int i = 0; i < A.length; i++) {
case1_arr[i] = A[i] - i;
}
int result1 = helper(case1_arr);
// case 2
int[] case2_arr = new int[A.length];
for (int i = 0; i < A.length; i++) {
case2_arr[i] = -1 * (A[i] + i);
}
int result2 = helper(case2_arr);
return Math.max(result1, result2);
}
private int helper(int[] arr) {
int min = arr[0];
int max = arr[0];
int result = 0;
for (int element : arr) {
min = Math.min(min, element);
max = Math.max(max, element);
result = Math.max(result, max - min);
}
return result;
}
/*
- Time: O(n)
- Space: O(1)
*/
private int solution2(int[] A) {
int result = 0;
int current_min1 = A[0];
int current_max1 = A[0];
int current_min2 = -A[0];
int current_max2 = -A[0];
int result1 = 0;
int result2 = 0;
for (int i = 1; i < A.length; i++) {
// case 1
int val1 = A[i] - i;
current_min1 = Math.min(current_min1, val1);
current_max1 = Math.max(current_max1, val1);
result1 = Math.max(result1, current_max1 - current_min1);
// case 2
int val2 = -(A[i] + i);
current_min2 = Math.min(current_min2, val2);
current_max2 = Math.max(current_max2, val2);
result2 = Math.max(result2, current_max2 - current_min2);
result = Math.max(result1, result2);
}
return result;
}
}