Talentd
FleetCode

1131. Maximum of Absolute Value Expression

Medium49.0% acceptance2 companies

Given two arrays of integers with equal lengths, return the maximum value of:

|arr1[i] - arr1[j]| + |arr2[i] - arr2[j]| + |i - j|

where the maximum is taken over all 0 <= i, j < arr1.length.

Example 1:

Input: arr1 = [1,2,3,4], arr2 = [-1,4,5,6]
Output: 13

Example 2:

Input: arr1 = [1,-2,-5,0,10], arr2 = [0,-2,-1,-7,-4]
Output: 20

Constraints:

  • 2 <= arr1.length == arr2.length <= 40000
  • -10^6 <= arr1[i], arr2[i] <= 10^6

Asked at 2 companies

Topics

Hints open one at a time

Hint 1
Use the idea that abs(A) + abs(B) = max(A+B, A-B, -A+B, -A-B).
34:09
Still stuck?Read 3 approaches, or watch a walkthrough.
Open solution
Loading editor…
1class Solution:
2 def maxAbsValExpr(self, arr1: List[int], arr2: List[int]) -> int:
3
Starter codePython 3Spaces: 4
Examples
Input
[1,2,3,4]
[-1,4,5,6]
Input
[1,-2,-5,0,10]
[0,-2,-1,-7,-4]
Your code is saved on this device.Solution