-
Notifications
You must be signed in to change notification settings - Fork 0
/
Minimum Cost to cut a board into squares
51 lines (45 loc) · 1.29 KB
/
Minimum Cost to cut a board into squares
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
int minimumCostOfBreaking(vector<int> X, vector<int> Y, int M, int N){
//Write your code here
sort(X.begin() , X.end() , greater<int>()) ;
sort(Y.begin() , Y.end() , greater<int>() ) ;
int hp = 1 ;
int vp = 1 ;
int i = 0;
int j = 0 ;
int c = 0 ;
while(i < X.size() && j < Y.size())
{
// cout << "v" << X[i] * vp << " " << Y[j] * hp << endl ;
if( X[i] > Y[j] )
{
c = c + X[i] * vp ;
i++ ;
hp++ ;
// cout << "h" <<c << " " << vp - 1 << " " << hp - 1<< endl ;
}
else
{
c = c + Y[j] * hp;
j++ ;
vp++ ;
// cout << "v" <<c << " " << vp - 1 << " " << hp - 1<< endl ;
}
}
if(i < X.size() )
{
while(i < X.size())
{
c = c + X[i] * vp ;
i++ ;
}
}
if(j < Y.size())
{
while(j < Y.size())
{
c = c + Y[j] * hp ;
j++ ;
}
}
return c ;
}