-
Notifications
You must be signed in to change notification settings - Fork 3
update: 添加问题“1458.两个子序列的最大点积”的代码和题解 #1311
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| 1458: AC.cpp (#1310) + word(en) + fix(pic) | ||
|
|
||
| cpp - AC,84.23%,29.53% | ||
| py - AC,97.99%,39.85% | ||
| go - AC,76.92%,38.46% | ||
| java - AC,65.24%,32.38% | ||
| rust - AC,100.00%,60.00% |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| /* | ||
| * @Author: LetMeFly | ||
| * @Date: 2026-01-08 09:21:01 | ||
| * @LastEditors: LetMeFly.xyz | ||
| * @LastEditTime: 2026-01-08 09:23:30 | ||
| */ | ||
| #if defined(_WIN32) || defined(__APPLE__) | ||
| #include "_[1,2]toVector.h" | ||
| #endif | ||
|
|
||
| class Solution { | ||
| public: | ||
| int maxDotProduct(vector<int>& nums1, vector<int>& nums2) { | ||
| int n = nums1.size(), m = nums2.size(); | ||
| vector<vector<int>> dp(n, vector<int>(m)); | ||
| for (int i = 0; i < n; i++) { | ||
| for (int j = 0; j < m; j++) { | ||
| dp[i][j] = nums1[i] * nums2[j]; | ||
| if (i) { | ||
| dp[i][j] = max(dp[i][j], dp[i - 1][j]); | ||
| } | ||
| if (j) { | ||
| dp[i][j] = max(dp[i][j], dp[i][j - 1]); | ||
| } | ||
| if (i && j) { | ||
| dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + nums1[i] * nums2[j]); | ||
| } | ||
| } | ||
| } | ||
| return dp[n - 1][m - 1]; | ||
| } | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| /* | ||
| * @Author: LetMeFly | ||
| * @Date: 2026-01-08 09:21:01 | ||
| * @LastEditors: LetMeFly.xyz | ||
| * @LastEditTime: 2026-01-08 09:36:59 | ||
| */ | ||
| package main | ||
|
|
||
| func max1458_2(a, b int) int { | ||
| if a > b { | ||
| return a | ||
| } | ||
| return b | ||
| } | ||
|
|
||
| func max1458(a, b, c, d int) int { | ||
| return max1458_2(a, max1458_2(b, max1458_2(c, d))) | ||
| } | ||
|
|
||
| func maxDotProduct(nums1 []int, nums2 []int) int { | ||
| n, m := len(nums1), len(nums2) | ||
| dp := make([][]int, n + 1) | ||
| for i := range dp { | ||
| dp[i] = make([]int, m + 1) | ||
| for j := range dp[i] { | ||
| dp[i][j] = -1000000; | ||
| } | ||
| } | ||
|
|
||
| for i, x := range nums1 { | ||
| for j, y := range nums2 { | ||
| dp[i+1][j+1] = max1458(x*y, dp[i][j+1], dp[i+1][j], dp[i][j] + x*y) | ||
| } | ||
| } | ||
| return dp[n][m] // 不是dp[n-1][m-1] | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,23 @@ | ||||||
| /* | ||||||
| * @Author: LetMeFly | ||||||
| * @Date: 2026-01-08 09:21:01 | ||||||
| * @LastEditors: LetMeFly.xyz | ||||||
| * @LastEditTime: 2026-01-08 09:45:31 | ||||||
| */ | ||||||
| import java.util.Arrays; | ||||||
|
|
||||||
| class Solution { | ||||||
| public int maxDotProduct(int[] nums1, int[] nums2) { | ||||||
| int n = nums1.length, m = nums2.length; | ||||||
| int[][] dp = new int[n+1][m+1]; | ||||||
| for (int i = 0; i <= n; i++) { | ||||||
| Arrays.fill(dp[i], -1000000); | ||||||
| } | ||||||
| for (int i = 1; i <= n; i++) { | ||||||
| for (int j = 1; j <= m; j++) { | ||||||
| dp[i][j] = Math.max(nums1[i-1] * nums2[j-1], Math.max(dp[i-1][j], Math.max(dp[i][j - 1], dp[i-1][j-1] + nums1[i-1] * nums2[j-1]))); | ||||||
|
||||||
| dp[i][j] = Math.max(nums1[i-1] * nums2[j-1], Math.max(dp[i-1][j], Math.max(dp[i][j - 1], dp[i-1][j-1] + nums1[i-1] * nums2[j-1]))); | |
| dp[i][j] = Math.max(nums1[i - 1] * nums2[j - 1], Math.max(dp[i - 1][j], Math.max(dp[i][j - 1], dp[i - 1][j - 1] + nums1[i - 1] * nums2[j - 1]))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
哦吼
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不如把dp[i][j - 1]换成·、dp[i][j-1]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inconsistent spacing around the subtraction operator. The expression uses dp[i][j - 1] with spaces but nums1[i-1] and nums2[j-1] without spaces. For better consistency and readability, consider using uniform spacing: either spaces around all minus operators or no spaces for all.减法算符周围的间距不一致。表达式使用 dp[i][j - 1],带有空格,但 nums1[i-1] 和 nums2[j-1],不含空格。为了提高一致性和可读性,建议使用均匀间距:要么在所有负算符周围空格,要么全都不加空格。
@copilot 把这次java的dp[i][j - 1]换成dp[i][j-1],pr中不要包含有多余文件
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| ''' | ||
| Author: LetMeFly | ||
| Date: 2026-01-08 09:21:01 | ||
| LastEditors: LetMeFly.xyz | ||
| LastEditTime: 2026-01-08 09:29:41 | ||
| ''' | ||
| from typing import List | ||
|
|
||
| class Solution: | ||
| def maxDotProduct(self, nums1: List[int], nums2: List[int]) -> int: | ||
| dp = [[-1000000000] * (len(nums2) + 1) for _ in range(len(nums1) + 1)] | ||
LetMeFly666 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| for i, x in enumerate(nums1, 1): | ||
| for j, y in enumerate(nums2, 1): | ||
| dp[i][j] = max(x * y, dp[i-1][j], dp[i][j-1], dp[i-1][j-1] + x * y) | ||
| return dp[-1][-1] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| /* | ||
| * @Author: LetMeFly | ||
| * @Date: 2026-01-08 09:21:01 | ||
| * @LastEditors: LetMeFly.xyz | ||
| * @LastEditTime: 2026-01-08 09:51:58 | ||
| */ | ||
| impl Solution { | ||
| pub fn max_dot_product(nums1: Vec<i32>, nums2: Vec<i32>) -> i32 { | ||
| let n: usize = nums1.len(); | ||
| let m: usize = nums2.len(); | ||
| let mut dp: Vec<Vec<i32>> = vec![vec![-1000000; m+1]; n+1]; | ||
| for i in 1..=n { | ||
| for j in 1..=m { | ||
| dp[i][j] = dp[i-1][j].max(dp[i][j-1].max((nums1[i-1] * nums2[j-1]).max(dp[i-1][j-1] + nums1[i-1] * nums2[j-1]))); | ||
| } | ||
| } | ||
| dp[n][m] | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.