Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit13e8601

Browse files
authored
Create Max Dot Product of Two Subsequences.java
1 parent0c6fe30 commit13e8601

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
classSolution {
2+
publicintmaxDotProduct(int[]nums1,int[]nums2) {
3+
intmaxOne =Integer.MIN_VALUE;
4+
intmaxTwo =Integer.MIN_VALUE;
5+
intminOne =Integer.MAX_VALUE;
6+
intminTwo =Integer.MAX_VALUE;
7+
for (intnum :nums1) {
8+
maxOne =Math.max(maxOne,num);
9+
minOne =Math.min(minOne,num);
10+
}
11+
for (intnum :nums2) {
12+
maxTwo =Math.max(maxTwo,num);
13+
minTwo =Math.min(minTwo,num);
14+
}
15+
if (maxOne <0 &&minTwo >0) {
16+
returnmaxOne *minTwo;
17+
}
18+
if (minOne >0 &&maxTwo <0) {
19+
returnminOne *maxTwo;
20+
}
21+
int[][]dp =newint[nums1.length +1][nums2.length +1];
22+
for (inti =nums1.length -1;i >=0;i--) {
23+
for (intj =nums2.length -1;j >=0;j--) {
24+
intcurr =nums1[i] *nums2[j] +dp[i +1][j +1];
25+
dp[i][j] =Math.max(curr,Math.max(dp[i +1][j],dp[i][j +1]));
26+
}
27+
}
28+
returndp[0][0];
29+
}
30+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp