Skip to content

Commit e2cb751

Browse files
authored
Update 1537-get-the-maximum-score.js
1 parent 8baaf7c commit e2cb751

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

1537-get-the-maximum-score.js

+36
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,39 @@ const maxSum = function(nums1, nums2) {
7070
}
7171
return Math.max(a, b) % mod;
7272
};
73+
74+
// another
75+
76+
/**
77+
* @param {number[]} nums1
78+
* @param {number[]} nums2
79+
* @return {number}
80+
*/
81+
const maxSum = function(nums1, nums2) {
82+
const len1 = nums1.length, len2 = nums2.length
83+
const mod = 10 ** 9 + 7
84+
const map = new Map()
85+
for(let i = 0; i < len1 - 1; i++) {
86+
if(!map.has(nums1[i])) map.set(nums1[i], [])
87+
map.get(nums1[i]).push(nums1[i + 1])
88+
}
89+
for(let j = 0; j < len2 - 1; j++) {
90+
if(!map.has(nums2[j])) map.set(nums2[j], [])
91+
map.get(nums2[j]).push(nums2[j + 1])
92+
}
93+
const memo = new Map()
94+
return Math.max(greedy(nums1[0], map, memo), greedy(nums2[0], map, memo)) % mod
95+
};
96+
97+
function greedy(cur, map, memo) {
98+
if(memo.has(cur)) return memo.get(cur)
99+
if(!map.has(cur)) return cur
100+
let res = 0
101+
for(let next of map.get(cur)) {
102+
const tmp = greedy(next, map, memo)
103+
if(tmp > res) res = tmp
104+
}
105+
res += cur
106+
memo.set(cur, res)
107+
return res
108+
}

0 commit comments

Comments
 (0)