【leetcode】1131. Maximum of Absolute Value Expression
题目如下:
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: 13Example 2:
Input: arr1 = [1,-2,-5,0,10], arr2 = [0,-2,-1,-7,-4]
Output: 20Constraints:
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的更多相关文章
- 【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 ...
- 【LeetCode】895. Maximum Frequency Stack 解题报告(Python)
[LeetCode]895. Maximum Frequency Stack 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxueming ...
- 【LeetCode】718. Maximum Length of Repeated Subarray 解题报告(Python)
[LeetCode]718. Maximum Length of Repeated Subarray 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxu ...
- 【LeetCode】662. Maximum Width of Binary Tree 解题报告(Python)
[LeetCode]662. Maximum Width of Binary Tree 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.co ...
- 【Leetcode】164. Maximum Gap 【基数排序】
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- 【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 ...
- 【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 ...
- 【LeetCode】1161. Maximum Level Sum of a Binary Tree 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS 日期 题目地址:https://leetcod ...
- 【LeetCode】104. Maximum Depth of Binary Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:BFS 方法二:DFS 参考资料 日期 题目 ...
随机推荐
- 【Fiddler】开启手机的http或https抓包
fiddler安装 下载fiddler最新版: 默认安装: 打开fiddler工具,默认界面: 选择上方,Tools-→options General界面 HTTPS界面 CONNECTIONS,po ...
- LeetCode.859-伙伴字符串(Buddy Strings)
这是悦乐书的第330次更新,第354篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第200题(顺位题号是859).给定两个字母A和B的小写字母,当且仅当我们可以在A中交换 ...
- zip函数用于对列表对应元素打包成元组
zip() 函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表. 如果各个迭代器的元素个数不一致,则返回列表长度与最短的对象相同,利用 * 号操作符,可以 ...
- C函数调用过程原理及函数栈帧分析(转)
在x86的计算机系统中,内存空间中的栈主要用于保存函数的参数,返回值,返回地址,本地变量等.一切的函数调用都要将不同的数据.地址压入或者弹出栈.因此,为了更好地理解函数的调用,我们需要先来看看栈是怎么 ...
- ES6生成器与迭代器
ES6迭代器的一个例子 function run(taskDef) { var task = taskDef(); var result = task.next(); // 递归执行迭代 functi ...
- webpack打包错误 ERROR in multi ./src/main.js ./dist/bundle.js
webpack打包错误 ERROR in multi ./src/main.js ./dist/bundle.js:https://www.jianshu.com/p/a55fb5bf75e1
- Linux环境安装mongodb
介绍 上篇介绍了Linux环境下安装Node.js的步骤,紧接着来安装mongodb.另外,推荐我的另一篇 Windows下图文详解Mongodb安装及配置,先在Windows下熟悉下mongodb, ...
- Intellij IDEA关闭 Build窗口提示
如果开启了Tomcat的话,我们在idea中代码有错误,并且在切换软件的时候,Idea会不断提示编译错误(Build错误),这个会很烦人.我们可以先将Tomcat关掉,就不会一直弹出Build窗口了.
- 使用autotools自动生成Makefile并在此之上使用dh-make生成可发布的deb程序包(详解)
转自:http://blog.csdn.net/longerzone/article/details/12705507 一.前言 本文将介绍如何使用autotools生成一个Makefile文件,并在 ...
- loli的测试-2018.12.9
模拟赛-2018.12.9 这是NOIP之后第一次模拟赛...但是考的比较悲惨. 非常喜欢写考试总结,不知道为什么... T1:https://www.luogu.org/problemnew/sho ...