-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMaximumScoreFromPerformingMultiplicationOperations.java
71 lines (67 loc) · 2.63 KB
/
MaximumScoreFromPerformingMultiplicationOperations.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*https://leetcode.com/problems/maximum-score-from-performing-multiplication-operations/*/
//TLE
class Solution {
public int maximumScore(int[] nums, int[] multipliers) {
return score(nums,multipliers,0,nums.length-1,0);
}
private int score(int[] nums, int[] muls, int start, int end, int index)
{
if (index == muls.length) return 0;
int left = muls[index]*nums[start]+score(nums,muls,start+1,end,index+1);
int right = muls[index]*nums[end]+score(nums,muls,start,end-1,index+1);
return Math.max(left,right);
}
}
//MLE
class Solution {
Integer[][][] table;
public int maximumScore(int[] nums, int[] multipliers) {
table = new Integer[nums.length][nums.length][multipliers.length];
table[0][nums.length-1][0] = score(nums,multipliers,0,nums.length-1,0);
return table[0][nums.length-1][0];
}
private int score(int[] nums, int[] muls, int start, int end, int index)
{
if (index == muls.length) return 0;
if (table[start][end][index] != null) return table[start][end][index];
int left = muls[index]*nums[start]+score(nums,muls,start+1,end,index+1);
int right = muls[index]*nums[end]+score(nums,muls,start,end-1,index+1);
return table[start][end][index] = Math.max(left,right);
}
}
//Again MLE
class Solution {
Integer[][] table;
public int maximumScore(int[] nums, int[] multipliers) {
table = new Integer[nums.length][multipliers.length];
table[0][0] = score(nums,multipliers,0,0);
return table[0][0];
}
private int score(int[] nums, int[] muls, int start, int index)
{
if (index == muls.length) return 0;
if (table[start][index] != null) return table[start][index];
int left = muls[index]*nums[start]+score(nums,muls,start+1,index+1);
int right = muls[index]*nums[nums.length-1-(index-start)]+score(nums,muls,start,index+1);
return table[start][index] = Math.max(left,right);
}
}
//Accepted
class Solution {
public int maximumScore(int[] nums, int[] multipliers) {
int n = nums.length, m = multipliers.length;
int[] dp = new int[m+1];
for (int index = m-1; index >= 0; --index)
{
int[] nextRow = dp.clone();
for (int left = index; left >= 0; --left)
{
dp[left] = Math.max(multipliers[index]*nums[left]+nextRow[left+1],multipliers[index]*nums[n-1-(index-left)]+nextRow[left]);
}
// for (int i = 0; i < dp.length; ++i)
// System.out.print(dp[i]+" ");
// System.out.println();
}
return dp[0];
}
}