-
Notifications
You must be signed in to change notification settings - Fork 0
/
Balls.java
126 lines (113 loc) · 2.16 KB
/
Balls.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Balls{
static void print_array(int[] A)
{
for(int i=0;i<A.length;i++)
{
System.out.print(A[i]+" ") ;
}
System.out.println() ;
}
static void matrix_printer(int[][] A)
{
for(int i=0;i<A.length;i++)
{
System.out.println() ;
for(int j=0;j<A[0].length;j++)
{
System.out.print(A[i][j]+" ") ;
}
}
System.out.println() ;
}
static int[] consecutive_removal(int[] A)
{
int first = -1 ;
boolean consec = false ;
for(int i=0;i<A.length-1;i++)
{
if(A[i]==A[i+1])
{
first = i ;
consec = true ;
break ;
}
}
if(consec)
{
int[] A2 = new int[A.length-2] ;
int j=0 ;
for(int i=0;i<A.length;i++)
{
if(i!=first && i!=(first+1))
{
A2[j++] = A[i] ;
}
}
return consecutive_removal(A2) ;
}
else
return A ;
}
public static void main(String args[])
{
Scanner in = new Scanner(System.in) ;
int n = in.nextInt() ;
int[] A = new int[n] ;
for(int i=0;i<n;i++)
{
A[i] = in.nextInt() ;
}
A = consecutive_removal(A) ;
//System.out.println("New Array:" );
//print_array(A) ;
n = A.length ;
if(n!=0)
{
int[][] M = new int[n][n] ;
for(int i=0;i<n;i++)
{
M[i][i] = 1 ;
}
for(int d = 1 ;d<n;d++)
{
for(int i=0;i<n-d;i++)
{
int j = i+d ;
M[i][j] = Integer.MAX_VALUE ; //initialization
for(int k=i;k<j;k++)
{
int sum = M[i][k]+M[k+1][j] ;
if(sum<M[i][j])
M[i][j] = sum ;
}
/* Older code
// System.out.println("i = "+i+"\nj = "+j) ;
if(M[i][j-1]>M[i+1][j])// since one more element is added into consideration
{
M[i][j] = M[i+1][j]+1 ;
// System.out.println("-----Case-1------") ;
}
else
{
M[i][j] = M[i][j-1]+1 ;
// System.out.println("-----Case-2------") ;
}*/
if(A[i]==A[j] && M[i+1][j-1]<M[i][j])
{
M[i][j] = M[i+1][j-1] ;
// System.out.println("-----Case-3------") ;
}
}
}
System.out.println(M[0][n-1]) ;
//matrix_printer(M) ;
}
else
System.out.println(0) ;
}
}