Skip to content

Commit 8eb5779

Browse files
author
Prashant Jain
committed
Game problems
1 parent 8dc600a commit 8eb5779

3 files changed

Lines changed: 388 additions & 107 deletions

File tree

Lines changed: 133 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,136 @@
11
package in.knowledgegate.dsa.dfs;
22

3-
public class Solitare {
3+
/**
4+
* While your players are waiting for a game,
5+
* you've developed a solitaire game for the
6+
* players to pass the time with.
7+
* The player is given an NxM board of tiles from
8+
* 0 to 9 like this:
9+
* 4 4 4 4
10+
* 5 5 5 4
11+
* 2 5 7 5
12+
* The player selects one of these tiles, and that
13+
* tile will disappear, along with any tiles with
14+
* the same number that are connected with that tile
15+
* (up, down, left, or right), and any tiles with the
16+
* same number connected with those, and so on. For
17+
* example, if the 4 in the upper left corner is
18+
* selected, these five tiles disappear
19+
* >4< >4< >4< >4<
20+
* 5 5 5 >4<
21+
* 2 5 7 5
22+
* If the 5 just below it is selected, these four
23+
* tiles disappear. Note that tiles are not
24+
* connected diagonally.
25+
* 4 4 4 4
26+
* >5< >5< >5< 4
27+
* 2 >5< 7 5
28+
* Write a function that, given a grid of tiles
29+
* and a selected row and column of a tile,
30+
* returns how many tiles will disappear.
31+
* grid1 = [[4, 4, 4, 4],
32+
* [5, 5, 5, 4],
33+
* [2, 5, 7, 5]]
34+
* disappear(grid1, 0, 0) => 5
35+
* disappear(grid1, 1, 1) => 4
36+
* disappear(grid1, 1, 0) => 4
37+
* This is the grid from above.
38+
*
39+
* Additional Inputs
40+
* grid2 = [[0, 3, 3, 3, 3, 3, 3],
41+
* [0, 1, 1, 1, 1, 1, 3],
42+
* [0, 2, 2, 0, 2, 1, 4],
43+
* [0, 1, 2, 2, 2, 1, 3],
44+
* [0, 1, 1, 1, 1, 1, 3],
45+
* [0, 0, 0, 0, 0, 0, 0]]
46+
*
47+
* grid3 = [[0]]
48+
*
49+
* grid4 = [[1, 1, 1],
50+
* [1, 1, 1],
51+
* [1, 1, 1]]
52+
*
53+
* All Test Cases
54+
* disappear(grid1, 0, 0) => 5
55+
* disappear(grid1, 1, 1) => 4
56+
* disappear(grid1, 1, 0) => 4
57+
* disappear(grid2, 0, 0) => 12
58+
* disappear(grid2, 3, 0) => 12
59+
* disappear(grid2, 1, 1) => 13
60+
* disappear(grid2, 2, 2) => 6
61+
* disappear(grid2, 0, 3) => 7
62+
* disappear(grid3, 0, 0) => 1
63+
* disappear(grid4, 0, 0) => 9
64+
*
65+
* N - Width of the grid
66+
* M - Height of the grid
67+
*
68+
* Clarifications
69+
* - All values are between 0 and 9
70+
* - The grid will not be empty
71+
* - The input board is immutable. If the
72+
* candidate suggests modifying the board you
73+
* should suggest they make a deep copy of the board
74+
* instead. This does not constitute handholding.
75+
* - The input row and column will always be on
76+
* the board.
77+
*/
78+
public class Solitaire {
79+
public int disappear(int[][] grid, int x,
80+
int y) {
81+
if (grid.length <= 0) return 0;
82+
int[][] newGrid =
83+
new int[grid.length][grid[0].length];
84+
for (int i = 0; i < grid.length; i++) {
85+
for (int j = 0; j < grid[i].length; j++) {
86+
newGrid[i][j] = grid[i][j];
87+
}
88+
}
89+
return disappear(newGrid, x, y, newGrid[x][y]);
90+
}
91+
public int disappear(int[][] grid, int x,
92+
int y, int num) {
93+
if (x >= grid.length || y >= grid[0].length ||
94+
x < 0 || y < 0 || grid[x][y] != num ||
95+
grid[x][y] == -1) return 0;
96+
97+
grid[x][y] = -1;
98+
return 1 + disappear(grid, x, y + 1, num)
99+
+ disappear(grid, x + 1, y, num)
100+
+ disappear(grid, x - 1, y, num)
101+
+ disappear(grid, x, y - 1, num);
102+
}
103+
104+
public static void main(String[] args) {
105+
int[][] grid1 =
106+
{{4, 4, 4, 4},
107+
{5, 5, 5, 4},
108+
{2, 5, 7, 5}};
109+
int[][] grid2 = {{0, 3, 3, 3, 3, 3, 3},
110+
{0, 1, 1, 1, 1, 1, 3},
111+
{0, 2, 2, 0, 2, 1, 4},
112+
{0, 1, 2, 2, 2, 1, 3},
113+
{0, 1, 1, 1, 1, 1, 3},
114+
{0, 0, 0, 0, 0, 0, 0}};
115+
int[][] grid3 = {{0}};
116+
int[][] grid4 = {{1, 1, 1},
117+
{1, 1, 1},
118+
{1, 1, 1}};
119+
120+
Solitaire solitaire = new Solitaire();
121+
System.out.println(solitaire.disappear(grid1, 0 , 0));
122+
System.out.println(solitaire.disappear(grid1, 1 , 0));
123+
System.out.println(solitaire.disappear(grid1, 2 , 0));
124+
System.out.println(solitaire.disappear(grid1, 2 , 2));
125+
126+
System.out.println(solitaire.disappear(grid2, 0 , 0));
127+
System.out.println(solitaire.disappear(grid2, 0 , 1));
128+
System.out.println(solitaire.disappear(grid2, 1 , 1));
129+
System.out.println(solitaire.disappear(grid2, 2 , 1));
130+
System.out.println(solitaire.disappear(grid2, 0 , 0));
131+
132+
System.out.println(solitaire.disappear(grid3, 0 , 0));
133+
134+
System.out.println(solitaire.disappear(grid4, 0 , 0));
135+
}
4136
}
Lines changed: 131 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -1,124 +1,150 @@
1-
package in.knowledgegate.dsa.maps;
1+
package in.knowledgegate.dsa.greedy;
22

33
import java.util.HashMap;
44
import java.util.Map;
55

66
/**
7-
* You're creating a game with some amusing mini-games,
8-
* and you've decided to make a simple variant of
9-
* the game Mahjong.
10-
*
11-
* In this variant, players have a number of tiles,
12-
* each marked 0-9. The tiles can be grouped into
13-
* pairs or triples of the same tile. For example,
14-
* if a player has "33344466", the player's hand
15-
* has a triple of 3s, a triple of 4s, and a pair
16-
* of 6s. Similarly, "55555777" has a triple of 5s,
17-
* a pair of 5s, and a triple of 7s.
18-
*
19-
* A "complete hand" is defined as a collection
20-
* of tiles where all the tiles can be grouped
21-
* into any number of triples (zero or more) and
22-
* exactly one pair, and each tile is used in
23-
* exactly one triple or pair.
24-
*
25-
* Write a function that takes a string representation of a collection of tiles in no particular order, and returns true or false depending on whether or not the collection represents a complete hand.
26-
*
27-
* tiles1 = "11133555" # True. 111 33 555
28-
* tiles2 = "111333555" # False. There are three triples, 111 333 555 but no pair.
29-
* tiles3 = "00000111" # True. 000 00 111. Your pair and a triplet can be of the same value
30-
* # There is also no limit to how many of each tile there is.
31-
* tiles4 = "13233121" # True. Tiles are not guaranteed to be in order
32-
* tiles5 = "11223344555" # False. There cannot be more than one pair
33-
* tiles6 = "99999999" # True. You can have many of one tile
34-
* tiles7 = "999999999" # False.
35-
* tiles8 = "9" # False.
36-
* tiles9 = "99" # True. One pair.
37-
* tiles10 = "000022" # False.
38-
* tiles11 = "888889" # False. There cannot be any tiles left over.
39-
* tiles12 = "889" # False. There cannot be any tiles left over.
40-
* tiles13 = "88888844" # True. Two triples and one pair
41-
* tiles14 = "77777777777777" # True. Four triples and one pair
42-
* tiles15 = "1111111" # False.
43-
* tiles16 = "1111122222" # False.
44-
*
45-
* complete(tiles1) => True
46-
* complete(tiles2) => False
47-
* complete(tiles3) => True
48-
* complete(tiles4) => True
49-
* complete(tiles5) => False
50-
* complete(tiles6) => True
51-
* complete(tiles7) => False
52-
* complete(tiles8) => False
53-
* complete(tiles9) => True
54-
* complete(tiles10) => False
55-
* complete(tiles11) => False
56-
* complete(tiles12) => False
57-
* complete(tiles13) => True
58-
* complete(tiles14) => True
59-
* complete(tiles15) => False
60-
* complete(tiles16) => False
61-
*
62-
* Complexity Variable
63-
* N - Number of tiles in the input string
7+
You've decided to make an advanced version of a
8+
variant of the game Mahjong, incorporating runs.
9+
10+
Players have a number of hand, each marked 0-9.
11+
The hand can be grouped into pairs or triples of
12+
the same tile or runs.
13+
14+
A run is a series of three consecutive hand,
15+
like 123, 678, or 456. 0 counts as the lowest tile,
16+
so 012 is a valid run, but 890 is not.
17+
18+
A complete hand consists of exactly one pair, and
19+
any number (including zero) of triples and/or
20+
three-tile runs. Each tile is used in exactly one
21+
triple, pair, or run.
22+
23+
Write a function that returns true or false
24+
depending on whether or not the collection
25+
represents a complete hand under these rules.
26+
27+
hand1 = "11123" # True. 11 123
28+
hand2 = "12131" # True. Also 11 123. hand are not necessarily sorted.
29+
hand3 = "11123455" # True. 111 234 55
30+
hand4 = "11122334" # True. 11 123 234
31+
hand5 = "11234" # True. 11 234
32+
hand6 = "123456" # False. Needs a pair
33+
hand7 = "11133355577" # True. 111 333 555 77
34+
hand8 = "11223344556677" # True. 11 234 234 567 567 among others
35+
hand9 = "12233444556677" # True. 123 234 44 567 567
36+
hand10 = "11234567899" # False.
37+
hand11 = "00123457" # False.
38+
hand12 = "0012345" # False. A run is only three hand
39+
hand13 = "11890" # False. 890 is not a valid run
40+
hand14 = "99" # True.
41+
hand15 = "111223344" # False.
42+
43+
All Test Cases
44+
advanced(hand1) => True
45+
advanced(hand2) => True
46+
advanced(hand3) => True
47+
advanced(hand4) => True
48+
advanced(hand5) => True
49+
advanced(hand6) => False
50+
advanced(hand7) => True
51+
advanced(hand8) => True
52+
advanced(hand9) => True
53+
advanced(hand10) => False
54+
advanced(hand11) => False
55+
advanced(hand12) => False
56+
advanced(hand13) => False
57+
advanced(hand14) => True
58+
advanced(hand15) => False
59+
60+
Complexity Variable
61+
N - Number of hand in the input string
6462
*/
65-
public class MiniMahjongWinningHand {
63+
public class MiniMahjongWinningHand2 {
64+
6665
public boolean isWinningHand(String hand) {
67-
Map<Character, Integer> map = new HashMap<>();
66+
Map<Integer, Integer> map = new HashMap<>();
6867
for (int i = 0; i < hand.length(); i++) {
69-
char c = hand.charAt(i);
68+
int c = hand.charAt(i) - '0';
7069
map.put(c, map.getOrDefault(c, 0) + 1);
7170
}
7271

73-
boolean pairFound = false;
74-
for (Integer count : map.values()) {
75-
if (count % 3 == 2) {
76-
if (!pairFound) {
77-
pairFound = true;
78-
} else {
79-
return false;
80-
}
81-
} else if (count % 3 != 0) {
72+
for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
73+
if (entry.getValue() < 2) continue;
74+
Map<Integer, Integer> newMap = new HashMap<>(map);
75+
newMap.put(entry.getKey(),
76+
entry.getValue() - 2);
77+
if (isWinningHand(newMap)) {
78+
return true;
79+
}
80+
}
81+
return false;
82+
}
83+
84+
private boolean isWinningHand(Map<Integer,
85+
Integer> newMap) {
86+
// remove triplets
87+
for (int i = 0; i <= 9; i++) {
88+
int val = newMap.getOrDefault(i, 0);
89+
newMap.put(i, val % 3);
90+
}
91+
92+
//remove all possible runs
93+
for (int i = 0; i < 7; i++) {
94+
int val1 = newMap.getOrDefault(i, 0);
95+
int val2 = newMap.getOrDefault(i+1, 0);
96+
int val3 = newMap.getOrDefault(i+2, 0);
97+
int run = Math.min(Math.min(val1, val2),
98+
val3);
99+
if (run > 0) {
100+
newMap.put(i, val1 - run);
101+
newMap.put(i+1, val2 - run);
102+
newMap.put(i+2, val3 - run);
103+
}
104+
}
105+
106+
// any value remaining
107+
for (Integer value : newMap.values()) {
108+
if (value > 0) {
82109
return false;
83110
}
84111
}
85-
return pairFound;
112+
113+
return true;
86114
}
87115

88116
public static void main(String[] args) {
89-
String tiles1 = "11133555";
90-
String tiles2 = "111333555";
91-
String tiles3 = "00000111";
92-
String tiles4 = "13233121";
93-
String tiles5 = "11223344555";
94-
String tiles6 = "99999999";
95-
String tiles7 = "999999999";
96-
String tiles8 = "9";
97-
String tiles9 = "99";
98-
String tiles10 = "000022";
99-
String tiles11 = "888889";
100-
String tiles12 = "889";
101-
String tiles13 = "88888844";
102-
String tiles14 = "77777777777777";
103-
String tiles15 = "1111111";
104-
String tiles16 = "1111122222";
105-
MiniMahjongWinningHand obj =
106-
new MiniMahjongWinningHand();
107-
System.out.println(tiles1 + ":" + obj.isWinningHand(tiles1));
108-
System.out.println(tiles2 + ":" + obj.isWinningHand(tiles2));
109-
System.out.println(tiles3 + ":" + obj.isWinningHand(tiles3));
110-
System.out.println(tiles4 + ":" + obj.isWinningHand(tiles4));
111-
System.out.println(tiles5 + ":" + obj.isWinningHand(tiles5));
112-
System.out.println(tiles6 + ":" + obj.isWinningHand(tiles6));
113-
System.out.println(tiles7 + ":" + obj.isWinningHand(tiles7));
114-
System.out.println(tiles8 + ":" + obj.isWinningHand(tiles8));
115-
System.out.println(tiles9 + ":" + obj.isWinningHand(tiles9));
116-
System.out.println(tiles10 + ":" + obj.isWinningHand(tiles10));
117-
System.out.println(tiles11 + ":" + obj.isWinningHand(tiles11));
118-
System.out.println(tiles12 + ":" + obj.isWinningHand(tiles12));
119-
System.out.println(tiles13 + ":" + obj.isWinningHand(tiles13));
120-
System.out.println(tiles14 + ":" + obj.isWinningHand(tiles14));
121-
System.out.println(tiles15 + ":" + obj.isWinningHand(tiles15));
122-
System.out.println(tiles16 + ":" + obj.isWinningHand(tiles16));
117+
String hand1 = "11123";
118+
String hand2 = "12131";
119+
String hand3 = "11123455";
120+
String hand4 = "11122334";
121+
String hand5 = "11234";
122+
String hand6 = "123456";
123+
String hand7 = "11133355577";
124+
String hand8 = "11223344556677";
125+
String hand9 = "12233444556677";
126+
String hand10 = "11234567899";
127+
String hand11 = "00123457";
128+
String hand12 = "0012345";
129+
String hand13 = "11890";
130+
String hand14 = "99";
131+
String hand15 = "111223344";
132+
MiniMahjongWinningHand2 obj =
133+
new MiniMahjongWinningHand2();
134+
System.out.println(hand1 + ":" + obj.isWinningHand(hand1));
135+
System.out.println(hand2 + ":" + obj.isWinningHand(hand2));
136+
System.out.println(hand3 + ":" + obj.isWinningHand(hand3));
137+
System.out.println(hand4 + ":" + obj.isWinningHand(hand4));
138+
System.out.println(hand5 + ":" + obj.isWinningHand(hand5));
139+
System.out.println(hand6 + ":" + obj.isWinningHand(hand6));
140+
System.out.println(hand7 + ":" + obj.isWinningHand(hand7));
141+
System.out.println(hand8 + ":" + obj.isWinningHand(hand8));
142+
System.out.println(hand9 + ":" + obj.isWinningHand(hand9));
143+
System.out.println(hand10 + ":" + obj.isWinningHand(hand10));
144+
System.out.println(hand11 + ":" + obj.isWinningHand(hand11));
145+
System.out.println(hand12 + ":" + obj.isWinningHand(hand12));
146+
System.out.println(hand13 + ":" + obj.isWinningHand(hand13));
147+
System.out.println(hand14 + ":" + obj.isWinningHand(hand14));
148+
System.out.println(hand15 + ":" + obj.isWinningHand(hand15));
123149
}
124150
}

0 commit comments

Comments
 (0)