description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Given an array of positive integers target and an array initial of same size with all zeros.
Return the minimum number of operations to form a target array from initial if you are allowed to do the following operation:
Choose any subarray from initial and increment each value by one.
The answer is guaranteed to fit within the range of a 32-bit signed integer.
Example 1:
Input: target = [1,2,3,2,1]
Output: 3
Explanation: We need at least 3 operations to form the target array from the initial array.
[0,0,0,0,0] increment 1 from index 0 to 4 (inclusive).
[1,1,1,1,1] increment 1 from index 1 to 3 (inclusive).
[1,2,2,2,1] increment 1 at index 2.
[1,2,3,2,1] target array is formed.
Example 2:
Input: target = [3,1,1,2]
Output: 4
Explanation: (initial)[0,0,0,0] -> [1,1,1,1] -> [1,1,1,2] -> [2,1,1,2] -> [3,1,1,2] (target).
Example 3:
Input: target = [3,1,5,4,2]
Output: 7
Explanation: (initial)[0,0,0,0,0] -> [1,1,1,1,1] -> [2,1,1,1,1] -> [3,1,1,1,1]
-> [3,1,2,2,2] -> [3,1,3,3,2] -> [3,1,4,4,2] -> [3,1,5,4,2] (target).
Example 4:
Input: target = [1,1,1,1]
Output: 1
Constraints:
1 <= target.length <= 10^5
1 <= target[i] <= 10^5 | class Solution:
def minNumberOperations(self, target: List[int]) -> int:
tLen = len(target)
maxValInd = [target[0], 0]
minValInd = [target[0], 0]
steps = target[0]
for i in range(1, tLen):
if target[i] >= maxValInd[0]:
if not maxValInd[1] <= minValInd[1] <= i - 1:
steps += target[i] - maxValInd[0]
maxValInd = [target[i], i]
if target[i] <= minValInd[0]:
minValInd = [target[i], i]
else:
steps += target[i] - minValInd[0]
maxValInd = [target[i], i]
minValInd = [target[i], i]
else:
maxValInd = [target[i], i]
if target[i] <= minValInd[0]:
minValInd = [target[i], i]
return steps | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR NUMBER NUMBER ASSIGN VAR LIST VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR LIST VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR LIST VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR LIST VAR VAR VAR ASSIGN VAR LIST VAR VAR VAR ASSIGN VAR LIST VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR LIST VAR VAR VAR RETURN VAR VAR |
Given an array of positive integers target and an array initial of same size with all zeros.
Return the minimum number of operations to form a target array from initial if you are allowed to do the following operation:
Choose any subarray from initial and increment each value by one.
The answer is guaranteed to fit within the range of a 32-bit signed integer.
Example 1:
Input: target = [1,2,3,2,1]
Output: 3
Explanation: We need at least 3 operations to form the target array from the initial array.
[0,0,0,0,0] increment 1 from index 0 to 4 (inclusive).
[1,1,1,1,1] increment 1 from index 1 to 3 (inclusive).
[1,2,2,2,1] increment 1 at index 2.
[1,2,3,2,1] target array is formed.
Example 2:
Input: target = [3,1,1,2]
Output: 4
Explanation: (initial)[0,0,0,0] -> [1,1,1,1] -> [1,1,1,2] -> [2,1,1,2] -> [3,1,1,2] (target).
Example 3:
Input: target = [3,1,5,4,2]
Output: 7
Explanation: (initial)[0,0,0,0,0] -> [1,1,1,1,1] -> [2,1,1,1,1] -> [3,1,1,1,1]
-> [3,1,2,2,2] -> [3,1,3,3,2] -> [3,1,4,4,2] -> [3,1,5,4,2] (target).
Example 4:
Input: target = [1,1,1,1]
Output: 1
Constraints:
1 <= target.length <= 10^5
1 <= target[i] <= 10^5 | class Solution:
def minNumberOperations(self, target: List[int]) -> int:
stack, count = [], 0
for x in target:
m = 0
while stack and stack[-1] >= x:
m = max(m, stack.pop() - x)
count += m
stack.append(x)
count += stack[-1]
return count | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR LIST NUMBER FOR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR VAR |
Given an array of positive integers target and an array initial of same size with all zeros.
Return the minimum number of operations to form a target array from initial if you are allowed to do the following operation:
Choose any subarray from initial and increment each value by one.
The answer is guaranteed to fit within the range of a 32-bit signed integer.
Example 1:
Input: target = [1,2,3,2,1]
Output: 3
Explanation: We need at least 3 operations to form the target array from the initial array.
[0,0,0,0,0] increment 1 from index 0 to 4 (inclusive).
[1,1,1,1,1] increment 1 from index 1 to 3 (inclusive).
[1,2,2,2,1] increment 1 at index 2.
[1,2,3,2,1] target array is formed.
Example 2:
Input: target = [3,1,1,2]
Output: 4
Explanation: (initial)[0,0,0,0] -> [1,1,1,1] -> [1,1,1,2] -> [2,1,1,2] -> [3,1,1,2] (target).
Example 3:
Input: target = [3,1,5,4,2]
Output: 7
Explanation: (initial)[0,0,0,0,0] -> [1,1,1,1,1] -> [2,1,1,1,1] -> [3,1,1,1,1]
-> [3,1,2,2,2] -> [3,1,3,3,2] -> [3,1,4,4,2] -> [3,1,5,4,2] (target).
Example 4:
Input: target = [1,1,1,1]
Output: 1
Constraints:
1 <= target.length <= 10^5
1 <= target[i] <= 10^5 | class Solution:
def minNumberOperations(self, target: List[int]) -> int:
res = 0
for i in range(len(target)):
if i == 0:
res = target[0]
continue
d = target[i] - target[i - 1]
if d > 0:
res += d
return res | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR RETURN VAR VAR |
Given an array of positive integers target and an array initial of same size with all zeros.
Return the minimum number of operations to form a target array from initial if you are allowed to do the following operation:
Choose any subarray from initial and increment each value by one.
The answer is guaranteed to fit within the range of a 32-bit signed integer.
Example 1:
Input: target = [1,2,3,2,1]
Output: 3
Explanation: We need at least 3 operations to form the target array from the initial array.
[0,0,0,0,0] increment 1 from index 0 to 4 (inclusive).
[1,1,1,1,1] increment 1 from index 1 to 3 (inclusive).
[1,2,2,2,1] increment 1 at index 2.
[1,2,3,2,1] target array is formed.
Example 2:
Input: target = [3,1,1,2]
Output: 4
Explanation: (initial)[0,0,0,0] -> [1,1,1,1] -> [1,1,1,2] -> [2,1,1,2] -> [3,1,1,2] (target).
Example 3:
Input: target = [3,1,5,4,2]
Output: 7
Explanation: (initial)[0,0,0,0,0] -> [1,1,1,1,1] -> [2,1,1,1,1] -> [3,1,1,1,1]
-> [3,1,2,2,2] -> [3,1,3,3,2] -> [3,1,4,4,2] -> [3,1,5,4,2] (target).
Example 4:
Input: target = [1,1,1,1]
Output: 1
Constraints:
1 <= target.length <= 10^5
1 <= target[i] <= 10^5 | class Solution:
def minNumberOperations(self, target: List[int]) -> int:
if not target:
return 0
totalCost = target[0]
for index in range(1, len(target)):
num = target[index]
prev = target[index - 1]
diff = num - prev
totalCost += max(diff, 0)
return totalCost
lst = []
lst.append(target)
prev_layer = target
new_layer = []
while len(prev_layer) > 1:
counter = 0
while counter < len(prev_layer):
fi = prev_layer[counter]
se = fi
if counter + 1 < len(prev_layer):
se = prev_layer[counter + 1]
new_layer.append(min(fi, se))
counter += 2
lst.append(new_layer)
prev_layer = new_layer
new_layer = []
lst.append([0])
cost = 0
print(lst)
for index in range(len(lst) - 2, -1, -1):
l = lst[index]
print(("l", l))
for in_index in range(len(l)):
val = l[in_index]
parent_index = int(in_index / 2)
cost += val - lst[index + 1][parent_index]
print(("val", val))
print(("parent_val", lst[index + 1][parent_index]))
print(("cost", cost))
print("--------")
return cost | CLASS_DEF FUNC_DEF VAR VAR IF VAR RETURN NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR LIST NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR STRING VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR STRING VAR EXPR FUNC_CALL VAR STRING VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR STRING VAR EXPR FUNC_CALL VAR STRING RETURN VAR VAR |
Given an array of positive integers target and an array initial of same size with all zeros.
Return the minimum number of operations to form a target array from initial if you are allowed to do the following operation:
Choose any subarray from initial and increment each value by one.
The answer is guaranteed to fit within the range of a 32-bit signed integer.
Example 1:
Input: target = [1,2,3,2,1]
Output: 3
Explanation: We need at least 3 operations to form the target array from the initial array.
[0,0,0,0,0] increment 1 from index 0 to 4 (inclusive).
[1,1,1,1,1] increment 1 from index 1 to 3 (inclusive).
[1,2,2,2,1] increment 1 at index 2.
[1,2,3,2,1] target array is formed.
Example 2:
Input: target = [3,1,1,2]
Output: 4
Explanation: (initial)[0,0,0,0] -> [1,1,1,1] -> [1,1,1,2] -> [2,1,1,2] -> [3,1,1,2] (target).
Example 3:
Input: target = [3,1,5,4,2]
Output: 7
Explanation: (initial)[0,0,0,0,0] -> [1,1,1,1,1] -> [2,1,1,1,1] -> [3,1,1,1,1]
-> [3,1,2,2,2] -> [3,1,3,3,2] -> [3,1,4,4,2] -> [3,1,5,4,2] (target).
Example 4:
Input: target = [1,1,1,1]
Output: 1
Constraints:
1 <= target.length <= 10^5
1 <= target[i] <= 10^5 | class Solution:
def minNumberOperations(self, target: List[int]) -> int:
stack = []
ans = 0
for n in target:
if len(stack) != 0 and n <= stack[-1]:
ans += stack[-1] - n
while len(stack) != 0 and n <= stack[-1]:
x = stack.pop()
stack.append(n)
if len(stack) != 0:
ans += stack[-1]
return ans | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR WHILE FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER RETURN VAR VAR |
Given an array of positive integers target and an array initial of same size with all zeros.
Return the minimum number of operations to form a target array from initial if you are allowed to do the following operation:
Choose any subarray from initial and increment each value by one.
The answer is guaranteed to fit within the range of a 32-bit signed integer.
Example 1:
Input: target = [1,2,3,2,1]
Output: 3
Explanation: We need at least 3 operations to form the target array from the initial array.
[0,0,0,0,0] increment 1 from index 0 to 4 (inclusive).
[1,1,1,1,1] increment 1 from index 1 to 3 (inclusive).
[1,2,2,2,1] increment 1 at index 2.
[1,2,3,2,1] target array is formed.
Example 2:
Input: target = [3,1,1,2]
Output: 4
Explanation: (initial)[0,0,0,0] -> [1,1,1,1] -> [1,1,1,2] -> [2,1,1,2] -> [3,1,1,2] (target).
Example 3:
Input: target = [3,1,5,4,2]
Output: 7
Explanation: (initial)[0,0,0,0,0] -> [1,1,1,1,1] -> [2,1,1,1,1] -> [3,1,1,1,1]
-> [3,1,2,2,2] -> [3,1,3,3,2] -> [3,1,4,4,2] -> [3,1,5,4,2] (target).
Example 4:
Input: target = [1,1,1,1]
Output: 1
Constraints:
1 <= target.length <= 10^5
1 <= target[i] <= 10^5 | class Solution:
def minNumberOperations(self, target: List[int]) -> int:
count = target[0]
height = target[0]
for val in target[1:]:
if val > height:
count += val - height
height = val
return count | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR RETURN VAR VAR |
Given an array of positive integers target and an array initial of same size with all zeros.
Return the minimum number of operations to form a target array from initial if you are allowed to do the following operation:
Choose any subarray from initial and increment each value by one.
The answer is guaranteed to fit within the range of a 32-bit signed integer.
Example 1:
Input: target = [1,2,3,2,1]
Output: 3
Explanation: We need at least 3 operations to form the target array from the initial array.
[0,0,0,0,0] increment 1 from index 0 to 4 (inclusive).
[1,1,1,1,1] increment 1 from index 1 to 3 (inclusive).
[1,2,2,2,1] increment 1 at index 2.
[1,2,3,2,1] target array is formed.
Example 2:
Input: target = [3,1,1,2]
Output: 4
Explanation: (initial)[0,0,0,0] -> [1,1,1,1] -> [1,1,1,2] -> [2,1,1,2] -> [3,1,1,2] (target).
Example 3:
Input: target = [3,1,5,4,2]
Output: 7
Explanation: (initial)[0,0,0,0,0] -> [1,1,1,1,1] -> [2,1,1,1,1] -> [3,1,1,1,1]
-> [3,1,2,2,2] -> [3,1,3,3,2] -> [3,1,4,4,2] -> [3,1,5,4,2] (target).
Example 4:
Input: target = [1,1,1,1]
Output: 1
Constraints:
1 <= target.length <= 10^5
1 <= target[i] <= 10^5 | class Solution:
def minNumberOperations(self, target: List[int]) -> int:
res = 0
prev = 0
for t in target:
res += max(t - prev, 0)
prev = t
return res | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR |
Given an array of positive integers target and an array initial of same size with all zeros.
Return the minimum number of operations to form a target array from initial if you are allowed to do the following operation:
Choose any subarray from initial and increment each value by one.
The answer is guaranteed to fit within the range of a 32-bit signed integer.
Example 1:
Input: target = [1,2,3,2,1]
Output: 3
Explanation: We need at least 3 operations to form the target array from the initial array.
[0,0,0,0,0] increment 1 from index 0 to 4 (inclusive).
[1,1,1,1,1] increment 1 from index 1 to 3 (inclusive).
[1,2,2,2,1] increment 1 at index 2.
[1,2,3,2,1] target array is formed.
Example 2:
Input: target = [3,1,1,2]
Output: 4
Explanation: (initial)[0,0,0,0] -> [1,1,1,1] -> [1,1,1,2] -> [2,1,1,2] -> [3,1,1,2] (target).
Example 3:
Input: target = [3,1,5,4,2]
Output: 7
Explanation: (initial)[0,0,0,0,0] -> [1,1,1,1,1] -> [2,1,1,1,1] -> [3,1,1,1,1]
-> [3,1,2,2,2] -> [3,1,3,3,2] -> [3,1,4,4,2] -> [3,1,5,4,2] (target).
Example 4:
Input: target = [1,1,1,1]
Output: 1
Constraints:
1 <= target.length <= 10^5
1 <= target[i] <= 10^5 | class Solution:
def minNumberOperations(self, target: List[int]) -> int:
l = target
stack = []
total = 0
for i in range(len(l)):
if stack and l[stack[-1]] > l[i]:
total += l[stack[-1]] - l[i]
while stack and l[stack[-1]] > l[i]:
stack.pop()
if not stack or l[stack[-1]] < l[i]:
stack.append(i)
if stack:
total += l[stack[-1]]
return total | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR WHILE VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR IF VAR VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER RETURN VAR VAR |
Given an array of positive integers target and an array initial of same size with all zeros.
Return the minimum number of operations to form a target array from initial if you are allowed to do the following operation:
Choose any subarray from initial and increment each value by one.
The answer is guaranteed to fit within the range of a 32-bit signed integer.
Example 1:
Input: target = [1,2,3,2,1]
Output: 3
Explanation: We need at least 3 operations to form the target array from the initial array.
[0,0,0,0,0] increment 1 from index 0 to 4 (inclusive).
[1,1,1,1,1] increment 1 from index 1 to 3 (inclusive).
[1,2,2,2,1] increment 1 at index 2.
[1,2,3,2,1] target array is formed.
Example 2:
Input: target = [3,1,1,2]
Output: 4
Explanation: (initial)[0,0,0,0] -> [1,1,1,1] -> [1,1,1,2] -> [2,1,1,2] -> [3,1,1,2] (target).
Example 3:
Input: target = [3,1,5,4,2]
Output: 7
Explanation: (initial)[0,0,0,0,0] -> [1,1,1,1,1] -> [2,1,1,1,1] -> [3,1,1,1,1]
-> [3,1,2,2,2] -> [3,1,3,3,2] -> [3,1,4,4,2] -> [3,1,5,4,2] (target).
Example 4:
Input: target = [1,1,1,1]
Output: 1
Constraints:
1 <= target.length <= 10^5
1 <= target[i] <= 10^5 | class Solution:
def minNumberOperations(self, target: List[int]) -> int:
ans = 0
stack = [0]
for num in target:
max_ = 0
while stack and stack[-1] >= num:
max_ = max(max_, abs(stack.pop() - num))
ans += max_
stack.append(num)
while stack[-1]:
ans += abs(stack.pop() - stack[-1])
return ans | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR VAR |
Given an array of positive integers target and an array initial of same size with all zeros.
Return the minimum number of operations to form a target array from initial if you are allowed to do the following operation:
Choose any subarray from initial and increment each value by one.
The answer is guaranteed to fit within the range of a 32-bit signed integer.
Example 1:
Input: target = [1,2,3,2,1]
Output: 3
Explanation: We need at least 3 operations to form the target array from the initial array.
[0,0,0,0,0] increment 1 from index 0 to 4 (inclusive).
[1,1,1,1,1] increment 1 from index 1 to 3 (inclusive).
[1,2,2,2,1] increment 1 at index 2.
[1,2,3,2,1] target array is formed.
Example 2:
Input: target = [3,1,1,2]
Output: 4
Explanation: (initial)[0,0,0,0] -> [1,1,1,1] -> [1,1,1,2] -> [2,1,1,2] -> [3,1,1,2] (target).
Example 3:
Input: target = [3,1,5,4,2]
Output: 7
Explanation: (initial)[0,0,0,0,0] -> [1,1,1,1,1] -> [2,1,1,1,1] -> [3,1,1,1,1]
-> [3,1,2,2,2] -> [3,1,3,3,2] -> [3,1,4,4,2] -> [3,1,5,4,2] (target).
Example 4:
Input: target = [1,1,1,1]
Output: 1
Constraints:
1 <= target.length <= 10^5
1 <= target[i] <= 10^5 | class Solution:
def minNumberOperations(self, target: List[int]) -> int:
result = 0
for index, value in enumerate(target):
if index == 0:
result = value
elif value > target[index - 1]:
result += value - target[index - 1]
pass
return result | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR |
Given an array of positive integers target and an array initial of same size with all zeros.
Return the minimum number of operations to form a target array from initial if you are allowed to do the following operation:
Choose any subarray from initial and increment each value by one.
The answer is guaranteed to fit within the range of a 32-bit signed integer.
Example 1:
Input: target = [1,2,3,2,1]
Output: 3
Explanation: We need at least 3 operations to form the target array from the initial array.
[0,0,0,0,0] increment 1 from index 0 to 4 (inclusive).
[1,1,1,1,1] increment 1 from index 1 to 3 (inclusive).
[1,2,2,2,1] increment 1 at index 2.
[1,2,3,2,1] target array is formed.
Example 2:
Input: target = [3,1,1,2]
Output: 4
Explanation: (initial)[0,0,0,0] -> [1,1,1,1] -> [1,1,1,2] -> [2,1,1,2] -> [3,1,1,2] (target).
Example 3:
Input: target = [3,1,5,4,2]
Output: 7
Explanation: (initial)[0,0,0,0,0] -> [1,1,1,1,1] -> [2,1,1,1,1] -> [3,1,1,1,1]
-> [3,1,2,2,2] -> [3,1,3,3,2] -> [3,1,4,4,2] -> [3,1,5,4,2] (target).
Example 4:
Input: target = [1,1,1,1]
Output: 1
Constraints:
1 <= target.length <= 10^5
1 <= target[i] <= 10^5 | class Solution1:
def minNumberOperations(self, target: List[int]) -> int:
max_bar, blocks = max(target), 0
for bar in range(1, max_bar + 1):
op = [0] * len(target)
prev_op = 0
for i, t in enumerate(target):
if t >= bar:
op[i] = 1
if prev_op != op[i]:
prev_op = op[i]
if op[i] == 1:
blocks += 1
return blocks
class Solution:
def minNumberOperations(self, target: List[int]) -> int:
prev_t, n, valley = 0, len(target), 0
blocks = 0
for i in range(n):
if i + 1 < n and target[i + 1] == target[i]:
continue
if prev_t < target[i] and (i + 1 == n or target[i] > target[i + 1]):
blocks += target[i] - valley
if target[i] < prev_t and (i + 1 == n or target[i] <= target[i + 1]):
valley = target[i]
prev_t = target[i]
return blocks | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER RETURN VAR VAR CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR |
Given an array of positive integers target and an array initial of same size with all zeros.
Return the minimum number of operations to form a target array from initial if you are allowed to do the following operation:
Choose any subarray from initial and increment each value by one.
The answer is guaranteed to fit within the range of a 32-bit signed integer.
Example 1:
Input: target = [1,2,3,2,1]
Output: 3
Explanation: We need at least 3 operations to form the target array from the initial array.
[0,0,0,0,0] increment 1 from index 0 to 4 (inclusive).
[1,1,1,1,1] increment 1 from index 1 to 3 (inclusive).
[1,2,2,2,1] increment 1 at index 2.
[1,2,3,2,1] target array is formed.
Example 2:
Input: target = [3,1,1,2]
Output: 4
Explanation: (initial)[0,0,0,0] -> [1,1,1,1] -> [1,1,1,2] -> [2,1,1,2] -> [3,1,1,2] (target).
Example 3:
Input: target = [3,1,5,4,2]
Output: 7
Explanation: (initial)[0,0,0,0,0] -> [1,1,1,1,1] -> [2,1,1,1,1] -> [3,1,1,1,1]
-> [3,1,2,2,2] -> [3,1,3,3,2] -> [3,1,4,4,2] -> [3,1,5,4,2] (target).
Example 4:
Input: target = [1,1,1,1]
Output: 1
Constraints:
1 <= target.length <= 10^5
1 <= target[i] <= 10^5 | class Solution:
def minNumberOperations(self, target: List[int]) -> int:
ans = 0
stack = [0]
for k in target:
while len(stack) >= 1 and stack[-1] >= k:
ans += stack[-1] - max(k, stack[-2])
stack.pop()
stack.append(k)
return ans + stack[-1] | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR RETURN BIN_OP VAR VAR NUMBER VAR |
Given an array of positive integers target and an array initial of same size with all zeros.
Return the minimum number of operations to form a target array from initial if you are allowed to do the following operation:
Choose any subarray from initial and increment each value by one.
The answer is guaranteed to fit within the range of a 32-bit signed integer.
Example 1:
Input: target = [1,2,3,2,1]
Output: 3
Explanation: We need at least 3 operations to form the target array from the initial array.
[0,0,0,0,0] increment 1 from index 0 to 4 (inclusive).
[1,1,1,1,1] increment 1 from index 1 to 3 (inclusive).
[1,2,2,2,1] increment 1 at index 2.
[1,2,3,2,1] target array is formed.
Example 2:
Input: target = [3,1,1,2]
Output: 4
Explanation: (initial)[0,0,0,0] -> [1,1,1,1] -> [1,1,1,2] -> [2,1,1,2] -> [3,1,1,2] (target).
Example 3:
Input: target = [3,1,5,4,2]
Output: 7
Explanation: (initial)[0,0,0,0,0] -> [1,1,1,1,1] -> [2,1,1,1,1] -> [3,1,1,1,1]
-> [3,1,2,2,2] -> [3,1,3,3,2] -> [3,1,4,4,2] -> [3,1,5,4,2] (target).
Example 4:
Input: target = [1,1,1,1]
Output: 1
Constraints:
1 <= target.length <= 10^5
1 <= target[i] <= 10^5 | class Solution:
def minNumberOperations(self, target: List[int]) -> int:
tLen = len(target)
steps = target[0]
reusableOperations = target[0]
for i in range(1, tLen):
if target[i] <= reusableOperations:
reusableOperations = target[i]
else:
steps += target[i] - reusableOperations
reusableOperations = target[i]
return steps | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR |
During cleaning the coast, Alice found $n$ piles of stones. The $i$-th pile has $a_i$ stones.
Piles $i$ and $i + 1$ are neighbouring for all $1 \leq i \leq n - 1$. If pile $i$ becomes empty, piles $i - 1$ and $i + 1$ doesn't become neighbouring.
Alice is too lazy to remove these stones, so she asked you to take this duty. She allowed you to do only the following operation:
Select two neighboring piles and, if both of them are not empty, remove one stone from each of them.
Alice understands that sometimes it's impossible to remove all stones with the given operation, so she allowed you to use the following superability:
Before the start of cleaning, you can select two neighboring piles and swap them.
Determine, if it is possible to remove all stones using the superability not more than once.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \leq n \leq 2 \cdot 10^5$) — the number of piles.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) — the number of stones in each pile.
It is guaranteed that the total sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print YES or NO — is it possible to remove all stones using the superability not more than once or not.
-----Examples-----
Input
5
3
1 2 1
3
1 1 2
5
2 2 2 1 3
5
2100 1900 1600 3000 1600
2
2443 2445
Output
YES
YES
YES
YES
NO
-----Note-----
In the first test case, you can remove all stones without using a superability: $[1, 2, 1] \rightarrow [1, 1, 0] \rightarrow [0, 0, 0]$.
In the second test case, you can apply superability to the second and the third piles and then act like in the first testcase.
In the third test case, you can apply superability to the fourth and the fifth piles, thus getting $a = [2, 2, 2, 3, 1]$.
In the fourth test case, you can apply superability to the first and the second piles, thus getting $a = [1900, 2100, 1600, 3000, 1600]$. | import sys
readline = sys.stdin.readline
T = int(readline())
Ans = ["NO"] * T
INF = 2 * 10**9 + 7
for qu in range(T):
N = int(readline())
A = list(map(int, readline().split()))
if N & 1:
A.append(0)
N += 1
B = A[:]
for i in range(1, N):
B[i] -= B[i - 1]
if all(b >= 0 for b in B) and B[-1] == 0:
Ans[qu] = "YES"
continue
Beven = [B[i] for i in range(N) if i % 2 == 0]
Bodd = [B[i] for i in range(N) if i % 2 == 1]
for l in range(len(Beven) - 2, -1, -1):
Beven[l] = min(Beven[l], Beven[l + 1])
for l in range(len(Bodd) - 2, -1, -1):
Bodd[l] = min(Bodd[l], Bodd[l + 1])
Beven.append(INF)
Bodd.append(INF)
for i in range(N - 1):
if i and B[i - 1] < 0:
break
d = A[i + 1] - A[i]
if B[i] + d >= 0:
if i & 1:
if B[-1] + 2 * d:
continue
if Bodd[(i + 2) // 2] + 2 * d >= 0 and Beven[(i + 1) // 2] - 2 * d >= 0:
Ans[qu] = "YES"
break
else:
if B[-1] - 2 * d:
continue
if Beven[(i + 2) // 2] + 2 * d >= 0 and Bodd[(i + 1) // 2] - 2 * d >= 0:
Ans[qu] = "YES"
break
print("\n".join(Ans)) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER 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 IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR STRING ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF BIN_OP VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER BIN_OP NUMBER VAR IF BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR STRING IF BIN_OP VAR NUMBER BIN_OP NUMBER VAR IF BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER VAR NUMBER BIN_OP VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
During cleaning the coast, Alice found $n$ piles of stones. The $i$-th pile has $a_i$ stones.
Piles $i$ and $i + 1$ are neighbouring for all $1 \leq i \leq n - 1$. If pile $i$ becomes empty, piles $i - 1$ and $i + 1$ doesn't become neighbouring.
Alice is too lazy to remove these stones, so she asked you to take this duty. She allowed you to do only the following operation:
Select two neighboring piles and, if both of them are not empty, remove one stone from each of them.
Alice understands that sometimes it's impossible to remove all stones with the given operation, so she allowed you to use the following superability:
Before the start of cleaning, you can select two neighboring piles and swap them.
Determine, if it is possible to remove all stones using the superability not more than once.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \leq n \leq 2 \cdot 10^5$) — the number of piles.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) — the number of stones in each pile.
It is guaranteed that the total sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print YES or NO — is it possible to remove all stones using the superability not more than once or not.
-----Examples-----
Input
5
3
1 2 1
3
1 1 2
5
2 2 2 1 3
5
2100 1900 1600 3000 1600
2
2443 2445
Output
YES
YES
YES
YES
NO
-----Note-----
In the first test case, you can remove all stones without using a superability: $[1, 2, 1] \rightarrow [1, 1, 0] \rightarrow [0, 0, 0]$.
In the second test case, you can apply superability to the second and the third piles and then act like in the first testcase.
In the third test case, you can apply superability to the fourth and the fifth piles, thus getting $a = [2, 2, 2, 3, 1]$.
In the fourth test case, you can apply superability to the first and the second piles, thus getting $a = [1900, 2100, 1600, 3000, 1600]$. | def can_clean(case):
cur_num = case[0]
for i in range(1, len(case)):
next_num = case[i] - cur_num
if next_num < 0:
return False
cur_num = next_num
return True if cur_num == 0 else False
for t in range(int(input())):
n = int(input())
aa = [int(x) for x in input().split()]
fwd = {}
have_printed = False
bwd = {}
for i in range(len(aa)):
ai = aa[i]
if i == 0:
fwd[i] = ai
else:
next_val = ai - fwd[i - 1]
if next_val < 0:
break
fwd[i] = next_val
for i in range(len(aa) - 1, -1, -1):
ai = aa[i]
if i == len(aa) - 1:
bwd[i] = ai
else:
next_val = ai - bwd[i + 1]
if next_val < 0:
break
bwd[i] = next_val
if len(bwd) == len(aa) and bwd[0] == 0:
print("YES")
continue
for i in range(len(aa) - 1):
swap_pos = i, i + 1
if i == 0:
case = [aa[i + 1], aa[i], bwd.get(i + 2, -1)]
elif i + 1 == len(aa) - 1:
case = [fwd.get(i - 1, -1), aa[i + 1], aa[i]]
else:
case = [fwd.get(i - 1, -1), aa[i + 1], aa[i], bwd.get(i + 2, -1)]
if case[0] < 0 or case[-1] < 0:
continue
if can_clean(case):
print("YES")
have_printed = True
break
if not have_printed:
print("NO") | FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR RETURN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR LIST VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR LIST FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING |
During cleaning the coast, Alice found $n$ piles of stones. The $i$-th pile has $a_i$ stones.
Piles $i$ and $i + 1$ are neighbouring for all $1 \leq i \leq n - 1$. If pile $i$ becomes empty, piles $i - 1$ and $i + 1$ doesn't become neighbouring.
Alice is too lazy to remove these stones, so she asked you to take this duty. She allowed you to do only the following operation:
Select two neighboring piles and, if both of them are not empty, remove one stone from each of them.
Alice understands that sometimes it's impossible to remove all stones with the given operation, so she allowed you to use the following superability:
Before the start of cleaning, you can select two neighboring piles and swap them.
Determine, if it is possible to remove all stones using the superability not more than once.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \leq n \leq 2 \cdot 10^5$) — the number of piles.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) — the number of stones in each pile.
It is guaranteed that the total sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print YES or NO — is it possible to remove all stones using the superability not more than once or not.
-----Examples-----
Input
5
3
1 2 1
3
1 1 2
5
2 2 2 1 3
5
2100 1900 1600 3000 1600
2
2443 2445
Output
YES
YES
YES
YES
NO
-----Note-----
In the first test case, you can remove all stones without using a superability: $[1, 2, 1] \rightarrow [1, 1, 0] \rightarrow [0, 0, 0]$.
In the second test case, you can apply superability to the second and the third piles and then act like in the first testcase.
In the third test case, you can apply superability to the fourth and the fifth piles, thus getting $a = [2, 2, 2, 3, 1]$.
In the fourth test case, you can apply superability to the first and the second piles, thus getting $a = [1900, 2100, 1600, 3000, 1600]$. | from sys import stdin, stdout
def cleaning(n, a_a):
l_a = [-1] * n
r_a = [-1] * n
l_a[0] = a_a[0]
for i in range(1, n):
if a_a[i] < l_a[i - 1]:
break
l_a[i] = a_a[i] - l_a[i - 1]
if l_a[n - 1] == 0:
return "YES"
r_a[n - 1] = a_a[n - 1]
for i in range(n - 2, -1, -1):
if a_a[i] < r_a[i + 1]:
break
r_a[i] = a_a[i] - r_a[i + 1]
for i in range(0, n - 1):
r = a_a[i]
l = a_a[i + 1]
if i > 0:
if l_a[i - 1] < 0:
continue
l -= l_a[i - 1]
if i + 2 < n:
if r_a[i + 2] < 0:
continue
r -= r_a[i + 2]
if 0 <= l == r >= 0:
return "YES"
return "NO"
t = int(stdin.readline())
for _ in range(t):
n = int(stdin.readline())
a_a = list(map(int, stdin.readline().split()))
r = cleaning(n, a_a)
stdout.write(r + "\n") | FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER RETURN STRING ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER IF NUMBER VAR VAR NUMBER RETURN STRING RETURN STRING 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 FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR STRING |
During cleaning the coast, Alice found $n$ piles of stones. The $i$-th pile has $a_i$ stones.
Piles $i$ and $i + 1$ are neighbouring for all $1 \leq i \leq n - 1$. If pile $i$ becomes empty, piles $i - 1$ and $i + 1$ doesn't become neighbouring.
Alice is too lazy to remove these stones, so she asked you to take this duty. She allowed you to do only the following operation:
Select two neighboring piles and, if both of them are not empty, remove one stone from each of them.
Alice understands that sometimes it's impossible to remove all stones with the given operation, so she allowed you to use the following superability:
Before the start of cleaning, you can select two neighboring piles and swap them.
Determine, if it is possible to remove all stones using the superability not more than once.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \leq n \leq 2 \cdot 10^5$) — the number of piles.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) — the number of stones in each pile.
It is guaranteed that the total sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print YES or NO — is it possible to remove all stones using the superability not more than once or not.
-----Examples-----
Input
5
3
1 2 1
3
1 1 2
5
2 2 2 1 3
5
2100 1900 1600 3000 1600
2
2443 2445
Output
YES
YES
YES
YES
NO
-----Note-----
In the first test case, you can remove all stones without using a superability: $[1, 2, 1] \rightarrow [1, 1, 0] \rightarrow [0, 0, 0]$.
In the second test case, you can apply superability to the second and the third piles and then act like in the first testcase.
In the third test case, you can apply superability to the fourth and the fifth piles, thus getting $a = [2, 2, 2, 3, 1]$.
In the fourth test case, you can apply superability to the first and the second piles, thus getting $a = [1900, 2100, 1600, 3000, 1600]$. | import sys
input = sys.stdin.readline
for _ in range(int(input())):
N = int(input())
a = list(map(int, input().split()))
l = [-1] * N
l[0] = a[0]
for i in range(N - 1):
if l[i] >= 0:
l[i + 1] = a[i + 1] - l[i]
r = [-1] * N
r[-1] = a[-1]
for i in range(N - 1, 0, -1):
if r[i] >= 0:
r[i - 1] = a[i - 1] - r[i]
if l[-1] == r[0] == 0:
print("YES")
continue
for i in range(N - 1):
x, y, z, w = 0, a[i + 1], a[i], 0
if i - 1 >= 0:
x = l[i - 1]
if i + 2 < N:
w = r[i + 2]
if y - x == z - w and x >= 0 and w >= 0 and x <= y and w <= z:
print("YES")
break
else:
print("NO") | IMPORT ASSIGN 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
During cleaning the coast, Alice found $n$ piles of stones. The $i$-th pile has $a_i$ stones.
Piles $i$ and $i + 1$ are neighbouring for all $1 \leq i \leq n - 1$. If pile $i$ becomes empty, piles $i - 1$ and $i + 1$ doesn't become neighbouring.
Alice is too lazy to remove these stones, so she asked you to take this duty. She allowed you to do only the following operation:
Select two neighboring piles and, if both of them are not empty, remove one stone from each of them.
Alice understands that sometimes it's impossible to remove all stones with the given operation, so she allowed you to use the following superability:
Before the start of cleaning, you can select two neighboring piles and swap them.
Determine, if it is possible to remove all stones using the superability not more than once.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \leq n \leq 2 \cdot 10^5$) — the number of piles.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) — the number of stones in each pile.
It is guaranteed that the total sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print YES or NO — is it possible to remove all stones using the superability not more than once or not.
-----Examples-----
Input
5
3
1 2 1
3
1 1 2
5
2 2 2 1 3
5
2100 1900 1600 3000 1600
2
2443 2445
Output
YES
YES
YES
YES
NO
-----Note-----
In the first test case, you can remove all stones without using a superability: $[1, 2, 1] \rightarrow [1, 1, 0] \rightarrow [0, 0, 0]$.
In the second test case, you can apply superability to the second and the third piles and then act like in the first testcase.
In the third test case, you can apply superability to the fourth and the fifth piles, thus getting $a = [2, 2, 2, 3, 1]$.
In the fourth test case, you can apply superability to the first and the second piles, thus getting $a = [1900, 2100, 1600, 3000, 1600]$. | t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
s = [a[0]]
for i in range(1, n):
s.append(a[i] - s[-1])
mi0 = mi1 = 10**16
mia0 = []
mia1 = []
for i in range(n - 1, -1, -1):
v = s[i]
if i % 2 == 0:
mi0 = min(mi0, v)
else:
mi1 = min(mi1, v)
mia0.append(mi0)
mia1.append(mi1)
mia0.reverse()
mia1.reverse()
mia0.append(10**16)
mia1.append(10**16)
if min(mi0, mi1) >= 0 and s[-1] == 0:
print("YES")
else:
poss = False
for i in range(n - 1):
dv = -2 * (a[i] - a[i + 1])
if (i % 2 == 0) == (n % 2 == 0):
ne = s[-1] - dv
else:
ne = s[-1] + dv
if ne == 0:
if i % 2 == 1:
if mia0[i] - dv >= 0 and min(mia1[i + 1] + dv, s[i] + dv // 2) >= 0:
poss = True
break
elif mia1[i] - dv >= 0 and min(mia0[i + 1] + dv, s[i] + dv // 2) >= 0:
poss = True
break
if s[i] < 0:
break
print("YES" if poss else "NO") | 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 VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER IF FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING STRING |
During cleaning the coast, Alice found $n$ piles of stones. The $i$-th pile has $a_i$ stones.
Piles $i$ and $i + 1$ are neighbouring for all $1 \leq i \leq n - 1$. If pile $i$ becomes empty, piles $i - 1$ and $i + 1$ doesn't become neighbouring.
Alice is too lazy to remove these stones, so she asked you to take this duty. She allowed you to do only the following operation:
Select two neighboring piles and, if both of them are not empty, remove one stone from each of them.
Alice understands that sometimes it's impossible to remove all stones with the given operation, so she allowed you to use the following superability:
Before the start of cleaning, you can select two neighboring piles and swap them.
Determine, if it is possible to remove all stones using the superability not more than once.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \leq n \leq 2 \cdot 10^5$) — the number of piles.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) — the number of stones in each pile.
It is guaranteed that the total sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print YES or NO — is it possible to remove all stones using the superability not more than once or not.
-----Examples-----
Input
5
3
1 2 1
3
1 1 2
5
2 2 2 1 3
5
2100 1900 1600 3000 1600
2
2443 2445
Output
YES
YES
YES
YES
NO
-----Note-----
In the first test case, you can remove all stones without using a superability: $[1, 2, 1] \rightarrow [1, 1, 0] \rightarrow [0, 0, 0]$.
In the second test case, you can apply superability to the second and the third piles and then act like in the first testcase.
In the third test case, you can apply superability to the fourth and the fifth piles, thus getting $a = [2, 2, 2, 3, 1]$.
In the fourth test case, you can apply superability to the first and the second piles, thus getting $a = [1900, 2100, 1600, 3000, 1600]$. | import sys
input = sys.stdin.readline
t = int(input())
out = []
for _ in range(t):
n = int(input())
l = list(map(int, input().split()))
left = [0]
for i in range(n):
v = l[i]
if left[-1] == -1 or v < left[-1]:
left.append(-1)
else:
left.append(v - left[-1])
right = [0]
for i in range(n - 1, -1, -1):
v = l[i]
if right[-1] == -1 or v < right[-1]:
right.append(-1)
else:
right.append(v - right[-1])
right.reverse()
if left[-1] == 0:
out.append("YES")
continue
for i in range(n - 1):
four = [left[i], l[i + 1], l[i], right[i + 2]]
if (
four[0] >= 0
and four[3] >= 0
and four[1] >= four[0]
and four[1] - four[0] == four[2] - four[3]
):
out.append("YES")
break
else:
out.append("NO")
print("\n".join(out)) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST 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 NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER NUMBER VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
During cleaning the coast, Alice found $n$ piles of stones. The $i$-th pile has $a_i$ stones.
Piles $i$ and $i + 1$ are neighbouring for all $1 \leq i \leq n - 1$. If pile $i$ becomes empty, piles $i - 1$ and $i + 1$ doesn't become neighbouring.
Alice is too lazy to remove these stones, so she asked you to take this duty. She allowed you to do only the following operation:
Select two neighboring piles and, if both of them are not empty, remove one stone from each of them.
Alice understands that sometimes it's impossible to remove all stones with the given operation, so she allowed you to use the following superability:
Before the start of cleaning, you can select two neighboring piles and swap them.
Determine, if it is possible to remove all stones using the superability not more than once.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \leq n \leq 2 \cdot 10^5$) — the number of piles.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) — the number of stones in each pile.
It is guaranteed that the total sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print YES or NO — is it possible to remove all stones using the superability not more than once or not.
-----Examples-----
Input
5
3
1 2 1
3
1 1 2
5
2 2 2 1 3
5
2100 1900 1600 3000 1600
2
2443 2445
Output
YES
YES
YES
YES
NO
-----Note-----
In the first test case, you can remove all stones without using a superability: $[1, 2, 1] \rightarrow [1, 1, 0] \rightarrow [0, 0, 0]$.
In the second test case, you can apply superability to the second and the third piles and then act like in the first testcase.
In the third test case, you can apply superability to the fourth and the fifth piles, thus getting $a = [2, 2, 2, 3, 1]$.
In the fourth test case, you can apply superability to the first and the second piles, thus getting $a = [1900, 2100, 1600, 3000, 1600]$. | from sys import stdin
input = stdin.readline
def pyk(l):
res = [-1] * len(l)
res[0] = 0
for i in range(1, len(l)):
res[i] = l[i - 1]
l[i] -= l[i - 1]
if l[i] < 0:
break
return res
t = int(input())
for _ in range(t):
n = int(input())
l = [int(a) for a in input().split()]
from_left = pyk(l[:])
from_right = pyk(l[::-1])[::-1]
okok = False
for a, b, x, y in zip(l[:-1], l[1:], from_left[:-1], from_right[1:]):
if x != -1 and y != -1:
if a - x >= 0 and a - x == b - y:
okok = True
break
if b - x >= 0 and a - y == b - x:
okok = True
break
print("YES" if okok else "NO") | ASSIGN VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER RETURN VAR 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 VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR STRING STRING |
During cleaning the coast, Alice found $n$ piles of stones. The $i$-th pile has $a_i$ stones.
Piles $i$ and $i + 1$ are neighbouring for all $1 \leq i \leq n - 1$. If pile $i$ becomes empty, piles $i - 1$ and $i + 1$ doesn't become neighbouring.
Alice is too lazy to remove these stones, so she asked you to take this duty. She allowed you to do only the following operation:
Select two neighboring piles and, if both of them are not empty, remove one stone from each of them.
Alice understands that sometimes it's impossible to remove all stones with the given operation, so she allowed you to use the following superability:
Before the start of cleaning, you can select two neighboring piles and swap them.
Determine, if it is possible to remove all stones using the superability not more than once.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \leq n \leq 2 \cdot 10^5$) — the number of piles.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) — the number of stones in each pile.
It is guaranteed that the total sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print YES or NO — is it possible to remove all stones using the superability not more than once or not.
-----Examples-----
Input
5
3
1 2 1
3
1 1 2
5
2 2 2 1 3
5
2100 1900 1600 3000 1600
2
2443 2445
Output
YES
YES
YES
YES
NO
-----Note-----
In the first test case, you can remove all stones without using a superability: $[1, 2, 1] \rightarrow [1, 1, 0] \rightarrow [0, 0, 0]$.
In the second test case, you can apply superability to the second and the third piles and then act like in the first testcase.
In the third test case, you can apply superability to the fourth and the fifth piles, thus getting $a = [2, 2, 2, 3, 1]$.
In the fourth test case, you can apply superability to the first and the second piles, thus getting $a = [1900, 2100, 1600, 3000, 1600]$. | import sys
input = sys.stdin.readline
(T,) = map(int, input().split())
for _ in range(T):
(N,) = map(int, input().split())
X = list(map(int, input().split()))
l = [-1] * N
b = 0
f = 1
for i in range(N):
x = X[i]
if b > x:
f = 0
break
b = x - b
l[i] = b
if f and b == 0:
print("YES")
continue
l2 = [-1] * N
b = 0
for i in range(N - 1, -1, -1):
x = X[i]
if b > x:
break
b = x - b
l2[i] = b
f = 0
for i in range(N - 1):
c, b = X[i], X[i + 1]
if i == 0:
a = 0
else:
a = l[i - 1]
if i == N - 2:
d = 0
else:
d = l2[i + 2]
if a == -1 or d == -1:
continue
if b - a == c - d and b >= a:
print("YES")
f = 1
break
if not f:
print("NO") | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING |
During cleaning the coast, Alice found $n$ piles of stones. The $i$-th pile has $a_i$ stones.
Piles $i$ and $i + 1$ are neighbouring for all $1 \leq i \leq n - 1$. If pile $i$ becomes empty, piles $i - 1$ and $i + 1$ doesn't become neighbouring.
Alice is too lazy to remove these stones, so she asked you to take this duty. She allowed you to do only the following operation:
Select two neighboring piles and, if both of them are not empty, remove one stone from each of them.
Alice understands that sometimes it's impossible to remove all stones with the given operation, so she allowed you to use the following superability:
Before the start of cleaning, you can select two neighboring piles and swap them.
Determine, if it is possible to remove all stones using the superability not more than once.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \leq n \leq 2 \cdot 10^5$) — the number of piles.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) — the number of stones in each pile.
It is guaranteed that the total sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print YES or NO — is it possible to remove all stones using the superability not more than once or not.
-----Examples-----
Input
5
3
1 2 1
3
1 1 2
5
2 2 2 1 3
5
2100 1900 1600 3000 1600
2
2443 2445
Output
YES
YES
YES
YES
NO
-----Note-----
In the first test case, you can remove all stones without using a superability: $[1, 2, 1] \rightarrow [1, 1, 0] \rightarrow [0, 0, 0]$.
In the second test case, you can apply superability to the second and the third piles and then act like in the first testcase.
In the third test case, you can apply superability to the fourth and the fifth piles, thus getting $a = [2, 2, 2, 3, 1]$.
In the fourth test case, you can apply superability to the first and the second piles, thus getting $a = [1900, 2100, 1600, 3000, 1600]$. | for t in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
p = [-1] * (n + 1)
p[0] = 0
for i in range(n):
p[i + 1] = a[i] - p[i]
if p[i + 1] < 0:
break
if p[n] == 0:
print("YES")
continue
s = 0
for i in range(n - 1, 0, -1):
if p[i - 1] >= 0 and a[i - 1] - s >= 0 and a[i - 1] - s == a[i] - p[i - 1]:
print("YES")
break
s1 = a[i] - s
if s1 < 0:
print("NO")
break
s = s1
else:
print("NO") | 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 BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR EXPR FUNC_CALL VAR STRING |
During cleaning the coast, Alice found $n$ piles of stones. The $i$-th pile has $a_i$ stones.
Piles $i$ and $i + 1$ are neighbouring for all $1 \leq i \leq n - 1$. If pile $i$ becomes empty, piles $i - 1$ and $i + 1$ doesn't become neighbouring.
Alice is too lazy to remove these stones, so she asked you to take this duty. She allowed you to do only the following operation:
Select two neighboring piles and, if both of them are not empty, remove one stone from each of them.
Alice understands that sometimes it's impossible to remove all stones with the given operation, so she allowed you to use the following superability:
Before the start of cleaning, you can select two neighboring piles and swap them.
Determine, if it is possible to remove all stones using the superability not more than once.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \leq n \leq 2 \cdot 10^5$) — the number of piles.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) — the number of stones in each pile.
It is guaranteed that the total sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print YES or NO — is it possible to remove all stones using the superability not more than once or not.
-----Examples-----
Input
5
3
1 2 1
3
1 1 2
5
2 2 2 1 3
5
2100 1900 1600 3000 1600
2
2443 2445
Output
YES
YES
YES
YES
NO
-----Note-----
In the first test case, you can remove all stones without using a superability: $[1, 2, 1] \rightarrow [1, 1, 0] \rightarrow [0, 0, 0]$.
In the second test case, you can apply superability to the second and the third piles and then act like in the first testcase.
In the third test case, you can apply superability to the fourth and the fifth piles, thus getting $a = [2, 2, 2, 3, 1]$.
In the fourth test case, you can apply superability to the first and the second piles, thus getting $a = [1900, 2100, 1600, 3000, 1600]$. | import sys
zz = 1
sys.setrecursionlimit(10**5)
if zz:
input = sys.stdin.readline
else:
sys.stdin = open("input.txt", "r")
sys.stdout = open("all.txt", "w")
di = [[-1, 0], [1, 0], [0, 1], [0, -1]]
def fori(n):
return [fi() for i in range(n)]
def inc(d, c, x=1):
d[c] = d[c] + x if c in d else x
def ii():
return input().rstrip()
def li():
return [int(xx) for xx in input().split()]
def fli():
return [float(x) for x in input().split()]
def comp(a, b):
if a > b:
return 2
return 2 if a == b else 0
def gi():
return [xx for xx in input().split()]
def gtc(tc, ans):
print("Case #" + str(tc) + ":", ans)
def cil(n, m):
return n // m + int(n % m > 0)
def fi():
return int(input())
def pro(a):
return reduce(lambda a, b: a * b, a)
def swap(a, i, j):
a[i], a[j] = a[j], a[i]
def si():
return list(input().rstrip())
def mi():
return map(int, input().split())
def gh():
sys.stdout.flush()
def isvalid(i, j, n, m):
return 0 <= i < n and 0 <= j < m
def bo(i):
return ord(i) - ord("a")
def graph(n, m):
for i in range(m):
x, y = mi()
a[x].append(y)
a[y].append(x)
t = fi()
uu = t
while t > 0:
t -= 1
n = fi()
a = li()
pre = [[0, 1] for i in range(n + 1)]
suf = [[0, 1] for i in range(n + 1)]
c = 0
for i in range(n):
pre[i][1] = pre[i - 1][1]
if c > a[i]:
pre[i][1] = 0
pre[i][0] = c
c = a[i] - c
pre[n][0] = c
c = 0
for i in range(n - 1, -1, -1):
suf[i][1] = suf[i + 1][1]
if c > a[i]:
suf[i][1] = 0
suf[i][0] = c
c = a[i] - c
suf[n][0] = c
c = 0
if pre[n - 1][1] and suf[0][1] and pre[n][0] == 0 and suf[n][0] == 0:
print("YES")
continue
for i in range(1, n):
c = pre[i - 2][1] & suf[i + 1][1]
if (
pre[i - 1][0] > a[i]
or suf[i][0] > a[i - 1]
or pre[i - 1][0] + a[i - 1] - suf[i][0] > a[i]
or suf[i][0] + a[i] - pre[i - 1][0] > a[i - 1]
):
c = 0
if c == 1:
break
print("YES" if c else "NO") | IMPORT ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER IF VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR LIST LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR VAR RETURN NUMBER RETURN VAR VAR NUMBER NUMBER FUNC_DEF RETURN VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR BIN_OP BIN_OP STRING FUNC_CALL VAR VAR STRING VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_DEF RETURN NUMBER VAR VAR NUMBER VAR VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR STRING STRING |
During cleaning the coast, Alice found $n$ piles of stones. The $i$-th pile has $a_i$ stones.
Piles $i$ and $i + 1$ are neighbouring for all $1 \leq i \leq n - 1$. If pile $i$ becomes empty, piles $i - 1$ and $i + 1$ doesn't become neighbouring.
Alice is too lazy to remove these stones, so she asked you to take this duty. She allowed you to do only the following operation:
Select two neighboring piles and, if both of them are not empty, remove one stone from each of them.
Alice understands that sometimes it's impossible to remove all stones with the given operation, so she allowed you to use the following superability:
Before the start of cleaning, you can select two neighboring piles and swap them.
Determine, if it is possible to remove all stones using the superability not more than once.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \leq n \leq 2 \cdot 10^5$) — the number of piles.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) — the number of stones in each pile.
It is guaranteed that the total sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print YES or NO — is it possible to remove all stones using the superability not more than once or not.
-----Examples-----
Input
5
3
1 2 1
3
1 1 2
5
2 2 2 1 3
5
2100 1900 1600 3000 1600
2
2443 2445
Output
YES
YES
YES
YES
NO
-----Note-----
In the first test case, you can remove all stones without using a superability: $[1, 2, 1] \rightarrow [1, 1, 0] \rightarrow [0, 0, 0]$.
In the second test case, you can apply superability to the second and the third piles and then act like in the first testcase.
In the third test case, you can apply superability to the fourth and the fifth piles, thus getting $a = [2, 2, 2, 3, 1]$.
In the fourth test case, you can apply superability to the first and the second piles, thus getting $a = [1900, 2100, 1600, 3000, 1600]$. | t = int(input())
for _ in range(t):
n = int(input())
b = list(map(int, input().split()))
fow = [0]
back = [0]
ind1 = []
ind2 = []
for j in range(n):
fow.append(b[j] - fow[-1])
if fow[-1] < 0:
ind1.append(j)
back.append(b[n - 1 - j] - back[-1])
if back[-1] < 0:
ind2.append(n - j - 1)
fow.pop(0)
back.reverse()
back.pop()
pre = [j for j in back]
suf = [j for j in fow]
for j in range(n):
if j - 2 >= 0:
pre[j] = min(pre[j], pre[j - 2])
if n - j + 1 < n:
suf[n - j - 1] = min(suf[n - j - 1], suf[n - j + 1])
poss = 0
for j in range(n):
if j == 0:
if fow[-1] == 0 and ind1 == []:
poss = 1
break
if back[0] == 0 and ind2 == []:
poss = 1
break
else:
pr = b[j] - b[j - 1]
q = j % 2
r = (n - 1) % 2
if q == r:
res1 = fow[-1] - 2 * pr
else:
res1 = fow[-1] + 2 * pr
if (
res1 == 0
and fow[j - 1] + pr >= 0
and suf[j] >= 2 * pr
and (1 if j + 1 < n and suf[j + 1] >= -2 * pr or j + 1 >= n else 0)
):
if ind1 == []:
poss = 1
break
elif ind1[0] >= j - 1:
poss = 1
break
r = 0
if q == r:
res1 = back[0] - 2 * pr
else:
res1 = back[0] + 2 * pr
if (
res1 == 0
and pre[j - 1] >= -2 * pr
and back[j] - pr >= 0
and (1 if j - 2 >= 0 and pre[j - 2] >= 2 * pr or j - 2 < 0 else 0)
):
if ind2 == []:
poss = 1
break
elif ind2[0] < j + 1:
poss = 1
break
if poss:
print("YES")
else:
print("NO") | 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 NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR NUMBER NUMBER VAR LIST ASSIGN VAR NUMBER IF VAR NUMBER NUMBER VAR LIST ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR IF VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER NUMBER IF VAR LIST ASSIGN VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER NUMBER IF VAR LIST ASSIGN VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
During cleaning the coast, Alice found $n$ piles of stones. The $i$-th pile has $a_i$ stones.
Piles $i$ and $i + 1$ are neighbouring for all $1 \leq i \leq n - 1$. If pile $i$ becomes empty, piles $i - 1$ and $i + 1$ doesn't become neighbouring.
Alice is too lazy to remove these stones, so she asked you to take this duty. She allowed you to do only the following operation:
Select two neighboring piles and, if both of them are not empty, remove one stone from each of them.
Alice understands that sometimes it's impossible to remove all stones with the given operation, so she allowed you to use the following superability:
Before the start of cleaning, you can select two neighboring piles and swap them.
Determine, if it is possible to remove all stones using the superability not more than once.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \leq n \leq 2 \cdot 10^5$) — the number of piles.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) — the number of stones in each pile.
It is guaranteed that the total sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print YES or NO — is it possible to remove all stones using the superability not more than once or not.
-----Examples-----
Input
5
3
1 2 1
3
1 1 2
5
2 2 2 1 3
5
2100 1900 1600 3000 1600
2
2443 2445
Output
YES
YES
YES
YES
NO
-----Note-----
In the first test case, you can remove all stones without using a superability: $[1, 2, 1] \rightarrow [1, 1, 0] \rightarrow [0, 0, 0]$.
In the second test case, you can apply superability to the second and the third piles and then act like in the first testcase.
In the third test case, you can apply superability to the fourth and the fifth piles, thus getting $a = [2, 2, 2, 3, 1]$.
In the fourth test case, you can apply superability to the first and the second piles, thus getting $a = [1900, 2100, 1600, 3000, 1600]$. | import sys
input = sys.stdin.readline
b = 10**9 + 1
t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
p = [b] * n
s = [b] * n
p[0] = a[0]
curr = p[0]
for i in range(1, n):
curr = a[i] - curr
if curr < 0:
break
p[i] = curr
s[-1] = a[-1]
curr = s[-1]
for i in range(n - 2, -1, -1):
curr = a[i] - curr
if curr < 0:
break
s[i] = curr
if p[n - 1] == 0:
flag = 1
else:
flag = 0
if not flag and not n == 2:
if a[0] - s[2] == a[1]:
flag = 1
elif a[n - 1] - p[n - 3] == a[n - 2]:
flag = 1
for i in range(1, n - 2):
pi = a[i + 1] - p[i - 1]
si = a[i] - s[i + 2]
if pi >= 0 and pi == si:
flag = 1
break
if flag:
print("YES")
continue
print("NO") | IMPORT ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER 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 BIN_OP LIST VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
During cleaning the coast, Alice found $n$ piles of stones. The $i$-th pile has $a_i$ stones.
Piles $i$ and $i + 1$ are neighbouring for all $1 \leq i \leq n - 1$. If pile $i$ becomes empty, piles $i - 1$ and $i + 1$ doesn't become neighbouring.
Alice is too lazy to remove these stones, so she asked you to take this duty. She allowed you to do only the following operation:
Select two neighboring piles and, if both of them are not empty, remove one stone from each of them.
Alice understands that sometimes it's impossible to remove all stones with the given operation, so she allowed you to use the following superability:
Before the start of cleaning, you can select two neighboring piles and swap them.
Determine, if it is possible to remove all stones using the superability not more than once.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \leq n \leq 2 \cdot 10^5$) — the number of piles.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) — the number of stones in each pile.
It is guaranteed that the total sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print YES or NO — is it possible to remove all stones using the superability not more than once or not.
-----Examples-----
Input
5
3
1 2 1
3
1 1 2
5
2 2 2 1 3
5
2100 1900 1600 3000 1600
2
2443 2445
Output
YES
YES
YES
YES
NO
-----Note-----
In the first test case, you can remove all stones without using a superability: $[1, 2, 1] \rightarrow [1, 1, 0] \rightarrow [0, 0, 0]$.
In the second test case, you can apply superability to the second and the third piles and then act like in the first testcase.
In the third test case, you can apply superability to the fourth and the fifth piles, thus getting $a = [2, 2, 2, 3, 1]$.
In the fourth test case, you can apply superability to the first and the second piles, thus getting $a = [1900, 2100, 1600, 3000, 1600]$. | def ke(x: list):
y = x.copy()
for o in range(0, len(y) - 1):
if y[o] > y[o + 1]:
return False
else:
y[o + 1] -= y[o]
y[o] = 0
if y[len(y) - 1] == 0:
return True
return False
t = int(input())
for cas in range(0, t):
n = int(input())
v = list(map(int, input().split()))
if ke(v):
print("YES")
continue
if n == 1:
print("NO")
continue
z = v.copy()
z[0], z[1] = z[1], z[0]
if ke(z):
print("YES")
continue
z = v.copy()
z[n - 2], z[n - 1] = z[n - 1], z[n - 2]
if ke(z):
print("YES")
continue
zuo = [0] * n
you = [0] * n
z = v.copy()
zuo[0] = v[0]
for i in range(1, n):
if zuo[i - 1] == -1 or z[i - 1] > z[i]:
zuo[i] = -1
else:
z[i] -= z[i - 1]
z[i - 1] = 0
zuo[i] = z[i]
z = v.copy()
you[n - 1] = v[n - 1]
for i in range(n - 2, -1, -1):
if you[i + 1] == -1 or z[i + 1] > z[i]:
you[i] = -1
else:
z[i] -= z[i + 1]
z[i + 1] = 0
you[i] = z[i]
z = v.copy()
xing = False
for i in range(1, n - 2):
if (
zuo[i - 1] != -1
and you[i + 2] != -1
and ke([zuo[i - 1], z[i + 1], z[i], you[i + 2]])
):
xing = True
break
if xing:
print("YES")
else:
print("NO") | FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
During cleaning the coast, Alice found $n$ piles of stones. The $i$-th pile has $a_i$ stones.
Piles $i$ and $i + 1$ are neighbouring for all $1 \leq i \leq n - 1$. If pile $i$ becomes empty, piles $i - 1$ and $i + 1$ doesn't become neighbouring.
Alice is too lazy to remove these stones, so she asked you to take this duty. She allowed you to do only the following operation:
Select two neighboring piles and, if both of them are not empty, remove one stone from each of them.
Alice understands that sometimes it's impossible to remove all stones with the given operation, so she allowed you to use the following superability:
Before the start of cleaning, you can select two neighboring piles and swap them.
Determine, if it is possible to remove all stones using the superability not more than once.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \leq n \leq 2 \cdot 10^5$) — the number of piles.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) — the number of stones in each pile.
It is guaranteed that the total sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print YES or NO — is it possible to remove all stones using the superability not more than once or not.
-----Examples-----
Input
5
3
1 2 1
3
1 1 2
5
2 2 2 1 3
5
2100 1900 1600 3000 1600
2
2443 2445
Output
YES
YES
YES
YES
NO
-----Note-----
In the first test case, you can remove all stones without using a superability: $[1, 2, 1] \rightarrow [1, 1, 0] \rightarrow [0, 0, 0]$.
In the second test case, you can apply superability to the second and the third piles and then act like in the first testcase.
In the third test case, you can apply superability to the fourth and the fifth piles, thus getting $a = [2, 2, 2, 3, 1]$.
In the fourth test case, you can apply superability to the first and the second piles, thus getting $a = [1900, 2100, 1600, 3000, 1600]$. | def solve2(X):
return X[0] == X[1]
def s3h(a, b, c):
return solveN_nosuper([a, b, c])
def solve3(X):
a, b, c = X
return s3h(a, b, c) or s3h(b, a, c) or s3h(a, c, b)
def s4h(a, b, c, d):
if min(a, b, c, d) < 0:
return False
return solveN_nosuper([a, b, c, d])
def solve4(X):
a, b, c, d = X
return s4h(a, b, c, d) or s4h(b, a, c, d) or s4h(a, c, b, d) or s4h(a, b, d, c)
def solveN_nosuper(X):
X = X.copy()
for i in range(len(X) - 1):
if X[i] > X[i + 1]:
return False
X[i + 1] -= X[i]
X[i] = 0
return X[-1] == 0
def fix(X):
X = X.copy()
ret = [X[0]]
for i in range(1, len(X)):
if ret[i - 1] == -1 or X[i - 1] > X[i]:
ret.append(-1)
else:
X[i] -= X[i - 1]
X[i - 1] = 0
ret.append(X[i])
return ret
def solveN(X):
if solveN_nosuper(X):
return True
L = [0] + fix(X) + [0]
R = [0] + fix(X[::-1])[::-1] + [0]
X = [0] + X + [0]
for i in range(1, len(X) - 2):
if s4h(L[i - 1], X[i + 1], X[i], R[i + 2]):
return True
return False
for _ in range(int(input())):
N = int(input())
A = list(map(int, input().split()))
if N == 2:
ans = solve2(A)
elif N == 3:
ans = solve3(A)
elif N == 4:
ans = solve4(A)
else:
ans = solveN(A)
print("YES" if ans else "NO") | FUNC_DEF RETURN VAR NUMBER VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR LIST VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR VAR VAR VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR LIST VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER RETURN VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF IF FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP LIST NUMBER FUNC_CALL VAR VAR LIST NUMBER ASSIGN VAR BIN_OP BIN_OP LIST NUMBER FUNC_CALL VAR VAR NUMBER NUMBER LIST NUMBER ASSIGN VAR BIN_OP BIN_OP LIST NUMBER VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER 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 IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR STRING STRING |
During cleaning the coast, Alice found $n$ piles of stones. The $i$-th pile has $a_i$ stones.
Piles $i$ and $i + 1$ are neighbouring for all $1 \leq i \leq n - 1$. If pile $i$ becomes empty, piles $i - 1$ and $i + 1$ doesn't become neighbouring.
Alice is too lazy to remove these stones, so she asked you to take this duty. She allowed you to do only the following operation:
Select two neighboring piles and, if both of them are not empty, remove one stone from each of them.
Alice understands that sometimes it's impossible to remove all stones with the given operation, so she allowed you to use the following superability:
Before the start of cleaning, you can select two neighboring piles and swap them.
Determine, if it is possible to remove all stones using the superability not more than once.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \leq n \leq 2 \cdot 10^5$) — the number of piles.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) — the number of stones in each pile.
It is guaranteed that the total sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print YES or NO — is it possible to remove all stones using the superability not more than once or not.
-----Examples-----
Input
5
3
1 2 1
3
1 1 2
5
2 2 2 1 3
5
2100 1900 1600 3000 1600
2
2443 2445
Output
YES
YES
YES
YES
NO
-----Note-----
In the first test case, you can remove all stones without using a superability: $[1, 2, 1] \rightarrow [1, 1, 0] \rightarrow [0, 0, 0]$.
In the second test case, you can apply superability to the second and the third piles and then act like in the first testcase.
In the third test case, you can apply superability to the fourth and the fifth piles, thus getting $a = [2, 2, 2, 3, 1]$.
In the fourth test case, you can apply superability to the first and the second piles, thus getting $a = [1900, 2100, 1600, 3000, 1600]$. | from sys import gettrace, stderr, stdin
if gettrace():
def inputi():
return input()
else:
def input():
return next(stdin)[:-1]
def inputi():
return stdin.buffer.readline()
def solve():
n = int(input())
aa = [int(a) for a in input().split()]
asum = 0
asums = []
fneg = n
for i, a in enumerate(aa):
asum = a - asum
if asum < 0 and fneg == n:
fneg = i
asums.append(asum)
if asum == 0 and fneg == n:
print("YES")
return
mins = [0] * (n - 2) + [asums[-2], asums[-1]]
for i in range(-3, -n - 1, -1):
mins[i] = min(asums[i], mins[i + 2])
lim = min(fneg + 1, n - 1)
for i in range(lim):
sign = (-1) ** (n - 1 - i)
cval = aa[i] - aa[i + 1]
if (
2 * cval * sign == asum
and asums[i] - cval >= 0
and mins[i + 1] + 2 * cval >= 0
and (i == n - 2 or mins[i + 2] - 2 * cval >= 0)
):
print("YES")
return
print("NO")
def main():
t = int(input())
for _ in range(t):
solve()
main() | IF FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR BIN_OP BIN_OP LIST NUMBER BIN_OP VAR NUMBER LIST VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP NUMBER VAR VAR VAR BIN_OP VAR VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN EXPR FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR |
During cleaning the coast, Alice found $n$ piles of stones. The $i$-th pile has $a_i$ stones.
Piles $i$ and $i + 1$ are neighbouring for all $1 \leq i \leq n - 1$. If pile $i$ becomes empty, piles $i - 1$ and $i + 1$ doesn't become neighbouring.
Alice is too lazy to remove these stones, so she asked you to take this duty. She allowed you to do only the following operation:
Select two neighboring piles and, if both of them are not empty, remove one stone from each of them.
Alice understands that sometimes it's impossible to remove all stones with the given operation, so she allowed you to use the following superability:
Before the start of cleaning, you can select two neighboring piles and swap them.
Determine, if it is possible to remove all stones using the superability not more than once.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \leq n \leq 2 \cdot 10^5$) — the number of piles.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) — the number of stones in each pile.
It is guaranteed that the total sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print YES or NO — is it possible to remove all stones using the superability not more than once or not.
-----Examples-----
Input
5
3
1 2 1
3
1 1 2
5
2 2 2 1 3
5
2100 1900 1600 3000 1600
2
2443 2445
Output
YES
YES
YES
YES
NO
-----Note-----
In the first test case, you can remove all stones without using a superability: $[1, 2, 1] \rightarrow [1, 1, 0] \rightarrow [0, 0, 0]$.
In the second test case, you can apply superability to the second and the third piles and then act like in the first testcase.
In the third test case, you can apply superability to the fourth and the fifth piles, thus getting $a = [2, 2, 2, 3, 1]$.
In the fourth test case, you can apply superability to the first and the second piles, thus getting $a = [1900, 2100, 1600, 3000, 1600]$. | import sys
input = sys.stdin.readline
for _ in range(int(input())):
n = int(input())
A = list(map(int, input().split()))
def check(A):
B = A.copy()
for i in range(len(A) - 1):
if B[i] > B[i + 1]:
return False
B[i + 1] -= B[i]
B[i] = 0
return B[-1] == 0
if check(A):
print("YES")
continue
A[0], A[1] = A[1], A[0]
if check(A):
print("YES")
continue
A[1], A[0] = A[0], A[1]
A[-2], A[-1] = A[-1], A[-2]
if check(A):
print("YES")
continue
A[-1], A[-2] = A[-2], A[-1]
pre, suf = [0] * n, [0] * n
B = A.copy()
pre[0] = B[0]
for i in range(1, n):
if pre[i - 1] == -1 or B[i - 1] > B[i]:
pre[i] = -1
else:
B[i] -= B[i - 1]
B[i - 1] = 0
pre[i] = B[i]
B = A.copy()
suf[-1] = B[-1]
for i in range(n - 2, -1, -1):
if suf[i + 1] == -1 or B[i + 1] > B[i]:
suf[i] = -1
else:
B[i] -= B[i + 1]
B[i + 1] = 0
suf[i] = B[i]
for i in range(1, n - 2):
cur = [pre[i - 1], A[i + 1], A[i], suf[i + 2]]
if pre[i - 1] == -1 or suf[i + 2] == -1:
continue
if check(cur):
print("YES")
break
else:
print("NO") | IMPORT ASSIGN 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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER RETURN VAR NUMBER NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
During cleaning the coast, Alice found $n$ piles of stones. The $i$-th pile has $a_i$ stones.
Piles $i$ and $i + 1$ are neighbouring for all $1 \leq i \leq n - 1$. If pile $i$ becomes empty, piles $i - 1$ and $i + 1$ doesn't become neighbouring.
Alice is too lazy to remove these stones, so she asked you to take this duty. She allowed you to do only the following operation:
Select two neighboring piles and, if both of them are not empty, remove one stone from each of them.
Alice understands that sometimes it's impossible to remove all stones with the given operation, so she allowed you to use the following superability:
Before the start of cleaning, you can select two neighboring piles and swap them.
Determine, if it is possible to remove all stones using the superability not more than once.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \leq n \leq 2 \cdot 10^5$) — the number of piles.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) — the number of stones in each pile.
It is guaranteed that the total sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print YES or NO — is it possible to remove all stones using the superability not more than once or not.
-----Examples-----
Input
5
3
1 2 1
3
1 1 2
5
2 2 2 1 3
5
2100 1900 1600 3000 1600
2
2443 2445
Output
YES
YES
YES
YES
NO
-----Note-----
In the first test case, you can remove all stones without using a superability: $[1, 2, 1] \rightarrow [1, 1, 0] \rightarrow [0, 0, 0]$.
In the second test case, you can apply superability to the second and the third piles and then act like in the first testcase.
In the third test case, you can apply superability to the fourth and the fifth piles, thus getting $a = [2, 2, 2, 3, 1]$.
In the fourth test case, you can apply superability to the first and the second piles, thus getting $a = [1900, 2100, 1600, 3000, 1600]$. | def solve(n, arr):
if n == 1:
return "YES"
if n == 2:
if arr[0] == arr[1]:
return "YES"
else:
return "NO"
inv = arr[::-1]
x1 = [arr[0]]
x2 = [arr[n - 1]]
for i in range(1, n - 1):
x1.append(arr[i] - x1[-1])
for i in range(n - 2, 0, -1):
x2.append(arr[i] - x2[-1])
x2 = x2[::-1]
gt1 = [(False) for i in range(n - 1)]
gt2 = [(False) for i in range(n - 1)]
for i in range(n - 1):
if x1[i] >= 0:
gt1[i] = True
else:
break
for i in range(n - 2, -1, -1):
if x2[i] >= 0:
gt2[i] = True
else:
break
d = arr[n - 1] - x1[n - 2]
if d == 0 and gt2[0]:
return "YES"
for i in range(n - 1):
cur = x1[i] - x2[i] + 2 * (arr[i + 1] - arr[i])
if (
cur == 0
and (i == 0 or gt1[i - 1])
and (i == n - 2 or gt2[i + 1])
and x1[i] + (arr[i + 1] - arr[i]) >= 0
):
return "YES"
return "NO"
t = int(input())
for i in range(t):
n = int(input())
arr = list(map(int, input().split()))
print(solve(n, arr)) | FUNC_DEF IF VAR NUMBER RETURN STRING IF VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN STRING RETURN STRING ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER ASSIGN VAR LIST VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER RETURN STRING RETURN STRING 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
During cleaning the coast, Alice found $n$ piles of stones. The $i$-th pile has $a_i$ stones.
Piles $i$ and $i + 1$ are neighbouring for all $1 \leq i \leq n - 1$. If pile $i$ becomes empty, piles $i - 1$ and $i + 1$ doesn't become neighbouring.
Alice is too lazy to remove these stones, so she asked you to take this duty. She allowed you to do only the following operation:
Select two neighboring piles and, if both of them are not empty, remove one stone from each of them.
Alice understands that sometimes it's impossible to remove all stones with the given operation, so she allowed you to use the following superability:
Before the start of cleaning, you can select two neighboring piles and swap them.
Determine, if it is possible to remove all stones using the superability not more than once.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \leq n \leq 2 \cdot 10^5$) — the number of piles.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) — the number of stones in each pile.
It is guaranteed that the total sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print YES or NO — is it possible to remove all stones using the superability not more than once or not.
-----Examples-----
Input
5
3
1 2 1
3
1 1 2
5
2 2 2 1 3
5
2100 1900 1600 3000 1600
2
2443 2445
Output
YES
YES
YES
YES
NO
-----Note-----
In the first test case, you can remove all stones without using a superability: $[1, 2, 1] \rightarrow [1, 1, 0] \rightarrow [0, 0, 0]$.
In the second test case, you can apply superability to the second and the third piles and then act like in the first testcase.
In the third test case, you can apply superability to the fourth and the fifth piles, thus getting $a = [2, 2, 2, 3, 1]$.
In the fourth test case, you can apply superability to the first and the second piles, thus getting $a = [1900, 2100, 1600, 3000, 1600]$. | import sys
sys.setrecursionlimit(10**5)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II():
return int(sys.stdin.buffer.readline())
def MI():
return map(int, sys.stdin.buffer.readline().split())
def LI():
return list(map(int, sys.stdin.buffer.readline().split()))
def LLI(rows_number):
return [LI() for _ in range(rows_number)]
def BI():
return sys.stdin.buffer.readline().rstrip()
def SI():
return sys.stdin.buffer.readline().rstrip().decode()
inf = 10**16
md = 10**9 + 7
def toll(aa):
res = [aa[0]]
for a in aa[1:]:
res.append(a - res[-1])
return res
for _ in range(II()):
n = II()
aa = LI()
ll = toll(aa)
if ll[-1] == 0 and min(ll) >= 0:
print("YES")
continue
rr = toll(aa[::-1])[::-1]
e = n - 1
for i, l in enumerate(ll[:-1]):
if l < 0:
e = i + 1
break
b = 0
for i in range(n - 1, 0, -1):
if rr[i] < 0:
b = i - 1
break
def ok(i):
pre = aa[1] if i == 0 else aa[i + 1] - ll[i - 1]
suf = aa[n - 2] if i == n - 2 else aa[i] - rr[i + 2]
return pre == suf and pre >= 0
for i in range(b, e):
if ok(i):
print("YES")
break
else:
print("NO") | IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR LIST VAR NUMBER FOR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
During cleaning the coast, Alice found $n$ piles of stones. The $i$-th pile has $a_i$ stones.
Piles $i$ and $i + 1$ are neighbouring for all $1 \leq i \leq n - 1$. If pile $i$ becomes empty, piles $i - 1$ and $i + 1$ doesn't become neighbouring.
Alice is too lazy to remove these stones, so she asked you to take this duty. She allowed you to do only the following operation:
Select two neighboring piles and, if both of them are not empty, remove one stone from each of them.
Alice understands that sometimes it's impossible to remove all stones with the given operation, so she allowed you to use the following superability:
Before the start of cleaning, you can select two neighboring piles and swap them.
Determine, if it is possible to remove all stones using the superability not more than once.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \leq n \leq 2 \cdot 10^5$) — the number of piles.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) — the number of stones in each pile.
It is guaranteed that the total sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print YES or NO — is it possible to remove all stones using the superability not more than once or not.
-----Examples-----
Input
5
3
1 2 1
3
1 1 2
5
2 2 2 1 3
5
2100 1900 1600 3000 1600
2
2443 2445
Output
YES
YES
YES
YES
NO
-----Note-----
In the first test case, you can remove all stones without using a superability: $[1, 2, 1] \rightarrow [1, 1, 0] \rightarrow [0, 0, 0]$.
In the second test case, you can apply superability to the second and the third piles and then act like in the first testcase.
In the third test case, you can apply superability to the fourth and the fifth piles, thus getting $a = [2, 2, 2, 3, 1]$.
In the fourth test case, you can apply superability to the first and the second piles, thus getting $a = [1900, 2100, 1600, 3000, 1600]$. | from sys import stdin, stdout
input = stdin.readline
t = int(input())
for _ in range(t):
n = int(input())
a = [int(x) for x in input().split()]
s = 0
vals = []
b = 1
for i in a[::-1]:
p = i - s
if p < 0:
b = 0
s = p
vals.append(p)
if vals[-1] == 0:
if b:
print("YES")
else:
print("NO")
continue
vals = vals[::-1]
p = -1
for i in range(n):
if vals[i] < 0:
p = i
diff = {}
b = 0
for i in range(n - 1):
val = a[i] - a[i + 1]
if b % 2 == 0:
val *= -1
val *= 2
b ^= 1
if val not in diff:
diff[val] = [i + 1]
continue
if diff[val][-1] < p:
diff[val] = [i + 1]
elif diff[val][-1] == p:
diff[val].append(i + 1)
else:
continue
delta = -vals[0]
if delta not in diff:
print("NO")
continue
p1 = diff[delta][0]
for i in range(n):
if i > p1:
delta = 0
if i == p1:
delta //= 2
if vals[i] + delta < 0:
break
delta *= -1
else:
print("YES")
continue
delta = -vals[0]
if len(diff[delta]) == 1:
print("NO")
continue
p1 = diff[delta][1]
for i in range(n):
if i > p1:
delta = 0
if i == p1:
delta //= 2
if vals[i] + delta < 0:
break
delta *= -1
else:
print("YES")
continue
print("NO") | ASSIGN VAR VAR 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 VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR LIST BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR VAR LIST BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
During cleaning the coast, Alice found $n$ piles of stones. The $i$-th pile has $a_i$ stones.
Piles $i$ and $i + 1$ are neighbouring for all $1 \leq i \leq n - 1$. If pile $i$ becomes empty, piles $i - 1$ and $i + 1$ doesn't become neighbouring.
Alice is too lazy to remove these stones, so she asked you to take this duty. She allowed you to do only the following operation:
Select two neighboring piles and, if both of them are not empty, remove one stone from each of them.
Alice understands that sometimes it's impossible to remove all stones with the given operation, so she allowed you to use the following superability:
Before the start of cleaning, you can select two neighboring piles and swap them.
Determine, if it is possible to remove all stones using the superability not more than once.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \leq n \leq 2 \cdot 10^5$) — the number of piles.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) — the number of stones in each pile.
It is guaranteed that the total sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print YES or NO — is it possible to remove all stones using the superability not more than once or not.
-----Examples-----
Input
5
3
1 2 1
3
1 1 2
5
2 2 2 1 3
5
2100 1900 1600 3000 1600
2
2443 2445
Output
YES
YES
YES
YES
NO
-----Note-----
In the first test case, you can remove all stones without using a superability: $[1, 2, 1] \rightarrow [1, 1, 0] \rightarrow [0, 0, 0]$.
In the second test case, you can apply superability to the second and the third piles and then act like in the first testcase.
In the third test case, you can apply superability to the fourth and the fifth piles, thus getting $a = [2, 2, 2, 3, 1]$.
In the fourth test case, you can apply superability to the first and the second piles, thus getting $a = [1900, 2100, 1600, 3000, 1600]$. | import sys
def minp():
return sys.stdin.readline().strip()
def mint():
return int(minp())
def mints():
return map(int, minp().split())
def solve():
for t in range(mint()):
n = mint()
a = list(mints())
p = [-1] * (n + 1)
p[0] = 0
s = 0
for i in range(n):
p[i + 1] = a[i] - p[i]
if p[i + 1] < 0:
break
if p[n] == 0:
print("YES")
continue
for i in range(n - 1, 0, -1):
if p[i - 1] >= 0 and a[i - 1] - s >= 0 and a[i - 1] - s == a[i] - p[i - 1]:
print("YES")
break
s1 = a[i] - s
if s1 < 0:
print("NO")
break
s = s1
else:
print("NO")
solve() | IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR |
During cleaning the coast, Alice found $n$ piles of stones. The $i$-th pile has $a_i$ stones.
Piles $i$ and $i + 1$ are neighbouring for all $1 \leq i \leq n - 1$. If pile $i$ becomes empty, piles $i - 1$ and $i + 1$ doesn't become neighbouring.
Alice is too lazy to remove these stones, so she asked you to take this duty. She allowed you to do only the following operation:
Select two neighboring piles and, if both of them are not empty, remove one stone from each of them.
Alice understands that sometimes it's impossible to remove all stones with the given operation, so she allowed you to use the following superability:
Before the start of cleaning, you can select two neighboring piles and swap them.
Determine, if it is possible to remove all stones using the superability not more than once.
-----Input-----
The first line contains a single integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases.
The first line of each test case contains the single integer $n$ ($2 \leq n \leq 2 \cdot 10^5$) — the number of piles.
The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$) — the number of stones in each pile.
It is guaranteed that the total sum of $n$ over all test cases doesn't exceed $2 \cdot 10^5$.
-----Output-----
For each test case, print YES or NO — is it possible to remove all stones using the superability not more than once or not.
-----Examples-----
Input
5
3
1 2 1
3
1 1 2
5
2 2 2 1 3
5
2100 1900 1600 3000 1600
2
2443 2445
Output
YES
YES
YES
YES
NO
-----Note-----
In the first test case, you can remove all stones without using a superability: $[1, 2, 1] \rightarrow [1, 1, 0] \rightarrow [0, 0, 0]$.
In the second test case, you can apply superability to the second and the third piles and then act like in the first testcase.
In the third test case, you can apply superability to the fourth and the fifth piles, thus getting $a = [2, 2, 2, 3, 1]$.
In the fourth test case, you can apply superability to the first and the second piles, thus getting $a = [1900, 2100, 1600, 3000, 1600]$. | for _ in range(int(input())):
n = int(input())
a = [int(x) for x in input().split()]
suok = [True]
sulft = [0]
for i in range(n - 1, -1, -1):
if not suok[-1] or a[i] < sulft[-1]:
suok.append(False)
sulft.append(0)
else:
suok.append(True)
sulft.append(a[i] - sulft[-1])
a.append(0)
suok = suok[::-1]
sulft = sulft[::-1]
if suok[0] and sulft[0] == 0:
print("YES")
continue
prlft = 0
win = False
for i in range(n - 1):
a[i], a[i + 1] = a[i + 1], a[i]
if (
suok[i + 2]
and a[i] >= prlft
and a[i + 1] >= a[i] - prlft
and a[i + 1] - (a[i] - prlft) == sulft[i + 2]
):
win = True
break
a[i], a[i + 1] = a[i + 1], a[i]
if a[i] < prlft:
break
prlft = a[i] - prlft
print("YES" if win else "NO") | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING STRING |
You have a multiset containing several integers. Initially, it contains $a_1$ elements equal to $1$, $a_2$ elements equal to $2$, ..., $a_n$ elements equal to $n$.
You may apply two types of operations: choose two integers $l$ and $r$ ($l \le r$), then remove one occurrence of $l$, one occurrence of $l + 1$, ..., one occurrence of $r$ from the multiset. This operation can be applied only if each number from $l$ to $r$ occurs at least once in the multiset; choose two integers $i$ and $x$ ($x \ge 1$), then remove $x$ occurrences of $i$ from the multiset. This operation can be applied only if the multiset contains at least $x$ occurrences of $i$.
What is the minimum number of operations required to delete all elements from the multiset?
-----Input-----
The first line contains one integer $n$ ($1 \le n \le 5000$).
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 10^9$).
-----Output-----
Print one integer — the minimum number of operations required to delete all elements from the multiset.
-----Examples-----
Input
4
1 4 1 1
Output
2
Input
5
1 0 1 0 1
Output
3 | import sys
def solve(l, r):
if l >= r:
return 0
x = a.index(min(a[l:r]), l, r)
mn = min(a[l:r])
i, j = 0, 0
if l - 1 >= 0:
i = a[l - 1]
if r < n:
j = a[r]
out = max(i, j)
return min(r - l, mn + solve(l, x) + solve(x + 1, r) - out)
sys.setrecursionlimit(10000)
n = int(input())
a = list(map(int, input().split()))
print(solve(0, n)) | IMPORT FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR |
You have a multiset containing several integers. Initially, it contains $a_1$ elements equal to $1$, $a_2$ elements equal to $2$, ..., $a_n$ elements equal to $n$.
You may apply two types of operations: choose two integers $l$ and $r$ ($l \le r$), then remove one occurrence of $l$, one occurrence of $l + 1$, ..., one occurrence of $r$ from the multiset. This operation can be applied only if each number from $l$ to $r$ occurs at least once in the multiset; choose two integers $i$ and $x$ ($x \ge 1$), then remove $x$ occurrences of $i$ from the multiset. This operation can be applied only if the multiset contains at least $x$ occurrences of $i$.
What is the minimum number of operations required to delete all elements from the multiset?
-----Input-----
The first line contains one integer $n$ ($1 \le n \le 5000$).
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 10^9$).
-----Output-----
Print one integer — the minimum number of operations required to delete all elements from the multiset.
-----Examples-----
Input
4
1 4 1 1
Output
2
Input
5
1 0 1 0 1
Output
3 | import sys
input = sys.stdin.readline
sys.setrecursionlimit(100000)
def rec(l, r, sub):
if l >= r:
return 0
m = a.index(min(a[l:r]), l, r)
return min(r - l, a[m] - sub + rec(l, m, a[m]) + rec(m + 1, r, a[m]))
n = int(input())
a = list(map(int, input().split()))
print(rec(0, n, 0)) | IMPORT ASSIGN VAR VAR EXPR FUNC_CALL VAR NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR NUMBER |
You have a multiset containing several integers. Initially, it contains $a_1$ elements equal to $1$, $a_2$ elements equal to $2$, ..., $a_n$ elements equal to $n$.
You may apply two types of operations: choose two integers $l$ and $r$ ($l \le r$), then remove one occurrence of $l$, one occurrence of $l + 1$, ..., one occurrence of $r$ from the multiset. This operation can be applied only if each number from $l$ to $r$ occurs at least once in the multiset; choose two integers $i$ and $x$ ($x \ge 1$), then remove $x$ occurrences of $i$ from the multiset. This operation can be applied only if the multiset contains at least $x$ occurrences of $i$.
What is the minimum number of operations required to delete all elements from the multiset?
-----Input-----
The first line contains one integer $n$ ($1 \le n \le 5000$).
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 10^9$).
-----Output-----
Print one integer — the minimum number of operations required to delete all elements from the multiset.
-----Examples-----
Input
4
1 4 1 1
Output
2
Input
5
1 0 1 0 1
Output
3 | import sys
sys.setrecursionlimit(10000)
n = int(input())
a = list(map(int, input().split()))
def solve(i, j, x):
if i > j:
return 0
idx = a.index(min(a[i : j + 1]), i, j + 1)
mn = a[idx]
case = mn - x + solve(i, idx - 1, mn) + solve(idx + 1, j, mn)
return min(case, j - i + 1)
print(solve(0, n - 1, 0)) | IMPORT EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER |
You have a multiset containing several integers. Initially, it contains $a_1$ elements equal to $1$, $a_2$ elements equal to $2$, ..., $a_n$ elements equal to $n$.
You may apply two types of operations: choose two integers $l$ and $r$ ($l \le r$), then remove one occurrence of $l$, one occurrence of $l + 1$, ..., one occurrence of $r$ from the multiset. This operation can be applied only if each number from $l$ to $r$ occurs at least once in the multiset; choose two integers $i$ and $x$ ($x \ge 1$), then remove $x$ occurrences of $i$ from the multiset. This operation can be applied only if the multiset contains at least $x$ occurrences of $i$.
What is the minimum number of operations required to delete all elements from the multiset?
-----Input-----
The first line contains one integer $n$ ($1 \le n \le 5000$).
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 10^9$).
-----Output-----
Print one integer — the minimum number of operations required to delete all elements from the multiset.
-----Examples-----
Input
4
1 4 1 1
Output
2
Input
5
1 0 1 0 1
Output
3 | import sys
n = int(input())
a = list(map(int, input().split()))
sys.setrecursionlimit(10**5)
def calc(l, r, base):
if l > r:
return 0
mn = min(a[l : r + 1])
ans = mn - base
L = l
for i in range(l, r + 1):
if a[i] == mn:
ans += calc(L, i - 1, mn)
L = i + 1
ans += calc(L, r, mn)
return min(ans, r - l + 1)
print(calc(0, n - 1, 0)) | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER |
You have a multiset containing several integers. Initially, it contains $a_1$ elements equal to $1$, $a_2$ elements equal to $2$, ..., $a_n$ elements equal to $n$.
You may apply two types of operations: choose two integers $l$ and $r$ ($l \le r$), then remove one occurrence of $l$, one occurrence of $l + 1$, ..., one occurrence of $r$ from the multiset. This operation can be applied only if each number from $l$ to $r$ occurs at least once in the multiset; choose two integers $i$ and $x$ ($x \ge 1$), then remove $x$ occurrences of $i$ from the multiset. This operation can be applied only if the multiset contains at least $x$ occurrences of $i$.
What is the minimum number of operations required to delete all elements from the multiset?
-----Input-----
The first line contains one integer $n$ ($1 \le n \le 5000$).
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 10^9$).
-----Output-----
Print one integer — the minimum number of operations required to delete all elements from the multiset.
-----Examples-----
Input
4
1 4 1 1
Output
2
Input
5
1 0 1 0 1
Output
3 | import sys
input = sys.stdin.readline
INF = 10**5
n = int(input())
a_raw = [int(item) for item in input().split()]
a = []
cnt = 0
for item in a_raw:
if item >= n:
cnt += 1
else:
a.append(item)
nn = len(a)
dp = [([INF] * n) for _ in range(nn + 1)]
dp[0][0] = cnt
for i, item in enumerate(a):
for j in range(n):
if dp[i][j] == INF:
continue
if item > j:
dp[i + 1][j] = min(dp[i + 1][j], dp[i][j] + 1)
dp[i + 1][item] = min(dp[i + 1][item], dp[i][j] + max(0, item - j))
print(min(dp[-1])) | IMPORT ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER |
You have a multiset containing several integers. Initially, it contains $a_1$ elements equal to $1$, $a_2$ elements equal to $2$, ..., $a_n$ elements equal to $n$.
You may apply two types of operations: choose two integers $l$ and $r$ ($l \le r$), then remove one occurrence of $l$, one occurrence of $l + 1$, ..., one occurrence of $r$ from the multiset. This operation can be applied only if each number from $l$ to $r$ occurs at least once in the multiset; choose two integers $i$ and $x$ ($x \ge 1$), then remove $x$ occurrences of $i$ from the multiset. This operation can be applied only if the multiset contains at least $x$ occurrences of $i$.
What is the minimum number of operations required to delete all elements from the multiset?
-----Input-----
The first line contains one integer $n$ ($1 \le n \le 5000$).
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 10^9$).
-----Output-----
Print one integer — the minimum number of operations required to delete all elements from the multiset.
-----Examples-----
Input
4
1 4 1 1
Output
2
Input
5
1 0 1 0 1
Output
3 | import sys
input = sys.stdin.readline
sys.setrecursionlimit(5010)
n = int(input())
A = list(map(int, input().split()))
DICT = dict()
def dfs(x, y):
if x == y:
return []
MIN = min(A[x:y])
for i in range(x, y):
A[i] -= MIN
ANS = []
NOW = x
for i in range(x, y):
if A[i] == 0:
if NOW != i:
ANS.append((NOW, i))
NOW = i + 1
if NOW != y:
ANS.append((NOW, y))
DICT[x, y] = ANS, MIN
return ANS
Q = [(0, n)]
TOP = []
while Q:
x, y = Q.pop()
TOP.append((x, y))
Q += dfs(x, y)
ANSDICT = dict()
for x, y in TOP[::-1]:
A = y - x
B = DICT[x, y][1]
for z, w in DICT[x, y][0]:
B += ANSDICT[z, w]
ANSDICT[x, y] = min(A, B)
print(ANSDICT[0, n]) | IMPORT ASSIGN VAR VAR EXPR FUNC_CALL VAR NUMBER 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 FUNC_DEF IF VAR VAR RETURN LIST ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR ASSIGN VAR LIST NUMBER VAR ASSIGN VAR LIST WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR NUMBER FOR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR |
You have a multiset containing several integers. Initially, it contains $a_1$ elements equal to $1$, $a_2$ elements equal to $2$, ..., $a_n$ elements equal to $n$.
You may apply two types of operations: choose two integers $l$ and $r$ ($l \le r$), then remove one occurrence of $l$, one occurrence of $l + 1$, ..., one occurrence of $r$ from the multiset. This operation can be applied only if each number from $l$ to $r$ occurs at least once in the multiset; choose two integers $i$ and $x$ ($x \ge 1$), then remove $x$ occurrences of $i$ from the multiset. This operation can be applied only if the multiset contains at least $x$ occurrences of $i$.
What is the minimum number of operations required to delete all elements from the multiset?
-----Input-----
The first line contains one integer $n$ ($1 \le n \le 5000$).
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 10^9$).
-----Output-----
Print one integer — the minimum number of operations required to delete all elements from the multiset.
-----Examples-----
Input
4
1 4 1 1
Output
2
Input
5
1 0 1 0 1
Output
3 | import sys
def minp():
return sys.stdin.readline().strip()
def mint():
return int(minp())
def mints():
return map(int, minp().split())
def check(s, n, v):
for i in range(n):
if s[i : i + n].count(v) == 0:
return False
return True
def solve():
n = mint()
a = list(mints())
a.append(0)
dp = [int(1e18)] * (n + 2)
dp[0] = 0
for i in range(1, n + 2):
d = int(1e18)
s = 0
z = a[i - 1]
for j in range(i - 2, -2, -1):
if a[j] < z:
z = a[j]
d = min(d, s + max(a[j] - z, 0) + dp[j + 1])
s += 1
dp[i] = d
print(dp[n + 1])
solve() | IMPORT FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR |
You have a multiset containing several integers. Initially, it contains $a_1$ elements equal to $1$, $a_2$ elements equal to $2$, ..., $a_n$ elements equal to $n$.
You may apply two types of operations: choose two integers $l$ and $r$ ($l \le r$), then remove one occurrence of $l$, one occurrence of $l + 1$, ..., one occurrence of $r$ from the multiset. This operation can be applied only if each number from $l$ to $r$ occurs at least once in the multiset; choose two integers $i$ and $x$ ($x \ge 1$), then remove $x$ occurrences of $i$ from the multiset. This operation can be applied only if the multiset contains at least $x$ occurrences of $i$.
What is the minimum number of operations required to delete all elements from the multiset?
-----Input-----
The first line contains one integer $n$ ($1 \le n \le 5000$).
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 10^9$).
-----Output-----
Print one integer — the minimum number of operations required to delete all elements from the multiset.
-----Examples-----
Input
4
1 4 1 1
Output
2
Input
5
1 0 1 0 1
Output
3 | import sys
sys.setrecursionlimit(10**6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II():
return int(sys.stdin.readline())
def MI():
return map(int, sys.stdin.readline().split())
def LI():
return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number):
return [LI() for _ in range(rows_number)]
def SI():
return sys.stdin.readline()[:-1]
def cal(l, r, h=0):
if l >= r:
return 0
mn = mni = inf
for i in range(l, r):
if aa[i] < mn:
mn = aa[i]
mni = i
return min(cal(l, mni, mn) + cal(mni + 1, r, mn) + mn - h, r - l)
inf = 10**16
n = II()
aa = LI()
print(cal(0, n)) | IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR NUMBER FUNC_DEF NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR |
You have a multiset containing several integers. Initially, it contains $a_1$ elements equal to $1$, $a_2$ elements equal to $2$, ..., $a_n$ elements equal to $n$.
You may apply two types of operations: choose two integers $l$ and $r$ ($l \le r$), then remove one occurrence of $l$, one occurrence of $l + 1$, ..., one occurrence of $r$ from the multiset. This operation can be applied only if each number from $l$ to $r$ occurs at least once in the multiset; choose two integers $i$ and $x$ ($x \ge 1$), then remove $x$ occurrences of $i$ from the multiset. This operation can be applied only if the multiset contains at least $x$ occurrences of $i$.
What is the minimum number of operations required to delete all elements from the multiset?
-----Input-----
The first line contains one integer $n$ ($1 \le n \le 5000$).
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 10^9$).
-----Output-----
Print one integer — the minimum number of operations required to delete all elements from the multiset.
-----Examples-----
Input
4
1 4 1 1
Output
2
Input
5
1 0 1 0 1
Output
3 | n = int(input()) + 1
l = list(map(int, input().split())) + [0]
out = 0
q = []
for v in l:
if v == 0:
dp = []
n = len(q)
for i in range(n):
curr = q[i] + i
smol = q[i]
for j in range(i - 1, -1, -1):
smol = min(q[j], smol)
diff = q[i] - smol
curr = min(curr, diff + dp[j] + i - j - 1)
dp.append(curr)
real = [(n - i + dp[i] - 1) for i in range(n)] + [n]
out += min(real)
q = []
else:
q.append(v)
print(out) | ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR LIST VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
You have a multiset containing several integers. Initially, it contains $a_1$ elements equal to $1$, $a_2$ elements equal to $2$, ..., $a_n$ elements equal to $n$.
You may apply two types of operations: choose two integers $l$ and $r$ ($l \le r$), then remove one occurrence of $l$, one occurrence of $l + 1$, ..., one occurrence of $r$ from the multiset. This operation can be applied only if each number from $l$ to $r$ occurs at least once in the multiset; choose two integers $i$ and $x$ ($x \ge 1$), then remove $x$ occurrences of $i$ from the multiset. This operation can be applied only if the multiset contains at least $x$ occurrences of $i$.
What is the minimum number of operations required to delete all elements from the multiset?
-----Input-----
The first line contains one integer $n$ ($1 \le n \le 5000$).
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 10^9$).
-----Output-----
Print one integer — the minimum number of operations required to delete all elements from the multiset.
-----Examples-----
Input
4
1 4 1 1
Output
2
Input
5
1 0 1 0 1
Output
3 | import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
n = int(input())
a = list(map(int, input().split()))
def calc(l, r, h):
if l >= r:
return 0
m = a.index(min(a[l:r]), l, r)
return min(r - l, a[m] - h + calc(l, m, a[m]) + calc(m + 1, r, a[m]))
print(calc(0, n, 0)) | IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR NUMBER |
You have a multiset containing several integers. Initially, it contains $a_1$ elements equal to $1$, $a_2$ elements equal to $2$, ..., $a_n$ elements equal to $n$.
You may apply two types of operations: choose two integers $l$ and $r$ ($l \le r$), then remove one occurrence of $l$, one occurrence of $l + 1$, ..., one occurrence of $r$ from the multiset. This operation can be applied only if each number from $l$ to $r$ occurs at least once in the multiset; choose two integers $i$ and $x$ ($x \ge 1$), then remove $x$ occurrences of $i$ from the multiset. This operation can be applied only if the multiset contains at least $x$ occurrences of $i$.
What is the minimum number of operations required to delete all elements from the multiset?
-----Input-----
The first line contains one integer $n$ ($1 \le n \le 5000$).
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 10^9$).
-----Output-----
Print one integer — the minimum number of operations required to delete all elements from the multiset.
-----Examples-----
Input
4
1 4 1 1
Output
2
Input
5
1 0 1 0 1
Output
3 | n = int(input())
arr = [int(x) for x in input().split()]
arrays = [arr]
brr = [0]
stack = [0]
ans = [0]
while stack:
state = stack.pop()
if state >= 0:
arr = arrays[state]
b = brr[state]
n = j = len(arr)
if n == 0:
continue
a = min(arr)
stack.append(~state)
ans.append(a - b)
for i in reversed(range(n)):
if arr[i] == a:
stack.append(len(arrays))
arrays.append(arr[i + 1 : j])
brr.append(a)
j = i
stack.append(len(arrays))
arrays.append(arr[0:j])
brr.append(a)
else:
s = ans.pop()
ans[-1] += min(s, len(arrays[~state]))
print(ans[0]) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER |
You have a multiset containing several integers. Initially, it contains $a_1$ elements equal to $1$, $a_2$ elements equal to $2$, ..., $a_n$ elements equal to $n$.
You may apply two types of operations: choose two integers $l$ and $r$ ($l \le r$), then remove one occurrence of $l$, one occurrence of $l + 1$, ..., one occurrence of $r$ from the multiset. This operation can be applied only if each number from $l$ to $r$ occurs at least once in the multiset; choose two integers $i$ and $x$ ($x \ge 1$), then remove $x$ occurrences of $i$ from the multiset. This operation can be applied only if the multiset contains at least $x$ occurrences of $i$.
What is the minimum number of operations required to delete all elements from the multiset?
-----Input-----
The first line contains one integer $n$ ($1 \le n \le 5000$).
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 10^9$).
-----Output-----
Print one integer — the minimum number of operations required to delete all elements from the multiset.
-----Examples-----
Input
4
1 4 1 1
Output
2
Input
5
1 0 1 0 1
Output
3 | import sys
sys.setrecursionlimit(10000)
n = int(input())
a = list(map(int, input().split()))
def f(l, r, h):
if l > r:
return 0
x = a.index(min(a[l : r + 1]), l, r + 1)
return min(r - l + 1, a[x] - h + f(l, x - 1, a[x]) + f(x + 1, r, a[x]))
print(f(0, n - 1, 0)) | IMPORT EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER |
You have a multiset containing several integers. Initially, it contains $a_1$ elements equal to $1$, $a_2$ elements equal to $2$, ..., $a_n$ elements equal to $n$.
You may apply two types of operations: choose two integers $l$ and $r$ ($l \le r$), then remove one occurrence of $l$, one occurrence of $l + 1$, ..., one occurrence of $r$ from the multiset. This operation can be applied only if each number from $l$ to $r$ occurs at least once in the multiset; choose two integers $i$ and $x$ ($x \ge 1$), then remove $x$ occurrences of $i$ from the multiset. This operation can be applied only if the multiset contains at least $x$ occurrences of $i$.
What is the minimum number of operations required to delete all elements from the multiset?
-----Input-----
The first line contains one integer $n$ ($1 \le n \le 5000$).
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 10^9$).
-----Output-----
Print one integer — the minimum number of operations required to delete all elements from the multiset.
-----Examples-----
Input
4
1 4 1 1
Output
2
Input
5
1 0 1 0 1
Output
3 | import sys
input = sys.stdin.buffer.readline
n = int(input())
a = list(map(int, input().split()))
dp = [n] * (n * (n + 1))
dg = n + 1
mi = [0] * (n + 1)
for j in range(n + 1):
if j > a[0]:
break
if j == a[0]:
dp[j] = j
else:
dp[j] = j + 1
tmp = 2 * n
for j in range(n + 1):
tmp = min(tmp, dp[j] + n - j)
mi[j] = tmp
for i in range(1, n):
for j in range(n, -1, -1):
if j > a[i]:
continue
if j != n:
tmp = dp[i * dg + j + 1]
else:
tmp = n
if a[i] > j:
tmp = min(tmp, dp[(i - 1) * dg + j] + 1)
else:
tmp = min(tmp, dp[(i - 1) * dg + j])
if j != 0:
if a[i] > j:
tmp = min(tmp, mi[j - 1] + j - n + 1)
else:
tmp = min(tmp, mi[j - 1] + j - n)
dp[i * dg + j] = tmp
tmp = 2 * n
for j in range(n + 1):
tmp = min(tmp, dp[i * dg + j] + n - j)
mi[j] = tmp
print(dp[(n - 1) * dg]) | IMPORT ASSIGN 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 BIN_OP LIST VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR IF VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR |
You have a multiset containing several integers. Initially, it contains $a_1$ elements equal to $1$, $a_2$ elements equal to $2$, ..., $a_n$ elements equal to $n$.
You may apply two types of operations: choose two integers $l$ and $r$ ($l \le r$), then remove one occurrence of $l$, one occurrence of $l + 1$, ..., one occurrence of $r$ from the multiset. This operation can be applied only if each number from $l$ to $r$ occurs at least once in the multiset; choose two integers $i$ and $x$ ($x \ge 1$), then remove $x$ occurrences of $i$ from the multiset. This operation can be applied only if the multiset contains at least $x$ occurrences of $i$.
What is the minimum number of operations required to delete all elements from the multiset?
-----Input-----
The first line contains one integer $n$ ($1 \le n \le 5000$).
The second line contains $n$ integers $a_1$, $a_2$, ..., $a_n$ ($0 \le a_i \le 10^9$).
-----Output-----
Print one integer — the minimum number of operations required to delete all elements from the multiset.
-----Examples-----
Input
4
1 4 1 1
Output
2
Input
5
1 0 1 0 1
Output
3 | from sys import stdin
n = int(stdin.readline())
a = list(map(int, stdin.readline().split()))
a = [0] + a
ans = n
dp = [float("inf")] * (n + 1)
dp[0] = 0
for i in range(1, n + 1):
nmin = float("inf")
for j in range(i - 1, -1, -1):
if a[j] <= nmin:
dp[i] = min(dp[i], dp[j] + max(0, a[i] - a[j]) + (i - j - 1))
nmin = min(nmin, a[j])
for i in range(n + 1):
ans = min(ans, dp[i] + n - i)
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR |
A sequence of numbers is called a wiggle sequence if the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with fewer than two elements is trivially a wiggle sequence.
For example, [1,7,4,9,2,5] is a wiggle sequence because the differences (6,-3,5,-7,3) are alternately positive and negative. In contrast, [1,4,7,2,5] and [1,7,4,5,5] are not wiggle sequences, the first because its first two differences are positive and the second because its last difference is zero.
Given a sequence of integers, return the length of the longest subsequence that is a wiggle sequence. A subsequence is obtained by deleting some number of elements (eventually, also zero) from the original sequence, leaving the remaining elements in their original order.
Examples:
Input: [1,7,4,9,2,5]
Output: 6
The entire sequence is a wiggle sequence.
Input: [1,17,5,10,13,15,10,5,16,8]
Output: 7
There are several subsequences that achieve this length. One is [1,17,10,13,10,16,8].
Input: [1,2,3,4,5,6,7,8,9]
Output: 2
Follow up:
Can you do it in O(n) time?
Credits:Special thanks to @agave and @StefanPochmann for adding this problem and creating all test cases. | class Solution:
def wiggleMaxLength(self, nums):
nums = [nums[i] for i in range(len(nums)) if i == 0 or nums[i] != nums[i - 1]]
print(nums)
total = min(2, len(nums))
for i in range(1, len(nums) - 1):
if nums[i] > nums[i - 1] and nums[i] > nums[i + 1]:
total += 1
elif nums[i] < nums[i - 1] and nums[i] < nums[i + 1]:
total += 1
return total | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR |
A sequence of numbers is called a wiggle sequence if the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with fewer than two elements is trivially a wiggle sequence.
For example, [1,7,4,9,2,5] is a wiggle sequence because the differences (6,-3,5,-7,3) are alternately positive and negative. In contrast, [1,4,7,2,5] and [1,7,4,5,5] are not wiggle sequences, the first because its first two differences are positive and the second because its last difference is zero.
Given a sequence of integers, return the length of the longest subsequence that is a wiggle sequence. A subsequence is obtained by deleting some number of elements (eventually, also zero) from the original sequence, leaving the remaining elements in their original order.
Examples:
Input: [1,7,4,9,2,5]
Output: 6
The entire sequence is a wiggle sequence.
Input: [1,17,5,10,13,15,10,5,16,8]
Output: 7
There are several subsequences that achieve this length. One is [1,17,10,13,10,16,8].
Input: [1,2,3,4,5,6,7,8,9]
Output: 2
Follow up:
Can you do it in O(n) time?
Credits:Special thanks to @agave and @StefanPochmann for adding this problem and creating all test cases. | class Solution:
def wiggleMaxLength(self, nums):
n = len(nums)
if n == 0:
return 0
gn = [1] * n
trend = 0
for i in range(1, n):
prev = nums[i - 1]
if prev == nums[i]:
gn[i] = gn[i - 1]
else:
if trend == 0:
gn[i] = gn[i - 1] + 1
trend = 1 if nums[i] > prev else -1
continue
if nums[i] > prev and trend == -1 or nums[i] < prev and trend == 1:
gn[i] = gn[i - 1] + 1
trend = -trend
else:
gn[i] = gn[i - 1]
return gn[-1] | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR NUMBER |
A sequence of numbers is called a wiggle sequence if the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with fewer than two elements is trivially a wiggle sequence.
For example, [1,7,4,9,2,5] is a wiggle sequence because the differences (6,-3,5,-7,3) are alternately positive and negative. In contrast, [1,4,7,2,5] and [1,7,4,5,5] are not wiggle sequences, the first because its first two differences are positive and the second because its last difference is zero.
Given a sequence of integers, return the length of the longest subsequence that is a wiggle sequence. A subsequence is obtained by deleting some number of elements (eventually, also zero) from the original sequence, leaving the remaining elements in their original order.
Examples:
Input: [1,7,4,9,2,5]
Output: 6
The entire sequence is a wiggle sequence.
Input: [1,17,5,10,13,15,10,5,16,8]
Output: 7
There are several subsequences that achieve this length. One is [1,17,10,13,10,16,8].
Input: [1,2,3,4,5,6,7,8,9]
Output: 2
Follow up:
Can you do it in O(n) time?
Credits:Special thanks to @agave and @StefanPochmann for adding this problem and creating all test cases. | class Solution:
def wiggleMaxLength(self, nums):
n = len(nums)
if n < 2:
return n
prev_diff = nums[1] - nums[0]
count = 2 if prev_diff != 0 else 1
for i in range(2, n):
diff = nums[i] - nums[i - 1]
if diff > 0 and prev_diff <= 0 or diff < 0 and prev_diff >= 0:
count += 1
prev_diff = diff
return count | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR RETURN VAR |
A sequence of numbers is called a wiggle sequence if the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with fewer than two elements is trivially a wiggle sequence.
For example, [1,7,4,9,2,5] is a wiggle sequence because the differences (6,-3,5,-7,3) are alternately positive and negative. In contrast, [1,4,7,2,5] and [1,7,4,5,5] are not wiggle sequences, the first because its first two differences are positive and the second because its last difference is zero.
Given a sequence of integers, return the length of the longest subsequence that is a wiggle sequence. A subsequence is obtained by deleting some number of elements (eventually, also zero) from the original sequence, leaving the remaining elements in their original order.
Examples:
Input: [1,7,4,9,2,5]
Output: 6
The entire sequence is a wiggle sequence.
Input: [1,17,5,10,13,15,10,5,16,8]
Output: 7
There are several subsequences that achieve this length. One is [1,17,10,13,10,16,8].
Input: [1,2,3,4,5,6,7,8,9]
Output: 2
Follow up:
Can you do it in O(n) time?
Credits:Special thanks to @agave and @StefanPochmann for adding this problem and creating all test cases. | class Solution:
def wiggleMaxLength(self, arr):
n = len(arr)
if n < 2:
return n
wsl = [0] * n
wsl[0] = 1
for cur in range(1, n):
prev = cur - 1
if arr[cur] > arr[prev] and wsl[prev] <= 1:
wsl[cur] = abs(wsl[prev]) + 1
elif arr[cur] < arr[prev] and wsl[prev] > 0:
wsl[cur] = (abs(wsl[prev]) + 1) * -1
else:
wsl[cur] = wsl[prev]
return abs(wsl[n - 1]) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER |
A sequence of numbers is called a wiggle sequence if the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with fewer than two elements is trivially a wiggle sequence.
For example, [1,7,4,9,2,5] is a wiggle sequence because the differences (6,-3,5,-7,3) are alternately positive and negative. In contrast, [1,4,7,2,5] and [1,7,4,5,5] are not wiggle sequences, the first because its first two differences are positive and the second because its last difference is zero.
Given a sequence of integers, return the length of the longest subsequence that is a wiggle sequence. A subsequence is obtained by deleting some number of elements (eventually, also zero) from the original sequence, leaving the remaining elements in their original order.
Examples:
Input: [1,7,4,9,2,5]
Output: 6
The entire sequence is a wiggle sequence.
Input: [1,17,5,10,13,15,10,5,16,8]
Output: 7
There are several subsequences that achieve this length. One is [1,17,10,13,10,16,8].
Input: [1,2,3,4,5,6,7,8,9]
Output: 2
Follow up:
Can you do it in O(n) time?
Credits:Special thanks to @agave and @StefanPochmann for adding this problem and creating all test cases. | class Solution:
def wiggleMaxLength(self, nums):
if len(nums) == 0:
return 0
max_up = [1]
max_down = [1]
for i in range(1, len(nums)):
if nums[i] > nums[i - 1]:
max_up.append(max_down[i - 1] + 1)
max_down.append(max_down[i - 1])
elif nums[i] < nums[i - 1]:
max_up.append(max_up[i - 1])
max_down.append(max_up[i - 1] + 1)
else:
max_up.append(max_up[i - 1])
max_down.append(max_down[i - 1])
return max(max_up[-1], max_down[-1]) | CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR NUMBER |
A sequence of numbers is called a wiggle sequence if the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with fewer than two elements is trivially a wiggle sequence.
For example, [1,7,4,9,2,5] is a wiggle sequence because the differences (6,-3,5,-7,3) are alternately positive and negative. In contrast, [1,4,7,2,5] and [1,7,4,5,5] are not wiggle sequences, the first because its first two differences are positive and the second because its last difference is zero.
Given a sequence of integers, return the length of the longest subsequence that is a wiggle sequence. A subsequence is obtained by deleting some number of elements (eventually, also zero) from the original sequence, leaving the remaining elements in their original order.
Examples:
Input: [1,7,4,9,2,5]
Output: 6
The entire sequence is a wiggle sequence.
Input: [1,17,5,10,13,15,10,5,16,8]
Output: 7
There are several subsequences that achieve this length. One is [1,17,10,13,10,16,8].
Input: [1,2,3,4,5,6,7,8,9]
Output: 2
Follow up:
Can you do it in O(n) time?
Credits:Special thanks to @agave and @StefanPochmann for adding this problem and creating all test cases. | class Solution:
def wiggleMaxLength(self, nums):
l = len(nums)
if l == 0:
return 0
elif l == 1:
return 1
st = 1
while nums[0] == nums[st]:
st += 1
if st == l:
return 1
one, two = nums[0], nums[st]
cnt = 2
for i in range(st + 1, l):
if nums[i] == two:
continue
if one < two:
if nums[i] > two:
two = nums[i]
else:
one = two
two = nums[i]
cnt += 1
elif nums[i] < two:
two = nums[i]
else:
one = two
two = nums[i]
cnt += 1
return cnt | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR IF VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR NUMBER RETURN VAR |
A sequence of numbers is called a wiggle sequence if the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with fewer than two elements is trivially a wiggle sequence.
For example, [1,7,4,9,2,5] is a wiggle sequence because the differences (6,-3,5,-7,3) are alternately positive and negative. In contrast, [1,4,7,2,5] and [1,7,4,5,5] are not wiggle sequences, the first because its first two differences are positive and the second because its last difference is zero.
Given a sequence of integers, return the length of the longest subsequence that is a wiggle sequence. A subsequence is obtained by deleting some number of elements (eventually, also zero) from the original sequence, leaving the remaining elements in their original order.
Examples:
Input: [1,7,4,9,2,5]
Output: 6
The entire sequence is a wiggle sequence.
Input: [1,17,5,10,13,15,10,5,16,8]
Output: 7
There are several subsequences that achieve this length. One is [1,17,10,13,10,16,8].
Input: [1,2,3,4,5,6,7,8,9]
Output: 2
Follow up:
Can you do it in O(n) time?
Credits:Special thanks to @agave and @StefanPochmann for adding this problem and creating all test cases. | class Solution:
def wiggleMaxLength(self, nums):
if nums == []:
return 0
if len(nums) < 2:
return 1
max_length = 1
state = 0
i = 1
while i < len(nums):
while state == 0 and i < len(nums):
if nums[i - 1] > nums[i]:
max_length = max_length + 1
state = 2
if nums[i - 1] < nums[i]:
max_length = max_length + 1
state = 1
i = i + 1
while state == 1 and i < len(nums):
if nums[i - 1] > nums[i]:
max_length = max_length + 1
state = 2
i = i + 1
while state == 2 and i < len(nums):
if nums[i - 1] < nums[i]:
state = 1
max_length = max_length + 1
i = i + 1
return max_length | CLASS_DEF FUNC_DEF IF VAR LIST RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR WHILE VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR |
A sequence of numbers is called a wiggle sequence if the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with fewer than two elements is trivially a wiggle sequence.
For example, [1,7,4,9,2,5] is a wiggle sequence because the differences (6,-3,5,-7,3) are alternately positive and negative. In contrast, [1,4,7,2,5] and [1,7,4,5,5] are not wiggle sequences, the first because its first two differences are positive and the second because its last difference is zero.
Given a sequence of integers, return the length of the longest subsequence that is a wiggle sequence. A subsequence is obtained by deleting some number of elements (eventually, also zero) from the original sequence, leaving the remaining elements in their original order.
Examples:
Input: [1,7,4,9,2,5]
Output: 6
The entire sequence is a wiggle sequence.
Input: [1,17,5,10,13,15,10,5,16,8]
Output: 7
There are several subsequences that achieve this length. One is [1,17,10,13,10,16,8].
Input: [1,2,3,4,5,6,7,8,9]
Output: 2
Follow up:
Can you do it in O(n) time?
Credits:Special thanks to @agave and @StefanPochmann for adding this problem and creating all test cases. | class Solution:
def wiggleMaxLength(self, nums):
if not nums:
return 0
l = len(nums)
dp = [0] * l
up = [0] * l
down = [0] * l
up[0] = down[0] = 1
for i in range(1, l):
if nums[i - 1] < nums[i]:
up[i] = up[i - 1]
down[i] = up[i - 1] + 1
elif nums[i - 1] > nums[i]:
down[i] = down[i - 1]
up[i] = down[i - 1] + 1
else:
up[i] = up[i - 1]
down[i] = down[i - 1]
return max(down[-1], up[-1]) | CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR NUMBER |
A sequence of numbers is called a wiggle sequence if the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with fewer than two elements is trivially a wiggle sequence.
For example, [1,7,4,9,2,5] is a wiggle sequence because the differences (6,-3,5,-7,3) are alternately positive and negative. In contrast, [1,4,7,2,5] and [1,7,4,5,5] are not wiggle sequences, the first because its first two differences are positive and the second because its last difference is zero.
Given a sequence of integers, return the length of the longest subsequence that is a wiggle sequence. A subsequence is obtained by deleting some number of elements (eventually, also zero) from the original sequence, leaving the remaining elements in their original order.
Examples:
Input: [1,7,4,9,2,5]
Output: 6
The entire sequence is a wiggle sequence.
Input: [1,17,5,10,13,15,10,5,16,8]
Output: 7
There are several subsequences that achieve this length. One is [1,17,10,13,10,16,8].
Input: [1,2,3,4,5,6,7,8,9]
Output: 2
Follow up:
Can you do it in O(n) time?
Credits:Special thanks to @agave and @StefanPochmann for adding this problem and creating all test cases. | class Solution:
def wiggleMaxLength(self, nums):
if len(nums) == 0:
return 0
if len(nums) == 1:
return 1
N = len(nums)
sol = [(0) for _ in nums]
sol[0] = [1, 1]
for i in range(1, N):
new = [0, 0]
if nums[i] > nums[i - 1]:
new[0] = sol[i - 1][1] + 1
else:
new[0] = sol[i - 1][0]
if nums[i] < nums[i - 1]:
new[1] = sol[i - 1][0] + 1
else:
new[1] = sol[i - 1][1]
sol[i] = new
return max(sol[-1]) | CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR VAR ASSIGN VAR NUMBER LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR LIST NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR RETURN FUNC_CALL VAR VAR NUMBER |
A sequence of numbers is called a wiggle sequence if the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with fewer than two elements is trivially a wiggle sequence.
For example, [1,7,4,9,2,5] is a wiggle sequence because the differences (6,-3,5,-7,3) are alternately positive and negative. In contrast, [1,4,7,2,5] and [1,7,4,5,5] are not wiggle sequences, the first because its first two differences are positive and the second because its last difference is zero.
Given a sequence of integers, return the length of the longest subsequence that is a wiggle sequence. A subsequence is obtained by deleting some number of elements (eventually, also zero) from the original sequence, leaving the remaining elements in their original order.
Examples:
Input: [1,7,4,9,2,5]
Output: 6
The entire sequence is a wiggle sequence.
Input: [1,17,5,10,13,15,10,5,16,8]
Output: 7
There are several subsequences that achieve this length. One is [1,17,10,13,10,16,8].
Input: [1,2,3,4,5,6,7,8,9]
Output: 2
Follow up:
Can you do it in O(n) time?
Credits:Special thanks to @agave and @StefanPochmann for adding this problem and creating all test cases. | class Solution:
def wiggleMaxLength(self, nums):
l = len(nums)
if l == 0:
return 0
elif l == 1:
return 1
st = 1
while nums[0] == nums[st]:
st += 1
if st == l:
return 1
seq = [nums[0], nums[st]]
for i in range(st + 1, l):
if nums[i] == seq[-1]:
continue
if seq[-1] < seq[-2]:
if nums[i] < seq[-1]:
seq[-1] = nums[i]
else:
seq.append(nums[i])
elif nums[i] > seq[-1]:
seq[-1] = nums[i]
else:
seq.append(nums[i])
return len(seq) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR LIST VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR |
A sequence of numbers is called a wiggle sequence if the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with fewer than two elements is trivially a wiggle sequence.
For example, [1,7,4,9,2,5] is a wiggle sequence because the differences (6,-3,5,-7,3) are alternately positive and negative. In contrast, [1,4,7,2,5] and [1,7,4,5,5] are not wiggle sequences, the first because its first two differences are positive and the second because its last difference is zero.
Given a sequence of integers, return the length of the longest subsequence that is a wiggle sequence. A subsequence is obtained by deleting some number of elements (eventually, also zero) from the original sequence, leaving the remaining elements in their original order.
Examples:
Input: [1,7,4,9,2,5]
Output: 6
The entire sequence is a wiggle sequence.
Input: [1,17,5,10,13,15,10,5,16,8]
Output: 7
There are several subsequences that achieve this length. One is [1,17,10,13,10,16,8].
Input: [1,2,3,4,5,6,7,8,9]
Output: 2
Follow up:
Can you do it in O(n) time?
Credits:Special thanks to @agave and @StefanPochmann for adding this problem and creating all test cases. | class Solution:
def wiggleMaxLength(self, nums):
if len(nums) < 2:
return len(nums)
diffs = []
for i in range(1, len(nums)):
diffs.append(nums[i] - nums[i - 1])
prev_idx = 0
while diffs[prev_idx] == 0:
prev_idx += 1
if prev_idx == len(diffs):
return 1
cnt = 2
for i in range(prev_idx + 1, len(diffs)):
if diffs[prev_idx] * diffs[i] < 0:
cnt += 1
prev_idx = i
return cnt | CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR RETURN VAR |
Given an positive integer N and a list of N integers A[]. Each element in the array denotes the maximum length of jump you can cover. Find out if you can make it to the last index if you start at the first index of the list.
Example 1:
Input:
N = 6
A[] = {1, 2, 0, 3, 0, 0}
Output:
1
Explanation:
Jump 1 step from first index to
second index. Then jump 2 steps to reach
4_{th }index, and now jump 2 steps to reach
the end.
Example 2:
Input:
N = 3
A[] = {1, 0, 2}
Output:
0
Explanation:
You can't reach the end of the array.
Your Task:
You don't need to read input or print anything. Your task is to complete the function canReach() which takes a Integer N and a list A of size N as input and returns 1 if the end of the array is reachable, else return 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
0 <= A[i] <= 10^{5} | class Solution:
def canReach(self, A, N):
pos, dis = 0, 0
for i in range(N - 1):
dis = max(dis, i + A[i])
if pos == i:
pos = dis
if pos == i:
return 0
return 1 | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER |
Given an positive integer N and a list of N integers A[]. Each element in the array denotes the maximum length of jump you can cover. Find out if you can make it to the last index if you start at the first index of the list.
Example 1:
Input:
N = 6
A[] = {1, 2, 0, 3, 0, 0}
Output:
1
Explanation:
Jump 1 step from first index to
second index. Then jump 2 steps to reach
4_{th }index, and now jump 2 steps to reach
the end.
Example 2:
Input:
N = 3
A[] = {1, 0, 2}
Output:
0
Explanation:
You can't reach the end of the array.
Your Task:
You don't need to read input or print anything. Your task is to complete the function canReach() which takes a Integer N and a list A of size N as input and returns 1 if the end of the array is reachable, else return 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
0 <= A[i] <= 10^{5} | class Solution:
def canReach(self, a, n):
if n in [0, 1]:
return 1
maxReach = a[0]
steps = a[0]
i = 0
jumps = 0
while i < n - 1:
maxReach = max(maxReach, a[i] + i)
steps -= 1
if steps == 0:
if maxReach <= i:
return 0
steps = maxReach - i
jumps += 1
i += 1
return 1 | CLASS_DEF FUNC_DEF IF VAR LIST NUMBER NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER RETURN NUMBER |
Given an positive integer N and a list of N integers A[]. Each element in the array denotes the maximum length of jump you can cover. Find out if you can make it to the last index if you start at the first index of the list.
Example 1:
Input:
N = 6
A[] = {1, 2, 0, 3, 0, 0}
Output:
1
Explanation:
Jump 1 step from first index to
second index. Then jump 2 steps to reach
4_{th }index, and now jump 2 steps to reach
the end.
Example 2:
Input:
N = 3
A[] = {1, 0, 2}
Output:
0
Explanation:
You can't reach the end of the array.
Your Task:
You don't need to read input or print anything. Your task is to complete the function canReach() which takes a Integer N and a list A of size N as input and returns 1 if the end of the array is reachable, else return 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
0 <= A[i] <= 10^{5} | class Solution:
def get_number_of_jumps(self, a, n):
if n == 1:
return 1
if n == 0 or a[0] == 0:
return -1
jumps = [0] * n
for i in range(1, n):
jumps[i] = -1
for j in range(i):
if j + a[j] >= i and jumps[j] != -1:
jumps[i] = jumps[j] + 1
break
return jumps[-1]
def canReach(self, A, N):
count = self.get_number_of_jumps(A, N)
if count == -1:
return 0
return 1 | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER RETURN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN NUMBER RETURN NUMBER |
Given an positive integer N and a list of N integers A[]. Each element in the array denotes the maximum length of jump you can cover. Find out if you can make it to the last index if you start at the first index of the list.
Example 1:
Input:
N = 6
A[] = {1, 2, 0, 3, 0, 0}
Output:
1
Explanation:
Jump 1 step from first index to
second index. Then jump 2 steps to reach
4_{th }index, and now jump 2 steps to reach
the end.
Example 2:
Input:
N = 3
A[] = {1, 0, 2}
Output:
0
Explanation:
You can't reach the end of the array.
Your Task:
You don't need to read input or print anything. Your task is to complete the function canReach() which takes a Integer N and a list A of size N as input and returns 1 if the end of the array is reachable, else return 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
0 <= A[i] <= 10^{5} | class Solution:
def canReach(self, a, n):
goal = n - 1
for i in range(n - 2, -1, -1):
if i + a[i] >= goal:
goal = i
if goal == 0:
return 1
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER RETURN NUMBER RETURN NUMBER |
Given an positive integer N and a list of N integers A[]. Each element in the array denotes the maximum length of jump you can cover. Find out if you can make it to the last index if you start at the first index of the list.
Example 1:
Input:
N = 6
A[] = {1, 2, 0, 3, 0, 0}
Output:
1
Explanation:
Jump 1 step from first index to
second index. Then jump 2 steps to reach
4_{th }index, and now jump 2 steps to reach
the end.
Example 2:
Input:
N = 3
A[] = {1, 0, 2}
Output:
0
Explanation:
You can't reach the end of the array.
Your Task:
You don't need to read input or print anything. Your task is to complete the function canReach() which takes a Integer N and a list A of size N as input and returns 1 if the end of the array is reachable, else return 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
0 <= A[i] <= 10^{5} | class Solution:
def canReach(self, A, N):
re = 0
i = 0
while i < N:
if re < i:
return 0
re = max(re, i + A[i])
i += 1
return 1 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR NUMBER RETURN NUMBER |
Given an positive integer N and a list of N integers A[]. Each element in the array denotes the maximum length of jump you can cover. Find out if you can make it to the last index if you start at the first index of the list.
Example 1:
Input:
N = 6
A[] = {1, 2, 0, 3, 0, 0}
Output:
1
Explanation:
Jump 1 step from first index to
second index. Then jump 2 steps to reach
4_{th }index, and now jump 2 steps to reach
the end.
Example 2:
Input:
N = 3
A[] = {1, 0, 2}
Output:
0
Explanation:
You can't reach the end of the array.
Your Task:
You don't need to read input or print anything. Your task is to complete the function canReach() which takes a Integer N and a list A of size N as input and returns 1 if the end of the array is reachable, else return 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
0 <= A[i] <= 10^{5} | class Solution:
def canReach(self, A, N):
if N == 1:
return 1
m = A[0]
for i in range(N):
if m == 0:
return 0
m = m - 1
if A[i] > m:
m = A[i]
return 1 | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR RETURN NUMBER |
Given an positive integer N and a list of N integers A[]. Each element in the array denotes the maximum length of jump you can cover. Find out if you can make it to the last index if you start at the first index of the list.
Example 1:
Input:
N = 6
A[] = {1, 2, 0, 3, 0, 0}
Output:
1
Explanation:
Jump 1 step from first index to
second index. Then jump 2 steps to reach
4_{th }index, and now jump 2 steps to reach
the end.
Example 2:
Input:
N = 3
A[] = {1, 0, 2}
Output:
0
Explanation:
You can't reach the end of the array.
Your Task:
You don't need to read input or print anything. Your task is to complete the function canReach() which takes a Integer N and a list A of size N as input and returns 1 if the end of the array is reachable, else return 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
0 <= A[i] <= 10^{5} | class Solution:
def canReach(self, A, N):
curr = A[0]
maxi = A[0]
for x in range(1, N):
if x > maxi:
return 0
if maxi < A[x] + x:
maxi = A[x] + x
if curr == x:
curr = maxi
if curr == N - 1:
return 1
return 1 | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR RETURN NUMBER IF VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER |
Given an positive integer N and a list of N integers A[]. Each element in the array denotes the maximum length of jump you can cover. Find out if you can make it to the last index if you start at the first index of the list.
Example 1:
Input:
N = 6
A[] = {1, 2, 0, 3, 0, 0}
Output:
1
Explanation:
Jump 1 step from first index to
second index. Then jump 2 steps to reach
4_{th }index, and now jump 2 steps to reach
the end.
Example 2:
Input:
N = 3
A[] = {1, 0, 2}
Output:
0
Explanation:
You can't reach the end of the array.
Your Task:
You don't need to read input or print anything. Your task is to complete the function canReach() which takes a Integer N and a list A of size N as input and returns 1 if the end of the array is reachable, else return 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
0 <= A[i] <= 10^{5} | class Solution:
def canReach(self, A, N):
i = N - 1
while i > 0:
for j in range(i - 1, -1, -1):
if A[j] + j >= i:
i = j
break
else:
break
return 1 if i == 0 else 0 | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR RETURN VAR NUMBER NUMBER NUMBER |
Given an positive integer N and a list of N integers A[]. Each element in the array denotes the maximum length of jump you can cover. Find out if you can make it to the last index if you start at the first index of the list.
Example 1:
Input:
N = 6
A[] = {1, 2, 0, 3, 0, 0}
Output:
1
Explanation:
Jump 1 step from first index to
second index. Then jump 2 steps to reach
4_{th }index, and now jump 2 steps to reach
the end.
Example 2:
Input:
N = 3
A[] = {1, 0, 2}
Output:
0
Explanation:
You can't reach the end of the array.
Your Task:
You don't need to read input or print anything. Your task is to complete the function canReach() which takes a Integer N and a list A of size N as input and returns 1 if the end of the array is reachable, else return 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
0 <= A[i] <= 10^{5} | class Solution:
def canReach(self, A, N):
if N <= 1:
return 1
curr = 0
reach = N - 1
tempReach = 0
maxReach = 0
while curr < reach and A[curr] != 0:
tempReach = curr + A[curr]
if tempReach >= reach:
return 1
maxReach = tempReach + A[tempReach]
for i in range(curr + 1, curr + A[curr]):
if i + A[i] > maxReach:
tempReach = i
maxReach = i + A[i]
curr = tempReach
if A[curr] == 0:
return 0
return 1 | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR IF VAR VAR NUMBER RETURN NUMBER RETURN NUMBER |
Given an positive integer N and a list of N integers A[]. Each element in the array denotes the maximum length of jump you can cover. Find out if you can make it to the last index if you start at the first index of the list.
Example 1:
Input:
N = 6
A[] = {1, 2, 0, 3, 0, 0}
Output:
1
Explanation:
Jump 1 step from first index to
second index. Then jump 2 steps to reach
4_{th }index, and now jump 2 steps to reach
the end.
Example 2:
Input:
N = 3
A[] = {1, 0, 2}
Output:
0
Explanation:
You can't reach the end of the array.
Your Task:
You don't need to read input or print anything. Your task is to complete the function canReach() which takes a Integer N and a list A of size N as input and returns 1 if the end of the array is reachable, else return 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
0 <= A[i] <= 10^{5} | class Solution:
def canReach(self, A, N):
last = N - 1
i = last - 1
while 0 <= i:
if last <= i + A[i]:
last = i
i -= 1
return 1 if last == 0 else 0 | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE NUMBER VAR IF VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR NUMBER NUMBER NUMBER |
Given an positive integer N and a list of N integers A[]. Each element in the array denotes the maximum length of jump you can cover. Find out if you can make it to the last index if you start at the first index of the list.
Example 1:
Input:
N = 6
A[] = {1, 2, 0, 3, 0, 0}
Output:
1
Explanation:
Jump 1 step from first index to
second index. Then jump 2 steps to reach
4_{th }index, and now jump 2 steps to reach
the end.
Example 2:
Input:
N = 3
A[] = {1, 0, 2}
Output:
0
Explanation:
You can't reach the end of the array.
Your Task:
You don't need to read input or print anything. Your task is to complete the function canReach() which takes a Integer N and a list A of size N as input and returns 1 if the end of the array is reachable, else return 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
0 <= A[i] <= 10^{5} | class Solution:
def canReach(self, A, N):
dp = [float("inf") for _ in range(N)]
dp[-1] = 0
for i in reversed(range(0, N - 1)):
distWeCanJump = A[i]
if i + distWeCanJump >= N - 1:
dp[i] = 1
else:
minEffort = float("inf")
for j in range(i + 1, i + distWeCanJump + 1):
minEffort = min(minEffort, dp[j])
dp[i] = minEffort
return 1 if dp[0] < float("inf") else 0 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR NUMBER FUNC_CALL VAR STRING NUMBER NUMBER |
Given an positive integer N and a list of N integers A[]. Each element in the array denotes the maximum length of jump you can cover. Find out if you can make it to the last index if you start at the first index of the list.
Example 1:
Input:
N = 6
A[] = {1, 2, 0, 3, 0, 0}
Output:
1
Explanation:
Jump 1 step from first index to
second index. Then jump 2 steps to reach
4_{th }index, and now jump 2 steps to reach
the end.
Example 2:
Input:
N = 3
A[] = {1, 0, 2}
Output:
0
Explanation:
You can't reach the end of the array.
Your Task:
You don't need to read input or print anything. Your task is to complete the function canReach() which takes a Integer N and a list A of size N as input and returns 1 if the end of the array is reachable, else return 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
0 <= A[i] <= 10^{5} | class Solution:
def canReach(self, A, N):
if N == 1:
return 1
maxReach = A[0]
for i in range(1, N):
maxReach = max(maxReach, A[i] + i)
if maxReach >= N - 1:
return 1
if i == N - 1:
return 1
if maxReach <= i:
return 0
return 1 | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER |
Given an positive integer N and a list of N integers A[]. Each element in the array denotes the maximum length of jump you can cover. Find out if you can make it to the last index if you start at the first index of the list.
Example 1:
Input:
N = 6
A[] = {1, 2, 0, 3, 0, 0}
Output:
1
Explanation:
Jump 1 step from first index to
second index. Then jump 2 steps to reach
4_{th }index, and now jump 2 steps to reach
the end.
Example 2:
Input:
N = 3
A[] = {1, 0, 2}
Output:
0
Explanation:
You can't reach the end of the array.
Your Task:
You don't need to read input or print anything. Your task is to complete the function canReach() which takes a Integer N and a list A of size N as input and returns 1 if the end of the array is reachable, else return 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
0 <= A[i] <= 10^{5} | class Solution:
def canReach(self, A, N):
count = 0
i = 0
while i < N:
ind, jump = -1, 0
if A[i] + i >= N - 1:
return 1
for j in range(1, A[i] + 1):
if A[i + j] + i + j > jump:
jump = A[i + j] + i + j
ind = i + j
count += 1
if ind == -1:
return 0
i = ind
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR RETURN NUMBER |
Given an positive integer N and a list of N integers A[]. Each element in the array denotes the maximum length of jump you can cover. Find out if you can make it to the last index if you start at the first index of the list.
Example 1:
Input:
N = 6
A[] = {1, 2, 0, 3, 0, 0}
Output:
1
Explanation:
Jump 1 step from first index to
second index. Then jump 2 steps to reach
4_{th }index, and now jump 2 steps to reach
the end.
Example 2:
Input:
N = 3
A[] = {1, 0, 2}
Output:
0
Explanation:
You can't reach the end of the array.
Your Task:
You don't need to read input or print anything. Your task is to complete the function canReach() which takes a Integer N and a list A of size N as input and returns 1 if the end of the array is reachable, else return 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
0 <= A[i] <= 10^{5} | class Solution:
def canReach(self, arr, n):
reachable = [0] * n
reachable[0] = 1
for i in range(1, n):
for j in range(i):
if reachable[j] and i <= j + arr[j]:
reachable[i] = 1
break
return reachable[-1] | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER RETURN VAR NUMBER |
Given an positive integer N and a list of N integers A[]. Each element in the array denotes the maximum length of jump you can cover. Find out if you can make it to the last index if you start at the first index of the list.
Example 1:
Input:
N = 6
A[] = {1, 2, 0, 3, 0, 0}
Output:
1
Explanation:
Jump 1 step from first index to
second index. Then jump 2 steps to reach
4_{th }index, and now jump 2 steps to reach
the end.
Example 2:
Input:
N = 3
A[] = {1, 0, 2}
Output:
0
Explanation:
You can't reach the end of the array.
Your Task:
You don't need to read input or print anything. Your task is to complete the function canReach() which takes a Integer N and a list A of size N as input and returns 1 if the end of the array is reachable, else return 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
0 <= A[i] <= 10^{5} | class Solution:
def canReach(self, A, N):
reach = 0
for i in range(len(A)):
if reach < i:
return 0
reach = max(reach, i + A[i])
return 1 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR RETURN NUMBER |
Given an positive integer N and a list of N integers A[]. Each element in the array denotes the maximum length of jump you can cover. Find out if you can make it to the last index if you start at the first index of the list.
Example 1:
Input:
N = 6
A[] = {1, 2, 0, 3, 0, 0}
Output:
1
Explanation:
Jump 1 step from first index to
second index. Then jump 2 steps to reach
4_{th }index, and now jump 2 steps to reach
the end.
Example 2:
Input:
N = 3
A[] = {1, 0, 2}
Output:
0
Explanation:
You can't reach the end of the array.
Your Task:
You don't need to read input or print anything. Your task is to complete the function canReach() which takes a Integer N and a list A of size N as input and returns 1 if the end of the array is reachable, else return 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
0 <= A[i] <= 10^{5} | class Solution:
def canReach(self, A, N):
dp = [float("inf") for i in range(N)]
dp[0] = 1
for i in range(N):
for j in range(i, i + A[i] + 1):
dp[j] = min(dp[i] + 1, dp[j])
if j == N - 1:
return 1
if A[i] == 0 and dp[i + 1] == float("inf"):
return 0
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER FUNC_CALL VAR STRING RETURN NUMBER RETURN NUMBER |
Given an positive integer N and a list of N integers A[]. Each element in the array denotes the maximum length of jump you can cover. Find out if you can make it to the last index if you start at the first index of the list.
Example 1:
Input:
N = 6
A[] = {1, 2, 0, 3, 0, 0}
Output:
1
Explanation:
Jump 1 step from first index to
second index. Then jump 2 steps to reach
4_{th }index, and now jump 2 steps to reach
the end.
Example 2:
Input:
N = 3
A[] = {1, 0, 2}
Output:
0
Explanation:
You can't reach the end of the array.
Your Task:
You don't need to read input or print anything. Your task is to complete the function canReach() which takes a Integer N and a list A of size N as input and returns 1 if the end of the array is reachable, else return 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
0 <= A[i] <= 10^{5} | class Solution:
def canReach(self, A, N):
reachable = [(True) for _ in range(N)]
return 1 if self.helper(A, N, reachable) else 0
def helper(self, A, N, reachable, i=0):
if N - 1 <= i:
return True
if not reachable[i]:
return False
for jump in range(A[i], 0, -1):
if self.helper(A, N, reachable, i + jump):
return True
reachable[i] = False
return False | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER FUNC_DEF NUMBER IF BIN_OP VAR NUMBER VAR RETURN NUMBER IF VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER RETURN NUMBER |
Given an positive integer N and a list of N integers A[]. Each element in the array denotes the maximum length of jump you can cover. Find out if you can make it to the last index if you start at the first index of the list.
Example 1:
Input:
N = 6
A[] = {1, 2, 0, 3, 0, 0}
Output:
1
Explanation:
Jump 1 step from first index to
second index. Then jump 2 steps to reach
4_{th }index, and now jump 2 steps to reach
the end.
Example 2:
Input:
N = 3
A[] = {1, 0, 2}
Output:
0
Explanation:
You can't reach the end of the array.
Your Task:
You don't need to read input or print anything. Your task is to complete the function canReach() which takes a Integer N and a list A of size N as input and returns 1 if the end of the array is reachable, else return 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
0 <= A[i] <= 10^{5} | class Solution:
def canReach(self, A, N):
maxi = 0
if N == 1:
return 1
for i in range(N):
if A[i] != 0:
maxi = max(maxi, i + A[i])
if maxi >= N - 1:
return 1
elif maxi <= i:
return 0
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER |
Given an positive integer N and a list of N integers A[]. Each element in the array denotes the maximum length of jump you can cover. Find out if you can make it to the last index if you start at the first index of the list.
Example 1:
Input:
N = 6
A[] = {1, 2, 0, 3, 0, 0}
Output:
1
Explanation:
Jump 1 step from first index to
second index. Then jump 2 steps to reach
4_{th }index, and now jump 2 steps to reach
the end.
Example 2:
Input:
N = 3
A[] = {1, 0, 2}
Output:
0
Explanation:
You can't reach the end of the array.
Your Task:
You don't need to read input or print anything. Your task is to complete the function canReach() which takes a Integer N and a list A of size N as input and returns 1 if the end of the array is reachable, else return 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
0 <= A[i] <= 10^{5} | class Solution:
def canReach(self, nums, N):
if len(nums) == 1:
return 1
if nums[0] == 0:
return 0
dp = [0] * len(nums)
dp[0] = 1
for i in range(1, len(nums)):
for j in range(i):
if dp[j]:
if nums[j] >= i - j:
dp[i] = 1
break
return dp[-1] | CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER RETURN VAR NUMBER |
Given an positive integer N and a list of N integers A[]. Each element in the array denotes the maximum length of jump you can cover. Find out if you can make it to the last index if you start at the first index of the list.
Example 1:
Input:
N = 6
A[] = {1, 2, 0, 3, 0, 0}
Output:
1
Explanation:
Jump 1 step from first index to
second index. Then jump 2 steps to reach
4_{th }index, and now jump 2 steps to reach
the end.
Example 2:
Input:
N = 3
A[] = {1, 0, 2}
Output:
0
Explanation:
You can't reach the end of the array.
Your Task:
You don't need to read input or print anything. Your task is to complete the function canReach() which takes a Integer N and a list A of size N as input and returns 1 if the end of the array is reachable, else return 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
0 <= A[i] <= 10^{5} | class Solution:
def canReach(self, arr, n):
if n == 1:
return 1
if arr[0] == 0:
return 0
maxReach = arr[0]
jumps = 0
steps = arr[0]
for i in range(1, n - 1):
steps -= 1
maxReach = max(maxReach, i + arr[i])
if steps == 0:
jumps += 1
steps = maxReach - i
if steps == 0:
return 0
return 1 | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER RETURN NUMBER RETURN NUMBER |
Given an positive integer N and a list of N integers A[]. Each element in the array denotes the maximum length of jump you can cover. Find out if you can make it to the last index if you start at the first index of the list.
Example 1:
Input:
N = 6
A[] = {1, 2, 0, 3, 0, 0}
Output:
1
Explanation:
Jump 1 step from first index to
second index. Then jump 2 steps to reach
4_{th }index, and now jump 2 steps to reach
the end.
Example 2:
Input:
N = 3
A[] = {1, 0, 2}
Output:
0
Explanation:
You can't reach the end of the array.
Your Task:
You don't need to read input or print anything. Your task is to complete the function canReach() which takes a Integer N and a list A of size N as input and returns 1 if the end of the array is reachable, else return 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
0 <= A[i] <= 10^{5} | class Solution:
def canReach(self, A, N):
table = [(0) for x in range(N)]
table[0] = 1
maxReached = 1
for i in range(1, N + 1):
if table[i - 1] == 1 or i <= maxReached:
maxReached = max(i + A[i - 1], maxReached)
if N <= maxReached:
return 1
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR IF VAR VAR RETURN NUMBER RETURN NUMBER |
Given an positive integer N and a list of N integers A[]. Each element in the array denotes the maximum length of jump you can cover. Find out if you can make it to the last index if you start at the first index of the list.
Example 1:
Input:
N = 6
A[] = {1, 2, 0, 3, 0, 0}
Output:
1
Explanation:
Jump 1 step from first index to
second index. Then jump 2 steps to reach
4_{th }index, and now jump 2 steps to reach
the end.
Example 2:
Input:
N = 3
A[] = {1, 0, 2}
Output:
0
Explanation:
You can't reach the end of the array.
Your Task:
You don't need to read input or print anything. Your task is to complete the function canReach() which takes a Integer N and a list A of size N as input and returns 1 if the end of the array is reachable, else return 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
0 <= A[i] <= 10^{5} | class Solution:
def canReach(self, A, N):
end = 0
i = 0
while i <= end and i < N:
if end >= N - 1:
return 1
elif i + A[i] > end:
end = i + A[i]
i += 1
elif i + A[i] == end and A[i] == 0:
return 0
else:
i += 1 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER RETURN NUMBER IF BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER RETURN NUMBER VAR NUMBER |
Given an positive integer N and a list of N integers A[]. Each element in the array denotes the maximum length of jump you can cover. Find out if you can make it to the last index if you start at the first index of the list.
Example 1:
Input:
N = 6
A[] = {1, 2, 0, 3, 0, 0}
Output:
1
Explanation:
Jump 1 step from first index to
second index. Then jump 2 steps to reach
4_{th }index, and now jump 2 steps to reach
the end.
Example 2:
Input:
N = 3
A[] = {1, 0, 2}
Output:
0
Explanation:
You can't reach the end of the array.
Your Task:
You don't need to read input or print anything. Your task is to complete the function canReach() which takes a Integer N and a list A of size N as input and returns 1 if the end of the array is reachable, else return 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
0 <= A[i] <= 10^{5} | class Solution:
def f(self, A, ind, N, dp):
if ind >= N - 1:
return True
if ind + A[ind] == ind:
return False
res = False
if dp[ind] != -1:
return dp[ind]
for i in range(1, A[ind] + 1):
res = self.f(A, ind + i, N, dp) or res
dp[ind] = res
return dp[ind]
def canReach(self, A, N):
target = N - 1
while target != 0:
temp = target - 1
flag = True
while temp >= 0:
if temp + A[temp] >= target:
target = temp
flag = False
break
temp -= 1
if flag == True:
return 0
return 1 | CLASS_DEF FUNC_DEF IF VAR BIN_OP VAR NUMBER RETURN NUMBER IF BIN_OP VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR NUMBER IF VAR VAR NUMBER RETURN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER |
Given an positive integer N and a list of N integers A[]. Each element in the array denotes the maximum length of jump you can cover. Find out if you can make it to the last index if you start at the first index of the list.
Example 1:
Input:
N = 6
A[] = {1, 2, 0, 3, 0, 0}
Output:
1
Explanation:
Jump 1 step from first index to
second index. Then jump 2 steps to reach
4_{th }index, and now jump 2 steps to reach
the end.
Example 2:
Input:
N = 3
A[] = {1, 0, 2}
Output:
0
Explanation:
You can't reach the end of the array.
Your Task:
You don't need to read input or print anything. Your task is to complete the function canReach() which takes a Integer N and a list A of size N as input and returns 1 if the end of the array is reachable, else return 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
0 <= A[i] <= 10^{5} | class Solution:
def recur(self, i, N, A, dp):
if i >= N - 1:
return 1
if dp[i] != -1:
return dp[i]
for ind in range(1, min(A[i] + 1, N)):
if self.recur(i + ind, N, A, dp):
dp[i] = 1
return 1
return 0
def canReach(self, A, N):
if A[0] == 0 and N > 1:
return 0
tab = [0] * N
tab[N - 1] = 1
for i in range(N - 2, -1, -1):
for jump in range(1, min(A[i] + 1, N)):
if jump + i >= N - 1 or tab[i + jump] == 1:
tab[i] = 1
break
return tab[0] | CLASS_DEF FUNC_DEF IF VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR NUMBER NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR IF BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN VAR NUMBER |
Given an positive integer N and a list of N integers A[]. Each element in the array denotes the maximum length of jump you can cover. Find out if you can make it to the last index if you start at the first index of the list.
Example 1:
Input:
N = 6
A[] = {1, 2, 0, 3, 0, 0}
Output:
1
Explanation:
Jump 1 step from first index to
second index. Then jump 2 steps to reach
4_{th }index, and now jump 2 steps to reach
the end.
Example 2:
Input:
N = 3
A[] = {1, 0, 2}
Output:
0
Explanation:
You can't reach the end of the array.
Your Task:
You don't need to read input or print anything. Your task is to complete the function canReach() which takes a Integer N and a list A of size N as input and returns 1 if the end of the array is reachable, else return 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
0 <= A[i] <= 10^{5} | class Solution:
def canReach(self, a, n):
if a == [0]:
return 1
if a[0] == 0:
return 0
if n == 1:
return 1
i = n - 2
j = n - 1
while i >= 0:
if a[i] >= j - i:
j = i
if j == 0:
return 1
i = j - 1
else:
i -= 1
return 0 | CLASS_DEF FUNC_DEF IF VAR LIST NUMBER RETURN NUMBER IF VAR NUMBER NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER RETURN NUMBER |
Given an positive integer N and a list of N integers A[]. Each element in the array denotes the maximum length of jump you can cover. Find out if you can make it to the last index if you start at the first index of the list.
Example 1:
Input:
N = 6
A[] = {1, 2, 0, 3, 0, 0}
Output:
1
Explanation:
Jump 1 step from first index to
second index. Then jump 2 steps to reach
4_{th }index, and now jump 2 steps to reach
the end.
Example 2:
Input:
N = 3
A[] = {1, 0, 2}
Output:
0
Explanation:
You can't reach the end of the array.
Your Task:
You don't need to read input or print anything. Your task is to complete the function canReach() which takes a Integer N and a list A of size N as input and returns 1 if the end of the array is reachable, else return 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
0 <= A[i] <= 10^{5} | class Solution:
def canReach(self, a, n):
max_reach = a[0]
steps = a[0]
if n == 1:
return 1
elif a[0] == 0:
return 0
i = 1
while i < n:
if i == n - 1:
return 1
max_reach = max(max_reach, i + a[i])
steps -= 1
if steps == 0:
if max_reach == i:
return 0
steps = max_reach - i
i += 1
return -1 | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER RETURN NUMBER |
Given an positive integer N and a list of N integers A[]. Each element in the array denotes the maximum length of jump you can cover. Find out if you can make it to the last index if you start at the first index of the list.
Example 1:
Input:
N = 6
A[] = {1, 2, 0, 3, 0, 0}
Output:
1
Explanation:
Jump 1 step from first index to
second index. Then jump 2 steps to reach
4_{th }index, and now jump 2 steps to reach
the end.
Example 2:
Input:
N = 3
A[] = {1, 0, 2}
Output:
0
Explanation:
You can't reach the end of the array.
Your Task:
You don't need to read input or print anything. Your task is to complete the function canReach() which takes a Integer N and a list A of size N as input and returns 1 if the end of the array is reachable, else return 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
0 <= A[i] <= 10^{5} | class Solution:
def canReach(self, nums, N):
count = 0
distance = 0
curr_pos = 0
for i in range(len(nums) - 1):
distance = max(i + nums[i], distance)
if curr_pos == i:
curr_pos = distance
return 1 if curr_pos >= N - 1 else 0 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR BIN_OP VAR NUMBER NUMBER NUMBER |
Given an positive integer N and a list of N integers A[]. Each element in the array denotes the maximum length of jump you can cover. Find out if you can make it to the last index if you start at the first index of the list.
Example 1:
Input:
N = 6
A[] = {1, 2, 0, 3, 0, 0}
Output:
1
Explanation:
Jump 1 step from first index to
second index. Then jump 2 steps to reach
4_{th }index, and now jump 2 steps to reach
the end.
Example 2:
Input:
N = 3
A[] = {1, 0, 2}
Output:
0
Explanation:
You can't reach the end of the array.
Your Task:
You don't need to read input or print anything. Your task is to complete the function canReach() which takes a Integer N and a list A of size N as input and returns 1 if the end of the array is reachable, else return 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
0 <= A[i] <= 10^{5} | class Solution:
def canReach(self, arr, n):
energy = arr[0]
for i in range(1, n):
if energy == 0:
return 0
energy -= 1
if arr[i] > energy:
energy = arr[i]
return 1 | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER RETURN NUMBER VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR RETURN NUMBER |
Given an positive integer N and a list of N integers A[]. Each element in the array denotes the maximum length of jump you can cover. Find out if you can make it to the last index if you start at the first index of the list.
Example 1:
Input:
N = 6
A[] = {1, 2, 0, 3, 0, 0}
Output:
1
Explanation:
Jump 1 step from first index to
second index. Then jump 2 steps to reach
4_{th }index, and now jump 2 steps to reach
the end.
Example 2:
Input:
N = 3
A[] = {1, 0, 2}
Output:
0
Explanation:
You can't reach the end of the array.
Your Task:
You don't need to read input or print anything. Your task is to complete the function canReach() which takes a Integer N and a list A of size N as input and returns 1 if the end of the array is reachable, else return 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
0 <= A[i] <= 10^{5} | class Solution:
def canReach(self, a, n):
i = 0
if n == 1:
return 1
while i < n:
m = 0
if a[i] == 0:
return 0
for j in range(1, a[i] + 1):
if m < a[i + j] + i + j:
m = a[i + j] + i + j
m_ind = j
if m >= n - 1:
return 1
i = i + m_ind | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER RETURN NUMBER WHILE VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR 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 ASSIGN VAR VAR IF VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR |
Given an positive integer N and a list of N integers A[]. Each element in the array denotes the maximum length of jump you can cover. Find out if you can make it to the last index if you start at the first index of the list.
Example 1:
Input:
N = 6
A[] = {1, 2, 0, 3, 0, 0}
Output:
1
Explanation:
Jump 1 step from first index to
second index. Then jump 2 steps to reach
4_{th }index, and now jump 2 steps to reach
the end.
Example 2:
Input:
N = 3
A[] = {1, 0, 2}
Output:
0
Explanation:
You can't reach the end of the array.
Your Task:
You don't need to read input or print anything. Your task is to complete the function canReach() which takes a Integer N and a list A of size N as input and returns 1 if the end of the array is reachable, else return 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
0 <= A[i] <= 10^{5} | class Solution:
def canReach(self, A, N):
res, i = 0, 0
while i < N and i <= res:
res = max(res, i + A[i])
i += 1
if i == N:
return 1
else:
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER |
Given an positive integer N and a list of N integers A[]. Each element in the array denotes the maximum length of jump you can cover. Find out if you can make it to the last index if you start at the first index of the list.
Example 1:
Input:
N = 6
A[] = {1, 2, 0, 3, 0, 0}
Output:
1
Explanation:
Jump 1 step from first index to
second index. Then jump 2 steps to reach
4_{th }index, and now jump 2 steps to reach
the end.
Example 2:
Input:
N = 3
A[] = {1, 0, 2}
Output:
0
Explanation:
You can't reach the end of the array.
Your Task:
You don't need to read input or print anything. Your task is to complete the function canReach() which takes a Integer N and a list A of size N as input and returns 1 if the end of the array is reachable, else return 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
0 <= A[i] <= 10^{5} | class Solution:
def canReach(self, arr, n):
p = n - 1
for i in range(n - 1, -1, -1):
if i + arr[i] >= p:
p = i
return int(p == 0) | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR NUMBER |
Given an positive integer N and a list of N integers A[]. Each element in the array denotes the maximum length of jump you can cover. Find out if you can make it to the last index if you start at the first index of the list.
Example 1:
Input:
N = 6
A[] = {1, 2, 0, 3, 0, 0}
Output:
1
Explanation:
Jump 1 step from first index to
second index. Then jump 2 steps to reach
4_{th }index, and now jump 2 steps to reach
the end.
Example 2:
Input:
N = 3
A[] = {1, 0, 2}
Output:
0
Explanation:
You can't reach the end of the array.
Your Task:
You don't need to read input or print anything. Your task is to complete the function canReach() which takes a Integer N and a list A of size N as input and returns 1 if the end of the array is reachable, else return 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
0 <= A[i] <= 10^{5} | class Solution:
def canReach(self, A, N):
reach = A[0]
for pos in range(1, N):
if pos > reach:
return 0
elif reach >= N:
return 1
reach = max(reach, pos + A[pos])
return 1 | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR RETURN NUMBER |
Given an positive integer N and a list of N integers A[]. Each element in the array denotes the maximum length of jump you can cover. Find out if you can make it to the last index if you start at the first index of the list.
Example 1:
Input:
N = 6
A[] = {1, 2, 0, 3, 0, 0}
Output:
1
Explanation:
Jump 1 step from first index to
second index. Then jump 2 steps to reach
4_{th }index, and now jump 2 steps to reach
the end.
Example 2:
Input:
N = 3
A[] = {1, 0, 2}
Output:
0
Explanation:
You can't reach the end of the array.
Your Task:
You don't need to read input or print anything. Your task is to complete the function canReach() which takes a Integer N and a list A of size N as input and returns 1 if the end of the array is reachable, else return 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
0 <= A[i] <= 10^{5} | class Solution:
def canReach(self, A, N):
right_max = 0
end_max = N - 1
for i in range(N):
if i > right_max:
return 0
if A[i] + i > right_max:
right_max = A[i] + i
if right_max >= end_max:
return 1
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER IF BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER |
Given an positive integer N and a list of N integers A[]. Each element in the array denotes the maximum length of jump you can cover. Find out if you can make it to the last index if you start at the first index of the list.
Example 1:
Input:
N = 6
A[] = {1, 2, 0, 3, 0, 0}
Output:
1
Explanation:
Jump 1 step from first index to
second index. Then jump 2 steps to reach
4_{th }index, and now jump 2 steps to reach
the end.
Example 2:
Input:
N = 3
A[] = {1, 0, 2}
Output:
0
Explanation:
You can't reach the end of the array.
Your Task:
You don't need to read input or print anything. Your task is to complete the function canReach() which takes a Integer N and a list A of size N as input and returns 1 if the end of the array is reachable, else return 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
0 <= A[i] <= 10^{5} | class Solution:
def canReach(self, A, N):
jumps = [0] * N
jumps[0] = 1
for i in range(1, N):
for j in range(0, i):
if jumps[i]:
break
if A[j] + j >= i and jumps[j]:
jumps[i] = 1
if jumps[-1]:
return 1
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR IF BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER |
Given an positive integer N and a list of N integers A[]. Each element in the array denotes the maximum length of jump you can cover. Find out if you can make it to the last index if you start at the first index of the list.
Example 1:
Input:
N = 6
A[] = {1, 2, 0, 3, 0, 0}
Output:
1
Explanation:
Jump 1 step from first index to
second index. Then jump 2 steps to reach
4_{th }index, and now jump 2 steps to reach
the end.
Example 2:
Input:
N = 3
A[] = {1, 0, 2}
Output:
0
Explanation:
You can't reach the end of the array.
Your Task:
You don't need to read input or print anything. Your task is to complete the function canReach() which takes a Integer N and a list A of size N as input and returns 1 if the end of the array is reachable, else return 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
0 <= A[i] <= 10^{5} | class Solution:
def canReach(self, A, N):
currentMaxReach = A[0]
jump = 0
maxReachSoFar = A[0]
i = 0
while i < N:
if i <= currentMaxReach:
maxReachSoFar = max(maxReachSoFar, i + A[i])
if maxReachSoFar >= N - 1:
return 1
if currentMaxReach == i:
jump += 1
currentMaxReach = maxReachSoFar
i += 1
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER |
Given an positive integer N and a list of N integers A[]. Each element in the array denotes the maximum length of jump you can cover. Find out if you can make it to the last index if you start at the first index of the list.
Example 1:
Input:
N = 6
A[] = {1, 2, 0, 3, 0, 0}
Output:
1
Explanation:
Jump 1 step from first index to
second index. Then jump 2 steps to reach
4_{th }index, and now jump 2 steps to reach
the end.
Example 2:
Input:
N = 3
A[] = {1, 0, 2}
Output:
0
Explanation:
You can't reach the end of the array.
Your Task:
You don't need to read input or print anything. Your task is to complete the function canReach() which takes a Integer N and a list A of size N as input and returns 1 if the end of the array is reachable, else return 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
0 <= A[i] <= 10^{5} | class Solution:
def canReach(self, A, N):
jump = 1
step = A[0]
max_reach = A[0]
if step == 0:
if N == 1:
return 1
else:
return 0
for i in range(1, N):
step -= 1
max_reach = max(max_reach, A[i] + i)
if step == 0:
if i == N - 1:
return 1
jump += 1
if max_reach <= i:
return 0
step = max_reach - i
return 1 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR NUMBER IF VAR BIN_OP VAR NUMBER RETURN NUMBER VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR RETURN NUMBER |
Given an positive integer N and a list of N integers A[]. Each element in the array denotes the maximum length of jump you can cover. Find out if you can make it to the last index if you start at the first index of the list.
Example 1:
Input:
N = 6
A[] = {1, 2, 0, 3, 0, 0}
Output:
1
Explanation:
Jump 1 step from first index to
second index. Then jump 2 steps to reach
4_{th }index, and now jump 2 steps to reach
the end.
Example 2:
Input:
N = 3
A[] = {1, 0, 2}
Output:
0
Explanation:
You can't reach the end of the array.
Your Task:
You don't need to read input or print anything. Your task is to complete the function canReach() which takes a Integer N and a list A of size N as input and returns 1 if the end of the array is reachable, else return 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
0 <= A[i] <= 10^{5} | class Solution:
def canReach(self, A, N):
step = 0
jump = 0
for i in range(len(A)):
jump = max(jump, A[i] + i)
if i == step:
step = jump
if step >= len(A) - 1:
return 1
break
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN NUMBER |
Given an positive integer N and a list of N integers A[]. Each element in the array denotes the maximum length of jump you can cover. Find out if you can make it to the last index if you start at the first index of the list.
Example 1:
Input:
N = 6
A[] = {1, 2, 0, 3, 0, 0}
Output:
1
Explanation:
Jump 1 step from first index to
second index. Then jump 2 steps to reach
4_{th }index, and now jump 2 steps to reach
the end.
Example 2:
Input:
N = 3
A[] = {1, 0, 2}
Output:
0
Explanation:
You can't reach the end of the array.
Your Task:
You don't need to read input or print anything. Your task is to complete the function canReach() which takes a Integer N and a list A of size N as input and returns 1 if the end of the array is reachable, else return 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
0 <= A[i] <= 10^{5} | class Solution:
def canReach(self, A, N):
if len(A) == 0:
return 1
i = 0
maxReach = 0
while i <= maxReach and i < N:
maxReach = max(maxReach, i + A[i])
i += 1
if maxReach >= N - 1:
return 1
return 0 | CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER |
Given an positive integer N and a list of N integers A[]. Each element in the array denotes the maximum length of jump you can cover. Find out if you can make it to the last index if you start at the first index of the list.
Example 1:
Input:
N = 6
A[] = {1, 2, 0, 3, 0, 0}
Output:
1
Explanation:
Jump 1 step from first index to
second index. Then jump 2 steps to reach
4_{th }index, and now jump 2 steps to reach
the end.
Example 2:
Input:
N = 3
A[] = {1, 0, 2}
Output:
0
Explanation:
You can't reach the end of the array.
Your Task:
You don't need to read input or print anything. Your task is to complete the function canReach() which takes a Integer N and a list A of size N as input and returns 1 if the end of the array is reachable, else return 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
0 <= A[i] <= 10^{5} | class Solution:
def canReach(self, A, N):
jumps = 0
current = 0
maxiReachable = A[current]
maxiAsOfNow = A[current]
while True:
maxiAsOfNow = max(maxiAsOfNow, current + A[current])
if maxiAsOfNow >= N - 1:
return 1
if current == maxiReachable:
jumps += 1
if maxiAsOfNow <= maxiReachable:
return 0
maxiReachable = maxiAsOfNow
current += 1
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR WHILE NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER |
Given an positive integer N and a list of N integers A[]. Each element in the array denotes the maximum length of jump you can cover. Find out if you can make it to the last index if you start at the first index of the list.
Example 1:
Input:
N = 6
A[] = {1, 2, 0, 3, 0, 0}
Output:
1
Explanation:
Jump 1 step from first index to
second index. Then jump 2 steps to reach
4_{th }index, and now jump 2 steps to reach
the end.
Example 2:
Input:
N = 3
A[] = {1, 0, 2}
Output:
0
Explanation:
You can't reach the end of the array.
Your Task:
You don't need to read input or print anything. Your task is to complete the function canReach() which takes a Integer N and a list A of size N as input and returns 1 if the end of the array is reachable, else return 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
0 <= A[i] <= 10^{5} | class Solution:
def canReach(self, A, N):
curr = A[0]
if N == 1:
return 1
if curr == 0:
return 0
for i in range(1, N):
curr -= 1
if curr < A[i]:
curr = A[i]
elif curr == 0 and i < N - 1:
return 0
return 1 | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER |
Given an positive integer N and a list of N integers A[]. Each element in the array denotes the maximum length of jump you can cover. Find out if you can make it to the last index if you start at the first index of the list.
Example 1:
Input:
N = 6
A[] = {1, 2, 0, 3, 0, 0}
Output:
1
Explanation:
Jump 1 step from first index to
second index. Then jump 2 steps to reach
4_{th }index, and now jump 2 steps to reach
the end.
Example 2:
Input:
N = 3
A[] = {1, 0, 2}
Output:
0
Explanation:
You can't reach the end of the array.
Your Task:
You don't need to read input or print anything. Your task is to complete the function canReach() which takes a Integer N and a list A of size N as input and returns 1 if the end of the array is reachable, else return 0.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 <= N <= 10^{5}
0 <= A[i] <= 10^{5} | class Solution:
def canReach(self, A, N):
if A[0] == 0:
return 1
maxindex = 0
for i in range(N):
if i <= maxindex:
maxindex = max(maxindex, i + A[i])
else:
return 0
return 1 | CLASS_DEF FUNC_DEF IF VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR RETURN NUMBER RETURN NUMBER |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.