【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 参考资料 日期 题目 ...
随机推荐
- Linux下安装redis-4.0.10
1.下载redis-4.0.10 在redis官网(https://redis.io/download)下载redis-4.0.10 2.将安装包上传至Linux服务器 在Linux服务器根目录下创建 ...
- django的url的name参数的意义及view中reverse
Templates的链接地址都是根据urlpatterns定义的地址,拼凑成地址字符串,很难看,而且Templates里拼凑成的地址,随着页面的增加而不断增加,一旦在urlpatterns里的某个地址 ...
- 6.824 Lab 3: Fault-tolerant Key/Value Service 3A
6.824 Lab 3: Fault-tolerant Key/Value Service Due Part A: Mar 13 23:59 Due Part B: Apr 10 23:59 Intr ...
- CSS3——表单 计数器 网页布局 应用实例
CSS应用实例 表单 实例 输入框样式 输入框填充-----内边距 输入框------边框 输入框-----颜色 输入框-----聚焦 输入框-------图标 输入框------动画 [自动右滑] ...
- 【FICO系列】SAP FICO-模块 关于固定资产年结和折旧的问题
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[FICO系列]SAP FICO-模块 关于固定 ...
- netcat命令用法
1,端口扫描nc -z -v -n 172.31.100.7 21-25 2,聊天Server:nc -l 1567Client:nc 172.31.100.7 1567 3,文件传输Server:n ...
- spring -boot定时任务 quartz 基于 JobDetailFactoryBean实现
这个有点小问题 尚未解决 后期优化 基于 JobDetailFactoryBean实现 依赖包 <dependencies> <dependency> <groupId ...
- netstat -anop|more 查看网络队列
nux下netstat --timers / -o详解及keepalive相关 第一列,一般有一下几种状态: keepalive - #表示是keepalive的时间计时 on - #表示是重发(re ...
- 【6.10校内test】T2 医院设置
医院设置[题目链接] 感觉我超废 我是一个连floyd都不会写了的灵魂OI选手qwq(考场上写了半天spfa然后写炸了(微笑)) floyd的暴力: 1.先建树:用邻接矩阵存.存之前记得先初始化为IN ...
- ubuntu系统更新命令
一.图形界面更新升级 1.点击”系统设置“,打开“软件和更新”,切到“更新”栏目进行更新设置. 2.可以通过软件更新器进行更新升级自己想要更新的 二.命令方式更新升级 1.先解锁 ps -e|grep ...