Skip to content

Commit a609164

Browse files
committed
maxWeightTransformation updated
1 parent b14ed69 commit a609164

2 files changed

Lines changed: 200 additions & 104 deletions

File tree

Lines changed: 93 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,101 @@
1-
/*
2-
Problem Statement: Maximum weight transformation of a given string
3-
Given a string consisting of only A’s and B’s. We can transform the given string to another string by toggling any character. Thus many transformations of the given string are possible. The task is to find Weight of the maximum weight transformation.
41

5-
Weight of a sting is calculated using below formula.
2+
/**
3+
* Problem Statement: Maximum weight transformation of a given string
4+
* Given a string consisting of only A’s and B’s. We can transform the given string to another string by toggling any character. Thus many transformations of the given string are possible. The task is to find Weight of the maximum weight transformation.
5+
* <p>
6+
* Weight of a sting is calculated using below formula.
7+
* <p>
8+
* Weight of string = Weight of total pairs +
9+
* weight of single characters -
10+
* Total number of toggles.
11+
* <p>
12+
* Two consecutive characters are considered as pair only if they
13+
* are different.
14+
* Weight of a single pair (both character are different) = 4
15+
* Weight of a single character = 1
16+
* <p>
17+
* This problem was asked during an interview in Adobe.
18+
* <p>
19+
* First I provided a DP based solution and then a brute force solution.
20+
*/
621

7-
Weight of string = Weight of total pairs +
8-
weight of single characters -
9-
Total number of toggles.
22+
package algo.dp;
1023

11-
Two consecutive characters are considered as pair only if they
12-
are different.
13-
Weight of a single pair (both character are different) = 4
14-
Weight of a single character = 1
24+
public class MaxWeightTransformation {
1525

16-
This problem was asked during an interview in Adobe.
1726

18-
First I provided a DP based solution and then a brute force solution.
19-
*/
27+
/**
28+
* Dynamic Programming Solution.
29+
* str1[] : String array (str.toCharArray())
30+
* i: first index of string (0)
31+
* size: str.length() -1
32+
* lookup: int[str.length()] (initialize all lookup indexes with -1)
33+
*/
34+
35+
public int maxWeightTransformationDPSol(char str1[], int i, int size, int lookup[]) {
36+
// If the counter has gone past maximum size
37+
// of string, then return 0
38+
if (i > size)
39+
return 0;
40+
// If the counter is equal to the size of
41+
// string, then retun 1. Since, there is no point of string
42+
// transformation.
43+
if (i == size)
44+
return 1;
45+
46+
// if the subproblem is already solved
47+
if (lookup[i] != -1)
48+
return lookup[i];
49+
50+
// If pair is not made, weight gained is 1
51+
int res = 1 + maxWeightTransformationDPSol(str1, i + 1, size, lookup);
52+
53+
// is pair is made
54+
if (i + 1 <= size) {
55+
// if consecutive elements are same
56+
// weight gained is 3 after transformation
57+
if (str1[i] == str1[i + 1]) {
58+
res = Math.max(res, 3 + maxWeightTransformationDPSol(str1, i + 2, size, lookup));
59+
} else if (str1[i] != str1[i + 1]) { // if consecutive elements are different weight gained is 4
60+
res = Math.max(res, 4 + maxWeightTransformationDPSol(str1, i + 2, size, lookup));
61+
}
62+
}
63+
return lookup[i] = res;
64+
}
65+
66+
/**
67+
* Brute Force Solution
68+
* str1[] : String array (str.toCharArray())
69+
* i: first index of string (0)
70+
* size: str.length() -1
71+
*/
72+
public int maxWeightTransformationNoDPSol(char str1[], int i, int size) {
73+
// If the counter has gone past maximum size
74+
// of string, then return 0
75+
if (i > size)
76+
return 0;
77+
78+
// If the counter is equal to the size of
79+
// string, then retun 1. Since, there is no point of string
80+
// transformation.
81+
if (i == size)
82+
return 1;
83+
84+
// If pair is not made, weight gained is 1
85+
int res = 1 + maxWeightTransformationNoDPSol(str1, i + 1, size);
86+
87+
// is pair is made
88+
if (i + 1 <= size) {
89+
if (str1[i] == str1[i + 1]) {// if consecutive elements are same, weight gained is 3 after transformation
90+
res = Math.max(res, 3 + maxWeightTransformationNoDPSol(str1, i + 2, size));
91+
} else if (str1[i] != str1[i + 1]) {// if consecutive elements are different, weight gained is 4
92+
res = Math.max(res, 4 + maxWeightTransformationNoDPSol(str1, i + 2, size));
93+
}
94+
}
95+
96+
return res;
97+
}
2098

21-
package algo.dp;
2299

23-
public class MaxWeightTransformation{
24-
25-
/*
26-
Dynamic Programming Solution.
27-
str1[] : String array (str.toCharArray())
28-
i: first index of string (0)
29-
size: str.length() -1
30-
lookup: int[str.length()] (initialize all lookup indexes with -1)
31-
*/
32-
/*public int maxWeightTransformationDPSol(char str1[], int i, int size, int lookup[]) {
33-
// If the counter has gone past maximum size
34-
// of string, then return 0
35-
if (i > size)
36-
return 0;
37-
// If the counter is equal to the size of
38-
// string, then retun 1. Since, there is no point of string
39-
// transformation.
40-
if (i == size)
41-
return 1;
42-
43-
// if the subproblem is already solved
44-
if (lookup[i] != -1)
45-
return lookup[i];
46-
47-
// If pair is not made, weight gained is 1
48-
int res = 1 + max_len(str1, i+1, size, lookup);
49-
50-
// is pair is made
51-
if(i+1 <= size)
52-
{
53-
// if consecutive elements are same
54-
// weight gained is 3 after transformation
55-
if (str1[i] == str1[i+1])
56-
{
57-
res = Math.max(res, 3 + max_len(str1, i+2, size, lookup));
58-
}
59-
// if consecutive elements are different
60-
// weight gained is 4
61-
else if(str1[i] != str1[i+1])
62-
{
63-
res = Math.max(res, 4 + max_len(str1, i+2, size, lookup));
64-
}
65-
}
66-
return lookup[i] = res;
67-
}*/
68-
69-
/*
70-
Brute Force Solution
71-
str1[] : String array (str.toCharArray())
72-
i: first index of string (0)
73-
size: str.length() -1
74-
*/
75-
/*public int maxWeightTransformationNoDPSol(char str1[], int i, int size) {
76-
// If the counter has gone past maximum size
77-
// of string, then return 0
78-
if (i > size)
79-
return 0;
80-
81-
// If the counter is equal to the size of
82-
// string, then retun 1. Since, there is no point of string
83-
// transformation.
84-
if (i == size)
85-
return 1;
86-
87-
// If pair is not made, weight gained is 1
88-
int res = 1 + max_len(str1, i+1, size);
89-
90-
// is pair is made
91-
if(i+1 <= size)
92-
{
93-
// if consecutive elements are same
94-
// weight gained is 3 after transformation
95-
if (str1[i] == str1[i+1])
96-
{
97-
res = Math.max(res, 3 + max_len(str1, i+2, size));
98-
}
99-
// if consecutive elements are different
100-
// weight gained is 4
101-
else if(str1[i] != str1[i+1])
102-
{
103-
res = Math.max(res, 4 + max_len(str1, i+2, size));
104-
}
105-
}
106-
107-
return res;
108-
}*/
109-
110-
111-
}
100+
}
112101

‎src/triplebyte/Board.java‎

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package triplebyte;
2+
3+
import java.util.Scanner;
4+
5+
/**
6+
* Created by sherxon on 5/17/17.
7+
*/
8+
public class Board {
9+
10+
static char[][] a = new char[3][3];
11+
12+
public static void main(String[] args) {
13+
clear();
14+
Scanner in = new Scanner(System.in);
15+
AI ai = new AI(a);
16+
while (!isFull()) {
17+
int i = in.nextInt();
18+
int j = in.nextInt();
19+
if (i > 2 || i < 0 || j < 0 || j > 2) {
20+
System.out.println("Please enter valid input");
21+
continue;
22+
}
23+
if (a[i][j] != '-') {
24+
System.out.println("the spot is already taken sorry ((");
25+
continue;
26+
}
27+
28+
a[i][j] = 'X';
29+
30+
if (isFull()) {
31+
break;
32+
}
33+
ai.move();
34+
print();
35+
}
36+
}
37+
38+
private static void clear() {
39+
for (int i = 0; i < a.length; i++) {
40+
for (int j = 0; j < a[i].length; j++) {
41+
a[i][j] = '-';
42+
}
43+
}
44+
}
45+
46+
private static void read(char token, int i, int j) {
47+
if (a[i][j] != '0')
48+
a[i][j] = token;
49+
else
50+
System.out.println("taken");
51+
}
52+
53+
private static void print() {
54+
for (int i = 0; i < a.length; i++) {
55+
for (int j = 0; j < a[i].length; j++) {
56+
if (j != a.length - 1)
57+
System.out.print(a[i][j] + "|");
58+
else
59+
System.out.print(a[i][j]);
60+
61+
}
62+
System.out.println();
63+
}
64+
}
65+
66+
67+
public static boolean isFull() {
68+
for (int i = 0; i < a.length; i++) {
69+
for (int j = 0; j < a[i].length; j++) {
70+
if (a[i][j] != '-') return true;
71+
}
72+
}
73+
74+
return false;
75+
}
76+
77+
private static class AI {
78+
char[][] a;
79+
80+
public AI(char[][] a) {
81+
this.a = a;
82+
}
83+
84+
void move() {
85+
if (!checkMove())
86+
throw new IllegalStateException("AI, you are wrong ");
87+
}
88+
89+
int[] makeMove() {
90+
for (int i = 0; i < a.length; i++) {
91+
for (int j = 0; j < a[i].length; j++) {
92+
if (a[i][j] == '-') return new int[]{i, j};
93+
}
94+
}
95+
return new int[]{0, 0};
96+
}
97+
98+
boolean checkMove() {
99+
int[] index = makeMove();
100+
if (a[index[0]][index[1]] != '-') return false;
101+
102+
a[index[0]][index[1]] = '0';
103+
104+
return true;
105+
}
106+
}
107+
}

0 commit comments

Comments
 (0)