-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #657 from 0xff-dev/1458
Add solution and test-cases for problem 1458
- Loading branch information
Showing
3 changed files
with
94 additions
and
12 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
leetcode/1401-1500/1458.Max-Dot-Product-of-Two-Subsequences/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# [1458. Max Dot Product of Two Subsequences][title] | ||
|
||
## Description | ||
Given two arrays `nums1` and `nums2`. | ||
|
||
Return the maximum dot product between **non-empty** subsequences of nums1 and nums2 with the same length. | ||
|
||
A subsequence of a array is a new array which is formed from the original array by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, [`2,3,5`] is a subsequence of [`1,2,3,4,5`] while [`1,5,3`] is not). | ||
|
||
**Example 1:** | ||
|
||
``` | ||
Input: nums1 = [2,1,-2,5], nums2 = [3,0,-6] | ||
Output: 18 | ||
Explanation: Take subsequence [2,-2] from nums1 and subsequence [3,-6] from nums2. | ||
Their dot product is (2*3 + (-2)*(-6)) = 18. | ||
``` | ||
|
||
**Example 2:** | ||
|
||
``` | ||
Input: nums1 = [3,-2], nums2 = [2,-6,7] | ||
Output: 21 | ||
Explanation: Take subsequence [3] from nums1 and subsequence [7] from nums2. | ||
Their dot product is (3*7) = 21. | ||
``` | ||
|
||
**Example 3:** | ||
|
||
``` | ||
Input: nums1 = [-1,-1], nums2 = [1,1] | ||
Output: -1 | ||
Explanation: Take subsequence [-1] from nums1 and subsequence [1] from nums2. | ||
Their dot product is -1. | ||
``` | ||
|
||
## 结语 | ||
|
||
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me] | ||
|
||
[title]: https://leetcode.com/problems/max-dot-product-of-two-subsequences | ||
[me]: https://github.com/kylesliu/awesome-golang-algorithm |
44 changes: 42 additions & 2 deletions
44
leetcode/1401-1500/1458.Max-Dot-Product-of-Two-Subsequences/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,45 @@ | ||
package Solution | ||
|
||
func Solution(x bool) bool { | ||
return x | ||
import ( | ||
"math" | ||
) | ||
|
||
func Solution(nums1 []int, nums2 []int) int { | ||
l1, l2 := len(nums1), len(nums2) | ||
dp := make([][]int, l1) | ||
for i := 0; i < l1; i++ { | ||
dp[i] = make([]int, l2) | ||
for j := 0; j < l2; j++ { | ||
dp[i][j] = math.MinInt | ||
} | ||
} | ||
// -1, -1 | ||
// 1, 1 | ||
//bool是否确定返回的0是否是因为到了边界导致的, 上面的测试用例就是例子,应该返回-1,但是遇到了边界导致返回0,结果不对 | ||
var dfs func(int, int) (int, bool) | ||
dfs = func(i, j int) (int, bool) { | ||
if i == l1 || j == l2 { | ||
return 0, false | ||
} | ||
if dp[i][j] != math.MinInt { | ||
return dp[i][j], true | ||
} | ||
a := nums1[i] * nums2[j] | ||
|
||
if b, ok := dfs(i+1, j+1); ok && b > 0 { | ||
a += b | ||
} | ||
// 因为到了边界,返回的0,导致比-1大 | ||
if c, ok := dfs(i, j+1); ok && c > a { | ||
a = c | ||
} | ||
if d, ok := dfs(i+1, j); ok && d > a { | ||
a = d | ||
} | ||
dp[i][j] = a | ||
return a, true | ||
} | ||
|
||
a, _ := dfs(0, 0) | ||
return a | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters