题目如下:

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

解题思路:对于表达式 |arr1[i] - arr1[j]| + |arr2[i] - arr2[j]| + |i - j|,在i < j的情况下,这个表达式的值是下面其中四个之一:

(arr1[i] + arr2[i] - i) -  (arr1[j] + arr2[j] - j)

(arr1[i] - arr2[i] - i)   -    (arr1[j] - arr2[j] - j)

(arr2[i] - arr1[i] - i)   -    (arr2[j] - arr1[j] - j)

(arr2[i] + arr1[i] + i)   -    (arr2[j] + arr1[j] + j)

所以只要遍历一次数组,求出四个表达式中差值的最大值和最小值即可。

代码如下:

class Solution(object):
def maxAbsValExpr(self, arr1, arr2):
"""
:type arr1: List[int]
:type arr2: List[int]
:rtype: int
"""
case_1_max = case_2_max = case_3_max = case_4_max = -float('inf')
case_1_min = case_2_min = case_3_min = case_4_min = float('inf')
for i in range(len(arr1)):
case_1_max = max(case_1_max,arr1[i] + arr2[i] - i)
case_1_min = min(case_1_min, arr1[i] + arr2[i] - i) case_2_max = max(case_2_max, arr1[i] - arr2[i] - i)
case_2_min = min(case_2_min, arr1[i] - arr2[i] - i) case_3_max = max(case_3_max, arr2[i] - arr1[i] - i)
case_3_min = min(case_3_min, arr2[i] - arr1[i] - i) case_4_max = max(case_4_max, arr2[i] + arr1[i] + i)
case_4_min = min(case_4_min, arr2[i] + arr1[i] + i) return max(case_1_max - case_1_min,case_2_max - case_2_min,case_3_max - case_3_min,case_4_max - case_4_min)

【leetcode】1131. Maximum of Absolute Value Expression的更多相关文章

  1. 【leetcode】998. Maximum Binary Tree II

    题目如下: We are given the root node of a maximum tree: a tree where every node has a value greater than ...

  2. 【LeetCode】895. Maximum Frequency Stack 解题报告(Python)

    [LeetCode]895. Maximum Frequency Stack 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxueming ...

  3. 【LeetCode】718. Maximum Length of Repeated Subarray 解题报告(Python)

    [LeetCode]718. Maximum Length of Repeated Subarray 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxu ...

  4. 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)

    [LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...

  5. 【Leetcode】164. Maximum Gap 【基数排序】

    Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...

  6. 【leetcode】1255. Maximum Score Words Formed by Letters

    题目如下: Given a list of words, list of  single letters (might be repeating) and score of every charact ...

  7. 【leetcode】1189. Maximum Number of Balloons

    题目如下: Given a string text, you want to use the characters of text to form as many instances of the w ...

  8. 【LeetCode】1161. Maximum Level Sum of a Binary Tree 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 日期 题目地址:https://leetcod ...

  9. 【LeetCode】104. Maximum Depth of Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:BFS 方法二:DFS 参考资料 日期 题目 ...

随机推荐

  1. nginx实现域名跳转

    server { listen 80; server_name www.dd.com www.tt.com; index index.html index.htm index.php; root /u ...

  2. 【Hibernate】---Query、Criteria、SQLQuery

    一.核心配置文件 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-con ...

  3. flask如何实现https以及自定义证书的制作

    http://blog.csdn.net/yannanxiu/article/details/70672744 http://blog.csdn.net/yannanxiu/article/detai ...

  4. TensorFlow2.0矩阵与向量的加减乘

    1.矩阵加法使用 a = np.random.random((3,3))b = np.random.randint(0,9,(3,3)) ad = tf.add(a,b) 2.矩阵乘法注意 # ten ...

  5. (ROT-13解密)Flare-On4: Challenge1 login.html

    说是FlareOn的逆向 倒不如说是crypto....... 题目不难 F12看源码: document.getElementById("prompt").onclick = f ...

  6. MySql日期加天数,小时,分钟...得到新的时间

    在当前的日期上加三天,天数随便改: SELECT date_add(CURRENT_DATE(), interval 3 day); 在指定的日期上加三天: SELECT date_add('2014 ...

  7. P1969积木大赛

    这是2018与2013提高组的真题,可怕,,原题出了两年,是个纯模拟. 读完题后就想写一个朴素的模拟,先遍历层数,再把达到层数的宽度#存起来,再判断是否连续,如果不连续ans++,然后每一次循环都要初 ...

  8. Nginx 配置二级虚拟目录访问 Laravel 重写

    server { listen 80; server_name _; root /opt/sites; index index.php index.html index.htm; etag on; g ...

  9. Redux 关系图解

    Redux是一款状态管理库,并且提供了react-redux库来与React亲密配合, 但是总是傻傻分不清楚这2者提供的API和相应的关系.这篇文章就来理一理. Redux Redux 三大核心 Re ...

  10. python学习第五十二天logging模块的使用

    很多程序都有记录日志的需求,并且日志包含的信息即有正常的程序访问日志,还可能有错误,警告等信息输出,python的 logging模块提供了标准的日志接口,你可以通过它存储各种格式的日志,loggin ...