Skip to content

Commit 13407e4

Browse files
authored
Merge pull request #5 from shivu2806/main
matrixChain.java
2 parents ecb8dab + ea0c5ef commit 13407e4

File tree

3 files changed

+133
-0
lines changed

3 files changed

+133
-0
lines changed

matrixChainMultiply.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/* A naive recursive implementation that simply follows
2+
the above optimal substructure property */
3+
class MatrixChainMultiplication {
4+
// Matrix Ai has dimension p[i-1] x p[i] for i = 1..n
5+
static int MatrixChainOrder(int p[], int i, int j)
6+
{
7+
if (i == j)
8+
return 0;
9+
10+
int min = Integer.MAX_VALUE;
11+
12+
// place parenthesis at different places between
13+
// first and last matrix, recursively calculate
14+
// count of multiplications for each parenthesis
15+
// placement and return the minimum count
16+
for (int k = i; k < j; k++)
17+
{
18+
int count = MatrixChainOrder(p, i, k)
19+
+ MatrixChainOrder(p, k + 1, j)
20+
+ p[i - 1] * p[k] * p[j];
21+
22+
if (count < min)
23+
min = count;
24+
}
25+
26+
// Return minimum count
27+
return min;
28+
}
29+
30+
// Driver code
31+
public static void main(String args[])
32+
{
33+
int arr[] = new int[] { 1, 2, 3, 4, 3 };
34+
int n = arr.length;
35+
36+
System.out.println(
37+
"Minimum number of multiplications is "
38+
+ MatrixChainOrder(arr, 1, n - 1));
39+
}
40+
}
41+
/* This code is contributed by Rajat Mishra*/

minCost.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/* A Naive recursive implementation of
2+
MCP(Minimum Cost Path) problem */
3+
public class GFG {
4+
5+
/* A utility function that returns
6+
minimum of 3 integers */
7+
static int min(int x, int y, int z)
8+
{
9+
if (x < y)
10+
return (x < z) ? x : z;
11+
else
12+
return (y < z) ? y : z;
13+
}
14+
15+
/* Returns cost of minimum cost path
16+
from (0,0) to (m, n) in mat[R][C]*/
17+
static int minCost(int cost[][], int m,
18+
int n)
19+
{
20+
if (n < 0 || m < 0)
21+
return Integer.MAX_VALUE;
22+
else if (m == 0 && n == 0)
23+
return cost[m][n];
24+
else
25+
return cost[m][n] +
26+
min( minCost(cost, m-1, n-1),
27+
minCost(cost, m-1, n),
28+
minCost(cost, m, n-1) );
29+
}
30+
31+
// Driver code
32+
public static void main(String args[])
33+
{
34+
35+
int cost[][] = { {1, 2, 3},
36+
{4, 8, 2},
37+
{1, 5, 3} };
38+
39+
System.out.print(minCost(cost, 2, 2));
40+
}
41+
}
42+
43+
// This code is contributed by Sam007

stringBuilder.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Java code to illustrate StringBuilder
2+
3+
import java.util.*;
4+
import java.util.concurrent.LinkedBlockingQueue;
5+
6+
public class GFG1 {
7+
public static void main(String[] argv)
8+
throws Exception
9+
{
10+
11+
// create a StringBuilder object
12+
// usind StringBuilder() constructor
13+
StringBuilder str
14+
= new StringBuilder();
15+
16+
str.append("GFG");
17+
18+
// print string
19+
System.out.println("String = "
20+
+ str.toString());
21+
22+
// create a StringBuilder object
23+
// usind StringBuilder(CharSequence) constructor
24+
StringBuilder str1
25+
= new StringBuilder("AAAABBBCCCC");
26+
27+
// print string
28+
System.out.println("String1 = "
29+
+ str1.toString());
30+
31+
// create a StringBuilder object
32+
// usind StringBuilder(capacity) constructor
33+
StringBuilder str2
34+
= new StringBuilder(10);
35+
36+
// print string
37+
System.out.println("String2 capacity = "
38+
+ str2.capacity());
39+
40+
// create a StringBuilder object
41+
// usind StringBuilder(String) constructor
42+
StringBuilder str3
43+
= new StringBuilder(str1.toString());
44+
45+
// print string
46+
System.out.println("String3 = "
47+
+ str3.toString());
48+
}
49+
}

0 commit comments

Comments
 (0)