hand_of_straights
1# @leet start 2from sortedcontainers import SortedList 3 4 5class Solution: 6 def isNStraightHand(self, hand: list[int], groupSize: int) -> bool: 7 """ 8 This question asks us to find if we can decompose a given set of cards 9 into `groupSize` groups that are all straights (the cards are in order). 10 11 To do this, we can greedily iterate through the hand in sorted order. 12 We first put the hand into a sorted list, and we try to create each group. 13 To do so, we pick the minimum available card, and then, for the next 14 groupSize - 1 cards, we make sure we have the cards to make a straight. 15 If we don't, we return false. 16 17 If at the end we process all of the cards, we return true. 18 19 We can do this in $O(n)$ time if we use a counter and count backwards. 20 """ 21 n = len(hand) 22 if n % groupSize != 0: 23 return False 24 25 sorted_hand = SortedList(hand) 26 27 while sorted_hand: 28 min_val = sorted_hand.pop(0) 29 for i in range(1, groupSize): 30 if min_val + i not in sorted_hand: 31 return False 32 sorted_hand.remove(min_val + i) 33 34 return True 35 36 37# @leet end 38q = Solution().isNStraightHand 39 40 41def test(): 42 assert q([1, 2, 3, 6, 2, 3, 4, 7, 8], 3) 43 assert q([1, 2, 3, 4, 5], 4) is False
6class Solution: 7 def isNStraightHand(self, hand: list[int], groupSize: int) -> bool: 8 """ 9 This question asks us to find if we can decompose a given set of cards 10 into `groupSize` groups that are all straights (the cards are in order). 11 12 To do this, we can greedily iterate through the hand in sorted order. 13 We first put the hand into a sorted list, and we try to create each group. 14 To do so, we pick the minimum available card, and then, for the next 15 groupSize - 1 cards, we make sure we have the cards to make a straight. 16 If we don't, we return false. 17 18 If at the end we process all of the cards, we return true. 19 20 We can do this in $O(n)$ time if we use a counter and count backwards. 21 """ 22 n = len(hand) 23 if n % groupSize != 0: 24 return False 25 26 sorted_hand = SortedList(hand) 27 28 while sorted_hand: 29 min_val = sorted_hand.pop(0) 30 for i in range(1, groupSize): 31 if min_val + i not in sorted_hand: 32 return False 33 sorted_hand.remove(min_val + i) 34 35 return True
7 def isNStraightHand(self, hand: list[int], groupSize: int) -> bool: 8 """ 9 This question asks us to find if we can decompose a given set of cards 10 into `groupSize` groups that are all straights (the cards are in order). 11 12 To do this, we can greedily iterate through the hand in sorted order. 13 We first put the hand into a sorted list, and we try to create each group. 14 To do so, we pick the minimum available card, and then, for the next 15 groupSize - 1 cards, we make sure we have the cards to make a straight. 16 If we don't, we return false. 17 18 If at the end we process all of the cards, we return true. 19 20 We can do this in $O(n)$ time if we use a counter and count backwards. 21 """ 22 n = len(hand) 23 if n % groupSize != 0: 24 return False 25 26 sorted_hand = SortedList(hand) 27 28 while sorted_hand: 29 min_val = sorted_hand.pop(0) 30 for i in range(1, groupSize): 31 if min_val + i not in sorted_hand: 32 return False 33 sorted_hand.remove(min_val + i) 34 35 return True
This question asks us to find if we can decompose a given set of cards
into groupSize groups that are all straights (the cards are in order).
To do this, we can greedily iterate through the hand in sorted order. We first put the hand into a sorted list, and we try to create each group. To do so, we pick the minimum available card, and then, for the next groupSize - 1 cards, we make sure we have the cards to make a straight. If we don't, we return false.
If at the end we process all of the cards, we return true.
We can do this in $O(n)$ time if we use a counter and count backwards.
7 def isNStraightHand(self, hand: list[int], groupSize: int) -> bool: 8 """ 9 This question asks us to find if we can decompose a given set of cards 10 into `groupSize` groups that are all straights (the cards are in order). 11 12 To do this, we can greedily iterate through the hand in sorted order. 13 We first put the hand into a sorted list, and we try to create each group. 14 To do so, we pick the minimum available card, and then, for the next 15 groupSize - 1 cards, we make sure we have the cards to make a straight. 16 If we don't, we return false. 17 18 If at the end we process all of the cards, we return true. 19 20 We can do this in $O(n)$ time if we use a counter and count backwards. 21 """ 22 n = len(hand) 23 if n % groupSize != 0: 24 return False 25 26 sorted_hand = SortedList(hand) 27 28 while sorted_hand: 29 min_val = sorted_hand.pop(0) 30 for i in range(1, groupSize): 31 if min_val + i not in sorted_hand: 32 return False 33 sorted_hand.remove(min_val + i) 34 35 return True
This question asks us to find if we can decompose a given set of cards
into groupSize groups that are all straights (the cards are in order).
To do this, we can greedily iterate through the hand in sorted order. We first put the hand into a sorted list, and we try to create each group. To do so, we pick the minimum available card, and then, for the next groupSize - 1 cards, we make sure we have the cards to make a straight. If we don't, we return false.
If at the end we process all of the cards, we return true.
We can do this in $O(n)$ time if we use a counter and count backwards.