Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 4fd6349

Browse files
committedApr 4, 2025
Add solution #1131
1 parent e59b5fb commit 4fd6349

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed
 

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,146 LeetCode solutions in JavaScript
1+
# 1,147 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -892,6 +892,7 @@
892892
1128|[Number of Equivalent Domino Pairs](./solutions/1128-number-of-equivalent-domino-pairs.js)|Easy|
893893
1129|[Shortest Path with Alternating Colors](./solutions/1129-shortest-path-with-alternating-colors.js)|Medium|
894894
1130|[Minimum Cost Tree From Leaf Values](./solutions/1130-minimum-cost-tree-from-leaf-values.js)|Medium|
895+
1131|[Maximum of Absolute Value Expression](./solutions/1131-maximum-of-absolute-value-expression.js)|Medium|
895896
1137|[N-th Tribonacci Number](./solutions/1137-n-th-tribonacci-number.js)|Easy|
896897
1143|[Longest Common Subsequence](./solutions/1143-longest-common-subsequence.js)|Medium|
897898
1161|[Maximum Level Sum of a Binary Tree](./solutions/1161-maximum-level-sum-of-a-binary-tree.js)|Medium|
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* 1131. Maximum of Absolute Value Expression
3+
* https://leetcode.com/problems/maximum-of-absolute-value-expression/
4+
* Difficulty: Medium
5+
*
6+
* Given two arrays of integers with equal lengths, return the maximum value of:
7+
* - |arr1[i] - arr1[j]| + |arr2[i] - arr2[j]| + |i - j|
8+
*
9+
* where the maximum is taken over all 0 <= i, j < arr1.length.
10+
*/
11+
12+
/**
13+
* @param {number[]} arr1
14+
* @param {number[]} arr2
15+
* @return {number}
16+
*/
17+
var maxAbsValExpr = function(arr1, arr2) {
18+
const directions = [[1, 1], [1, -1], [-1, 1], [-1, -1]];
19+
let result = 0;
20+
21+
for (const [dx, dy] of directions) {
22+
let minSeen = Infinity;
23+
let maxSeen = -Infinity;
24+
25+
for (let i = 0; i < arr1.length; i++) {
26+
const value = dx * arr1[i] + dy * arr2[i] + i;
27+
minSeen = Math.min(minSeen, value);
28+
maxSeen = Math.max(maxSeen, value);
29+
}
30+
31+
result = Math.max(result, maxSeen - minSeen);
32+
}
33+
34+
return result;
35+
};

0 commit comments

Comments
 (0)
Please sign in to comment.