|
1 | | -package in.knowledgegate.dsa.maps; |
| 1 | +package in.knowledgegate.dsa.greedy; |
2 | 2 |
|
3 | 3 | import java.util.HashMap; |
4 | 4 | import java.util.Map; |
5 | 5 |
|
6 | 6 | /** |
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 |
64 | 62 | */ |
65 | | -public class MiniMahjongWinningHand { |
| 63 | +public class MiniMahjongWinningHand2 { |
| 64 | + |
66 | 65 | public boolean isWinningHand(String hand) { |
67 | | - Map<Character, Integer> map = new HashMap<>(); |
| 66 | + Map<Integer, Integer> map = new HashMap<>(); |
68 | 67 | for (int i = 0; i < hand.length(); i++) { |
69 | | - char c = hand.charAt(i); |
| 68 | + int c = hand.charAt(i) - '0'; |
70 | 69 | map.put(c, map.getOrDefault(c, 0) + 1); |
71 | 70 | } |
72 | 71 |
|
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) { |
82 | 109 | return false; |
83 | 110 | } |
84 | 111 | } |
85 | | - return pairFound; |
| 112 | + |
| 113 | + return true; |
86 | 114 | } |
87 | 115 |
|
88 | 116 | 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)); |
123 | 149 | } |
124 | 150 | } |
0 commit comments