description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: hand = sorted(hand) indices = {} for index in reversed(list(range(len(hand)))): num = hand[index] if num not in indices: indices[num] = [index] else: indices[num].append(index) while len(indices) != 0: first = None for num in hand: if num in indices: first = num break for i in range(W): num = first + i if num in indices: indices[num].pop() if len(indices[num]) == 0: del indices[num] else: return False return True
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR LIST VAR EXPR FUNC_CALL VAR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR NONE FOR VAR VAR IF VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR RETURN NUMBER RETURN NUMBER VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: if len(hand) < W or len(hand) % W != 0: return False d = {} for i in hand: d[i] = d.get(i, 0) + 1 keys = sorted(list(d.keys())) for i in keys: if d[i] == 0: continue while d[i] > 0: for j in range(i, i + W): if j not in keys or d[j] <= 0: return False else: d[j] = d[j] - 1 return True
CLASS_DEF FUNC_DEF VAR VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR NUMBER WHILE VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER RETURN NUMBER VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: if len(hand) % W != 0: return False cardCount = {} for card in hand: if not card in cardCount: cardCount[card] = 0 cardCount[card] += 1 k = len(hand) // W for i in range(k): start = min(cardCount.keys()) for j in range(W): if not start + j in cardCount: return False for j in range(W): cardCount[start + j] -= 1 if cardCount[start + j] == 0: cardCount.pop(start + j) return True
CLASS_DEF FUNC_DEF VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN NUMBER VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: if len(hand) % W != 0: return False count = Counter(hand) for i in range(len(hand) // W): num, group_count = min(count.keys()), 0 while group_count < W: count[num] -= 1 if count[num] == 0: del count[num] elif count[num] < 0: return False group_count += 1 num += 1 return True
CLASS_DEF FUNC_DEF VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER WHILE VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR IF VAR VAR NUMBER RETURN NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, nums: List[int], W: int) -> bool: n = len(nums) if n % W != 0: return False nums.sort() dic = OrderedDict() for num in nums: dic[num] = dic.get(num, 0) + 1 while dic: m = min(dic) for i in range(m, m + W): val = dic.get(i) if not val: return False if val == 1: del dic[i] else: dic[i] -= 1 return True
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR RETURN NUMBER IF VAR NUMBER VAR VAR VAR VAR NUMBER RETURN NUMBER VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: n = len(hand) if n % W: return False C = Counter(hand) for i in range(n // W): mn = min(C.keys()) for j in range(mn, mn + W): if j not in C: return False else: C[j] -= 1 if C[j] == 0: del C[j] return True
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR RETURN NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR RETURN NUMBER VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: if len(hand) % W != 0: return False hand.sort() last = -1 j = 0 changed = False while hand: if hand[0] == last + 1 or last == -1: changed = False last = hand.pop(0) j += 1 if j % W == 0: last = -1 elif not changed: for i in range(1, len(hand)): if hand[i] <= last + 1: hand[i], hand[0] = hand[0], hand[i] else: break changed = True else: return False return True
CLASS_DEF FUNC_DEF VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR IF VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR VAR ASSIGN VAR NUMBER RETURN NUMBER RETURN NUMBER VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: counter = Counter(hand) heap_hands = [] groups = [] for k, v in counter.items(): heapq.heappush(heap_hands, (k, v)) while heap_hands: count = 0 sub_group = [] remaining_elements = [] while count < W: if not heap_hands: return False popped_elem = heapq.heappop(heap_hands) if popped_elem[1] - 1 > 0: remaining_elements.append(popped_elem) if not sub_group: sub_group.append(popped_elem[0]) elif popped_elem[0] - 1 == sub_group[-1]: sub_group.append(popped_elem[0]) else: return False count += 1 groups += sub_group for elem in remaining_elements: heapq.heappush(heap_hands, (elem[0], elem[1] - 1)) return True
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR WHILE VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST WHILE VAR VAR IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN NUMBER VAR NUMBER VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN NUMBER VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand, W): nums = len(hand) if nums % W: return False elif W == 1: return True else: heapify(hand) arrange = deque() lastOp = [0, -1] groups = 0 while hand: h = heappop(hand) i = 0 if h == lastOp[1]: i = lastOp[0] + 1 if len(arrange) < i + 1: arrange.append([]) groups += 1 if groups > nums // W: return False if len(arrange[i]) and arrange[i][-1] + 1 != h: return False else: arrange[i].append(h) lastOp = [i, h] if len(arrange[i]) == W: arrange.popleft() lastOp = [0, -1] return True
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR RETURN NUMBER IF VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER IF VAR BIN_OP VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST VAR VAR IF FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER RETURN NUMBER
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: l = len(hand) if l % W != 0: return False cards = Counter(hand) keys = sorted(list(cards.keys())) start = keys[0] start_idx = 0 for _ in range(l // W): cards[start] -= 1 for i in range(start + 1, start + W): if i not in keys or cards[i] == 0: return False else: cards[i] -= 1 while cards[start] == 0 and start_idx < len(keys) - 1: start_idx += 1 start = keys[start_idx] return True
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR IF VAR VAR VAR VAR NUMBER RETURN NUMBER VAR VAR NUMBER WHILE VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR RETURN NUMBER VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: hand.sort() while hand: curr = hand[0] try: for i in range(W): hand.remove(curr) curr += 1 except: return False return True
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: l = len(hand) if l % W != 0: return False if W == 1: return True ct = Counter(hand) lst = sorted(list(ct.keys())) for l in lst: if l - 1 not in lst and l + 1 not in lst: return False while lst: begin = lst[0] ct[begin] -= 1 if ct[begin] == 0: lst.pop(0) for i in range(1, W): if begin + i not in ct: return False else: ct[begin + i] -= 1 if ct[begin + i] == 0: lst.remove(begin + i) return True
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR RETURN NUMBER WHILE VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR RETURN NUMBER VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN NUMBER VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: if len(hand) == 0: return False if len(hand) % W > 0: return False count = {} for n in hand: if n not in count: count[n] = 1 else: count[n] += 1 total = sum(count.values()) for n in sorted(hand): if total == 0: break if count[n] == 0: continue for i in range(W): target = n + i if target in count and count[target] > 0: count[target] -= 1 total -= 1 else: return False return True
CLASS_DEF FUNC_DEF VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: d = collections.defaultdict(lambda: 0) for h in hand: d[h] += 1 while d: start = min(d.keys()) for i in range(start, start + W): if not d[i]: return False d[i] -= 1 if not d[i]: del d[i] return True
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR VAR VAR VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR RETURN NUMBER VAR VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: l = len(hand) if l % W != 0: return False d = {} for i in range(l): if hand[i] not in d: d[hand[i]] = 1 else: d[hand[i]] += 1 while d: num = min(d) j = 1 while num + 1 in d and j != W: j += 1 num = num + 1 if j == W: num = min(d) for k in range(W): if d[num] == 1: del d[num] else: d[num] -= 1 num += 1 else: return False return True
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER RETURN NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: if len(hand) % W != 0: return False hand = sorted(hand) for split in range(len(hand) // W): min_val = min(hand) hand.remove(min_val) for i in range(1, W): if min_val + 1 not in hand: return False else: min_val += 1 hand.remove(min_val) return True
CLASS_DEF FUNC_DEF VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR RETURN NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN NUMBER VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: d = {} for i in hand: if i in d: d[i] += 1 else: d[i] = 1 sd = {k: v for k, v in sorted(list(d.items()), key=lambda item: item[0])} while sd: keys = list(sd.keys()) i = keys[0] sd[i] -= 1 if sd[i] == 0: del sd[i] count = 1 while count < W: if i + count not in sd: return False else: sd[i + count] -= 1 if sd[i + count] == 0: del sd[i + count] count += 1 return True
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR RETURN NUMBER VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER RETURN NUMBER VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: ogLen = len(hand) if W == 1: return True counter = Counter(hand) counted = [(key, counter[key]) for key in counter] counted.sort() print(counted) heapify(counted) groups = [] group = [] group.append(heappop(counted)) tempStorage = set() while counted: n0 = group[-1] if n0[0] + 1 == counted[0][0]: n1 = heappop(counted) elif counted[0][0] > n0[0] + 1: group = [] group.append(heappop(counted)) continue group.append(n1) if len(group) == W: groups.append(group) for val, count in group: count -= 1 if count != 0: heappush(counted, (val, count)) group = [] if counted: group.append(heappop(counted)) print((len(groups) * W, ogLen)) return len(groups) * W == ogLen
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: if not hand or len(hand) % W != 0: return False counter = Counter(hand) for c in sorted(counter): if counter[c] > 0: for i in range(W)[::-1]: counter[c + i] -= counter[c] if counter[c + i] < 0: return False return True
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR IF VAR BIN_OP VAR VAR NUMBER RETURN NUMBER RETURN NUMBER VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: if len(hand) % W: return False if W == 1: return True counts = collections.Counter(hand) for num in sorted(hand): if not counts[num]: continue for next in range(num, num + W): if next not in counts or not counts[next]: return False counts[next] -= 1 return True
CLASS_DEF FUNC_DEF VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR RETURN NUMBER VAR VAR NUMBER RETURN NUMBER VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: if len(hand) % W: return False counter = dict() for h in hand: temp = counter.get(h, 0) temp += 1 counter[h] = temp while counter: start = min(counter.keys()) for k in range(start, start + W): v = counter.get(k) if not v: return False if v == 1: del counter[k] else: counter[k] = v - 1 return True
CLASS_DEF FUNC_DEF VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR RETURN NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER RETURN NUMBER VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: if not hand or len(hand) % W != 0: return False hand_dict = self.get_hand_dict(hand) return self.is_n_straigh_hand(hand_dict, W) def is_n_straigh_hand(self, hand_dict, W): while hand_dict: start_hand = min(hand_dict) count = hand_dict[start_hand] for i in range(start_hand, W + start_hand): if i not in hand_dict: return False else: hand_dict[i] -= count if hand_dict[i] == 0: del hand_dict[i] elif hand_dict[i] < 0: return False return True def get_hand_dict(self, hand): hand_dict = {} for h in hand: hand_dict[h] = hand_dict.get(h, 0) hand_dict[h] += 1 return hand_dict
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR RETURN NUMBER VAR VAR VAR IF VAR VAR NUMBER VAR VAR IF VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER RETURN VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: card_dict = dict() for card in hand: if card in card_dict: card_dict[card] = card_dict[card] + 1 else: card_dict[card] = 1 while len(card_dict) > 0: min_ele = min(card_dict.keys()) for i in range(W): if not min_ele in card_dict: return False elif card_dict[min_ele] > 1: card_dict[min_ele] = card_dict[min_ele] - 1 else: del card_dict[min_ele] min_ele = min_ele + 1 return True
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN NUMBER VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: n = len(hand) if n % W != 0: return False count = collections.Counter(hand) while n > 0: a = min(count) for i in range(W): if count[a + i] <= 0: return False count[a + i] -= 1 if count[a + i] == 0: del count[a + i] n -= W return True
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR NUMBER RETURN NUMBER VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR RETURN NUMBER VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: N = len(hand) if N % W != 0: return False counts = collections.Counter(hand) while counts: card = min(counts.keys()) for _ in range(W): if card not in counts: return False counts[card] -= 1 if counts[card] == 0: del counts[card] card += 1 return True
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: if len(hand) % W != 0: return False counter = defaultdict(int) for elem in hand: counter[elem] += 1 while len(counter) > 0: curr = min(counter.keys()) counter[curr] -= 1 if counter[curr] == 0: del counter[curr] for i in range(1, W): curr += 1 if curr in counter: counter[curr] -= 1 if counter[curr] == 0: del counter[curr] else: return False return True
CLASS_DEF FUNC_DEF VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR RETURN NUMBER RETURN NUMBER VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: if not hand: return False hand = sorted(hand) queue, rs, group = [], [], [] while hand: card = hand.pop(0) if not group or group[-1] == card - 1: group.append(card) if len(group) == W: hand = queue + hand rs.append(group) group, queue = [], [] else: queue.append(card) if not group and not queue: return True else: return False
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR LIST LIST LIST WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST LIST EXPR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: heapq.heapify(hand) while hand: last_item = heapq.heappop(hand) leftover = [] for _ in range(W - 1): if not hand: return False next_item = heapq.heappop(hand) while next_item == last_item: leftover.append(next_item) if not hand: return False next_item = heapq.heappop(hand) if next_item > last_item + 1: return False last_item = next_item for item in leftover: heapq.heappush(hand, item) return True
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR EXPR FUNC_CALL VAR VAR IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN NUMBER VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: maps = Counter(hand) while maps: k = min(maps.keys()) count = maps[k] for i in range(k, k + W): if i not in maps or maps[i] < count: return False maps[i] -= count if maps[i] == 0: maps.pop(i) return True
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR RETURN NUMBER VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN NUMBER VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: if len(hand) % W != 0: return False dic = {} for num in hand: if num not in dic: dic[num] = 1 else: dic[num] += 1 hand.sort() while len(hand) > 0: smallest = hand[0] dic[smallest] -= 1 for i in range(1, W): check = smallest + i if check not in dic: return False elif dic[check] <= 0: return False else: dic[check] -= 1 hand.remove(check) hand.remove(smallest) return True
CLASS_DEF FUNC_DEF VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR NUMBER RETURN NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: if not hand or not W: return False _map = defaultdict(list) for i in range(len(hand)): _map[hand[i]] += [i] while _map: hand = [k for k, _ in list(_map.items())] top = min(hand) _map[top].pop() if not _map[top]: del _map[top] i = 1 while hand and i < W: top += 1 if top not in _map: return False _map[top].pop() if not _map[top]: del _map[top] i += 1 if i < W: return False return True
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR LIST VAR WHILE VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: if not hand: return W == 0 if len(hand) % W != 0: return False hand.sort() def isnstraighthand(li): if not li: return True start = li[0] for i in range(start, start + W): if i not in li: return False else: li.remove(i) return isnstraighthand(li) return isnstraighthand(hand)
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR RETURN VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], w: int) -> bool: d = {} for i in hand: if i not in d: d[i] = 1 else: d[i] += 1 d = sorted(list(d.items()), key=lambda x: x[0]) d1 = {} for i in d: d1[i[0]] = i[1] while len(d1) > 0: r = list(d1.keys())[0] for i in range(r, r + w): if i not in d1: return False count = d1[i] if count == 1: d1.pop(i) else: d1[i] = d1[i] - 1 return True
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER RETURN NUMBER VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: if len(hand) % W != 0: return False hand.sort() length = len(hand) count = 0 group = [] while 1: for i in range(W - 1): if hand[0] + i + 1 in hand: count += 1 hand.pop(hand.index(hand[0] + i + 1)) else: return False hand.pop(0) if len(hand) == 0: return True
CLASS_DEF FUNC_DEF VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: counted = {} for i in hand: if i not in counted: counted[i] = 0 counted[i] += 1 def containStrait(numbers, counted, W): if len(counted) == 0: return True if len(numbers) < W: return False if numbers[W - 1] - numbers[0] > W: return False else: for i in reversed(range(W)): counted[numbers[i]] -= 1 if counted[numbers[i]] == 0: counted.pop(numbers[i]) numbers.remove(numbers[i]) return containStrait(numbers, counted, W) numbers = sorted(counted.keys()) return containStrait(numbers, counted, W)
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR VAR RETURN NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: hand = collections.Counter(hand) while True: if len(hand) == 0: return True m = min(hand) for c in range(m, m + W): if c not in hand: return False if hand[c] == 1: del hand[c] else: hand[c] -= 1
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: if len(hand) % W != 0: return False counter = collections.Counter(hand) d = collections.OrderedDict(sorted(counter.items())) count = 0 while count < len(d): group = [] for card in d.keys(): if d[card] == 0: continue if not group or len(group) < W and group[-1] + 1 == card: group.append(card) d[card] -= 1 if d[card] == 0: count += 1 if len(group) == W: break else: return False return True
CLASS_DEF FUNC_DEF VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR IF VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN NUMBER VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: straights, pq = [], [] for h in hand: heapq.heappush(pq, h) while len(pq) > 0: straight, dump = [], [] while len(pq) > 0 and len(straight) < W: pop = heapq.heappop(pq) if len(straight) == 0 or pop == straight[-1] + 1: straight.append(pop) else: dump.append(pop) straights.append(straight) if len(straight) < W: return [] else: for d in dump: heapq.heappush(pq, d) return len(straights) > 0
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR LIST LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR LIST LIST WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR RETURN LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR NUMBER VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: dic = collections.Counter(hand) print((dic, min(dic), dic[10])) while dic: m = min(dic) for num in range(m, m + W): if dic[num] == 0: return False if dic[num] == 1: del dic[num] else: dic[num] = dic[num] - 1 return True
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER RETURN NUMBER VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: if not hand: return False if len(hand) % W != 0: return False hand.sort() n = len(hand) dic = Counter(hand) cards = list(dic.keys()) cards.sort() for c in cards: num = dic[c] if num == 0: continue for i in range(1, W): if c + i in dic and dic[c + i] >= num: dic[c + i] -= num else: return False return True
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR RETURN NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR RETURN NUMBER RETURN NUMBER VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: if len(hand) % W != 0: return False hand.sort() count = collections.Counter(hand) while count: key = list(count.keys())[0] for i in range(W): if count[key + i] > 0: count[key + i] -= 1 if count[key + i] == 0: del count[key + i] else: return False return True
CLASS_DEF FUNC_DEF VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR RETURN NUMBER RETURN NUMBER VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: if len(hand) % W != 0: return False hand = sorted(hand) failed = False while hand and not failed: group = [hand[0]] del hand[0] idx = 0 while len(group) < W and idx < len(hand): if group[-1] + 1 == hand[idx]: group.append(hand[idx]) del hand[idx] else: idx += 1 if len(group) < W: failed = True return not failed
CLASS_DEF FUNC_DEF VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR LIST VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER RETURN VAR VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: hand.sort() dic = collections.Counter(hand) while dic: m = next(iter(dic)) for k in range(m, m + W): v = dic.get(k) if not v: return False if v == 1: del dic[k] else: dic[k] = v - 1 return True
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR RETURN NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER RETURN NUMBER VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: if len(hand) % W != 0: return False hand.sort() c = Counter(hand) for i in range(len(hand) // W): if len(hand) < W: return False keys = list(c.keys()) key = keys[0] for j in range(W): if key not in c: return False if c[key] == 1: del c[key] key += 1 else: c[key] -= 1 key += 1 return True
CLASS_DEF FUNC_DEF VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER RETURN NUMBER VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: if len(hand) % W != 0: return False sorted_hand = hand sorted_hand.sort() num_groups = int(len(hand) / W) for i in range(num_groups): num = sorted_hand[0] sorted_hand.remove(num) for j in range(W - 1): num += 1 if num in sorted_hand: sorted_hand.remove(num) else: return False return True
CLASS_DEF FUNC_DEF VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: n = len(hand) if n % W: return False C = Counter(hand) keys = sorted(C.keys()) output = [] for i in range(n // W): mn = keys[0] straight = [] for j in range(mn, mn + W): if j not in keys: return False else: C[j] -= 1 straight.append(j) if C[j] == 0: del C[j] keys.remove(j) output.append(straight) print(output) return True
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR RETURN NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: if len(hand) % W != 0: return False counter, smallest = self.counter(hand) for _ in range(len(hand) // W): if smallest is None: smallest = min(counter) curr = smallest counter[curr] -= 1 if counter[curr] == 0: del counter[curr] smallest = None for _ in range(W - 1): curr = curr + 1 if curr not in counter: return False else: counter[curr] -= 1 if counter[curr] == 0: del counter[curr] elif smallest is None: smallest = curr return True def counter(self, array): smallest = None counter = {} for el in array: if el in counter: counter[el] += 1 else: counter[el] = 1 if smallest is None or el < smallest: smallest = el return counter, smallest
CLASS_DEF FUNC_DEF VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR ASSIGN VAR NONE FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR RETURN NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR IF VAR NONE ASSIGN VAR VAR RETURN NUMBER VAR FUNC_DEF ASSIGN VAR NONE ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NONE VAR VAR ASSIGN VAR VAR RETURN VAR VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: if len(hand) % W != 0: return False counter = collections.Counter(hand) for num in hand: if counter[num - 1] > 0 or counter[num] == 0: continue curr = num cnt = 0 while cnt < W: if counter[curr] == 0: return False counter[curr] -= 1 curr += 1 cnt += 1 return True
CLASS_DEF FUNC_DEF VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER RETURN NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: n = len(hand) if n % W != 0: return False hand = sorted(hand) counts = collections.Counter(hand) print(counts) while sum(counts.values()) > 0: start_card = min(counts) print(start_card) for card in range(start_card, start_card + W): if card not in counts: return False counts[card] -= 1 if counts[card] == 0: del counts[card] return True
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR RETURN NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR RETURN NUMBER VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: if len(hand) % W != 0: return False hand = sorted(hand) dct = {} for x in hand: if x not in dct: dct[x] = 1 else: dct[x] += 1 while len(hand) > 0: group = [hand[0]] check = hand[0] hand.remove(check) while len(group) < W: if check + 1 not in dct or dct[check + 1] < 1: return False group.append(check + 1) hand.remove(check + 1) dct[check + 1] -= 1 check += 1 return True
CLASS_DEF FUNC_DEF VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER RETURN NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN NUMBER VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: counts = defaultdict(int) for number in hand: counts[number] += 1 ordered_numbers = sorted(counts.keys()) while counts: group = [] for number in ordered_numbers: if number not in counts: continue if counts[number] > 0: if group: if group[-1] != number - 1: return False group.append(number) counts[number] -= 1 else: del counts[number] if len(group) == W: break if group and len(group) != W: return False return True
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR LIST FOR VAR VAR IF VAR VAR IF VAR VAR NUMBER IF VAR IF VAR NUMBER BIN_OP VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR IF FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN NUMBER VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: dp = defaultdict(list) for e in sorted(hand): ( dp[e].append(dp[e - 1].pop() + 1) if len(dp[e - 1]) != 0 else dp[e].append(1) ) if dp[e] and dp[e][-1] == W: dp[e].pop() return all(len(e) == 0 for e in list(dp.values()))
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: if len(hand) % W != 0: return False i = 0 hand.sort() while hand: for j in range(1, W): if hand[i] + j not in hand: return False hand.pop(hand.index(hand[i] + j)) hand.pop(i) return True
CLASS_DEF FUNC_DEF VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR WHILE VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: count = Counter(hand) result = [] while count: m = min(count) straights = [] for i in range(m, m + W): if i in count: straights.append(i) count[i] -= 1 if count[i] == 0: del count[i] else: return False result.append(straights) print(result) return True
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: hand.sort() while len(hand) > 0: val = hand.pop(0) for j in range(W - 1): try: val += 1 indx = hand.index(val) hand.pop(indx) except: return False return True
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: n = len(hand) if n % W != 0: return False counter = Counter(hand) nums = sorted(counter.keys()) for num in nums: count = counter[num] if count == 0: continue min_count = count for i in range(W): if count[num + i] < min_count: return False count[num + i] -= min_count return True def isNStraightHand(self, hand: List[int], W: int) -> bool: if len(hand) % W != 0: return False counter = Counter(hand) q = [] for num, freq in list(counter.items()): heapq.heappush(q, (num, freq)) while q and len(q) >= W: tmp = [] for i in range(W): x = heapq.heappop(q) if tmp and tmp[-1][0] + 1 != x[0]: return False tmp.append(x) for num, freq in tmp: if freq > 1: heapq.heappush(q, (num, freq - 1)) return 0 == len(q) def isNStraightHand4(self, hand: List[int], W: int) -> bool: if len(hand) % W != 0: return False hand.sort() q = [] for num in hand: if q and q[-1][0] == num: q[-1][1] += 1 else: q.append([num, 1]) n = len(hand) while n > 0: if len(q) < W: return False for i in range(1, W): if q[i][0] != q[i - 1][0] + 1: return False j = 0 for i in range(W): if q[j][1] == 1: q.pop(j) else: q[j][1] -= 1 j += 1 n -= W return len(q) == 0 def isNStraightHand3(self, hand: List[int], W: int) -> bool: counter = Counter(hand) while counter: m = min(counter) for k in range(m, m + W): v = counter[k] if not v: return False if v == 1: del counter[k] else: counter[k] = v - 1 return True
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR VAR RETURN NUMBER VAR BIN_OP VAR VAR VAR RETURN NUMBER VAR FUNC_DEF VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER FUNC_CALL VAR VAR VAR FUNC_DEF VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR IF VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR NUMBER IF FUNC_CALL VAR VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER VAR NUMBER VAR VAR RETURN FUNC_CALL VAR VAR NUMBER VAR FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR IF VAR RETURN NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER RETURN NUMBER VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: if len(hand) % W != 0: return False hand_count = {} for x in hand: if x not in hand_count: hand_count[x] = 1 else: hand_count[x] += 1 while len(hand_count) > 0: min_key = min(hand_count.keys()) min_key_count = hand_count[min_key] for i in range(W): key = min_key + i if key not in hand_count: return False hand_count[key] -= min_key_count if hand_count[key] == 0: del hand_count[key] elif hand_count[key] < 0: return False return True
CLASS_DEF FUNC_DEF VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR RETURN NUMBER VAR VAR VAR IF VAR VAR NUMBER VAR VAR IF VAR VAR NUMBER RETURN NUMBER RETURN NUMBER VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: hand.sort() if len(hand) % W != 0: return False else: for i in range(len(hand) // W): tmp = hand.pop(0) for j in range(W - 1): if tmp + j + 1 in hand: hand.pop(hand.index(tmp + j + 1)) else: return False print(hand) if len(hand) == 0: return True else: return False
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN NUMBER VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution(object): def isNStraightHand(self, hand, W): if len(hand) % W != 0: return False count = collections.Counter(hand) while count: m = min(count.keys()) num = count[m] for k in range(m, m + W): v = count[k] if v < num: return False if v == num: del count[k] else: count[k] = v - num return True
CLASS_DEF VAR FUNC_DEF IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR RETURN NUMBER
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: if len(hand) % W != 0: return False if len(hand) == 0: return True hand = sorted(hand) seed = hand[0] for i in range(1, W): if seed + i not in hand: return False for i in range(0, W): hand.remove(seed + i) return self.isNStraightHand(hand, W)
CLASS_DEF FUNC_DEF VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand, W): if len(hand) % W != 0: return False hand = sorted(hand) cnt = {} for card in hand: if card not in cnt: cnt[card] = 0 cnt[card] += 1 cur_len = 0 last_val = -1 num_sub_hands = 0 unique_vals = sorted(set(hand)) flag = True loops = 0 while len(cnt) and flag and num_sub_hands < len(hand) // W: flag = False tmp = sorted(set(unique_vals)) for element in tmp: loops += 1 if last_val == -1 or element == last_val + 1: last_val = element cnt[element] -= 1 cur_len += 1 flag = True if cnt[element] == 0: del cnt[element] unique_vals.remove(element) if cur_len == W: last_val = -1 cur_len = 0 num_sub_hands += 1 break return len(cnt) == 0 and num_sub_hands == len(hand) // W
CLASS_DEF FUNC_DEF IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: count = Counter(hand) while count: init = min(count.keys()) if count[init] == 1: del count[init] else: count[init] = count[init] - 1 for i in range(1, W): v = count[init + 1] if init + 1 in count.keys(): if v == 1: del count[init + 1] else: count[init + 1] = v - 1 else: return False init = init + 1 return True
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN NUMBER VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand, W): nums = len(hand) if nums % W: return False elif W == 1: return True else: heapify(hand) arrange = [[] for _ in range(nums // W)] start = 0 while hand: h = heappop(hand) i = start while True: if len(arrange[i]) == W: i += 1 start = i if len(arrange[i]) == 0 or arrange[i] and arrange[i][-1] != h: arrange[i].append(h) elif arrange[i] and arrange[i][-1] == h: if i + 1 == len(arrange): return False i += 1 continue if len(arrange[i]) > 1 and arrange[i][-2] + 1 != arrange[i][-1]: return False else: break return True
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR RETURN NUMBER IF VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR NUMBER VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR RETURN NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER RETURN NUMBER RETURN NUMBER
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: if len(hand) % W != 0: return False dicts = {} for n in hand: dicts[n] = dicts.get(n, 0) + 1 while dicts: m = min(dicts) for k in range(m, m + W): if k not in dicts: return False elif dicts[k] == 1: del dicts[k] else: dicts[k] -= 1 return True
CLASS_DEF FUNC_DEF VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER RETURN NUMBER VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: count = collections.Counter(hand) dic = collections.OrderedDict(sorted(count.items())) while dic: m = next(iter(dic)) for k in range(m, m + W): v = dic.get(k) print(v, k) if not v: return False if v == 1: del dic[k] else: dic[k] = v - 1 return True
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR RETURN NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER RETURN NUMBER VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: if W == 1: return True sorted_indexes = [] frequency_map = {} for h in hand: if h not in list(frequency_map.keys()): frequency_map[h] = 1 self.addToSortedList(h, sorted_indexes) else: frequency_map[h] += 1 print((sorted_indexes, frequency_map)) index = 0 while index < len(sorted_indexes): for ele in range(sorted_indexes[index], sorted_indexes[index] + W): if ele not in list(frequency_map.keys()) or frequency_map[ele] == 0: return False frequency_map[ele] -= 1 while ( index < len(sorted_indexes) and frequency_map[sorted_indexes[index]] == 0 ): frequency_map.pop(sorted_indexes[index]) index += 1 return len(list(frequency_map.keys())) == 0 def addToSortedList(self, value: int, sorted_list: List[int]): if len(sorted_list) == 0: sorted_list.append(value) return low = 0 high = len(sorted_list) - 1 while low < high: mid = math.floor((low + high) / 2) if value < sorted_list[mid]: high = mid - 1 elif value > sorted_list[mid]: low = mid + 1 if high < 0: sorted_list.insert(0, value) elif value > sorted_list[high]: sorted_list.insert(high + 1, value) else: sorted_list.insert(high, value)
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR VAR IF VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR IF VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR FUNC_DEF VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: hand = sorted(hand) forward = list(range(1, len(hand))) forward.append(None) backward = [None] backward.extend(list(range(0, len(hand) - 1))) ptr = 0 while ptr is not None: firstSkipped = None for i in range(W): if ptr is None: return False currHand = hand[ptr] prev = backward[ptr] forw = forward[ptr] if prev is not None: forward[prev] = forw if forw is not None: backward[forw] = prev forward[ptr] = backward[ptr] = None ptr = forw while ptr is not None and hand[ptr] == currHand: if firstSkipped is None: firstSkipped = ptr ptr = forward[ptr] if ptr is not None and i != W - 1 and hand[ptr] - 1 != currHand: return False if firstSkipped is not None: ptr = firstSkipped return True
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NONE ASSIGN VAR LIST NONE EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NONE ASSIGN VAR NONE FOR VAR FUNC_CALL VAR VAR IF VAR NONE RETURN NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NONE ASSIGN VAR VAR VAR IF VAR NONE ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR NONE ASSIGN VAR VAR WHILE VAR NONE VAR VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR VAR IF VAR NONE VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER VAR RETURN NUMBER IF VAR NONE ASSIGN VAR VAR RETURN NUMBER VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: if len(hand) % W != 0: return False while hand != []: minele = min(hand) for i in range(0, W): try: hand.remove(minele + i) except: return False return True
CLASS_DEF FUNC_DEF VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER WHILE VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN NUMBER RETURN NUMBER VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: d = collections.defaultdict(int) for i in hand: d[i] += 1 while d: l = list(d) heapq.heapify(l) prev = None for i in range(W): if len(l) == 0: return False ele = heapq.heappop(l) if prev is not None: if ele - prev > 1: return False prev = ele if d[ele] == 1: del d[ele] else: d[ele] -= 1 return True
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NONE FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NONE IF BIN_OP VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER RETURN NUMBER VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: if len(hand) % W != 0: return False dic = defaultdict(int) for i in range(len(hand)): dic[hand[i]] += 1 for i in range(0, len(hand), W): start = min(dic.keys()) dic[start] -= 1 if dic[start] == 0: del dic[start] for i in range(1, W): start += 1 if start not in dic: return False dic[start] -= 1 if dic[start] == 0: del dic[start] return True
CLASS_DEF FUNC_DEF VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR NUMBER IF VAR VAR RETURN NUMBER VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR RETURN NUMBER VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, nums: List[int], k: int) -> bool: n = len(nums) if n % k != 0: return False cnt = Counter(nums) no = min(cnt.keys()) while cnt: for j in range(k): if cnt[no] == 0: return False cnt[no] -= 1 if cnt[no] == 0: cnt.pop(no) no += 1 if cnt: no = min(cnt.keys()) return True
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER RETURN NUMBER VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR RETURN NUMBER VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: if len(hand) % W: return False counter = Counter(hand) keys = set(counter.keys()) while counter: key = min(keys) while key in keys: for w in range(W): if key + w not in counter: return False counter[key + w] -= 1 if counter[key + w] == 0: del counter[key + w] keys.remove(key + w) return True
CLASS_DEF FUNC_DEF VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR RETURN NUMBER VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN NUMBER VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: if len(hand) % W != 0: return False hand.sort() flag = 0 while len(hand) > 0 and flag == 0: temp = [] start = hand[0] temp.append(start) for i in range(1, W): if start + i in hand: temp.append(start + i) else: flag = 1 break for x in temp: hand.remove(x) if flag == 0: return True else: return False
CLASS_DEF FUNC_DEF VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER RETURN NUMBER VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: if len(hand) % W != 0: return False hand.sort() while hand: start = hand[0] hand.remove(start) for i in range(W - 1): start += 1 if start in hand: hand.remove(start) else: return False return len(hand) == 0
CLASS_DEF FUNC_DEF VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, h, W): l = len(h) if l % W != 0: return False heapify(h) c = Counter(h) for _ in range(l // W): x = heappop(h) while c[x] == 0: x = heappop(h) for _ in range(W): if c[x] == 0: return False c[x] -= 1 x += 1 return True
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER RETURN NUMBER VAR VAR NUMBER VAR NUMBER RETURN NUMBER
Alice has a hand of cards, given as an array of integers. Now she wants to rearrange the cards into groups so that each group is size W, and consists of W consecutive cards. Return true if and only if she can.   Example 1: Input: hand = [1,2,3,6,2,3,4,7,8], W = 3 Output: true Explanation: Alice's hand can be rearranged as [1,2,3],[2,3,4],[6,7,8]. Example 2: Input: hand = [1,2,3,4,5], W = 4 Output: false Explanation: Alice's hand can't be rearranged into groups of 4.   Constraints: 1 <= hand.length <= 10000 0 <= hand[i] <= 10^9 1 <= W <= hand.length Note: This question is the same as 1296: https://leetcode.com/problems/divide-array-in-sets-of-k-consecutive-numbers/
class Solution: def isNStraightHand(self, hand: List[int], W: int) -> bool: q = deque() opened = 0 last = 0 counter = Counter(hand) for n in sorted(counter): count = counter[n] if n > last + 1 and opened > 0: return False if n == last + 1 and count < opened: return False q.append(count - opened) opened = count if len(q) == W: opened -= q.popleft() last = n return not opened
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR RETURN VAR VAR
Your country has an infinite number of lakes. Initially, all the lakes are empty, but when it rains over the nth lake, the nth lake becomes full of water. If it rains over a lake which is full of water, there will be a flood. Your goal is to avoid the flood in any lake. Given an integer array rains where: rains[i] > 0 means there will be rains over the rains[i] lake. rains[i] == 0 means there are no rains this day and you can choose one lake this day and dry it. Return an array ans where: ans.length == rains.length ans[i] == -1 if rains[i] > 0. ans[i] is the lake you choose to dry in the ith day if rains[i] == 0. If there are multiple valid answers return any of them. If it is impossible to avoid flood return an empty array. Notice that if you chose to dry a full lake, it becomes empty, but if you chose to dry an empty lake, nothing changes. (see example 4)   Example 1: Input: rains = [1,2,3,4] Output: [-1,-1,-1,-1] Explanation: After the first day full lakes are [1] After the second day full lakes are [1,2] After the third day full lakes are [1,2,3] After the fourth day full lakes are [1,2,3,4] There's no day to dry any lake and there is no flood in any lake. Example 2: Input: rains = [1,2,0,0,2,1] Output: [-1,-1,2,1,-1,-1] Explanation: After the first day full lakes are [1] After the second day full lakes are [1,2] After the third day, we dry lake 2. Full lakes are [1] After the fourth day, we dry lake 1. There is no full lakes. After the fifth day, full lakes are [2]. After the sixth day, full lakes are [1,2]. It is easy that this scenario is flood-free. [-1,-1,1,2,-1,-1] is another acceptable scenario. Example 3: Input: rains = [1,2,0,1,2] Output: [] Explanation: After the second day, full lakes are [1,2]. We have to dry one lake in the third day. After that, it will rain over lakes [1,2]. It's easy to prove that no matter which lake you choose to dry in the 3rd day, the other one will flood. Example 4: Input: rains = [69,0,0,0,69] Output: [-1,69,1,1,-1] Explanation: Any solution on one of the forms [-1,69,x,y,-1], [-1,x,69,y,-1] or [-1,x,y,69,-1] is acceptable where 1 <= x,y <= 10^9 Example 5: Input: rains = [10,20,20] Output: [] Explanation: It will rain over lake 20 two consecutive days. There is no chance to dry any lake.   Constraints: 1 <= rains.length <= 10^5 0 <= rains[i] <= 10^9
class Solution: def find_min_greater(self, a, x): start = 0 end = len(a) - 1 while start < end: mid = start + end mid = mid // 2 if a[mid] < x and a[mid + 1] > x: return mid + 1 elif a[mid] < x: start = mid else: end = mid return start def avoidFlood(self, rains: List[int]) -> List[int]: last_rained = {} no_rain = [] ans = [] for i in range(len(rains)): if rains[i] == 0: no_rain.append(i) ans.append(1) else: lake = rains[i] ans.append(-1) if lake not in last_rained.keys(): last_rained[lake] = i else: lr = last_rained[lake] zl = len(no_rain) if len(no_rain) == 0 or no_rain[zl - 1] < lr: return [] else: empty = self.find_min_greater(no_rain, lr) ans[no_rain[empty]] = lake last_rained[lake] = i no_rain.pop(empty) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF VAR VAR ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR RETURN LIST ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR
Your country has an infinite number of lakes. Initially, all the lakes are empty, but when it rains over the nth lake, the nth lake becomes full of water. If it rains over a lake which is full of water, there will be a flood. Your goal is to avoid the flood in any lake. Given an integer array rains where: rains[i] > 0 means there will be rains over the rains[i] lake. rains[i] == 0 means there are no rains this day and you can choose one lake this day and dry it. Return an array ans where: ans.length == rains.length ans[i] == -1 if rains[i] > 0. ans[i] is the lake you choose to dry in the ith day if rains[i] == 0. If there are multiple valid answers return any of them. If it is impossible to avoid flood return an empty array. Notice that if you chose to dry a full lake, it becomes empty, but if you chose to dry an empty lake, nothing changes. (see example 4)   Example 1: Input: rains = [1,2,3,4] Output: [-1,-1,-1,-1] Explanation: After the first day full lakes are [1] After the second day full lakes are [1,2] After the third day full lakes are [1,2,3] After the fourth day full lakes are [1,2,3,4] There's no day to dry any lake and there is no flood in any lake. Example 2: Input: rains = [1,2,0,0,2,1] Output: [-1,-1,2,1,-1,-1] Explanation: After the first day full lakes are [1] After the second day full lakes are [1,2] After the third day, we dry lake 2. Full lakes are [1] After the fourth day, we dry lake 1. There is no full lakes. After the fifth day, full lakes are [2]. After the sixth day, full lakes are [1,2]. It is easy that this scenario is flood-free. [-1,-1,1,2,-1,-1] is another acceptable scenario. Example 3: Input: rains = [1,2,0,1,2] Output: [] Explanation: After the second day, full lakes are [1,2]. We have to dry one lake in the third day. After that, it will rain over lakes [1,2]. It's easy to prove that no matter which lake you choose to dry in the 3rd day, the other one will flood. Example 4: Input: rains = [69,0,0,0,69] Output: [-1,69,1,1,-1] Explanation: Any solution on one of the forms [-1,69,x,y,-1], [-1,x,69,y,-1] or [-1,x,y,69,-1] is acceptable where 1 <= x,y <= 10^9 Example 5: Input: rains = [10,20,20] Output: [] Explanation: It will rain over lake 20 two consecutive days. There is no chance to dry any lake.   Constraints: 1 <= rains.length <= 10^5 0 <= rains[i] <= 10^9
class Solution: def avoidFlood(self, rains: List[int]) -> List[int]: closest = [] locations = collections.defaultdict(collections.deque) for index, lake in enumerate(rains): locations[lake].append(index) res = [] for index, lake in enumerate(rains): if closest and closest[0] == index: return [] if not lake: if not closest: res.append(1) continue nxt = heapq.heappop(closest) res.append(rains[nxt]) else: l = locations[lake] l.popleft() if l: nxt = l[0] heapq.heappush(closest, nxt) res.append(-1) return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR RETURN LIST IF VAR IF VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR IF VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN VAR VAR VAR
Your country has an infinite number of lakes. Initially, all the lakes are empty, but when it rains over the nth lake, the nth lake becomes full of water. If it rains over a lake which is full of water, there will be a flood. Your goal is to avoid the flood in any lake. Given an integer array rains where: rains[i] > 0 means there will be rains over the rains[i] lake. rains[i] == 0 means there are no rains this day and you can choose one lake this day and dry it. Return an array ans where: ans.length == rains.length ans[i] == -1 if rains[i] > 0. ans[i] is the lake you choose to dry in the ith day if rains[i] == 0. If there are multiple valid answers return any of them. If it is impossible to avoid flood return an empty array. Notice that if you chose to dry a full lake, it becomes empty, but if you chose to dry an empty lake, nothing changes. (see example 4)   Example 1: Input: rains = [1,2,3,4] Output: [-1,-1,-1,-1] Explanation: After the first day full lakes are [1] After the second day full lakes are [1,2] After the third day full lakes are [1,2,3] After the fourth day full lakes are [1,2,3,4] There's no day to dry any lake and there is no flood in any lake. Example 2: Input: rains = [1,2,0,0,2,1] Output: [-1,-1,2,1,-1,-1] Explanation: After the first day full lakes are [1] After the second day full lakes are [1,2] After the third day, we dry lake 2. Full lakes are [1] After the fourth day, we dry lake 1. There is no full lakes. After the fifth day, full lakes are [2]. After the sixth day, full lakes are [1,2]. It is easy that this scenario is flood-free. [-1,-1,1,2,-1,-1] is another acceptable scenario. Example 3: Input: rains = [1,2,0,1,2] Output: [] Explanation: After the second day, full lakes are [1,2]. We have to dry one lake in the third day. After that, it will rain over lakes [1,2]. It's easy to prove that no matter which lake you choose to dry in the 3rd day, the other one will flood. Example 4: Input: rains = [69,0,0,0,69] Output: [-1,69,1,1,-1] Explanation: Any solution on one of the forms [-1,69,x,y,-1], [-1,x,69,y,-1] or [-1,x,y,69,-1] is acceptable where 1 <= x,y <= 10^9 Example 5: Input: rains = [10,20,20] Output: [] Explanation: It will rain over lake 20 two consecutive days. There is no chance to dry any lake.   Constraints: 1 <= rains.length <= 10^5 0 <= rains[i] <= 10^9
class Solution: def avoidFlood(self, rains: List[int]) -> List[int]: return_arr = [(-1) for i in range(len(rains))] rains_dict = {} zeros_indices = [] for i in range(len(rains)): if rains[i] == 0: return_arr[i] = 1 zeros_indices.append(i) elif rains[i] not in rains_dict: rains_dict[rains[i]] = i else: if len(zeros_indices) == 0: return [] index = 0 while zeros_indices[index] < rains_dict[rains[i]]: index += 1 print(index) if index == len(zeros_indices): return [] return_arr[zeros_indices[index]] = rains[i] rains_dict[rains[i]] = i del zeros_indices[index] return return_arr
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN LIST ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR RETURN LIST ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR RETURN VAR VAR VAR
Your country has an infinite number of lakes. Initially, all the lakes are empty, but when it rains over the nth lake, the nth lake becomes full of water. If it rains over a lake which is full of water, there will be a flood. Your goal is to avoid the flood in any lake. Given an integer array rains where: rains[i] > 0 means there will be rains over the rains[i] lake. rains[i] == 0 means there are no rains this day and you can choose one lake this day and dry it. Return an array ans where: ans.length == rains.length ans[i] == -1 if rains[i] > 0. ans[i] is the lake you choose to dry in the ith day if rains[i] == 0. If there are multiple valid answers return any of them. If it is impossible to avoid flood return an empty array. Notice that if you chose to dry a full lake, it becomes empty, but if you chose to dry an empty lake, nothing changes. (see example 4)   Example 1: Input: rains = [1,2,3,4] Output: [-1,-1,-1,-1] Explanation: After the first day full lakes are [1] After the second day full lakes are [1,2] After the third day full lakes are [1,2,3] After the fourth day full lakes are [1,2,3,4] There's no day to dry any lake and there is no flood in any lake. Example 2: Input: rains = [1,2,0,0,2,1] Output: [-1,-1,2,1,-1,-1] Explanation: After the first day full lakes are [1] After the second day full lakes are [1,2] After the third day, we dry lake 2. Full lakes are [1] After the fourth day, we dry lake 1. There is no full lakes. After the fifth day, full lakes are [2]. After the sixth day, full lakes are [1,2]. It is easy that this scenario is flood-free. [-1,-1,1,2,-1,-1] is another acceptable scenario. Example 3: Input: rains = [1,2,0,1,2] Output: [] Explanation: After the second day, full lakes are [1,2]. We have to dry one lake in the third day. After that, it will rain over lakes [1,2]. It's easy to prove that no matter which lake you choose to dry in the 3rd day, the other one will flood. Example 4: Input: rains = [69,0,0,0,69] Output: [-1,69,1,1,-1] Explanation: Any solution on one of the forms [-1,69,x,y,-1], [-1,x,69,y,-1] or [-1,x,y,69,-1] is acceptable where 1 <= x,y <= 10^9 Example 5: Input: rains = [10,20,20] Output: [] Explanation: It will rain over lake 20 two consecutive days. There is no chance to dry any lake.   Constraints: 1 <= rains.length <= 10^5 0 <= rains[i] <= 10^9
class Solution: def avoidFlood(self, rains: List[int]) -> List[int]: q = [] ans = [] hashmap = {} for i in range(len(rains)): if rains[i] == 0: q.append(i) ans.append(1) else: if rains[i] in hashmap: if len(q) == 0: return [] else: index = hashmap[rains[i]] pos = bisect.bisect_right(q, index) if pos < len(q): ans[q[pos]] = rains[i] q.pop(pos) else: return [] hashmap[rains[i]] = i ans.append(-1) return ans
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN LIST ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN LIST ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN VAR VAR VAR
Your country has an infinite number of lakes. Initially, all the lakes are empty, but when it rains over the nth lake, the nth lake becomes full of water. If it rains over a lake which is full of water, there will be a flood. Your goal is to avoid the flood in any lake. Given an integer array rains where: rains[i] > 0 means there will be rains over the rains[i] lake. rains[i] == 0 means there are no rains this day and you can choose one lake this day and dry it. Return an array ans where: ans.length == rains.length ans[i] == -1 if rains[i] > 0. ans[i] is the lake you choose to dry in the ith day if rains[i] == 0. If there are multiple valid answers return any of them. If it is impossible to avoid flood return an empty array. Notice that if you chose to dry a full lake, it becomes empty, but if you chose to dry an empty lake, nothing changes. (see example 4)   Example 1: Input: rains = [1,2,3,4] Output: [-1,-1,-1,-1] Explanation: After the first day full lakes are [1] After the second day full lakes are [1,2] After the third day full lakes are [1,2,3] After the fourth day full lakes are [1,2,3,4] There's no day to dry any lake and there is no flood in any lake. Example 2: Input: rains = [1,2,0,0,2,1] Output: [-1,-1,2,1,-1,-1] Explanation: After the first day full lakes are [1] After the second day full lakes are [1,2] After the third day, we dry lake 2. Full lakes are [1] After the fourth day, we dry lake 1. There is no full lakes. After the fifth day, full lakes are [2]. After the sixth day, full lakes are [1,2]. It is easy that this scenario is flood-free. [-1,-1,1,2,-1,-1] is another acceptable scenario. Example 3: Input: rains = [1,2,0,1,2] Output: [] Explanation: After the second day, full lakes are [1,2]. We have to dry one lake in the third day. After that, it will rain over lakes [1,2]. It's easy to prove that no matter which lake you choose to dry in the 3rd day, the other one will flood. Example 4: Input: rains = [69,0,0,0,69] Output: [-1,69,1,1,-1] Explanation: Any solution on one of the forms [-1,69,x,y,-1], [-1,x,69,y,-1] or [-1,x,y,69,-1] is acceptable where 1 <= x,y <= 10^9 Example 5: Input: rains = [10,20,20] Output: [] Explanation: It will rain over lake 20 two consecutive days. There is no chance to dry any lake.   Constraints: 1 <= rains.length <= 10^5 0 <= rains[i] <= 10^9
class Solution: def avoidFlood(self, rains: List[int]) -> List[int]: floods, n, visited, water_heap = None, len(rains), defaultdict(int), [] for i in range(n - 1, -1, -1): if rains[i]: if rains[i] in visited: floods = i, visited[rains[i]], floods visited[rains[i]] = i for i in range(n): if not rains[i]: while floods and floods[0] < i: start, end, floods = floods heapq.heappush(water_heap, (end, start)) if not water_heap: rains[i] = 1 else: end, start = heappop(water_heap) if end < i: return [] rains[i] = rains[end] else: rains[i] = -1 if water_heap or floods: return [] return rains
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR VAR VAR NONE FUNC_CALL VAR VAR FUNC_CALL VAR VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR WHILE VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN LIST ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR RETURN LIST RETURN VAR VAR VAR
Your country has an infinite number of lakes. Initially, all the lakes are empty, but when it rains over the nth lake, the nth lake becomes full of water. If it rains over a lake which is full of water, there will be a flood. Your goal is to avoid the flood in any lake. Given an integer array rains where: rains[i] > 0 means there will be rains over the rains[i] lake. rains[i] == 0 means there are no rains this day and you can choose one lake this day and dry it. Return an array ans where: ans.length == rains.length ans[i] == -1 if rains[i] > 0. ans[i] is the lake you choose to dry in the ith day if rains[i] == 0. If there are multiple valid answers return any of them. If it is impossible to avoid flood return an empty array. Notice that if you chose to dry a full lake, it becomes empty, but if you chose to dry an empty lake, nothing changes. (see example 4)   Example 1: Input: rains = [1,2,3,4] Output: [-1,-1,-1,-1] Explanation: After the first day full lakes are [1] After the second day full lakes are [1,2] After the third day full lakes are [1,2,3] After the fourth day full lakes are [1,2,3,4] There's no day to dry any lake and there is no flood in any lake. Example 2: Input: rains = [1,2,0,0,2,1] Output: [-1,-1,2,1,-1,-1] Explanation: After the first day full lakes are [1] After the second day full lakes are [1,2] After the third day, we dry lake 2. Full lakes are [1] After the fourth day, we dry lake 1. There is no full lakes. After the fifth day, full lakes are [2]. After the sixth day, full lakes are [1,2]. It is easy that this scenario is flood-free. [-1,-1,1,2,-1,-1] is another acceptable scenario. Example 3: Input: rains = [1,2,0,1,2] Output: [] Explanation: After the second day, full lakes are [1,2]. We have to dry one lake in the third day. After that, it will rain over lakes [1,2]. It's easy to prove that no matter which lake you choose to dry in the 3rd day, the other one will flood. Example 4: Input: rains = [69,0,0,0,69] Output: [-1,69,1,1,-1] Explanation: Any solution on one of the forms [-1,69,x,y,-1], [-1,x,69,y,-1] or [-1,x,y,69,-1] is acceptable where 1 <= x,y <= 10^9 Example 5: Input: rains = [10,20,20] Output: [] Explanation: It will rain over lake 20 two consecutive days. There is no chance to dry any lake.   Constraints: 1 <= rains.length <= 10^5 0 <= rains[i] <= 10^9
class Solution: def avoidFlood(self, rains: List[int]) -> List[int]: nearest = [] locs = collections.defaultdict(collections.deque) res = [-1] * len(rains) for i, lake in enumerate(rains): locs[lake].append(i) for i, lake in enumerate(rains): if nearest and nearest[0] == i: return [] if lake != 0: locs[lake].popleft() if locs[lake]: nxt = locs[lake][0] heapq.heappush(nearest, nxt) elif not nearest: res[i] = 1 else: next_wet_day = heapq.heappop(nearest) wet_lake = rains[next_wet_day] res[i] = wet_lake return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR RETURN LIST IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR VAR
Your country has an infinite number of lakes. Initially, all the lakes are empty, but when it rains over the nth lake, the nth lake becomes full of water. If it rains over a lake which is full of water, there will be a flood. Your goal is to avoid the flood in any lake. Given an integer array rains where: rains[i] > 0 means there will be rains over the rains[i] lake. rains[i] == 0 means there are no rains this day and you can choose one lake this day and dry it. Return an array ans where: ans.length == rains.length ans[i] == -1 if rains[i] > 0. ans[i] is the lake you choose to dry in the ith day if rains[i] == 0. If there are multiple valid answers return any of them. If it is impossible to avoid flood return an empty array. Notice that if you chose to dry a full lake, it becomes empty, but if you chose to dry an empty lake, nothing changes. (see example 4)   Example 1: Input: rains = [1,2,3,4] Output: [-1,-1,-1,-1] Explanation: After the first day full lakes are [1] After the second day full lakes are [1,2] After the third day full lakes are [1,2,3] After the fourth day full lakes are [1,2,3,4] There's no day to dry any lake and there is no flood in any lake. Example 2: Input: rains = [1,2,0,0,2,1] Output: [-1,-1,2,1,-1,-1] Explanation: After the first day full lakes are [1] After the second day full lakes are [1,2] After the third day, we dry lake 2. Full lakes are [1] After the fourth day, we dry lake 1. There is no full lakes. After the fifth day, full lakes are [2]. After the sixth day, full lakes are [1,2]. It is easy that this scenario is flood-free. [-1,-1,1,2,-1,-1] is another acceptable scenario. Example 3: Input: rains = [1,2,0,1,2] Output: [] Explanation: After the second day, full lakes are [1,2]. We have to dry one lake in the third day. After that, it will rain over lakes [1,2]. It's easy to prove that no matter which lake you choose to dry in the 3rd day, the other one will flood. Example 4: Input: rains = [69,0,0,0,69] Output: [-1,69,1,1,-1] Explanation: Any solution on one of the forms [-1,69,x,y,-1], [-1,x,69,y,-1] or [-1,x,y,69,-1] is acceptable where 1 <= x,y <= 10^9 Example 5: Input: rains = [10,20,20] Output: [] Explanation: It will rain over lake 20 two consecutive days. There is no chance to dry any lake.   Constraints: 1 <= rains.length <= 10^5 0 <= rains[i] <= 10^9
class Solution: def backtrack(self, rains, full, position, seq): if position >= len(rains): return True if rains[position] > 0: if rains[position] in full: return False seq.append(-1) full.add(rains[position]) if self.backtrack(rains, full, position + 1, seq): return True full.remove(rains[position]) seq.pop() elif rains[position] == 0: for lake in full: seq.append(lake) full.remove(lake) if self.backtrack(rains, full, position + 1, seq): return True full.add(lake) seq.pop() if len(full) < 1: seq.append(1) if self.backtrack(rains, full, position + 1, seq): return True seq.pop() def avoidFloodBacktrack(self, rains: List[int]) -> List[int]: seq = [] full = set() if not self.backtrack(rains, full, 0, seq): return [] return seq def avoidFlood(self, rains: List[int]) -> List[int]: spares = [] recent = dict() full = set() ans = [] for i in range(len(rains)): if rains[i] > 0: ans.append(-1) if rains[i] in full: if len(spares) < 1: return [] valid = False for d in range(len(spares)): dry = spares[d] if dry > recent[rains[i]]: ans[dry] = rains[i] full.remove(rains[i]) del spares[d] valid = True break if not valid: return [] elif rains[i] in recent and recent[rains[i]] == i - 1: return [] else: full.add(rains[i]) recent[rains[i]] = i else: ans.append(1) spares.append(i) return ans if False: assert Solution().avoidFlood([69, 0, 0, 0, 69]) == [-1, 69, 1, 1, -1] assert Solution().avoidFlood([1, 2, 0, 0, 2, 1]) == [-1, -1, 2, 1, -1, -1] assert Solution().avoidFlood([1, 2, 0, 1, 2]) == [] assert Solution().avoidFlood([10, 20, 20]) == []
CLASS_DEF FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR NUMBER IF VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR IF VAR VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN NUMBER EXPR FUNC_CALL VAR FUNC_DEF VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR RETURN LIST RETURN VAR VAR VAR FUNC_DEF VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR RETURN LIST IF VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR IF NUMBER FUNC_CALL FUNC_CALL VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_CALL FUNC_CALL VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_CALL FUNC_CALL VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER LIST FUNC_CALL FUNC_CALL VAR LIST NUMBER NUMBER NUMBER LIST
Your country has an infinite number of lakes. Initially, all the lakes are empty, but when it rains over the nth lake, the nth lake becomes full of water. If it rains over a lake which is full of water, there will be a flood. Your goal is to avoid the flood in any lake. Given an integer array rains where: rains[i] > 0 means there will be rains over the rains[i] lake. rains[i] == 0 means there are no rains this day and you can choose one lake this day and dry it. Return an array ans where: ans.length == rains.length ans[i] == -1 if rains[i] > 0. ans[i] is the lake you choose to dry in the ith day if rains[i] == 0. If there are multiple valid answers return any of them. If it is impossible to avoid flood return an empty array. Notice that if you chose to dry a full lake, it becomes empty, but if you chose to dry an empty lake, nothing changes. (see example 4)   Example 1: Input: rains = [1,2,3,4] Output: [-1,-1,-1,-1] Explanation: After the first day full lakes are [1] After the second day full lakes are [1,2] After the third day full lakes are [1,2,3] After the fourth day full lakes are [1,2,3,4] There's no day to dry any lake and there is no flood in any lake. Example 2: Input: rains = [1,2,0,0,2,1] Output: [-1,-1,2,1,-1,-1] Explanation: After the first day full lakes are [1] After the second day full lakes are [1,2] After the third day, we dry lake 2. Full lakes are [1] After the fourth day, we dry lake 1. There is no full lakes. After the fifth day, full lakes are [2]. After the sixth day, full lakes are [1,2]. It is easy that this scenario is flood-free. [-1,-1,1,2,-1,-1] is another acceptable scenario. Example 3: Input: rains = [1,2,0,1,2] Output: [] Explanation: After the second day, full lakes are [1,2]. We have to dry one lake in the third day. After that, it will rain over lakes [1,2]. It's easy to prove that no matter which lake you choose to dry in the 3rd day, the other one will flood. Example 4: Input: rains = [69,0,0,0,69] Output: [-1,69,1,1,-1] Explanation: Any solution on one of the forms [-1,69,x,y,-1], [-1,x,69,y,-1] or [-1,x,y,69,-1] is acceptable where 1 <= x,y <= 10^9 Example 5: Input: rains = [10,20,20] Output: [] Explanation: It will rain over lake 20 two consecutive days. There is no chance to dry any lake.   Constraints: 1 <= rains.length <= 10^5 0 <= rains[i] <= 10^9
class Solution: def avoidFlood(self, rains: List[int]) -> List[int]: ans = [-1] * len(rains) lake_to_days = collections.defaultdict(list) full_lakes = set() to_empty = [] for day, lake in enumerate(rains): lake_to_days[lake].append(day) for day in range(len(rains)): lake = rains[day] if lake: if lake in full_lakes: return [] full_lakes.add(lake) lake_to_days[lake].pop(0) if lake_to_days[lake]: heapq.heappush(to_empty, lake_to_days[lake][0]) elif to_empty: ans[day] = rains[heapq.heappop(to_empty)] full_lakes.remove(ans[day]) else: ans[day] = 1 return ans
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR IF VAR VAR RETURN LIST EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER RETURN VAR VAR VAR
Your country has an infinite number of lakes. Initially, all the lakes are empty, but when it rains over the nth lake, the nth lake becomes full of water. If it rains over a lake which is full of water, there will be a flood. Your goal is to avoid the flood in any lake. Given an integer array rains where: rains[i] > 0 means there will be rains over the rains[i] lake. rains[i] == 0 means there are no rains this day and you can choose one lake this day and dry it. Return an array ans where: ans.length == rains.length ans[i] == -1 if rains[i] > 0. ans[i] is the lake you choose to dry in the ith day if rains[i] == 0. If there are multiple valid answers return any of them. If it is impossible to avoid flood return an empty array. Notice that if you chose to dry a full lake, it becomes empty, but if you chose to dry an empty lake, nothing changes. (see example 4)   Example 1: Input: rains = [1,2,3,4] Output: [-1,-1,-1,-1] Explanation: After the first day full lakes are [1] After the second day full lakes are [1,2] After the third day full lakes are [1,2,3] After the fourth day full lakes are [1,2,3,4] There's no day to dry any lake and there is no flood in any lake. Example 2: Input: rains = [1,2,0,0,2,1] Output: [-1,-1,2,1,-1,-1] Explanation: After the first day full lakes are [1] After the second day full lakes are [1,2] After the third day, we dry lake 2. Full lakes are [1] After the fourth day, we dry lake 1. There is no full lakes. After the fifth day, full lakes are [2]. After the sixth day, full lakes are [1,2]. It is easy that this scenario is flood-free. [-1,-1,1,2,-1,-1] is another acceptable scenario. Example 3: Input: rains = [1,2,0,1,2] Output: [] Explanation: After the second day, full lakes are [1,2]. We have to dry one lake in the third day. After that, it will rain over lakes [1,2]. It's easy to prove that no matter which lake you choose to dry in the 3rd day, the other one will flood. Example 4: Input: rains = [69,0,0,0,69] Output: [-1,69,1,1,-1] Explanation: Any solution on one of the forms [-1,69,x,y,-1], [-1,x,69,y,-1] or [-1,x,y,69,-1] is acceptable where 1 <= x,y <= 10^9 Example 5: Input: rains = [10,20,20] Output: [] Explanation: It will rain over lake 20 two consecutive days. There is no chance to dry any lake.   Constraints: 1 <= rains.length <= 10^5 0 <= rains[i] <= 10^9
class Solution: def avoidFlood(self, rains: List[int]) -> List[int]: sunny_day_idx = [] res = [-1] * len(rains) for day, lake in enumerate(rains): if lake == 0: sunny_day_idx.append(day) res[day] = 1 last_day_rains_over_lake = {} def binary_search(sunny_day_idx, prev): low, high = 0, len(sunny_day_idx) while low < high: mid = (low + high) // 2 if sunny_day_idx[mid] > prev: high = mid else: low = mid + 1 return ( low if low < len(sunny_day_idx) and sunny_day_idx[low] > prev else None ) print(binary_search([2, 3], 0)) for day, lake in enumerate(rains): if lake != 0 and lake not in last_day_rains_over_lake: last_day_rains_over_lake[lake] = day elif lake != 0 and lake in last_day_rains_over_lake: if not sunny_day_idx: return [] idx = binary_search(sunny_day_idx, last_day_rains_over_lake[lake]) if idx == None: return [] if sunny_day_idx[idx] > day: return [] res[sunny_day_idx[idx]] = lake last_day_rains_over_lake[lake] = day sunny_day_idx.pop(idx) return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR DICT FUNC_DEF ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NONE EXPR FUNC_CALL VAR FUNC_CALL VAR LIST NUMBER NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR VAR IF VAR RETURN LIST ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NONE RETURN LIST IF VAR VAR VAR RETURN LIST ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR
Your country has an infinite number of lakes. Initially, all the lakes are empty, but when it rains over the nth lake, the nth lake becomes full of water. If it rains over a lake which is full of water, there will be a flood. Your goal is to avoid the flood in any lake. Given an integer array rains where: rains[i] > 0 means there will be rains over the rains[i] lake. rains[i] == 0 means there are no rains this day and you can choose one lake this day and dry it. Return an array ans where: ans.length == rains.length ans[i] == -1 if rains[i] > 0. ans[i] is the lake you choose to dry in the ith day if rains[i] == 0. If there are multiple valid answers return any of them. If it is impossible to avoid flood return an empty array. Notice that if you chose to dry a full lake, it becomes empty, but if you chose to dry an empty lake, nothing changes. (see example 4)   Example 1: Input: rains = [1,2,3,4] Output: [-1,-1,-1,-1] Explanation: After the first day full lakes are [1] After the second day full lakes are [1,2] After the third day full lakes are [1,2,3] After the fourth day full lakes are [1,2,3,4] There's no day to dry any lake and there is no flood in any lake. Example 2: Input: rains = [1,2,0,0,2,1] Output: [-1,-1,2,1,-1,-1] Explanation: After the first day full lakes are [1] After the second day full lakes are [1,2] After the third day, we dry lake 2. Full lakes are [1] After the fourth day, we dry lake 1. There is no full lakes. After the fifth day, full lakes are [2]. After the sixth day, full lakes are [1,2]. It is easy that this scenario is flood-free. [-1,-1,1,2,-1,-1] is another acceptable scenario. Example 3: Input: rains = [1,2,0,1,2] Output: [] Explanation: After the second day, full lakes are [1,2]. We have to dry one lake in the third day. After that, it will rain over lakes [1,2]. It's easy to prove that no matter which lake you choose to dry in the 3rd day, the other one will flood. Example 4: Input: rains = [69,0,0,0,69] Output: [-1,69,1,1,-1] Explanation: Any solution on one of the forms [-1,69,x,y,-1], [-1,x,69,y,-1] or [-1,x,y,69,-1] is acceptable where 1 <= x,y <= 10^9 Example 5: Input: rains = [10,20,20] Output: [] Explanation: It will rain over lake 20 two consecutive days. There is no chance to dry any lake.   Constraints: 1 <= rains.length <= 10^5 0 <= rains[i] <= 10^9
class Solution: def avoidFlood(self, rains: List[int]) -> List[int]: res = [] for rain in rains: if rain > 0: res.append(-1) else: res.append(1) full_lakes = defaultdict(list) sunny_days = [] start = 0 end = len(rains) - 1 while rains[start] == 0: start += 1 while rains[end] == 0: end -= 1 def binarySearch(sunny_days, prev_rain, curr): if not sunny_days: return -1 low = 0 high = len(sunny_days) while low < high: mid = (low + high) // 2 if sunny_days[mid] <= prev_rain: low = mid + 1 else: high = mid if ( low >= len(sunny_days) or sunny_days[low] <= prev_rain or sunny_days[low] >= curr ): return -1 else: return low for i in range(start, end + 1): if rains[i] != 0: if rains[i] not in full_lakes or not full_lakes[rains[i]]: full_lakes[rains[i]].append(i) else: prev_rain = full_lakes[rains[i]][-1] if not sunny_days: return [] idx = binarySearch(sunny_days, prev_rain, i) if idx == -1: return [] else: res[sunny_days[idx]] = rains[i] full_lakes[rains[i]].pop() full_lakes[rains[i]].append(i) sunny_days.pop(idx) else: sunny_days.append(i) return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER RETURN VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR RETURN LIST ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER RETURN LIST ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR
Your country has an infinite number of lakes. Initially, all the lakes are empty, but when it rains over the nth lake, the nth lake becomes full of water. If it rains over a lake which is full of water, there will be a flood. Your goal is to avoid the flood in any lake. Given an integer array rains where: rains[i] > 0 means there will be rains over the rains[i] lake. rains[i] == 0 means there are no rains this day and you can choose one lake this day and dry it. Return an array ans where: ans.length == rains.length ans[i] == -1 if rains[i] > 0. ans[i] is the lake you choose to dry in the ith day if rains[i] == 0. If there are multiple valid answers return any of them. If it is impossible to avoid flood return an empty array. Notice that if you chose to dry a full lake, it becomes empty, but if you chose to dry an empty lake, nothing changes. (see example 4)   Example 1: Input: rains = [1,2,3,4] Output: [-1,-1,-1,-1] Explanation: After the first day full lakes are [1] After the second day full lakes are [1,2] After the third day full lakes are [1,2,3] After the fourth day full lakes are [1,2,3,4] There's no day to dry any lake and there is no flood in any lake. Example 2: Input: rains = [1,2,0,0,2,1] Output: [-1,-1,2,1,-1,-1] Explanation: After the first day full lakes are [1] After the second day full lakes are [1,2] After the third day, we dry lake 2. Full lakes are [1] After the fourth day, we dry lake 1. There is no full lakes. After the fifth day, full lakes are [2]. After the sixth day, full lakes are [1,2]. It is easy that this scenario is flood-free. [-1,-1,1,2,-1,-1] is another acceptable scenario. Example 3: Input: rains = [1,2,0,1,2] Output: [] Explanation: After the second day, full lakes are [1,2]. We have to dry one lake in the third day. After that, it will rain over lakes [1,2]. It's easy to prove that no matter which lake you choose to dry in the 3rd day, the other one will flood. Example 4: Input: rains = [69,0,0,0,69] Output: [-1,69,1,1,-1] Explanation: Any solution on one of the forms [-1,69,x,y,-1], [-1,x,69,y,-1] or [-1,x,y,69,-1] is acceptable where 1 <= x,y <= 10^9 Example 5: Input: rains = [10,20,20] Output: [] Explanation: It will rain over lake 20 two consecutive days. There is no chance to dry any lake.   Constraints: 1 <= rains.length <= 10^5 0 <= rains[i] <= 10^9
class Solution: def avoidFlood(self, rains: List[int]) -> List[int]: full = set() priority = [] toReturn = [] for i in rains: if i != 0: if i in full: priority.append(i) else: full.add(i) full = set() for i in rains: if i == 0: done = False for x in range(len(priority)): if priority[x] in full: a = priority.pop(x) toReturn.append(a) full.remove(a) done = True break if not done: toReturn.append(1) elif i in full: return [] else: full.add(i) toReturn.append(-1) return toReturn
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR RETURN LIST EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN VAR VAR VAR
Your country has an infinite number of lakes. Initially, all the lakes are empty, but when it rains over the nth lake, the nth lake becomes full of water. If it rains over a lake which is full of water, there will be a flood. Your goal is to avoid the flood in any lake. Given an integer array rains where: rains[i] > 0 means there will be rains over the rains[i] lake. rains[i] == 0 means there are no rains this day and you can choose one lake this day and dry it. Return an array ans where: ans.length == rains.length ans[i] == -1 if rains[i] > 0. ans[i] is the lake you choose to dry in the ith day if rains[i] == 0. If there are multiple valid answers return any of them. If it is impossible to avoid flood return an empty array. Notice that if you chose to dry a full lake, it becomes empty, but if you chose to dry an empty lake, nothing changes. (see example 4)   Example 1: Input: rains = [1,2,3,4] Output: [-1,-1,-1,-1] Explanation: After the first day full lakes are [1] After the second day full lakes are [1,2] After the third day full lakes are [1,2,3] After the fourth day full lakes are [1,2,3,4] There's no day to dry any lake and there is no flood in any lake. Example 2: Input: rains = [1,2,0,0,2,1] Output: [-1,-1,2,1,-1,-1] Explanation: After the first day full lakes are [1] After the second day full lakes are [1,2] After the third day, we dry lake 2. Full lakes are [1] After the fourth day, we dry lake 1. There is no full lakes. After the fifth day, full lakes are [2]. After the sixth day, full lakes are [1,2]. It is easy that this scenario is flood-free. [-1,-1,1,2,-1,-1] is another acceptable scenario. Example 3: Input: rains = [1,2,0,1,2] Output: [] Explanation: After the second day, full lakes are [1,2]. We have to dry one lake in the third day. After that, it will rain over lakes [1,2]. It's easy to prove that no matter which lake you choose to dry in the 3rd day, the other one will flood. Example 4: Input: rains = [69,0,0,0,69] Output: [-1,69,1,1,-1] Explanation: Any solution on one of the forms [-1,69,x,y,-1], [-1,x,69,y,-1] or [-1,x,y,69,-1] is acceptable where 1 <= x,y <= 10^9 Example 5: Input: rains = [10,20,20] Output: [] Explanation: It will rain over lake 20 two consecutive days. There is no chance to dry any lake.   Constraints: 1 <= rains.length <= 10^5 0 <= rains[i] <= 10^9
class Solution: def avoidFlood(self, rains): filled, dry_days = {}, [] ans = [1] * len(rains) for day, lake in enumerate(rains): if not lake: dry_days.append(day) continue ans[day] = -1 if lake in filled: if not dry_days: return [] dry_on_day_index = bisect.bisect_left(dry_days, filled[lake]) if dry_on_day_index >= len(dry_days): return [] dry_on_day = dry_days.pop(dry_on_day_index) ans[dry_on_day] = lake filled[lake] = day return ans
CLASS_DEF FUNC_DEF ASSIGN VAR VAR DICT LIST ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR IF VAR RETURN LIST ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR RETURN LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR
Your country has an infinite number of lakes. Initially, all the lakes are empty, but when it rains over the nth lake, the nth lake becomes full of water. If it rains over a lake which is full of water, there will be a flood. Your goal is to avoid the flood in any lake. Given an integer array rains where: rains[i] > 0 means there will be rains over the rains[i] lake. rains[i] == 0 means there are no rains this day and you can choose one lake this day and dry it. Return an array ans where: ans.length == rains.length ans[i] == -1 if rains[i] > 0. ans[i] is the lake you choose to dry in the ith day if rains[i] == 0. If there are multiple valid answers return any of them. If it is impossible to avoid flood return an empty array. Notice that if you chose to dry a full lake, it becomes empty, but if you chose to dry an empty lake, nothing changes. (see example 4)   Example 1: Input: rains = [1,2,3,4] Output: [-1,-1,-1,-1] Explanation: After the first day full lakes are [1] After the second day full lakes are [1,2] After the third day full lakes are [1,2,3] After the fourth day full lakes are [1,2,3,4] There's no day to dry any lake and there is no flood in any lake. Example 2: Input: rains = [1,2,0,0,2,1] Output: [-1,-1,2,1,-1,-1] Explanation: After the first day full lakes are [1] After the second day full lakes are [1,2] After the third day, we dry lake 2. Full lakes are [1] After the fourth day, we dry lake 1. There is no full lakes. After the fifth day, full lakes are [2]. After the sixth day, full lakes are [1,2]. It is easy that this scenario is flood-free. [-1,-1,1,2,-1,-1] is another acceptable scenario. Example 3: Input: rains = [1,2,0,1,2] Output: [] Explanation: After the second day, full lakes are [1,2]. We have to dry one lake in the third day. After that, it will rain over lakes [1,2]. It's easy to prove that no matter which lake you choose to dry in the 3rd day, the other one will flood. Example 4: Input: rains = [69,0,0,0,69] Output: [-1,69,1,1,-1] Explanation: Any solution on one of the forms [-1,69,x,y,-1], [-1,x,69,y,-1] or [-1,x,y,69,-1] is acceptable where 1 <= x,y <= 10^9 Example 5: Input: rains = [10,20,20] Output: [] Explanation: It will rain over lake 20 two consecutive days. There is no chance to dry any lake.   Constraints: 1 <= rains.length <= 10^5 0 <= rains[i] <= 10^9
class Solution: def avoidFlood(self, rains: List[int]) -> List[int]: lakes = {} dries = [] res = [-1] * len(rains) for i, rain in enumerate(rains): if rain == 0: if lakes: dries.append(i) else: res[i] = 1 elif rain in lakes: if len(dries) == 0: return [] idx = bisect_right(dries, lakes[rain]) if idx == len(dries): return [] res[dries.pop(idx)] = rain lakes[rain] = i else: lakes[rain] = i for d in dries: if lakes: res[d] = lakes.popitem()[0] else: res[d] = 1 return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN LIST ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR RETURN LIST ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR IF VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER RETURN VAR VAR VAR
Your country has an infinite number of lakes. Initially, all the lakes are empty, but when it rains over the nth lake, the nth lake becomes full of water. If it rains over a lake which is full of water, there will be a flood. Your goal is to avoid the flood in any lake. Given an integer array rains where: rains[i] > 0 means there will be rains over the rains[i] lake. rains[i] == 0 means there are no rains this day and you can choose one lake this day and dry it. Return an array ans where: ans.length == rains.length ans[i] == -1 if rains[i] > 0. ans[i] is the lake you choose to dry in the ith day if rains[i] == 0. If there are multiple valid answers return any of them. If it is impossible to avoid flood return an empty array. Notice that if you chose to dry a full lake, it becomes empty, but if you chose to dry an empty lake, nothing changes. (see example 4)   Example 1: Input: rains = [1,2,3,4] Output: [-1,-1,-1,-1] Explanation: After the first day full lakes are [1] After the second day full lakes are [1,2] After the third day full lakes are [1,2,3] After the fourth day full lakes are [1,2,3,4] There's no day to dry any lake and there is no flood in any lake. Example 2: Input: rains = [1,2,0,0,2,1] Output: [-1,-1,2,1,-1,-1] Explanation: After the first day full lakes are [1] After the second day full lakes are [1,2] After the third day, we dry lake 2. Full lakes are [1] After the fourth day, we dry lake 1. There is no full lakes. After the fifth day, full lakes are [2]. After the sixth day, full lakes are [1,2]. It is easy that this scenario is flood-free. [-1,-1,1,2,-1,-1] is another acceptable scenario. Example 3: Input: rains = [1,2,0,1,2] Output: [] Explanation: After the second day, full lakes are [1,2]. We have to dry one lake in the third day. After that, it will rain over lakes [1,2]. It's easy to prove that no matter which lake you choose to dry in the 3rd day, the other one will flood. Example 4: Input: rains = [69,0,0,0,69] Output: [-1,69,1,1,-1] Explanation: Any solution on one of the forms [-1,69,x,y,-1], [-1,x,69,y,-1] or [-1,x,y,69,-1] is acceptable where 1 <= x,y <= 10^9 Example 5: Input: rains = [10,20,20] Output: [] Explanation: It will rain over lake 20 two consecutive days. There is no chance to dry any lake.   Constraints: 1 <= rains.length <= 10^5 0 <= rains[i] <= 10^9
class Solution: def avoidFlood(self, rains: List[int]) -> List[int]: n = len(rains) M = max(rains) ans = [-1] * n l = set() r = collections.defaultdict(list) for j in range(n): if rains[j] > 0: r[rains[j]] += [j] i = 0 tbd = [] while i < n: if rains[i] > 0: if rains[i] in l: return [] else: l.add(rains[i]) r[rains[i]].pop(0) if len(r[rains[i]]) > 0: heapq.heappush(tbd, r[rains[i]][0]) elif rains[i] == 0: if len(tbd) > 0: get = heapq.heappop(tbd) ans[i] = rains[get] l.remove(rains[get]) else: ans[i] = M + 1 i += 1 return ans
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR LIST VAR ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR IF VAR VAR NUMBER IF VAR VAR VAR RETURN LIST EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR VAR VAR
Your country has an infinite number of lakes. Initially, all the lakes are empty, but when it rains over the nth lake, the nth lake becomes full of water. If it rains over a lake which is full of water, there will be a flood. Your goal is to avoid the flood in any lake. Given an integer array rains where: rains[i] > 0 means there will be rains over the rains[i] lake. rains[i] == 0 means there are no rains this day and you can choose one lake this day and dry it. Return an array ans where: ans.length == rains.length ans[i] == -1 if rains[i] > 0. ans[i] is the lake you choose to dry in the ith day if rains[i] == 0. If there are multiple valid answers return any of them. If it is impossible to avoid flood return an empty array. Notice that if you chose to dry a full lake, it becomes empty, but if you chose to dry an empty lake, nothing changes. (see example 4)   Example 1: Input: rains = [1,2,3,4] Output: [-1,-1,-1,-1] Explanation: After the first day full lakes are [1] After the second day full lakes are [1,2] After the third day full lakes are [1,2,3] After the fourth day full lakes are [1,2,3,4] There's no day to dry any lake and there is no flood in any lake. Example 2: Input: rains = [1,2,0,0,2,1] Output: [-1,-1,2,1,-1,-1] Explanation: After the first day full lakes are [1] After the second day full lakes are [1,2] After the third day, we dry lake 2. Full lakes are [1] After the fourth day, we dry lake 1. There is no full lakes. After the fifth day, full lakes are [2]. After the sixth day, full lakes are [1,2]. It is easy that this scenario is flood-free. [-1,-1,1,2,-1,-1] is another acceptable scenario. Example 3: Input: rains = [1,2,0,1,2] Output: [] Explanation: After the second day, full lakes are [1,2]. We have to dry one lake in the third day. After that, it will rain over lakes [1,2]. It's easy to prove that no matter which lake you choose to dry in the 3rd day, the other one will flood. Example 4: Input: rains = [69,0,0,0,69] Output: [-1,69,1,1,-1] Explanation: Any solution on one of the forms [-1,69,x,y,-1], [-1,x,69,y,-1] or [-1,x,y,69,-1] is acceptable where 1 <= x,y <= 10^9 Example 5: Input: rains = [10,20,20] Output: [] Explanation: It will rain over lake 20 two consecutive days. There is no chance to dry any lake.   Constraints: 1 <= rains.length <= 10^5 0 <= rains[i] <= 10^9
class Solution: def avoidFlood(self, rains: List[int]) -> List[int]: queue = [] lakes = dict() days = [] for i in range(len(rains)): if rains[i] == 0: queue.append(i) elif rains[i] in lakes: if not queue: return [] for queue_index in range(len(queue)): if queue[queue_index] < lakes[rains[i]]: if queue_index == len(queue) - 1: return [] continue else: days[queue.pop(queue_index)] = rains[i] break lakes[rains[i]] = i else: lakes[rains[i]] = i days.append(-1 if rains[i] != 0 else 999) return days
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR RETURN LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN LIST ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER RETURN VAR VAR VAR
Your country has an infinite number of lakes. Initially, all the lakes are empty, but when it rains over the nth lake, the nth lake becomes full of water. If it rains over a lake which is full of water, there will be a flood. Your goal is to avoid the flood in any lake. Given an integer array rains where: rains[i] > 0 means there will be rains over the rains[i] lake. rains[i] == 0 means there are no rains this day and you can choose one lake this day and dry it. Return an array ans where: ans.length == rains.length ans[i] == -1 if rains[i] > 0. ans[i] is the lake you choose to dry in the ith day if rains[i] == 0. If there are multiple valid answers return any of them. If it is impossible to avoid flood return an empty array. Notice that if you chose to dry a full lake, it becomes empty, but if you chose to dry an empty lake, nothing changes. (see example 4)   Example 1: Input: rains = [1,2,3,4] Output: [-1,-1,-1,-1] Explanation: After the first day full lakes are [1] After the second day full lakes are [1,2] After the third day full lakes are [1,2,3] After the fourth day full lakes are [1,2,3,4] There's no day to dry any lake and there is no flood in any lake. Example 2: Input: rains = [1,2,0,0,2,1] Output: [-1,-1,2,1,-1,-1] Explanation: After the first day full lakes are [1] After the second day full lakes are [1,2] After the third day, we dry lake 2. Full lakes are [1] After the fourth day, we dry lake 1. There is no full lakes. After the fifth day, full lakes are [2]. After the sixth day, full lakes are [1,2]. It is easy that this scenario is flood-free. [-1,-1,1,2,-1,-1] is another acceptable scenario. Example 3: Input: rains = [1,2,0,1,2] Output: [] Explanation: After the second day, full lakes are [1,2]. We have to dry one lake in the third day. After that, it will rain over lakes [1,2]. It's easy to prove that no matter which lake you choose to dry in the 3rd day, the other one will flood. Example 4: Input: rains = [69,0,0,0,69] Output: [-1,69,1,1,-1] Explanation: Any solution on one of the forms [-1,69,x,y,-1], [-1,x,69,y,-1] or [-1,x,y,69,-1] is acceptable where 1 <= x,y <= 10^9 Example 5: Input: rains = [10,20,20] Output: [] Explanation: It will rain over lake 20 two consecutive days. There is no chance to dry any lake.   Constraints: 1 <= rains.length <= 10^5 0 <= rains[i] <= 10^9
class Solution: def avoidFlood(self, rains: List[int]) -> List[int]: ans = [] lakes = {} freeDays = [] for i in range(0, len(rains)): if rains[i] != 0: if rains[i] not in lakes: lakes[rains[i]] = [] lakes[rains[i]].append(0) lakes[rains[i]].append(i) elif lakes[rains[i]][0] == 1 and len(freeDays) > 0: for j in range(0, len(freeDays)): if freeDays[j] > lakes[rains[i]][1]: ans[freeDays[j]] = rains[i] lakes[rains[i]][0] -= 1 lakes[rains[i]][1] = i freeDays.pop(j) break lakes[rains[i]][0] += 1 lakes[rains[i]][1] = i if lakes[rains[i]][0] > 1: return [] ans.append(-1) else: freeDays.append(i) ans.append(1) return ans
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR IF VAR VAR VAR NUMBER NUMBER RETURN LIST EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN VAR VAR VAR
Your country has an infinite number of lakes. Initially, all the lakes are empty, but when it rains over the nth lake, the nth lake becomes full of water. If it rains over a lake which is full of water, there will be a flood. Your goal is to avoid the flood in any lake. Given an integer array rains where: rains[i] > 0 means there will be rains over the rains[i] lake. rains[i] == 0 means there are no rains this day and you can choose one lake this day and dry it. Return an array ans where: ans.length == rains.length ans[i] == -1 if rains[i] > 0. ans[i] is the lake you choose to dry in the ith day if rains[i] == 0. If there are multiple valid answers return any of them. If it is impossible to avoid flood return an empty array. Notice that if you chose to dry a full lake, it becomes empty, but if you chose to dry an empty lake, nothing changes. (see example 4)   Example 1: Input: rains = [1,2,3,4] Output: [-1,-1,-1,-1] Explanation: After the first day full lakes are [1] After the second day full lakes are [1,2] After the third day full lakes are [1,2,3] After the fourth day full lakes are [1,2,3,4] There's no day to dry any lake and there is no flood in any lake. Example 2: Input: rains = [1,2,0,0,2,1] Output: [-1,-1,2,1,-1,-1] Explanation: After the first day full lakes are [1] After the second day full lakes are [1,2] After the third day, we dry lake 2. Full lakes are [1] After the fourth day, we dry lake 1. There is no full lakes. After the fifth day, full lakes are [2]. After the sixth day, full lakes are [1,2]. It is easy that this scenario is flood-free. [-1,-1,1,2,-1,-1] is another acceptable scenario. Example 3: Input: rains = [1,2,0,1,2] Output: [] Explanation: After the second day, full lakes are [1,2]. We have to dry one lake in the third day. After that, it will rain over lakes [1,2]. It's easy to prove that no matter which lake you choose to dry in the 3rd day, the other one will flood. Example 4: Input: rains = [69,0,0,0,69] Output: [-1,69,1,1,-1] Explanation: Any solution on one of the forms [-1,69,x,y,-1], [-1,x,69,y,-1] or [-1,x,y,69,-1] is acceptable where 1 <= x,y <= 10^9 Example 5: Input: rains = [10,20,20] Output: [] Explanation: It will rain over lake 20 two consecutive days. There is no chance to dry any lake.   Constraints: 1 <= rains.length <= 10^5 0 <= rains[i] <= 10^9
class Solution: def avoidFlood(self, rains: List[int]) -> List[int]: n = len(rains) idx = defaultdict(list) for i in range(n): if rains[i] > 0: idx[rains[i]].append(i) nex = defaultdict(lambda: -1) for k in idx.keys(): for i in range(len(idx[k]) - 1): nex[idx[k][i]] = idx[k][i + 1] cnt = defaultdict(int) pq = [] ans = [] for i in range(n): if rains[i] > 0: if cnt[rains[i]] == 1: return [] cnt[rains[i]] = 1 if nex[i] != -1: heappush(pq, (nex[i], rains[i])) ans.append(-1) elif len(pq) == 0: ans.append(1) else: _, lake = heappop(pq) cnt[lake] -= 1 ans.append(lake) return ans
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR VAR VAR NUMBER RETURN LIST ASSIGN VAR VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR
Your country has an infinite number of lakes. Initially, all the lakes are empty, but when it rains over the nth lake, the nth lake becomes full of water. If it rains over a lake which is full of water, there will be a flood. Your goal is to avoid the flood in any lake. Given an integer array rains where: rains[i] > 0 means there will be rains over the rains[i] lake. rains[i] == 0 means there are no rains this day and you can choose one lake this day and dry it. Return an array ans where: ans.length == rains.length ans[i] == -1 if rains[i] > 0. ans[i] is the lake you choose to dry in the ith day if rains[i] == 0. If there are multiple valid answers return any of them. If it is impossible to avoid flood return an empty array. Notice that if you chose to dry a full lake, it becomes empty, but if you chose to dry an empty lake, nothing changes. (see example 4)   Example 1: Input: rains = [1,2,3,4] Output: [-1,-1,-1,-1] Explanation: After the first day full lakes are [1] After the second day full lakes are [1,2] After the third day full lakes are [1,2,3] After the fourth day full lakes are [1,2,3,4] There's no day to dry any lake and there is no flood in any lake. Example 2: Input: rains = [1,2,0,0,2,1] Output: [-1,-1,2,1,-1,-1] Explanation: After the first day full lakes are [1] After the second day full lakes are [1,2] After the third day, we dry lake 2. Full lakes are [1] After the fourth day, we dry lake 1. There is no full lakes. After the fifth day, full lakes are [2]. After the sixth day, full lakes are [1,2]. It is easy that this scenario is flood-free. [-1,-1,1,2,-1,-1] is another acceptable scenario. Example 3: Input: rains = [1,2,0,1,2] Output: [] Explanation: After the second day, full lakes are [1,2]. We have to dry one lake in the third day. After that, it will rain over lakes [1,2]. It's easy to prove that no matter which lake you choose to dry in the 3rd day, the other one will flood. Example 4: Input: rains = [69,0,0,0,69] Output: [-1,69,1,1,-1] Explanation: Any solution on one of the forms [-1,69,x,y,-1], [-1,x,69,y,-1] or [-1,x,y,69,-1] is acceptable where 1 <= x,y <= 10^9 Example 5: Input: rains = [10,20,20] Output: [] Explanation: It will rain over lake 20 two consecutive days. There is no chance to dry any lake.   Constraints: 1 <= rains.length <= 10^5 0 <= rains[i] <= 10^9
class Solution: def avoidFlood(self, rains: List[int]) -> List[int]: sol = [(53456) for _ in range(len(rains))] pos = collections.defaultdict(list) for idx, n in enumerate(rains): if n > 0: pos[n].append(idx) for key in pos: pos[key].reverse() q = [] used = set() for idx, n in enumerate(rains): if n > 0: if n in used: return [] else: pos[n].pop() if pos[n]: heapq.heappush(q, (pos[n][-1], n)) else: heapq.heappush(q, (math.inf, n)) used.add(n) sol[idx] = -1 elif n == 0: if q: _, val = heapq.heappop(q) sol[idx] = val used.remove(val) return sol
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR RETURN LIST EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER IF VAR ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR
Your country has an infinite number of lakes. Initially, all the lakes are empty, but when it rains over the nth lake, the nth lake becomes full of water. If it rains over a lake which is full of water, there will be a flood. Your goal is to avoid the flood in any lake. Given an integer array rains where: rains[i] > 0 means there will be rains over the rains[i] lake. rains[i] == 0 means there are no rains this day and you can choose one lake this day and dry it. Return an array ans where: ans.length == rains.length ans[i] == -1 if rains[i] > 0. ans[i] is the lake you choose to dry in the ith day if rains[i] == 0. If there are multiple valid answers return any of them. If it is impossible to avoid flood return an empty array. Notice that if you chose to dry a full lake, it becomes empty, but if you chose to dry an empty lake, nothing changes. (see example 4)   Example 1: Input: rains = [1,2,3,4] Output: [-1,-1,-1,-1] Explanation: After the first day full lakes are [1] After the second day full lakes are [1,2] After the third day full lakes are [1,2,3] After the fourth day full lakes are [1,2,3,4] There's no day to dry any lake and there is no flood in any lake. Example 2: Input: rains = [1,2,0,0,2,1] Output: [-1,-1,2,1,-1,-1] Explanation: After the first day full lakes are [1] After the second day full lakes are [1,2] After the third day, we dry lake 2. Full lakes are [1] After the fourth day, we dry lake 1. There is no full lakes. After the fifth day, full lakes are [2]. After the sixth day, full lakes are [1,2]. It is easy that this scenario is flood-free. [-1,-1,1,2,-1,-1] is another acceptable scenario. Example 3: Input: rains = [1,2,0,1,2] Output: [] Explanation: After the second day, full lakes are [1,2]. We have to dry one lake in the third day. After that, it will rain over lakes [1,2]. It's easy to prove that no matter which lake you choose to dry in the 3rd day, the other one will flood. Example 4: Input: rains = [69,0,0,0,69] Output: [-1,69,1,1,-1] Explanation: Any solution on one of the forms [-1,69,x,y,-1], [-1,x,69,y,-1] or [-1,x,y,69,-1] is acceptable where 1 <= x,y <= 10^9 Example 5: Input: rains = [10,20,20] Output: [] Explanation: It will rain over lake 20 two consecutive days. There is no chance to dry any lake.   Constraints: 1 <= rains.length <= 10^5 0 <= rains[i] <= 10^9
def avoid(rains): zeros = [] full = {} sol = [-1] * len(rains) for ix, r in enumerate(rains): if r == 0: zeros.append(ix) elif r not in full: full[r] = ix else: if not zeros: return [] zix = None rix = full[r] for i in range(len(zeros)): if zeros[i] > rix: zix = zeros.pop(i) break if not zix: return [] sol[zix] = r full[r] = ix while zeros: sol[zeros.pop()] = 1 return sol class Solution: def avoidFlood(self, rains: List[int]) -> List[int]: return avoid(rains)
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR IF VAR RETURN LIST ASSIGN VAR NONE ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR RETURN LIST ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER RETURN VAR CLASS_DEF FUNC_DEF VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR
Your country has an infinite number of lakes. Initially, all the lakes are empty, but when it rains over the nth lake, the nth lake becomes full of water. If it rains over a lake which is full of water, there will be a flood. Your goal is to avoid the flood in any lake. Given an integer array rains where: rains[i] > 0 means there will be rains over the rains[i] lake. rains[i] == 0 means there are no rains this day and you can choose one lake this day and dry it. Return an array ans where: ans.length == rains.length ans[i] == -1 if rains[i] > 0. ans[i] is the lake you choose to dry in the ith day if rains[i] == 0. If there are multiple valid answers return any of them. If it is impossible to avoid flood return an empty array. Notice that if you chose to dry a full lake, it becomes empty, but if you chose to dry an empty lake, nothing changes. (see example 4)   Example 1: Input: rains = [1,2,3,4] Output: [-1,-1,-1,-1] Explanation: After the first day full lakes are [1] After the second day full lakes are [1,2] After the third day full lakes are [1,2,3] After the fourth day full lakes are [1,2,3,4] There's no day to dry any lake and there is no flood in any lake. Example 2: Input: rains = [1,2,0,0,2,1] Output: [-1,-1,2,1,-1,-1] Explanation: After the first day full lakes are [1] After the second day full lakes are [1,2] After the third day, we dry lake 2. Full lakes are [1] After the fourth day, we dry lake 1. There is no full lakes. After the fifth day, full lakes are [2]. After the sixth day, full lakes are [1,2]. It is easy that this scenario is flood-free. [-1,-1,1,2,-1,-1] is another acceptable scenario. Example 3: Input: rains = [1,2,0,1,2] Output: [] Explanation: After the second day, full lakes are [1,2]. We have to dry one lake in the third day. After that, it will rain over lakes [1,2]. It's easy to prove that no matter which lake you choose to dry in the 3rd day, the other one will flood. Example 4: Input: rains = [69,0,0,0,69] Output: [-1,69,1,1,-1] Explanation: Any solution on one of the forms [-1,69,x,y,-1], [-1,x,69,y,-1] or [-1,x,y,69,-1] is acceptable where 1 <= x,y <= 10^9 Example 5: Input: rains = [10,20,20] Output: [] Explanation: It will rain over lake 20 two consecutive days. There is no chance to dry any lake.   Constraints: 1 <= rains.length <= 10^5 0 <= rains[i] <= 10^9
class Solution: def avoidFlood(self, rains: List[int]) -> List[int]: res = [] free_day = [] filled = {} for day, city in enumerate(rains): if city: res.append(-1) if city not in filled: filled[city] = day elif free_day and free_day[-1] > filled[city]: dry_day = bisect.bisect_left(free_day, filled[city]) res[free_day.pop(dry_day)] = city filled[city] = day else: return [] else: res.append(1) free_day.append(day) return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR IF VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR RETURN LIST EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR
Your country has an infinite number of lakes. Initially, all the lakes are empty, but when it rains over the nth lake, the nth lake becomes full of water. If it rains over a lake which is full of water, there will be a flood. Your goal is to avoid the flood in any lake. Given an integer array rains where: rains[i] > 0 means there will be rains over the rains[i] lake. rains[i] == 0 means there are no rains this day and you can choose one lake this day and dry it. Return an array ans where: ans.length == rains.length ans[i] == -1 if rains[i] > 0. ans[i] is the lake you choose to dry in the ith day if rains[i] == 0. If there are multiple valid answers return any of them. If it is impossible to avoid flood return an empty array. Notice that if you chose to dry a full lake, it becomes empty, but if you chose to dry an empty lake, nothing changes. (see example 4)   Example 1: Input: rains = [1,2,3,4] Output: [-1,-1,-1,-1] Explanation: After the first day full lakes are [1] After the second day full lakes are [1,2] After the third day full lakes are [1,2,3] After the fourth day full lakes are [1,2,3,4] There's no day to dry any lake and there is no flood in any lake. Example 2: Input: rains = [1,2,0,0,2,1] Output: [-1,-1,2,1,-1,-1] Explanation: After the first day full lakes are [1] After the second day full lakes are [1,2] After the third day, we dry lake 2. Full lakes are [1] After the fourth day, we dry lake 1. There is no full lakes. After the fifth day, full lakes are [2]. After the sixth day, full lakes are [1,2]. It is easy that this scenario is flood-free. [-1,-1,1,2,-1,-1] is another acceptable scenario. Example 3: Input: rains = [1,2,0,1,2] Output: [] Explanation: After the second day, full lakes are [1,2]. We have to dry one lake in the third day. After that, it will rain over lakes [1,2]. It's easy to prove that no matter which lake you choose to dry in the 3rd day, the other one will flood. Example 4: Input: rains = [69,0,0,0,69] Output: [-1,69,1,1,-1] Explanation: Any solution on one of the forms [-1,69,x,y,-1], [-1,x,69,y,-1] or [-1,x,y,69,-1] is acceptable where 1 <= x,y <= 10^9 Example 5: Input: rains = [10,20,20] Output: [] Explanation: It will rain over lake 20 two consecutive days. There is no chance to dry any lake.   Constraints: 1 <= rains.length <= 10^5 0 <= rains[i] <= 10^9
class Solution: def avoidFlood(self, rains: List[int]) -> List[int]: drying = [] out = [] lakes = {} v = 1 for c, i in enumerate(rains): out.append(-1) if i <= 0: drying.append(c) elif i in lakes and drying: found = -1 for index, val in enumerate(drying): if val > lakes[i]: found = index break if found > -1: out[drying.pop(found)] = i lakes[i] = c else: return [] elif i in lakes: return [] else: lakes[i] = c v = i for j in drying: out[j] = v return out
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR RETURN LIST IF VAR VAR RETURN LIST ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR VAR
Your country has an infinite number of lakes. Initially, all the lakes are empty, but when it rains over the nth lake, the nth lake becomes full of water. If it rains over a lake which is full of water, there will be a flood. Your goal is to avoid the flood in any lake. Given an integer array rains where: rains[i] > 0 means there will be rains over the rains[i] lake. rains[i] == 0 means there are no rains this day and you can choose one lake this day and dry it. Return an array ans where: ans.length == rains.length ans[i] == -1 if rains[i] > 0. ans[i] is the lake you choose to dry in the ith day if rains[i] == 0. If there are multiple valid answers return any of them. If it is impossible to avoid flood return an empty array. Notice that if you chose to dry a full lake, it becomes empty, but if you chose to dry an empty lake, nothing changes. (see example 4)   Example 1: Input: rains = [1,2,3,4] Output: [-1,-1,-1,-1] Explanation: After the first day full lakes are [1] After the second day full lakes are [1,2] After the third day full lakes are [1,2,3] After the fourth day full lakes are [1,2,3,4] There's no day to dry any lake and there is no flood in any lake. Example 2: Input: rains = [1,2,0,0,2,1] Output: [-1,-1,2,1,-1,-1] Explanation: After the first day full lakes are [1] After the second day full lakes are [1,2] After the third day, we dry lake 2. Full lakes are [1] After the fourth day, we dry lake 1. There is no full lakes. After the fifth day, full lakes are [2]. After the sixth day, full lakes are [1,2]. It is easy that this scenario is flood-free. [-1,-1,1,2,-1,-1] is another acceptable scenario. Example 3: Input: rains = [1,2,0,1,2] Output: [] Explanation: After the second day, full lakes are [1,2]. We have to dry one lake in the third day. After that, it will rain over lakes [1,2]. It's easy to prove that no matter which lake you choose to dry in the 3rd day, the other one will flood. Example 4: Input: rains = [69,0,0,0,69] Output: [-1,69,1,1,-1] Explanation: Any solution on one of the forms [-1,69,x,y,-1], [-1,x,69,y,-1] or [-1,x,y,69,-1] is acceptable where 1 <= x,y <= 10^9 Example 5: Input: rains = [10,20,20] Output: [] Explanation: It will rain over lake 20 two consecutive days. There is no chance to dry any lake.   Constraints: 1 <= rains.length <= 10^5 0 <= rains[i] <= 10^9
class Solution: def avoidFlood(self, rains: List[int]) -> List[int]: lakes = {} dries = [] res = [] for i, rain in enumerate(rains): if rain == 0: dries.append(i) res.append(1) else: if rain in lakes: if len(dries) == 0: return [] idx = bisect_left(dries, lakes[rain]) if idx == len(dries): return [] res[dries.pop(idx)] = rain lakes[rain] = i res.append(-1) return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN LIST ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR RETURN LIST ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN VAR VAR VAR
Your country has an infinite number of lakes. Initially, all the lakes are empty, but when it rains over the nth lake, the nth lake becomes full of water. If it rains over a lake which is full of water, there will be a flood. Your goal is to avoid the flood in any lake. Given an integer array rains where: rains[i] > 0 means there will be rains over the rains[i] lake. rains[i] == 0 means there are no rains this day and you can choose one lake this day and dry it. Return an array ans where: ans.length == rains.length ans[i] == -1 if rains[i] > 0. ans[i] is the lake you choose to dry in the ith day if rains[i] == 0. If there are multiple valid answers return any of them. If it is impossible to avoid flood return an empty array. Notice that if you chose to dry a full lake, it becomes empty, but if you chose to dry an empty lake, nothing changes. (see example 4)   Example 1: Input: rains = [1,2,3,4] Output: [-1,-1,-1,-1] Explanation: After the first day full lakes are [1] After the second day full lakes are [1,2] After the third day full lakes are [1,2,3] After the fourth day full lakes are [1,2,3,4] There's no day to dry any lake and there is no flood in any lake. Example 2: Input: rains = [1,2,0,0,2,1] Output: [-1,-1,2,1,-1,-1] Explanation: After the first day full lakes are [1] After the second day full lakes are [1,2] After the third day, we dry lake 2. Full lakes are [1] After the fourth day, we dry lake 1. There is no full lakes. After the fifth day, full lakes are [2]. After the sixth day, full lakes are [1,2]. It is easy that this scenario is flood-free. [-1,-1,1,2,-1,-1] is another acceptable scenario. Example 3: Input: rains = [1,2,0,1,2] Output: [] Explanation: After the second day, full lakes are [1,2]. We have to dry one lake in the third day. After that, it will rain over lakes [1,2]. It's easy to prove that no matter which lake you choose to dry in the 3rd day, the other one will flood. Example 4: Input: rains = [69,0,0,0,69] Output: [-1,69,1,1,-1] Explanation: Any solution on one of the forms [-1,69,x,y,-1], [-1,x,69,y,-1] or [-1,x,y,69,-1] is acceptable where 1 <= x,y <= 10^9 Example 5: Input: rains = [10,20,20] Output: [] Explanation: It will rain over lake 20 two consecutive days. There is no chance to dry any lake.   Constraints: 1 <= rains.length <= 10^5 0 <= rains[i] <= 10^9
class Solution: def avoidFlood(self, rains: List[int]) -> List[int]: d = {} dry = [] res = [] for day, rain in enumerate(rains): if rain != 0: if rain in d: p = d[rain] flag = -1 for dry_day in dry: if dry_day > p: flag = dry_day break if flag == -1: return [] res[flag] = rain dry.remove(flag) d[rain] = day else: d[rain] = day res.append(-1) else: dry.append(day) res.append(56) return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER RETURN LIST ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN VAR VAR VAR
Your country has an infinite number of lakes. Initially, all the lakes are empty, but when it rains over the nth lake, the nth lake becomes full of water. If it rains over a lake which is full of water, there will be a flood. Your goal is to avoid the flood in any lake. Given an integer array rains where: rains[i] > 0 means there will be rains over the rains[i] lake. rains[i] == 0 means there are no rains this day and you can choose one lake this day and dry it. Return an array ans where: ans.length == rains.length ans[i] == -1 if rains[i] > 0. ans[i] is the lake you choose to dry in the ith day if rains[i] == 0. If there are multiple valid answers return any of them. If it is impossible to avoid flood return an empty array. Notice that if you chose to dry a full lake, it becomes empty, but if you chose to dry an empty lake, nothing changes. (see example 4)   Example 1: Input: rains = [1,2,3,4] Output: [-1,-1,-1,-1] Explanation: After the first day full lakes are [1] After the second day full lakes are [1,2] After the third day full lakes are [1,2,3] After the fourth day full lakes are [1,2,3,4] There's no day to dry any lake and there is no flood in any lake. Example 2: Input: rains = [1,2,0,0,2,1] Output: [-1,-1,2,1,-1,-1] Explanation: After the first day full lakes are [1] After the second day full lakes are [1,2] After the third day, we dry lake 2. Full lakes are [1] After the fourth day, we dry lake 1. There is no full lakes. After the fifth day, full lakes are [2]. After the sixth day, full lakes are [1,2]. It is easy that this scenario is flood-free. [-1,-1,1,2,-1,-1] is another acceptable scenario. Example 3: Input: rains = [1,2,0,1,2] Output: [] Explanation: After the second day, full lakes are [1,2]. We have to dry one lake in the third day. After that, it will rain over lakes [1,2]. It's easy to prove that no matter which lake you choose to dry in the 3rd day, the other one will flood. Example 4: Input: rains = [69,0,0,0,69] Output: [-1,69,1,1,-1] Explanation: Any solution on one of the forms [-1,69,x,y,-1], [-1,x,69,y,-1] or [-1,x,y,69,-1] is acceptable where 1 <= x,y <= 10^9 Example 5: Input: rains = [10,20,20] Output: [] Explanation: It will rain over lake 20 two consecutive days. There is no chance to dry any lake.   Constraints: 1 <= rains.length <= 10^5 0 <= rains[i] <= 10^9
class Solution: def avoidFlood(self, rains: List[int]) -> List[int]: lakes = {} zeroes = [] length = len(rains) for i, rain in enumerate(rains): if rain == 0: zeroes.append(i) continue if rain in lakes: lake_index = lakes[rain] found = False for j, zero in enumerate(zeroes): if zero > lake_index: rains[zero] = rain found = True del zeroes[j] break if not found: return [] lakes[rain] = i rains[i] = -1 else: lakes[rain] = i rains[i] = -1 for zero in zeroes: rains[zero] = 1 return rains
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER VAR VAR IF VAR RETURN LIST ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER RETURN VAR VAR VAR