-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathHandOfStraights.java
47 lines (45 loc) · 2.24 KB
/
HandOfStraights.java
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
/*https://leetcode.com/problems/hand-of-straights/*/
class Solution {
public boolean isNStraightHand(int[] hand, int groupSize) {
if (hand.length%groupSize != 0) return false;
Arrays.sort(hand); //sor the array
LinkedHashMap<Integer,Integer> map = new LinkedHashMap<Integer,Integer>();
int i;
//add everything to hashmap
for (i = 0; i < hand.length; ++i)
map.put(hand[i], map.containsKey(hand[i]) ? (Integer)map.get(hand[i])+1 : 1);
boolean result = true; //set result to true
int count = 0; //set count to 0
Iterator it = map.entrySet().iterator(); //start a new iterator over the map
while (count < hand.length) //till count is incomplete
{
it = map.entrySet().iterator(); //restart the iterator
if (it.hasNext()) //call hasNext
{
Map.Entry firstElem = (Map.Entry)it.next(); //get the first element
if ((Integer)firstElem.getValue() > 0) //if the element still exists
{
++count; //increment count
map.put((Integer)firstElem.getKey(),(Integer)firstElem.getValue()-1); //reduce frequency
for (i = 1; i < groupSize; ++i) //for rest of the elements of the group
{
if (!map.containsKey((Integer)firstElem.getKey()+i) || ((Integer)map.get((Integer)firstElem.getKey()+i) == 0)) //if the next element does not exist
{
result = false; //mark result as false
break; //break from loop
}
else //if the element exist
{
++count; //increment count
map.put((Integer)firstElem.getKey()+i,(Integer)map.get((Integer)firstElem.getKey()+i)-1); //reduce frequency
}
}
}
else //if the element does not exist
map.remove((Integer)firstElem.getKey()); //remove from map
}
if (!result) break; //if result has become false, break out of the loop
}
return result; //return result
}
}