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 569da7d

Browse files
committedApr 18, 2025
Add solution #1537
1 parent 9baead5 commit 569da7d

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-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,352 LeetCode solutions in JavaScript
1+
# 1,353 LeetCode solutions in JavaScript
22

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

@@ -1174,6 +1174,7 @@
11741174
1534|[Count Good Triplets](./solutions/1534-count-good-triplets.js)|Easy|
11751175
1535|[Find the Winner of an Array Game](./solutions/1535-find-the-winner-of-an-array-game.js)|Medium|
11761176
1536|[Minimum Swaps to Arrange a Binary Grid](./solutions/1536-minimum-swaps-to-arrange-a-binary-grid.js)|Medium|
1177+
1537|[Get the Maximum Score](./solutions/1537-get-the-maximum-score.js)|Hard|
11771178
1550|[Three Consecutive Odds](./solutions/1550-three-consecutive-odds.js)|Easy|
11781179
1551|[Minimum Operations to Make Array Equal](./solutions/1551-minimum-operations-to-make-array-equal.js)|Medium|
11791180
1566|[Detect Pattern of Length M Repeated K or More Times](./solutions/1566-detect-pattern-of-length-m-repeated-k-or-more-times.js)|Easy|
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* 1537. Get the Maximum Score
3+
* https://leetcode.com/problems/get-the-maximum-score/
4+
* Difficulty: Hard
5+
*
6+
* You are given two sorted arrays of distinct integers nums1 and nums2.
7+
*
8+
* A valid path is defined as follows:
9+
* - Choose array nums1 or nums2 to traverse (from index-0).
10+
* - Traverse the current array from left to right.
11+
* - If you are reading any value that is present in nums1 and nums2 you are allowed to change
12+
* your path to the other array. (Only one repeated value is considered in the valid path).
13+
*
14+
* The score is defined as the sum of unique values in a valid path.
15+
*
16+
* Return the maximum score you can obtain of all possible valid paths. Since the answer may be
17+
* too large, return it modulo 109 + 7.
18+
*/
19+
20+
/**
21+
* @param {number[]} nums1
22+
* @param {number[]} nums2
23+
* @return {number}
24+
*/
25+
var maxSum = function(nums1, nums2) {
26+
const MOD = 1e9 + 7;
27+
const m = nums1.length;
28+
const n = nums2.length;
29+
let i = 0;
30+
let j = 0;
31+
let sum1 = 0;
32+
let sum2 = 0;
33+
34+
while (i < m && j < n) {
35+
if (nums1[i] < nums2[j]) {
36+
sum1 += nums1[i++];
37+
} else if (nums1[i] > nums2[j]) {
38+
sum2 += nums2[j++];
39+
} else {
40+
sum1 = sum2 = Math.max(sum1, sum2) + nums1[i];
41+
i++;
42+
j++;
43+
}
44+
}
45+
46+
while (i < m) {
47+
sum1 += nums1[i++];
48+
}
49+
50+
while (j < n) {
51+
sum2 += nums2[j++];
52+
}
53+
54+
return Math.max(sum1, sum2) % MOD;
55+
};

0 commit comments

Comments
 (0)
Please sign in to comment.