|
| 1 | +/** |
| 2 | + * Time Complexity: O(n log n) - TreeMap operations |
| 3 | + * Space Complexity: O(n) - TreeMap |
| 4 | + */ |
| 5 | +class Solution { |
| 6 | + public boolean isNStraightHand(int[] hand, int groupSize) { |
| 7 | + if (hand.length % groupSize != 0) { |
| 8 | + return false; |
| 9 | + } |
| 10 | + |
| 11 | + TreeMap<Integer, Integer> cardCount = new TreeMap<>(); |
| 12 | + for (int card : hand) { |
| 13 | + cardCount.put(card, cardCount.getOrDefault(card, 0) + 1); |
| 14 | + } |
| 15 | + |
| 16 | + while (!cardCount.isEmpty()) { |
| 17 | + int firstCard = cardCount.firstKey(); |
| 18 | + |
| 19 | + for (int i = 0; i < groupSize; i++) { |
| 20 | + int card = firstCard + i; |
| 21 | + if (!cardCount.containsKey(card)) { |
| 22 | + return false; |
| 23 | + } |
| 24 | + |
| 25 | + cardCount.put(card, cardCount.get(card) - 1); |
| 26 | + if (cardCount.get(card) == 0) { |
| 27 | + cardCount.remove(card); |
| 28 | + } |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + return true; |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +// Alternative approach using HashMap + Sorting |
| 37 | +class SolutionHashMap { |
| 38 | + public boolean isNStraightHand(int[] hand, int groupSize) { |
| 39 | + if (hand.length % groupSize != 0) { |
| 40 | + return false; |
| 41 | + } |
| 42 | + |
| 43 | + Map<Integer, Integer> cardCount = new HashMap<>(); |
| 44 | + for (int card : hand) { |
| 45 | + cardCount.put(card, cardCount.getOrDefault(card, 0) + 1); |
| 46 | + } |
| 47 | + |
| 48 | + List<Integer> sortedCards = new ArrayList<>(cardCount.keySet()); |
| 49 | + Collections.sort(sortedCards); |
| 50 | + |
| 51 | + for (int card : sortedCards) { |
| 52 | + int count = cardCount.get(card); |
| 53 | + if (count > 0) { |
| 54 | + for (int i = 0; i < groupSize; i++) { |
| 55 | + int currentCard = card + i; |
| 56 | + if (!cardCount.containsKey(currentCard) || cardCount.get(currentCard) < count) { |
| 57 | + return false; |
| 58 | + } |
| 59 | + cardCount.put(currentCard, cardCount.get(currentCard) - count); |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + return true; |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +// Alternative approach using Priority Queue |
| 69 | +class SolutionPriorityQueue { |
| 70 | + public boolean isNStraightHand(int[] hand, int groupSize) { |
| 71 | + if (hand.length % groupSize != 0) { |
| 72 | + return false; |
| 73 | + } |
| 74 | + |
| 75 | + Map<Integer, Integer> cardCount = new HashMap<>(); |
| 76 | + for (int card : hand) { |
| 77 | + cardCount.put(card, cardCount.getOrDefault(card, 0) + 1); |
| 78 | + } |
| 79 | + |
| 80 | + PriorityQueue<Integer> minHeap = new PriorityQueue<>(cardCount.keySet()); |
| 81 | + |
| 82 | + while (!minHeap.isEmpty()) { |
| 83 | + int firstCard = minHeap.peek(); |
| 84 | + |
| 85 | + for (int i = 0; i < groupSize; i++) { |
| 86 | + int card = firstCard + i; |
| 87 | + if (!cardCount.containsKey(card) || cardCount.get(card) == 0) { |
| 88 | + return false; |
| 89 | + } |
| 90 | + |
| 91 | + cardCount.put(card, cardCount.get(card) - 1); |
| 92 | + if (cardCount.get(card) == 0) { |
| 93 | + cardCount.remove(card); |
| 94 | + minHeap.remove(card); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + return true; |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +// Alternative approach using Array |
| 104 | +class SolutionArray { |
| 105 | + public boolean isNStraightHand(int[] hand, int groupSize) { |
| 106 | + if (hand.length % groupSize != 0) { |
| 107 | + return false; |
| 108 | + } |
| 109 | + |
| 110 | + Map<Integer, Integer> cardCount = new HashMap<>(); |
| 111 | + for (int card : hand) { |
| 112 | + cardCount.put(card, cardCount.getOrDefault(card, 0) + 1); |
| 113 | + } |
| 114 | + |
| 115 | + List<Integer> sortedCards = new ArrayList<>(cardCount.keySet()); |
| 116 | + Collections.sort(sortedCards); |
| 117 | + |
| 118 | + for (int card : sortedCards) { |
| 119 | + int count = cardCount.get(card); |
| 120 | + if (count > 0) { |
| 121 | + for (int i = 0; i < groupSize; i++) { |
| 122 | + int currentCard = card + i; |
| 123 | + if (!cardCount.containsKey(currentCard) || cardCount.get(currentCard) < count) { |
| 124 | + return false; |
| 125 | + } |
| 126 | + cardCount.put(currentCard, cardCount.get(currentCard) - count); |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + return true; |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +// Alternative approach using iterative |
| 136 | +class SolutionIterative { |
| 137 | + public boolean isNStraightHand(int[] hand, int groupSize) { |
| 138 | + if (hand.length % groupSize != 0) { |
| 139 | + return false; |
| 140 | + } |
| 141 | + |
| 142 | + TreeMap<Integer, Integer> cardCount = new TreeMap<>(); |
| 143 | + for (int card : hand) { |
| 144 | + cardCount.put(card, cardCount.getOrDefault(card, 0) + 1); |
| 145 | + } |
| 146 | + |
| 147 | + while (!cardCount.isEmpty()) { |
| 148 | + int firstCard = cardCount.firstKey(); |
| 149 | + |
| 150 | + for (int i = 0; i < groupSize; i++) { |
| 151 | + int card = firstCard + i; |
| 152 | + if (!cardCount.containsKey(card)) { |
| 153 | + return false; |
| 154 | + } |
| 155 | + |
| 156 | + cardCount.put(card, cardCount.get(card) - 1); |
| 157 | + if (cardCount.get(card) == 0) { |
| 158 | + cardCount.remove(card); |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + return true; |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +// Alternative approach using while loop |
| 168 | +class SolutionWhileLoop { |
| 169 | + public boolean isNStraightHand(int[] hand, int groupSize) { |
| 170 | + if (hand.length % groupSize != 0) { |
| 171 | + return false; |
| 172 | + } |
| 173 | + |
| 174 | + TreeMap<Integer, Integer> cardCount = new TreeMap<>(); |
| 175 | + for (int card : hand) { |
| 176 | + cardCount.put(card, cardCount.getOrDefault(card, 0) + 1); |
| 177 | + } |
| 178 | + |
| 179 | + while (!cardCount.isEmpty()) { |
| 180 | + int firstCard = cardCount.firstKey(); |
| 181 | + |
| 182 | + for (int i = 0; i < groupSize; i++) { |
| 183 | + int card = firstCard + i; |
| 184 | + if (!cardCount.containsKey(card)) { |
| 185 | + return false; |
| 186 | + } |
| 187 | + |
| 188 | + cardCount.put(card, cardCount.get(card) - 1); |
| 189 | + if (cardCount.get(card) == 0) { |
| 190 | + cardCount.remove(card); |
| 191 | + } |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + return true; |
| 196 | + } |
| 197 | +} |
| 198 | + |
| 199 | +// More concise version |
| 200 | +class SolutionConcise { |
| 201 | + public boolean isNStraightHand(int[] hand, int groupSize) { |
| 202 | + if (hand.length % groupSize != 0) return false; |
| 203 | + |
| 204 | + TreeMap<Integer, Integer> cardCount = new TreeMap<>(); |
| 205 | + for (int card : hand) { |
| 206 | + cardCount.put(card, cardCount.getOrDefault(card, 0) + 1); |
| 207 | + } |
| 208 | + |
| 209 | + while (!cardCount.isEmpty()) { |
| 210 | + int firstCard = cardCount.firstKey(); |
| 211 | + for (int i = 0; i < groupSize; i++) { |
| 212 | + int card = firstCard + i; |
| 213 | + if (!cardCount.containsKey(card)) return false; |
| 214 | + |
| 215 | + cardCount.put(card, cardCount.get(card) - 1); |
| 216 | + if (cardCount.get(card) == 0) cardCount.remove(card); |
| 217 | + } |
| 218 | + } |
| 219 | + |
| 220 | + return true; |
| 221 | + } |
| 222 | +} |
0 commit comments