LeetCode 1131. 绝对值表达式的最大值(数学 绝对值展开)

文章目录

    • 1. 题目
    • 2. 解题

1. 题目

给你两个长度相等的整数数组,返回下面表达式的最大值

|arr1[i] - arr1[j]| + |arr2[i] - arr2[j]| + |i - j|

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

示例 1:
输入:arr1 = [1,2,3,4], arr2 = [-1,4,5,6]
输出:13

示例 2:
输入:arr1 = [1,-2,-5,0,10], arr2 = [0,-2,-1,-7,-4]
输出:20
 
提示:
2 <= arr1.length == arr2.length <= 40000
-10^6 <= arr1[i], arr2[i] <= 10^6

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/maximum-of-absolute-value-expression
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

2. 解题

只有这8种可能
a r r 1 [ i ] − a r r 1 [ j ] + a r r 2 [ i ] − a r r 2 [ j ] + i − j ; − a r r 1 [ i ] + a r r 1 [ j ] − a r r 2 [ i ] + a r r 2 [ j ] − i + j ; − a r r 1 [ i ] + a r r 1 [ j ] + a r r 2 [ i ] − a r r 2 [ j ] + i − j ; a r r 1 [ i ] − a r r 1 [ j ] − a r r 2 [ i ] + a r r 2 [ j ] + i − j ; a r r 1 [ i ] − a r r 1 [ j ] + a r r 2 [ i ] − a r r 2 [ j ] − i + j ; − a r r 1 [ i ] + a r r 1 [ j ] − a r r 2 [ i ] + a r r 2 [ j ] + i − j ; a r r 1 [ i ] − a r r 1 [ j ] − a r r 2 [ i ] + a r r 2 [ j ] − i + j ; − a r r 1 [ i ] + a r r 1 [ j ] + a r r 2 [ i ] − a r r 2 [ j ] − i + j ; arr1[i] - arr1[j] + arr2[i] - arr2[j] + i - j;\\ -arr1[i] + arr1[j] - arr2[i] + arr2[j] - i + j;\\ -arr1[i] + arr1[j] + arr2[i] - arr2[j] + i - j;\\ arr1[i] - arr1[j] - arr2[i] + arr2[j] + i - j;\\ arr1[i] - arr1[j] + arr2[i] - arr2[j] - i + j;\\ -arr1[i] + arr1[j] - arr2[i] + arr2[j] + i - j;\\ arr1[i] - arr1[j] - arr2[i] + arr2[j] - i + j;\\ -arr1[i] + arr1[j] + arr2[i] - arr2[j] - i + j; arr1[i]arr1[j]+arr2[i]arr2[j]+ij;arr1[i]+arr1[j]arr2[i]+arr2[j]i+j;arr1[i]+arr1[j]+arr2[i]arr2[j]+ij;arr1[i]arr1[j]arr2[i]+arr2[j]+ij;arr1[i]arr1[j]+arr2[i]arr2[j]i+j;arr1[i]+arr1[j]arr2[i]+arr2[j]+ij;arr1[i]arr1[j]arr2[i]+arr2[j]i+j;arr1[i]+arr1[j]+arr2[i]arr2[j]i+j;

把 j 拿走
a r r 1 [ i ] + a r r 2 [ i ] + i ; − a r r 1 [ i ] − a r r 2 [ i ] − i ; − a r r 1 [ i ] + a r r 2 [ i ] + i ; a r r 1 [ i ] − a r r 2 [ i ] + i ; a r r 1 [ i ] + a r r 2 [ i ] − i ; − a r r 1 [ i ] − a r r 2 [ i ] + i ; a r r 1 [ i ] − a r r 2 [ i ] − i ; − a r r 1 [ i ] + a r r 2 [ i ] − i ; arr1[i] + arr2[i] + i;\\ -arr1[i] - arr2[i] - i;\\ -arr1[i] + arr2[i] + i;\\ arr1[i] - arr2[i] + i;\\ arr1[i] + arr2[i] - i;\\ -arr1[i] - arr2[i] + i;\\ arr1[i] - arr2[i] - i;\\ -arr1[i] + arr2[i] - i;\\ arr1[i]+arr2[i]+i;arr1[i]arr2[i]i;arr1[i]+arr2[i]+i;arr1[i]arr2[i]+i;arr1[i]+arr2[i]i;arr1[i]arr2[i]+i;arr1[i]arr2[i]i;arr1[i]+arr2[i]i;
发现只有4种情况
a r r 1 [ i ] + a r r 2 [ i ] + i a s A ; − a r r 1 [ i ] − a r r 2 [ i ] − i a s − A ; − a r r 1 [ i ] + a r r 2 [ i ] + i a s B ; a r r 1 [ i ] − a r r 2 [ i ] + i a s C ; a r r 1 [ i ] + a r r 2 [ i ] − i a s D ; − a r r 1 [ i ] − a r r 2 [ i ] + i a s − D ; a r r 1 [ i ] − a r r 2 [ i ] − i a s − B ; − a r r 1 [ i ] + a r r 2 [ i ] − i a s − C ; arr1[i] + arr2[i] + i \quad as A;\\ -arr1[i] - arr2[i] - i \quad as -A;\\ -arr1[i] + arr2[i] + i \quad as B;\\ arr1[i] - arr2[i] + i \quad as C;\\ arr1[i] + arr2[i] - i \quad as D;\\ -arr1[i] - arr2[i] + i \quad as -D;\\ arr1[i] - arr2[i] - i \quad as -B;\\ -arr1[i] + arr2[i] - i \quad as -C; arr1[i]+arr2[i]+iasA;arr1[i]arr2[i]iasA;arr1[i]+arr2[i]+iasB;arr1[i]arr2[i]+iasC;arr1[i]+arr2[i]iasD;arr1[i]arr2[i]+iasD;arr1[i]arr2[i]iasB;arr1[i]+arr2[i]iasC;
只需要取出ABCD,求其最大最小值,做差

class Solution {
public:
    int maxAbsValExpr(vector<int>& arr1, vector<int>& arr2) {
    	int min1 = INT_MAX, min2 = INT_MAX, min3 = INT_MAX, min4 = INT_MAX;
    	int max1 = INT_MIN, max2 = INT_MIN, max3 = INT_MIN, max4 = INT_MIN;
    	for(int i = 0; i < arr1.size(); ++i)
    	{
    		max1 = max(max1, arr1[i] + arr2[i] + i);
    		max2 = max(max2,-arr1[i] + arr2[i] + i);
    		max3 = max(max3, arr1[i] - arr2[i] + i);
    		max4 = max(max4, arr1[i] + arr2[i] - i);
    		min1 = min(min1, arr1[i] + arr2[i] + i);
    		min2 = min(min2,-arr1[i] + arr2[i] + i);
    		min3 = min(min3, arr1[i] - arr2[i] + i);
    		min4 = min(min4, arr1[i] + arr2[i] - i);
    	}
    	return max(max(max1-min1, max2-min2),max(max3-min3, max4-min4));
    }
};

100 ms 23.2 MB


我的CSDN博客地址 https://michael.blog.csdn.net/

长按或扫码关注我的公众号(Michael阿明),一起加油、一起学习进步!
Michael阿明

你可能感兴趣的:(LeetCode)