Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .commitmsg
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%
32 changes: 32 additions & 0 deletions Codes/1458-max-dot-product-of-two-subsequences.cpp
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];
}
};
36 changes: 36 additions & 0 deletions Codes/1458-max-dot-product-of-two-subsequences.go
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]
}
23 changes: 23 additions & 0 deletions Codes/1458-max-dot-product-of-two-subsequences.java
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])));
Copy link

Copilot AI Jan 8, 2026

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.

Suggested change
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])));

Copilot uses AI. Check for mistakes.
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

哦吼

Copy link
Owner Author

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]

Copy link
Owner Author

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中不要包含有多余文件

}
}
return dp[n][m];
}
}
15 changes: 15 additions & 0 deletions Codes/1458-max-dot-product-of-two-subsequences.py
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)]
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]
19 changes: 19 additions & 0 deletions Codes/1458-max-dot-product-of-two-subsequences.rs
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]
}
}
2 changes: 1 addition & 1 deletion Codes/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @LastEditTime: 2026-01-06 13:17:16
*/
pub struct Solution;
include!("1161-maximum-level-sum-of-a-binary-tree_20260106.rs"); // 这个fileName是会被脚本替换掉的
include!("1458-max-dot-product-of-two-subsequences.rs"); // 这个fileName是会被脚本替换掉的

#[derive(Debug, PartialEq, Eq)]
pub struct TreeNode {
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,7 @@
|1450.在既定时间做作业的学生人数|简单|<a href="https://leetcode.cn/problems/number-of-students-doing-homework-at-a-given-time/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/08/19/LeetCode%201450.%E5%9C%A8%E6%97%A2%E5%AE%9A%E6%97%B6%E9%97%B4%E5%81%9A%E4%BD%9C%E4%B8%9A%E7%9A%84%E5%AD%A6%E7%94%9F%E4%BA%BA%E6%95%B0/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/126417256" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/number-of-students-doing-homework-at-a-given-time/solution/letmefly-1450zai-ji-ding-shi-jian-zuo-zu-0sd7/" target="_blank">LeetCode题解</a>|
|1455.检查单词是否为句中其他单词的前缀|简单|<a href="https://leetcode.cn/problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/08/21/LeetCode%201455.%E6%A3%80%E6%9F%A5%E5%8D%95%E8%AF%8D%E6%98%AF%E5%90%A6%E4%B8%BA%E5%8F%A5%E4%B8%AD%E5%85%B6%E4%BB%96%E5%8D%95%E8%AF%8D%E7%9A%84%E5%89%8D%E7%BC%80/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/126448124" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/solution/letmefly-1455jian-cha-dan-ci-shi-fou-wei-m3rs/" target="_blank">LeetCode题解</a>|
|1457.二叉树中的伪回文路径|中等|<a href="https://leetcode.cn/problems/pseudo-palindromic-paths-in-a-binary-tree/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/11/25/LeetCode%201457.%E4%BA%8C%E5%8F%89%E6%A0%91%E4%B8%AD%E7%9A%84%E4%BC%AA%E5%9B%9E%E6%96%87%E8%B7%AF%E5%BE%84/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/134617854" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/pseudo-palindromic-paths-in-a-binary-tree/solutions/2541535/letmefly-1457er-cha-shu-zhong-de-wei-hui-91vp/" target="_blank">LeetCode题解</a>|
|1458.两个子序列的最大点积|困难|<a href="https://leetcode.cn/problems/max-dot-product-of-two-subsequences/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2026/01/08/LeetCode%201458.%E4%B8%A4%E4%B8%AA%E5%AD%90%E5%BA%8F%E5%88%97%E7%9A%84%E6%9C%80%E5%A4%A7%E7%82%B9%E7%A7%AF/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/156713484" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/max-dot-product-of-two-subsequences/solutions/3875454/letmefly-1458liang-ge-zi-xu-lie-de-zui-d-3f54/" target="_blank">LeetCode题解</a>|
|1460.通过翻转子数组使两个数组相等|简单|<a href="https://leetcode.cn/problems/make-two-arrays-equal-by-reversing-sub-arrays/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/08/24/LeetCode%201460.%E9%80%9A%E8%BF%87%E7%BF%BB%E8%BD%AC%E5%AD%90%E6%95%B0%E7%BB%84%E4%BD%BF%E4%B8%A4%E4%B8%AA%E6%95%B0%E7%BB%84%E7%9B%B8%E7%AD%89/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/126499790" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/make-two-arrays-equal-by-reversing-sub-arrays/solution/letmefly-1460tong-guo-fan-zhuan-zi-shu-z-q812/" target="_blank">LeetCode题解</a>|
|1462.课程表IV|中等|<a href="https://leetcode.cn/problems/course-schedule-iv/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2023/09/12/LeetCode%201462.%E8%AF%BE%E7%A8%8B%E8%A1%A8IV/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/132825649" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/course-schedule-iv/solutions/2438404/letmefly-1462ke-cheng-biao-ivtuo-bu-pai-gufke/" target="_blank">LeetCode题解</a>|
|1464.数组中两元素的最大乘积|简单|<a href="https://leetcode.cn/problems/maximum-product-of-two-elements-in-an-array/" target="_blank">题目地址</a>|<a href="https://blog.letmefly.xyz/2022/08/26/LeetCode%201464.%E6%95%B0%E7%BB%84%E4%B8%AD%E4%B8%A4%E5%85%83%E7%B4%A0%E7%9A%84%E6%9C%80%E5%A4%A7%E4%B9%98%E7%A7%AF/" target="_blank">题解地址</a>|<a href="https://letmefly.blog.csdn.net/article/details/126536351" target="_blank">CSDN题解</a>|<a href="https://leetcode.cn/problems/maximum-product-of-two-elements-in-an-array/solution/letmefly-1464shu-zu-zhong-liang-yuan-su-txf8l/" target="_blank">LeetCode题解</a>|
Expand Down
4 changes: 2 additions & 2 deletions Solutions/LeetCode 1411.给Nx3网格图涂色的方案数.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: 1411.给 N x 3 网格图涂色的方案数:递推+原地滚动(动态
date: 2026-01-03 21:46:12
tags: [题解, LeetCode, 困难, 动态规划, DP]
categories: [题解, LeetCode]
index_img: https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/04/12/e1.png
index_img: https://assets.leetcode.cn/aliyun-lc-upload/uploads/2020/04/12/e1.png
---

# 【LetMeFly】1411.给 N x 3 网格图涂色的方案数:递推+原地滚动(动态规划)
Expand All @@ -23,7 +23,7 @@ index_img: https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/04/12/e1
<pre><strong>输入:</strong>n = 1
<strong>输出:</strong>12
<strong>解释:</strong>总共有 12 种可行的方法:
<img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/04/12/e1.png" style="height: 289px; width: 450px;">
<img alt="" src="https://assets.leetcode.cn/aliyun-lc-upload/uploads/2020/04/12/e1.png" style="height: 289px; width: 450px;">
</pre>

<p><strong>示例 2:</strong></p>
Expand Down
Loading
Loading