-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0a216c2
commit bf613b6
Showing
4 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
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,49 @@ | ||
//{ Driver Code Starts | ||
//Initial Template for Java | ||
|
||
import java.io.*; | ||
import java.util.*; | ||
class GfG | ||
{ | ||
public static void main(String args[]) | ||
{ | ||
Scanner sc = new Scanner(System.in); | ||
int t = sc.nextInt(); | ||
while(t-->0) | ||
{ | ||
int n = sc.nextInt(); | ||
int m = sc.nextInt(); | ||
int a[] = new int[n]; | ||
int b[] = new int[m]; | ||
for(int i = 0;i<n;i++) | ||
a[i] = sc.nextInt(); | ||
for(int i = 0;i<m;i++) | ||
b[i] = sc.nextInt(); | ||
Solution ob = new Solution(); | ||
System.out.println(ob.maxDotProduct(n, m, a, b)); | ||
} | ||
} | ||
} | ||
// } Driver Code Ends | ||
|
||
|
||
//User function Template for Java | ||
|
||
class Solution | ||
{ | ||
public int maxDotProduct(int n, int m, int a[], int b[]) | ||
{ | ||
// Your code goes here | ||
int[][] dp = new int[m + 1][n + 1]; | ||
|
||
for (int i = 1; i <= m; i++) | ||
{ | ||
for (int j = i; j <= n; j++) | ||
{ | ||
dp[i][j] = Math.max(dp[i - 1][j - 1] + a[j - 1] * b[i - 1], dp[i][j - 1]); | ||
} | ||
} | ||
|
||
return dp[m][n]; | ||
} | ||
} |
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,2 @@ | ||
Time complexity - O(n*m) | ||
Space complexity - O(n*m) |
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,2 @@ | ||
Time complexity - O(n) | ||
Space complexity - O(1) |
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,32 @@ | ||
class Solution | ||
{ | ||
public boolean checkValidString(String s) | ||
{ | ||
int low = 0; | ||
int high = 0; | ||
|
||
for (char c : s.toCharArray()) | ||
{ | ||
switch (c) | ||
{ | ||
case '(': | ||
++low; | ||
++high; | ||
break; | ||
case ')': | ||
low = Math.max(0, --low); | ||
--high; | ||
break; | ||
case '*': | ||
low = Math.max(0, --low); | ||
++high; | ||
break; | ||
} | ||
|
||
if (high < 0) | ||
return false; | ||
} | ||
|
||
return low == 0; | ||
} | ||
} |