aboutsummaryrefslogtreecommitdiff
path: root/hand_of_straights.py
diff options
context:
space:
mode:
Diffstat (limited to 'hand_of_straights.py')
-rw-r--r--hand_of_straights.py33
1 files changed, 33 insertions, 0 deletions
diff --git a/hand_of_straights.py b/hand_of_straights.py
new file mode 100644
index 0000000..4b5ea9f
--- /dev/null
+++ b/hand_of_straights.py
@@ -0,0 +1,33 @@
+# 846. Hand of Straights
+from collections import Counter
+
+"""
+alice has some number of cards and she wants to rearrange the cards into
+groups so that each group is of size 'groupSize' and consists of 'groupSize'
+consecutive cards. given an integer array 'hand' where 'hand[i]' is the value
+written on the i'th card and an integer 'groupSize', return true if she can
+rearrange the cards or false otherwise.
+"""
+
+
+class Solution(object):
+ def isNStraightHand(self, hand, groupSize):
+ """
+ :type hand: List[int]
+ :type groupSize: int
+ :rtype: bool
+ """
+ c = Counter(hand)
+ for i in sorted(c):
+ if c[i] > 0:
+ for j in range(groupSize)[::-1]:
+ c[i + j] -= c[i]
+ if c[i + j] < 0:
+ return False
+ return True
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.isNStraightHand(hand=[1, 2, 3, 6, 2, 3, 4, 7, 8], groupSize=3))
+ print(obj.isNStraightHand(hand=[1, 2, 3, 4, 5], groupSize=4))