Skip to content

Prepare For Coder Interview – Denny

  • Basic
  • Medium
  • Hard
  • Architect
  • Life

LeetCode: Maximum of Absolute Value Expression

Posted on February 20, 2018July 26, 2020 by braindenny

Maximum of Absolute Value Expression



Similar Problems:

  • LeetCode: Reverse Subarray To Maximize Array Value
  • CheatSheet: Leetcode For Code Interview
  • CheatSheet: Common Code Problems & Follow-ups
  • Tag: #array, #inspiring

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:

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

Github: code.dennyzhang.com

Credits To: leetcode.com

Leave me comments, if you have better ways to solve.


  • Solution:
// https://code.dennyzhang.com/maximum-of-absolute-value-expression
// Basic Ideas:
//
//  Notice
//  1. To get max(|f(i)-f(j)|), find the smallest and biggest of f(i)
//  2. abs(A) + abs(B) = max(A+B, A-B, -A+B, -A-B)
//
//  max(|arr1[i] - arr1[j]| + |arr2[i] - arr2[j]| + |i - j|)
//      max(|(arr1[i]+arr2[i]+i)-(arr1[j]+arr2[j]+j|),
//          |(arr1[i]+arr2[i]-i)-(arr1[j]+arr2[j]-j)|,
//          |(arr1[i]-arr2[i]+i)-(arr1[j]-arr2[j]+j)|,
//          |(arr1[i]-arr2[i]-i)-(arr1[j]-arr2[j]-j)|)
//
//  Since we only need to choose two elements, we can sort the arrays.
//  But this turns out will not help to speed up the search
//
// Complexity: Time O(n), Space O(1)
// Basic Ideas: math
//
//  max(|arr1[i] - arr1[j]| + |arr2[i] - arr2[j]| + |i - j|)
//      max(|(arr1[i]+arr2[i]+i)-(arr1[j]+arr2[j]+j|),
//          |(arr1[i]+arr2[i]-i)-(arr1[j]+arr2[j]-j)|,
//          |(arr1[i]-arr2[i]+i)-(arr1[j]-arr2[j]+j)|,
//          |(arr1[i]-arr2[i]-i)-(arr1[j]-arr2[j]-j)|)
//
// Complexity: Time O(n), Space O(1)
func maxAbsValExpr(arr1 []int, arr2 []int) int {
    smalls := []int{1<<32-1, 1<<32-1, 1<<32-1, 1<<32-1}
    bigs := []int{-1<<32, -1<<32, -1<<32, -1<<32}
    for i, _ := range arr1 {
        for j, v := range []int{arr1[i]+arr2[i]+i, 
                               arr1[i]+arr2[i]-i,
                               arr1[i]-arr2[i]+i,
                                arr1[i]-arr2[i]-i} {
            if v < smalls[j] {
                smalls[j] = v
            }
            if v > bigs[j] {
                bigs[j] = v
            }
        }
    }
    res := 0
    for j:=0; j<=3; j++ {
        if bigs[j]-smalls[j] > res {
            res = bigs[j]-smalls[j]
        }
    }
    return res
}
linkedin
github
slack

Post Views: 7
Posted in MediumTagged #array, #inspiring

Post navigation

LeetCode: Next Greater Element II
LeetCode: Queue Reconstruction by Height

Leave a Reply Cancel reply

Your email address will not be published.

Tags

#array #backtracking #bfs #binarytree #bitmanipulation #blog #classic #codetemplate #combination #dfs #dynamicprogramming #game #graph #greedy #heap #inspiring #interval #linkedlist #manydetails #math #palindrome #recursive #slidingwindow #stack #string #subarray #trie #twopointer #twosum binarysearch editdistance hashmap intervaldp knapsack monotone oodesign presum rectangle redo review rotatelist series sql treetraversal unionfind

Recent Posts

  • a
  • a
  • a
  • a
  • a

Recent Comments

    Archives

    Categories

    • Amusing
    • Basic
    • Easy
    • Hard
    • Life
    • Medium
    • Resource
    • Review
    • Series
    • Uncategorized
    Proudly powered by WordPress | Theme: petals by Aurorum.