1230. Maximum of Absolute Value Expression
My accepted Python solution to LeetCode problem 1230, Maximum of Absolute Value Expression, running in 37ms.
- Difficulty: Medium
- Python
- Runtime 37ms
- Memory 26.4MB
- Updated
Read the problem on LeetCode View on GitHub
The problem statement is LeetCode’s and stays on their site. What follows is my accepted solution.
Python
Accepted on LeetCode — runtime 37ms, memory 26.4MB, accepted 2026-01-02.
class Solution:
def maxAbsValExpr(self, arr1: List[int], arr2: List[int]) -> int:
# |arr1[i] - arr1[j]| + |arr2[i] - arr2[j]| + |i - j|
# Expand absolute values into 4 cases:
# Case 1: (arr1[i] + arr2[i] + i) - (arr1[j] + arr2[j] + j)
# Case 2: (arr1[i] + arr2[i] - i) - (arr1[j] + arr2[j] - j)
# Case 3: (arr1[i] - arr2[i] + i) - (arr1[j] - arr2[j] + j)
# Case 4: (arr1[i] - arr2[i] - i) - (arr1[j] - arr2[j] - j)
n = len(arr1)
result = 0
for p, q in [(1, 1), (1, -1), (-1, 1), (-1, -1)]:
# Calculate p*arr1[i] + q*arr2[i] + i for all i
vals = [p * arr1[i] + q * arr2[i] + i for i in range(n)]
result = max(result, max(vals) - min(vals))
return result