-
Notifications
You must be signed in to change notification settings - Fork 65
/
bb.c
59 lines (48 loc) · 1.09 KB
/
bb.c
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
// C# program to illustrate
// Burst balloon proble
using System;
class GFG{
public static int getMax(int[] A, int N)
{
// Add Bordering Balloons
int[] B = new int[N + 2];
B[0] = B[N + 1] = 1;
for(int i = 1; i <= N; i++)
B[i] = A[i - 1];
// Declaring DP array
int[,] dp = new int[(N + 2), (N + 2)];
for(int length = 1;
length < N + 1; length++)
{
for(int left = 1;
left < N - length + 2; left++)
{
int right = left + length -1;
// For a sub-array from indices
// left, right. This innermost
// loop finds the last balloon burst
for(int last = left;
last < right + 1; last++)
{
dp[left, right] = Math.Max(
dp[left, right],
dp[left, last - 1] +
B[left - 1] * B[last] *
B[right + 1] +
dp[last + 1, right]);
}
}
}
return dp[1, N];
}
// Driver code
public static void Main()
{
int[] A = new int[] { 1, 2, 3, 4, 5 };
// Size of the array
int N = A.Length;
// Calling function
Console.WriteLine(getMax(A, N));
}
}
// This code is contributed by sanjoy_62