|
38 | 38 |
|
39 | 39 | <!-- 这里可写通用的实现逻辑 -->
|
40 | 40 |
|
| 41 | +**方法一:数学 + 枚举** |
| 42 | + |
| 43 | +我们不妨令 $x_i = arr1[i]$, $y_i = arr2[i]$,由于 $i$ 和 $j$ 的大小关系不影响表达式的值,我们不妨假设 $i \ge j$,那么表达式可以变为: |
| 44 | + |
| 45 | +$$ |
| 46 | +| x_i - x_j | + | y_i - y_j | + i - j = \max \begin{cases} (x_i + y_i) - (x_j + y_j) \\ (x_i - y_i) - (x_j - y_j) \\ (-x_i + y_i) - (-x_j + y_j) \\ (-x_i - y_i) - (-x_j - y_j) \end{cases} + i - j\\ |
| 47 | += \max \begin{cases} (x_i + y_i + i) - (x_j + y_j + j) \\ (x_i - y_i + i) - (x_j - y_j + j) \\ (-x_i + y_i + i) - (-x_j + y_j + j) \\ (-x_i - y_i + i) - (-x_j - y_j + j) \end{cases} |
| 48 | +$$ |
| 49 | + |
| 50 | +因此,我们只要求出 $a \times x_i + b \times y_i + i$ 的最大值 $mx$,以及最小值 $mi$,其中 $a, b \in \{-1, 1\}$。那么答案就是所有 $mx - mi$ 中的最大值。 |
| 51 | + |
| 52 | +时间复杂度 $O(n)$,其中 $n$ 是数组长度。空间复杂度 $O(1)$。 |
| 53 | + |
| 54 | +相似题目: |
| 55 | + |
| 56 | +- [1330. 翻转子数组得到最大的数组值](/solution/1300-1399/1330.Reverse%20Subarray%20To%20Maximize%20Array%20Value/README.md) |
| 57 | + |
41 | 58 | <!-- tabs:start -->
|
42 | 59 |
|
43 | 60 | ### **Python3**
|
44 | 61 |
|
45 | 62 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
46 | 63 |
|
47 | 64 | ```python
|
48 |
| - |
| 65 | +class Solution: |
| 66 | + def maxAbsValExpr(self, arr1: List[int], arr2: List[int]) -> int: |
| 67 | + dirs = (1, -1, -1, 1, 1) |
| 68 | + ans = -inf |
| 69 | + for a, b in pairwise(dirs): |
| 70 | + mx, mi = -inf, inf |
| 71 | + for i, (x, y) in enumerate(zip(arr1, arr2)): |
| 72 | + mx = max(mx, a * x + b * y + i) |
| 73 | + mi = min(mi, a * x + b * y + i) |
| 74 | + ans = max(ans, mx - mi) |
| 75 | + return ans |
49 | 76 | ```
|
50 | 77 |
|
51 | 78 | ### **Java**
|
52 | 79 |
|
53 | 80 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
54 | 81 |
|
55 | 82 | ```java
|
| 83 | +class Solution { |
| 84 | + public int maxAbsValExpr(int[] arr1, int[] arr2) { |
| 85 | + int[] dirs = {1, -1, -1, 1, 1}; |
| 86 | + final int inf = 1 << 30; |
| 87 | + int ans = -inf; |
| 88 | + int n = arr1.length; |
| 89 | + for (int k = 0; k < 4; ++k) { |
| 90 | + int a = dirs[k], b = dirs[k + 1]; |
| 91 | + int mx = -inf, mi = inf; |
| 92 | + for (int i = 0; i < n; ++i) { |
| 93 | + mx = Math.max(mx, a * arr1[i] + b * arr2[i] + i); |
| 94 | + mi = Math.min(mi, a * arr1[i] + b * arr2[i] + i); |
| 95 | + ans = Math.max(ans, mx - mi); |
| 96 | + } |
| 97 | + } |
| 98 | + return ans; |
| 99 | + } |
| 100 | +} |
| 101 | +``` |
| 102 | + |
| 103 | +### **C++** |
| 104 | + |
| 105 | +```cpp |
| 106 | +class Solution { |
| 107 | +public: |
| 108 | + int maxAbsValExpr(vector<int>& arr1, vector<int>& arr2) { |
| 109 | + int dirs[5] = {1, -1, -1, 1, 1}; |
| 110 | + const int inf = 1 << 30; |
| 111 | + int ans = -inf; |
| 112 | + int n = arr1.size(); |
| 113 | + for (int k = 0; k < 4; ++k) { |
| 114 | + int a = dirs[k], b = dirs[k + 1]; |
| 115 | + int mx = -inf, mi = inf; |
| 116 | + for (int i = 0; i < n; ++i) { |
| 117 | + mx = max(mx, a * arr1[i] + b * arr2[i] + i); |
| 118 | + mi = min(mi, a * arr1[i] + b * arr2[i] + i); |
| 119 | + ans = max(ans, mx - mi); |
| 120 | + } |
| 121 | + } |
| 122 | + return ans; |
| 123 | + } |
| 124 | +}; |
| 125 | +``` |
| 126 | +
|
| 127 | +### **Go** |
| 128 | +
|
| 129 | +```go |
| 130 | +func maxAbsValExpr(arr1 []int, arr2 []int) int { |
| 131 | + dirs := [5]int{1, -1, -1, 1, 1} |
| 132 | + const inf = 1 << 30 |
| 133 | + ans := -inf |
| 134 | + for k := 0; k < 4; k++ { |
| 135 | + a, b := dirs[k], dirs[k+1] |
| 136 | + mx, mi := -inf, inf |
| 137 | + for i, x := range arr1 { |
| 138 | + y := arr2[i] |
| 139 | + mx = max(mx, a*x+b*y+i) |
| 140 | + mi = min(mi, a*x+b*y+i) |
| 141 | + ans = max(ans, mx-mi) |
| 142 | + } |
| 143 | + } |
| 144 | + return ans |
| 145 | +} |
| 146 | +
|
| 147 | +func max(a, b int) int { |
| 148 | + if a > b { |
| 149 | + return a |
| 150 | + } |
| 151 | + return b |
| 152 | +} |
| 153 | +
|
| 154 | +func min(a, b int) int { |
| 155 | + if a < b { |
| 156 | + return a |
| 157 | + } |
| 158 | + return b |
| 159 | +} |
| 160 | +``` |
56 | 161 |
|
| 162 | +### **TypeScript** |
| 163 | + |
| 164 | +```ts |
| 165 | +function maxAbsValExpr(arr1: number[], arr2: number[]): number { |
| 166 | + const dirs = [1, -1, -1, 1, 1]; |
| 167 | + const inf = 1 << 30; |
| 168 | + let ans = -inf; |
| 169 | + for (let k = 0; k < 4; ++k) { |
| 170 | + const [a, b] = [dirs[k], dirs[k + 1]]; |
| 171 | + let mx = -inf; |
| 172 | + let mi = inf; |
| 173 | + for (let i = 0; i < arr1.length; ++i) { |
| 174 | + const [x, y] = [arr1[i], arr2[i]]; |
| 175 | + mx = Math.max(mx, a * x + b * y + i); |
| 176 | + mi = Math.min(mi, a * x + b * y + i); |
| 177 | + ans = Math.max(ans, mx - mi); |
| 178 | + } |
| 179 | + } |
| 180 | + return ans; |
| 181 | +} |
57 | 182 | ```
|
58 | 183 |
|
59 | 184 | ### **...**
|
|
0 commit comments