[Swift]LeetCode1131. 绝对值表达式的最大值 | Maximum of Absolute Value Expression

[Swift]LeetCode1131. 绝对值表达式的最大值 | Maximum of Absolute Value Expression,第1张

概述★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ?微信公众号:山青咏芝(shanqingyongzhi) ?博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/) ?GitHub地址:https://github.com/strengthen/LeetCode ?原文地址:https://www.cnblogs.com/st

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
?微信公众号:山青咏芝(shanqingyongzhi)
?博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)
?GitHub地址:https://github.com/strengthen/LeetCode
?原文地址:https://www.cnblogs.com/strengthen/p/11223723.html 
?如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
?原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

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,10],arr2 = [0,-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|

其中下标 ij 满足 0 <= i,j < arr1.length

示例 1:

输入:arr1 = [1,6]输出:13

示例 2:

输入:arr1 = [1,-4]输出:20

提示:

2 <= arr1.length == arr2.length <= 40000 -10^6 <= arr1[i],arr2[i] <= 10^6 Runtime: 200 ms Memory Usage: 21.3 MB
 1 class Solution { 2     func maxAbsValExpr(_ arr1: [Int],_ arr2: [Int]) -> Int { 3         var n:Int = arr1.count 4         var ans:Int = 0 5         var maxmm:Int = Int.min / 2 6         var maxmp:Int = Int.min / 2 7         var maxpm:Int = Int.min / 2 8         var maxpp:Int = Int.min / 2 9         for i in 0..<n10         {11             maxmm = max(maxmm,-arr1[i] - arr2[i]-i)12             maxmp = max(maxmp,-arr1[i] + arr2[i]-i)13             maxpm = max(maxpm,arr1[i] - arr2[i]-i)14             maxpp = max(maxpp,arr1[i] + arr2[i]-i)15             ans = max(ans,maxmm + arr1[i] + arr2[i]+i)16             ans = max(ans,maxmp + arr1[i] - arr2[i]+i)17             ans = max(ans,maxpm - arr1[i] + arr2[i]+i)18             ans = max(ans,maxpp - arr1[i] - arr2[i]+i)19         }20         return ans        21     }22 }
总结

以上是内存溢出为你收集整理的[Swift]LeetCode1131. 绝对值表达式的最大值 | Maximum of Absolute Value Expression全部内容,希望文章能够帮你解决[Swift]LeetCode1131. 绝对值表达式的最大值 | Maximum of Absolute Value Expression所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1001565.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-21
下一篇 2022-05-21

发表评论

登录后才能评论

评论列表(0条)

保存