Skip to content

Commit 8b6c665

Browse files
committed
add prob #1131
1 parent d84017c commit 8b6c665

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
Given two arrays of integers with equal lengths, return the maximum value of:
3+
4+
|arr1[i] - arr1[j]| + |arr2[i] - arr2[j]| + |i - j|
5+
6+
where the maximum is taken over all 0 <= i, j < arr1.length.
7+
8+
 
9+
10+
Example 1:
11+
12+
Input: arr1 = [1,2,3,4], arr2 = [-1,4,5,6]
13+
Output: 13
14+
Example 2:
15+
16+
Input: arr1 = [1,-2,-5,0,10], arr2 = [0,-2,-1,-7,-4]
17+
Output: 20
18+
 
19+
20+
Constraints:
21+
22+
2 <= arr1.length == arr2.length <= 40000
23+
-10^6 <= arr1[i], arr2[i] <= 10^6
24+
25+
来源:力扣(LeetCode)
26+
链接:https://leetcode-cn.com/problems/maximum-of-absolute-value-expression
27+
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
28+
*/
29+
30+
/* O(N) in time and O(1) in space */
31+
#include <vector>
32+
using namespace std;
33+
34+
class Solution {
35+
public:
36+
int maxAbsValExpr(vector<int>& arr1, vector<int>& arr2) {
37+
vector<int> max_val(8, 0);
38+
vector<int> min_val(8, 0);
39+
int i;
40+
41+
for (i = 0; i < (int)arr1.size(); ++i) {
42+
int temp1 = arr1[i] + arr2[i] + i;
43+
int temp2 = arr1[i] + arr2[i] - i;
44+
int temp3 = arr1[i] - arr2[i] + i;
45+
int temp4 = arr1[i] - arr2[i] - i;
46+
int temp5 = -arr1[i] + arr2[i] + i;
47+
int temp6 = -arr1[i] + arr2[i] - i;
48+
int temp7 = -arr1[i] - arr2[i] + i;
49+
int temp8 = -arr1[i] - arr2[i] - i;
50+
if(i==0){
51+
max_val[0] = temp1;
52+
max_val[1] = temp2;
53+
max_val[2] = temp3;
54+
max_val[3] = temp4;
55+
max_val[4] = temp5;
56+
max_val[5] = temp6;
57+
max_val[6] = temp7;
58+
max_val[7] = temp8;
59+
min_val[0] = temp1;
60+
min_val[1] = temp2;
61+
min_val[2] = temp3;
62+
min_val[3] = temp4;
63+
min_val[4] = temp5;
64+
min_val[5] = temp6;
65+
min_val[6] = temp7;
66+
min_val[7] = temp8;
67+
}
68+
else{
69+
max_val[0] = max(temp1, max_val[0]);
70+
max_val[1] = max(temp2, max_val[1]);
71+
max_val[2] = max(temp3, max_val[2]);
72+
max_val[3] = max(temp4, max_val[3]);
73+
max_val[4] = max(temp5, max_val[4]);
74+
max_val[5] = max(temp6, max_val[5]);
75+
max_val[6] = max(temp7, max_val[6]);
76+
max_val[7] = max(temp8, max_val[7]);
77+
min_val[0] = min(temp1, min_val[0]);
78+
min_val[1] = min(temp2, min_val[1]);
79+
min_val[2] = min(temp3, min_val[2]);
80+
min_val[3] = min(temp4, min_val[3]);
81+
min_val[4] = min(temp5, min_val[4]);
82+
min_val[5] = min(temp6, min_val[5]);
83+
min_val[6] = min(temp7, min_val[6]);
84+
min_val[7] = min(temp8, min_val[7]);
85+
}
86+
}
87+
88+
int res=0;
89+
for (i = 0; i < (int)max_val.size(); ++i) {
90+
res = max(max_val[i]-min_val[i], res);
91+
}
92+
return res;
93+
}
94+
};

0 commit comments

Comments
 (0)