description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints. In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards. Your score is the sum of the points of the cards you have taken. Given the integer array cardPoints and the integer k, return the maximum score you can obtain.   Example 1: Input: cardPoints = [1,2,3,4,5,6,1], k = 3 Output: 12 Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12. Example 2: Input: cardPoints = [2,2,2], k = 2 Output: 4 Explanation: Regardless of which two cards you take, your score will always be 4. Example 3: Input: cardPoints = [9,7,7,9,7,7,9], k = 7 Output: 55 Explanation: You have to take all the cards. Your score is the sum of points of all cards. Example 4: Input: cardPoints = [1,1000,1], k = 1 Output: 1 Explanation: You cannot take the card in the middle. Your best score is 1. Example 5: Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3 Output: 202   Constraints: 1 <= cardPoints.length <= 10^5 1 <= cardPoints[i] <= 10^4 1 <= k <= cardPoints.length
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: left_sums = [0] * (k + 1) right_sums = [0] * (k + 1) for i in range(k): left_sums[i + 1] = left_sums[i] + cardPoints[i] right_sums[i + 1] = right_sums[i] + cardPoints[len(cardPoints) - i - 1] res = 0 for i in range(k + 1): j = k - i res = max(res, left_sums[i] + right_sums[j]) return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR RETURN VAR VAR
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints. In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards. Your score is the sum of the points of the cards you have taken. Given the integer array cardPoints and the integer k, return the maximum score you can obtain.   Example 1: Input: cardPoints = [1,2,3,4,5,6,1], k = 3 Output: 12 Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12. Example 2: Input: cardPoints = [2,2,2], k = 2 Output: 4 Explanation: Regardless of which two cards you take, your score will always be 4. Example 3: Input: cardPoints = [9,7,7,9,7,7,9], k = 7 Output: 55 Explanation: You have to take all the cards. Your score is the sum of points of all cards. Example 4: Input: cardPoints = [1,1000,1], k = 1 Output: 1 Explanation: You cannot take the card in the middle. Your best score is 1. Example 5: Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3 Output: 202   Constraints: 1 <= cardPoints.length <= 10^5 1 <= cardPoints[i] <= 10^4 1 <= k <= cardPoints.length
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: if k == len(cardPoints): return sum(cardPoints) front = [0] * k back = [0] * k front[0] = cardPoints[0] for i in range(1, k): front[i] = front[i - 1] + cardPoints[i] back[0] = cardPoints[-1] for i in range(1, k): back[i] = back[i - 1] + cardPoints[-1 - i] max_score = 0 print(front, back) for i in range(k + 1): if i == 0: max_score = max(max_score, back[-1]) elif i == k: max_score = max(max_score, front[k - 1]) else: max_score = max(max_score, front[i - 1] + back[k - i - 1]) return max_score
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints. In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards. Your score is the sum of the points of the cards you have taken. Given the integer array cardPoints and the integer k, return the maximum score you can obtain.   Example 1: Input: cardPoints = [1,2,3,4,5,6,1], k = 3 Output: 12 Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12. Example 2: Input: cardPoints = [2,2,2], k = 2 Output: 4 Explanation: Regardless of which two cards you take, your score will always be 4. Example 3: Input: cardPoints = [9,7,7,9,7,7,9], k = 7 Output: 55 Explanation: You have to take all the cards. Your score is the sum of points of all cards. Example 4: Input: cardPoints = [1,1000,1], k = 1 Output: 1 Explanation: You cannot take the card in the middle. Your best score is 1. Example 5: Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3 Output: 202   Constraints: 1 <= cardPoints.length <= 10^5 1 <= cardPoints[i] <= 10^4 1 <= k <= cardPoints.length
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: right_index = len(cardPoints) - k curr_max = sum(cardPoints[right_index:]) curr_sum = curr_max for left_index, right_index in zip( list(range(0, k)), list(range(len(cardPoints) - k, len(cardPoints))) ): print((left_index, right_index)) curr_sum -= cardPoints[right_index] curr_sum += cardPoints[left_index] curr_max = max(curr_max, curr_sum) return curr_max
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints. In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards. Your score is the sum of the points of the cards you have taken. Given the integer array cardPoints and the integer k, return the maximum score you can obtain.   Example 1: Input: cardPoints = [1,2,3,4,5,6,1], k = 3 Output: 12 Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12. Example 2: Input: cardPoints = [2,2,2], k = 2 Output: 4 Explanation: Regardless of which two cards you take, your score will always be 4. Example 3: Input: cardPoints = [9,7,7,9,7,7,9], k = 7 Output: 55 Explanation: You have to take all the cards. Your score is the sum of points of all cards. Example 4: Input: cardPoints = [1,1000,1], k = 1 Output: 1 Explanation: You cannot take the card in the middle. Your best score is 1. Example 5: Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3 Output: 202   Constraints: 1 <= cardPoints.length <= 10^5 1 <= cardPoints[i] <= 10^4 1 <= k <= cardPoints.length
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: ans = win = 0 for i in range(-k, k): win += cardPoints[i] if i >= 0: win -= cardPoints[i - k] ans = max(win, ans) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints. In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards. Your score is the sum of the points of the cards you have taken. Given the integer array cardPoints and the integer k, return the maximum score you can obtain.   Example 1: Input: cardPoints = [1,2,3,4,5,6,1], k = 3 Output: 12 Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12. Example 2: Input: cardPoints = [2,2,2], k = 2 Output: 4 Explanation: Regardless of which two cards you take, your score will always be 4. Example 3: Input: cardPoints = [9,7,7,9,7,7,9], k = 7 Output: 55 Explanation: You have to take all the cards. Your score is the sum of points of all cards. Example 4: Input: cardPoints = [1,1000,1], k = 1 Output: 1 Explanation: You cannot take the card in the middle. Your best score is 1. Example 5: Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3 Output: 202   Constraints: 1 <= cardPoints.length <= 10^5 1 <= cardPoints[i] <= 10^4 1 <= k <= cardPoints.length
class Solution: def maxScore(self, nums: List[int], k: int) -> int: n = len(nums) if k == n: return sum(nums) pre = [0] post = [] for e in nums: pre.append(pre[-1] + e) post.append(e) post.append(0) for i in range(n - 1, -1, -1): post[i] = post[i + 1] + nums[i] res = 0 j = n - k while j <= n: res = max(res, pre[i] + post[j]) i += 1 j += 1 return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints. In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards. Your score is the sum of the points of the cards you have taken. Given the integer array cardPoints and the integer k, return the maximum score you can obtain.   Example 1: Input: cardPoints = [1,2,3,4,5,6,1], k = 3 Output: 12 Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12. Example 2: Input: cardPoints = [2,2,2], k = 2 Output: 4 Explanation: Regardless of which two cards you take, your score will always be 4. Example 3: Input: cardPoints = [9,7,7,9,7,7,9], k = 7 Output: 55 Explanation: You have to take all the cards. Your score is the sum of points of all cards. Example 4: Input: cardPoints = [1,1000,1], k = 1 Output: 1 Explanation: You cannot take the card in the middle. Your best score is 1. Example 5: Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3 Output: 202   Constraints: 1 <= cardPoints.length <= 10^5 1 <= cardPoints[i] <= 10^4 1 <= k <= cardPoints.length
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: left_cumsum = [0] right_cumsum = [0] for p in cardPoints[: k + 1]: left_cumsum.append(left_cumsum[-1] + p) for p in reversed(cardPoints[-(k + 1) :]): right_cumsum.append(right_cumsum[-1] + p) result = 0 for i in range(k + 1): result = max(result, left_cumsum[i] + right_cumsum[k - i]) return result
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER FOR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR RETURN VAR VAR
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints. In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards. Your score is the sum of the points of the cards you have taken. Given the integer array cardPoints and the integer k, return the maximum score you can obtain.   Example 1: Input: cardPoints = [1,2,3,4,5,6,1], k = 3 Output: 12 Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12. Example 2: Input: cardPoints = [2,2,2], k = 2 Output: 4 Explanation: Regardless of which two cards you take, your score will always be 4. Example 3: Input: cardPoints = [9,7,7,9,7,7,9], k = 7 Output: 55 Explanation: You have to take all the cards. Your score is the sum of points of all cards. Example 4: Input: cardPoints = [1,1000,1], k = 1 Output: 1 Explanation: You cannot take the card in the middle. Your best score is 1. Example 5: Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3 Output: 202   Constraints: 1 <= cardPoints.length <= 10^5 1 <= cardPoints[i] <= 10^4 1 <= k <= cardPoints.length
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: n = len(cardPoints) w = n - k left, right = 0, w - 1 ans = float("-inf") win_sum = sum(cardPoints[:w]) total = sum(cardPoints) while right < n: sub = total - win_sum ans = max(sub, ans) if left < n: win_sum -= cardPoints[left] left += 1 right += 1 if right < n: win_sum += cardPoints[right] return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR RETURN VAR VAR
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints. In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards. Your score is the sum of the points of the cards you have taken. Given the integer array cardPoints and the integer k, return the maximum score you can obtain.   Example 1: Input: cardPoints = [1,2,3,4,5,6,1], k = 3 Output: 12 Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12. Example 2: Input: cardPoints = [2,2,2], k = 2 Output: 4 Explanation: Regardless of which two cards you take, your score will always be 4. Example 3: Input: cardPoints = [9,7,7,9,7,7,9], k = 7 Output: 55 Explanation: You have to take all the cards. Your score is the sum of points of all cards. Example 4: Input: cardPoints = [1,1000,1], k = 1 Output: 1 Explanation: You cannot take the card in the middle. Your best score is 1. Example 5: Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3 Output: 202   Constraints: 1 <= cardPoints.length <= 10^5 1 <= cardPoints[i] <= 10^4 1 <= k <= cardPoints.length
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: total = sum(cardPoints[:k]) if k == len(cardPoints): return total max_sum = total print(max_sum) for i in range(k - 1, -1, -1): total = total + cardPoints[i - k] - cardPoints[i] print((i - k, total, cardPoints[i - k], cardPoints[i])) if total > max_sum: max_sum = total return max_sum
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR VAR
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints. In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards. Your score is the sum of the points of the cards you have taken. Given the integer array cardPoints and the integer k, return the maximum score you can obtain.   Example 1: Input: cardPoints = [1,2,3,4,5,6,1], k = 3 Output: 12 Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12. Example 2: Input: cardPoints = [2,2,2], k = 2 Output: 4 Explanation: Regardless of which two cards you take, your score will always be 4. Example 3: Input: cardPoints = [9,7,7,9,7,7,9], k = 7 Output: 55 Explanation: You have to take all the cards. Your score is the sum of points of all cards. Example 4: Input: cardPoints = [1,1000,1], k = 1 Output: 1 Explanation: You cannot take the card in the middle. Your best score is 1. Example 5: Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3 Output: 202   Constraints: 1 <= cardPoints.length <= 10^5 1 <= cardPoints[i] <= 10^4 1 <= k <= cardPoints.length
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: score = sum(cardPoints[:k]) best = score for i in range(k): score += cardPoints[-(i + 1)] - cardPoints[k - i - 1] if score > best: best = score return best
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR RETURN VAR VAR
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints. In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards. Your score is the sum of the points of the cards you have taken. Given the integer array cardPoints and the integer k, return the maximum score you can obtain.   Example 1: Input: cardPoints = [1,2,3,4,5,6,1], k = 3 Output: 12 Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12. Example 2: Input: cardPoints = [2,2,2], k = 2 Output: 4 Explanation: Regardless of which two cards you take, your score will always be 4. Example 3: Input: cardPoints = [9,7,7,9,7,7,9], k = 7 Output: 55 Explanation: You have to take all the cards. Your score is the sum of points of all cards. Example 4: Input: cardPoints = [1,1000,1], k = 1 Output: 1 Explanation: You cannot take the card in the middle. Your best score is 1. Example 5: Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3 Output: 202   Constraints: 1 <= cardPoints.length <= 10^5 1 <= cardPoints[i] <= 10^4 1 <= k <= cardPoints.length
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: s = sum(cardPoints) if k >= len(cardPoints): return s maxPoint = 0 cur = 0 j = 0 n = len(cardPoints) for i, point in enumerate(cardPoints): if i - j + k > n - 1: cur -= cardPoints[j] j += 1 cur += point if i - j + k == n - 1: maxPoint = max(maxPoint, s - cur) return maxPoint
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR IF BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR VAR
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints. In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards. Your score is the sum of the points of the cards you have taken. Given the integer array cardPoints and the integer k, return the maximum score you can obtain.   Example 1: Input: cardPoints = [1,2,3,4,5,6,1], k = 3 Output: 12 Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12. Example 2: Input: cardPoints = [2,2,2], k = 2 Output: 4 Explanation: Regardless of which two cards you take, your score will always be 4. Example 3: Input: cardPoints = [9,7,7,9,7,7,9], k = 7 Output: 55 Explanation: You have to take all the cards. Your score is the sum of points of all cards. Example 4: Input: cardPoints = [1,1000,1], k = 1 Output: 1 Explanation: You cannot take the card in the middle. Your best score is 1. Example 5: Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3 Output: 202   Constraints: 1 <= cardPoints.length <= 10^5 1 <= cardPoints[i] <= 10^4 1 <= k <= cardPoints.length
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: n = len(cardPoints) total = sum(cardPoints) if n == k or n < k: return total remove = n - k ans = 0 curr = 0 start = 0 for right in range(n): curr += cardPoints[right] if right - start + 1 == remove: ans = max(ans, total - curr) curr -= cardPoints[start] start += 1 return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR RETURN VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints. In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards. Your score is the sum of the points of the cards you have taken. Given the integer array cardPoints and the integer k, return the maximum score you can obtain.   Example 1: Input: cardPoints = [1,2,3,4,5,6,1], k = 3 Output: 12 Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12. Example 2: Input: cardPoints = [2,2,2], k = 2 Output: 4 Explanation: Regardless of which two cards you take, your score will always be 4. Example 3: Input: cardPoints = [9,7,7,9,7,7,9], k = 7 Output: 55 Explanation: You have to take all the cards. Your score is the sum of points of all cards. Example 4: Input: cardPoints = [1,1000,1], k = 1 Output: 1 Explanation: You cannot take the card in the middle. Your best score is 1. Example 5: Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3 Output: 202   Constraints: 1 <= cardPoints.length <= 10^5 1 <= cardPoints[i] <= 10^4 1 <= k <= cardPoints.length
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: points_L = [] points_R = [] for p in cardPoints: if len(points_L): points_L.append(points_L[-1] + p) else: points_L.append(p) for p in cardPoints[::-1]: if len(points_R): points_R.append(points_R[-1] + p) else: points_R.append(p) points_R = points_R[::-1] return self.solution(points_L, points_R, k) def solution(self, points_L, points_R, k): max = 0 for i in range(k + 1): cmp1 = points_L[i - 1] if i > 0 else 0 cmp2 = points_R[-(k - i)] if i < k else 0 score = cmp1 + cmp2 if score > max: max = score return max
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints. In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards. Your score is the sum of the points of the cards you have taken. Given the integer array cardPoints and the integer k, return the maximum score you can obtain.   Example 1: Input: cardPoints = [1,2,3,4,5,6,1], k = 3 Output: 12 Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12. Example 2: Input: cardPoints = [2,2,2], k = 2 Output: 4 Explanation: Regardless of which two cards you take, your score will always be 4. Example 3: Input: cardPoints = [9,7,7,9,7,7,9], k = 7 Output: 55 Explanation: You have to take all the cards. Your score is the sum of points of all cards. Example 4: Input: cardPoints = [1,1000,1], k = 1 Output: 1 Explanation: You cannot take the card in the middle. Your best score is 1. Example 5: Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3 Output: 202   Constraints: 1 <= cardPoints.length <= 10^5 1 <= cardPoints[i] <= 10^4 1 <= k <= cardPoints.length
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: points = cardPoints[-k:] + cardPoints[:k] curr = 0 max_ = 0 for ind, score in enumerate(points): if ind >= k: curr -= points[ind - k] curr += score max_ = max(max_, curr) return max_
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints. In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards. Your score is the sum of the points of the cards you have taken. Given the integer array cardPoints and the integer k, return the maximum score you can obtain.   Example 1: Input: cardPoints = [1,2,3,4,5,6,1], k = 3 Output: 12 Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12. Example 2: Input: cardPoints = [2,2,2], k = 2 Output: 4 Explanation: Regardless of which two cards you take, your score will always be 4. Example 3: Input: cardPoints = [9,7,7,9,7,7,9], k = 7 Output: 55 Explanation: You have to take all the cards. Your score is the sum of points of all cards. Example 4: Input: cardPoints = [1,1000,1], k = 1 Output: 1 Explanation: You cannot take the card in the middle. Your best score is 1. Example 5: Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3 Output: 202   Constraints: 1 <= cardPoints.length <= 10^5 1 <= cardPoints[i] <= 10^4 1 <= k <= cardPoints.length
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: size = len(cardPoints) maxRightSum = sum(cardPoints[size - k :]) ans = maxRightSum currSum = maxRightSum for i in range(k): currSum = currSum - cardPoints[size - k + i] + cardPoints[i] ans = max(ans, currSum) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints. In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards. Your score is the sum of the points of the cards you have taken. Given the integer array cardPoints and the integer k, return the maximum score you can obtain.   Example 1: Input: cardPoints = [1,2,3,4,5,6,1], k = 3 Output: 12 Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12. Example 2: Input: cardPoints = [2,2,2], k = 2 Output: 4 Explanation: Regardless of which two cards you take, your score will always be 4. Example 3: Input: cardPoints = [9,7,7,9,7,7,9], k = 7 Output: 55 Explanation: You have to take all the cards. Your score is the sum of points of all cards. Example 4: Input: cardPoints = [1,1000,1], k = 1 Output: 1 Explanation: You cannot take the card in the middle. Your best score is 1. Example 5: Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3 Output: 202   Constraints: 1 <= cardPoints.length <= 10^5 1 <= cardPoints[i] <= 10^4 1 <= k <= cardPoints.length
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: if len(cardPoints) < k: return -1 remain = len(cardPoints) - k suum = sum(cardPoints[:remain]) min_suum = suum for i in range(remain, len(cardPoints)): suum = suum - cardPoints[i - remain] + cardPoints[i] min_suum = min(min_suum, suum) return sum(cardPoints) - min_suum
CLASS_DEF FUNC_DEF VAR VAR VAR IF FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints. In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards. Your score is the sum of the points of the cards you have taken. Given the integer array cardPoints and the integer k, return the maximum score you can obtain.   Example 1: Input: cardPoints = [1,2,3,4,5,6,1], k = 3 Output: 12 Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12. Example 2: Input: cardPoints = [2,2,2], k = 2 Output: 4 Explanation: Regardless of which two cards you take, your score will always be 4. Example 3: Input: cardPoints = [9,7,7,9,7,7,9], k = 7 Output: 55 Explanation: You have to take all the cards. Your score is the sum of points of all cards. Example 4: Input: cardPoints = [1,1000,1], k = 1 Output: 1 Explanation: You cannot take the card in the middle. Your best score is 1. Example 5: Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3 Output: 202   Constraints: 1 <= cardPoints.length <= 10^5 1 <= cardPoints[i] <= 10^4 1 <= k <= cardPoints.length
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: maxSum = sum(cardPoints) if len(cardPoints) <= k: return maxSum subSum = 0 ans = 0 for i in range(len(cardPoints)): subSum += cardPoints[i] if i + 1 >= len(cardPoints) - k: ans = max(ans, maxSum - subSum) subSum -= cardPoints[i - (len(cardPoints) - k - 1)] return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints. In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards. Your score is the sum of the points of the cards you have taken. Given the integer array cardPoints and the integer k, return the maximum score you can obtain.   Example 1: Input: cardPoints = [1,2,3,4,5,6,1], k = 3 Output: 12 Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12. Example 2: Input: cardPoints = [2,2,2], k = 2 Output: 4 Explanation: Regardless of which two cards you take, your score will always be 4. Example 3: Input: cardPoints = [9,7,7,9,7,7,9], k = 7 Output: 55 Explanation: You have to take all the cards. Your score is the sum of points of all cards. Example 4: Input: cardPoints = [1,1000,1], k = 1 Output: 1 Explanation: You cannot take the card in the middle. Your best score is 1. Example 5: Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3 Output: 202   Constraints: 1 <= cardPoints.length <= 10^5 1 <= cardPoints[i] <= 10^4 1 <= k <= cardPoints.length
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: N = len(cardPoints) res = float("inf") run = 0 for i, p in enumerate(cardPoints): if i >= N - k: run -= cardPoints[i - N + k] run += p if i >= N - k - 1: res = min(res, run) return sum(cardPoints) - res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints. In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards. Your score is the sum of the points of the cards you have taken. Given the integer array cardPoints and the integer k, return the maximum score you can obtain.   Example 1: Input: cardPoints = [1,2,3,4,5,6,1], k = 3 Output: 12 Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12. Example 2: Input: cardPoints = [2,2,2], k = 2 Output: 4 Explanation: Regardless of which two cards you take, your score will always be 4. Example 3: Input: cardPoints = [9,7,7,9,7,7,9], k = 7 Output: 55 Explanation: You have to take all the cards. Your score is the sum of points of all cards. Example 4: Input: cardPoints = [1,1000,1], k = 1 Output: 1 Explanation: You cannot take the card in the middle. Your best score is 1. Example 5: Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3 Output: 202   Constraints: 1 <= cardPoints.length <= 10^5 1 <= cardPoints[i] <= 10^4 1 <= k <= cardPoints.length
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: if k == len(cardPoints): return sum(cardPoints) size = len(cardPoints) - k min_sum = float("inf") left = 0 right = 0 window_sum = 0 while right < len(cardPoints): window_sum += cardPoints[right] right += 1 while right - left == size: min_sum = min(min_sum, window_sum) window_sum -= cardPoints[left] left += 1 return sum(cardPoints) - min_sum
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER WHILE BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints. In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards. Your score is the sum of the points of the cards you have taken. Given the integer array cardPoints and the integer k, return the maximum score you can obtain.   Example 1: Input: cardPoints = [1,2,3,4,5,6,1], k = 3 Output: 12 Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12. Example 2: Input: cardPoints = [2,2,2], k = 2 Output: 4 Explanation: Regardless of which two cards you take, your score will always be 4. Example 3: Input: cardPoints = [9,7,7,9,7,7,9], k = 7 Output: 55 Explanation: You have to take all the cards. Your score is the sum of points of all cards. Example 4: Input: cardPoints = [1,1000,1], k = 1 Output: 1 Explanation: You cannot take the card in the middle. Your best score is 1. Example 5: Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3 Output: 202   Constraints: 1 <= cardPoints.length <= 10^5 1 <= cardPoints[i] <= 10^4 1 <= k <= cardPoints.length
class Solution: def maxScore(self, card_points: List[int], k: int) -> int: window_size = len(card_points) - k current_min_sum, min_sum = 0, sys.maxsize total_points = 0 left = 0 for right, value in enumerate(card_points): total_points += value current_min_sum += value current_size = right - left + 1 if current_size < window_size: continue if current_size > window_size: current_min_sum -= card_points[left] left += 1 min_sum = min(min_sum, current_min_sum) return total_points - min_sum
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR VAR VAR
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints. In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards. Your score is the sum of the points of the cards you have taken. Given the integer array cardPoints and the integer k, return the maximum score you can obtain.   Example 1: Input: cardPoints = [1,2,3,4,5,6,1], k = 3 Output: 12 Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12. Example 2: Input: cardPoints = [2,2,2], k = 2 Output: 4 Explanation: Regardless of which two cards you take, your score will always be 4. Example 3: Input: cardPoints = [9,7,7,9,7,7,9], k = 7 Output: 55 Explanation: You have to take all the cards. Your score is the sum of points of all cards. Example 4: Input: cardPoints = [1,1000,1], k = 1 Output: 1 Explanation: You cannot take the card in the middle. Your best score is 1. Example 5: Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3 Output: 202   Constraints: 1 <= cardPoints.length <= 10^5 1 <= cardPoints[i] <= 10^4 1 <= k <= cardPoints.length
def max_score_using_subsequence(card_points, k): l = len(card_points) - k if l == 0: return sum(card_points) sum_subsequence = 0 for i in range(l): sum_subsequence += card_points[i] lowest = sum_subsequence for i in range(len(card_points) - l): sum_subsequence = sum_subsequence - card_points[i] + card_points[i + l] if sum_subsequence < lowest: lowest = sum_subsequence return sum(card_points) - lowest class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: return max_score_using_subsequence(cardPoints, k)
FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR CLASS_DEF FUNC_DEF VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints. In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards. Your score is the sum of the points of the cards you have taken. Given the integer array cardPoints and the integer k, return the maximum score you can obtain.   Example 1: Input: cardPoints = [1,2,3,4,5,6,1], k = 3 Output: 12 Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12. Example 2: Input: cardPoints = [2,2,2], k = 2 Output: 4 Explanation: Regardless of which two cards you take, your score will always be 4. Example 3: Input: cardPoints = [9,7,7,9,7,7,9], k = 7 Output: 55 Explanation: You have to take all the cards. Your score is the sum of points of all cards. Example 4: Input: cardPoints = [1,1000,1], k = 1 Output: 1 Explanation: You cannot take the card in the middle. Your best score is 1. Example 5: Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3 Output: 202   Constraints: 1 <= cardPoints.length <= 10^5 1 <= cardPoints[i] <= 10^4 1 <= k <= cardPoints.length
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: sub_len = len(cardPoints) - k localSum = 0 localSum = sum(cardPoints[0:sub_len]) localMin = localSum for i in range(sub_len, len(cardPoints)): localSum += cardPoints[i] localSum -= cardPoints[i - sub_len] localMin = min(localSum, localMin) return sum(cardPoints) - localMin
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints. In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards. Your score is the sum of the points of the cards you have taken. Given the integer array cardPoints and the integer k, return the maximum score you can obtain.   Example 1: Input: cardPoints = [1,2,3,4,5,6,1], k = 3 Output: 12 Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12. Example 2: Input: cardPoints = [2,2,2], k = 2 Output: 4 Explanation: Regardless of which two cards you take, your score will always be 4. Example 3: Input: cardPoints = [9,7,7,9,7,7,9], k = 7 Output: 55 Explanation: You have to take all the cards. Your score is the sum of points of all cards. Example 4: Input: cardPoints = [1,1000,1], k = 1 Output: 1 Explanation: You cannot take the card in the middle. Your best score is 1. Example 5: Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3 Output: 202   Constraints: 1 <= cardPoints.length <= 10^5 1 <= cardPoints[i] <= 10^4 1 <= k <= cardPoints.length
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: pre_sum = [] temp_left = 0 for i, v in enumerate(cardPoints): pre_sum.append(temp_left) temp_left += v pre_sum.append(temp_left) post_sum = [] temp_right = 0 temp_right = 0 for i in range(len(cardPoints) - 1, -1, -1): v = cardPoints[i] post_sum.append(temp_right) temp_right += v post_sum.append(v) post_sum.reverse() return max([(pre_sum[i] + post_sum[-(k - i) - 1]) for i in range(k + 1)])
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR
There are several cards arranged in a row, and each card has an associated number of points The points are given in the integer array cardPoints. In one step, you can take one card from the beginning or from the end of the row. You have to take exactly k cards. Your score is the sum of the points of the cards you have taken. Given the integer array cardPoints and the integer k, return the maximum score you can obtain.   Example 1: Input: cardPoints = [1,2,3,4,5,6,1], k = 3 Output: 12 Explanation: After the first step, your score will always be 1. However, choosing the rightmost card first will maximize your total score. The optimal strategy is to take the three cards on the right, giving a final score of 1 + 6 + 5 = 12. Example 2: Input: cardPoints = [2,2,2], k = 2 Output: 4 Explanation: Regardless of which two cards you take, your score will always be 4. Example 3: Input: cardPoints = [9,7,7,9,7,7,9], k = 7 Output: 55 Explanation: You have to take all the cards. Your score is the sum of points of all cards. Example 4: Input: cardPoints = [1,1000,1], k = 1 Output: 1 Explanation: You cannot take the card in the middle. Your best score is 1. Example 5: Input: cardPoints = [1,79,80,1,1,1,200,1], k = 3 Output: 202   Constraints: 1 <= cardPoints.length <= 10^5 1 <= cardPoints[i] <= 10^4 1 <= k <= cardPoints.length
class Solution: def maxScore(self, cardPoints: List[int], k: int) -> int: leftsum = [0] * len(cardPoints) rightsum = [0] * len(cardPoints) n = len(cardPoints) leftsum[0] = cardPoints[0] rightsum[n - 1] = cardPoints[n - 1] for i in range(1, n): leftsum[i] = leftsum[i - 1] + cardPoints[i] rightsum[n - 1 - i] = rightsum[n - 1 - i + 1] + cardPoints[n - 1 - i] res = max(leftsum[k - 1], rightsum[-(k - 1 + 1)]) for i in range(k - 1): res = max(leftsum[i] + rightsum[-(k - i - 1)], res) return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN VAR VAR
Given a BST (Binary Search Tree), find the shortest range [x, y], such that, at least one node of every level of the BST lies in the range. If there are multiple ranges with the same gap (i.e. (y-x)) return the range with the smallest x. Example 1: Input: 8 / \ 3 10 / \ \ 2 6 14 / \ / 4 7 12 / \ 11 13 Output: 6 11 Explanation: Level order traversal of the tree is [8], [3, 10], [2, 6, 14], [4, 7, 12], [11, 13]. The shortest range which satisfies the above mentioned condition is [6, 11]. Example 2: Input: 12 \ 13 \ 14 \ 15 \ 16 Output: 12 16 Explanation: Each level contains one node, so the shortest range is [12, 16]. Your Task: You don't need to read input or print anything. Complete the function shortestRange() which takes the root of the tree as an input parameter and returns the pair of numbers Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ Node Value ≤ 10^{5}
class Solution: def shortestRange(self, root): lists = get_lists(root) min_range = min(l[0] for l in lists), max(l[0] for l in lists) indexes = [(0) for _ in range(len(lists))] while True: nums = [l[i] for l, i in zip(lists, indexes)] mx, mn = max(nums), min(nums) if mx - mn < min_range[1] - min_range[0]: min_range = mn, mx min_i = 0 min_v = nums[0] for i, v in enumerate(nums): if v < min_v: min_v = v min_i = i if len(lists[min_i]) == indexes[min_i] + 1: return min_range indexes[min_i] += 1 def get_lists(root): queue, next_layer, res = [root], [], [] while queue: res.append([]) for node in queue: res[-1].append(node.data) if node.left: next_layer.append(node.left) if node.right: next_layer.append(node.right) queue, next_layer = next_layer, [] return res
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE NUMBER ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER RETURN VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR LIST VAR LIST LIST WHILE VAR EXPR FUNC_CALL VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR IF VAR EXPR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR LIST RETURN VAR
Given a BST (Binary Search Tree), find the shortest range [x, y], such that, at least one node of every level of the BST lies in the range. If there are multiple ranges with the same gap (i.e. (y-x)) return the range with the smallest x. Example 1: Input: 8 / \ 3 10 / \ \ 2 6 14 / \ / 4 7 12 / \ 11 13 Output: 6 11 Explanation: Level order traversal of the tree is [8], [3, 10], [2, 6, 14], [4, 7, 12], [11, 13]. The shortest range which satisfies the above mentioned condition is [6, 11]. Example 2: Input: 12 \ 13 \ 14 \ 15 \ 16 Output: 12 16 Explanation: Each level contains one node, so the shortest range is [12, 16]. Your Task: You don't need to read input or print anything. Complete the function shortestRange() which takes the root of the tree as an input parameter and returns the pair of numbers Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ Node Value ≤ 10^{5}
def inorder_(root, inArr, lvlSet, lvl): if root == None: return inorder_(root.left, inArr, lvlSet, lvl + 1) inArr.append([root.data, lvl]) lvlSet.add(lvl) inorder_(root.right, inArr, lvlSet, lvl + 1) def thisIsAns(freq, levels): for i in range(levels): if freq[i] > 0: continue return False return True def findMinRange(root): inArr = [] lvlSet = set() inorder_(root, inArr, lvlSet, 1) n = len(inArr) if n == 1: return [root.data, root.data] levels = len(lvlSet) i = 0 j = i ans = None min = 0 freq = [0] * levels flg = True while i <= j and j < n: if flg: freq[inArr[j][1] - 1] += 1 if thisIsAns(freq, levels): p_ans = [inArr[i][0], inArr[j][0]] p_min = p_ans[1] - p_ans[0] if ans is None: ans = p_ans min = p_min elif p_min < min: ans = p_ans min = p_min elif p_min == min and p_ans[0] < ans[0]: ans = p_ans freq[inArr[i][1] - 1] -= 1 i += 1 flg = False continue flg = True j += 1 return ans class Solution: def shortestRange(self, root): return findMinRange(root)
FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN LIST VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR LIST VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER RETURN VAR CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR
Given a BST (Binary Search Tree), find the shortest range [x, y], such that, at least one node of every level of the BST lies in the range. If there are multiple ranges with the same gap (i.e. (y-x)) return the range with the smallest x. Example 1: Input: 8 / \ 3 10 / \ \ 2 6 14 / \ / 4 7 12 / \ 11 13 Output: 6 11 Explanation: Level order traversal of the tree is [8], [3, 10], [2, 6, 14], [4, 7, 12], [11, 13]. The shortest range which satisfies the above mentioned condition is [6, 11]. Example 2: Input: 12 \ 13 \ 14 \ 15 \ 16 Output: 12 16 Explanation: Each level contains one node, so the shortest range is [12, 16]. Your Task: You don't need to read input or print anything. Complete the function shortestRange() which takes the root of the tree as an input parameter and returns the pair of numbers Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ Node Value ≤ 10^{5}
class Solution: def shortestRange(self, root): nums = [] level_count = {} traverse(root, 0, nums, level_count) num_levels = len(level_count) level_counts = {} best_start = 0 best_end = 999999999999 i = 0 j = 0 while j < len(nums): val, level = nums[j] if level not in level_counts: level_counts[level] = 1 else: level_counts[level] += 1 while i <= j and len(level_counts) == num_levels: if ( nums[j][0] - nums[i][0] < best_end - best_start or nums[j][0] - nums[i][0] == best_end - best_start and nums[i][0] < best_start ): best_start = nums[i][0] best_end = nums[j][0] level_counts[nums[i][1]] -= 1 if level_counts[nums[i][1]] == 0: level_counts.pop(nums[i][1]) i += 1 j += 1 return best_start, best_end def traverse(cur, level, res, level_count): if not cur: return traverse(cur.left, level + 1, res, level_count) res.append((cur.data, level)) if level in level_count: level_count[level] += 1 else: level_count[level] = 1 traverse(cur.right, level + 1, res, level_count)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR DICT EXPR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER WHILE VAR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR VAR VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR FUNC_DEF IF VAR RETURN EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR
Given a BST (Binary Search Tree), find the shortest range [x, y], such that, at least one node of every level of the BST lies in the range. If there are multiple ranges with the same gap (i.e. (y-x)) return the range with the smallest x. Example 1: Input: 8 / \ 3 10 / \ \ 2 6 14 / \ / 4 7 12 / \ 11 13 Output: 6 11 Explanation: Level order traversal of the tree is [8], [3, 10], [2, 6, 14], [4, 7, 12], [11, 13]. The shortest range which satisfies the above mentioned condition is [6, 11]. Example 2: Input: 12 \ 13 \ 14 \ 15 \ 16 Output: 12 16 Explanation: Each level contains one node, so the shortest range is [12, 16]. Your Task: You don't need to read input or print anything. Complete the function shortestRange() which takes the root of the tree as an input parameter and returns the pair of numbers Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ Node Value ≤ 10^{5}
class Solution: def shortestRange(self, root): a = [] def dfs(node, level): if node: a.append((node.data, level)) dfs(node.left, level + 1) dfs(node.right, level + 1) dfs(root, 0) a.sort() n, m = max(x for _, x in a) + 1, {} l, r = a[0][0], a[-1][0] i = 0 j = 0 for j, (_, t) in enumerate(a): m[t] = m.get(t, 0) + 1 while i < j and len(m) == n: if a[j][0] - a[i][0] < r - l: l, r = a[i][0], a[j][0] if m[a[i][1]] == 1: del m[a[i][1]] else: m[a[i][1]] -= 1 i += 1 return [l, r]
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR NUMBER DICT ASSIGN VAR VAR VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER WHILE VAR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER VAR NUMBER RETURN LIST VAR VAR
Given a BST (Binary Search Tree), find the shortest range [x, y], such that, at least one node of every level of the BST lies in the range. If there are multiple ranges with the same gap (i.e. (y-x)) return the range with the smallest x. Example 1: Input: 8 / \ 3 10 / \ \ 2 6 14 / \ / 4 7 12 / \ 11 13 Output: 6 11 Explanation: Level order traversal of the tree is [8], [3, 10], [2, 6, 14], [4, 7, 12], [11, 13]. The shortest range which satisfies the above mentioned condition is [6, 11]. Example 2: Input: 12 \ 13 \ 14 \ 15 \ 16 Output: 12 16 Explanation: Each level contains one node, so the shortest range is [12, 16]. Your Task: You don't need to read input or print anything. Complete the function shortestRange() which takes the root of the tree as an input parameter and returns the pair of numbers Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ Node Value ≤ 10^{5}
class Solution: def inorder(self, node, level, output): if node is None: return level height = self.inorder(node.left, level + 1, output) output.append((node.data, level)) height = max(height, self.inorder(node.right, level + 1, output)) return height def shortestRange(self, root): nodes = [] height = self.inorder(root, 0, nodes) level_count = [0] * height right = -1 count = 0 while count < height: right += 1 if level_count[nodes[right][1]] == 0: count += 1 level_count[nodes[right][1]] += 1 left = 0 while level_count[nodes[left][1]] > 1: level_count[nodes[left][1]] -= 1 left += 1 min_ = 0 max_ = 100000 while True: if nodes[right][0] - nodes[left][0] < max_ - min_: min_ = nodes[left][0] max_ = nodes[right][0] right += 1 if right == len(nodes): return [min_, max_] level_count[nodes[right][1]] += 1 while level_count[nodes[left][1]] > 1: level_count[nodes[left][1]] -= 1 left += 1
CLASS_DEF FUNC_DEF IF VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER NUMBER VAR NUMBER VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN LIST VAR VAR VAR VAR VAR NUMBER NUMBER WHILE VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER VAR NUMBER
Given a BST (Binary Search Tree), find the shortest range [x, y], such that, at least one node of every level of the BST lies in the range. If there are multiple ranges with the same gap (i.e. (y-x)) return the range with the smallest x. Example 1: Input: 8 / \ 3 10 / \ \ 2 6 14 / \ / 4 7 12 / \ 11 13 Output: 6 11 Explanation: Level order traversal of the tree is [8], [3, 10], [2, 6, 14], [4, 7, 12], [11, 13]. The shortest range which satisfies the above mentioned condition is [6, 11]. Example 2: Input: 12 \ 13 \ 14 \ 15 \ 16 Output: 12 16 Explanation: Each level contains one node, so the shortest range is [12, 16]. Your Task: You don't need to read input or print anything. Complete the function shortestRange() which takes the root of the tree as an input parameter and returns the pair of numbers Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ Node Value ≤ 10^{5}
class Solution: def level_order(self, root): res = [] trav = [(root, 0)] while trav: cur, depth = trav.pop() if cur.right: trav.append((cur.right, depth + 1)) if cur.left: trav.append((cur.left, depth + 1)) if depth > len(res) - 1: res.append([cur.data]) else: res[depth].append(cur.data) return res def shortestRange(self, root): KSortedArray = self.level_order(root) k = len(KSortedArray) arr = [] for i in range(len(KSortedArray)): for j in KSortedArray[i]: arr.append((j, i)) arr.sort() j = 0 counts = {} total = 0 ans = () diff = 10000000.0 for i in range(len(arr)): if arr[i][1] not in counts: total += 1 counts[arr[i][1]] = 1 else: counts[arr[i][1]] += 1 if total == k: while total == k: if counts[arr[j][1]] > 1: counts[arr[j][1]] -= 1 j += 1 else: break if arr[i][0] - arr[j][0] < diff: ans = arr[j][0], arr[i][0] diff = arr[i][0] - arr[j][0] return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST VAR NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER IF VAR VAR WHILE VAR VAR IF VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR VAR NUMBER RETURN VAR
In an array A of 0s and 1s, how many non-empty subarrays have sum S?   Example 1: Input: A = [1,0,1,0,1], S = 2 Output: 4 Explanation: The 4 subarrays are bolded below: [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1]   Note: A.length <= 30000 0 <= S <= A.length A[i] is either 0 or 1.
class Solution: def numSubarraysWithSum(self, A: List[int], S: int) -> int: count = {(0): 1} total = maxS = 0 for num in A: total += num maxS += count.get(total - S, 0) count[total] = count.get(total, 0) + 1 return maxS
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR VAR
In an array A of 0s and 1s, how many non-empty subarrays have sum S?   Example 1: Input: A = [1,0,1,0,1], S = 2 Output: 4 Explanation: The 4 subarrays are bolded below: [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1]   Note: A.length <= 30000 0 <= S <= A.length A[i] is either 0 or 1.
class Solution: def numSubarraysWithSum(self, A: List[int], S: int) -> int: hashmap = {(0): 1} preSum = 0 ans = 0 for i in range(len(A)): preSum += A[i] if preSum - S in hashmap: ans += hashmap[preSum - S] hashmap[preSum] = 1 if preSum not in hashmap else hashmap[preSum] + 1 return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER RETURN VAR VAR
In an array A of 0s and 1s, how many non-empty subarrays have sum S?   Example 1: Input: A = [1,0,1,0,1], S = 2 Output: 4 Explanation: The 4 subarrays are bolded below: [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1]   Note: A.length <= 30000 0 <= S <= A.length A[i] is either 0 or 1.
class Solution: def numSubarraysWithSum(self, A: List[int], S: int) -> int: dp = [] oneIndices = [-1] ans = 0 for i, num in enumerate(A): if num == 1: oneIndices.append(i) if len(oneIndices) >= S + 1: if S == 0: ans += i - oneIndices[-S - 1] else: ans += oneIndices[-S] - oneIndices[-S - 1] return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR
In an array A of 0s and 1s, how many non-empty subarrays have sum S?   Example 1: Input: A = [1,0,1,0,1], S = 2 Output: 4 Explanation: The 4 subarrays are bolded below: [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1]   Note: A.length <= 30000 0 <= S <= A.length A[i] is either 0 or 1.
class Solution: def numSubarraysWithSum(self, A: List[int], S: int) -> int: n = len(A) def num_subar_at_most(lim): subarr_ct = 0 r_sum = 0 li = 0 for ri in range(n): r_sum += A[ri] while li <= ri and r_sum > lim: r_sum -= A[li] li += 1 subarr_ct += ri - li + 1 return subarr_ct return num_subar_at_most(S) - num_subar_at_most(S - 1)
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR
In an array A of 0s and 1s, how many non-empty subarrays have sum S?   Example 1: Input: A = [1,0,1,0,1], S = 2 Output: 4 Explanation: The 4 subarrays are bolded below: [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1]   Note: A.length <= 30000 0 <= S <= A.length A[i] is either 0 or 1.
class Solution: def numSubarraysWithSum(self, A: List[int], S: int) -> int: ctr = collections.Counter({(0): 1}) prefix = res = 0 for a in A: prefix += a res += ctr[prefix - S] ctr[prefix] += 1 return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR DICT NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR VAR
In an array A of 0s and 1s, how many non-empty subarrays have sum S?   Example 1: Input: A = [1,0,1,0,1], S = 2 Output: 4 Explanation: The 4 subarrays are bolded below: [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1]   Note: A.length <= 30000 0 <= S <= A.length A[i] is either 0 or 1.
class Solution: def numSubarraysWithSum(self, A: List[int], S: int) -> int: csum = [0] for a in A: csum.append(csum[-1] + a) counter = collections.Counter() ans = 0 print(csum) for p in csum: ans += counter[p - S] counter[p] += 1 return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR VAR
In an array A of 0s and 1s, how many non-empty subarrays have sum S?   Example 1: Input: A = [1,0,1,0,1], S = 2 Output: 4 Explanation: The 4 subarrays are bolded below: [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1]   Note: A.length <= 30000 0 <= S <= A.length A[i] is either 0 or 1.
class Solution: def numSubarraysWithSum(self, nums: List[int], k: int) -> int: if not nums: return 0 counter = collections.defaultdict(int) counter[0] = 1 cumsum = 0 ans = 0 for i in range(len(nums)): cumsum += nums[i] if cumsum - k in counter: ans += counter[cumsum - k] counter[cumsum] += 1 return ans
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR VAR
In an array A of 0s and 1s, how many non-empty subarrays have sum S?   Example 1: Input: A = [1,0,1,0,1], S = 2 Output: 4 Explanation: The 4 subarrays are bolded below: [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1]   Note: A.length <= 30000 0 <= S <= A.length A[i] is either 0 or 1.
class Solution: def numSubarraysWithSum(self, A: List[int], S: int) -> int: def helper(k): if k < 0: return 0 num = 0 i = 0 for j in range(len(A)): k -= A[j] while k < 0: k += A[i] i += 1 num += j - i + 1 return num return helper(S) - helper(S - 1)
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR WHILE VAR NUMBER VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR
In an array A of 0s and 1s, how many non-empty subarrays have sum S?   Example 1: Input: A = [1,0,1,0,1], S = 2 Output: 4 Explanation: The 4 subarrays are bolded below: [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1]   Note: A.length <= 30000 0 <= S <= A.length A[i] is either 0 or 1.
class Solution: def numSubarraysWithSum(self, A: List[int], S: int) -> int: n = len(A) suma = [0] * (n + 1) for i in range(1, n + 1): suma[i] = suma[i - 1] + A[i - 1] left = 0 res = 0 m = collections.defaultdict(lambda: 0) m[0] = 1 for i in range(n): if suma[i + 1] - S in m: res += m[suma[i + 1] - S] m[suma[i + 1]] += 1 return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER RETURN VAR VAR
In an array A of 0s and 1s, how many non-empty subarrays have sum S?   Example 1: Input: A = [1,0,1,0,1], S = 2 Output: 4 Explanation: The 4 subarrays are bolded below: [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1]   Note: A.length <= 30000 0 <= S <= A.length A[i] is either 0 or 1.
class Solution: def numSubarraysWithSum(self, A: List[int], S: int) -> int: size = len(A) l, r, total, cnt = [0] * 4 if S == 0: l = 1 for x in A: if x == 0: cnt += l l += 1 else: l = 1 return cnt while r < size and total < S: total += A[r] r += 1 if r == size and total < S: return 0 while r < size: cnt1, cnt2 = 1, 1 while l < r and A[l] == 0: l += 1 cnt1 += 1 while r < size and A[r] == 0: r += 1 cnt2 += 1 cnt += cnt1 * cnt2 l += 1 r += 1 if A[-1] == 1: cnt += 1 while l < size and A[l] == 0: cnt += 1 l += 1 return cnt
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP LIST NUMBER NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER RETURN VAR WHILE VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR RETURN NUMBER WHILE VAR VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR
In an array A of 0s and 1s, how many non-empty subarrays have sum S?   Example 1: Input: A = [1,0,1,0,1], S = 2 Output: 4 Explanation: The 4 subarrays are bolded below: [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1]   Note: A.length <= 30000 0 <= S <= A.length A[i] is either 0 or 1.
class Solution: def numSubarraysWithSum(self, A: List[int], S: int) -> int: def atMost(S): ans, l, tmp = 0, 0, 0 for r in range(len(A)): tmp = tmp + A[r] while tmp > S and l <= r: tmp = tmp - A[l] l = l + 1 ans += r - l + 1 return ans return atMost(S) - atMost(S - 1)
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR WHILE VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR
In an array A of 0s and 1s, how many non-empty subarrays have sum S?   Example 1: Input: A = [1,0,1,0,1], S = 2 Output: 4 Explanation: The 4 subarrays are bolded below: [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1]   Note: A.length <= 30000 0 <= S <= A.length A[i] is either 0 or 1.
class Solution: def numSubarraysWithSum(self, A: List[int], S: int) -> int: n = len(A) i, j, total, res = 0, 0, 0, 0 while j < n: total += A[j] while i < j and total > S: total -= A[i] i += 1 if total == S: res += 1 k = i while k < j and A[k] == 0: k += 1 res += 1 j += 1 return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER WHILE VAR VAR VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR
In an array A of 0s and 1s, how many non-empty subarrays have sum S?   Example 1: Input: A = [1,0,1,0,1], S = 2 Output: 4 Explanation: The 4 subarrays are bolded below: [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1]   Note: A.length <= 30000 0 <= S <= A.length A[i] is either 0 or 1.
class Solution: def numSubarraysWithSum(self, A: List[int], S: int) -> int: A = [0] + list(itertools.accumulate(A)) hm = Counter() res = 0 for i in A: res += hm[i] hm[i + S] += 1 return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER RETURN VAR VAR
In an array A of 0s and 1s, how many non-empty subarrays have sum S?   Example 1: Input: A = [1,0,1,0,1], S = 2 Output: 4 Explanation: The 4 subarrays are bolded below: [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1]   Note: A.length <= 30000 0 <= S <= A.length A[i] is either 0 or 1.
class Solution: def numSubarraysWithSum(self, A: List[int], S: int) -> int: size = len(A) prefix = {(0): 1} cnt = 0 ans = 0 for i, num in enumerate(A): if num == 1: cnt += 1 if cnt - S in prefix: ans += prefix[cnt - S] prefix[cnt] = prefix.get(cnt, 0) + 1 return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR VAR
In an array A of 0s and 1s, how many non-empty subarrays have sum S?   Example 1: Input: A = [1,0,1,0,1], S = 2 Output: 4 Explanation: The 4 subarrays are bolded below: [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1]   Note: A.length <= 30000 0 <= S <= A.length A[i] is either 0 or 1.
class Solution: def numSubarraysWithSum(self, A: List[int], S: int) -> int: return self.at_most(A, S) - self.at_most(A, S - 1) def at_most(self, A, S): if S < 0: return 0 start = 0 res = 0 for end in range(len(A)): S -= A[end] while start < len(A) and S < 0: S += A[start] start += 1 res += end - start + 1 return res
CLASS_DEF FUNC_DEF VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR
In an array A of 0s and 1s, how many non-empty subarrays have sum S?   Example 1: Input: A = [1,0,1,0,1], S = 2 Output: 4 Explanation: The 4 subarrays are bolded below: [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1]   Note: A.length <= 30000 0 <= S <= A.length A[i] is either 0 or 1.
class Solution: def numSubarraysWithSum(self, A: List[int], S: int) -> int: cur = 0 res = 0 dp = collections.defaultdict(int) dp[0] = 1 for i in range(len(A)): cur += A[i] diff = cur - S if diff in dp: res += dp[diff] dp[cur] += 1 return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR
In an array A of 0s and 1s, how many non-empty subarrays have sum S?   Example 1: Input: A = [1,0,1,0,1], S = 2 Output: 4 Explanation: The 4 subarrays are bolded below: [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1]   Note: A.length <= 30000 0 <= S <= A.length A[i] is either 0 or 1.
class Solution: def numSubarraysWithSum(self, A: List[int], S: int) -> int: left = res = cur_sum = count = 0 for right in range(len(A)): cur_sum += A[right] if A[right] == 1: count = 0 while left <= right and cur_sum >= S: if cur_sum == S: count += 1 cur_sum -= A[left] left += 1 res += count return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR RETURN VAR VAR
In an array A of 0s and 1s, how many non-empty subarrays have sum S?   Example 1: Input: A = [1,0,1,0,1], S = 2 Output: 4 Explanation: The 4 subarrays are bolded below: [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1]   Note: A.length <= 30000 0 <= S <= A.length A[i] is either 0 or 1.
class Solution: def numSubarraysWithSum(self, pl, S): ans = 0 if S == 0: c = 0 for i in range(len(pl)): if pl[i] == 0: c += 1 else: c = 0 ans += c return ans l = [-1] for i in range(len(pl)): if pl[i] == 1: l.append(i) l.append(len(pl)) ans = 0 for i in range(1, len(l) - S): ans += (l[i] - l[i - 1]) * (l[i + S] - l[i + S - 1]) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR VAR RETURN VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR
In an array A of 0s and 1s, how many non-empty subarrays have sum S?   Example 1: Input: A = [1,0,1,0,1], S = 2 Output: 4 Explanation: The 4 subarrays are bolded below: [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1]   Note: A.length <= 30000 0 <= S <= A.length A[i] is either 0 or 1.
class Solution: def numSubarraysWithSum(self, A: List[int], S: int) -> int: if S == 0 and sum(A) == 0: return len(A) * (len(A) + 1) // 2 def atmost(s): if s < 0: return 0 sumi = s l = 0 r = 0 count = 0 while r < len(A): sumi -= A[r] while sumi < 0: sumi += A[l] l += 1 count += r - l + 1 r += 1 return count return atmost(S) - atmost(S - 1)
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER FUNC_CALL VAR VAR NUMBER RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR WHILE VAR NUMBER VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR
In an array A of 0s and 1s, how many non-empty subarrays have sum S?   Example 1: Input: A = [1,0,1,0,1], S = 2 Output: 4 Explanation: The 4 subarrays are bolded below: [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1]   Note: A.length <= 30000 0 <= S <= A.length A[i] is either 0 or 1.
class Solution: def numSubarraysWithSum(self, A: List[int], S: int) -> int: cumsum = 0 dic = {(0): [-1]} ans = 0 for i, num in enumerate(A): cumsum += num dic[cumsum] = dic.get(cumsum, []) + [i] ans += len(dic.get(cumsum - S, [])) return ans - (0 if S else len(A))
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER LIST NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR LIST LIST VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR LIST RETURN BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR
In an array A of 0s and 1s, how many non-empty subarrays have sum S?   Example 1: Input: A = [1,0,1,0,1], S = 2 Output: 4 Explanation: The 4 subarrays are bolded below: [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1]   Note: A.length <= 30000 0 <= S <= A.length A[i] is either 0 or 1.
class Solution: def numSubarraysWithSum(self, A: List[int], S: int) -> int: numToFreq = {} numToFreq[0] = 1 now_sum = 0 ans = 0 for num in A: now_sum += num if now_sum == S: ans += numToFreq[0] elif now_sum > S: if now_sum - S in numToFreq: ans += numToFreq[now_sum - S] if now_sum not in numToFreq: numToFreq[now_sum] = 0 numToFreq[now_sum] += 1 return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER RETURN VAR VAR
In an array A of 0s and 1s, how many non-empty subarrays have sum S?   Example 1: Input: A = [1,0,1,0,1], S = 2 Output: 4 Explanation: The 4 subarrays are bolded below: [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1]   Note: A.length <= 30000 0 <= S <= A.length A[i] is either 0 or 1.
class Solution: def numSubarraysWithSum(self, A, S): return self.numSubarraysAtMostSum(A, S) - self.numSubarraysAtMostSum(A, S - 1) def numSubarraysAtMostSum(self, A, S): if S < 0: return 0 if not A: return 0 n = len(A) right = 0 now_sum = 0 res = 0 for left in range(n): while right <= n - 1 and (now_sum < S or A[right] == 0): now_sum += A[right] right += 1 res += right - left now_sum -= A[left] return res
CLASS_DEF FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR RETURN VAR
In an array A of 0s and 1s, how many non-empty subarrays have sum S?   Example 1: Input: A = [1,0,1,0,1], S = 2 Output: 4 Explanation: The 4 subarrays are bolded below: [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1]   Note: A.length <= 30000 0 <= S <= A.length A[i] is either 0 or 1.
class Solution: def numSubarraysWithSum(self, A: List[int], S: int) -> int: n = len(A) prefix = [(0) for _ in range(n + 1)] for i in range(n): prefix[i + 1] = prefix[i] + A[i] def at_most_k(k: int) -> int: begin, end = 0, 1 cnt = 0 while end < n + 1: while begin < end and prefix[end] - prefix[begin] > k: begin += 1 cnt += end - begin end += 1 return cnt return at_most_k(S) - at_most_k(S - 1)
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR FUNC_DEF VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER WHILE VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR
In an array A of 0s and 1s, how many non-empty subarrays have sum S?   Example 1: Input: A = [1,0,1,0,1], S = 2 Output: 4 Explanation: The 4 subarrays are bolded below: [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1]   Note: A.length <= 30000 0 <= S <= A.length A[i] is either 0 or 1.
class Solution: def numSubarraysWithSum(self, A: List[int], S: int) -> int: def findZerosBefore(index, array): zerosBefore = 0 index -= 1 while index >= 0 and array[index] != 1: zerosBefore += 1 index -= 1 return zerosBefore def findZerosAfter(index, array): zerosAfter = 0 index += 1 while index < len(array) and array[index] != 1: zerosAfter += 1 index += 1 return zerosAfter def zeroString(index, array): zeros = 0 while index < len(array) and array[index] != 1: zeros += 1 index += 1 return zeros def zeroCombs(n): combs = 0 for i in range(n, 0, -1): combs = combs + n - i + 1 return combs def firstZero(index, array): if index == 0: return True if array[index - 1] == 1: return True else: return False total = 0 if S > 0: one_array = [i for i, one in enumerate(A) if one == 1] print(one_array) start_index = [] end_index = [] for n in range(len(one_array)): if n + S - 1 < len(one_array): start_index.append(one_array[n]) end_index.append(one_array[n + (S - 1)]) print(start_index) print(end_index) for i in range(len(start_index)): Bef = findZerosBefore(start_index[i], A) print(Bef) Aft = findZerosAfter(end_index[i], A) total = total + Bef + Aft + Bef * Aft + 1 if S == 0: zero_array = [i for i, zero in enumerate(A) if zero == 0] for n in range(len(zero_array)): if firstZero(zero_array[n], A): sticky = zeroString(zero_array[n], A) total = int(total + (sticky**2 + sticky) / 2) return total
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR NUMBER WHILE VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER RETURN VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR BIN_OP VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER RETURN VAR VAR
In an array A of 0s and 1s, how many non-empty subarrays have sum S?   Example 1: Input: A = [1,0,1,0,1], S = 2 Output: 4 Explanation: The 4 subarrays are bolded below: [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1]   Note: A.length <= 30000 0 <= S <= A.length A[i] is either 0 or 1.
class Solution: def f(self, A): ret = 0 cnt = 0 for a in A: if not cnt: if not a: cnt += 1 elif not a: cnt += 1 else: ret += cnt * (cnt + 1) // 2 cnt = 0 if cnt: ret += cnt * (cnt + 1) // 2 return ret pass def numSubarraysWithSum(self, A: List[int], k: int) -> int: ret = 0 if not k: return self.f(A) mp = {(0): 1} cnt = 0 for a in A: cnt += a if cnt not in mp: mp[cnt] = 0 mp[cnt] += 1 if cnt - k in mp: ret += mp[cnt - k] return ret
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR IF VAR VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN VAR FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER IF VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR VAR
In an array A of 0s and 1s, how many non-empty subarrays have sum S?   Example 1: Input: A = [1,0,1,0,1], S = 2 Output: 4 Explanation: The 4 subarrays are bolded below: [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1]   Note: A.length <= 30000 0 <= S <= A.length A[i] is either 0 or 1.
class Solution: def numSubarraysWithSum(self, A: List[int], S: int) -> int: i, s, res, count = 0, 0, 0, 1 for j in range(len(A)): s += A[j] while s > S: s -= A[i] i += 1 count = 1 while i < j and A[i] == 0: i += 1 count += 1 if s == S and i <= j: res += count return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR RETURN VAR VAR
In an array A of 0s and 1s, how many non-empty subarrays have sum S?   Example 1: Input: A = [1,0,1,0,1], S = 2 Output: 4 Explanation: The 4 subarrays are bolded below: [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1]   Note: A.length <= 30000 0 <= S <= A.length A[i] is either 0 or 1.
class Solution: def numSubarraysWithSum(self, A: List[int], S: int) -> int: ans = 0 il, ih, sl, sh = 0, 0, 0, 0 for j, x in enumerate(A): sl += x while il < j and sl > S: sl -= A[il] il += 1 sh += x while ih < j and (sh > S or sh == S and not A[ih]): sh -= A[ih] ih += 1 if sl == S: ans += ih - il + 1 return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR
In an array A of 0s and 1s, how many non-empty subarrays have sum S?   Example 1: Input: A = [1,0,1,0,1], S = 2 Output: 4 Explanation: The 4 subarrays are bolded below: [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1] [1,0,1,0,1]   Note: A.length <= 30000 0 <= S <= A.length A[i] is either 0 or 1.
class Solution: def numSubarraysWithSum(self, A: List[int], S: int) -> int: d = {(0): 1} ss = 0 for i in A: ss += i d[ss] = d.get(ss, 0) + 1 res = 0 print(d) if S > 0: for i in range(ss - S + 1): res += d[i] * d[i + S] if S == 0: for i in range(ss + 1): res += d[i] * (d[i] - 1) // 2 return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER NUMBER RETURN VAR VAR
Given an array A of 0s and 1s, we may change up to K values from 0 to 1. Return the length of the longest (contiguous) subarray that contains only 1s.    Example 1: Input: A = [1,1,1,0,0,0,1,1,1,1,0], K = 2 Output: 6 Explanation: [1,1,1,0,0,1,1,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. Example 2: Input: A = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3 Output: 10 Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.   Note: 1 <= A.length <= 20000 0 <= K <= A.length A[i] is 0 or 1
class Solution: def longestOnes(self, A: List[int], K: int) -> int: k = K j = ans = 0 for i in range(len(A)): if not A[i]: k -= 1 while k < 0: if not A[j]: k += 1 j += 1 ans = max(ans, i - j + 1) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR
Given an array A of 0s and 1s, we may change up to K values from 0 to 1. Return the length of the longest (contiguous) subarray that contains only 1s.    Example 1: Input: A = [1,1,1,0,0,0,1,1,1,1,0], K = 2 Output: 6 Explanation: [1,1,1,0,0,1,1,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. Example 2: Input: A = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3 Output: 10 Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.   Note: 1 <= A.length <= 20000 0 <= K <= A.length A[i] is 0 or 1
class Solution: def longestOnes(self, A: List[int], K: int) -> int: hashMap = {} windowStart = 0 m = 0 hashMap[1] = 0 for windowEnd in range(len(A)): if A[windowEnd] not in hashMap: hashMap[A[windowEnd]] = 0 hashMap[A[windowEnd]] += 1 while sum(hashMap.values()) - hashMap[1] > K: hashMap[A[windowStart]] -= 1 windowStart += 1 m = max(m, windowEnd - windowStart + 1) return m
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER WHILE BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR
Given an array A of 0s and 1s, we may change up to K values from 0 to 1. Return the length of the longest (contiguous) subarray that contains only 1s.    Example 1: Input: A = [1,1,1,0,0,0,1,1,1,1,0], K = 2 Output: 6 Explanation: [1,1,1,0,0,1,1,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. Example 2: Input: A = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3 Output: 10 Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.   Note: 1 <= A.length <= 20000 0 <= K <= A.length A[i] is 0 or 1
class Solution: def longestOnes(self, A: List[int], K: int) -> int: l = 0 r = 0 max_count = K while l < len(A) and r < len(A): while r < len(A): if A[r] == 1: r += 1 elif K > 0: r += 1 K -= 1 else: break if r - l + 1 > max_count: max_count = r - l print(l, r, max_count) if A[l] == 0: K += 1 l += 1 return max_count
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR
Given an array A of 0s and 1s, we may change up to K values from 0 to 1. Return the length of the longest (contiguous) subarray that contains only 1s.    Example 1: Input: A = [1,1,1,0,0,0,1,1,1,1,0], K = 2 Output: 6 Explanation: [1,1,1,0,0,1,1,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. Example 2: Input: A = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3 Output: 10 Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.   Note: 1 <= A.length <= 20000 0 <= K <= A.length A[i] is 0 or 1
class Solution: def longestOnes(self, A: List[int], K: int) -> int: pre_sum = [A[0]] for i in range(1, len(A)): pre_sum.append(pre_sum[-1] + A[i]) def cal(l, r): if l == 0: return pre_sum[r] else: return pre_sum[r] - pre_sum[l - 1] l = 0 r = 0 ans = K while l < len(A): while r < len(A) and r - l + 1 - cal(l, r) <= K: ans = max(ans, r - l + 1) r = r + 1 if r == len(A): break while l < len(A) and r - l + 1 - cal(l, r) > K: l = l + 1 return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_DEF IF VAR NUMBER RETURN VAR VAR RETURN BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
Given an array A of 0s and 1s, we may change up to K values from 0 to 1. Return the length of the longest (contiguous) subarray that contains only 1s.    Example 1: Input: A = [1,1,1,0,0,0,1,1,1,1,0], K = 2 Output: 6 Explanation: [1,1,1,0,0,1,1,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. Example 2: Input: A = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3 Output: 10 Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.   Note: 1 <= A.length <= 20000 0 <= K <= A.length A[i] is 0 or 1
class Solution: def longestOnes(self, A: List[int], K: int) -> int: count = 0 maxCount = -1 first = 0 for i in range(len(A)): if A[i] == 0 and K != 0: count += 1 K -= 1 elif A[i] == 0 and K == 0: while A[first] != 0: first += 1 count -= 1 first += 1 else: count += 1 maxCount = max(maxCount, count) return maxCount
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
Given an array A of 0s and 1s, we may change up to K values from 0 to 1. Return the length of the longest (contiguous) subarray that contains only 1s.    Example 1: Input: A = [1,1,1,0,0,0,1,1,1,1,0], K = 2 Output: 6 Explanation: [1,1,1,0,0,1,1,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. Example 2: Input: A = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3 Output: 10 Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.   Note: 1 <= A.length <= 20000 0 <= K <= A.length A[i] is 0 or 1
class Solution: def longestOnes(self, A: List[int], K: int) -> int: le, ri = 0, 0 max_len = 0 while ri < len(A): if A[ri] == 0: K -= 1 ri += 1 if K >= 0: max_len = max(max_len, ri - le) print((le, ri)) while K < 0 and le < ri: if A[le] == 0: K += 1 le += 1 return max_len
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR WHILE VAR NUMBER VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR
Given an array A of 0s and 1s, we may change up to K values from 0 to 1. Return the length of the longest (contiguous) subarray that contains only 1s.    Example 1: Input: A = [1,1,1,0,0,0,1,1,1,1,0], K = 2 Output: 6 Explanation: [1,1,1,0,0,1,1,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. Example 2: Input: A = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3 Output: 10 Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.   Note: 1 <= A.length <= 20000 0 <= K <= A.length A[i] is 0 or 1
class Solution: def longestOnes(self, A: List[int], K: int) -> int: left, right = 0, 0 n, c1, c0 = len(A), 0, 0 ans = 0 while True: if c0 <= K: ans = max(ans, right - left) if right < n: right += 1 x = A[right - 1] if x: c1 += 1 else: c0 += 1 else: return ans else: x = A[left] left += 1 if x: c1 -= 1 else: c0 -= 1
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR
Given an array A of 0s and 1s, we may change up to K values from 0 to 1. Return the length of the longest (contiguous) subarray that contains only 1s.    Example 1: Input: A = [1,1,1,0,0,0,1,1,1,1,0], K = 2 Output: 6 Explanation: [1,1,1,0,0,1,1,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. Example 2: Input: A = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3 Output: 10 Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.   Note: 1 <= A.length <= 20000 0 <= K <= A.length A[i] is 0 or 1
class Solution: def findMaxConsecutiveOnes(self, nums: List[int]) -> int: if len(nums) == 0: return 0 left = 0 while left < len(nums) and nums[left] != 1: left += 1 right = left while right + 1 < len(nums) and nums[right + 1] == 1: right += 1 if right == len(nums): return 0 maxOnes = right - left + 1 while left < len(nums): left = right + 1 while left < len(nums) and nums[left] != 1: left += 1 right = left while right + 1 < len(nums) and nums[right + 1] == 1: right += 1 maxOnes = max(maxOnes, right - left + 1) return maxOnes def longestOnes(self, A: List[int], K: int) -> int: if K == 0: return self.findMaxConsecutiveOnes(A) if len(A) == 0: return 0 left, right = 0, 0 if A[right] == 0: K -= 1 while right + 1 < len(A) and K > 0: right += 1 if A[right] == 0: K -= 1 while right + 1 < len(A) and A[right + 1] == 1: right += 1 maxSize = right - left + 1 while right < len(A): while left < len(A) and A[left] != 0: left += 1 if left < len(A): left += 1 else: return maxSize right += 1 while right + 1 < len(A) and A[right + 1] == 1: right += 1 maxSize = max(maxSize, right - left + 1) return maxSize
CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER NUMBER IF VAR VAR NUMBER VAR NUMBER WHILE BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER WHILE BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR NUMBER WHILE BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR
Given an array A of 0s and 1s, we may change up to K values from 0 to 1. Return the length of the longest (contiguous) subarray that contains only 1s.    Example 1: Input: A = [1,1,1,0,0,0,1,1,1,1,0], K = 2 Output: 6 Explanation: [1,1,1,0,0,1,1,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. Example 2: Input: A = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3 Output: 10 Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.   Note: 1 <= A.length <= 20000 0 <= K <= A.length A[i] is 0 or 1
class Solution: def longestOnes(self, A: List[int], K: int) -> int: l = 0 r = 0 m = len(A) count = 0 max_count = 0 while r < m: if A[r] == 1: count += 1 while r - l + 1 - count > K: if A[l] == 1: count -= 1 l += 1 max_count = max(r - l + 1, max_count) r += 1 return max_count
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR NUMBER WHILE BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER RETURN VAR VAR
Given an array A of 0s and 1s, we may change up to K values from 0 to 1. Return the length of the longest (contiguous) subarray that contains only 1s.    Example 1: Input: A = [1,1,1,0,0,0,1,1,1,1,0], K = 2 Output: 6 Explanation: [1,1,1,0,0,1,1,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. Example 2: Input: A = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3 Output: 10 Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.   Note: 1 <= A.length <= 20000 0 <= K <= A.length A[i] is 0 or 1
class Solution: def longestOnes(self, A: List[int], K: int) -> int: l = 0 for r in range(len(A)): if A[r] == 0: K -= 1 if K < 0: if A[l] == 0: K += 1 l += 1 return r - l + 1
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR NUMBER VAR
Given an array A of 0s and 1s, we may change up to K values from 0 to 1. Return the length of the longest (contiguous) subarray that contains only 1s.    Example 1: Input: A = [1,1,1,0,0,0,1,1,1,1,0], K = 2 Output: 6 Explanation: [1,1,1,0,0,1,1,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. Example 2: Input: A = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3 Output: 10 Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.   Note: 1 <= A.length <= 20000 0 <= K <= A.length A[i] is 0 or 1
class Solution: def longestOnes(self, A: List[int], K: int) -> int: i, j = 0, 0 count = 0 out = 0 for i in range(len(A)): if A[i] == 0: count += 1 while count > K and j < len(A): if A[j] == 0: count -= 1 j += 1 out = max(out, i - j + 1) return out
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR
Given an array A of 0s and 1s, we may change up to K values from 0 to 1. Return the length of the longest (contiguous) subarray that contains only 1s.    Example 1: Input: A = [1,1,1,0,0,0,1,1,1,1,0], K = 2 Output: 6 Explanation: [1,1,1,0,0,1,1,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. Example 2: Input: A = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3 Output: 10 Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.   Note: 1 <= A.length <= 20000 0 <= K <= A.length A[i] is 0 or 1
class Solution: def longestOnes(self, A: List[int], K: int) -> int: i, res = 0, 0 for j in range(len(A)): K -= 1 - A[j] while K < 0: K += 1 - A[i] i += 1 res = max(res, j - i + 1) return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR VAR WHILE VAR NUMBER VAR BIN_OP NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR
Given an array A of 0s and 1s, we may change up to K values from 0 to 1. Return the length of the longest (contiguous) subarray that contains only 1s.    Example 1: Input: A = [1,1,1,0,0,0,1,1,1,1,0], K = 2 Output: 6 Explanation: [1,1,1,0,0,1,1,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. Example 2: Input: A = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3 Output: 10 Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.   Note: 1 <= A.length <= 20000 0 <= K <= A.length A[i] is 0 or 1
class Solution: def longestOnes(self, A, K): if not A: return 0 n = len(A) right = 0 cnt = 0 res = 0 for left in range(n): while right <= n - 1 and (cnt < K or A[right] == 1): cnt += A[right] == 0 right += 1 res = max(res, right - left) cnt -= A[left] == 0 return res
CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER RETURN VAR
Given an array A of 0s and 1s, we may change up to K values from 0 to 1. Return the length of the longest (contiguous) subarray that contains only 1s.    Example 1: Input: A = [1,1,1,0,0,0,1,1,1,1,0], K = 2 Output: 6 Explanation: [1,1,1,0,0,1,1,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. Example 2: Input: A = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3 Output: 10 Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.   Note: 1 <= A.length <= 20000 0 <= K <= A.length A[i] is 0 or 1
class Solution: def longestOnes(self, A: List[int], K: int) -> int: hulu = [] cnt = 0 num = A[0] for x in A: if x == num: cnt += 1 else: hulu.append([num, cnt]) cnt = 1 num = x if cnt > 0: hulu.append([num, cnt]) output = 0 if A[0] == 1: start = 0 else: start = 1 if len(hulu) < 2: return min(K, len(A)) end = start usage = 0 ones = hulu[start][1] while end + 2 < len(hulu) and usage + hulu[end + 1][1] <= K: usage += hulu[end + 1][1] ones += hulu[end + 2][1] end += 2 output = ones + K start += 2 while start < len(hulu): ones -= hulu[start - 2][1] usage -= hulu[start - 1][1] if start > end: end = start ones = hulu[start][1] usage = 0 while end + 2 < len(hulu) and usage + hulu[end + 1][1] <= K: usage += hulu[end + 1][1] ones += hulu[end + 2][1] end += 2 output = max(output, ones + K) start += 2 return min(output, len(A))
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR
Given an array A of 0s and 1s, we may change up to K values from 0 to 1. Return the length of the longest (contiguous) subarray that contains only 1s.    Example 1: Input: A = [1,1,1,0,0,0,1,1,1,1,0], K = 2 Output: 6 Explanation: [1,1,1,0,0,1,1,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. Example 2: Input: A = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3 Output: 10 Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.   Note: 1 <= A.length <= 20000 0 <= K <= A.length A[i] is 0 or 1
class Solution: def longestOnes(self, A: List[int], K: int) -> int: hash_count = dict() hash_count[1] = 0 start = 0 max_count = 0 max_result = 0 for end in range(len(A)): if A[end] == 1: hash_count[A[end]] += 1 max_count = max(max_count, hash_count[1]) if max_count + K < end - start + 1: if A[start] == 1 and hash_count.get(1): hash_count[1] -= 1 start += 1 max_result = max(max_result, end - start + 1) return max_result
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER FUNC_CALL VAR NUMBER VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR
Given an array A of 0s and 1s, we may change up to K values from 0 to 1. Return the length of the longest (contiguous) subarray that contains only 1s.    Example 1: Input: A = [1,1,1,0,0,0,1,1,1,1,0], K = 2 Output: 6 Explanation: [1,1,1,0,0,1,1,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. Example 2: Input: A = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3 Output: 10 Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.   Note: 1 <= A.length <= 20000 0 <= K <= A.length A[i] is 0 or 1
class Solution: def longestOnes(self, A: List[int], K: int) -> int: subarrays = [] start = -1 i = 0 while i < len(A): if A[i] == 1: if start == -1: start = i elif start != -1: subarrays.append([start, i]) start = -1 i += 1 if A[i - 1] == 1: subarrays.append([start, i]) print(subarrays) if K == 0: result = 0 for arr in subarrays: if arr[1] - arr[0] > result: result = arr[1] - arr[0] return result window = [] start = [] end = [] result = 0 for i in range(len(A)): if A[i] == 0: if len(window) == K: window.pop(0) window.append(i) left = window[0] - 1 right = window[-1] + 1 while left >= 0 and A[left] != 0: left -= 1 while right < len(A) and A[right] != 0: right += 1 if right - left > result: result = right - left return result - 1
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER WHILE VAR NUMBER VAR VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN BIN_OP VAR NUMBER VAR
Given an array A of 0s and 1s, we may change up to K values from 0 to 1. Return the length of the longest (contiguous) subarray that contains only 1s.    Example 1: Input: A = [1,1,1,0,0,0,1,1,1,1,0], K = 2 Output: 6 Explanation: [1,1,1,0,0,1,1,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. Example 2: Input: A = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3 Output: 10 Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.   Note: 1 <= A.length <= 20000 0 <= K <= A.length A[i] is 0 or 1
class Solution: def longestOnes(self, A: List[int], K: int) -> int: count = maxS = j = 0 for i, a in enumerate(A): if a == 0: count += 1 while j < len(A) and count > K: if A[j] == 0: count -= 1 j += 1 maxS = max(maxS, i - j + 1) return maxS
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR
Given an array A of 0s and 1s, we may change up to K values from 0 to 1. Return the length of the longest (contiguous) subarray that contains only 1s.    Example 1: Input: A = [1,1,1,0,0,0,1,1,1,1,0], K = 2 Output: 6 Explanation: [1,1,1,0,0,1,1,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. Example 2: Input: A = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3 Output: 10 Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.   Note: 1 <= A.length <= 20000 0 <= K <= A.length A[i] is 0 or 1
class Solution: def longestOnes(self, A: List[int], K: int) -> int: windowstart, most_common, res = 0, 0, 0 currentwindow = {(1): 0, (0): 0} for windowend in range(len(A)): currentwindow[A[windowend]] += 1 most_common = currentwindow[1] if windowend - windowstart + 1 - most_common > K: currentwindow[A[windowstart]] -= 1 windowstart += 1 res = max(res, windowend - windowstart + 1) return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR
Given an array A of 0s and 1s, we may change up to K values from 0 to 1. Return the length of the longest (contiguous) subarray that contains only 1s.    Example 1: Input: A = [1,1,1,0,0,0,1,1,1,1,0], K = 2 Output: 6 Explanation: [1,1,1,0,0,1,1,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. Example 2: Input: A = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3 Output: 10 Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.   Note: 1 <= A.length <= 20000 0 <= K <= A.length A[i] is 0 or 1
class Solution: def longestOnes(self, A: List[int], K: int) -> int: lo = 0 hi = len(A) A = [0] + [(1 - x) for x in A] n = len(A) for i in range(1, n): A[i] += A[i - 1] while lo < hi: mid = (lo + hi + 1) // 2 if min(A[i] - A[i - mid] for i in range(mid, len(A))) <= K: lo = mid else: hi = mid - 1 return lo
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
Given an array A of 0s and 1s, we may change up to K values from 0 to 1. Return the length of the longest (contiguous) subarray that contains only 1s.    Example 1: Input: A = [1,1,1,0,0,0,1,1,1,1,0], K = 2 Output: 6 Explanation: [1,1,1,0,0,1,1,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. Example 2: Input: A = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3 Output: 10 Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.   Note: 1 <= A.length <= 20000 0 <= K <= A.length A[i] is 0 or 1
class Solution: def longestOnes(self, A, K: int) -> int: ans = count = 0 j = 0 for i in range(len(A)): if i > 0 and A[i - 1] == 0: count -= 1 while j < len(A) and (A[j] or count < K): if A[j] == 1: j += 1 elif count < K: count += 1 j += 1 ans = max(ans, j - i) return ans
CLASS_DEF FUNC_DEF VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR VAR
Given an array A of 0s and 1s, we may change up to K values from 0 to 1. Return the length of the longest (contiguous) subarray that contains only 1s.    Example 1: Input: A = [1,1,1,0,0,0,1,1,1,1,0], K = 2 Output: 6 Explanation: [1,1,1,0,0,1,1,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. Example 2: Input: A = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3 Output: 10 Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.   Note: 1 <= A.length <= 20000 0 <= K <= A.length A[i] is 0 or 1
class Solution: def longestOnes(self, A: List[int], K: int) -> int: st, ed = 0, 0 ans = 0 longest = 0 k = 0 while ed < len(A): while ed < len(A): if A[ed] == 1: longest += 1 elif k < K: k += 1 longest += 1 else: break ed += 1 ans = max(longest, ans) while st <= ed and st < len(A): longest -= 1 if A[st] == 0: k -= 1 st += 1 break st += 1 return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR
Given an array A of 0s and 1s, we may change up to K values from 0 to 1. Return the length of the longest (contiguous) subarray that contains only 1s.    Example 1: Input: A = [1,1,1,0,0,0,1,1,1,1,0], K = 2 Output: 6 Explanation: [1,1,1,0,0,1,1,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. Example 2: Input: A = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3 Output: 10 Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.   Note: 1 <= A.length <= 20000 0 <= K <= A.length A[i] is 0 or 1
class Solution: def longestOnes(self, A: List[int], K: int) -> int: start = 0 end = 0 l = len(A) count = 0 max_len = 0 while end < l: if A[end] == 1: max_len = max(max_len, end - start + 1) end = end + 1 elif count < K: max_len = max(max_len, end - start + 1) count = count + 1 end = end + 1 else: while start <= end and count >= K: if A[start] == 0: count = count - 1 start = start + 1 return max_len
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
Given an array A of 0s and 1s, we may change up to K values from 0 to 1. Return the length of the longest (contiguous) subarray that contains only 1s.    Example 1: Input: A = [1,1,1,0,0,0,1,1,1,1,0], K = 2 Output: 6 Explanation: [1,1,1,0,0,1,1,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. Example 2: Input: A = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3 Output: 10 Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.   Note: 1 <= A.length <= 20000 0 <= K <= A.length A[i] is 0 or 1
class Solution: def longestOnes(self, A: List[int], K: int) -> int: left = 0 maxLen = 0 oneFreq = 0 maxFreq = 0 for right in range(len(A)): oneFreq += A[right] maxFreq = max(maxFreq, oneFreq) if right - left + 1 - maxFreq > K: oneFreq -= A[left] left += 1 maxLen = max(maxLen, right - left + 1) return maxLen
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR
Given an array A of 0s and 1s, we may change up to K values from 0 to 1. Return the length of the longest (contiguous) subarray that contains only 1s.    Example 1: Input: A = [1,1,1,0,0,0,1,1,1,1,0], K = 2 Output: 6 Explanation: [1,1,1,0,0,1,1,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. Example 2: Input: A = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3 Output: 10 Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.   Note: 1 <= A.length <= 20000 0 <= K <= A.length A[i] is 0 or 1
class Solution: def longestOnes(self, A: List[int], K: int) -> int: max_length = 0 start = 0 end = 1 zeros = 0 if len(A) == 0: return 0 if len(A) == 1: return 1 zeros = 2 - sum(A[:2]) while end < len(A): valid = zeros <= K if valid: max_length = max(max_length, end - start + 1) end += 1 if end == len(A): return max_length if A[end] == 0: zeros += 1 else: if A[start] == 0: zeros -= 1 start += 1 return max_length
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN VAR IF VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR
Given an array A of 0s and 1s, we may change up to K values from 0 to 1. Return the length of the longest (contiguous) subarray that contains only 1s.    Example 1: Input: A = [1,1,1,0,0,0,1,1,1,1,0], K = 2 Output: 6 Explanation: [1,1,1,0,0,1,1,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. Example 2: Input: A = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3 Output: 10 Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.   Note: 1 <= A.length <= 20000 0 <= K <= A.length A[i] is 0 or 1
class Solution: def longestOnes(self, A: List[int], K: int) -> int: if not A: return 0 best_window_len = 0 p, q = 0, 0 num_zeros = 0 while q < len(A): if A[q] == 0: num_zeros += 1 if num_zeros > K: best_window_len = max(q - p, best_window_len) while num_zeros > K: if A[p] == 0: num_zeros -= 1 p += 1 q += 1 best_window_len = max(q - p, best_window_len) return best_window_len
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR WHILE VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN VAR VAR
Given an array A of 0s and 1s, we may change up to K values from 0 to 1. Return the length of the longest (contiguous) subarray that contains only 1s.    Example 1: Input: A = [1,1,1,0,0,0,1,1,1,1,0], K = 2 Output: 6 Explanation: [1,1,1,0,0,1,1,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. Example 2: Input: A = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3 Output: 10 Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.   Note: 1 <= A.length <= 20000 0 <= K <= A.length A[i] is 0 or 1
class Solution: def longestOnes(self, A: List[int], K: int) -> int: ans, i, c = 0, 0, [] for j in range(len(A)): if A[j] == 0: c += (j,) while len(c) > K: i = c.pop(0) + 1 ans = max(ans, j - i + 1) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR
Given an array A of 0s and 1s, we may change up to K values from 0 to 1. Return the length of the longest (contiguous) subarray that contains only 1s.    Example 1: Input: A = [1,1,1,0,0,0,1,1,1,1,0], K = 2 Output: 6 Explanation: [1,1,1,0,0,1,1,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. Example 2: Input: A = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3 Output: 10 Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.   Note: 1 <= A.length <= 20000 0 <= K <= A.length A[i] is 0 or 1
class Solution: def longestOnes(self, A: List[int], K: int) -> int: n = len(A) res = 0 j, count0 = 0, 0 for i in range(n): if not A[i]: count0 += 1 if count0 > K: res = max(res, i - j) while A[j]: j += 1 j += 1 count0 -= 1 res = max(res, i - j + 1) return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR WHILE VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR
Given an array A of 0s and 1s, we may change up to K values from 0 to 1. Return the length of the longest (contiguous) subarray that contains only 1s.    Example 1: Input: A = [1,1,1,0,0,0,1,1,1,1,0], K = 2 Output: 6 Explanation: [1,1,1,0,0,1,1,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. Example 2: Input: A = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3 Output: 10 Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.   Note: 1 <= A.length <= 20000 0 <= K <= A.length A[i] is 0 or 1
class Solution: def longestOnes(self, A: List[int], K: int) -> int: ans = 0 l = 0 r = 0 prefix = [(0) for i in range(len(A))] for i in range(len(A)): if i == 0: prefix[i] = A[0] else: prefix[i] = A[i] + prefix[i - 1] if K >= len(A) - prefix[-1]: return len(A) while r < len(A) and l < len(A): while r < len(A): if l == 0: this_sum = prefix[r] else: this_sum = prefix[r] - prefix[l - 1] if r - l + 1 - this_sum > K: r -= 1 break r += 1 if r >= len(A): if r - l > ans: ans = r - l break if r - l + 1 > ans: ans = r - l + 1 while l < len(A) and A[l] == 1: l += 1 l += 1 return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR
Given an array A of 0s and 1s, we may change up to K values from 0 to 1. Return the length of the longest (contiguous) subarray that contains only 1s.    Example 1: Input: A = [1,1,1,0,0,0,1,1,1,1,0], K = 2 Output: 6 Explanation: [1,1,1,0,0,1,1,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. Example 2: Input: A = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3 Output: 10 Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.   Note: 1 <= A.length <= 20000 0 <= K <= A.length A[i] is 0 or 1
class Solution: def longestOnes(self, A: List[int], K: int) -> int: z_count = 0 l = 0 result = 0 for i in range(len(A)): if not A[i]: z_count += 1 if z_count <= K: result = max(result, i - l + 1) while l < i and z_count > K: if not A[l]: z_count -= 1 l += 1 return result
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
Given an array A of 0s and 1s, we may change up to K values from 0 to 1. Return the length of the longest (contiguous) subarray that contains only 1s.    Example 1: Input: A = [1,1,1,0,0,0,1,1,1,1,0], K = 2 Output: 6 Explanation: [1,1,1,0,0,1,1,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. Example 2: Input: A = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3 Output: 10 Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.   Note: 1 <= A.length <= 20000 0 <= K <= A.length A[i] is 0 or 1
class Solution: def longestOnes(self, A: List[int], K: int) -> int: left = 0 right = 0 k = K max_ans = 0 so_far = 0 while right < len(A): if A[right] == 1: right += 1 elif k > 0: right += 1 k -= 1 else: if A[left] == 0: k += 1 left += 1 max_ans = max(max_ans, right - left) return max_ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR VAR
Given an array A of 0s and 1s, we may change up to K values from 0 to 1. Return the length of the longest (contiguous) subarray that contains only 1s.    Example 1: Input: A = [1,1,1,0,0,0,1,1,1,1,0], K = 2 Output: 6 Explanation: [1,1,1,0,0,1,1,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. Example 2: Input: A = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3 Output: 10 Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.   Note: 1 <= A.length <= 20000 0 <= K <= A.length A[i] is 0 or 1
class Solution: def longestOnes(self, A: List[int], K: int) -> int: i, j, n = 0, 0, len(A) for x in A: j += 1 if x == 0: K -= 1 if K < 0: if A[i] == 0: K += 1 i += 1 return j - i
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR FOR VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN BIN_OP VAR VAR VAR
Given an array A of 0s and 1s, we may change up to K values from 0 to 1. Return the length of the longest (contiguous) subarray that contains only 1s.    Example 1: Input: A = [1,1,1,0,0,0,1,1,1,1,0], K = 2 Output: 6 Explanation: [1,1,1,0,0,1,1,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. Example 2: Input: A = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3 Output: 10 Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.   Note: 1 <= A.length <= 20000 0 <= K <= A.length A[i] is 0 or 1
class Solution: def longestOnes(self, a: List[int], K: int) -> int: i = 0 j = 0 ml = 0 while i < len(a): if a[i] == 0: K -= 1 while K < 0: if a[j] == 0: K += 1 j += 1 ml = max(ml, i - j + 1) i += 1 return ml
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER WHILE VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
Given an array A of 0s and 1s, we may change up to K values from 0 to 1. Return the length of the longest (contiguous) subarray that contains only 1s.    Example 1: Input: A = [1,1,1,0,0,0,1,1,1,1,0], K = 2 Output: 6 Explanation: [1,1,1,0,0,1,1,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. Example 2: Input: A = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3 Output: 10 Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.   Note: 1 <= A.length <= 20000 0 <= K <= A.length A[i] is 0 or 1
class Solution: def longestOnes(self, A: List[int], K: int) -> int: zeros_indices = [] for i in range(len(A)): if A[i] == 0: zeros_indices.append(i) longest_ones = 0 longest_yet = 0 for i in range(len(A)): if A[i] == 1: longest_yet += 1 longest_ones = max(longest_ones, longest_yet) else: longest_yet = 0 if K == 0: return longest_ones if K >= len(zeros_indices): return len(A) zero_order = -1 for i in range(len(A)): if A[i] == 0: zero_order += 1 right_cons_ones = left_cons_ones = 0 if zero_order + K < len(zeros_indices): nxt = zero_order + K right_cons_ones = zeros_indices[nxt] - zeros_indices[zero_order] else: nxt = len(zeros_indices) - 1 right_cons_ones = zeros_indices[nxt] - zeros_indices[zero_order] right_cons_ones += len(A) - zeros_indices[nxt] if not zero_order == 0: left_cons_ones = ( zeros_indices[zero_order] - zeros_indices[zero_order - 1] - 1 ) longest_ones = max(longest_ones, right_cons_ones + left_cons_ones) return longest_ones
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER RETURN VAR IF VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR VAR
Given an array A of 0s and 1s, we may change up to K values from 0 to 1. Return the length of the longest (contiguous) subarray that contains only 1s.    Example 1: Input: A = [1,1,1,0,0,0,1,1,1,1,0], K = 2 Output: 6 Explanation: [1,1,1,0,0,1,1,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. Example 2: Input: A = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3 Output: 10 Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.   Note: 1 <= A.length <= 20000 0 <= K <= A.length A[i] is 0 or 1
class Solution: def longestOnes(self, A: List[int], K: int) -> int: result = 0 zero_allowed = K left = 0 for right, x in enumerate(A): if x == 0: zero_allowed -= 1 if zero_allowed >= 0 and right - left + 1 > result: result = right - left + 1 while zero_allowed < 0 and right - left + 1 > result: if A[left] == 0: zero_allowed += 1 left += 1 return result
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR
Given an array A of 0s and 1s, we may change up to K values from 0 to 1. Return the length of the longest (contiguous) subarray that contains only 1s.    Example 1: Input: A = [1,1,1,0,0,0,1,1,1,1,0], K = 2 Output: 6 Explanation: [1,1,1,0,0,1,1,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. Example 2: Input: A = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3 Output: 10 Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.   Note: 1 <= A.length <= 20000 0 <= K <= A.length A[i] is 0 or 1
class Solution: def longestOnes(self, A: List[int], K: int) -> int: l = len(A) end = 0 start = 0 ans = 0 count = 0 for i in range(l): if A[i] == 1: count += 1 if end - start + 1 - count <= K: ans = max(ans, end - start + 1) else: if A[start] == 1: count -= 1 start += 1 end += 1 return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR
Given an array A of 0s and 1s, we may change up to K values from 0 to 1. Return the length of the longest (contiguous) subarray that contains only 1s.    Example 1: Input: A = [1,1,1,0,0,0,1,1,1,1,0], K = 2 Output: 6 Explanation: [1,1,1,0,0,1,1,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. Example 2: Input: A = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3 Output: 10 Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.   Note: 1 <= A.length <= 20000 0 <= K <= A.length A[i] is 0 or 1
class Solution: def longestOnes(self, A: List[int], K: int) -> int: left, right = 0, 0 alen = len(A) while right < alen: if A[right] == 0: K -= 1 if K < 0: if A[left] == 0: K += 1 left += 1 right += 1 return right - left
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR IF VAR VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN BIN_OP VAR VAR VAR
Given an array A of 0s and 1s, we may change up to K values from 0 to 1. Return the length of the longest (contiguous) subarray that contains only 1s.    Example 1: Input: A = [1,1,1,0,0,0,1,1,1,1,0], K = 2 Output: 6 Explanation: [1,1,1,0,0,1,1,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. Example 2: Input: A = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3 Output: 10 Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.   Note: 1 <= A.length <= 20000 0 <= K <= A.length A[i] is 0 or 1
class Solution: def longestOnes(self, A: List[int], K: int) -> int: begins = [-1] * (K + 1) cur = [0] * (K + 1) maxL = 0 curMax = 0 for i, a in enumerate(A): if a == 0: maxL = max(maxL, curMax) begins.pop(0) begins.append(i) curMax -= cur.pop(0) curMax += 1 if begins[0] == -1 else 0 cur.append(0) else: cur[-1] += 1 curMax += 1 return max(maxL, curMax)
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER VAR VAR NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR
Given an array A of 0s and 1s, we may change up to K values from 0 to 1. Return the length of the longest (contiguous) subarray that contains only 1s.    Example 1: Input: A = [1,1,1,0,0,0,1,1,1,1,0], K = 2 Output: 6 Explanation: [1,1,1,0,0,1,1,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. Example 2: Input: A = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3 Output: 10 Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.   Note: 1 <= A.length <= 20000 0 <= K <= A.length A[i] is 0 or 1
class Solution: def longestOnes(self, A: List[int], K: int) -> int: count_1 = 0 zero_inx = [] res = 0 zero_before = -1 for i in range(len(A)): if A[i] == 1: count_1 += 1 else: zero_inx.append(i) if K > 0: K -= 1 count_1 += 1 elif K == 0: res = max(res, count_1) first_0 = zero_inx.pop(0) count_1 -= first_0 - zero_before - 1 zero_before = first_0 res = max(res, count_1) return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
Given an array A of 0s and 1s, we may change up to K values from 0 to 1. Return the length of the longest (contiguous) subarray that contains only 1s.    Example 1: Input: A = [1,1,1,0,0,0,1,1,1,1,0], K = 2 Output: 6 Explanation: [1,1,1,0,0,1,1,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. Example 2: Input: A = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3 Output: 10 Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.   Note: 1 <= A.length <= 20000 0 <= K <= A.length A[i] is 0 or 1
class Solution: def longestOnes(self, A: List[int], K: int) -> int: start = end = 0 res = 0 map = collections.defaultdict(int) while end < len(A): map[A[end]] += 1 while end - start + 1 - map[1] > K: map[A[start]] -= 1 start += 1 res = max(res, end - start + 1) end += 1 return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER WHILE BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
Given an array A of 0s and 1s, we may change up to K values from 0 to 1. Return the length of the longest (contiguous) subarray that contains only 1s.    Example 1: Input: A = [1,1,1,0,0,0,1,1,1,1,0], K = 2 Output: 6 Explanation: [1,1,1,0,0,1,1,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. Example 2: Input: A = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3 Output: 10 Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.   Note: 1 <= A.length <= 20000 0 <= K <= A.length A[i] is 0 or 1
class Solution: def longestOnes(self, A: List[int], K: int) -> int: start, end, max_length, num_0 = 0, 0, 0, int(not A[0]) while end < len(A) - 1: if num_0 > K: if A[start] == 0: num_0 -= 1 start += 1 end += 1 if A[end] == 0: num_0 += 1 if num_0 <= K: max_length = end - start + 1 return max_length
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER FUNC_CALL VAR VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR
Given an array A of 0s and 1s, we may change up to K values from 0 to 1. Return the length of the longest (contiguous) subarray that contains only 1s.    Example 1: Input: A = [1,1,1,0,0,0,1,1,1,1,0], K = 2 Output: 6 Explanation: [1,1,1,0,0,1,1,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. Example 2: Input: A = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3 Output: 10 Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.   Note: 1 <= A.length <= 20000 0 <= K <= A.length A[i] is 0 or 1
class Solution: def longestOnes(self, x: List[int], K: int) -> int: buf = collections.deque() n = len(x) i = 0 unused = K start = -1 maxlen = float("-inf") while i < n: if x[i] == 0: if unused > 0: buf.append(i) unused -= 1 maxlen = max(i - start, maxlen) elif buf: start = buf.popleft() maxlen = max(i - start, maxlen) buf.append(i) else: maxlen = max(i - 1 - start, maxlen) start = i else: maxlen = max(i - start, maxlen) i += 1 return maxlen if n > 0 else n
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING WHILE VAR VAR IF VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR NUMBER VAR VAR VAR
Given an array A of 0s and 1s, we may change up to K values from 0 to 1. Return the length of the longest (contiguous) subarray that contains only 1s.    Example 1: Input: A = [1,1,1,0,0,0,1,1,1,1,0], K = 2 Output: 6 Explanation: [1,1,1,0,0,1,1,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. Example 2: Input: A = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3 Output: 10 Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.   Note: 1 <= A.length <= 20000 0 <= K <= A.length A[i] is 0 or 1
class Solution: def longestOnes(self, A: List[int], K: int) -> int: res = 0 i, j, N = 0, 0, len(A) remaining = K if A[i] == 1 else K - 1 while i < N: if i > j: j = i remaining = K if A[i] == 1 else K - 1 while j + 1 < N and remaining >= 0: if A[j + 1] == 0: if remaining > 0: remaining -= 1 else: break j += 1 res = max(res, j - i + 1) if A[i] == 0: remaining += 1 i += 1 return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR
Given an array A of 0s and 1s, we may change up to K values from 0 to 1. Return the length of the longest (contiguous) subarray that contains only 1s.    Example 1: Input: A = [1,1,1,0,0,0,1,1,1,1,0], K = 2 Output: 6 Explanation: [1,1,1,0,0,1,1,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined. Example 2: Input: A = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3 Output: 10 Explanation: [0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1] Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.   Note: 1 <= A.length <= 20000 0 <= K <= A.length A[i] is 0 or 1
class Solution: def longestOnes(self, A: List[int], k: int) -> int: ans = 0 i = 0 for j, c in enumerate(A): k -= 1 - c if k < 0: k += 1 - A[i] i += 1 ans = max(ans, j - i + 1) return max(ans, j - i + 1)
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR IF VAR NUMBER VAR BIN_OP NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR