Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 3b48110

Browse files
committed
Add solution #1458
1 parent 02053e7 commit 3b48110

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-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,312 LeetCode solutions in JavaScript
1+
# 1,313 LeetCode solutions in JavaScript
22

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

@@ -1114,6 +1114,7 @@
11141114
1455|[Check If a Word Occurs As a Prefix of Any Word in a Sentence](./solutions/1455-check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence.js)|Easy|
11151115
1456|[Maximum Number of Vowels in a Substring of Given Length](./solutions/1456-maximum-number-of-vowels-in-a-substring-of-given-length.js)|Medium|
11161116
1457|[Pseudo-Palindromic Paths in a Binary Tree](./solutions/1457-pseudo-palindromic-paths-in-a-binary-tree.js)|Medium|
1117+
1458|[Max Dot Product of Two Subsequences](./solutions/1458-max-dot-product-of-two-subsequences.js)|Hard|
11171118
1460|[Make Two Arrays Equal by Reversing Sub-arrays](./solutions/1460-make-two-arrays-equal-by-reversing-sub-arrays.js)|Easy|
11181119
1462|[Course Schedule IV](./solutions/1462-course-schedule-iv.js)|Medium|
11191120
1464|[Maximum Product of Two Elements in an Array](./solutions/1464-maximum-product-of-two-elements-in-an-array.js)|Easy|
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/**
2+
* 1458. Max Dot Product of Two Subsequences
3+
* https://leetcode.com/problems/max-dot-product-of-two-subsequences/
4+
* Difficulty: Hard
5+
*
6+
* Given two arrays nums1 and nums2.
7+
*
8+
* Return the maximum dot product between non-empty subsequences of nums1 and nums2 with the
9+
* same length.
10+
*
11+
* A subsequence of a array is a new array which is formed from the original array by deleting
12+
* some (can be none) of the characters without disturbing the relative positions of the remaining
13+
* characters. (ie, [2,3,5] is a subsequence of [1,2,3,4,5] while [1,5,3] is not).
14+
*/
15+
16+
/**
17+
* @param {number[]} nums1
18+
* @param {number[]} nums2
19+
* @return {number}
20+
*/
21+
var maxDotProduct = function(nums1, nums2) {
22+
const m = nums1.length;
23+
const n = nums2.length;
24+
const dp = new Array(m + 1).fill().map(() => new Array(n + 1).fill(-Infinity));
25+
26+
let result = -Infinity;
27+
for (let i = 1; i <= m; i++) {
28+
for (let j = 1; j <= n; j++) {
29+
result = Math.max(result, computeMax(i, j));
30+
}
31+
}
32+
33+
return result;
34+
35+
function computeMax(i, j) {
36+
if (i === 0 || j === 0) return -Infinity;
37+
if (dp[i][j] !== -Infinity) return dp[i][j];
38+
39+
dp[i][j] = nums1[i - 1] * nums2[j - 1];
40+
dp[i][j] = Math.max(
41+
dp[i][j],
42+
dp[i][j] + computeMax(i - 1, j - 1),
43+
computeMax(i - 1, j),
44+
computeMax(i, j - 1)
45+
);
46+
47+
return dp[i][j];
48+
}
49+
};

0 commit comments

Comments
 (0)