题目如下:

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. Python基本语法_变量作用域LEGB

    目录 目录 软件系统 变量的作用域 高级语言对数据类型的使用过程 作用域的产生 作用域的类型 Llocal局部作用域 Eenclosing嵌套作用域 Gglobal全局作用域 Bbuilt-in内置作 ...

  2. 阶段3 1.Mybatis_12.Mybatis注解开发_5 mybatis注解建立实体类属性和数据库表中列的对应关系

    创建新项目,一对多 复制刚才关闭的项目的文件 复制到们的新项目里面 复制包的依赖 删减相关代码.只保留这三个查询的方法 模糊查询改成传统的占位符的方式 之前是可以自定义实体类的属性字段,和数据库的字典 ...

  3. 七:flask-一些小细节

    1.在局域网中,让其他电脑访问我的网站:host参数 如果设置为0.0.0.0,则在局域网中,输入当前项目所在的ip+端口就可以访问这个项目如果host设置为固定的ip,如host=‘'192.168 ...

  4. Python学习之==>json处理

    json是一种所有语言都通用的Key-Value数据结构的数据类型,很像Python中的字典,在Python中可以通过json模块对json串和字典进行转换. 1.把字典转换成json串 import ...

  5. 【Linux开发】【Qt开发】arm-linux-gnueabihf-gdb versus gdb-multiarch

    主要是说,在Ubuntu14.04 64bit的操作系统上,配置Qt的gdb和gcc的时候,在Qt build&run选项中,debugger中选中arm-linux-gnuabihf-gdb ...

  6. python+selenium操作cookie

    WebDriver提供了操作Cookie的相关方法,可以读取.添加和删除cookie信息. WebDriver操作cookie的方法: get_cookies(): 获得所有cookie信息. get ...

  7. Mysql 5.7存储过程的学习

    存储过程:对sql的封装和重用,经编译创建并保存在数据库中,通过指定存储过程的名字并给定参数(需要时)来调用执行. 优缺点: (1) 优点: 执行速度快------存储过程只在创建时进行编译,以后每次 ...

  8. Spring框架学习总结

    一.Spring概述 1.什么是Spring? Spring是一个优秀轻量级的框架,是Java中使用最多的框架,Spring框架具有轻量.控制反转.面向切面.容器.框架.MVC的特点. 2.Sprin ...

  9. Yii2.0 limit(1)与one()

    ActiveRecord中的limit(1)与one()的区别 (new \yii\db\Query())->from('user')->limit(1)->one() public ...

  10. python 发送kafka

    python 发送kafka大体有三种方式 1 发送并忘记(不关注是否正常到达,不对返回结果做处理) 1 import pickle 2 import time 3 from kafka import ...