Skip to content

Commit

Permalink
Merge pull request #657 from 0xff-dev/1458
Browse files Browse the repository at this point in the history
Add solution and test-cases for problem 1458
  • Loading branch information
6boris authored Oct 28, 2023
2 parents d88196b + d160cca commit fbb3ae6
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 12 deletions.
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
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
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,30 @@ func TestSolution(t *testing.T) {
// 测试用例
cases := []struct {
name string
inputs bool
expect bool
n1, n2 []int
expect int
}{
{"TestCase", true, true},
{"TestCase", true, true},
{"TestCase", false, false},
{"TestCase1", []int{2, 1, -2, 5}, []int{3, 0, -6}, 18},
{"TestCase2", []int{3, -2}, []int{2, -6, 7}, 21},
{"TestCase3", []int{-1, -1}, []int{1, 1}, -1},
}

// 开始测试
for i, c := range cases {
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
got := Solution(c.inputs)
got := Solution(c.n1, c.n2)
if !reflect.DeepEqual(got, c.expect) {
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
c.expect, got, c.inputs)
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
c.expect, got, c.n1, c.n2)
}
})
}
}

// 压力测试
// 压力测试
func BenchmarkSolution(b *testing.B) {
}

// 使用案列
// 使用案列
func ExampleSolution() {
}

0 comments on commit fbb3ae6

Please sign in to comment.