description
stringlengths 171
4k
| code
stringlengths 94
3.98k
| normalized_code
stringlengths 57
4.99k
|
|---|---|---|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
curr_sum = 0
intervals = [(-200002, -100001), (-200002, -100001)]
h = {(0): -1}
f = [([100001] * len(arr)) for i in range(2)]
for index, a in enumerate(arr):
curr_sum += a
f[1][index] = min(
(
f[0][h[curr_sum - target]] + index - h[curr_sum - target]
if curr_sum - target in h
else 100001
),
f[1][index - 1],
)
f[0][index] = min(
f[0][index - 1] if index >= 1 else 100001,
index - h[curr_sum - target] if curr_sum - target in h else 100001,
)
h[curr_sum] = index
return -1 if f[-1][-1] >= 100001 else f[-1][-1]
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR NUMBER NUMBER NUMBER NUMBER VAR NUMBER NUMBER VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
if not arr:
return -1
def getMinSub(arr, target):
dp = [float("inf")] * len(arr)
total = 0
lookup = defaultdict(int)
for index, val in enumerate(arr):
total += val
if total == target:
dp[index] = index - 0 + 1
elif total - target in lookup:
dp[index] = min(index - lookup[total - target] + 1, dp[index - 1])
else:
dp[index] = dp[index - 1]
lookup[total] = index + 1
return dp
forward = getMinSub(arr, target)
backward = getMinSub(arr[::-1], target)[::-1]
minresult = float("inf")
for i in range(1, len(arr)):
minresult = min(minresult, forward[i - 1] + backward[i])
return minresult if minresult != float("inf") else -1
|
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR RETURN NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
lens_s = [math.inf] * len(arr)
lens_e = [math.inf] * len(arr)
cs = s = e = 0
for i in range(len(arr)):
cs += arr[e]
while cs >= target:
if cs == target:
lens_e[e] = lens_s[s] = e + 1 - s
cs -= arr[s]
s += 1
e += 1
forward = [math.inf] * (len(arr) + 1)
for i in range(1, len(arr)):
forward[i] = min(lens_e[i - 1], forward[i - 1])
backward = [math.inf] * (len(arr) + 1)
for i in reversed(list(range(0, len(arr)))):
backward[i] = min(lens_s[i], backward[i + 1])
best = math.inf
for f, b in zip(forward, backward):
best = min(best, f + b)
if best == math.inf:
return -1
return best
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR WHILE VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP LIST VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR RETURN NUMBER RETURN VAR VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
prefix = {(0): -1}
best_sofar_arr = [math.inf]
best_sofar = math.inf
curr_best = math.inf
ans = math.inf
new_ans = math.inf
for i, curr in enumerate(itertools.accumulate(arr)):
prefix[curr] = i
if curr - target in prefix:
curr_best = i - prefix[curr - target]
best_sofar = min(best_sofar, curr_best)
best_sofar_arr.append(best_sofar)
if i > 0:
new_ans = curr_best + best_sofar_arr[prefix[curr - target] + 1]
ans = min(new_ans, ans)
else:
best_sofar_arr.append(best_sofar)
return -1 if ans == math.inf else ans
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR LIST VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR NUMBER VAR VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
prefix, suffix = [float("inf")] * len(arr), [float("inf")] * len(arr)
curSum, l = 0, 0
for r in range(1, len(arr)):
prefix[r] = prefix[r - 1]
curSum += arr[r - 1]
while curSum >= target:
if curSum == target:
prefix[r] = min(prefix[r], r - l)
curSum -= arr[l]
l += 1
curSum, r = 0, len(arr)
for l in range(len(arr) - 1, 0, -1):
if l < len(arr) - 1:
suffix[l] = suffix[l + 1]
curSum += arr[l]
while curSum >= target:
if curSum == target:
suffix[l] = min(suffix[l], r - l)
curSum -= arr[r - 1]
r -= 1
res = len(arr) + 1
for i in range(1, len(arr) - 1):
res = min(res, prefix[i] + suffix[i])
return res if res <= len(arr) else -1
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR BIN_OP LIST FUNC_CALL VAR STRING FUNC_CALL VAR VAR BIN_OP LIST FUNC_CALL VAR STRING FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR WHILE VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR RETURN VAR FUNC_CALL VAR VAR VAR NUMBER VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
l = 0
currentSum = 0
res = -1
record = [-1] * len(arr)
for r in range(len(arr)):
currentSum += arr[r]
while currentSum > target and l < r:
currentSum -= arr[l]
l += 1
if currentSum == target:
curr = r - l + 1
print(curr)
if record[l - 1] != -1:
res = (
curr + record[l - 1]
if res == -1
else min(res, curr + record[l - 1])
)
record[r] = curr if record[r - 1] == -1 else min(record[r - 1], curr)
else:
record[r] = record[r - 1]
return res
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
n = len(arr)
dp = [100001] * n
presum = {(0): -1}
s = 0
ans = 100001
best_so_far = sys.maxsize
for i in range(n):
s += arr[i]
presum[s] = i
if s - target in presum:
j = presum[s - target]
best_so_far = min(best_so_far, i - j)
if j >= 0:
ans = min(ans, dp[j] + i - j)
dp[i] = best_so_far
return ans if ans < 100001 else -1
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR NUMBER VAR NUMBER VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
prefix_sum = [float("inf")] * len(arr)
suffix_sum = [float("inf")] * len(arr)
prefix_map = {(0): -1}
suffix_map = {(0): len(arr)}
cur_sum = 0
_min_till_now = float("inf")
for i in range(len(arr) - 1):
cur_sum += arr[i]
if cur_sum - target in prefix_map:
_min_till_now = min(_min_till_now, i - prefix_map[cur_sum - target])
prefix_sum[i + 1] = _min_till_now
prefix_map[cur_sum] = i
cur_sum = 0
_min_till_now = float("inf")
for i in range(len(arr) - 1, -1, -1):
cur_sum += arr[i]
if cur_sum - target in suffix_map:
_min_till_now = min(_min_till_now, suffix_map[cur_sum - target] - i)
suffix_sum[i] = _min_till_now
suffix_map[cur_sum] = i
ans = min([(x + y) for x, y in zip(prefix_sum, suffix_sum)])
return ans if ans != float("inf") else -1
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING FUNC_CALL VAR VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR DICT NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
if not arr:
return 0
n = len(arr)
dp = [float("inf")] * n
presum = 0
indexes = {(0): -1}
res = float("inf")
for i, a in enumerate(arr):
presum += a
if i > 0:
dp[i] = dp[i - 1]
if presum - target in indexes:
idx = indexes[presum - target]
l = i - idx
if dp[idx] != float("inf"):
res = min(res, l + dp[idx])
dp[i] = min(dp[i], l)
indexes[presum] = i
return res if res < float("inf") else -1
|
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING VAR ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
d = {}
d[0] = -1
possible_sum = {0}
cur_sum = 0
for i, a in enumerate(arr):
cur_sum += a
possible_sum.add(cur_sum)
d[cur_sum] = i
till_len = [float("inf")] * len(arr)
for s in possible_sum:
if s + target in possible_sum:
till_len[d[s + target]] = d[s + target] - d[s]
ans = float("inf")
pre_min = [float("inf")] * len(arr)
for i, t in enumerate(till_len):
if i == t - 1:
pre_min[i] = t
else:
pre_min[i] = min(t, pre_min[i - 1])
if t < float("inf") and pre_min[i - t] < float("inf"):
ans = min(ans, t + pre_min[i - t])
return ans if ans < float("inf") else -1
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING FUNC_CALL VAR VAR FOR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR STRING VAR BIN_OP VAR VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
prefix = [0]
d = {(0): -1}
for i in range(len(arr)):
prefix.append(arr[i] + prefix[-1])
d[prefix[-1]] = i
ans = first = second = float("inf")
for i in range(len(arr)):
if prefix[i + 1] - target in d:
first = min(first, i - d[prefix[i + 1] - target])
if first < float("inf") and prefix[i + 1] + target in d:
second = d[prefix[i + 1] + target] - i
ans = min(ans, first + second)
return -1 if ans == float("inf") else ans
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR DICT NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR IF VAR FUNC_CALL VAR STRING BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_CALL VAR STRING NUMBER VAR VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
smallest = [math.inf] * len(arr)
q = deque()
curr_sum = 0
result = math.inf
for i, n in enumerate(arr):
curr_sum += n
q.append(i)
while curr_sum > target:
curr_sum -= arr[q.popleft()]
if curr_sum == target:
if i == 0:
smallest[i] = len(q)
else:
smallest[i] = min(len(q), smallest[i - 1])
result = min(result, smallest[q[0] - 1] + len(q))
else:
smallest[i] = smallest[i - 1]
return -1 if result == math.inf else result
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR FUNC_CALL VAR IF VAR VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR NUMBER VAR VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
MAX = float("inf")
n = len(arr) + 1
best_till = [MAX for i in range(n + 1)]
presum = 0
maps = {}
maps[0] = -1
ans = best = MAX
for i, v in enumerate(arr):
presum += v
key = presum - target
if key in maps:
left_idx = maps[key]
curr_len = i - left_idx
best = min(best, curr_len)
if left_idx > -1:
first_len = best_till[left_idx]
ans = min(ans, first_len + curr_len)
best_till[i] = best
maps[presum] = i
if ans == MAX:
return -1
return ans
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR RETURN NUMBER RETURN VAR VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
pre_index, curr_sum = 0, 0
f = [100001] * (len(arr) + 1)
ans = 100001
for index, a in enumerate(arr):
curr_sum += a
while curr_sum > target:
curr_sum -= arr[pre_index]
pre_index += 1
if curr_sum == target:
ans = min(f[pre_index] + index + 1 - pre_index, ans)
f[index + 1] = min(f[index], index + 1 - pre_index)
else:
f[index + 1] = f[index]
return -1 if ans >= 100001 else ans
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR NUMBER NUMBER VAR VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
sums_list = [0]
sums_dict = {(0): 0}
s = 0
for i in range(len(arr)):
s += arr[i]
sums_dict[s] = i + 1
sums_list.append(s)
lengths = [0] * len(sums_list)
min_length = float("+inf")
for start in range(len(sums_list) - 1, -1, -1):
val = sums_list[start] + target
if val in sums_dict:
end = sums_dict[val]
min_length = min(min_length, end - start)
lengths[start] = min_length
min_length = float("+inf")
for start in range(len(sums_list)):
val = sums_list[start] + target
if val in sums_dict:
end = sums_dict[val]
l = end - start + lengths[end]
min_length = min(min_length, l)
if min_length != float("+inf"):
return min_length
else:
return -1
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR STRING RETURN VAR RETURN NUMBER VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
length = len(arr)
cumulative = [0] * length
reverse_cumulative = [0] * length
s = 0
sr = 0
for i in range(length):
s = s + arr[i]
cumulative[i] = s
sr += arr[length - 1 - i]
reverse_cumulative[i] = sr
d = {(0): -1}
dr = {(0): -1}
forward_vals = [math.inf] * length
backward_vals = [math.inf] * length
best1 = math.inf
best2 = math.inf
i = 0
while i < length:
d[cumulative[i]] = i
dr[reverse_cumulative[i]] = i
if cumulative[i] - target in d:
l = i - d[cumulative[i] - target]
best1 = min(best1, l)
forward_vals[i] = best1
if reverse_cumulative[i] - target in dr:
l = i - dr[reverse_cumulative[i] - target]
best2 = min(best2, l)
backward_vals[i] = best2
i += 1
best1 = math.inf
for i in range(len(forward_vals) - 1):
best1 = min(best1, forward_vals[i] + backward_vals[length - 2 - i])
if best1 == math.inf:
return -1
return best1
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR BIN_OP LIST VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR RETURN NUMBER RETURN VAR VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
minlen = [float("inf")] * len(arr)
res = float("inf")
l, r, windsum = 0, 0, 0
for r in range(len(arr)):
windsum += arr[r]
while windsum > target:
windsum -= arr[l]
l += 1
if windsum == target:
if minlen[l - 1] != float("inf"):
temp = minlen[l - 1] + r - l + 1
res = min(res, temp)
minlen[r] = min(minlen[r - 1], r - l + 1)
else:
minlen[r] = minlen[r - 1]
return res if res < float("inf") else -1
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR IF VAR BIN_OP VAR NUMBER FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
prefix = {(0): -1}
current = 0
queue = []
for i in range(len(arr)):
current += arr[i]
prefix[current] = i
if not prefix.get(current - target) is None:
if len(queue) == 0 or prefix[current - target] + 1 > queue[-1][0]:
queue.append((prefix[current - target] + 1, i))
if len(queue) < 2:
return -1
min_q = []
current = len(arr)
for i in range(len(queue) - 1, -1, -1):
current = min(current, queue[i][1] - queue[i][0] + 1)
min_q.append(current)
min_q = min_q[::-1]
pointer1 = 0
pointer2 = 1
res = -1
while pointer2 < len(queue):
if queue[pointer2][0] <= queue[pointer1][1]:
pointer2 += 1
continue
if res < 0:
res = queue[pointer1][1] - queue[pointer1][0] + 1 + min_q[pointer2]
else:
res = min(
res, queue[pointer1][1] - queue[pointer1][0] + 1 + min_q[pointer2]
)
pointer1 += 1
return res
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR NONE IF FUNC_CALL VAR VAR NUMBER BIN_OP VAR BIN_OP VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR NUMBER VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER RETURN VAR VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
premin = [float("inf")] * len(arr)
l, cur, res = 0, 0, float("inf")
for r, num in enumerate(arr):
if r > 0:
premin[r] = premin[r - 1]
cur += num
while cur > target:
cur -= arr[l]
l += 1
if cur == target:
size = r - l + 1
if l > 0:
res = min(res, size + premin[l - 1])
premin[r] = min(premin[r], size)
return res if res < float("inf") else -1
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER FUNC_CALL VAR STRING FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
preSum = [0]
for val in arr:
preSum.append(preSum[-1] + val)
sumMap = {}
preCands = [sys.maxsize] * len(arr)
sufCands = [sys.maxsize] * len(arr)
for i, val in enumerate(preSum):
if val - target in sumMap:
sufCands[sumMap[val - target]] = i - sumMap[val - target]
preCands[i - 1] = i - sumMap[val - target]
sumMap[val] = i
prefixSum = [sys.maxsize] * len(arr)
suffixSum = [sys.maxsize] * len(arr)
for i, val in enumerate(preCands):
if i + 1 < len(prefixSum):
prefixSum[i + 1] = min(preCands[i], prefixSum[i])
for i, val in reversed(list(enumerate(sufCands))):
if i + 1 < len(suffixSum):
suffixSum[i] = min(sufCands[i], suffixSum[i + 1])
else:
suffixSum[i] = sufCands[i]
res = sys.maxsize
for i in range(len(arr)):
res = min(res, prefixSum[i] + suffixSum[i])
return res if res != sys.maxsize else -1
|
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 DICT ASSIGN VAR BIN_OP LIST VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR RETURN VAR VAR VAR NUMBER VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution(object):
def minSumOfLengths(self, arr: List[int], target: int) -> int:
prefix = {(0): -1}
best_till = [math.inf] * len(arr)
ans = best = math.inf
for i, curr in enumerate(itertools.accumulate(arr)):
if curr - target in prefix:
end = prefix[curr - target]
if end > -1:
ans = min(ans, i - end + best_till[end])
best = min(best, i - end)
best_till[i] = best
prefix[curr] = i
return -1 if ans == math.inf else ans
def OldminSumOfLengths(self, arr, target):
ans, l = math.inf, len(arr)
la = [math.inf] * l
p1, p2, s, ml = 0, 0, arr[0], math.inf
while p1 < l and p2 < l:
action = None
la[p2] = ml
curl = p2 - p1 + 1
if s == target:
ml = min(ml, curl)
la[p2] = ml
if p1 - 1 >= 0:
ans = min(ans, la[p1 - 1] + curl)
p2 += 1
action = 1
elif s < target:
p2 += 1
action = 1
elif s > target:
p1 += 1
action = -1
if p1 >= l or p2 >= l:
break
elif p1 > p2:
p2 = p1
s = arr[p1]
elif action == 1:
s += arr[p2]
elif action == -1:
s -= arr[p1 - 1]
return -1 if ans == math.inf else ans
|
CLASS_DEF VAR FUNC_DEF VAR VAR VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR BIN_OP LIST VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER VAR NUMBER VAR WHILE VAR VAR VAR VAR ASSIGN VAR NONE ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR VAR VAR IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR NUMBER VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
n = len(arr)
left_presum = {(0): 0}
left_dp = [float("inf")] * (n + 1)
total = 0
for i in range(1, n + 1):
total += arr[i - 1]
if total - target in left_presum:
left_dp[i] = min(i - left_presum[total - target], left_dp[i - 1])
else:
left_dp[i] = left_dp[i - 1]
left_presum[total] = i
total = 0
right_presum = {(0): n}
right_dp = [float("inf")] * (n + 1)
for i in range(n - 1, -1, -1):
total += arr[i]
if total - target in right_presum:
right_dp[i] = min(right_dp[i + 1], right_presum[total - target] - i)
else:
right_dp[i] = right_dp[i + 1]
right_presum[total] = i
print(right_dp)
ans = float("inf")
for i in range(1, n):
if left_dp[i] == float("inf") or right_dp[i] == float("inf"):
continue
ans = min(ans, left_dp[i] + right_dp[i])
return -1 if ans == float("inf") else ans
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR FUNC_CALL VAR STRING VAR VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR RETURN VAR FUNC_CALL VAR STRING NUMBER VAR VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
n = len(arr)
def aux(arr):
h = {(0): 0}
preSum = 0
left = [(n + 1) for i in range(n)]
for i in range(1, n + 1):
preSum = preSum + arr[i - 1]
h[preSum] = i
if preSum - target in h:
left[i - 1] = i - h[preSum - target]
if i > 1:
left[i - 1] = min(left[i - 2], left[i - 1])
return left
left = aux(arr)
arr.reverse()
right = aux(arr)
ans = n + 1
for i in range(n - 1):
ans = min(ans, left[i] + right[n - 1 - (i + 1)])
return ans if ans < n + 1 else -1
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR BIN_OP VAR NUMBER VAR NUMBER VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
min_range = None
left = self.get_list(arr, target)
right = list(reversed(self.get_list(list(reversed(arr)), target)))
print(left)
print(right)
min_length = None
for i in range(len(arr) - 1):
if left[i] != -1 and right[i + 1] != -1:
if not min_length:
min_length = left[i] + right[i + 1]
else:
min_length = min(left[i] + right[i + 1], min_length)
return -1 if not min_length else min_length
def get_list(self, arr, target):
prefix_d = dict()
prefix_d[0] = -1
min_range_list = []
current_min = -1
current = 0
for i in range(len(arr)):
current += arr[i]
if current - target in prefix_d:
prev_idx = prefix_d[current - target]
dis = i - prev_idx
if current_min == -1:
current_min = dis
else:
current_min = min(current_min, dis)
min_range_list.append(current_min)
prefix_d[current] = i
return min_range_list
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NONE FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR RETURN VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
n = len(arr)
best_to = [(n + 1) for _ in range(n)]
best_from = dict()
min_total = n + 1
min_left = n + 1
sum_left = 0
s_left = 0
e_left = 0
for i in range(n):
sum_left += arr[i]
if sum_left == target:
best_to[i] = i - s_left + 1
best_from[s_left] = i - s_left + 1
elif sum_left > target:
while s_left < i and sum_left > target:
sum_left -= arr[s_left]
s_left += 1
if sum_left == target:
best_to[i] = i - s_left + 1
best_from[s_left] = i - s_left + 1
for i in range(1, n):
best_to[i] = min(best_to[i], best_to[i - 1])
for i in range(n - 1):
if best_to[i] > n:
continue
if i + 1 in best_from:
min_total = min(min_total, best_to[i] + best_from[i + 1])
if min_total > n:
return -1
return min_total
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR RETURN NUMBER RETURN VAR VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
psum = {(0): -1}
csum = 0
re = 100001
dp = [100001] * len(arr)
best = 100001
for i, csum in enumerate(itertools.accumulate(arr)):
psum[csum] = i
if csum - target in psum:
best = min(best, i - psum[csum - target])
re = min(re, dp[psum[csum - target]] + i - psum[csum - target])
dp[i] = best
if re < 100001:
return re
return -1
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER RETURN VAR RETURN NUMBER VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
prefxSum, prefx = 0, []
suffxSum, suffx = sum(arr), []
dct = {(0): -1}
dp = [float("inf")] * len(arr)
for i in range(0, len(arr)):
x = arr[i]
prefxSum += x
prefx.append(prefxSum)
suffx.append(suffxSum)
suffxSum -= x
dct[prefxSum] = i
prefxSum = 0
mn = float("inf")
for i in range(0, len(arr)):
prefxSum += arr[i]
if prefxSum - target in dct:
start = dct[prefxSum - target]
if start > -1:
if dp[start] != float("inf"):
mn = min(mn, dp[start] + i - start)
dp[i] = min(dp[i - 1], i - start)
else:
dp[i] = i + 1
elif i > 0:
dp[i] = dp[i - 1]
return mn if mn != float("inf") else -1
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR NUMBER LIST ASSIGN VAR VAR FUNC_CALL VAR VAR LIST ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR IF VAR NUMBER IF VAR VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
dp = [float("inf") for i in range(len(arr))]
curr_sum = 0
hist = {(0): -1}
res = float("inf")
shortest_length = float("inf")
for i, num in enumerate(arr):
curr_sum += num
diff = curr_sum - target
if diff in hist:
index = hist[diff]
length = i - index
if index > 0 and dp[index] != float("inf"):
res = min(res, dp[index] + length)
shortest_length = min(shortest_length, length)
hist[curr_sum] = i
dp[i] = shortest_length
return res if res != float("inf") else -1
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
def getPrefix(arr):
m = {(0): -1}
prefix = [float("inf")]
s = 0
for i, n in enumerate(arr):
s += n
minn = prefix[-1]
if s - target in m:
minn = min(i - m[s - target], minn)
prefix.append(minn)
m[s] = i
prefix.pop(0)
return prefix
prefix = getPrefix(arr)
sufix = list(reversed(getPrefix(reversed(arr))))
ans = float("inf")
for i in range(len(arr) - 1):
ans = min(ans, prefix[i] + sufix[i + 1])
return ans if ans != float("inf") else -1
|
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR LIST FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_CALL VAR STRING VAR NUMBER VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
n = len(arr)
acc = dict()
acc[-1] = 0
for i in range(0, n):
acc[i] = acc[i - 1] + arr[i]
best_to = [(n + 1) for _ in range(n)]
best_from = dict()
min_total = n + 1
s_left = 0
for i in range(n):
if acc[i] - acc[s_left - 1] == target:
best_to[i] = i - s_left + 1
best_from[s_left] = i - s_left + 1
elif acc[i] - acc[s_left - 1] > target:
while s_left < i:
s_left += 1
if acc[i] - acc[s_left - 1] < target:
break
if acc[i] - acc[s_left - 1] == target:
best_to[i] = i - s_left + 1
best_from[s_left] = i - s_left + 1
break
for i in range(1, n):
best_to[i] = min(best_to[i], best_to[i - 1])
for i in range(n - 1):
if best_to[i] > n:
continue
if i + 1 in best_from:
min_total = min(min_total, best_to[i] + best_from[i + 1])
if min_total > n:
return -1
return min_total
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR WHILE VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR IF BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR RETURN NUMBER RETURN VAR VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
lengths = [len(arr) + 1] * (len(arr) + 1)
i = 0
j = 0
s = 0
min_two_sum = len(arr) + 1
while j <= len(arr):
if s < target:
if j == len(arr):
break
if j > 0:
lengths[j] = lengths[j - 1]
s += arr[j]
j += 1
elif s > target:
s -= arr[i]
i += 1
else:
lengths[j] = min(lengths[j - 1], j - i)
min_two_sum = min(min_two_sum, lengths[i] + j - i)
if j == len(arr):
break
s += arr[j]
j += 1
if min_two_sum > len(arr):
min_two_sum = -1
return min_two_sum
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER RETURN VAR VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
def get_shortest(array):
shortest = [float("infinity") for _ in range(len(arr))]
prefix_pos = {}
prefix_sum = 0
for i in range(len(array)):
prefix_sum += array[i]
if prefix_sum == target:
if i == 0:
shortest[i] = i + 1
else:
shortest[i] = min(shortest[i - 1], i + 1)
elif prefix_sum - target in prefix_pos:
shortest[i] = i - prefix_pos[prefix_sum - target] + 1
shortest[i] = min(shortest[i - 1], shortest[i])
prefix_pos[prefix_sum] = i + 1
return shortest
left = get_shortest(arr)
right = get_shortest(arr[::-1])[::-1]
res = float("infinity")
for i in range(len(left) - 1):
res = min(res, left[i] + right[i + 1])
if res == float("infinity"):
return -1
return res
|
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR STRING RETURN NUMBER RETURN VAR VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
n = len(arr)
min_lens = [sys.maxsize] * n
l = 0
min_len = sys.maxsize
prefix = 0
ans = sys.maxsize
for r in range(n):
prefix += arr[r]
while prefix > target:
prefix -= arr[l]
l += 1
if prefix == target:
cur_len = r - l + 1
if l > 0 and min_lens[l - 1] != sys.maxsize:
ans = min(ans, cur_len + min_lens[l - 1])
min_len = min(min_len, cur_len)
min_lens[r] = min_len
return -1 if ans == sys.maxsize else ans
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR NUMBER VAR VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
n = len(arr)
curr_sum = 0
curr_best = -1
lo = hi = 0
lmin = []
while lo <= hi < n:
curr_sum += arr[hi]
while curr_sum > target and lo <= hi:
curr_sum -= arr[lo]
lo += 1
if curr_sum == target:
tmp = hi - lo + 1
if tmp < curr_best or curr_best == -1:
curr_best = tmp
lmin.append(curr_best)
hi += 1
curr_sum = 0
curr_best = -1
lo = hi = n - 1
rmin = [-1]
while 0 < lo <= hi:
curr_sum += arr[lo]
while curr_sum > target and lo <= hi:
curr_sum -= arr[hi]
hi -= 1
if curr_sum == target:
tmp = hi - lo + 1
if tmp < curr_best or curr_best == -1:
curr_best = tmp
rmin.append(curr_best)
lo -= 1
rmin = rmin[::-1]
res = -1
for i in range(n):
if lmin[i] != -1 and rmin[i] != -1:
if res == -1 or lmin[i] + rmin[i] < res:
res = lmin[i] + rmin[i]
return res
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR VAR VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER WHILE NUMBER VAR VAR VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR RETURN VAR VAR
|
Given an array of integers arr and an integer target.
You have to find two non-overlapping sub-arrays of arr each with sum equal target. There can be multiple answers so you have to find an answer where the sum of the lengths of the two sub-arrays is minimum.
Return the minimum sum of the lengths of the two required sub-arrays, or return -1 if you cannot find such two sub-arrays.
Example 1:
Input: arr = [3,2,2,4,3], target = 3
Output: 2
Explanation: Only two sub-arrays have sum = 3 ([3] and [3]). The sum of their lengths is 2.
Example 2:
Input: arr = [7,3,4,7], target = 7
Output: 2
Explanation: Although we have three non-overlapping sub-arrays of sum = 7 ([7], [3,4] and [7]), but we will choose the first and third sub-arrays as the sum of their lengths is 2.
Example 3:
Input: arr = [4,3,2,6,2,3,4], target = 6
Output: -1
Explanation: We have only one sub-array of sum = 6.
Example 4:
Input: arr = [5,5,4,4,5], target = 3
Output: -1
Explanation: We cannot find a sub-array of sum = 3.
Example 5:
Input: arr = [3,1,1,1,5,1,2,1], target = 3
Output: 3
Explanation: Note that sub-arrays [1,2] and [2,1] cannot be an answer because they overlap.
Constraints:
1 <= arr.length <= 10^5
1 <= arr[i] <= 1000
1 <= target <= 10^8
|
class Solution:
def minSumOfLengths(self, arr: List[int], target: int) -> int:
presum = [0] * len(arr)
c = 0
for i in range(len(arr)):
c += arr[i]
presum[i] = c
dic = {(0): -1}
index = [0] * len(arr)
short = [0] * len(arr)
temp = sys.maxsize
for i in range(len(arr)):
if presum[i] - target in dic:
index[i] = i - dic[presum[i] - target]
temp = min(temp, i - dic[presum[i] - target])
dic[presum[i]] = i
short[i] = temp
ans = sys.maxsize
print((index, short))
for i in range(1, len(arr)):
if (
index[i] > 0
and i - index[i] >= 0
and short[i - index[i]] != sys.maxsize
):
ans = min(ans, short[i - index[i]] + index[i])
if ans == sys.maxsize:
return -1
return ans
|
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR IF VAR VAR RETURN NUMBER RETURN VAR VAR
|
You are given an array A = [A_{1}, A_{2}, \ldots, A_{N}] containing N integers.
Consider any subarray [A_{L}, A_{L+1}, \ldots, A_{R}] of this subarray (where 1≤ L ≤ R ≤ N). Ther weirdness of this subarray, denoted w(L, R), is defined to be the number of indices L ≤ i ≤ R such that A_{i} equals the number of occurrences of A_{i} in this subarray.
Formally, we define w(L, R) for 1 ≤ L ≤ R ≤ N as follows:
For every integer x, let f_{L, R}(x) denote the number of indices L ≤ j ≤ R such that A_{j} = x.
w(L, R) then equals the number of L ≤ i ≤ R such that A_{i} = f_{L, R}(A_{i})
For example, let A = [1, 3, 5, 2, 1, 2, 9, 5]. Then,
w(1, 8) = 2, which can be computed as follows.
We have
- f_{1, 8}(1) = 2
- f_{1, 8}(2) = 2
- f_{1, 8}(3) = 1
- f_{1, 8}(5) = 2
- f_{1, 8}(9) = 1
The only indices which satisfy f_{1, 8}(A_{i}) = A_{i} are i = 4 and i = 6, and hence w(1, 8) = 2.
w(3, 5) = 1 — the subarray is [5, 2, 1], and each of 1, 2, 5 occur exactly once in this subarray. Of these, only 1 equals the number of its occurrences in this subarray.
Given A, your task is to compute the sum of the weirdness of all subarrays of A, i.e, the value
\sum_{L = 1}^{N} \sum_{R = L}^N w(L, R)
------ Input Format ------
- The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains an integer N, the size of the given array.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N} — the elements of A.
------ Output Format ------
For each test case, output a single line containing a single integer — the sum of the weirdness of all subarrays of the given array.
------ Constraints ------
$1 ≤ T ≤ 10^{3}$
$1 ≤ N ≤ 10^{6}$
$1 ≤ A_{i} ≤ 10^{6}$
- The sum of $N$ across all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
4
3
3 3 3
4
1 2 1 2
5
1 2 3 2 1
8
1 2 2 3 2 3 3 1
----- Sample Output 1 ------
3
10
16
54
----- explanation 1 ------
Test Case 1: The only subarray with non-zero weirdness is the whole array, which has $w(1, 3) = 3$ because every value is $3$ and it appears thrice.
Test Case 2: There are $10$ subarrays. Their weirdness values are as follows:
- $w(1, 1) = 1$, because $A_{1} = 1$ and $f_{1, 1}(1) = 1$.
- $w(1, 2) = 1$, because $A_{1} = 1$ and $f_{1, 2}(1) = 1$; but $A_{2} = 2$ and $f_{1, 2}(2) = 1 \neq 2$.
- $w(1, 3) = 0$, here $f_{1, 3}(1) = 2 \neq 1$ and $f_{1, 3}(2) = 1 \neq 2$ so no index satisfies the condition.
- $w(1, 4) = 2$, here indices $2$ and $4$ satisfy the condition.
- $w(2, 2) = 0$
- $w(2, 3) = 1$, index $3$ satisfies the condition.
- $w(2, 4) = 3$, all indices satisfy the condition.
- $w(3, 3) = 1$
- $w(3, 4) = 1$, index $3$ satisfies the condition.
- $w(4, 4) = 0$
|
t = int(input())
for _ in range(t):
n = int(input())
arr = list(map(int, input().split()))
hm = {}
for i in range(len(arr)):
if arr[i] in hm:
hm[arr[i]].append(i)
else:
hm[arr[i]] = [i]
ans = 0
for i in hm:
x = len(hm[i])
if x >= i:
l = 0
r = i - 1
temp = 0
while r < x:
if l == 0:
left = hm[i][l] + 1
else:
left = hm[i][l] - hm[i][l - 1]
if r == x - 1:
right = n - hm[i][r]
else:
right = hm[i][r + 1] - hm[i][r]
temp += left * right
l += 1
r += 1
ans += temp * i
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
|
You are given an array A = [A_{1}, A_{2}, \ldots, A_{N}] containing N integers.
Consider any subarray [A_{L}, A_{L+1}, \ldots, A_{R}] of this subarray (where 1≤ L ≤ R ≤ N). Ther weirdness of this subarray, denoted w(L, R), is defined to be the number of indices L ≤ i ≤ R such that A_{i} equals the number of occurrences of A_{i} in this subarray.
Formally, we define w(L, R) for 1 ≤ L ≤ R ≤ N as follows:
For every integer x, let f_{L, R}(x) denote the number of indices L ≤ j ≤ R such that A_{j} = x.
w(L, R) then equals the number of L ≤ i ≤ R such that A_{i} = f_{L, R}(A_{i})
For example, let A = [1, 3, 5, 2, 1, 2, 9, 5]. Then,
w(1, 8) = 2, which can be computed as follows.
We have
- f_{1, 8}(1) = 2
- f_{1, 8}(2) = 2
- f_{1, 8}(3) = 1
- f_{1, 8}(5) = 2
- f_{1, 8}(9) = 1
The only indices which satisfy f_{1, 8}(A_{i}) = A_{i} are i = 4 and i = 6, and hence w(1, 8) = 2.
w(3, 5) = 1 — the subarray is [5, 2, 1], and each of 1, 2, 5 occur exactly once in this subarray. Of these, only 1 equals the number of its occurrences in this subarray.
Given A, your task is to compute the sum of the weirdness of all subarrays of A, i.e, the value
\sum_{L = 1}^{N} \sum_{R = L}^N w(L, R)
------ Input Format ------
- The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains an integer N, the size of the given array.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N} — the elements of A.
------ Output Format ------
For each test case, output a single line containing a single integer — the sum of the weirdness of all subarrays of the given array.
------ Constraints ------
$1 ≤ T ≤ 10^{3}$
$1 ≤ N ≤ 10^{6}$
$1 ≤ A_{i} ≤ 10^{6}$
- The sum of $N$ across all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
4
3
3 3 3
4
1 2 1 2
5
1 2 3 2 1
8
1 2 2 3 2 3 3 1
----- Sample Output 1 ------
3
10
16
54
----- explanation 1 ------
Test Case 1: The only subarray with non-zero weirdness is the whole array, which has $w(1, 3) = 3$ because every value is $3$ and it appears thrice.
Test Case 2: There are $10$ subarrays. Their weirdness values are as follows:
- $w(1, 1) = 1$, because $A_{1} = 1$ and $f_{1, 1}(1) = 1$.
- $w(1, 2) = 1$, because $A_{1} = 1$ and $f_{1, 2}(1) = 1$; but $A_{2} = 2$ and $f_{1, 2}(2) = 1 \neq 2$.
- $w(1, 3) = 0$, here $f_{1, 3}(1) = 2 \neq 1$ and $f_{1, 3}(2) = 1 \neq 2$ so no index satisfies the condition.
- $w(1, 4) = 2$, here indices $2$ and $4$ satisfy the condition.
- $w(2, 2) = 0$
- $w(2, 3) = 1$, index $3$ satisfies the condition.
- $w(2, 4) = 3$, all indices satisfy the condition.
- $w(3, 3) = 1$
- $w(3, 4) = 1$, index $3$ satisfies the condition.
- $w(4, 4) = 0$
|
for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
adict = {}
for i in range(n):
if a[i] in adict:
adict[a[i]].append(i)
else:
adict[a[i]] = [i]
w = 0
for key in adict:
alist = adict[key]
prev = 0
for i in range(len(alist) - (key - 1)):
last = alist[i + key - 1]
if i == len(alist) - (key - 1) - 1:
next = n
else:
next = alist[i + key - 1 + 1]
front = alist[i] + 1 - prev
back = next - last
w += front * back * key
prev = alist[i] + 1
print(w)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You are given an array A = [A_{1}, A_{2}, \ldots, A_{N}] containing N integers.
Consider any subarray [A_{L}, A_{L+1}, \ldots, A_{R}] of this subarray (where 1≤ L ≤ R ≤ N). Ther weirdness of this subarray, denoted w(L, R), is defined to be the number of indices L ≤ i ≤ R such that A_{i} equals the number of occurrences of A_{i} in this subarray.
Formally, we define w(L, R) for 1 ≤ L ≤ R ≤ N as follows:
For every integer x, let f_{L, R}(x) denote the number of indices L ≤ j ≤ R such that A_{j} = x.
w(L, R) then equals the number of L ≤ i ≤ R such that A_{i} = f_{L, R}(A_{i})
For example, let A = [1, 3, 5, 2, 1, 2, 9, 5]. Then,
w(1, 8) = 2, which can be computed as follows.
We have
- f_{1, 8}(1) = 2
- f_{1, 8}(2) = 2
- f_{1, 8}(3) = 1
- f_{1, 8}(5) = 2
- f_{1, 8}(9) = 1
The only indices which satisfy f_{1, 8}(A_{i}) = A_{i} are i = 4 and i = 6, and hence w(1, 8) = 2.
w(3, 5) = 1 — the subarray is [5, 2, 1], and each of 1, 2, 5 occur exactly once in this subarray. Of these, only 1 equals the number of its occurrences in this subarray.
Given A, your task is to compute the sum of the weirdness of all subarrays of A, i.e, the value
\sum_{L = 1}^{N} \sum_{R = L}^N w(L, R)
------ Input Format ------
- The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains an integer N, the size of the given array.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N} — the elements of A.
------ Output Format ------
For each test case, output a single line containing a single integer — the sum of the weirdness of all subarrays of the given array.
------ Constraints ------
$1 ≤ T ≤ 10^{3}$
$1 ≤ N ≤ 10^{6}$
$1 ≤ A_{i} ≤ 10^{6}$
- The sum of $N$ across all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
4
3
3 3 3
4
1 2 1 2
5
1 2 3 2 1
8
1 2 2 3 2 3 3 1
----- Sample Output 1 ------
3
10
16
54
----- explanation 1 ------
Test Case 1: The only subarray with non-zero weirdness is the whole array, which has $w(1, 3) = 3$ because every value is $3$ and it appears thrice.
Test Case 2: There are $10$ subarrays. Their weirdness values are as follows:
- $w(1, 1) = 1$, because $A_{1} = 1$ and $f_{1, 1}(1) = 1$.
- $w(1, 2) = 1$, because $A_{1} = 1$ and $f_{1, 2}(1) = 1$; but $A_{2} = 2$ and $f_{1, 2}(2) = 1 \neq 2$.
- $w(1, 3) = 0$, here $f_{1, 3}(1) = 2 \neq 1$ and $f_{1, 3}(2) = 1 \neq 2$ so no index satisfies the condition.
- $w(1, 4) = 2$, here indices $2$ and $4$ satisfy the condition.
- $w(2, 2) = 0$
- $w(2, 3) = 1$, index $3$ satisfies the condition.
- $w(2, 4) = 3$, all indices satisfy the condition.
- $w(3, 3) = 1$
- $w(3, 4) = 1$, index $3$ satisfies the condition.
- $w(4, 4) = 0$
|
for tc in range(int(input())):
n = int(input())
ls = list(map(int, input().split()))
freq = dict()
for x in ls:
if freq.get(x) is None:
freq[x] = 1
else:
freq[x] += 1
dis = dict()
for x in range(len(ls)):
if freq[ls[x]] >= ls[x]:
if dis.get(ls[x]) is None:
dis[ls[x]] = [x]
else:
dis[ls[x]].append(x)
res = 0
for x in dis:
ta = 0
prev = 0
for r in range(x - 1, len(dis[x])):
leftSena = dis[x][ta] - prev + 1
if r == len(dis[x]) - 1:
rightSena = len(ls) - 1 - dis[x][r] + 1
else:
rightSena = dis[x][r + 1] - 1 - dis[x][r] + 1
res += leftSena * rightSena * x
prev = dis[x][ta] + 1
ta += 1
print(res)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF FUNC_CALL VAR VAR NONE ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR NONE ASSIGN VAR VAR VAR LIST VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You are given an array A = [A_{1}, A_{2}, \ldots, A_{N}] containing N integers.
Consider any subarray [A_{L}, A_{L+1}, \ldots, A_{R}] of this subarray (where 1≤ L ≤ R ≤ N). Ther weirdness of this subarray, denoted w(L, R), is defined to be the number of indices L ≤ i ≤ R such that A_{i} equals the number of occurrences of A_{i} in this subarray.
Formally, we define w(L, R) for 1 ≤ L ≤ R ≤ N as follows:
For every integer x, let f_{L, R}(x) denote the number of indices L ≤ j ≤ R such that A_{j} = x.
w(L, R) then equals the number of L ≤ i ≤ R such that A_{i} = f_{L, R}(A_{i})
For example, let A = [1, 3, 5, 2, 1, 2, 9, 5]. Then,
w(1, 8) = 2, which can be computed as follows.
We have
- f_{1, 8}(1) = 2
- f_{1, 8}(2) = 2
- f_{1, 8}(3) = 1
- f_{1, 8}(5) = 2
- f_{1, 8}(9) = 1
The only indices which satisfy f_{1, 8}(A_{i}) = A_{i} are i = 4 and i = 6, and hence w(1, 8) = 2.
w(3, 5) = 1 — the subarray is [5, 2, 1], and each of 1, 2, 5 occur exactly once in this subarray. Of these, only 1 equals the number of its occurrences in this subarray.
Given A, your task is to compute the sum of the weirdness of all subarrays of A, i.e, the value
\sum_{L = 1}^{N} \sum_{R = L}^N w(L, R)
------ Input Format ------
- The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains an integer N, the size of the given array.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N} — the elements of A.
------ Output Format ------
For each test case, output a single line containing a single integer — the sum of the weirdness of all subarrays of the given array.
------ Constraints ------
$1 ≤ T ≤ 10^{3}$
$1 ≤ N ≤ 10^{6}$
$1 ≤ A_{i} ≤ 10^{6}$
- The sum of $N$ across all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
4
3
3 3 3
4
1 2 1 2
5
1 2 3 2 1
8
1 2 2 3 2 3 3 1
----- Sample Output 1 ------
3
10
16
54
----- explanation 1 ------
Test Case 1: The only subarray with non-zero weirdness is the whole array, which has $w(1, 3) = 3$ because every value is $3$ and it appears thrice.
Test Case 2: There are $10$ subarrays. Their weirdness values are as follows:
- $w(1, 1) = 1$, because $A_{1} = 1$ and $f_{1, 1}(1) = 1$.
- $w(1, 2) = 1$, because $A_{1} = 1$ and $f_{1, 2}(1) = 1$; but $A_{2} = 2$ and $f_{1, 2}(2) = 1 \neq 2$.
- $w(1, 3) = 0$, here $f_{1, 3}(1) = 2 \neq 1$ and $f_{1, 3}(2) = 1 \neq 2$ so no index satisfies the condition.
- $w(1, 4) = 2$, here indices $2$ and $4$ satisfy the condition.
- $w(2, 2) = 0$
- $w(2, 3) = 1$, index $3$ satisfies the condition.
- $w(2, 4) = 3$, all indices satisfy the condition.
- $w(3, 3) = 1$
- $w(3, 4) = 1$, index $3$ satisfies the condition.
- $w(4, 4) = 0$
|
def read_int_ist():
lst = list(map(int, input().split(" ")))
return lst
def read_int_values():
return map(int, input().split(" "))
def solve(len1, a) -> int:
ans = 0
lst_indices = [[] for _ in range(len1 + 1)]
for i in range(1, len(a) + 1):
if a[i - 1] <= len1:
lst_indices[a[i - 1]].append(i)
for x in range(1, n + 1):
lst_index = list(lst_indices[x])
lst_index.insert(0, 0)
lst_index.insert(len(lst_index), n + 1)
for i in range(1, len(lst_index) - x):
ans += (
x
* (lst_index[i] - lst_index[i - 1])
* (lst_index[i + x] - lst_index[i + x - 1])
)
return ans
for _ in range(int(input())):
n = int(input())
lst = read_int_ist()
print(solve(n, lst))
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR 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 VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
You are given an array A = [A_{1}, A_{2}, \ldots, A_{N}] containing N integers.
Consider any subarray [A_{L}, A_{L+1}, \ldots, A_{R}] of this subarray (where 1≤ L ≤ R ≤ N). Ther weirdness of this subarray, denoted w(L, R), is defined to be the number of indices L ≤ i ≤ R such that A_{i} equals the number of occurrences of A_{i} in this subarray.
Formally, we define w(L, R) for 1 ≤ L ≤ R ≤ N as follows:
For every integer x, let f_{L, R}(x) denote the number of indices L ≤ j ≤ R such that A_{j} = x.
w(L, R) then equals the number of L ≤ i ≤ R such that A_{i} = f_{L, R}(A_{i})
For example, let A = [1, 3, 5, 2, 1, 2, 9, 5]. Then,
w(1, 8) = 2, which can be computed as follows.
We have
- f_{1, 8}(1) = 2
- f_{1, 8}(2) = 2
- f_{1, 8}(3) = 1
- f_{1, 8}(5) = 2
- f_{1, 8}(9) = 1
The only indices which satisfy f_{1, 8}(A_{i}) = A_{i} are i = 4 and i = 6, and hence w(1, 8) = 2.
w(3, 5) = 1 — the subarray is [5, 2, 1], and each of 1, 2, 5 occur exactly once in this subarray. Of these, only 1 equals the number of its occurrences in this subarray.
Given A, your task is to compute the sum of the weirdness of all subarrays of A, i.e, the value
\sum_{L = 1}^{N} \sum_{R = L}^N w(L, R)
------ Input Format ------
- The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains an integer N, the size of the given array.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N} — the elements of A.
------ Output Format ------
For each test case, output a single line containing a single integer — the sum of the weirdness of all subarrays of the given array.
------ Constraints ------
$1 ≤ T ≤ 10^{3}$
$1 ≤ N ≤ 10^{6}$
$1 ≤ A_{i} ≤ 10^{6}$
- The sum of $N$ across all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
4
3
3 3 3
4
1 2 1 2
5
1 2 3 2 1
8
1 2 2 3 2 3 3 1
----- Sample Output 1 ------
3
10
16
54
----- explanation 1 ------
Test Case 1: The only subarray with non-zero weirdness is the whole array, which has $w(1, 3) = 3$ because every value is $3$ and it appears thrice.
Test Case 2: There are $10$ subarrays. Their weirdness values are as follows:
- $w(1, 1) = 1$, because $A_{1} = 1$ and $f_{1, 1}(1) = 1$.
- $w(1, 2) = 1$, because $A_{1} = 1$ and $f_{1, 2}(1) = 1$; but $A_{2} = 2$ and $f_{1, 2}(2) = 1 \neq 2$.
- $w(1, 3) = 0$, here $f_{1, 3}(1) = 2 \neq 1$ and $f_{1, 3}(2) = 1 \neq 2$ so no index satisfies the condition.
- $w(1, 4) = 2$, here indices $2$ and $4$ satisfy the condition.
- $w(2, 2) = 0$
- $w(2, 3) = 1$, index $3$ satisfies the condition.
- $w(2, 4) = 3$, all indices satisfy the condition.
- $w(3, 3) = 1$
- $w(3, 4) = 1$, index $3$ satisfies the condition.
- $w(4, 4) = 0$
|
for _ in range(int(input())):
n = int(input())
x = list(map(int, input().split()))
for i in range(n):
x[i] = [x[i], i]
x = sorted(x)
xd = []
xd += [x[0][0], [x[0][1]]]
for i in range(1, n):
if x[i][0] == x[i - 1][0]:
xd[-1] += [x[i][1]]
else:
xd += [x[i][0], [x[i][1]]]
xd = {xd[i]: xd[i + 1] for i in range(0, len(xd), 2)}
ans = 0
for k, v in xd.items():
if len(v) > k:
temp = 0
temp += (v[0] + 1) * (v[k] - v[k - 1])
for i in range(1, len(v)):
if i + k < len(v):
temp += (v[i] - v[i - 1]) * (v[i + k] - v[i + k - 1])
elif i + k == len(v):
temp += (v[i] - v[i - 1]) * (n - v[i + k - 1])
else:
break
ans += temp * k
elif len(v) == k:
ans += k * (v[0] + 1) * (n - v[k - 1])
print(ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR LIST VAR NUMBER NUMBER LIST VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER LIST VAR VAR NUMBER VAR LIST VAR VAR NUMBER LIST VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR FUNC_CALL 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 IF BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR IF FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You are given an array A = [A_{1}, A_{2}, \ldots, A_{N}] containing N integers.
Consider any subarray [A_{L}, A_{L+1}, \ldots, A_{R}] of this subarray (where 1≤ L ≤ R ≤ N). Ther weirdness of this subarray, denoted w(L, R), is defined to be the number of indices L ≤ i ≤ R such that A_{i} equals the number of occurrences of A_{i} in this subarray.
Formally, we define w(L, R) for 1 ≤ L ≤ R ≤ N as follows:
For every integer x, let f_{L, R}(x) denote the number of indices L ≤ j ≤ R such that A_{j} = x.
w(L, R) then equals the number of L ≤ i ≤ R such that A_{i} = f_{L, R}(A_{i})
For example, let A = [1, 3, 5, 2, 1, 2, 9, 5]. Then,
w(1, 8) = 2, which can be computed as follows.
We have
- f_{1, 8}(1) = 2
- f_{1, 8}(2) = 2
- f_{1, 8}(3) = 1
- f_{1, 8}(5) = 2
- f_{1, 8}(9) = 1
The only indices which satisfy f_{1, 8}(A_{i}) = A_{i} are i = 4 and i = 6, and hence w(1, 8) = 2.
w(3, 5) = 1 — the subarray is [5, 2, 1], and each of 1, 2, 5 occur exactly once in this subarray. Of these, only 1 equals the number of its occurrences in this subarray.
Given A, your task is to compute the sum of the weirdness of all subarrays of A, i.e, the value
\sum_{L = 1}^{N} \sum_{R = L}^N w(L, R)
------ Input Format ------
- The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains an integer N, the size of the given array.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N} — the elements of A.
------ Output Format ------
For each test case, output a single line containing a single integer — the sum of the weirdness of all subarrays of the given array.
------ Constraints ------
$1 ≤ T ≤ 10^{3}$
$1 ≤ N ≤ 10^{6}$
$1 ≤ A_{i} ≤ 10^{6}$
- The sum of $N$ across all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
4
3
3 3 3
4
1 2 1 2
5
1 2 3 2 1
8
1 2 2 3 2 3 3 1
----- Sample Output 1 ------
3
10
16
54
----- explanation 1 ------
Test Case 1: The only subarray with non-zero weirdness is the whole array, which has $w(1, 3) = 3$ because every value is $3$ and it appears thrice.
Test Case 2: There are $10$ subarrays. Their weirdness values are as follows:
- $w(1, 1) = 1$, because $A_{1} = 1$ and $f_{1, 1}(1) = 1$.
- $w(1, 2) = 1$, because $A_{1} = 1$ and $f_{1, 2}(1) = 1$; but $A_{2} = 2$ and $f_{1, 2}(2) = 1 \neq 2$.
- $w(1, 3) = 0$, here $f_{1, 3}(1) = 2 \neq 1$ and $f_{1, 3}(2) = 1 \neq 2$ so no index satisfies the condition.
- $w(1, 4) = 2$, here indices $2$ and $4$ satisfy the condition.
- $w(2, 2) = 0$
- $w(2, 3) = 1$, index $3$ satisfies the condition.
- $w(2, 4) = 3$, all indices satisfy the condition.
- $w(3, 3) = 1$
- $w(3, 4) = 1$, index $3$ satisfies the condition.
- $w(4, 4) = 0$
|
for t in range(int(input())):
N = int(input())
A = list(map(int, input().split()))
dic = {}
count = 0
for i in range(N):
if A[i] not in dic:
dic[A[i]] = []
dic[A[i]].append(i)
for i in dic.keys():
if len(dic.get(i)) < i:
continue
temp = []
temp += dic.get(i)
temp.insert(0, -1)
temp.append(N)
for j in range(1, len(temp) - i):
count += i * (temp[j] - temp[j - 1]) * (temp[i + j] - temp[i + j - 1])
print(count)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You are given an array A = [A_{1}, A_{2}, \ldots, A_{N}] containing N integers.
Consider any subarray [A_{L}, A_{L+1}, \ldots, A_{R}] of this subarray (where 1≤ L ≤ R ≤ N). Ther weirdness of this subarray, denoted w(L, R), is defined to be the number of indices L ≤ i ≤ R such that A_{i} equals the number of occurrences of A_{i} in this subarray.
Formally, we define w(L, R) for 1 ≤ L ≤ R ≤ N as follows:
For every integer x, let f_{L, R}(x) denote the number of indices L ≤ j ≤ R such that A_{j} = x.
w(L, R) then equals the number of L ≤ i ≤ R such that A_{i} = f_{L, R}(A_{i})
For example, let A = [1, 3, 5, 2, 1, 2, 9, 5]. Then,
w(1, 8) = 2, which can be computed as follows.
We have
- f_{1, 8}(1) = 2
- f_{1, 8}(2) = 2
- f_{1, 8}(3) = 1
- f_{1, 8}(5) = 2
- f_{1, 8}(9) = 1
The only indices which satisfy f_{1, 8}(A_{i}) = A_{i} are i = 4 and i = 6, and hence w(1, 8) = 2.
w(3, 5) = 1 — the subarray is [5, 2, 1], and each of 1, 2, 5 occur exactly once in this subarray. Of these, only 1 equals the number of its occurrences in this subarray.
Given A, your task is to compute the sum of the weirdness of all subarrays of A, i.e, the value
\sum_{L = 1}^{N} \sum_{R = L}^N w(L, R)
------ Input Format ------
- The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains an integer N, the size of the given array.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N} — the elements of A.
------ Output Format ------
For each test case, output a single line containing a single integer — the sum of the weirdness of all subarrays of the given array.
------ Constraints ------
$1 ≤ T ≤ 10^{3}$
$1 ≤ N ≤ 10^{6}$
$1 ≤ A_{i} ≤ 10^{6}$
- The sum of $N$ across all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
4
3
3 3 3
4
1 2 1 2
5
1 2 3 2 1
8
1 2 2 3 2 3 3 1
----- Sample Output 1 ------
3
10
16
54
----- explanation 1 ------
Test Case 1: The only subarray with non-zero weirdness is the whole array, which has $w(1, 3) = 3$ because every value is $3$ and it appears thrice.
Test Case 2: There are $10$ subarrays. Their weirdness values are as follows:
- $w(1, 1) = 1$, because $A_{1} = 1$ and $f_{1, 1}(1) = 1$.
- $w(1, 2) = 1$, because $A_{1} = 1$ and $f_{1, 2}(1) = 1$; but $A_{2} = 2$ and $f_{1, 2}(2) = 1 \neq 2$.
- $w(1, 3) = 0$, here $f_{1, 3}(1) = 2 \neq 1$ and $f_{1, 3}(2) = 1 \neq 2$ so no index satisfies the condition.
- $w(1, 4) = 2$, here indices $2$ and $4$ satisfy the condition.
- $w(2, 2) = 0$
- $w(2, 3) = 1$, index $3$ satisfies the condition.
- $w(2, 4) = 3$, all indices satisfy the condition.
- $w(3, 3) = 1$
- $w(3, 4) = 1$, index $3$ satisfies the condition.
- $w(4, 4) = 0$
|
for _ in range(int(input())):
n = int(input())
l = list(map(int, input().split()))
d = {}
for i in range(n):
if l[i] not in d:
d[l[i]] = [0, i + 1]
else:
d[l[i]].append(i + 1)
ans = 0
for k, v in d.items():
v.append(n + 1)
if len(v) >= k + 2:
l = 1
r = l + k - 1
temp = 0
while r < len(v) - 1:
temp += abs(v[l] - v[l - 1]) * abs(v[r] - v[r + 1])
l += 1
r += 1
ans += temp * k
print(ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR LIST NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
|
You are given an array A = [A_{1}, A_{2}, \ldots, A_{N}] containing N integers.
Consider any subarray [A_{L}, A_{L+1}, \ldots, A_{R}] of this subarray (where 1≤ L ≤ R ≤ N). Ther weirdness of this subarray, denoted w(L, R), is defined to be the number of indices L ≤ i ≤ R such that A_{i} equals the number of occurrences of A_{i} in this subarray.
Formally, we define w(L, R) for 1 ≤ L ≤ R ≤ N as follows:
For every integer x, let f_{L, R}(x) denote the number of indices L ≤ j ≤ R such that A_{j} = x.
w(L, R) then equals the number of L ≤ i ≤ R such that A_{i} = f_{L, R}(A_{i})
For example, let A = [1, 3, 5, 2, 1, 2, 9, 5]. Then,
w(1, 8) = 2, which can be computed as follows.
We have
- f_{1, 8}(1) = 2
- f_{1, 8}(2) = 2
- f_{1, 8}(3) = 1
- f_{1, 8}(5) = 2
- f_{1, 8}(9) = 1
The only indices which satisfy f_{1, 8}(A_{i}) = A_{i} are i = 4 and i = 6, and hence w(1, 8) = 2.
w(3, 5) = 1 — the subarray is [5, 2, 1], and each of 1, 2, 5 occur exactly once in this subarray. Of these, only 1 equals the number of its occurrences in this subarray.
Given A, your task is to compute the sum of the weirdness of all subarrays of A, i.e, the value
\sum_{L = 1}^{N} \sum_{R = L}^N w(L, R)
------ Input Format ------
- The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains an integer N, the size of the given array.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N} — the elements of A.
------ Output Format ------
For each test case, output a single line containing a single integer — the sum of the weirdness of all subarrays of the given array.
------ Constraints ------
$1 ≤ T ≤ 10^{3}$
$1 ≤ N ≤ 10^{6}$
$1 ≤ A_{i} ≤ 10^{6}$
- The sum of $N$ across all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
4
3
3 3 3
4
1 2 1 2
5
1 2 3 2 1
8
1 2 2 3 2 3 3 1
----- Sample Output 1 ------
3
10
16
54
----- explanation 1 ------
Test Case 1: The only subarray with non-zero weirdness is the whole array, which has $w(1, 3) = 3$ because every value is $3$ and it appears thrice.
Test Case 2: There are $10$ subarrays. Their weirdness values are as follows:
- $w(1, 1) = 1$, because $A_{1} = 1$ and $f_{1, 1}(1) = 1$.
- $w(1, 2) = 1$, because $A_{1} = 1$ and $f_{1, 2}(1) = 1$; but $A_{2} = 2$ and $f_{1, 2}(2) = 1 \neq 2$.
- $w(1, 3) = 0$, here $f_{1, 3}(1) = 2 \neq 1$ and $f_{1, 3}(2) = 1 \neq 2$ so no index satisfies the condition.
- $w(1, 4) = 2$, here indices $2$ and $4$ satisfy the condition.
- $w(2, 2) = 0$
- $w(2, 3) = 1$, index $3$ satisfies the condition.
- $w(2, 4) = 3$, all indices satisfy the condition.
- $w(3, 3) = 1$
- $w(3, 4) = 1$, index $3$ satisfies the condition.
- $w(4, 4) = 0$
|
def correction(lst, n, m):
lst.append(n)
lst.append(None)
for i in range(m, -1, -1):
lst[i + 1] = lst[i]
lst[0] = -1
def solve():
n = int(input())
arr = [[] for i in range(n + 1)]
count = 0
for x in map(int, input().split()):
if x <= n:
arr[x].append(count)
count += 1
ans = 0
for j in range(n + 1):
l1 = arr[j]
m = len(l1)
if m >= j:
correction(l1, n, m)
k = 1
while k + j - 1 < m + 1:
L, R = k, k + j - 1
ans += (l1[L] - l1[L - 1]) * (l1[R + 1] - l1[R]) * j
k += 1
print(ans)
t = int(input())
for _ in range(t):
solve()
|
FUNC_DEF EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NONE FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
You are given an array A = [A_{1}, A_{2}, \ldots, A_{N}] containing N integers.
Consider any subarray [A_{L}, A_{L+1}, \ldots, A_{R}] of this subarray (where 1≤ L ≤ R ≤ N). Ther weirdness of this subarray, denoted w(L, R), is defined to be the number of indices L ≤ i ≤ R such that A_{i} equals the number of occurrences of A_{i} in this subarray.
Formally, we define w(L, R) for 1 ≤ L ≤ R ≤ N as follows:
For every integer x, let f_{L, R}(x) denote the number of indices L ≤ j ≤ R such that A_{j} = x.
w(L, R) then equals the number of L ≤ i ≤ R such that A_{i} = f_{L, R}(A_{i})
For example, let A = [1, 3, 5, 2, 1, 2, 9, 5]. Then,
w(1, 8) = 2, which can be computed as follows.
We have
- f_{1, 8}(1) = 2
- f_{1, 8}(2) = 2
- f_{1, 8}(3) = 1
- f_{1, 8}(5) = 2
- f_{1, 8}(9) = 1
The only indices which satisfy f_{1, 8}(A_{i}) = A_{i} are i = 4 and i = 6, and hence w(1, 8) = 2.
w(3, 5) = 1 — the subarray is [5, 2, 1], and each of 1, 2, 5 occur exactly once in this subarray. Of these, only 1 equals the number of its occurrences in this subarray.
Given A, your task is to compute the sum of the weirdness of all subarrays of A, i.e, the value
\sum_{L = 1}^{N} \sum_{R = L}^N w(L, R)
------ Input Format ------
- The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains an integer N, the size of the given array.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N} — the elements of A.
------ Output Format ------
For each test case, output a single line containing a single integer — the sum of the weirdness of all subarrays of the given array.
------ Constraints ------
$1 ≤ T ≤ 10^{3}$
$1 ≤ N ≤ 10^{6}$
$1 ≤ A_{i} ≤ 10^{6}$
- The sum of $N$ across all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
4
3
3 3 3
4
1 2 1 2
5
1 2 3 2 1
8
1 2 2 3 2 3 3 1
----- Sample Output 1 ------
3
10
16
54
----- explanation 1 ------
Test Case 1: The only subarray with non-zero weirdness is the whole array, which has $w(1, 3) = 3$ because every value is $3$ and it appears thrice.
Test Case 2: There are $10$ subarrays. Their weirdness values are as follows:
- $w(1, 1) = 1$, because $A_{1} = 1$ and $f_{1, 1}(1) = 1$.
- $w(1, 2) = 1$, because $A_{1} = 1$ and $f_{1, 2}(1) = 1$; but $A_{2} = 2$ and $f_{1, 2}(2) = 1 \neq 2$.
- $w(1, 3) = 0$, here $f_{1, 3}(1) = 2 \neq 1$ and $f_{1, 3}(2) = 1 \neq 2$ so no index satisfies the condition.
- $w(1, 4) = 2$, here indices $2$ and $4$ satisfy the condition.
- $w(2, 2) = 0$
- $w(2, 3) = 1$, index $3$ satisfies the condition.
- $w(2, 4) = 3$, all indices satisfy the condition.
- $w(3, 3) = 1$
- $w(3, 4) = 1$, index $3$ satisfies the condition.
- $w(4, 4) = 0$
|
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
freq = []
d = {}
for i in range(len(a)):
if a[i] in d:
d[a[i]] += [i]
else:
d[a[i]] = [-1, i]
result = 0
for item in d:
if len(d[item]) < item:
continue
d[item] += [n]
l = 1
r = item
while r < len(d[item]) - 1:
ans = (d[item][l] - d[item][l - 1]) * (d[item][r + 1] - d[item][r]) * item
result += ans
r += 1
l += 1
print(result)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR LIST VAR ASSIGN VAR VAR VAR LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR LIST VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
You are given an array A = [A_{1}, A_{2}, \ldots, A_{N}] containing N integers.
Consider any subarray [A_{L}, A_{L+1}, \ldots, A_{R}] of this subarray (where 1≤ L ≤ R ≤ N). Ther weirdness of this subarray, denoted w(L, R), is defined to be the number of indices L ≤ i ≤ R such that A_{i} equals the number of occurrences of A_{i} in this subarray.
Formally, we define w(L, R) for 1 ≤ L ≤ R ≤ N as follows:
For every integer x, let f_{L, R}(x) denote the number of indices L ≤ j ≤ R such that A_{j} = x.
w(L, R) then equals the number of L ≤ i ≤ R such that A_{i} = f_{L, R}(A_{i})
For example, let A = [1, 3, 5, 2, 1, 2, 9, 5]. Then,
w(1, 8) = 2, which can be computed as follows.
We have
- f_{1, 8}(1) = 2
- f_{1, 8}(2) = 2
- f_{1, 8}(3) = 1
- f_{1, 8}(5) = 2
- f_{1, 8}(9) = 1
The only indices which satisfy f_{1, 8}(A_{i}) = A_{i} are i = 4 and i = 6, and hence w(1, 8) = 2.
w(3, 5) = 1 — the subarray is [5, 2, 1], and each of 1, 2, 5 occur exactly once in this subarray. Of these, only 1 equals the number of its occurrences in this subarray.
Given A, your task is to compute the sum of the weirdness of all subarrays of A, i.e, the value
\sum_{L = 1}^{N} \sum_{R = L}^N w(L, R)
------ Input Format ------
- The first line of input contains a single integer T, denoting the number of test cases. The description of T test cases follows.
- The first line of each test case contains an integer N, the size of the given array.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N} — the elements of A.
------ Output Format ------
For each test case, output a single line containing a single integer — the sum of the weirdness of all subarrays of the given array.
------ Constraints ------
$1 ≤ T ≤ 10^{3}$
$1 ≤ N ≤ 10^{6}$
$1 ≤ A_{i} ≤ 10^{6}$
- The sum of $N$ across all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
4
3
3 3 3
4
1 2 1 2
5
1 2 3 2 1
8
1 2 2 3 2 3 3 1
----- Sample Output 1 ------
3
10
16
54
----- explanation 1 ------
Test Case 1: The only subarray with non-zero weirdness is the whole array, which has $w(1, 3) = 3$ because every value is $3$ and it appears thrice.
Test Case 2: There are $10$ subarrays. Their weirdness values are as follows:
- $w(1, 1) = 1$, because $A_{1} = 1$ and $f_{1, 1}(1) = 1$.
- $w(1, 2) = 1$, because $A_{1} = 1$ and $f_{1, 2}(1) = 1$; but $A_{2} = 2$ and $f_{1, 2}(2) = 1 \neq 2$.
- $w(1, 3) = 0$, here $f_{1, 3}(1) = 2 \neq 1$ and $f_{1, 3}(2) = 1 \neq 2$ so no index satisfies the condition.
- $w(1, 4) = 2$, here indices $2$ and $4$ satisfy the condition.
- $w(2, 2) = 0$
- $w(2, 3) = 1$, index $3$ satisfies the condition.
- $w(2, 4) = 3$, all indices satisfy the condition.
- $w(3, 3) = 1$
- $w(3, 4) = 1$, index $3$ satisfies the condition.
- $w(4, 4) = 0$
|
import sys
input = sys.stdin.readline
def solve(a, x):
m = len(a)
if m == 0:
return 0
ans = 0
for i in range(m - x + 1):
L, R = i, i + x - 1
l = a[L] - (a[L - 1] if L else -1)
r = (a[R + 1] if R + 1 < m else n) - a[R]
ans += l * r
return ans
for _ in range(int(input())):
n = int(input())
lst = list(map(int, input().split()))
b = [[] for _ in range(n + 1)]
for i, x in enumerate(lst):
if x <= n:
b[x].append(i)
print(sum(solve(v, i) * i for i, v in enumerate(b)))
|
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR
|
On each of the following $N$ days (numbered $1$ through $N$), Chef is planning to cook either pizza or broccoli. He wrote down a string $A$ with length $N$, where for each valid $i$, if the character $A_i$ is '1', then he will cook pizza on the $i$-th day, while if $A_i$ is '0', he will cook broccoli on this day.
Chefu, his son, loves pizza but hates broccoli ― just like most kids. He wants to select a substring of $A$ with length $K$ and change each character '0' in this substring to '1'. Afterwards, let's define pizza time as the maximum number of consecutive days where Chef will cook pizza. Find the maximum pizza time Chefu can achieve.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $K$.
- The second line contains a string $A$ with length $N$.
-----Output-----
For each test case, print a single line containing one integer ― the maximum pizza time.
-----Constraints-----
- $1 \le T \le 1,000$
- $1 \le K \le N \le 10^5$
- $A$ contains only characters '0' and '1'
- the sum of $N$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (50 points):
- $N \le 10^3$
- the sum of $N$ over all test cases does not exceed $10^4$
Subtask #2 (50 points): original constraints
-----Example Input-----
2
13 2
0101110000101
6 3
100001
-----Example Output-----
5
4
-----Explanation-----
Example case 1: Chefu can choose the substring $A[2, 3]$ = "10", and change the third character of $A$ to '1'. Then, the pizza time is $5$ days: from day $2$ to day $6$.
Example case 2: Chefu can choose the substring $A[2, 4]$ = "000". Then, the pizza time is $4$ days: from day $1$ to day $4$.
|
t = int(input())
for _ in range(t):
n, k = list(map(int, input().split()))
a = input()
pre = [0] * n
post = [0] * n
count = 0
for i in range(n):
if a[i] == "1":
count += 1
else:
count = 0
pre[i] = count
count = 0
for i in range(n - 1, -1, -1):
if a[i] == "1":
count += 1
else:
count = 0
post[i] = count
sol = 0
for i in range(0, n - k + 1):
time = 0
if i > 0:
time += pre[i - 1]
time += k
if i + k < n:
time += post[i + k]
sol = max(sol, time)
print(sol)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
On each of the following $N$ days (numbered $1$ through $N$), Chef is planning to cook either pizza or broccoli. He wrote down a string $A$ with length $N$, where for each valid $i$, if the character $A_i$ is '1', then he will cook pizza on the $i$-th day, while if $A_i$ is '0', he will cook broccoli on this day.
Chefu, his son, loves pizza but hates broccoli ― just like most kids. He wants to select a substring of $A$ with length $K$ and change each character '0' in this substring to '1'. Afterwards, let's define pizza time as the maximum number of consecutive days where Chef will cook pizza. Find the maximum pizza time Chefu can achieve.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $K$.
- The second line contains a string $A$ with length $N$.
-----Output-----
For each test case, print a single line containing one integer ― the maximum pizza time.
-----Constraints-----
- $1 \le T \le 1,000$
- $1 \le K \le N \le 10^5$
- $A$ contains only characters '0' and '1'
- the sum of $N$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (50 points):
- $N \le 10^3$
- the sum of $N$ over all test cases does not exceed $10^4$
Subtask #2 (50 points): original constraints
-----Example Input-----
2
13 2
0101110000101
6 3
100001
-----Example Output-----
5
4
-----Explanation-----
Example case 1: Chefu can choose the substring $A[2, 3]$ = "10", and change the third character of $A$ to '1'. Then, the pizza time is $5$ days: from day $2$ to day $6$.
Example case 2: Chefu can choose the substring $A[2, 4]$ = "000". Then, the pizza time is $4$ days: from day $1$ to day $4$.
|
for _ in range(int(input())):
n, k = map(int, input().split())
l = list(input())
l1 = [0] * n
l2 = [0] * n
for i in range(1, n):
if l[i - 1] == "1":
l1[i] = l1[i - 1] + 1
for i in range(n - 2, -1, -1):
if l[i + 1] == "1":
l2[i] = l2[i + 1] + 1
ans = 0
for i in range(n - k + 1):
ans = max(ans, l1[i] + l2[i + k - 1] + k)
print(ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
|
On each of the following $N$ days (numbered $1$ through $N$), Chef is planning to cook either pizza or broccoli. He wrote down a string $A$ with length $N$, where for each valid $i$, if the character $A_i$ is '1', then he will cook pizza on the $i$-th day, while if $A_i$ is '0', he will cook broccoli on this day.
Chefu, his son, loves pizza but hates broccoli ― just like most kids. He wants to select a substring of $A$ with length $K$ and change each character '0' in this substring to '1'. Afterwards, let's define pizza time as the maximum number of consecutive days where Chef will cook pizza. Find the maximum pizza time Chefu can achieve.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $K$.
- The second line contains a string $A$ with length $N$.
-----Output-----
For each test case, print a single line containing one integer ― the maximum pizza time.
-----Constraints-----
- $1 \le T \le 1,000$
- $1 \le K \le N \le 10^5$
- $A$ contains only characters '0' and '1'
- the sum of $N$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (50 points):
- $N \le 10^3$
- the sum of $N$ over all test cases does not exceed $10^4$
Subtask #2 (50 points): original constraints
-----Example Input-----
2
13 2
0101110000101
6 3
100001
-----Example Output-----
5
4
-----Explanation-----
Example case 1: Chefu can choose the substring $A[2, 3]$ = "10", and change the third character of $A$ to '1'. Then, the pizza time is $5$ days: from day $2$ to day $6$.
Example case 2: Chefu can choose the substring $A[2, 4]$ = "000". Then, the pizza time is $4$ days: from day $1$ to day $4$.
|
for z in range(int(input())):
n, k = map(int, input().split())
s = input()
maxl = 0
for i in range(0, n - k + 1):
j = i - 1
c0 = 0
c1 = 0
while j >= 0:
if s[j] == "1":
c0 += 1
j -= 1
else:
break
j = i + k
while j < n:
if s[j] == "1":
c1 += 1
j += 1
else:
break
if maxl < c0 + c1 + k:
maxl = c0 + c1 + k
else:
print(maxl)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR WHILE VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
On each of the following $N$ days (numbered $1$ through $N$), Chef is planning to cook either pizza or broccoli. He wrote down a string $A$ with length $N$, where for each valid $i$, if the character $A_i$ is '1', then he will cook pizza on the $i$-th day, while if $A_i$ is '0', he will cook broccoli on this day.
Chefu, his son, loves pizza but hates broccoli ― just like most kids. He wants to select a substring of $A$ with length $K$ and change each character '0' in this substring to '1'. Afterwards, let's define pizza time as the maximum number of consecutive days where Chef will cook pizza. Find the maximum pizza time Chefu can achieve.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $K$.
- The second line contains a string $A$ with length $N$.
-----Output-----
For each test case, print a single line containing one integer ― the maximum pizza time.
-----Constraints-----
- $1 \le T \le 1,000$
- $1 \le K \le N \le 10^5$
- $A$ contains only characters '0' and '1'
- the sum of $N$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (50 points):
- $N \le 10^3$
- the sum of $N$ over all test cases does not exceed $10^4$
Subtask #2 (50 points): original constraints
-----Example Input-----
2
13 2
0101110000101
6 3
100001
-----Example Output-----
5
4
-----Explanation-----
Example case 1: Chefu can choose the substring $A[2, 3]$ = "10", and change the third character of $A$ to '1'. Then, the pizza time is $5$ days: from day $2$ to day $6$.
Example case 2: Chefu can choose the substring $A[2, 4]$ = "000". Then, the pizza time is $4$ days: from day $1$ to day $4$.
|
for _ in range(int(input())):
n, k = map(int, input().split())
arr = list(input())
final_ans = 0
count = 0
for i in range(n - k + 1):
for j in range(i + k, n):
if arr[j] == "1":
count += 1
else:
break
m = i - 1
while m >= 0 and i != 0:
if arr[m] == "1":
count += 1
else:
break
m -= 1
ans = count + k
count = 0
final_ans = max(ans, final_ans)
print(final_ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
On each of the following $N$ days (numbered $1$ through $N$), Chef is planning to cook either pizza or broccoli. He wrote down a string $A$ with length $N$, where for each valid $i$, if the character $A_i$ is '1', then he will cook pizza on the $i$-th day, while if $A_i$ is '0', he will cook broccoli on this day.
Chefu, his son, loves pizza but hates broccoli ― just like most kids. He wants to select a substring of $A$ with length $K$ and change each character '0' in this substring to '1'. Afterwards, let's define pizza time as the maximum number of consecutive days where Chef will cook pizza. Find the maximum pizza time Chefu can achieve.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $K$.
- The second line contains a string $A$ with length $N$.
-----Output-----
For each test case, print a single line containing one integer ― the maximum pizza time.
-----Constraints-----
- $1 \le T \le 1,000$
- $1 \le K \le N \le 10^5$
- $A$ contains only characters '0' and '1'
- the sum of $N$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (50 points):
- $N \le 10^3$
- the sum of $N$ over all test cases does not exceed $10^4$
Subtask #2 (50 points): original constraints
-----Example Input-----
2
13 2
0101110000101
6 3
100001
-----Example Output-----
5
4
-----Explanation-----
Example case 1: Chefu can choose the substring $A[2, 3]$ = "10", and change the third character of $A$ to '1'. Then, the pizza time is $5$ days: from day $2$ to day $6$.
Example case 2: Chefu can choose the substring $A[2, 4]$ = "000". Then, the pizza time is $4$ days: from day $1$ to day $4$.
|
for _ in range(int(input())):
a, k = map(int, input().split())
inp = [int(i) for i in input()]
conright = list(inp)
prev = conright[0]
for i in range(1, a):
if conright[i] == 1:
if prev == 1:
conright[i] = conright[i - 1] + 1
else:
prev = conright[i]
else:
prev = conright[i]
conleft = list(inp)
prev = conleft[a - 1]
for i in range(a - 2, -1, -1):
if conleft[i] == 1:
if prev == 1:
conleft[i] = conleft[i + 1] + 1
else:
prev = conleft[i]
else:
prev = conleft[i]
conright.insert(0, 0)
conleft.append(0)
m = -1
for i in range(a - k + 1):
insr = conright[i - 1]
insl = conleft[i + k]
m = max(m, k + conright[i] + conleft[i + k])
print(m)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
|
On each of the following $N$ days (numbered $1$ through $N$), Chef is planning to cook either pizza or broccoli. He wrote down a string $A$ with length $N$, where for each valid $i$, if the character $A_i$ is '1', then he will cook pizza on the $i$-th day, while if $A_i$ is '0', he will cook broccoli on this day.
Chefu, his son, loves pizza but hates broccoli ― just like most kids. He wants to select a substring of $A$ with length $K$ and change each character '0' in this substring to '1'. Afterwards, let's define pizza time as the maximum number of consecutive days where Chef will cook pizza. Find the maximum pizza time Chefu can achieve.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $K$.
- The second line contains a string $A$ with length $N$.
-----Output-----
For each test case, print a single line containing one integer ― the maximum pizza time.
-----Constraints-----
- $1 \le T \le 1,000$
- $1 \le K \le N \le 10^5$
- $A$ contains only characters '0' and '1'
- the sum of $N$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (50 points):
- $N \le 10^3$
- the sum of $N$ over all test cases does not exceed $10^4$
Subtask #2 (50 points): original constraints
-----Example Input-----
2
13 2
0101110000101
6 3
100001
-----Example Output-----
5
4
-----Explanation-----
Example case 1: Chefu can choose the substring $A[2, 3]$ = "10", and change the third character of $A$ to '1'. Then, the pizza time is $5$ days: from day $2$ to day $6$.
Example case 2: Chefu can choose the substring $A[2, 4]$ = "000". Then, the pizza time is $4$ days: from day $1$ to day $4$.
|
for _ in range(int(input())):
n, k = list(map(int, input().split()))
arr = str(input())
m = 0
for i in range(n - k + 1):
count = k
j = i - 1
while j > -1 and j < n:
if arr[j] == "1":
count += 1
j -= 1
else:
break
l = i + k
while l < n and l > -1:
if arr[l] == "1":
count += 1
l += 1
else:
break
if count > m:
m = count
print(m)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR WHILE VAR VAR VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
On each of the following $N$ days (numbered $1$ through $N$), Chef is planning to cook either pizza or broccoli. He wrote down a string $A$ with length $N$, where for each valid $i$, if the character $A_i$ is '1', then he will cook pizza on the $i$-th day, while if $A_i$ is '0', he will cook broccoli on this day.
Chefu, his son, loves pizza but hates broccoli ― just like most kids. He wants to select a substring of $A$ with length $K$ and change each character '0' in this substring to '1'. Afterwards, let's define pizza time as the maximum number of consecutive days where Chef will cook pizza. Find the maximum pizza time Chefu can achieve.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $K$.
- The second line contains a string $A$ with length $N$.
-----Output-----
For each test case, print a single line containing one integer ― the maximum pizza time.
-----Constraints-----
- $1 \le T \le 1,000$
- $1 \le K \le N \le 10^5$
- $A$ contains only characters '0' and '1'
- the sum of $N$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (50 points):
- $N \le 10^3$
- the sum of $N$ over all test cases does not exceed $10^4$
Subtask #2 (50 points): original constraints
-----Example Input-----
2
13 2
0101110000101
6 3
100001
-----Example Output-----
5
4
-----Explanation-----
Example case 1: Chefu can choose the substring $A[2, 3]$ = "10", and change the third character of $A$ to '1'. Then, the pizza time is $5$ days: from day $2$ to day $6$.
Example case 2: Chefu can choose the substring $A[2, 4]$ = "000". Then, the pizza time is $4$ days: from day $1$ to day $4$.
|
def binary_search(start, end, l, p):
m = (start + end) // 2
if start >= end:
if l[start] < p:
return start + 1
return start
if l[m] > p:
return binary_search(start, m - 1, l, p)
elif l[m] < p:
return binary_search(m + 1, end, l, p)
elif l[m] == p:
return m
def f():
n, k = map(int, input().split(" "))
a = input()
l = []
for i in range(0, len(a)):
if a[i] == "0":
l.append(i)
if len(l) == 0:
maxi = len(a)
else:
maxi = 0
for i in range(0, len(l)):
t = l[i] + k
t = binary_search(i, len(l) - 1, l, t)
if i == 0:
q = -1
else:
q = l[i - 1]
if t > len(l) - 1:
t = len(a)
maxi = max(t - q - 1, maxi)
break
maxi = max(l[t] - q - 1, maxi)
print(maxi)
for i in range(int(input())):
f()
|
FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR IF VAR VAR VAR RETURN BIN_OP VAR NUMBER RETURN VAR IF VAR VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
|
On each of the following $N$ days (numbered $1$ through $N$), Chef is planning to cook either pizza or broccoli. He wrote down a string $A$ with length $N$, where for each valid $i$, if the character $A_i$ is '1', then he will cook pizza on the $i$-th day, while if $A_i$ is '0', he will cook broccoli on this day.
Chefu, his son, loves pizza but hates broccoli ― just like most kids. He wants to select a substring of $A$ with length $K$ and change each character '0' in this substring to '1'. Afterwards, let's define pizza time as the maximum number of consecutive days where Chef will cook pizza. Find the maximum pizza time Chefu can achieve.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $K$.
- The second line contains a string $A$ with length $N$.
-----Output-----
For each test case, print a single line containing one integer ― the maximum pizza time.
-----Constraints-----
- $1 \le T \le 1,000$
- $1 \le K \le N \le 10^5$
- $A$ contains only characters '0' and '1'
- the sum of $N$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (50 points):
- $N \le 10^3$
- the sum of $N$ over all test cases does not exceed $10^4$
Subtask #2 (50 points): original constraints
-----Example Input-----
2
13 2
0101110000101
6 3
100001
-----Example Output-----
5
4
-----Explanation-----
Example case 1: Chefu can choose the substring $A[2, 3]$ = "10", and change the third character of $A$ to '1'. Then, the pizza time is $5$ days: from day $2$ to day $6$.
Example case 2: Chefu can choose the substring $A[2, 4]$ = "000". Then, the pizza time is $4$ days: from day $1$ to day $4$.
|
def fun(li, k):
head = [0] * len(li)
tail = [0] * len(li)
for i in range(1, len(li)):
if li[i - 1] == "1":
head[i] = head[i - 1] + 1
else:
head[i] = 0
for i in range(len(li) - 2, -1, -1):
if li[i + 1] == "1":
tail[i] = tail[i + 1] + 1
else:
tail[i] = 0
ans = 0
for i in range(0, len(li) - k + 1):
ans = max(ans, k + head[i] + tail[i + k - 1])
return ans
t = int(input())
for i in range(0, t):
s = list(map(int, input().strip().split()))
s1 = s[0]
s2 = s[1]
li = input()
print(fun(li, s2))
|
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
On each of the following $N$ days (numbered $1$ through $N$), Chef is planning to cook either pizza or broccoli. He wrote down a string $A$ with length $N$, where for each valid $i$, if the character $A_i$ is '1', then he will cook pizza on the $i$-th day, while if $A_i$ is '0', he will cook broccoli on this day.
Chefu, his son, loves pizza but hates broccoli ― just like most kids. He wants to select a substring of $A$ with length $K$ and change each character '0' in this substring to '1'. Afterwards, let's define pizza time as the maximum number of consecutive days where Chef will cook pizza. Find the maximum pizza time Chefu can achieve.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $K$.
- The second line contains a string $A$ with length $N$.
-----Output-----
For each test case, print a single line containing one integer ― the maximum pizza time.
-----Constraints-----
- $1 \le T \le 1,000$
- $1 \le K \le N \le 10^5$
- $A$ contains only characters '0' and '1'
- the sum of $N$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (50 points):
- $N \le 10^3$
- the sum of $N$ over all test cases does not exceed $10^4$
Subtask #2 (50 points): original constraints
-----Example Input-----
2
13 2
0101110000101
6 3
100001
-----Example Output-----
5
4
-----Explanation-----
Example case 1: Chefu can choose the substring $A[2, 3]$ = "10", and change the third character of $A$ to '1'. Then, the pizza time is $5$ days: from day $2$ to day $6$.
Example case 2: Chefu can choose the substring $A[2, 4]$ = "000". Then, the pizza time is $4$ days: from day $1$ to day $4$.
|
def pizza_time(a, k, p, n):
return (a.index("0", p + k) if "0" in a[p + k :] else n) - (
a.rindex("0", 0, p) + 1 if "0" in a[:p] else 0
)
for i in range(int(input())):
n, k = map(int, input().split())
a = input()[:n]
if not "0" in a:
print(len(a))
continue
if not "1" in a:
print(k)
continue
if len(a) == k:
print(k)
continue
print(max([pizza_time(a, k, i, n) for i in range(n - k + 1)]))
|
FUNC_DEF RETURN BIN_OP STRING VAR BIN_OP VAR VAR FUNC_CALL VAR STRING BIN_OP VAR VAR VAR STRING VAR VAR BIN_OP FUNC_CALL VAR STRING NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF STRING VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF STRING VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER
|
On each of the following $N$ days (numbered $1$ through $N$), Chef is planning to cook either pizza or broccoli. He wrote down a string $A$ with length $N$, where for each valid $i$, if the character $A_i$ is '1', then he will cook pizza on the $i$-th day, while if $A_i$ is '0', he will cook broccoli on this day.
Chefu, his son, loves pizza but hates broccoli ― just like most kids. He wants to select a substring of $A$ with length $K$ and change each character '0' in this substring to '1'. Afterwards, let's define pizza time as the maximum number of consecutive days where Chef will cook pizza. Find the maximum pizza time Chefu can achieve.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $K$.
- The second line contains a string $A$ with length $N$.
-----Output-----
For each test case, print a single line containing one integer ― the maximum pizza time.
-----Constraints-----
- $1 \le T \le 1,000$
- $1 \le K \le N \le 10^5$
- $A$ contains only characters '0' and '1'
- the sum of $N$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (50 points):
- $N \le 10^3$
- the sum of $N$ over all test cases does not exceed $10^4$
Subtask #2 (50 points): original constraints
-----Example Input-----
2
13 2
0101110000101
6 3
100001
-----Example Output-----
5
4
-----Explanation-----
Example case 1: Chefu can choose the substring $A[2, 3]$ = "10", and change the third character of $A$ to '1'. Then, the pizza time is $5$ days: from day $2$ to day $6$.
Example case 2: Chefu can choose the substring $A[2, 4]$ = "000". Then, the pizza time is $4$ days: from day $1$ to day $4$.
|
def solve(arr, n, k):
D = [0] * (n + 1)
D[n] = 0
for i in range(n - 1, -1, -1):
if arr[i] == 1:
D[i] = D[i + 1] + 1
else:
D[i] = 0
I = [0] * (n + 1)
I[0] = 0
for i in range(1, n + 1):
if arr[i - 1] == 1:
I[i] = I[i - 1] + 1
else:
I[i] = 0
lbs = 0
for i in range(k, n + 1):
if lbs < D[i] + k + I[i - k]:
lbs = D[i] + k + I[i - k]
lbs = max(lbs, max(D), k)
return lbs
t = int(input())
while t > 0:
t -= 1
n, k = map(int, input().split())
arr = list(map(int, list(input().strip())))
print(solve(arr, n, k))
|
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
|
On each of the following $N$ days (numbered $1$ through $N$), Chef is planning to cook either pizza or broccoli. He wrote down a string $A$ with length $N$, where for each valid $i$, if the character $A_i$ is '1', then he will cook pizza on the $i$-th day, while if $A_i$ is '0', he will cook broccoli on this day.
Chefu, his son, loves pizza but hates broccoli ― just like most kids. He wants to select a substring of $A$ with length $K$ and change each character '0' in this substring to '1'. Afterwards, let's define pizza time as the maximum number of consecutive days where Chef will cook pizza. Find the maximum pizza time Chefu can achieve.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $K$.
- The second line contains a string $A$ with length $N$.
-----Output-----
For each test case, print a single line containing one integer ― the maximum pizza time.
-----Constraints-----
- $1 \le T \le 1,000$
- $1 \le K \le N \le 10^5$
- $A$ contains only characters '0' and '1'
- the sum of $N$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (50 points):
- $N \le 10^3$
- the sum of $N$ over all test cases does not exceed $10^4$
Subtask #2 (50 points): original constraints
-----Example Input-----
2
13 2
0101110000101
6 3
100001
-----Example Output-----
5
4
-----Explanation-----
Example case 1: Chefu can choose the substring $A[2, 3]$ = "10", and change the third character of $A$ to '1'. Then, the pizza time is $5$ days: from day $2$ to day $6$.
Example case 2: Chefu can choose the substring $A[2, 4]$ = "000". Then, the pizza time is $4$ days: from day $1$ to day $4$.
|
def cob(l, n):
i = n - 1
count = 0
while i != -1 and l[i] != 0:
i -= 1
count += 1
return count
def coa(l, n, ll):
i = n + 1
count = 0
while i != ll and l[i] != 0:
i += 1
count += 1
return count
tc = int(input())
for _ in range(tc):
n, k = map(int, input().split())
l = list(map(int, list(input())))
l1 = []
for i in range(n - k + 1):
l1.append(cob(l, i) + coa(l, i + k - 1, n))
print(max(l1) + k)
|
FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR
|
On each of the following $N$ days (numbered $1$ through $N$), Chef is planning to cook either pizza or broccoli. He wrote down a string $A$ with length $N$, where for each valid $i$, if the character $A_i$ is '1', then he will cook pizza on the $i$-th day, while if $A_i$ is '0', he will cook broccoli on this day.
Chefu, his son, loves pizza but hates broccoli ― just like most kids. He wants to select a substring of $A$ with length $K$ and change each character '0' in this substring to '1'. Afterwards, let's define pizza time as the maximum number of consecutive days where Chef will cook pizza. Find the maximum pizza time Chefu can achieve.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $K$.
- The second line contains a string $A$ with length $N$.
-----Output-----
For each test case, print a single line containing one integer ― the maximum pizza time.
-----Constraints-----
- $1 \le T \le 1,000$
- $1 \le K \le N \le 10^5$
- $A$ contains only characters '0' and '1'
- the sum of $N$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (50 points):
- $N \le 10^3$
- the sum of $N$ over all test cases does not exceed $10^4$
Subtask #2 (50 points): original constraints
-----Example Input-----
2
13 2
0101110000101
6 3
100001
-----Example Output-----
5
4
-----Explanation-----
Example case 1: Chefu can choose the substring $A[2, 3]$ = "10", and change the third character of $A$ to '1'. Then, the pizza time is $5$ days: from day $2$ to day $6$.
Example case 2: Chefu can choose the substring $A[2, 4]$ = "000". Then, the pizza time is $4$ days: from day $1$ to day $4$.
|
t = int(input())
for _ in range(t):
n, k = list(map(int, input().split()))
string = list(map(int, input()))
max_count = k
for i in range(n - k + 1):
count = k
j = i + k
while j < n and string[j] == 1:
count += 1
j += 1
j = i - 1
while j >= 0 and string[j] == 1:
count += 1
j -= 1
if count >= max_count:
max_count = count
print(max_count)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR WHILE VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
On each of the following $N$ days (numbered $1$ through $N$), Chef is planning to cook either pizza or broccoli. He wrote down a string $A$ with length $N$, where for each valid $i$, if the character $A_i$ is '1', then he will cook pizza on the $i$-th day, while if $A_i$ is '0', he will cook broccoli on this day.
Chefu, his son, loves pizza but hates broccoli ― just like most kids. He wants to select a substring of $A$ with length $K$ and change each character '0' in this substring to '1'. Afterwards, let's define pizza time as the maximum number of consecutive days where Chef will cook pizza. Find the maximum pizza time Chefu can achieve.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $K$.
- The second line contains a string $A$ with length $N$.
-----Output-----
For each test case, print a single line containing one integer ― the maximum pizza time.
-----Constraints-----
- $1 \le T \le 1,000$
- $1 \le K \le N \le 10^5$
- $A$ contains only characters '0' and '1'
- the sum of $N$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (50 points):
- $N \le 10^3$
- the sum of $N$ over all test cases does not exceed $10^4$
Subtask #2 (50 points): original constraints
-----Example Input-----
2
13 2
0101110000101
6 3
100001
-----Example Output-----
5
4
-----Explanation-----
Example case 1: Chefu can choose the substring $A[2, 3]$ = "10", and change the third character of $A$ to '1'. Then, the pizza time is $5$ days: from day $2$ to day $6$.
Example case 2: Chefu can choose the substring $A[2, 4]$ = "000". Then, the pizza time is $4$ days: from day $1$ to day $4$.
|
for t in range(int(input())):
N = input()
A = input()
N = N.split(" ")
k = int(N[1])
N = int(N[0])
longest = 0
max_ = k
for i in range(0, N):
if A[i] == "0":
if i + k >= N:
max_ = max(max_, longest + (N - i))
break
else:
q = i + k
while q < N and A[q] == "1":
q += 1
longest = longest + q - i
max_ = max(max_, longest)
longest = 0
else:
longest += 1
print(max(max_, longest))
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR WHILE VAR VAR VAR VAR STRING VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
|
On each of the following $N$ days (numbered $1$ through $N$), Chef is planning to cook either pizza or broccoli. He wrote down a string $A$ with length $N$, where for each valid $i$, if the character $A_i$ is '1', then he will cook pizza on the $i$-th day, while if $A_i$ is '0', he will cook broccoli on this day.
Chefu, his son, loves pizza but hates broccoli ― just like most kids. He wants to select a substring of $A$ with length $K$ and change each character '0' in this substring to '1'. Afterwards, let's define pizza time as the maximum number of consecutive days where Chef will cook pizza. Find the maximum pizza time Chefu can achieve.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $K$.
- The second line contains a string $A$ with length $N$.
-----Output-----
For each test case, print a single line containing one integer ― the maximum pizza time.
-----Constraints-----
- $1 \le T \le 1,000$
- $1 \le K \le N \le 10^5$
- $A$ contains only characters '0' and '1'
- the sum of $N$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (50 points):
- $N \le 10^3$
- the sum of $N$ over all test cases does not exceed $10^4$
Subtask #2 (50 points): original constraints
-----Example Input-----
2
13 2
0101110000101
6 3
100001
-----Example Output-----
5
4
-----Explanation-----
Example case 1: Chefu can choose the substring $A[2, 3]$ = "10", and change the third character of $A$ to '1'. Then, the pizza time is $5$ days: from day $2$ to day $6$.
Example case 2: Chefu can choose the substring $A[2, 4]$ = "000". Then, the pizza time is $4$ days: from day $1$ to day $4$.
|
for _ in range(int(input())):
n, k = map(int, input().split())
a = input()
heads = [(0) for i in range(n)]
tails = [(0) for i in range(n)]
heads[0] = 0
for i in range(1, n):
if a[i - 1] == "1":
heads[i] = heads[i - 1] + 1
else:
heads[i] = 0
tails[-1] = 0
for i in range(n - 2, -1, -1):
if a[i + 1] == "1":
tails[i] = tails[i + 1] + 1
else:
tails[i] = 0
maxones = 0
i = 0
while i + k <= n:
maxones = max(maxones, heads[i] + k + tails[i + k - 1])
i += 1
print(maxones)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
On each of the following $N$ days (numbered $1$ through $N$), Chef is planning to cook either pizza or broccoli. He wrote down a string $A$ with length $N$, where for each valid $i$, if the character $A_i$ is '1', then he will cook pizza on the $i$-th day, while if $A_i$ is '0', he will cook broccoli on this day.
Chefu, his son, loves pizza but hates broccoli ― just like most kids. He wants to select a substring of $A$ with length $K$ and change each character '0' in this substring to '1'. Afterwards, let's define pizza time as the maximum number of consecutive days where Chef will cook pizza. Find the maximum pizza time Chefu can achieve.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $K$.
- The second line contains a string $A$ with length $N$.
-----Output-----
For each test case, print a single line containing one integer ― the maximum pizza time.
-----Constraints-----
- $1 \le T \le 1,000$
- $1 \le K \le N \le 10^5$
- $A$ contains only characters '0' and '1'
- the sum of $N$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (50 points):
- $N \le 10^3$
- the sum of $N$ over all test cases does not exceed $10^4$
Subtask #2 (50 points): original constraints
-----Example Input-----
2
13 2
0101110000101
6 3
100001
-----Example Output-----
5
4
-----Explanation-----
Example case 1: Chefu can choose the substring $A[2, 3]$ = "10", and change the third character of $A$ to '1'. Then, the pizza time is $5$ days: from day $2$ to day $6$.
Example case 2: Chefu can choose the substring $A[2, 4]$ = "000". Then, the pizza time is $4$ days: from day $1$ to day $4$.
|
for _ in range(int(input())):
N, K = list(map(int, input().split()))
S = input()
left = [0] * N
right = [0] * N
left[0] = 1 * (S[0] == "1")
right[-1] = 1 * (S[-1] == "1")
for i in range(1, N):
if S[i] == "1":
left[i] = left[i - 1] + 1
else:
left[i] = 0
for i in range(N - 2, 0, -1):
if S[i] == "1":
right[i] = right[i + 1] + 1
else:
right[i] = 0
ans = K
for i in range(N - 1, K - 2, -1):
x = 0
y = 0
if left[i - (K - 1)] == 0:
if i - K >= 0 and left[i - K]:
x = left[i - K]
else:
x = left[i - (K - 1)] - 1
if right[i] == 0:
if i + 1 < N and right[i + 1]:
y = right[i + 1]
else:
y = right[i] - 1
ans = max(ans, K + x + y)
print(ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER BIN_OP NUMBER VAR NUMBER STRING ASSIGN VAR NUMBER BIN_OP NUMBER VAR NUMBER STRING FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
On each of the following $N$ days (numbered $1$ through $N$), Chef is planning to cook either pizza or broccoli. He wrote down a string $A$ with length $N$, where for each valid $i$, if the character $A_i$ is '1', then he will cook pizza on the $i$-th day, while if $A_i$ is '0', he will cook broccoli on this day.
Chefu, his son, loves pizza but hates broccoli ― just like most kids. He wants to select a substring of $A$ with length $K$ and change each character '0' in this substring to '1'. Afterwards, let's define pizza time as the maximum number of consecutive days where Chef will cook pizza. Find the maximum pizza time Chefu can achieve.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $K$.
- The second line contains a string $A$ with length $N$.
-----Output-----
For each test case, print a single line containing one integer ― the maximum pizza time.
-----Constraints-----
- $1 \le T \le 1,000$
- $1 \le K \le N \le 10^5$
- $A$ contains only characters '0' and '1'
- the sum of $N$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (50 points):
- $N \le 10^3$
- the sum of $N$ over all test cases does not exceed $10^4$
Subtask #2 (50 points): original constraints
-----Example Input-----
2
13 2
0101110000101
6 3
100001
-----Example Output-----
5
4
-----Explanation-----
Example case 1: Chefu can choose the substring $A[2, 3]$ = "10", and change the third character of $A$ to '1'. Then, the pizza time is $5$ days: from day $2$ to day $6$.
Example case 2: Chefu can choose the substring $A[2, 4]$ = "000". Then, the pizza time is $4$ days: from day $1$ to day $4$.
|
for _ in range(int(input())):
n, k = map(int, input().split())
l = [*map(int, input())]
count = [0] * (n + 1)
for i in range(n - 1, -1, -1):
if l[i] == 1:
count[i] = count[i + 1] + 1
x, y = 0, 0
for i in range(n):
if l[i] == 1:
x += 1
else:
try:
y = max(y, x + k + count[i + k])
except:
y = max(y, x + min(k, n - i))
x = 0
y = max(y, x)
print(y)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
On each of the following $N$ days (numbered $1$ through $N$), Chef is planning to cook either pizza or broccoli. He wrote down a string $A$ with length $N$, where for each valid $i$, if the character $A_i$ is '1', then he will cook pizza on the $i$-th day, while if $A_i$ is '0', he will cook broccoli on this day.
Chefu, his son, loves pizza but hates broccoli ― just like most kids. He wants to select a substring of $A$ with length $K$ and change each character '0' in this substring to '1'. Afterwards, let's define pizza time as the maximum number of consecutive days where Chef will cook pizza. Find the maximum pizza time Chefu can achieve.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $K$.
- The second line contains a string $A$ with length $N$.
-----Output-----
For each test case, print a single line containing one integer ― the maximum pizza time.
-----Constraints-----
- $1 \le T \le 1,000$
- $1 \le K \le N \le 10^5$
- $A$ contains only characters '0' and '1'
- the sum of $N$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (50 points):
- $N \le 10^3$
- the sum of $N$ over all test cases does not exceed $10^4$
Subtask #2 (50 points): original constraints
-----Example Input-----
2
13 2
0101110000101
6 3
100001
-----Example Output-----
5
4
-----Explanation-----
Example case 1: Chefu can choose the substring $A[2, 3]$ = "10", and change the third character of $A$ to '1'. Then, the pizza time is $5$ days: from day $2$ to day $6$.
Example case 2: Chefu can choose the substring $A[2, 4]$ = "000". Then, the pizza time is $4$ days: from day $1$ to day $4$.
|
def create(lst):
A = []
B = 0
for num in lst:
if num == 0:
B = 0
else:
B += 1
A.append(B)
return A
T = int(input())
for _ in range(T):
N, K = [int(s) for s in input().split()]
days = [0] + [int(s) for s in input()] + [0]
left = create(days)
right = create(days[::-1])[::-1]
A = 0
for i in range(K + 1, N + 2):
A = max(A, left[i - K - 1] + K + right[i])
print(A)
|
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
On each of the following $N$ days (numbered $1$ through $N$), Chef is planning to cook either pizza or broccoli. He wrote down a string $A$ with length $N$, where for each valid $i$, if the character $A_i$ is '1', then he will cook pizza on the $i$-th day, while if $A_i$ is '0', he will cook broccoli on this day.
Chefu, his son, loves pizza but hates broccoli ― just like most kids. He wants to select a substring of $A$ with length $K$ and change each character '0' in this substring to '1'. Afterwards, let's define pizza time as the maximum number of consecutive days where Chef will cook pizza. Find the maximum pizza time Chefu can achieve.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $K$.
- The second line contains a string $A$ with length $N$.
-----Output-----
For each test case, print a single line containing one integer ― the maximum pizza time.
-----Constraints-----
- $1 \le T \le 1,000$
- $1 \le K \le N \le 10^5$
- $A$ contains only characters '0' and '1'
- the sum of $N$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (50 points):
- $N \le 10^3$
- the sum of $N$ over all test cases does not exceed $10^4$
Subtask #2 (50 points): original constraints
-----Example Input-----
2
13 2
0101110000101
6 3
100001
-----Example Output-----
5
4
-----Explanation-----
Example case 1: Chefu can choose the substring $A[2, 3]$ = "10", and change the third character of $A$ to '1'. Then, the pizza time is $5$ days: from day $2$ to day $6$.
Example case 2: Chefu can choose the substring $A[2, 4]$ = "000". Then, the pizza time is $4$ days: from day $1$ to day $4$.
|
def main():
t = int(input())
for _ in range(t):
n, k = map(int, input().split())
a = list(map(int, list(input())))
i = 0
nootn = 0
nool = [0] * (n + 1)
max = k
while i <= n - k:
if i > 0 and a[i - 1] == 1:
nootn += 1
else:
nootn = 0
if i == n - k:
if max < nootn + k:
max = nootn + k
break
if nool[i + k - 1] == 0:
j = i + k
while j < n and a[j] == 1:
j += 1
nool[i + k] = j - i - k
else:
nool[i + k] = nool[i + k - 1] - 1
if max < nool[i + k] + k + nootn:
max = nool[i + k] + k + nootn
i += 1
print(max)
main()
|
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR BIN_OP VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR VAR IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR WHILE VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
On each of the following $N$ days (numbered $1$ through $N$), Chef is planning to cook either pizza or broccoli. He wrote down a string $A$ with length $N$, where for each valid $i$, if the character $A_i$ is '1', then he will cook pizza on the $i$-th day, while if $A_i$ is '0', he will cook broccoli on this day.
Chefu, his son, loves pizza but hates broccoli ― just like most kids. He wants to select a substring of $A$ with length $K$ and change each character '0' in this substring to '1'. Afterwards, let's define pizza time as the maximum number of consecutive days where Chef will cook pizza. Find the maximum pizza time Chefu can achieve.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $K$.
- The second line contains a string $A$ with length $N$.
-----Output-----
For each test case, print a single line containing one integer ― the maximum pizza time.
-----Constraints-----
- $1 \le T \le 1,000$
- $1 \le K \le N \le 10^5$
- $A$ contains only characters '0' and '1'
- the sum of $N$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (50 points):
- $N \le 10^3$
- the sum of $N$ over all test cases does not exceed $10^4$
Subtask #2 (50 points): original constraints
-----Example Input-----
2
13 2
0101110000101
6 3
100001
-----Example Output-----
5
4
-----Explanation-----
Example case 1: Chefu can choose the substring $A[2, 3]$ = "10", and change the third character of $A$ to '1'. Then, the pizza time is $5$ days: from day $2$ to day $6$.
Example case 2: Chefu can choose the substring $A[2, 4]$ = "000". Then, the pizza time is $4$ days: from day $1$ to day $4$.
|
def fun(s):
p = 0
m = 0
for i in range(n - 1):
if s[i] == "1" and s[i + 1] == "1":
p += 1
else:
p = 0
m = max(m, p)
return m + 1
for __ in range(int(input())):
n, k = map(int, input().split())
s = input()
r = 0
for i in range(n - k + 1):
s1 = "1" * k
h = fun(s[:i] + s1 + s[i + k :])
r = max(r, h)
print(r)
|
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP STRING VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
On each of the following $N$ days (numbered $1$ through $N$), Chef is planning to cook either pizza or broccoli. He wrote down a string $A$ with length $N$, where for each valid $i$, if the character $A_i$ is '1', then he will cook pizza on the $i$-th day, while if $A_i$ is '0', he will cook broccoli on this day.
Chefu, his son, loves pizza but hates broccoli ― just like most kids. He wants to select a substring of $A$ with length $K$ and change each character '0' in this substring to '1'. Afterwards, let's define pizza time as the maximum number of consecutive days where Chef will cook pizza. Find the maximum pizza time Chefu can achieve.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $K$.
- The second line contains a string $A$ with length $N$.
-----Output-----
For each test case, print a single line containing one integer ― the maximum pizza time.
-----Constraints-----
- $1 \le T \le 1,000$
- $1 \le K \le N \le 10^5$
- $A$ contains only characters '0' and '1'
- the sum of $N$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (50 points):
- $N \le 10^3$
- the sum of $N$ over all test cases does not exceed $10^4$
Subtask #2 (50 points): original constraints
-----Example Input-----
2
13 2
0101110000101
6 3
100001
-----Example Output-----
5
4
-----Explanation-----
Example case 1: Chefu can choose the substring $A[2, 3]$ = "10", and change the third character of $A$ to '1'. Then, the pizza time is $5$ days: from day $2$ to day $6$.
Example case 2: Chefu can choose the substring $A[2, 4]$ = "000". Then, the pizza time is $4$ days: from day $1$ to day $4$.
|
for _ in range(int(input())):
n, k = list(map(int, input().split()))
arr = list(map(int, list(input())))
right = [0] * (n + 1)
count = 0
for i in range(n - 1, -1, -1):
if arr[i] == 1:
count += 1
else:
count = 0
right[i] = count
count = 0
ans = 0
for i in range(n):
length = 0
length = count + (min(i + k, n) - i) + right[min(i + k, n)]
ans = max(ans, length)
if arr[i] == 1:
count += 1
else:
count = 0
print(ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
|
On each of the following $N$ days (numbered $1$ through $N$), Chef is planning to cook either pizza or broccoli. He wrote down a string $A$ with length $N$, where for each valid $i$, if the character $A_i$ is '1', then he will cook pizza on the $i$-th day, while if $A_i$ is '0', he will cook broccoli on this day.
Chefu, his son, loves pizza but hates broccoli ― just like most kids. He wants to select a substring of $A$ with length $K$ and change each character '0' in this substring to '1'. Afterwards, let's define pizza time as the maximum number of consecutive days where Chef will cook pizza. Find the maximum pizza time Chefu can achieve.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $K$.
- The second line contains a string $A$ with length $N$.
-----Output-----
For each test case, print a single line containing one integer ― the maximum pizza time.
-----Constraints-----
- $1 \le T \le 1,000$
- $1 \le K \le N \le 10^5$
- $A$ contains only characters '0' and '1'
- the sum of $N$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (50 points):
- $N \le 10^3$
- the sum of $N$ over all test cases does not exceed $10^4$
Subtask #2 (50 points): original constraints
-----Example Input-----
2
13 2
0101110000101
6 3
100001
-----Example Output-----
5
4
-----Explanation-----
Example case 1: Chefu can choose the substring $A[2, 3]$ = "10", and change the third character of $A$ to '1'. Then, the pizza time is $5$ days: from day $2$ to day $6$.
Example case 2: Chefu can choose the substring $A[2, 4]$ = "000". Then, the pizza time is $4$ days: from day $1$ to day $4$.
|
for _ in range(int(input())):
n, k = map(int, input().split())
arr = input()
pre = [0] * n
suf = [0] * n
i, j = 0, 1
flag = arr[0] != "0"
while i < n and j < n:
if not flag and arr[j] == "1":
i = j
flag = True
elif flag and arr[j] == "0":
ix = 1
while i < j:
suf[i] = j - i
pre[i] = ix
ix += 1
i += 1
flag = False
j += 1
diff = j - i
ix = 1
while i < j and flag:
pre[i] = ix
suf[i] = j - i
ix += 1
i += 1
ml = k + suf[k] if k < n else k
for i in range(1, n - k):
length = k + pre[i - 1] + suf[i + k]
ml = max(length, ml)
if n - k - 1 >= 0:
ml = max(pre[n - k - 1] + k, ml)
print(ml)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER STRING WHILE VAR VAR VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR STRING ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
|
On each of the following $N$ days (numbered $1$ through $N$), Chef is planning to cook either pizza or broccoli. He wrote down a string $A$ with length $N$, where for each valid $i$, if the character $A_i$ is '1', then he will cook pizza on the $i$-th day, while if $A_i$ is '0', he will cook broccoli on this day.
Chefu, his son, loves pizza but hates broccoli ― just like most kids. He wants to select a substring of $A$ with length $K$ and change each character '0' in this substring to '1'. Afterwards, let's define pizza time as the maximum number of consecutive days where Chef will cook pizza. Find the maximum pizza time Chefu can achieve.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $K$.
- The second line contains a string $A$ with length $N$.
-----Output-----
For each test case, print a single line containing one integer ― the maximum pizza time.
-----Constraints-----
- $1 \le T \le 1,000$
- $1 \le K \le N \le 10^5$
- $A$ contains only characters '0' and '1'
- the sum of $N$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (50 points):
- $N \le 10^3$
- the sum of $N$ over all test cases does not exceed $10^4$
Subtask #2 (50 points): original constraints
-----Example Input-----
2
13 2
0101110000101
6 3
100001
-----Example Output-----
5
4
-----Explanation-----
Example case 1: Chefu can choose the substring $A[2, 3]$ = "10", and change the third character of $A$ to '1'. Then, the pizza time is $5$ days: from day $2$ to day $6$.
Example case 2: Chefu can choose the substring $A[2, 4]$ = "000". Then, the pizza time is $4$ days: from day $1$ to day $4$.
|
def solve():
N, K = map(int, input().split())
S = input()
eleje = [(0) for i in range(N)]
vege = [(0) for i in range(N)]
if S[0] == "1":
vege[0] = 1
for i in range(1, N):
if S[i] == "1":
vege[i] = vege[i - 1] + 1
else:
vege[i] = 0
if S[N - 1] == "1":
eleje[N - 1] = 1
for i in range(N - 2, -1, -1):
if S[i] == "1":
eleje[i] = eleje[i + 1] + 1
else:
eleje[i] = 0
ans = -1
for i in range(N - K + 1):
j = i + K - 1
a = K
if i - 1 >= 0:
a += vege[i - 1]
if j + 1 < N:
a += eleje[j + 1]
ans = max(ans, a)
print(ans)
def main():
T = int(input())
while T > 0:
solve()
T = T - 1
main()
|
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR NUMBER STRING ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR
|
On each of the following $N$ days (numbered $1$ through $N$), Chef is planning to cook either pizza or broccoli. He wrote down a string $A$ with length $N$, where for each valid $i$, if the character $A_i$ is '1', then he will cook pizza on the $i$-th day, while if $A_i$ is '0', he will cook broccoli on this day.
Chefu, his son, loves pizza but hates broccoli ― just like most kids. He wants to select a substring of $A$ with length $K$ and change each character '0' in this substring to '1'. Afterwards, let's define pizza time as the maximum number of consecutive days where Chef will cook pizza. Find the maximum pizza time Chefu can achieve.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $K$.
- The second line contains a string $A$ with length $N$.
-----Output-----
For each test case, print a single line containing one integer ― the maximum pizza time.
-----Constraints-----
- $1 \le T \le 1,000$
- $1 \le K \le N \le 10^5$
- $A$ contains only characters '0' and '1'
- the sum of $N$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (50 points):
- $N \le 10^3$
- the sum of $N$ over all test cases does not exceed $10^4$
Subtask #2 (50 points): original constraints
-----Example Input-----
2
13 2
0101110000101
6 3
100001
-----Example Output-----
5
4
-----Explanation-----
Example case 1: Chefu can choose the substring $A[2, 3]$ = "10", and change the third character of $A$ to '1'. Then, the pizza time is $5$ days: from day $2$ to day $6$.
Example case 2: Chefu can choose the substring $A[2, 4]$ = "000". Then, the pizza time is $4$ days: from day $1$ to day $4$.
|
t = int(input())
for _ in range(t):
n, k = map(int, input().split())
st = list(input())
ans = 0
i = 0
while i <= n - k:
count = k
for j in range(i + k, n):
if st[j] == "1":
count += 1
else:
break
for j in range(i - 1, -1, -1):
if st[j] == "1":
count += 1
else:
break
ans = max(ans, count)
i += 1
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR VAR STRING VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
On each of the following $N$ days (numbered $1$ through $N$), Chef is planning to cook either pizza or broccoli. He wrote down a string $A$ with length $N$, where for each valid $i$, if the character $A_i$ is '1', then he will cook pizza on the $i$-th day, while if $A_i$ is '0', he will cook broccoli on this day.
Chefu, his son, loves pizza but hates broccoli ― just like most kids. He wants to select a substring of $A$ with length $K$ and change each character '0' in this substring to '1'. Afterwards, let's define pizza time as the maximum number of consecutive days where Chef will cook pizza. Find the maximum pizza time Chefu can achieve.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $K$.
- The second line contains a string $A$ with length $N$.
-----Output-----
For each test case, print a single line containing one integer ― the maximum pizza time.
-----Constraints-----
- $1 \le T \le 1,000$
- $1 \le K \le N \le 10^5$
- $A$ contains only characters '0' and '1'
- the sum of $N$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (50 points):
- $N \le 10^3$
- the sum of $N$ over all test cases does not exceed $10^4$
Subtask #2 (50 points): original constraints
-----Example Input-----
2
13 2
0101110000101
6 3
100001
-----Example Output-----
5
4
-----Explanation-----
Example case 1: Chefu can choose the substring $A[2, 3]$ = "10", and change the third character of $A$ to '1'. Then, the pizza time is $5$ days: from day $2$ to day $6$.
Example case 2: Chefu can choose the substring $A[2, 4]$ = "000". Then, the pizza time is $4$ days: from day $1$ to day $4$.
|
t = int(input())
for i in range(t):
st = input().split()
N = int(st[0])
K = int(st[1])
s = input().strip()
F = [0] * (N + 2)
R = [0] * (N + 2)
p = 0
c = 0
for k in range(1, N + 1):
if s[p] == "1":
c += 1
else:
c = 0
F[k] = c
p += 1
p = N
cnt = 0
p = N
c = 0
while p > 0:
if s[p - 1] == "1":
c += 1
else:
c = 0
R[p] = c
p -= 1
mx = K
for i in range(1, N + 2 - K):
n = F[i - 1] + K + R[i + K]
if n > mx:
mx = n
print(mx)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL 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 FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
On each of the following $N$ days (numbered $1$ through $N$), Chef is planning to cook either pizza or broccoli. He wrote down a string $A$ with length $N$, where for each valid $i$, if the character $A_i$ is '1', then he will cook pizza on the $i$-th day, while if $A_i$ is '0', he will cook broccoli on this day.
Chefu, his son, loves pizza but hates broccoli ― just like most kids. He wants to select a substring of $A$ with length $K$ and change each character '0' in this substring to '1'. Afterwards, let's define pizza time as the maximum number of consecutive days where Chef will cook pizza. Find the maximum pizza time Chefu can achieve.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $K$.
- The second line contains a string $A$ with length $N$.
-----Output-----
For each test case, print a single line containing one integer ― the maximum pizza time.
-----Constraints-----
- $1 \le T \le 1,000$
- $1 \le K \le N \le 10^5$
- $A$ contains only characters '0' and '1'
- the sum of $N$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (50 points):
- $N \le 10^3$
- the sum of $N$ over all test cases does not exceed $10^4$
Subtask #2 (50 points): original constraints
-----Example Input-----
2
13 2
0101110000101
6 3
100001
-----Example Output-----
5
4
-----Explanation-----
Example case 1: Chefu can choose the substring $A[2, 3]$ = "10", and change the third character of $A$ to '1'. Then, the pizza time is $5$ days: from day $2$ to day $6$.
Example case 2: Chefu can choose the substring $A[2, 4]$ = "000". Then, the pizza time is $4$ days: from day $1$ to day $4$.
|
for t in range(int(input())):
n, k = map(int, input().split())
s = input().strip()
left = [(0) for i in range(n)]
right = [(0) for i in range(n)]
if s[0] == "1":
left[0] = 1
for i in range(1, n):
if s[i] == "1":
left[i] = left[i - 1] + 1
if s[-1] == "1":
right[-1] = 1
for i in range(n - 2, -1, -1):
if s[i] == "1":
right[i] = right[i + 1] + 1
ans = k
for i in range(n):
temp = left[i]
if i + k + 1 < n:
temp += k + right[i + k + 1]
else:
temp += n - 1 - i
ans = max(ans, temp)
for i in range(n):
temp = right[i]
if i - k > 0:
temp += k + left[i - k - 1]
else:
temp += i
ans = max(ans, temp)
print(ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR NUMBER STRING ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER STRING ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
On each of the following $N$ days (numbered $1$ through $N$), Chef is planning to cook either pizza or broccoli. He wrote down a string $A$ with length $N$, where for each valid $i$, if the character $A_i$ is '1', then he will cook pizza on the $i$-th day, while if $A_i$ is '0', he will cook broccoli on this day.
Chefu, his son, loves pizza but hates broccoli ― just like most kids. He wants to select a substring of $A$ with length $K$ and change each character '0' in this substring to '1'. Afterwards, let's define pizza time as the maximum number of consecutive days where Chef will cook pizza. Find the maximum pizza time Chefu can achieve.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $K$.
- The second line contains a string $A$ with length $N$.
-----Output-----
For each test case, print a single line containing one integer ― the maximum pizza time.
-----Constraints-----
- $1 \le T \le 1,000$
- $1 \le K \le N \le 10^5$
- $A$ contains only characters '0' and '1'
- the sum of $N$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (50 points):
- $N \le 10^3$
- the sum of $N$ over all test cases does not exceed $10^4$
Subtask #2 (50 points): original constraints
-----Example Input-----
2
13 2
0101110000101
6 3
100001
-----Example Output-----
5
4
-----Explanation-----
Example case 1: Chefu can choose the substring $A[2, 3]$ = "10", and change the third character of $A$ to '1'. Then, the pizza time is $5$ days: from day $2$ to day $6$.
Example case 2: Chefu can choose the substring $A[2, 4]$ = "000". Then, the pizza time is $4$ days: from day $1$ to day $4$.
|
T = int(input())
while T > 0:
N, K = [int(x) for x in input().split()]
s = list(input())
l = [0] * N
r = [0] * N
temp = 0
for i in range(N):
if s[i] == "1":
temp += 1
l[i] = temp
else:
temp = 0
temp = 0
for i in range(N - 1, -1, -1):
if s[i] == "1":
temp += 1
r[i] = temp
else:
temp = 0
ans = K
for i in range(N - K + 1):
t = K
if i + K < N:
t += r[i + K]
if i > 0:
t += l[i - 1]
ans = max(t, ans)
print(ans)
T -= 1
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
|
On each of the following $N$ days (numbered $1$ through $N$), Chef is planning to cook either pizza or broccoli. He wrote down a string $A$ with length $N$, where for each valid $i$, if the character $A_i$ is '1', then he will cook pizza on the $i$-th day, while if $A_i$ is '0', he will cook broccoli on this day.
Chefu, his son, loves pizza but hates broccoli ― just like most kids. He wants to select a substring of $A$ with length $K$ and change each character '0' in this substring to '1'. Afterwards, let's define pizza time as the maximum number of consecutive days where Chef will cook pizza. Find the maximum pizza time Chefu can achieve.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $K$.
- The second line contains a string $A$ with length $N$.
-----Output-----
For each test case, print a single line containing one integer ― the maximum pizza time.
-----Constraints-----
- $1 \le T \le 1,000$
- $1 \le K \le N \le 10^5$
- $A$ contains only characters '0' and '1'
- the sum of $N$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (50 points):
- $N \le 10^3$
- the sum of $N$ over all test cases does not exceed $10^4$
Subtask #2 (50 points): original constraints
-----Example Input-----
2
13 2
0101110000101
6 3
100001
-----Example Output-----
5
4
-----Explanation-----
Example case 1: Chefu can choose the substring $A[2, 3]$ = "10", and change the third character of $A$ to '1'. Then, the pizza time is $5$ days: from day $2$ to day $6$.
Example case 2: Chefu can choose the substring $A[2, 4]$ = "000". Then, the pizza time is $4$ days: from day $1$ to day $4$.
|
T = int(input())
for _ in range(0, T):
N, K = map(int, input().split())
S = input()
ans = -1
for i in range(0, N - K + 1):
count = K
j = i - 1
while -1 < j < N:
if S[j] == "1":
count += 1
j -= 1
else:
break
k = i + K
while -1 < k < N:
if S[k] == "1":
count += 1
k += 1
else:
break
ans = max(ans, count)
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE NUMBER VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR WHILE NUMBER VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
|
On each of the following $N$ days (numbered $1$ through $N$), Chef is planning to cook either pizza or broccoli. He wrote down a string $A$ with length $N$, where for each valid $i$, if the character $A_i$ is '1', then he will cook pizza on the $i$-th day, while if $A_i$ is '0', he will cook broccoli on this day.
Chefu, his son, loves pizza but hates broccoli ― just like most kids. He wants to select a substring of $A$ with length $K$ and change each character '0' in this substring to '1'. Afterwards, let's define pizza time as the maximum number of consecutive days where Chef will cook pizza. Find the maximum pizza time Chefu can achieve.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $K$.
- The second line contains a string $A$ with length $N$.
-----Output-----
For each test case, print a single line containing one integer ― the maximum pizza time.
-----Constraints-----
- $1 \le T \le 1,000$
- $1 \le K \le N \le 10^5$
- $A$ contains only characters '0' and '1'
- the sum of $N$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (50 points):
- $N \le 10^3$
- the sum of $N$ over all test cases does not exceed $10^4$
Subtask #2 (50 points): original constraints
-----Example Input-----
2
13 2
0101110000101
6 3
100001
-----Example Output-----
5
4
-----Explanation-----
Example case 1: Chefu can choose the substring $A[2, 3]$ = "10", and change the third character of $A$ to '1'. Then, the pizza time is $5$ days: from day $2$ to day $6$.
Example case 2: Chefu can choose the substring $A[2, 4]$ = "000". Then, the pizza time is $4$ days: from day $1$ to day $4$.
|
num = int(input())
t = 0
while t < num:
n, k = map(int, input().split())
s = str(input())
count = 0
pos = []
for i in range(n):
if s[i] == "0":
pos.append(i)
for i in range(len(pos)):
ind = pos[i]
val = 0
if ind - k + 1 < 0:
val += ind + 1
else:
val += k
bn = ind - k
while bn >= 0:
if s[bn] == "1":
val += 1
else:
break
bn -= 1
if i != len(pos) - 1:
val += pos[i + 1] - pos[i] - 1
else:
ui = 0
gh = pos[len(pos) - 1] + 1
while gh < n:
if s[gh] == "1":
ui += 1
gh += 1
val += ui
if val > count:
count = val
if len(pos) == 0:
print(n)
else:
print(count)
t += 1
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR WHILE VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER WHILE VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
|
On each of the following $N$ days (numbered $1$ through $N$), Chef is planning to cook either pizza or broccoli. He wrote down a string $A$ with length $N$, where for each valid $i$, if the character $A_i$ is '1', then he will cook pizza on the $i$-th day, while if $A_i$ is '0', he will cook broccoli on this day.
Chefu, his son, loves pizza but hates broccoli ― just like most kids. He wants to select a substring of $A$ with length $K$ and change each character '0' in this substring to '1'. Afterwards, let's define pizza time as the maximum number of consecutive days where Chef will cook pizza. Find the maximum pizza time Chefu can achieve.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $K$.
- The second line contains a string $A$ with length $N$.
-----Output-----
For each test case, print a single line containing one integer ― the maximum pizza time.
-----Constraints-----
- $1 \le T \le 1,000$
- $1 \le K \le N \le 10^5$
- $A$ contains only characters '0' and '1'
- the sum of $N$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (50 points):
- $N \le 10^3$
- the sum of $N$ over all test cases does not exceed $10^4$
Subtask #2 (50 points): original constraints
-----Example Input-----
2
13 2
0101110000101
6 3
100001
-----Example Output-----
5
4
-----Explanation-----
Example case 1: Chefu can choose the substring $A[2, 3]$ = "10", and change the third character of $A$ to '1'. Then, the pizza time is $5$ days: from day $2$ to day $6$.
Example case 2: Chefu can choose the substring $A[2, 4]$ = "000". Then, the pizza time is $4$ days: from day $1$ to day $4$.
|
for _ in range(int(input())):
n, k = map(int, input().split())
s = input()
m = k
head = [0] * n
tail = [0] * n
head[0] = 0
tail[-1] = 0
for i in range(1, n):
if s[i - 1] == "1":
head[i] = 1 + head[i - 1]
for i in range(n - 2, -1, -1):
if s[i + 1] == "1":
tail[i] = 1 + tail[i + 1]
i = 0
while i + k <= n:
c = head[i] + k + tail[i + k - 1]
m = max(m, c)
i += 1
print(m)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
On each of the following $N$ days (numbered $1$ through $N$), Chef is planning to cook either pizza or broccoli. He wrote down a string $A$ with length $N$, where for each valid $i$, if the character $A_i$ is '1', then he will cook pizza on the $i$-th day, while if $A_i$ is '0', he will cook broccoli on this day.
Chefu, his son, loves pizza but hates broccoli ― just like most kids. He wants to select a substring of $A$ with length $K$ and change each character '0' in this substring to '1'. Afterwards, let's define pizza time as the maximum number of consecutive days where Chef will cook pizza. Find the maximum pizza time Chefu can achieve.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $K$.
- The second line contains a string $A$ with length $N$.
-----Output-----
For each test case, print a single line containing one integer ― the maximum pizza time.
-----Constraints-----
- $1 \le T \le 1,000$
- $1 \le K \le N \le 10^5$
- $A$ contains only characters '0' and '1'
- the sum of $N$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (50 points):
- $N \le 10^3$
- the sum of $N$ over all test cases does not exceed $10^4$
Subtask #2 (50 points): original constraints
-----Example Input-----
2
13 2
0101110000101
6 3
100001
-----Example Output-----
5
4
-----Explanation-----
Example case 1: Chefu can choose the substring $A[2, 3]$ = "10", and change the third character of $A$ to '1'. Then, the pizza time is $5$ days: from day $2$ to day $6$.
Example case 2: Chefu can choose the substring $A[2, 4]$ = "000". Then, the pizza time is $4$ days: from day $1$ to day $4$.
|
for t in range(int(input())):
n, k = map(int, input().split())
arr = list(input())
for i in range(len(arr)):
arr[i] = int(arr[i])
head = [(0) for i in range(n)]
for i in range(1, n):
if arr[i - 1] == 0:
head[i] = 0
elif arr[i - 1] == 1:
head[i] = head[i - 1] + 1
tail = [(0) for i in range(n)]
for i in range(n - 2, -1, -1):
if arr[i + 1] == 0:
tail[i] = 0
elif arr[i + 1] == 1:
tail[i] = tail[i + 1] + 1
ans = 0
j = 0
while j + k <= n:
ans = max(ans, k + head[j] + tail[j + k - 1])
j += 1
print(ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
On each of the following $N$ days (numbered $1$ through $N$), Chef is planning to cook either pizza or broccoli. He wrote down a string $A$ with length $N$, where for each valid $i$, if the character $A_i$ is '1', then he will cook pizza on the $i$-th day, while if $A_i$ is '0', he will cook broccoli on this day.
Chefu, his son, loves pizza but hates broccoli ― just like most kids. He wants to select a substring of $A$ with length $K$ and change each character '0' in this substring to '1'. Afterwards, let's define pizza time as the maximum number of consecutive days where Chef will cook pizza. Find the maximum pizza time Chefu can achieve.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $K$.
- The second line contains a string $A$ with length $N$.
-----Output-----
For each test case, print a single line containing one integer ― the maximum pizza time.
-----Constraints-----
- $1 \le T \le 1,000$
- $1 \le K \le N \le 10^5$
- $A$ contains only characters '0' and '1'
- the sum of $N$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (50 points):
- $N \le 10^3$
- the sum of $N$ over all test cases does not exceed $10^4$
Subtask #2 (50 points): original constraints
-----Example Input-----
2
13 2
0101110000101
6 3
100001
-----Example Output-----
5
4
-----Explanation-----
Example case 1: Chefu can choose the substring $A[2, 3]$ = "10", and change the third character of $A$ to '1'. Then, the pizza time is $5$ days: from day $2$ to day $6$.
Example case 2: Chefu can choose the substring $A[2, 4]$ = "000". Then, the pizza time is $4$ days: from day $1$ to day $4$.
|
N_cases = int(input())
for case in range(N_cases):
N, K = [int(n) for n in input().split(" ")]
A = input()
patches = N - K + 1
pizza_time = 0
for i in range(patches):
cprev = 0
cpost = 0
c = 1
while True and i - c >= 0:
if A[i - c] == "1":
cprev = cprev + 1
else:
break
c = c + 1
c = 0
while True and i + c <= N - K - 1:
if A[i + K + c] == "1":
cpost = cpost + 1
else:
break
c = c + 1
pizza_time_new = K + cpost + cprev
if pizza_time_new > pizza_time:
pizza_time = pizza_time_new
print(pizza_time)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
On each of the following $N$ days (numbered $1$ through $N$), Chef is planning to cook either pizza or broccoli. He wrote down a string $A$ with length $N$, where for each valid $i$, if the character $A_i$ is '1', then he will cook pizza on the $i$-th day, while if $A_i$ is '0', he will cook broccoli on this day.
Chefu, his son, loves pizza but hates broccoli ― just like most kids. He wants to select a substring of $A$ with length $K$ and change each character '0' in this substring to '1'. Afterwards, let's define pizza time as the maximum number of consecutive days where Chef will cook pizza. Find the maximum pizza time Chefu can achieve.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $K$.
- The second line contains a string $A$ with length $N$.
-----Output-----
For each test case, print a single line containing one integer ― the maximum pizza time.
-----Constraints-----
- $1 \le T \le 1,000$
- $1 \le K \le N \le 10^5$
- $A$ contains only characters '0' and '1'
- the sum of $N$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (50 points):
- $N \le 10^3$
- the sum of $N$ over all test cases does not exceed $10^4$
Subtask #2 (50 points): original constraints
-----Example Input-----
2
13 2
0101110000101
6 3
100001
-----Example Output-----
5
4
-----Explanation-----
Example case 1: Chefu can choose the substring $A[2, 3]$ = "10", and change the third character of $A$ to '1'. Then, the pizza time is $5$ days: from day $2$ to day $6$.
Example case 2: Chefu can choose the substring $A[2, 4]$ = "000". Then, the pizza time is $4$ days: from day $1$ to day $4$.
|
def solve(n, k, s):
l = [None] * (n + 1)
l[0] = 0
for i in range(n):
if int(s[i]):
l[i + 1] = l[i] + 1
else:
l[i + 1] = 0
r = [None] * (n + 1)
r[n] = 0
for i in reversed(range(n)):
if int(s[i]):
r[i] = r[i + 1] + 1
else:
r[i] = 0
ans = 0
for i in range(n - k + 1):
ans = max(ans, l[i] + k + r[i + k])
return ans
def main():
t = int(input())
for _ in range(t):
n, k = map(int, input().split())
s = input()
print(solve(n, k, s))
main()
|
FUNC_DEF ASSIGN VAR BIN_OP LIST NONE BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NONE BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR
|
On each of the following $N$ days (numbered $1$ through $N$), Chef is planning to cook either pizza or broccoli. He wrote down a string $A$ with length $N$, where for each valid $i$, if the character $A_i$ is '1', then he will cook pizza on the $i$-th day, while if $A_i$ is '0', he will cook broccoli on this day.
Chefu, his son, loves pizza but hates broccoli ― just like most kids. He wants to select a substring of $A$ with length $K$ and change each character '0' in this substring to '1'. Afterwards, let's define pizza time as the maximum number of consecutive days where Chef will cook pizza. Find the maximum pizza time Chefu can achieve.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $K$.
- The second line contains a string $A$ with length $N$.
-----Output-----
For each test case, print a single line containing one integer ― the maximum pizza time.
-----Constraints-----
- $1 \le T \le 1,000$
- $1 \le K \le N \le 10^5$
- $A$ contains only characters '0' and '1'
- the sum of $N$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (50 points):
- $N \le 10^3$
- the sum of $N$ over all test cases does not exceed $10^4$
Subtask #2 (50 points): original constraints
-----Example Input-----
2
13 2
0101110000101
6 3
100001
-----Example Output-----
5
4
-----Explanation-----
Example case 1: Chefu can choose the substring $A[2, 3]$ = "10", and change the third character of $A$ to '1'. Then, the pizza time is $5$ days: from day $2$ to day $6$.
Example case 2: Chefu can choose the substring $A[2, 4]$ = "000". Then, the pizza time is $4$ days: from day $1$ to day $4$.
|
def solve(arr, n, k):
lbs = 0
for i in range(0, n - k + 1):
cnt = k
for j in range(i - 1, -1, -1):
if arr[j] == 1:
cnt += 1
else:
break
for j in range(i + k, n):
if arr[j] == 1:
cnt += 1
else:
break
lbs = max(lbs, cnt)
return lbs
t = int(input())
while t > 0:
t -= 1
n, k = map(int, input().split())
arr = list(map(int, list(input().strip())))
print(solve(arr, n, k))
|
FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
|
On each of the following $N$ days (numbered $1$ through $N$), Chef is planning to cook either pizza or broccoli. He wrote down a string $A$ with length $N$, where for each valid $i$, if the character $A_i$ is '1', then he will cook pizza on the $i$-th day, while if $A_i$ is '0', he will cook broccoli on this day.
Chefu, his son, loves pizza but hates broccoli ― just like most kids. He wants to select a substring of $A$ with length $K$ and change each character '0' in this substring to '1'. Afterwards, let's define pizza time as the maximum number of consecutive days where Chef will cook pizza. Find the maximum pizza time Chefu can achieve.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $K$.
- The second line contains a string $A$ with length $N$.
-----Output-----
For each test case, print a single line containing one integer ― the maximum pizza time.
-----Constraints-----
- $1 \le T \le 1,000$
- $1 \le K \le N \le 10^5$
- $A$ contains only characters '0' and '1'
- the sum of $N$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (50 points):
- $N \le 10^3$
- the sum of $N$ over all test cases does not exceed $10^4$
Subtask #2 (50 points): original constraints
-----Example Input-----
2
13 2
0101110000101
6 3
100001
-----Example Output-----
5
4
-----Explanation-----
Example case 1: Chefu can choose the substring $A[2, 3]$ = "10", and change the third character of $A$ to '1'. Then, the pizza time is $5$ days: from day $2$ to day $6$.
Example case 2: Chefu can choose the substring $A[2, 4]$ = "000". Then, the pizza time is $4$ days: from day $1$ to day $4$.
|
t = int(input())
for _ in range(t):
n, k = (int(x) for x in input().split())
as_ = [int(x) for x in input()]
assert len(as_) == n
left_streak = [0]
ct_left = 0
for ii in as_[: n - k]:
ct_left += 1
if ii == 0:
ct_left = 0
left_streak.append(ct_left)
right_streak = [0]
ct_right = 0
for ii in reversed(as_[k:]):
ct_right += 1
if ii == 0:
ct_right = 0
right_streak.append(ct_right)
print(max(x + y + k for x, y in zip(left_streak, reversed(right_streak))))
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR
|
On each of the following $N$ days (numbered $1$ through $N$), Chef is planning to cook either pizza or broccoli. He wrote down a string $A$ with length $N$, where for each valid $i$, if the character $A_i$ is '1', then he will cook pizza on the $i$-th day, while if $A_i$ is '0', he will cook broccoli on this day.
Chefu, his son, loves pizza but hates broccoli ― just like most kids. He wants to select a substring of $A$ with length $K$ and change each character '0' in this substring to '1'. Afterwards, let's define pizza time as the maximum number of consecutive days where Chef will cook pizza. Find the maximum pizza time Chefu can achieve.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $K$.
- The second line contains a string $A$ with length $N$.
-----Output-----
For each test case, print a single line containing one integer ― the maximum pizza time.
-----Constraints-----
- $1 \le T \le 1,000$
- $1 \le K \le N \le 10^5$
- $A$ contains only characters '0' and '1'
- the sum of $N$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (50 points):
- $N \le 10^3$
- the sum of $N$ over all test cases does not exceed $10^4$
Subtask #2 (50 points): original constraints
-----Example Input-----
2
13 2
0101110000101
6 3
100001
-----Example Output-----
5
4
-----Explanation-----
Example case 1: Chefu can choose the substring $A[2, 3]$ = "10", and change the third character of $A$ to '1'. Then, the pizza time is $5$ days: from day $2$ to day $6$.
Example case 2: Chefu can choose the substring $A[2, 4]$ = "000". Then, the pizza time is $4$ days: from day $1$ to day $4$.
|
t = int(input())
for i in range(0, t):
n, k = map(int, input().split())
arr = input()
x = 0
y = 0
left = []
right = [0] * n
for i in range(0, n):
left.append(x)
if arr[i] == "1":
x = x + 1
else:
x = 0
for i in range(n - 1, -1, -1):
right[i] = y
if arr[i] == "1":
y = y + 1
else:
y = 0
ans = 0
for i in range(0, n):
if i + k - 1 < n:
max_ = left[i] + k + right[i + k - 1]
else:
max_ = left[i] + n - i
if max_ > ans:
ans = max_
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
|
On each of the following $N$ days (numbered $1$ through $N$), Chef is planning to cook either pizza or broccoli. He wrote down a string $A$ with length $N$, where for each valid $i$, if the character $A_i$ is '1', then he will cook pizza on the $i$-th day, while if $A_i$ is '0', he will cook broccoli on this day.
Chefu, his son, loves pizza but hates broccoli ― just like most kids. He wants to select a substring of $A$ with length $K$ and change each character '0' in this substring to '1'. Afterwards, let's define pizza time as the maximum number of consecutive days where Chef will cook pizza. Find the maximum pizza time Chefu can achieve.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $K$.
- The second line contains a string $A$ with length $N$.
-----Output-----
For each test case, print a single line containing one integer ― the maximum pizza time.
-----Constraints-----
- $1 \le T \le 1,000$
- $1 \le K \le N \le 10^5$
- $A$ contains only characters '0' and '1'
- the sum of $N$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (50 points):
- $N \le 10^3$
- the sum of $N$ over all test cases does not exceed $10^4$
Subtask #2 (50 points): original constraints
-----Example Input-----
2
13 2
0101110000101
6 3
100001
-----Example Output-----
5
4
-----Explanation-----
Example case 1: Chefu can choose the substring $A[2, 3]$ = "10", and change the third character of $A$ to '1'. Then, the pizza time is $5$ days: from day $2$ to day $6$.
Example case 2: Chefu can choose the substring $A[2, 4]$ = "000". Then, the pizza time is $4$ days: from day $1$ to day $4$.
|
for _ in range(int(input())):
n, k = map(int, input().split())
a = list(input())
l = [0] * (n + 2)
r = [0] * (n + 2)
result = 0
for i in range(n):
if a[i] == "1":
l[i + 1] = l[i] + 1
for i in range(n - 1, -1, -1):
if a[i] == "1":
r[i + 1] = r[i + 2] + 1
i = 1
while i + k - 1 <= n:
result = max(result, l[i - 1] + k + r[i + k])
i += 1
print(result)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
On each of the following $N$ days (numbered $1$ through $N$), Chef is planning to cook either pizza or broccoli. He wrote down a string $A$ with length $N$, where for each valid $i$, if the character $A_i$ is '1', then he will cook pizza on the $i$-th day, while if $A_i$ is '0', he will cook broccoli on this day.
Chefu, his son, loves pizza but hates broccoli ― just like most kids. He wants to select a substring of $A$ with length $K$ and change each character '0' in this substring to '1'. Afterwards, let's define pizza time as the maximum number of consecutive days where Chef will cook pizza. Find the maximum pizza time Chefu can achieve.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $K$.
- The second line contains a string $A$ with length $N$.
-----Output-----
For each test case, print a single line containing one integer ― the maximum pizza time.
-----Constraints-----
- $1 \le T \le 1,000$
- $1 \le K \le N \le 10^5$
- $A$ contains only characters '0' and '1'
- the sum of $N$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (50 points):
- $N \le 10^3$
- the sum of $N$ over all test cases does not exceed $10^4$
Subtask #2 (50 points): original constraints
-----Example Input-----
2
13 2
0101110000101
6 3
100001
-----Example Output-----
5
4
-----Explanation-----
Example case 1: Chefu can choose the substring $A[2, 3]$ = "10", and change the third character of $A$ to '1'. Then, the pizza time is $5$ days: from day $2$ to day $6$.
Example case 2: Chefu can choose the substring $A[2, 4]$ = "000". Then, the pizza time is $4$ days: from day $1$ to day $4$.
|
t = int(input())
for i in range(t):
n, k = [int(x) for x in input().split()]
arr = [int(x) for x in input()]
index = k - 1
start = 0
res = 0
while index < n:
tmp_res = k
tmp_index = index
while tmp_index + 1 < n and arr[tmp_index + 1] == 1:
tmp_res += 1
tmp_index += 1
tmp_index = start
while tmp_index - 1 >= 0 and arr[tmp_index - 1] == 1:
tmp_res += 1
tmp_index -= 1
res = max(res, tmp_res)
index += 1
start += 1
print(res)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
On each of the following $N$ days (numbered $1$ through $N$), Chef is planning to cook either pizza or broccoli. He wrote down a string $A$ with length $N$, where for each valid $i$, if the character $A_i$ is '1', then he will cook pizza on the $i$-th day, while if $A_i$ is '0', he will cook broccoli on this day.
Chefu, his son, loves pizza but hates broccoli ― just like most kids. He wants to select a substring of $A$ with length $K$ and change each character '0' in this substring to '1'. Afterwards, let's define pizza time as the maximum number of consecutive days where Chef will cook pizza. Find the maximum pizza time Chefu can achieve.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $K$.
- The second line contains a string $A$ with length $N$.
-----Output-----
For each test case, print a single line containing one integer ― the maximum pizza time.
-----Constraints-----
- $1 \le T \le 1,000$
- $1 \le K \le N \le 10^5$
- $A$ contains only characters '0' and '1'
- the sum of $N$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (50 points):
- $N \le 10^3$
- the sum of $N$ over all test cases does not exceed $10^4$
Subtask #2 (50 points): original constraints
-----Example Input-----
2
13 2
0101110000101
6 3
100001
-----Example Output-----
5
4
-----Explanation-----
Example case 1: Chefu can choose the substring $A[2, 3]$ = "10", and change the third character of $A$ to '1'. Then, the pizza time is $5$ days: from day $2$ to day $6$.
Example case 2: Chefu can choose the substring $A[2, 4]$ = "000". Then, the pizza time is $4$ days: from day $1$ to day $4$.
|
for _ in range(int(input())):
n, k = (int(x) for x in input().split())
a = input()
hidari = [0] * n
migi = [0] * n
for i in range(n - k + 1):
loli = i
i -= 1
num = 0
while i >= 0:
if a[i] == "1":
num += 1
i -= 1
else:
break
hidari[loli] = num
for j in range(k - 1, n):
kaori = j
j += 1
num = 0
while j <= n - 1:
if a[j] == "1":
num += 1
j += 1
else:
break
migi[kaori] = num
if n == k:
print(n)
else:
max = 0
for i in range(n - k + 1):
if max < hidari[i] + k + migi[i + k - 1]:
max = hidari[i] + k + migi[i + k - 1]
print(max)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
On each of the following $N$ days (numbered $1$ through $N$), Chef is planning to cook either pizza or broccoli. He wrote down a string $A$ with length $N$, where for each valid $i$, if the character $A_i$ is '1', then he will cook pizza on the $i$-th day, while if $A_i$ is '0', he will cook broccoli on this day.
Chefu, his son, loves pizza but hates broccoli ― just like most kids. He wants to select a substring of $A$ with length $K$ and change each character '0' in this substring to '1'. Afterwards, let's define pizza time as the maximum number of consecutive days where Chef will cook pizza. Find the maximum pizza time Chefu can achieve.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $K$.
- The second line contains a string $A$ with length $N$.
-----Output-----
For each test case, print a single line containing one integer ― the maximum pizza time.
-----Constraints-----
- $1 \le T \le 1,000$
- $1 \le K \le N \le 10^5$
- $A$ contains only characters '0' and '1'
- the sum of $N$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (50 points):
- $N \le 10^3$
- the sum of $N$ over all test cases does not exceed $10^4$
Subtask #2 (50 points): original constraints
-----Example Input-----
2
13 2
0101110000101
6 3
100001
-----Example Output-----
5
4
-----Explanation-----
Example case 1: Chefu can choose the substring $A[2, 3]$ = "10", and change the third character of $A$ to '1'. Then, the pizza time is $5$ days: from day $2$ to day $6$.
Example case 2: Chefu can choose the substring $A[2, 4]$ = "000". Then, the pizza time is $4$ days: from day $1$ to day $4$.
|
def pizzatime(s, a, k):
cont1, m_time = 0, 0
for i in range(len(s)):
if s[i] == "1":
cont1 += 1
else:
try:
m_time = max(m_time, cont1 + k + a[i + k])
except:
m_time = max(m_time, cont1 + min(k, len(s) - i))
cont1 = 0
m_time = max(m_time, cont1)
return m_time
for T in range(int(input())):
n, k = map(int, input().split())
s = input()
all_1 = [0] * (n + 1)
check = True
for ind in range(len(s) - 1, -1, -1):
if s[ind] == "1":
all_1[ind] += all_1[ind + 1] + 1
print(pizzatime(s, all_1, k))
|
FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
|
On each of the following $N$ days (numbered $1$ through $N$), Chef is planning to cook either pizza or broccoli. He wrote down a string $A$ with length $N$, where for each valid $i$, if the character $A_i$ is '1', then he will cook pizza on the $i$-th day, while if $A_i$ is '0', he will cook broccoli on this day.
Chefu, his son, loves pizza but hates broccoli ― just like most kids. He wants to select a substring of $A$ with length $K$ and change each character '0' in this substring to '1'. Afterwards, let's define pizza time as the maximum number of consecutive days where Chef will cook pizza. Find the maximum pizza time Chefu can achieve.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $K$.
- The second line contains a string $A$ with length $N$.
-----Output-----
For each test case, print a single line containing one integer ― the maximum pizza time.
-----Constraints-----
- $1 \le T \le 1,000$
- $1 \le K \le N \le 10^5$
- $A$ contains only characters '0' and '1'
- the sum of $N$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (50 points):
- $N \le 10^3$
- the sum of $N$ over all test cases does not exceed $10^4$
Subtask #2 (50 points): original constraints
-----Example Input-----
2
13 2
0101110000101
6 3
100001
-----Example Output-----
5
4
-----Explanation-----
Example case 1: Chefu can choose the substring $A[2, 3]$ = "10", and change the third character of $A$ to '1'. Then, the pizza time is $5$ days: from day $2$ to day $6$.
Example case 2: Chefu can choose the substring $A[2, 4]$ = "000". Then, the pizza time is $4$ days: from day $1$ to day $4$.
|
def solve():
n, k = map(int, input().split())
a = input()
r = [0] * (n + 1)
l = [0] * n
for i in range(n):
if a[i] == "1":
l[i] = 1
if i > 0:
l[i] += l[i - 1]
j = n - i - 1
if a[j] == "1":
r[j] = 1 + r[j + 1]
mx = 0
for i in range(n - k + 1):
x = k + r[i + k]
if i > 0:
x += l[i - 1]
if x > mx:
mx = x
print(mx)
t = 1
t = int(input())
for _test_ in range(t):
solve()
|
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
|
On each of the following $N$ days (numbered $1$ through $N$), Chef is planning to cook either pizza or broccoli. He wrote down a string $A$ with length $N$, where for each valid $i$, if the character $A_i$ is '1', then he will cook pizza on the $i$-th day, while if $A_i$ is '0', he will cook broccoli on this day.
Chefu, his son, loves pizza but hates broccoli ― just like most kids. He wants to select a substring of $A$ with length $K$ and change each character '0' in this substring to '1'. Afterwards, let's define pizza time as the maximum number of consecutive days where Chef will cook pizza. Find the maximum pizza time Chefu can achieve.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $K$.
- The second line contains a string $A$ with length $N$.
-----Output-----
For each test case, print a single line containing one integer ― the maximum pizza time.
-----Constraints-----
- $1 \le T \le 1,000$
- $1 \le K \le N \le 10^5$
- $A$ contains only characters '0' and '1'
- the sum of $N$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (50 points):
- $N \le 10^3$
- the sum of $N$ over all test cases does not exceed $10^4$
Subtask #2 (50 points): original constraints
-----Example Input-----
2
13 2
0101110000101
6 3
100001
-----Example Output-----
5
4
-----Explanation-----
Example case 1: Chefu can choose the substring $A[2, 3]$ = "10", and change the third character of $A$ to '1'. Then, the pizza time is $5$ days: from day $2$ to day $6$.
Example case 2: Chefu can choose the substring $A[2, 4]$ = "000". Then, the pizza time is $4$ days: from day $1$ to day $4$.
|
for _ in range(int(input())):
n, k = map(int, input().split())
a = input()
left = [0] * n
right = [0] * n
if a[0] == "0":
co = 0
else:
co = 1
for i in range(1, n):
left[i] = co
if a[i] == "0":
co = 0
else:
co += 1
if a[n - 1] == 1:
co = 1
else:
co = 0
for i in range(n - 1, -1, -1):
right[i] = co
if a[i] == "0":
co = 0
else:
co += 1
if k == n:
print(n)
else:
max = 1
for i in range(0, n - k + 1):
if max < left[i] + k + right[i + k - 1]:
max = left[i] + k + right[i + k - 1]
print(max)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR IF VAR NUMBER STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
On each of the following $N$ days (numbered $1$ through $N$), Chef is planning to cook either pizza or broccoli. He wrote down a string $A$ with length $N$, where for each valid $i$, if the character $A_i$ is '1', then he will cook pizza on the $i$-th day, while if $A_i$ is '0', he will cook broccoli on this day.
Chefu, his son, loves pizza but hates broccoli ― just like most kids. He wants to select a substring of $A$ with length $K$ and change each character '0' in this substring to '1'. Afterwards, let's define pizza time as the maximum number of consecutive days where Chef will cook pizza. Find the maximum pizza time Chefu can achieve.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $K$.
- The second line contains a string $A$ with length $N$.
-----Output-----
For each test case, print a single line containing one integer ― the maximum pizza time.
-----Constraints-----
- $1 \le T \le 1,000$
- $1 \le K \le N \le 10^5$
- $A$ contains only characters '0' and '1'
- the sum of $N$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (50 points):
- $N \le 10^3$
- the sum of $N$ over all test cases does not exceed $10^4$
Subtask #2 (50 points): original constraints
-----Example Input-----
2
13 2
0101110000101
6 3
100001
-----Example Output-----
5
4
-----Explanation-----
Example case 1: Chefu can choose the substring $A[2, 3]$ = "10", and change the third character of $A$ to '1'. Then, the pizza time is $5$ days: from day $2$ to day $6$.
Example case 2: Chefu can choose the substring $A[2, 4]$ = "000". Then, the pizza time is $4$ days: from day $1$ to day $4$.
|
for _ in range(int(input())):
N, K = list(map(int, input().split()))
A = ["0"]
A.extend(input())
A.extend(["0"])
B = [0] * (N + 2)
C = B.copy()
for i, j in enumerate(A[1:], start=1):
if j == "1":
B[i] = B[i - 1] + 1
A.reverse()
for i, j in enumerate(A[1:], start=1):
if j == "1":
C[i] = C[i - 1] + 1
A.reverse()
C.reverse()
i = 1
j = K
ans = 0
while j <= N:
ans = max(ans, K + B[i - 1] + C[j + 1])
i += 1
j += 1
print(ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST STRING EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST STRING ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
On each of the following $N$ days (numbered $1$ through $N$), Chef is planning to cook either pizza or broccoli. He wrote down a string $A$ with length $N$, where for each valid $i$, if the character $A_i$ is '1', then he will cook pizza on the $i$-th day, while if $A_i$ is '0', he will cook broccoli on this day.
Chefu, his son, loves pizza but hates broccoli ― just like most kids. He wants to select a substring of $A$ with length $K$ and change each character '0' in this substring to '1'. Afterwards, let's define pizza time as the maximum number of consecutive days where Chef will cook pizza. Find the maximum pizza time Chefu can achieve.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $K$.
- The second line contains a string $A$ with length $N$.
-----Output-----
For each test case, print a single line containing one integer ― the maximum pizza time.
-----Constraints-----
- $1 \le T \le 1,000$
- $1 \le K \le N \le 10^5$
- $A$ contains only characters '0' and '1'
- the sum of $N$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (50 points):
- $N \le 10^3$
- the sum of $N$ over all test cases does not exceed $10^4$
Subtask #2 (50 points): original constraints
-----Example Input-----
2
13 2
0101110000101
6 3
100001
-----Example Output-----
5
4
-----Explanation-----
Example case 1: Chefu can choose the substring $A[2, 3]$ = "10", and change the third character of $A$ to '1'. Then, the pizza time is $5$ days: from day $2$ to day $6$.
Example case 2: Chefu can choose the substring $A[2, 4]$ = "000". Then, the pizza time is $4$ days: from day $1$ to day $4$.
|
def pizzaOrBroc(n, k, a):
L = [0] * n
R = [0] * n
maxpizza = 0
for i in range(1, n):
if int(a[i - 1]) == 1:
L[i] = L[i - 1] + 1
for i in range(n - 2, -1, -1):
if int(a[i + 1]) == 1:
R[i] = R[i + 1] + 1
for i in range(0, n - k + 1):
maxpizza = max(maxpizza, L[i] + k + R[i + k - 1])
return maxpizza
t = int(input())
for _ in range(t):
n, k = map(int, input().strip().split())
temp = input()
a = list(temp)
print(pizzaOrBroc(n, k, a))
|
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
|
On each of the following $N$ days (numbered $1$ through $N$), Chef is planning to cook either pizza or broccoli. He wrote down a string $A$ with length $N$, where for each valid $i$, if the character $A_i$ is '1', then he will cook pizza on the $i$-th day, while if $A_i$ is '0', he will cook broccoli on this day.
Chefu, his son, loves pizza but hates broccoli ― just like most kids. He wants to select a substring of $A$ with length $K$ and change each character '0' in this substring to '1'. Afterwards, let's define pizza time as the maximum number of consecutive days where Chef will cook pizza. Find the maximum pizza time Chefu can achieve.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $K$.
- The second line contains a string $A$ with length $N$.
-----Output-----
For each test case, print a single line containing one integer ― the maximum pizza time.
-----Constraints-----
- $1 \le T \le 1,000$
- $1 \le K \le N \le 10^5$
- $A$ contains only characters '0' and '1'
- the sum of $N$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (50 points):
- $N \le 10^3$
- the sum of $N$ over all test cases does not exceed $10^4$
Subtask #2 (50 points): original constraints
-----Example Input-----
2
13 2
0101110000101
6 3
100001
-----Example Output-----
5
4
-----Explanation-----
Example case 1: Chefu can choose the substring $A[2, 3]$ = "10", and change the third character of $A$ to '1'. Then, the pizza time is $5$ days: from day $2$ to day $6$.
Example case 2: Chefu can choose the substring $A[2, 4]$ = "000". Then, the pizza time is $4$ days: from day $1$ to day $4$.
|
te = int(input())
while te > 0:
te -= 1
n, k = list(map(int, input().split()))
dishes = list(input())
heads = [0] * n
tails = [0] * n
for i in range(1, n):
if dishes[i - 1] == "1":
heads[i] = heads[i - 1] + 1
else:
heads[i] = 0
j = n - 2
while j >= 0:
if dishes[j + 1] == "1":
tails[j] = tails[j + 1] + 1
else:
tails[j] = 0
j -= 1
ans = 0
i = 0
while i + k <= n:
ans = max(ans, heads[i] + k + tails[i + k - 1])
i += 1
print(ans)
|
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
|
On each of the following $N$ days (numbered $1$ through $N$), Chef is planning to cook either pizza or broccoli. He wrote down a string $A$ with length $N$, where for each valid $i$, if the character $A_i$ is '1', then he will cook pizza on the $i$-th day, while if $A_i$ is '0', he will cook broccoli on this day.
Chefu, his son, loves pizza but hates broccoli ― just like most kids. He wants to select a substring of $A$ with length $K$ and change each character '0' in this substring to '1'. Afterwards, let's define pizza time as the maximum number of consecutive days where Chef will cook pizza. Find the maximum pizza time Chefu can achieve.
-----Input-----
- The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows.
- The first line of each test case contains two space-separated integers $N$ and $K$.
- The second line contains a string $A$ with length $N$.
-----Output-----
For each test case, print a single line containing one integer ― the maximum pizza time.
-----Constraints-----
- $1 \le T \le 1,000$
- $1 \le K \le N \le 10^5$
- $A$ contains only characters '0' and '1'
- the sum of $N$ over all test cases does not exceed $10^6$
-----Subtasks-----
Subtask #1 (50 points):
- $N \le 10^3$
- the sum of $N$ over all test cases does not exceed $10^4$
Subtask #2 (50 points): original constraints
-----Example Input-----
2
13 2
0101110000101
6 3
100001
-----Example Output-----
5
4
-----Explanation-----
Example case 1: Chefu can choose the substring $A[2, 3]$ = "10", and change the third character of $A$ to '1'. Then, the pizza time is $5$ days: from day $2$ to day $6$.
Example case 2: Chefu can choose the substring $A[2, 4]$ = "000". Then, the pizza time is $4$ days: from day $1$ to day $4$.
|
for _ in range(int(input())):
n, k = map(int, input().split())
l = input()
head, tail = [0] * n, [0] * n
for i in range(1, n):
if l[i - 1] == "0":
head[i] = 0
else:
head[i] += head[i - 1] + 1
temp = l[::-1]
for i in range(1, n):
if temp[i - 1] == "0":
tail[i] = 0
else:
tail[i] += tail[i - 1] + 1
tail = tail[::-1]
ans = head[0] + k + tail[k - 1]
for i in range(1, n - k + 1):
ans = max(ans, head[i] + k + tail[i + k - 1])
print(ans)
|
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
|
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.
Note: You may not slant the container and n is at least 2.
|
class Solution:
def maxArea(self, height):
area = (len(height) - 1) * min([height[0], height[-1]])
left_idx = 0
right_idx = len(height) - 1
while left_idx < right_idx:
if height[left_idx] < height[right_idx]:
start = left_idx + 1
while start < right_idx:
if height[start] > height[left_idx]:
left_idx = start
area2 = (right_idx - left_idx) * min(
[height[left_idx], height[right_idx]]
)
if area2 > area:
area = area2
break
start += 1
if start != left_idx:
break
elif height[left_idx] > height[right_idx]:
start = right_idx - 1
while start > left_idx:
if height[start] > height[right_idx]:
right_idx = start
area2 = (right_idx - left_idx) * min(
[height[left_idx], height[right_idx]]
)
if area2 > area:
area = area2
break
start -= 1
if start != right_idx:
break
else:
left_idx += 1
return area
|
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR LIST VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR LIST VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR LIST VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER RETURN VAR
|
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.
Note: You may not slant the container and n is at least 2.
|
class Solution:
def maxArea(self, items):
max_area = 0
lo = 0
hi = len(items) - 1
while lo < hi:
height_a = items[lo]
height_b = items[hi]
width = hi - lo
height = min(height_a, height_b)
area = height * width
if area > max_area:
max_area = area
if height_a < height_b:
lo += 1
else:
hi -= 1
return max_area
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
|
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.
Note: You may not slant the container and n is at least 2.
|
class Solution:
def maxArea(self, height):
left = 0
right = len(height) - 1
if height[left] > height[right]:
minH = height[right]
minIndex = right
else:
minH = height[left]
minIndex = left
area = (right - left) * minH
maxArea = area
while left != right:
if minIndex == left:
while left != right:
left += 1
if height[left] > minH:
if height[left] > height[right]:
minH = height[right]
minIndex = right
else:
minH = height[left]
minIndex = left
break
area = (right - left) * minH
else:
while left != right:
right -= 1
if height[right] > minH:
if height[right] > height[left]:
minH = height[left]
minIndex = left
else:
minH = height[right]
minIndex = right
break
area = (right - left) * minH
if area > maxArea:
maxArea = area
return maxArea
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR WHILE VAR VAR VAR NUMBER IF VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR WHILE VAR VAR VAR NUMBER IF VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR
|
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.
Note: You may not slant the container and n is at least 2.
|
class Solution:
def get_ans(self, l, r):
return (r - l) * min(self.__height[l], self.__height[r])
def rmove(self, l):
for i in range(l + 1, len(self.__height)):
if self.__height[i] > self.__height[l]:
return i
return 10000000
def lmove(self, r):
for i in range(r - 1, -1, -1):
if self.__height[i] > self.__height[r]:
return i
return -1
def maxArea(self, height):
self.__height = height
ans = 0
L = 0
R = len(height) - 1
while L < R:
ans = max(ans, self.get_ans(L, R))
if height[L] <= height[R]:
L = self.rmove(L)
else:
R = self.lmove(R)
return ans
|
CLASS_DEF FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR RETURN VAR RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR RETURN VAR RETURN NUMBER FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR
|
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.
Note: You may not slant the container and n is at least 2.
|
class Solution:
def maxArea(self, height):
left, right = 0, len(height) - 1
area = 0
while left < right:
if height[left] <= height[right]:
h = height[left]
tmp = (right - left) * h
left += 1
else:
h = height[right]
tmp = (right - left) * h
right -= 1
if area < tmp:
area = tmp
return area
|
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR RETURN VAR
|
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.
Note: You may not slant the container and n is at least 2.
|
class Solution:
def maxArea(self, height):
h = height
most = 0
i = 0
j = len(h) - 1
while i < j:
most = max(most, (j - i) * min(h[i], h[j]))
if h[i] < h[j]:
i += 1
else:
j -= 1
return most
|
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
|
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.
Note: You may not slant the container and n is at least 2.
|
class Solution:
def maxArea(self, height):
if len(height) < 2:
return False
if len(height) == 2:
return min(height[0], height[1])
current_highest = 0
i, j = 0, len(height) - 1
while i < j:
left = height[i]
right = height[j]
current_highest = max(current_highest, min(left, right) * (j - i))
if right > left:
i += 1
else:
j -= 1
return current_highest
|
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
|
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.
Note: You may not slant the container and n is at least 2.
|
class Solution:
def maxArea(self, height):
left = 0
right = len(height) - 1
max_area = 0
while left < right:
area = min(height[left], height[right]) * (right - left)
if area > max_area:
max_area = area
if height[left] < height[right]:
left += 1
else:
right -= 1
return max_area
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
|
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.
Note: You may not slant the container and n is at least 2.
|
class Solution:
def maxArea(self, height):
if height == []:
return 0
l = len(height)
p1 = 0
p2 = l - 1
area = 0
while p1 < p2:
if height[p1] <= height[p2]:
area = max(area, height[p1] * (p2 - p1))
p1 += 1
else:
area = max(area, height[p2] * (p2 - p1))
p2 -= 1
return area
|
CLASS_DEF FUNC_DEF IF VAR LIST RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR
|
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.
Note: You may not slant the container and n is at least 2.
|
class Solution:
def maxArea(self, height):
left_index = 0
right_index = len(height) - 1
water = 0
while True:
if left_index >= right_index:
break
left_height = height[left_index]
right_height = height[right_index]
water = max(
water, (right_index - left_index) * min(left_height, right_height)
)
if left_height < right_height:
left_index += 1
else:
right_index -= 1
return water
|
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.