description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Given a String, find the longest palindromic subsequence.
Example 1:
Input:
S = "bbabcbcab"
Output: 7
Explanation: Subsequence "babcbab" is the
longest subsequence which is also a palindrome.
Example 2:
Input:
S = "abcd"
Output: 1
Explanation: "a", "b", "c" and "d" are
palindromic and all have a length 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestPalinSubseq() which takes the string S as input and returns an integer denoting the length of the longest palindromic subsequence of S.
Expected Time Complexity: O(|S|*|S|).
Expected Auxiliary Space: O(|S|*|S|).
Constraints:
1 ≤ |S| ≤ 1000 | class Solution:
def longestPalinSubseq(self, S):
n = len(S)
dp = [[(0) for _ in range(n)] for _ in range(n)]
max_len = 0
for i in range(n):
dp[i][i] = 1
if i and S[i] == S[i - 1]:
dp[i - 1][i] = 2
else:
dp[i - 1][i] = 1
for l in range(3, n + 1):
for j in range(0, n - l + 1):
if S[j] == S[j + l - 1]:
dp[j][j + l - 1] = dp[j + 1][j + l - 2] + 2
else:
dp[j][j + l - 1] = max(dp[j + 1][j + l - 1], dp[j][j + l - 2])
return dp[0][n - 1] | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR NUMBER BIN_OP VAR NUMBER |
Given a String, find the longest palindromic subsequence.
Example 1:
Input:
S = "bbabcbcab"
Output: 7
Explanation: Subsequence "babcbab" is the
longest subsequence which is also a palindrome.
Example 2:
Input:
S = "abcd"
Output: 1
Explanation: "a", "b", "c" and "d" are
palindromic and all have a length 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestPalinSubseq() which takes the string S as input and returns an integer denoting the length of the longest palindromic subsequence of S.
Expected Time Complexity: O(|S|*|S|).
Expected Auxiliary Space: O(|S|*|S|).
Constraints:
1 ≤ |S| ≤ 1000 | class Solution:
def longestPalinSubseq(self, S):
dp = [[(0) for j in range(len(S))] for i in range(len(S))]
for k in range(len(S)):
j = k
for i in range(len(S) - k):
if i == j:
dp[i][j] = 1
j += 1
continue
if s[i] == s[j]:
dp[i][j] = 2 + dp[i + 1][j - 1]
j += 1
continue
dp[i][j] = max(dp[i][j - 1], dp[i + 1][j])
j += 1
return dp[0][-1] | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER RETURN VAR NUMBER NUMBER |
Given a String, find the longest palindromic subsequence.
Example 1:
Input:
S = "bbabcbcab"
Output: 7
Explanation: Subsequence "babcbab" is the
longest subsequence which is also a palindrome.
Example 2:
Input:
S = "abcd"
Output: 1
Explanation: "a", "b", "c" and "d" are
palindromic and all have a length 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestPalinSubseq() which takes the string S as input and returns an integer denoting the length of the longest palindromic subsequence of S.
Expected Time Complexity: O(|S|*|S|).
Expected Auxiliary Space: O(|S|*|S|).
Constraints:
1 ≤ |S| ≤ 1000 | class Solution:
def longestPalinSubseq(self, S):
n = len(S)
m = len(S)
S2 = S[::-1]
x = self.lcs(n, m, S, S2)
return x
def lcs(self, x, y, s1, s2):
t = [[(-1) for i in range(y + 1)] for j in range(x + 1)]
for i in range(y + 1):
t[0][i] = 0
for j in range(x + 1):
t[j][0] = 0
for i in range(1, x + 1):
for j in range(1, y + 1):
if s1[i - 1] == s2[j - 1]:
t[i][j] = 1 + t[i - 1][j - 1]
else:
t[i][j] = max(t[i - 1][j], t[i][j - 1])
return t[x][y] | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR |
Given a String, find the longest palindromic subsequence.
Example 1:
Input:
S = "bbabcbcab"
Output: 7
Explanation: Subsequence "babcbab" is the
longest subsequence which is also a palindrome.
Example 2:
Input:
S = "abcd"
Output: 1
Explanation: "a", "b", "c" and "d" are
palindromic and all have a length 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestPalinSubseq() which takes the string S as input and returns an integer denoting the length of the longest palindromic subsequence of S.
Expected Time Complexity: O(|S|*|S|).
Expected Auxiliary Space: O(|S|*|S|).
Constraints:
1 ≤ |S| ≤ 1000 | class Solution:
def longestPalinSubseq(self, S):
n = len(S)
self.d = [([None] * n) for i in range(n)]
self.solve(S, S[::-1], 0, 0, len(S))
return self.d[0][0]
def solve(self, s, r, i, j, n):
if i == n or j == n:
return 0
if self.d[i][j] is not None:
return self.d[i][j]
if s[i] == r[j]:
self.d[i][j] = 1 + self.solve(s, r, i + 1, j + 1, n)
else:
self.d[i][j] = max(
self.solve(s, r, i + 1, j, n), self.solve(s, r, i, j + 1, n)
)
return self.d[i][j] | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NONE VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER FUNC_CALL VAR VAR RETURN VAR NUMBER NUMBER FUNC_DEF IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR NONE RETURN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR |
Given a String, find the longest palindromic subsequence.
Example 1:
Input:
S = "bbabcbcab"
Output: 7
Explanation: Subsequence "babcbab" is the
longest subsequence which is also a palindrome.
Example 2:
Input:
S = "abcd"
Output: 1
Explanation: "a", "b", "c" and "d" are
palindromic and all have a length 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestPalinSubseq() which takes the string S as input and returns an integer denoting the length of the longest palindromic subsequence of S.
Expected Time Complexity: O(|S|*|S|).
Expected Auxiliary Space: O(|S|*|S|).
Constraints:
1 ≤ |S| ≤ 1000 | class Solution:
def longestPalinSubseq(self, S):
def manu(s1, s2, i, j):
if i == len(s1) or j == len(s1):
return 0
if dp[i][j] != -1:
return dp[i][j]
if s1[i] == s2[j]:
k = 1 + manu(s1, s2, i + 1, j + 1)
else:
k = max(manu(s1, s2, i + 1, j), manu(s1, s2, i, j + 1))
dp[i][j] = k
return k
dp = [[(-1) for i in range(len(s))] for i in range(len(s))]
return manu(s, s[::-1], 0, 0) | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR NUMBER NUMBER NUMBER |
Given a String, find the longest palindromic subsequence.
Example 1:
Input:
S = "bbabcbcab"
Output: 7
Explanation: Subsequence "babcbab" is the
longest subsequence which is also a palindrome.
Example 2:
Input:
S = "abcd"
Output: 1
Explanation: "a", "b", "c" and "d" are
palindromic and all have a length 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestPalinSubseq() which takes the string S as input and returns an integer denoting the length of the longest palindromic subsequence of S.
Expected Time Complexity: O(|S|*|S|).
Expected Auxiliary Space: O(|S|*|S|).
Constraints:
1 ≤ |S| ≤ 1000 | import sys
class Solution:
def longestPalinSubseq(self, string: str) -> int:
dp = [None] * (len(string) + 1)
dp[0], dp[1] = [None, None, None], [0, None, None]
for i in range(1, len(string)):
for j in range(len(dp) - 3, -1, -1):
if dp[j] is None:
continue
start = dp[j][0] - 1 if j != 0 else i - 1
stop = dp[j + 2][0] if dp[j + 2] is not None else -1
for k in range(start, stop, -1):
if string[i] == string[k]:
dp[j + 2] = [k, i, dp[j]]
break
dp[1] = [i, None, None]
for i in range(len(dp) - 1, -1, -1):
if dp[i] is not None:
ans = i
break
dpData = dp[ans]
ansLeft = []
ansRight = []
while dpData is not None:
if dpData[0] is not None:
ansLeft.append(dpData[0])
if dpData[1] is not None:
ansRight.append(dpData[1])
dpData = dpData[2]
ansStringIndexes = ansLeft + ansRight[::-1]
ansString = "".join([string[i] for i in ansStringIndexes])
return ans | IMPORT CLASS_DEF FUNC_DEF VAR ASSIGN VAR BIN_OP LIST NONE BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER LIST NONE NONE NONE LIST NUMBER NONE NONE FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR NONE ASSIGN VAR VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NONE VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER LIST VAR VAR VAR VAR ASSIGN VAR NUMBER LIST VAR NONE NONE FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST WHILE VAR NONE IF VAR NUMBER NONE EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NONE EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL STRING VAR VAR VAR VAR RETURN VAR VAR |
Given a String, find the longest palindromic subsequence.
Example 1:
Input:
S = "bbabcbcab"
Output: 7
Explanation: Subsequence "babcbab" is the
longest subsequence which is also a palindrome.
Example 2:
Input:
S = "abcd"
Output: 1
Explanation: "a", "b", "c" and "d" are
palindromic and all have a length 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestPalinSubseq() which takes the string S as input and returns an integer denoting the length of the longest palindromic subsequence of S.
Expected Time Complexity: O(|S|*|S|).
Expected Auxiliary Space: O(|S|*|S|).
Constraints:
1 ≤ |S| ≤ 1000 | import sys
sys.setrecursionlimit(10**9)
class Solution:
def longestPalinSubseq(self, S):
d = dict()
def dfs(i, j):
if i > j:
return 0
if i == j:
return 1
if d.get((i, j)) != None:
return d[i, j]
if S[i] == S[j]:
d[i, j] = 2 + dfs(i + 1, j - 1)
return d[i, j]
d[i, j] = max(dfs(i + 1, j), dfs(i, j - 1))
return d[i, j]
return dfs(0, len(S) - 1) | IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR VAR NONE RETURN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR RETURN FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER |
Given a String, find the longest palindromic subsequence.
Example 1:
Input:
S = "bbabcbcab"
Output: 7
Explanation: Subsequence "babcbab" is the
longest subsequence which is also a palindrome.
Example 2:
Input:
S = "abcd"
Output: 1
Explanation: "a", "b", "c" and "d" are
palindromic and all have a length 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestPalinSubseq() which takes the string S as input and returns an integer denoting the length of the longest palindromic subsequence of S.
Expected Time Complexity: O(|S|*|S|).
Expected Auxiliary Space: O(|S|*|S|).
Constraints:
1 ≤ |S| ≤ 1000 | class Solution:
def longestPalinSubseq(self, a):
n = len(a)
b = a[::-1]
def fun(i, j):
if i == 0 or j == 0:
return int(a[i] == b[j])
if a[i] == b[j]:
return 1 + fun(i - 1, j - 1)
return max(fun(i, j - 1), fun(i - 1, j))
dp = [([0] * (n + 1)) for j in range(n + 1)]
for i in range(1, n + 1):
for j in range(1, n + 1):
if a[i - 1] == b[j - 1]:
dp[i][j] += 1 + dp[i - 1][j - 1]
else:
dp[i][j] = max(dp[i][j - 1], dp[i - 1][j])
return dp[n][n] | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR VAR RETURN BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR |
Given a String, find the longest palindromic subsequence.
Example 1:
Input:
S = "bbabcbcab"
Output: 7
Explanation: Subsequence "babcbab" is the
longest subsequence which is also a palindrome.
Example 2:
Input:
S = "abcd"
Output: 1
Explanation: "a", "b", "c" and "d" are
palindromic and all have a length 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestPalinSubseq() which takes the string S as input and returns an integer denoting the length of the longest palindromic subsequence of S.
Expected Time Complexity: O(|S|*|S|).
Expected Auxiliary Space: O(|S|*|S|).
Constraints:
1 ≤ |S| ≤ 1000 | class Solution:
def longestPalinSubseq(self, S):
if len(S) == 0:
return 0
dp = [[(-1) for _ in range(len(S))] for __ in range(len(S))]
return self.is_same(S, dp, 0, len(S) - 1)
def is_same(self, string, arr, start, end):
if start == end:
return 1
if start > end:
return 0
if arr[start][end] != -1:
return arr[start][end]
if string[start] == string[end]:
arr[start][end] = self.is_same(string, arr, start + 1, end - 1) + 2
else:
arr[start][end] = max(
self.is_same(string, arr, start, end - 1),
self.is_same(string, arr, start + 1, end),
)
return arr[start][end] | CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR |
Given a String, find the longest palindromic subsequence.
Example 1:
Input:
S = "bbabcbcab"
Output: 7
Explanation: Subsequence "babcbab" is the
longest subsequence which is also a palindrome.
Example 2:
Input:
S = "abcd"
Output: 1
Explanation: "a", "b", "c" and "d" are
palindromic and all have a length 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestPalinSubseq() which takes the string S as input and returns an integer denoting the length of the longest palindromic subsequence of S.
Expected Time Complexity: O(|S|*|S|).
Expected Auxiliary Space: O(|S|*|S|).
Constraints:
1 ≤ |S| ≤ 1000 | class Solution:
dp = []
def gen(self, s, l, r, c, flag):
if l == r:
return 1
if r - l == 1 and s[l] != s[r]:
return 1
if l > r:
return 0
if self.dp[l][r] != -1:
return self.dp[l][r]
if s[l] == s[r]:
c = self.gen(s, l + 1, r - 1, c, flag) + 2
p = self.gen(s, l + 1, r, 0, flag)
q = self.gen(s, l, r - 1, 0, flag)
self.dp[l][r] = max(p, q, c)
return max(p, q, c)
def longestPalinSubseq(self, S):
l = len(S)
self.dp = []
for i in range(l + 1):
b = []
for j in range(l + 1):
b.append(-1)
self.dp.append(b)
a = self.gen(S, 0, len(S) - 1, 0, 0)
return a | CLASS_DEF ASSIGN VAR LIST FUNC_DEF IF VAR VAR RETURN NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER RETURN VAR |
Given a String, find the longest palindromic subsequence.
Example 1:
Input:
S = "bbabcbcab"
Output: 7
Explanation: Subsequence "babcbab" is the
longest subsequence which is also a palindrome.
Example 2:
Input:
S = "abcd"
Output: 1
Explanation: "a", "b", "c" and "d" are
palindromic and all have a length 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestPalinSubseq() which takes the string S as input and returns an integer denoting the length of the longest palindromic subsequence of S.
Expected Time Complexity: O(|S|*|S|).
Expected Auxiliary Space: O(|S|*|S|).
Constraints:
1 ≤ |S| ≤ 1000 | class Solution:
def longestPalinSubseq(self, s):
dp = {}
def f(i, j, s1, s2):
if i < 0 or j < 0:
return 0
if (i, j) in dp:
return dp[i, j]
if s1[i] == s2[j]:
return 1 + f(i - 1, j - 1, s1, s2)
dp[i, j] = max(f(i - 1, j, s1, s2), f(i, j - 1, s1, s2))
return dp[i, j]
n = len(s)
return f(n - 1, n - 1, s, s[::-1]) | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR VAR RETURN VAR VAR VAR IF VAR VAR VAR VAR RETURN BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER |
Given a String, find the longest palindromic subsequence.
Example 1:
Input:
S = "bbabcbcab"
Output: 7
Explanation: Subsequence "babcbab" is the
longest subsequence which is also a palindrome.
Example 2:
Input:
S = "abcd"
Output: 1
Explanation: "a", "b", "c" and "d" are
palindromic and all have a length 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestPalinSubseq() which takes the string S as input and returns an integer denoting the length of the longest palindromic subsequence of S.
Expected Time Complexity: O(|S|*|S|).
Expected Auxiliary Space: O(|S|*|S|).
Constraints:
1 ≤ |S| ≤ 1000 | class Solution:
def longestPalinSubseq(self, S):
s = S[::-1]
m = len(S)
n = len(s)
dp = [([0] * (n + 1)) for _ in range(m + 1)]
ma = dp[0][0]
for i in range(1, n + 1):
for j in range(1, m + 1):
x = i
y = j
if m == 0 or n == 0:
dp[i][j] = 0
elif S[x - 1] == s[y - 1]:
dp[i][j] = 1 + dp[x - 1][y - 1]
else:
dp[i][j] = max(dp[x - 1][y], dp[x][y - 1])
return dp[m][n] | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR |
Given a String, find the longest palindromic subsequence.
Example 1:
Input:
S = "bbabcbcab"
Output: 7
Explanation: Subsequence "babcbab" is the
longest subsequence which is also a palindrome.
Example 2:
Input:
S = "abcd"
Output: 1
Explanation: "a", "b", "c" and "d" are
palindromic and all have a length 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestPalinSubseq() which takes the string S as input and returns an integer denoting the length of the longest palindromic subsequence of S.
Expected Time Complexity: O(|S|*|S|).
Expected Auxiliary Space: O(|S|*|S|).
Constraints:
1 ≤ |S| ≤ 1000 | class Solution:
def lcs(self, str1, str2):
lcsMatrix = [[(0) for i in range(len(str2) + 1)] for j in range(len(str1) + 1)]
for x in range(len(str1) + 1):
for y in range(len(str2) + 1):
if x == 0 or y == 0:
lcsMatrix[x][y] = 0
elif str1[x - 1] == str2[y - 1]:
lcsMatrix[x][y] = 1 + lcsMatrix[x - 1][y - 1]
else:
lcsMatrix[x][y] = max(lcsMatrix[x - 1][y], lcsMatrix[x][y - 1])
return lcsMatrix[len(str1)][len(str2)]
def longestPalinSubseq(self, S):
return self.lcs(S, S[::-1]) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR NUMBER |
Given a String, find the longest palindromic subsequence.
Example 1:
Input:
S = "bbabcbcab"
Output: 7
Explanation: Subsequence "babcbab" is the
longest subsequence which is also a palindrome.
Example 2:
Input:
S = "abcd"
Output: 1
Explanation: "a", "b", "c" and "d" are
palindromic and all have a length 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestPalinSubseq() which takes the string S as input and returns an integer denoting the length of the longest palindromic subsequence of S.
Expected Time Complexity: O(|S|*|S|).
Expected Auxiliary Space: O(|S|*|S|).
Constraints:
1 ≤ |S| ≤ 1000 | class Solution:
def longestPalinSubseq(self, S):
n = len(s)
dp = [([-1] * n) for i in range(n)]
def lcs(i, j, n, s1, s2):
if i >= n or j >= n:
return 0
if dp[i][j] != -1:
return dp[i][j]
inc = 0
if s1[i] == s2[j]:
inc = 1 + lcs(i + 1, j + 1, n, s1, s2)
inc = max(inc, lcs(i + 1, j, n, s1, s2))
inc = max(inc, lcs(i, j + 1, n, s1, s2))
dp[i][j] = inc
return inc
return lcs(0, 0, n, s, s[::-1]) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR NUMBER NUMBER VAR VAR VAR NUMBER |
Given a String, find the longest palindromic subsequence.
Example 1:
Input:
S = "bbabcbcab"
Output: 7
Explanation: Subsequence "babcbab" is the
longest subsequence which is also a palindrome.
Example 2:
Input:
S = "abcd"
Output: 1
Explanation: "a", "b", "c" and "d" are
palindromic and all have a length 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestPalinSubseq() which takes the string S as input and returns an integer denoting the length of the longest palindromic subsequence of S.
Expected Time Complexity: O(|S|*|S|).
Expected Auxiliary Space: O(|S|*|S|).
Constraints:
1 ≤ |S| ≤ 1000 | class Solution:
def solve(self, S1, S2, m, n, dp):
if m == 0 or n == 0:
return 0
ct = 0
for i in range(1, m + 1):
for j in range(1, n + 1):
if S1[i - 1] == S2[j - 1]:
dp[i][j] = 1 + dp[i - 1][j - 1]
ct = max(ct, dp[i][j])
else:
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
ct = max(ct, dp[i][j])
return ct
def longestPalinSubseq(self, S):
S1, S2 = S, ""
for i in range(len(S1) - 1, -1, -1):
S2 += S[i]
dp = [[(0) for i in range(len(S) + 1)] for j in range(len(S) + 1)]
a = self.solve(S1, S2, len(S1), len(S2), dp)
return a | CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR VAR STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given a String, find the longest palindromic subsequence.
Example 1:
Input:
S = "bbabcbcab"
Output: 7
Explanation: Subsequence "babcbab" is the
longest subsequence which is also a palindrome.
Example 2:
Input:
S = "abcd"
Output: 1
Explanation: "a", "b", "c" and "d" are
palindromic and all have a length 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestPalinSubseq() which takes the string S as input and returns an integer denoting the length of the longest palindromic subsequence of S.
Expected Time Complexity: O(|S|*|S|).
Expected Auxiliary Space: O(|S|*|S|).
Constraints:
1 ≤ |S| ≤ 1000 | class Solution:
def longestPalinSubseq(self, s):
s1 = s
s2 = s[::-1]
x = len(s1)
y = len(s2)
dp = [([0] * (y + 1)) for _ in range(x + 1)]
for i in range(x + 1):
for j in range(y + 1):
m, n = i, j
if m == 0 or n == 0:
pass
elif s1[m - 1] == s2[n - 1]:
dp[i][j] = 1 + dp[i - 1][j - 1]
else:
c1 = dp[i - 1][j]
c2 = dp[i][j - 1]
dp[i][j] = max(c1, c2)
return dp[x][y] | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR |
Given a String, find the longest palindromic subsequence.
Example 1:
Input:
S = "bbabcbcab"
Output: 7
Explanation: Subsequence "babcbab" is the
longest subsequence which is also a palindrome.
Example 2:
Input:
S = "abcd"
Output: 1
Explanation: "a", "b", "c" and "d" are
palindromic and all have a length 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestPalinSubseq() which takes the string S as input and returns an integer denoting the length of the longest palindromic subsequence of S.
Expected Time Complexity: O(|S|*|S|).
Expected Auxiliary Space: O(|S|*|S|).
Constraints:
1 ≤ |S| ≤ 1000 | class Solution:
def longestPalinSubseq(self, S):
def lcs(x, y, s1, s2):
l = [([0] * (y + 1)) for j in range(x + 1)]
for i in range(x + 1):
for j in range(y + 1):
if i == 0 or j == 0:
l[i][j] = 0
elif s1[i - 1] == s2[j - 1]:
l[i][j] = l[i - 1][j - 1] + 1
else:
l[i][j] = max(l[i - 1][j], l[i][j - 1])
return l[x][y]
S1 = S[::-1]
ans = lcs(len(S), len(S1), S, S1)
return ans | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR |
Given a String, find the longest palindromic subsequence.
Example 1:
Input:
S = "bbabcbcab"
Output: 7
Explanation: Subsequence "babcbab" is the
longest subsequence which is also a palindrome.
Example 2:
Input:
S = "abcd"
Output: 1
Explanation: "a", "b", "c" and "d" are
palindromic and all have a length 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestPalinSubseq() which takes the string S as input and returns an integer denoting the length of the longest palindromic subsequence of S.
Expected Time Complexity: O(|S|*|S|).
Expected Auxiliary Space: O(|S|*|S|).
Constraints:
1 ≤ |S| ≤ 1000 | class Solution:
def solve(self, x, y, s1, s2, i, j):
if i == x or j == y:
return 0
if s1[i] == s2[j]:
return 1 + self.solve(x, y, s1, s2, i + 1, j + 1)
else:
return max(
self.solve(x, y, s1, s2, i + 1, j), self.solve(x, y, s1, s2, i, j + 1)
)
def solve_memo(self, x, y, s1, s2, i, j, F):
if i == x or j == y:
return 0
if F[i][j] != -1:
return F[i][j]
if s1[i] == s2[j]:
F[i][j] = 1 + self.solve_memo(x, y, s1, s2, i + 1, j + 1, F)
else:
F[i][j] = max(
self.solve_memo(x, y, s1, s2, i + 1, j, F),
self.solve_memo(x, y, s1, s2, i, j + 1, F),
)
return F[i][j]
def solve_tab(self, x, y, s1, s2):
F = [[(0) for _ in range(y + 1)] for _ in range(x + 1)]
for i in range(x - 1, -1, -1):
for j in range(y - 1, -1, -1):
if s1[i] == s2[j]:
F[i][j] = 1 + F[i + 1][j + 1]
else:
F[i][j] = max(F[i + 1][j], F[i][j + 1])
return F[0][0]
def solve_tab2(self, x, y, s1, s2):
curr = [(0) for _ in range(y + 1)]
nextt = [(0) for _ in range(y + 1)]
for i in range(x - 1, -1, -1):
for j in range(y - 1, -1, -1):
if s1[i] == s2[j]:
curr[j] = 1 + nextt[j + 1]
else:
curr[j] = max(nextt[j], curr[j + 1])
nextt = curr[:]
return nextt[0]
def longestPalinSubseq(self, S):
rev_s = S[::-1]
x, y = len(S), len(rev_s)
F = [[(-1) for _ in range(y)] for _ in range(x)]
return self.solve_memo(x, y, S, rev_s, 0, 0, F) | CLASS_DEF FUNC_DEF IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR NUMBER NUMBER FUNC_DEF 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 NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR NUMBER FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR NUMBER NUMBER VAR |
Given a String, find the longest palindromic subsequence.
Example 1:
Input:
S = "bbabcbcab"
Output: 7
Explanation: Subsequence "babcbab" is the
longest subsequence which is also a palindrome.
Example 2:
Input:
S = "abcd"
Output: 1
Explanation: "a", "b", "c" and "d" are
palindromic and all have a length 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestPalinSubseq() which takes the string S as input and returns an integer denoting the length of the longest palindromic subsequence of S.
Expected Time Complexity: O(|S|*|S|).
Expected Auxiliary Space: O(|S|*|S|).
Constraints:
1 ≤ |S| ≤ 1000 | class Solution:
def lcs(self, s1, s2, x, y):
t = [[(-1) for _ in range(y + 1)] for _ in range(x + 1)]
for i in range(x + 1):
for j in range(y + 1):
if i == 0 or j == 0:
t[i][j] = 0
for i in range(1, x + 1):
for j in range(1, y + 1):
if s1[i - 1] == s2[j - 1]:
t[i][j] = 1 + t[i - 1][j - 1]
else:
t[i][j] = max(t[i][j - 1], t[i - 1][j])
return t[x][y]
def longestPalinSubseq(self, S):
x = len(S)
return self.lcs(S, S[::-1], x, x) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR NUMBER VAR VAR |
Given a String, find the longest palindromic subsequence.
Example 1:
Input:
S = "bbabcbcab"
Output: 7
Explanation: Subsequence "babcbab" is the
longest subsequence which is also a palindrome.
Example 2:
Input:
S = "abcd"
Output: 1
Explanation: "a", "b", "c" and "d" are
palindromic and all have a length 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestPalinSubseq() which takes the string S as input and returns an integer denoting the length of the longest palindromic subsequence of S.
Expected Time Complexity: O(|S|*|S|).
Expected Auxiliary Space: O(|S|*|S|).
Constraints:
1 ≤ |S| ≤ 1000 | class Solution:
def __init__(self):
self.res = ""
self.leng = 0
def longestPalinSubseq(self, s1):
def lcs(i1, i2, n1, n2, s1, s2, dp, s):
if i1 >= n1 or i2 >= n2:
return 0
if dp[i1][i2] != -1:
return dp[i1][i2]
m1 = -(10**9)
m2 = -(10**9)
if s1[i1] == s2[i2]:
return lcs(i1 + 1, i2 + 1, n1, n2, s1, s2, dp, s + s1[i1]) + 1
else:
m1 = lcs(i1 + 1, i2, n1, n2, s1, s2, dp, "")
m2 = lcs(i1, i2 + 1, n1, n2, s1, s2, dp, "")
dp[i1][i2] = max(m1, m2)
return dp[i1][i2]
n1 = len(s1)
s2 = s1[::-1]
dp = []
for i in range(n1):
dp.append([-1] * n1)
return lcs(0, 0, n1, n1, s1, s2, dp, "") | CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR NUMBER FUNC_DEF FUNC_DEF IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER IF VAR VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP LIST NUMBER VAR RETURN FUNC_CALL VAR NUMBER NUMBER VAR VAR VAR VAR VAR STRING |
Given a String, find the longest palindromic subsequence.
Example 1:
Input:
S = "bbabcbcab"
Output: 7
Explanation: Subsequence "babcbab" is the
longest subsequence which is also a palindrome.
Example 2:
Input:
S = "abcd"
Output: 1
Explanation: "a", "b", "c" and "d" are
palindromic and all have a length 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestPalinSubseq() which takes the string S as input and returns an integer denoting the length of the longest palindromic subsequence of S.
Expected Time Complexity: O(|S|*|S|).
Expected Auxiliary Space: O(|S|*|S|).
Constraints:
1 ≤ |S| ≤ 1000 | class Solution:
def longestPalinSubseq(self, S):
arr = [[(0) for a in range(len(S) + 1)] for b in range(len(S) + 1)]
st1 = S[::-1]
for i in range(len(S)):
for j in range(len(S)):
if st1[j] == S[i]:
arr[i + 1][j + 1] = 1 + arr[i][j]
else:
arr[i + 1][j + 1] = max(arr[i + 1][j], arr[i][j + 1])
return arr[-1][-1] | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR NUMBER NUMBER |
Given a String, find the longest palindromic subsequence.
Example 1:
Input:
S = "bbabcbcab"
Output: 7
Explanation: Subsequence "babcbab" is the
longest subsequence which is also a palindrome.
Example 2:
Input:
S = "abcd"
Output: 1
Explanation: "a", "b", "c" and "d" are
palindromic and all have a length 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestPalinSubseq() which takes the string S as input and returns an integer denoting the length of the longest palindromic subsequence of S.
Expected Time Complexity: O(|S|*|S|).
Expected Auxiliary Space: O(|S|*|S|).
Constraints:
1 ≤ |S| ≤ 1000 | class Solution:
def longestPalinSubseq(self, s):
dp = [[(0) for _ in range(len(s))] for _ in range(len(s))]
for k in range(1, len(s) + 1):
for i in range(len(s) - k + 1):
j = k + i - 1
if i == j:
dp[i][j] = 1
elif i + 1 == j and s[i] == s[j]:
dp[i][j] = 2
elif s[i] == s[j]:
dp[i][j] = dp[i + 1][j - 1] + 2
else:
dp[i][j] = max(dp[i + 1][j], dp[i][j - 1])
return dp[0][-1] | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR NUMBER NUMBER |
Given a String, find the longest palindromic subsequence.
Example 1:
Input:
S = "bbabcbcab"
Output: 7
Explanation: Subsequence "babcbab" is the
longest subsequence which is also a palindrome.
Example 2:
Input:
S = "abcd"
Output: 1
Explanation: "a", "b", "c" and "d" are
palindromic and all have a length 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestPalinSubseq() which takes the string S as input and returns an integer denoting the length of the longest palindromic subsequence of S.
Expected Time Complexity: O(|S|*|S|).
Expected Auxiliary Space: O(|S|*|S|).
Constraints:
1 ≤ |S| ≤ 1000 | class Solution:
def longestPalinSubseq(self, text1: str):
text2 = text1[::-1]
next = [(0) for col in range(len(text2) + 1)]
for i in range(len(text1) - 1, -1, -1):
curr = [(0) for row in range(len(text2) + 1)]
for j in range(len(text2) - 1, -1, -1):
ans = 0
if text1[i] == text2[j]:
ans = 1 + next[j + 1]
else:
ans = max(next[j], curr[j + 1])
curr[j] = ans
next = curr
return next[0] | CLASS_DEF FUNC_DEF VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR RETURN VAR NUMBER |
Given a String, find the longest palindromic subsequence.
Example 1:
Input:
S = "bbabcbcab"
Output: 7
Explanation: Subsequence "babcbab" is the
longest subsequence which is also a palindrome.
Example 2:
Input:
S = "abcd"
Output: 1
Explanation: "a", "b", "c" and "d" are
palindromic and all have a length 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestPalinSubseq() which takes the string S as input and returns an integer denoting the length of the longest palindromic subsequence of S.
Expected Time Complexity: O(|S|*|S|).
Expected Auxiliary Space: O(|S|*|S|).
Constraints:
1 ≤ |S| ≤ 1000 | class Solution:
def longestPalinSubseq(self, S):
lps = [[(0) for i in range(len(S))] for j in range(len(S))]
for gap in range(0, len(S)):
j = gap
i = 0
while i < len(S) and j < len(S):
if gap == 0:
lps[i][j] = 1
elif gap == 1:
if S[i] == S[j]:
lps[i][j] = 2
else:
lps[i][j] = 1
elif S[i] == S[j]:
lps[i][j] = lps[i + 1][j - 1] + 2
else:
lps[i][j] = max(lps[i][j - 1], lps[i + 1][j])
j += 1
i += 1
return lps[0][len(S) - 1] | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER RETURN VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER |
Given a String, find the longest palindromic subsequence.
Example 1:
Input:
S = "bbabcbcab"
Output: 7
Explanation: Subsequence "babcbab" is the
longest subsequence which is also a palindrome.
Example 2:
Input:
S = "abcd"
Output: 1
Explanation: "a", "b", "c" and "d" are
palindromic and all have a length 1.
Your Task:
You don't need to read input or print anything. Your task is to complete the function longestPalinSubseq() which takes the string S as input and returns an integer denoting the length of the longest palindromic subsequence of S.
Expected Time Complexity: O(|S|*|S|).
Expected Auxiliary Space: O(|S|*|S|).
Constraints:
1 ≤ |S| ≤ 1000 | class Solution:
def longestPalinSubseq(self, S):
n = len(S)
temp = ""
for i in range(n - 1, -1, -1):
temp += S[i]
dp = [[(0) for _ in range(n + 1)] for _ in range(n + 1)]
for i in range(n + 1):
for j in range(n + 1):
if i == 0 or j == 0:
dp[i][j] = 0
elif S[i - 1] == temp[j - 1]:
dp[i][j] = 1 + dp[i - 1][j - 1]
else:
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
ans = 0
for i in range(n + 1):
for j in range(n + 1):
ans = max(ans, dp[i][j])
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR |
This is the easy version of the problem. The difference between the versions is that the easy version does not require you to output the numbers of the rods to be removed. You can make hacks only if all versions of the problem are solved.
Stitch likes experimenting with different machines with his friend Sparky. Today they built another machine.
The main element of this machine are $n$ rods arranged along one straight line and numbered from $1$ to $n$ inclusive. Each of these rods must carry an electric charge quantitatively equal to either $1$ or $-1$ (otherwise the machine will not work). Another condition for this machine to work is that the sign-variable sum of the charge on all rods must be zero.
More formally, the rods can be represented as an array of $n$ numbers characterizing the charge: either $1$ or $-1$. Then the condition must hold: $a_1 - a_2 + a_3 - a_4 + \ldots = 0$, or $\sum\limits_{i=1}^n (-1)^{i-1} \cdot a_i = 0$.
Sparky charged all $n$ rods with an electric current, but unfortunately it happened that the rods were not charged correctly (the sign-variable sum of the charge is not zero). The friends decided to leave only some of the rods in the machine. Sparky has $q$ questions. In the $i$th question Sparky asks: if the machine consisted only of rods with numbers $l_i$ to $r_i$ inclusive, what minimal number of rods could be removed from the machine so that the sign-variable sum of charges on the remaining ones would be zero? Perhaps the friends got something wrong, and the sign-variable sum is already zero. In that case, you don't have to remove the rods at all.
If the number of rods is zero, we will assume that the sign-variable sum of charges is zero, that is, we can always remove all rods.
Help your friends and answer all of Sparky's questions!
-----Input-----
Each test contains multiple test cases.
The first line contains one positive integer $t$ ($1 \le t \le 10^3$), denoting the number of test cases. Description of the test cases follows.
The first line of each test case contains two positive integers $n$ and $q$ ($1 \le n, q \le 3 \cdot 10^5$) — the number of rods and the number of questions.
The second line of each test case contains a non-empty string $s$ of length $n$, where the charge of the $i$-th rod is $1$ if $s_i$ is the "+" symbol, or $-1$ if $s_i$ is the "-" symbol.
Each next line from the next $q$ lines contains two positive integers $l_i$ ans $r_i$ ($1 \le l_i \le r_i \le n$) — numbers, describing Sparky's questions.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$, and the sum of $q$ over all test cases does not exceed $3 \cdot 10^5$.
-----Output-----
For each test case, print a single integer — the minimal number of rods that can be removed.
-----Examples-----
Input
3
14 1
+--++---++-++-
1 14
14 3
+--++---+++---
1 14
6 12
3 10
4 10
+-+-
1 1
1 2
1 3
1 4
2 2
2 3
2 4
3 3
3 4
4 4
Output
2
2
1
0
1
2
1
2
1
2
1
1
2
1
-----Note-----
In the first test case for the first query you can remove the rods numbered $5$ and $8$, then the following set of rods will remain: +--+--++-++-. It is easy to see that here the sign-variable sum is zero.
In the second test case:
For the first query, we can remove the rods numbered $1$ and $11$, then the following set of rods will remain: --++---++---. It is easy to see that here the sign-variable sum is zero.
For the second query we can remove the rod numbered $9$, then the following set of rods will remain: ---++-. It is easy to see that here the variable sum is zero.
For the third query we can not remove the rods at all. | import sys
def get_ints():
return map(int, sys.stdin.readline().strip().split())
def get_list():
return list(map(int, sys.stdin.readline().strip().split()))
def get_string():
return sys.stdin.readline().strip()
def get_int():
return int(sys.stdin.readline().strip())
def get_list_strings():
return list(map(str, sys.stdin.readline().strip().split()))
def solve(n, s, q, arr):
store = []
for i, ele in enumerate(s):
if ele == "+":
val = 1
else:
val = -1
if i % 2 == 1:
val *= -1
store.append(val)
final = [0]
final.extend(store)
for i in range(1, n + 1):
final[i] += final[i - 1]
for ele in arr:
left = ele[0]
right = ele[1]
if final[right] == final[left - 1]:
print(0)
continue
l = right - left + 1
if l % 2 == 1:
print(1)
else:
print(2)
T = get_int()
while T:
n, q = get_ints()
s = get_string()
tempQ = q
arr = []
while tempQ:
li, ri = get_ints()
arr.append([li, ri])
tempQ -= 1
solve(n, s, q, arr)
T -= 1 | IMPORT FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR LIST WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER |
This is the easy version of the problem. The difference between the versions is that the easy version does not require you to output the numbers of the rods to be removed. You can make hacks only if all versions of the problem are solved.
Stitch likes experimenting with different machines with his friend Sparky. Today they built another machine.
The main element of this machine are $n$ rods arranged along one straight line and numbered from $1$ to $n$ inclusive. Each of these rods must carry an electric charge quantitatively equal to either $1$ or $-1$ (otherwise the machine will not work). Another condition for this machine to work is that the sign-variable sum of the charge on all rods must be zero.
More formally, the rods can be represented as an array of $n$ numbers characterizing the charge: either $1$ or $-1$. Then the condition must hold: $a_1 - a_2 + a_3 - a_4 + \ldots = 0$, or $\sum\limits_{i=1}^n (-1)^{i-1} \cdot a_i = 0$.
Sparky charged all $n$ rods with an electric current, but unfortunately it happened that the rods were not charged correctly (the sign-variable sum of the charge is not zero). The friends decided to leave only some of the rods in the machine. Sparky has $q$ questions. In the $i$th question Sparky asks: if the machine consisted only of rods with numbers $l_i$ to $r_i$ inclusive, what minimal number of rods could be removed from the machine so that the sign-variable sum of charges on the remaining ones would be zero? Perhaps the friends got something wrong, and the sign-variable sum is already zero. In that case, you don't have to remove the rods at all.
If the number of rods is zero, we will assume that the sign-variable sum of charges is zero, that is, we can always remove all rods.
Help your friends and answer all of Sparky's questions!
-----Input-----
Each test contains multiple test cases.
The first line contains one positive integer $t$ ($1 \le t \le 10^3$), denoting the number of test cases. Description of the test cases follows.
The first line of each test case contains two positive integers $n$ and $q$ ($1 \le n, q \le 3 \cdot 10^5$) — the number of rods and the number of questions.
The second line of each test case contains a non-empty string $s$ of length $n$, where the charge of the $i$-th rod is $1$ if $s_i$ is the "+" symbol, or $-1$ if $s_i$ is the "-" symbol.
Each next line from the next $q$ lines contains two positive integers $l_i$ ans $r_i$ ($1 \le l_i \le r_i \le n$) — numbers, describing Sparky's questions.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$, and the sum of $q$ over all test cases does not exceed $3 \cdot 10^5$.
-----Output-----
For each test case, print a single integer — the minimal number of rods that can be removed.
-----Examples-----
Input
3
14 1
+--++---++-++-
1 14
14 3
+--++---+++---
1 14
6 12
3 10
4 10
+-+-
1 1
1 2
1 3
1 4
2 2
2 3
2 4
3 3
3 4
4 4
Output
2
2
1
0
1
2
1
2
1
2
1
1
2
1
-----Note-----
In the first test case for the first query you can remove the rods numbered $5$ and $8$, then the following set of rods will remain: +--+--++-++-. It is easy to see that here the sign-variable sum is zero.
In the second test case:
For the first query, we can remove the rods numbered $1$ and $11$, then the following set of rods will remain: --++---++---. It is easy to see that here the sign-variable sum is zero.
For the second query we can remove the rod numbered $9$, then the following set of rods will remain: ---++-. It is easy to see that here the variable sum is zero.
For the third query we can not remove the rods at all. | from sys import stdin
input = stdin.readline
def answer():
pos, neg = [0], [0]
for i in range(n):
pos.append(pos[-1])
neg.append(neg[-1])
if i & 1 == 0:
if s[i] == "+":
pos[-1] += 1
else:
neg[-1] += 1
elif s[i] == "-":
pos[-1] += 1
else:
neg[-1] += 1
for i in range(m):
l, r = map(int, input().split())
positive = pos[r] - pos[l - 1]
negitive = neg[r] - neg[l - 1]
if positive - negitive == 0:
print(0)
elif l & 1 == r & 1:
print(1)
else:
print(2)
for T in range(int(input())):
n, m = map(int, input().split())
s = input().strip()
answer() | ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR LIST NUMBER LIST NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING VAR NUMBER NUMBER VAR NUMBER NUMBER IF VAR VAR STRING VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR |
This is the easy version of the problem. The difference between the versions is that the easy version does not require you to output the numbers of the rods to be removed. You can make hacks only if all versions of the problem are solved.
Stitch likes experimenting with different machines with his friend Sparky. Today they built another machine.
The main element of this machine are $n$ rods arranged along one straight line and numbered from $1$ to $n$ inclusive. Each of these rods must carry an electric charge quantitatively equal to either $1$ or $-1$ (otherwise the machine will not work). Another condition for this machine to work is that the sign-variable sum of the charge on all rods must be zero.
More formally, the rods can be represented as an array of $n$ numbers characterizing the charge: either $1$ or $-1$. Then the condition must hold: $a_1 - a_2 + a_3 - a_4 + \ldots = 0$, or $\sum\limits_{i=1}^n (-1)^{i-1} \cdot a_i = 0$.
Sparky charged all $n$ rods with an electric current, but unfortunately it happened that the rods were not charged correctly (the sign-variable sum of the charge is not zero). The friends decided to leave only some of the rods in the machine. Sparky has $q$ questions. In the $i$th question Sparky asks: if the machine consisted only of rods with numbers $l_i$ to $r_i$ inclusive, what minimal number of rods could be removed from the machine so that the sign-variable sum of charges on the remaining ones would be zero? Perhaps the friends got something wrong, and the sign-variable sum is already zero. In that case, you don't have to remove the rods at all.
If the number of rods is zero, we will assume that the sign-variable sum of charges is zero, that is, we can always remove all rods.
Help your friends and answer all of Sparky's questions!
-----Input-----
Each test contains multiple test cases.
The first line contains one positive integer $t$ ($1 \le t \le 10^3$), denoting the number of test cases. Description of the test cases follows.
The first line of each test case contains two positive integers $n$ and $q$ ($1 \le n, q \le 3 \cdot 10^5$) — the number of rods and the number of questions.
The second line of each test case contains a non-empty string $s$ of length $n$, where the charge of the $i$-th rod is $1$ if $s_i$ is the "+" symbol, or $-1$ if $s_i$ is the "-" symbol.
Each next line from the next $q$ lines contains two positive integers $l_i$ ans $r_i$ ($1 \le l_i \le r_i \le n$) — numbers, describing Sparky's questions.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$, and the sum of $q$ over all test cases does not exceed $3 \cdot 10^5$.
-----Output-----
For each test case, print a single integer — the minimal number of rods that can be removed.
-----Examples-----
Input
3
14 1
+--++---++-++-
1 14
14 3
+--++---+++---
1 14
6 12
3 10
4 10
+-+-
1 1
1 2
1 3
1 4
2 2
2 3
2 4
3 3
3 4
4 4
Output
2
2
1
0
1
2
1
2
1
2
1
1
2
1
-----Note-----
In the first test case for the first query you can remove the rods numbered $5$ and $8$, then the following set of rods will remain: +--+--++-++-. It is easy to see that here the sign-variable sum is zero.
In the second test case:
For the first query, we can remove the rods numbered $1$ and $11$, then the following set of rods will remain: --++---++---. It is easy to see that here the sign-variable sum is zero.
For the second query we can remove the rod numbered $9$, then the following set of rods will remain: ---++-. It is easy to see that here the variable sum is zero.
For the third query we can not remove the rods at all. | import sys
input = sys.stdin.readline
def solve():
n, q = map(int, input().split())
s = input()
l = [0] * n
for i in range(n):
if s[i] == "+":
l[i] = 1
else:
l[i] = -1
ssum1 = [0] * (n + 1)
ssum1[1] = l[0]
for i in range(1, n):
ssum1[i + 1] = ssum1[i] + l[i] * (-1) ** i
for _ in range(q):
l, r = map(int, input().split())
k = ssum1[r] - ssum1[l - 1]
print(0 if k == 0 else 2 if k % 2 == 0 else 1)
t = int(input())
for i in range(t):
solve() | IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
This is the easy version of the problem. The difference between the versions is that the easy version does not require you to output the numbers of the rods to be removed. You can make hacks only if all versions of the problem are solved.
Stitch likes experimenting with different machines with his friend Sparky. Today they built another machine.
The main element of this machine are $n$ rods arranged along one straight line and numbered from $1$ to $n$ inclusive. Each of these rods must carry an electric charge quantitatively equal to either $1$ or $-1$ (otherwise the machine will not work). Another condition for this machine to work is that the sign-variable sum of the charge on all rods must be zero.
More formally, the rods can be represented as an array of $n$ numbers characterizing the charge: either $1$ or $-1$. Then the condition must hold: $a_1 - a_2 + a_3 - a_4 + \ldots = 0$, or $\sum\limits_{i=1}^n (-1)^{i-1} \cdot a_i = 0$.
Sparky charged all $n$ rods with an electric current, but unfortunately it happened that the rods were not charged correctly (the sign-variable sum of the charge is not zero). The friends decided to leave only some of the rods in the machine. Sparky has $q$ questions. In the $i$th question Sparky asks: if the machine consisted only of rods with numbers $l_i$ to $r_i$ inclusive, what minimal number of rods could be removed from the machine so that the sign-variable sum of charges on the remaining ones would be zero? Perhaps the friends got something wrong, and the sign-variable sum is already zero. In that case, you don't have to remove the rods at all.
If the number of rods is zero, we will assume that the sign-variable sum of charges is zero, that is, we can always remove all rods.
Help your friends and answer all of Sparky's questions!
-----Input-----
Each test contains multiple test cases.
The first line contains one positive integer $t$ ($1 \le t \le 10^3$), denoting the number of test cases. Description of the test cases follows.
The first line of each test case contains two positive integers $n$ and $q$ ($1 \le n, q \le 3 \cdot 10^5$) — the number of rods and the number of questions.
The second line of each test case contains a non-empty string $s$ of length $n$, where the charge of the $i$-th rod is $1$ if $s_i$ is the "+" symbol, or $-1$ if $s_i$ is the "-" symbol.
Each next line from the next $q$ lines contains two positive integers $l_i$ ans $r_i$ ($1 \le l_i \le r_i \le n$) — numbers, describing Sparky's questions.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$, and the sum of $q$ over all test cases does not exceed $3 \cdot 10^5$.
-----Output-----
For each test case, print a single integer — the minimal number of rods that can be removed.
-----Examples-----
Input
3
14 1
+--++---++-++-
1 14
14 3
+--++---+++---
1 14
6 12
3 10
4 10
+-+-
1 1
1 2
1 3
1 4
2 2
2 3
2 4
3 3
3 4
4 4
Output
2
2
1
0
1
2
1
2
1
2
1
1
2
1
-----Note-----
In the first test case for the first query you can remove the rods numbered $5$ and $8$, then the following set of rods will remain: +--+--++-++-. It is easy to see that here the sign-variable sum is zero.
In the second test case:
For the first query, we can remove the rods numbered $1$ and $11$, then the following set of rods will remain: --++---++---. It is easy to see that here the sign-variable sum is zero.
For the second query we can remove the rod numbered $9$, then the following set of rods will remain: ---++-. It is easy to see that here the variable sum is zero.
For the third query we can not remove the rods at all. | I = lambda: map(int, input().split())
(k,) = I()
for i in range(k):
n, q = I()
s, a, y, z, o, p, r = input(), [], 1, {(0): (0, 0)}, 0, 0, ""
for i in range(n):
if s[i] == "+":
a += [y]
else:
a += [-1 * y]
y *= -1
for i in range(1, n + 1):
if a[i - 1] == 1:
o += 1
else:
p += 1
z[i] = o, p
for i in range(q):
c, v = I()
w = abs(z[v][0] - z[c - 1][0] - z[v][1] + z[c - 1][1])
r += str(w if w == 0 else 1 if w % 2 else 2) + "\n"
print(r) | ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR LIST NUMBER DICT NUMBER NUMBER NUMBER NUMBER NUMBER STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR LIST VAR VAR LIST BIN_OP NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER STRING EXPR FUNC_CALL VAR VAR |
This is the easy version of the problem. The difference between the versions is that the easy version does not require you to output the numbers of the rods to be removed. You can make hacks only if all versions of the problem are solved.
Stitch likes experimenting with different machines with his friend Sparky. Today they built another machine.
The main element of this machine are $n$ rods arranged along one straight line and numbered from $1$ to $n$ inclusive. Each of these rods must carry an electric charge quantitatively equal to either $1$ or $-1$ (otherwise the machine will not work). Another condition for this machine to work is that the sign-variable sum of the charge on all rods must be zero.
More formally, the rods can be represented as an array of $n$ numbers characterizing the charge: either $1$ or $-1$. Then the condition must hold: $a_1 - a_2 + a_3 - a_4 + \ldots = 0$, or $\sum\limits_{i=1}^n (-1)^{i-1} \cdot a_i = 0$.
Sparky charged all $n$ rods with an electric current, but unfortunately it happened that the rods were not charged correctly (the sign-variable sum of the charge is not zero). The friends decided to leave only some of the rods in the machine. Sparky has $q$ questions. In the $i$th question Sparky asks: if the machine consisted only of rods with numbers $l_i$ to $r_i$ inclusive, what minimal number of rods could be removed from the machine so that the sign-variable sum of charges on the remaining ones would be zero? Perhaps the friends got something wrong, and the sign-variable sum is already zero. In that case, you don't have to remove the rods at all.
If the number of rods is zero, we will assume that the sign-variable sum of charges is zero, that is, we can always remove all rods.
Help your friends and answer all of Sparky's questions!
-----Input-----
Each test contains multiple test cases.
The first line contains one positive integer $t$ ($1 \le t \le 10^3$), denoting the number of test cases. Description of the test cases follows.
The first line of each test case contains two positive integers $n$ and $q$ ($1 \le n, q \le 3 \cdot 10^5$) — the number of rods and the number of questions.
The second line of each test case contains a non-empty string $s$ of length $n$, where the charge of the $i$-th rod is $1$ if $s_i$ is the "+" symbol, or $-1$ if $s_i$ is the "-" symbol.
Each next line from the next $q$ lines contains two positive integers $l_i$ ans $r_i$ ($1 \le l_i \le r_i \le n$) — numbers, describing Sparky's questions.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$, and the sum of $q$ over all test cases does not exceed $3 \cdot 10^5$.
-----Output-----
For each test case, print a single integer — the minimal number of rods that can be removed.
-----Examples-----
Input
3
14 1
+--++---++-++-
1 14
14 3
+--++---+++---
1 14
6 12
3 10
4 10
+-+-
1 1
1 2
1 3
1 4
2 2
2 3
2 4
3 3
3 4
4 4
Output
2
2
1
0
1
2
1
2
1
2
1
1
2
1
-----Note-----
In the first test case for the first query you can remove the rods numbered $5$ and $8$, then the following set of rods will remain: +--+--++-++-. It is easy to see that here the sign-variable sum is zero.
In the second test case:
For the first query, we can remove the rods numbered $1$ and $11$, then the following set of rods will remain: --++---++---. It is easy to see that here the sign-variable sum is zero.
For the second query we can remove the rod numbered $9$, then the following set of rods will remain: ---++-. It is easy to see that here the variable sum is zero.
For the third query we can not remove the rods at all. | import sys
from itertools import accumulate
input = sys.stdin.buffer.readline
for _ in range(int(input())):
n, q = map(int, input().split())
arr = list(input())
for i in range(n):
arr[i] = chr(arr[i])
for i in range(n):
if arr[i] == "+":
arr[i] = 1
else:
arr[i] = -1
for i in range(n):
if (i - n) % 2 == 0:
arr[i] = arr[i] * 1
else:
arr[i] = arr[i] * -1
brr = list(accumulate(arr))
for i in range(q):
l, r = map(int, input().split())
r -= 1
l -= 1
c = 0
if l == 0:
c = brr[r]
else:
c = brr[r] - brr[l - 1]
if c == 0:
print(0)
elif c % 2 == 1:
print(1)
else:
print(2) | IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
This is the easy version of the problem. The difference between the versions is that the easy version does not require you to output the numbers of the rods to be removed. You can make hacks only if all versions of the problem are solved.
Stitch likes experimenting with different machines with his friend Sparky. Today they built another machine.
The main element of this machine are $n$ rods arranged along one straight line and numbered from $1$ to $n$ inclusive. Each of these rods must carry an electric charge quantitatively equal to either $1$ or $-1$ (otherwise the machine will not work). Another condition for this machine to work is that the sign-variable sum of the charge on all rods must be zero.
More formally, the rods can be represented as an array of $n$ numbers characterizing the charge: either $1$ or $-1$. Then the condition must hold: $a_1 - a_2 + a_3 - a_4 + \ldots = 0$, or $\sum\limits_{i=1}^n (-1)^{i-1} \cdot a_i = 0$.
Sparky charged all $n$ rods with an electric current, but unfortunately it happened that the rods were not charged correctly (the sign-variable sum of the charge is not zero). The friends decided to leave only some of the rods in the machine. Sparky has $q$ questions. In the $i$th question Sparky asks: if the machine consisted only of rods with numbers $l_i$ to $r_i$ inclusive, what minimal number of rods could be removed from the machine so that the sign-variable sum of charges on the remaining ones would be zero? Perhaps the friends got something wrong, and the sign-variable sum is already zero. In that case, you don't have to remove the rods at all.
If the number of rods is zero, we will assume that the sign-variable sum of charges is zero, that is, we can always remove all rods.
Help your friends and answer all of Sparky's questions!
-----Input-----
Each test contains multiple test cases.
The first line contains one positive integer $t$ ($1 \le t \le 10^3$), denoting the number of test cases. Description of the test cases follows.
The first line of each test case contains two positive integers $n$ and $q$ ($1 \le n, q \le 3 \cdot 10^5$) — the number of rods and the number of questions.
The second line of each test case contains a non-empty string $s$ of length $n$, where the charge of the $i$-th rod is $1$ if $s_i$ is the "+" symbol, or $-1$ if $s_i$ is the "-" symbol.
Each next line from the next $q$ lines contains two positive integers $l_i$ ans $r_i$ ($1 \le l_i \le r_i \le n$) — numbers, describing Sparky's questions.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$, and the sum of $q$ over all test cases does not exceed $3 \cdot 10^5$.
-----Output-----
For each test case, print a single integer — the minimal number of rods that can be removed.
-----Examples-----
Input
3
14 1
+--++---++-++-
1 14
14 3
+--++---+++---
1 14
6 12
3 10
4 10
+-+-
1 1
1 2
1 3
1 4
2 2
2 3
2 4
3 3
3 4
4 4
Output
2
2
1
0
1
2
1
2
1
2
1
1
2
1
-----Note-----
In the first test case for the first query you can remove the rods numbered $5$ and $8$, then the following set of rods will remain: +--+--++-++-. It is easy to see that here the sign-variable sum is zero.
In the second test case:
For the first query, we can remove the rods numbered $1$ and $11$, then the following set of rods will remain: --++---++---. It is easy to see that here the sign-variable sum is zero.
For the second query we can remove the rod numbered $9$, then the following set of rods will remain: ---++-. It is easy to see that here the variable sum is zero.
For the third query we can not remove the rods at all. | import sys
input = sys.stdin.readline
for i in range(int(input())):
n, q = map(int, input().split())
s = input()
if n == 1:
l = [0, [1, -1][s == "-"]]
else:
l = [[1, -1][s[0] == "-"], [1, -1][s[0] == "-"] - [1, -1][s[1] == "-"]]
for i in range(2, n):
f = [1, -1][s[i] == "-"]
if i % 2 != 0:
l.append(l[-2] + ([1, -1][s[i - 1] == "-"] - f))
else:
l.append(l[-1] + f)
l = [0] + l
for i in range(q):
k, r = map(int, input().split())
ff = abs(l[r] - l[k - 1])
if ff == 0:
print(0)
elif ff % 2 == 0:
print(2)
else:
print(1) | IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER ASSIGN VAR LIST NUMBER LIST NUMBER NUMBER VAR STRING ASSIGN VAR LIST LIST NUMBER NUMBER VAR NUMBER STRING BIN_OP LIST NUMBER NUMBER VAR NUMBER STRING LIST NUMBER NUMBER VAR NUMBER STRING FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR LIST NUMBER NUMBER VAR VAR STRING IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP LIST NUMBER NUMBER VAR BIN_OP VAR NUMBER STRING VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
This is the easy version of the problem. The difference between the versions is that the easy version does not require you to output the numbers of the rods to be removed. You can make hacks only if all versions of the problem are solved.
Stitch likes experimenting with different machines with his friend Sparky. Today they built another machine.
The main element of this machine are $n$ rods arranged along one straight line and numbered from $1$ to $n$ inclusive. Each of these rods must carry an electric charge quantitatively equal to either $1$ or $-1$ (otherwise the machine will not work). Another condition for this machine to work is that the sign-variable sum of the charge on all rods must be zero.
More formally, the rods can be represented as an array of $n$ numbers characterizing the charge: either $1$ or $-1$. Then the condition must hold: $a_1 - a_2 + a_3 - a_4 + \ldots = 0$, or $\sum\limits_{i=1}^n (-1)^{i-1} \cdot a_i = 0$.
Sparky charged all $n$ rods with an electric current, but unfortunately it happened that the rods were not charged correctly (the sign-variable sum of the charge is not zero). The friends decided to leave only some of the rods in the machine. Sparky has $q$ questions. In the $i$th question Sparky asks: if the machine consisted only of rods with numbers $l_i$ to $r_i$ inclusive, what minimal number of rods could be removed from the machine so that the sign-variable sum of charges on the remaining ones would be zero? Perhaps the friends got something wrong, and the sign-variable sum is already zero. In that case, you don't have to remove the rods at all.
If the number of rods is zero, we will assume that the sign-variable sum of charges is zero, that is, we can always remove all rods.
Help your friends and answer all of Sparky's questions!
-----Input-----
Each test contains multiple test cases.
The first line contains one positive integer $t$ ($1 \le t \le 10^3$), denoting the number of test cases. Description of the test cases follows.
The first line of each test case contains two positive integers $n$ and $q$ ($1 \le n, q \le 3 \cdot 10^5$) — the number of rods and the number of questions.
The second line of each test case contains a non-empty string $s$ of length $n$, where the charge of the $i$-th rod is $1$ if $s_i$ is the "+" symbol, or $-1$ if $s_i$ is the "-" symbol.
Each next line from the next $q$ lines contains two positive integers $l_i$ ans $r_i$ ($1 \le l_i \le r_i \le n$) — numbers, describing Sparky's questions.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$, and the sum of $q$ over all test cases does not exceed $3 \cdot 10^5$.
-----Output-----
For each test case, print a single integer — the minimal number of rods that can be removed.
-----Examples-----
Input
3
14 1
+--++---++-++-
1 14
14 3
+--++---+++---
1 14
6 12
3 10
4 10
+-+-
1 1
1 2
1 3
1 4
2 2
2 3
2 4
3 3
3 4
4 4
Output
2
2
1
0
1
2
1
2
1
2
1
1
2
1
-----Note-----
In the first test case for the first query you can remove the rods numbered $5$ and $8$, then the following set of rods will remain: +--+--++-++-. It is easy to see that here the sign-variable sum is zero.
In the second test case:
For the first query, we can remove the rods numbered $1$ and $11$, then the following set of rods will remain: --++---++---. It is easy to see that here the sign-variable sum is zero.
For the second query we can remove the rod numbered $9$, then the following set of rods will remain: ---++-. It is easy to see that here the variable sum is zero.
For the third query we can not remove the rods at all. | import sys
input = sys.stdin.buffer.readline
def process(S, Q):
odd_plus = [0]
even_plus = [0]
odd_minus = [0]
even_minus = [0]
n = len(S)
for i in range(n):
if S[i] == "+" and i % 2 == 0:
odd_plus.append(odd_plus[-1])
even_plus.append(even_plus[-1] + 1)
odd_minus.append(odd_minus[-1])
even_minus.append(even_minus[-1])
elif S[i] == "+" and i % 2 == 1:
odd_plus.append(odd_plus[-1] + 1)
even_plus.append(even_plus[-1])
odd_minus.append(odd_minus[-1])
even_minus.append(even_minus[-1])
elif S[i] == "-" and i % 2 == 0:
odd_plus.append(odd_plus[-1])
even_plus.append(even_plus[-1])
odd_minus.append(odd_minus[-1])
even_minus.append(even_minus[-1] + 1)
elif S[i] == "-" and i % 2 == 1:
odd_plus.append(odd_plus[-1])
even_plus.append(even_plus[-1])
odd_minus.append(odd_minus[-1] + 1)
even_minus.append(even_minus[-1])
answer = []
for l, r in Q:
m = r - l + 1
my_oddplus = odd_plus[r] - odd_plus[l - 1]
my_oddminus = odd_minus[r] - odd_minus[l - 1]
my_evenplus = even_plus[r] - even_plus[l - 1]
my_evenminus = even_minus[r] - even_minus[l - 1]
if my_oddplus != my_evenplus or my_oddminus != my_evenminus:
if m % 2 == 0:
a = 2
else:
a = 1
else:
a = 0
answer.append(a)
return answer
t = int(input())
for i in range(t):
n, q = [int(x) for x in input().split()]
S = input().decode()
Q = []
for j in range(q):
l, r = [int(x) for x in input().split()]
Q.append([l, r])
answer = process(S, Q)
for x in answer:
print(x) | IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR VAR STRING BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR VAR STRING BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR |
This is the easy version of the problem. The difference between the versions is that the easy version does not require you to output the numbers of the rods to be removed. You can make hacks only if all versions of the problem are solved.
Stitch likes experimenting with different machines with his friend Sparky. Today they built another machine.
The main element of this machine are $n$ rods arranged along one straight line and numbered from $1$ to $n$ inclusive. Each of these rods must carry an electric charge quantitatively equal to either $1$ or $-1$ (otherwise the machine will not work). Another condition for this machine to work is that the sign-variable sum of the charge on all rods must be zero.
More formally, the rods can be represented as an array of $n$ numbers characterizing the charge: either $1$ or $-1$. Then the condition must hold: $a_1 - a_2 + a_3 - a_4 + \ldots = 0$, or $\sum\limits_{i=1}^n (-1)^{i-1} \cdot a_i = 0$.
Sparky charged all $n$ rods with an electric current, but unfortunately it happened that the rods were not charged correctly (the sign-variable sum of the charge is not zero). The friends decided to leave only some of the rods in the machine. Sparky has $q$ questions. In the $i$th question Sparky asks: if the machine consisted only of rods with numbers $l_i$ to $r_i$ inclusive, what minimal number of rods could be removed from the machine so that the sign-variable sum of charges on the remaining ones would be zero? Perhaps the friends got something wrong, and the sign-variable sum is already zero. In that case, you don't have to remove the rods at all.
If the number of rods is zero, we will assume that the sign-variable sum of charges is zero, that is, we can always remove all rods.
Help your friends and answer all of Sparky's questions!
-----Input-----
Each test contains multiple test cases.
The first line contains one positive integer $t$ ($1 \le t \le 10^3$), denoting the number of test cases. Description of the test cases follows.
The first line of each test case contains two positive integers $n$ and $q$ ($1 \le n, q \le 3 \cdot 10^5$) — the number of rods and the number of questions.
The second line of each test case contains a non-empty string $s$ of length $n$, where the charge of the $i$-th rod is $1$ if $s_i$ is the "+" symbol, or $-1$ if $s_i$ is the "-" symbol.
Each next line from the next $q$ lines contains two positive integers $l_i$ ans $r_i$ ($1 \le l_i \le r_i \le n$) — numbers, describing Sparky's questions.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$, and the sum of $q$ over all test cases does not exceed $3 \cdot 10^5$.
-----Output-----
For each test case, print a single integer — the minimal number of rods that can be removed.
-----Examples-----
Input
3
14 1
+--++---++-++-
1 14
14 3
+--++---+++---
1 14
6 12
3 10
4 10
+-+-
1 1
1 2
1 3
1 4
2 2
2 3
2 4
3 3
3 4
4 4
Output
2
2
1
0
1
2
1
2
1
2
1
1
2
1
-----Note-----
In the first test case for the first query you can remove the rods numbered $5$ and $8$, then the following set of rods will remain: +--+--++-++-. It is easy to see that here the sign-variable sum is zero.
In the second test case:
For the first query, we can remove the rods numbered $1$ and $11$, then the following set of rods will remain: --++---++---. It is easy to see that here the sign-variable sum is zero.
For the second query we can remove the rod numbered $9$, then the following set of rods will remain: ---++-. It is easy to see that here the variable sum is zero.
For the third query we can not remove the rods at all. | import sys
input = lambda: sys.stdin.readline().strip()
mp = {"+": 1, "-": -1}
def solve():
n, m = map(int, input().split())
s = input()
u = [0] + [(mp[c] * (1 if i & 1 else -1)) for i, c in enumerate(s)]
for i in range(1, n + 1):
u[i] += u[i - 1]
for _ in range(m):
l, r = map(int, input().split())
x = u[r] - u[l - 1]
if x == 0:
print(0)
elif r - l + 1 & 1:
print(1)
else:
print(2)
return
for _ in range(int(input())):
solve() | IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT STRING STRING NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR |
This is the easy version of the problem. The difference between the versions is that the easy version does not require you to output the numbers of the rods to be removed. You can make hacks only if all versions of the problem are solved.
Stitch likes experimenting with different machines with his friend Sparky. Today they built another machine.
The main element of this machine are $n$ rods arranged along one straight line and numbered from $1$ to $n$ inclusive. Each of these rods must carry an electric charge quantitatively equal to either $1$ or $-1$ (otherwise the machine will not work). Another condition for this machine to work is that the sign-variable sum of the charge on all rods must be zero.
More formally, the rods can be represented as an array of $n$ numbers characterizing the charge: either $1$ or $-1$. Then the condition must hold: $a_1 - a_2 + a_3 - a_4 + \ldots = 0$, or $\sum\limits_{i=1}^n (-1)^{i-1} \cdot a_i = 0$.
Sparky charged all $n$ rods with an electric current, but unfortunately it happened that the rods were not charged correctly (the sign-variable sum of the charge is not zero). The friends decided to leave only some of the rods in the machine. Sparky has $q$ questions. In the $i$th question Sparky asks: if the machine consisted only of rods with numbers $l_i$ to $r_i$ inclusive, what minimal number of rods could be removed from the machine so that the sign-variable sum of charges on the remaining ones would be zero? Perhaps the friends got something wrong, and the sign-variable sum is already zero. In that case, you don't have to remove the rods at all.
If the number of rods is zero, we will assume that the sign-variable sum of charges is zero, that is, we can always remove all rods.
Help your friends and answer all of Sparky's questions!
-----Input-----
Each test contains multiple test cases.
The first line contains one positive integer $t$ ($1 \le t \le 10^3$), denoting the number of test cases. Description of the test cases follows.
The first line of each test case contains two positive integers $n$ and $q$ ($1 \le n, q \le 3 \cdot 10^5$) — the number of rods and the number of questions.
The second line of each test case contains a non-empty string $s$ of length $n$, where the charge of the $i$-th rod is $1$ if $s_i$ is the "+" symbol, or $-1$ if $s_i$ is the "-" symbol.
Each next line from the next $q$ lines contains two positive integers $l_i$ ans $r_i$ ($1 \le l_i \le r_i \le n$) — numbers, describing Sparky's questions.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$, and the sum of $q$ over all test cases does not exceed $3 \cdot 10^5$.
-----Output-----
For each test case, print a single integer — the minimal number of rods that can be removed.
-----Examples-----
Input
3
14 1
+--++---++-++-
1 14
14 3
+--++---+++---
1 14
6 12
3 10
4 10
+-+-
1 1
1 2
1 3
1 4
2 2
2 3
2 4
3 3
3 4
4 4
Output
2
2
1
0
1
2
1
2
1
2
1
1
2
1
-----Note-----
In the first test case for the first query you can remove the rods numbered $5$ and $8$, then the following set of rods will remain: +--+--++-++-. It is easy to see that here the sign-variable sum is zero.
In the second test case:
For the first query, we can remove the rods numbered $1$ and $11$, then the following set of rods will remain: --++---++---. It is easy to see that here the sign-variable sum is zero.
For the second query we can remove the rod numbered $9$, then the following set of rods will remain: ---++-. It is easy to see that here the variable sum is zero.
For the third query we can not remove the rods at all. | import sys
input = sys.stdin.readline
for _ in range(int(input())):
n, q = map(int, input().split())
s = input()[:-1]
A, B = [0], [0]
for i, c in enumerate(s):
if not i % 2:
A.append(A[-1] + (1 if c == "+" else -1))
B.append(B[-1])
else:
A.append(A[-1])
B.append(B[-1] + (1 if c == "+" else -1))
for _ in range(q):
l, r = map(int, input().split())
x = A[r] - A[l - 1]
y = B[r] - B[l - 1]
if x == y:
print(0)
elif abs(x - y) % 2:
print(1)
else:
print(2) | IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR LIST NUMBER LIST NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR STRING NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR STRING NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
This is the easy version of the problem. The difference between the versions is that the easy version does not require you to output the numbers of the rods to be removed. You can make hacks only if all versions of the problem are solved.
Stitch likes experimenting with different machines with his friend Sparky. Today they built another machine.
The main element of this machine are $n$ rods arranged along one straight line and numbered from $1$ to $n$ inclusive. Each of these rods must carry an electric charge quantitatively equal to either $1$ or $-1$ (otherwise the machine will not work). Another condition for this machine to work is that the sign-variable sum of the charge on all rods must be zero.
More formally, the rods can be represented as an array of $n$ numbers characterizing the charge: either $1$ or $-1$. Then the condition must hold: $a_1 - a_2 + a_3 - a_4 + \ldots = 0$, or $\sum\limits_{i=1}^n (-1)^{i-1} \cdot a_i = 0$.
Sparky charged all $n$ rods with an electric current, but unfortunately it happened that the rods were not charged correctly (the sign-variable sum of the charge is not zero). The friends decided to leave only some of the rods in the machine. Sparky has $q$ questions. In the $i$th question Sparky asks: if the machine consisted only of rods with numbers $l_i$ to $r_i$ inclusive, what minimal number of rods could be removed from the machine so that the sign-variable sum of charges on the remaining ones would be zero? Perhaps the friends got something wrong, and the sign-variable sum is already zero. In that case, you don't have to remove the rods at all.
If the number of rods is zero, we will assume that the sign-variable sum of charges is zero, that is, we can always remove all rods.
Help your friends and answer all of Sparky's questions!
-----Input-----
Each test contains multiple test cases.
The first line contains one positive integer $t$ ($1 \le t \le 10^3$), denoting the number of test cases. Description of the test cases follows.
The first line of each test case contains two positive integers $n$ and $q$ ($1 \le n, q \le 3 \cdot 10^5$) — the number of rods and the number of questions.
The second line of each test case contains a non-empty string $s$ of length $n$, where the charge of the $i$-th rod is $1$ if $s_i$ is the "+" symbol, or $-1$ if $s_i$ is the "-" symbol.
Each next line from the next $q$ lines contains two positive integers $l_i$ ans $r_i$ ($1 \le l_i \le r_i \le n$) — numbers, describing Sparky's questions.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$, and the sum of $q$ over all test cases does not exceed $3 \cdot 10^5$.
-----Output-----
For each test case, print a single integer — the minimal number of rods that can be removed.
-----Examples-----
Input
3
14 1
+--++---++-++-
1 14
14 3
+--++---+++---
1 14
6 12
3 10
4 10
+-+-
1 1
1 2
1 3
1 4
2 2
2 3
2 4
3 3
3 4
4 4
Output
2
2
1
0
1
2
1
2
1
2
1
1
2
1
-----Note-----
In the first test case for the first query you can remove the rods numbered $5$ and $8$, then the following set of rods will remain: +--+--++-++-. It is easy to see that here the sign-variable sum is zero.
In the second test case:
For the first query, we can remove the rods numbered $1$ and $11$, then the following set of rods will remain: --++---++---. It is easy to see that here the sign-variable sum is zero.
For the second query we can remove the rod numbered $9$, then the following set of rods will remain: ---++-. It is easy to see that here the variable sum is zero.
For the third query we can not remove the rods at all. | import sys
input = sys.stdin.readline
for _ in range(int(input())):
n, q = map(int, input().split())
arr = input()
pl = 0
mns = 0
F = [(0, 0)]
for x in range(n):
if arr[x] == "+":
pl += (-1) ** x
else:
mns += (-1) ** x
F.append((pl, mns))
for _ in range(q):
l, r = map(int, input().split())
pl, mns = F[r][0] - F[l - 1][0], F[r][1] - F[l - 1][1]
if pl == mns == 0:
print(0)
elif pl % 2 and mns % 2 or not (pl % 2 or mns % 2):
print(2)
else:
print(1) | IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR BIN_OP NUMBER VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
This is the easy version of the problem. The difference between the versions is that the easy version does not require you to output the numbers of the rods to be removed. You can make hacks only if all versions of the problem are solved.
Stitch likes experimenting with different machines with his friend Sparky. Today they built another machine.
The main element of this machine are $n$ rods arranged along one straight line and numbered from $1$ to $n$ inclusive. Each of these rods must carry an electric charge quantitatively equal to either $1$ or $-1$ (otherwise the machine will not work). Another condition for this machine to work is that the sign-variable sum of the charge on all rods must be zero.
More formally, the rods can be represented as an array of $n$ numbers characterizing the charge: either $1$ or $-1$. Then the condition must hold: $a_1 - a_2 + a_3 - a_4 + \ldots = 0$, or $\sum\limits_{i=1}^n (-1)^{i-1} \cdot a_i = 0$.
Sparky charged all $n$ rods with an electric current, but unfortunately it happened that the rods were not charged correctly (the sign-variable sum of the charge is not zero). The friends decided to leave only some of the rods in the machine. Sparky has $q$ questions. In the $i$th question Sparky asks: if the machine consisted only of rods with numbers $l_i$ to $r_i$ inclusive, what minimal number of rods could be removed from the machine so that the sign-variable sum of charges on the remaining ones would be zero? Perhaps the friends got something wrong, and the sign-variable sum is already zero. In that case, you don't have to remove the rods at all.
If the number of rods is zero, we will assume that the sign-variable sum of charges is zero, that is, we can always remove all rods.
Help your friends and answer all of Sparky's questions!
-----Input-----
Each test contains multiple test cases.
The first line contains one positive integer $t$ ($1 \le t \le 10^3$), denoting the number of test cases. Description of the test cases follows.
The first line of each test case contains two positive integers $n$ and $q$ ($1 \le n, q \le 3 \cdot 10^5$) — the number of rods and the number of questions.
The second line of each test case contains a non-empty string $s$ of length $n$, where the charge of the $i$-th rod is $1$ if $s_i$ is the "+" symbol, or $-1$ if $s_i$ is the "-" symbol.
Each next line from the next $q$ lines contains two positive integers $l_i$ ans $r_i$ ($1 \le l_i \le r_i \le n$) — numbers, describing Sparky's questions.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$, and the sum of $q$ over all test cases does not exceed $3 \cdot 10^5$.
-----Output-----
For each test case, print a single integer — the minimal number of rods that can be removed.
-----Examples-----
Input
3
14 1
+--++---++-++-
1 14
14 3
+--++---+++---
1 14
6 12
3 10
4 10
+-+-
1 1
1 2
1 3
1 4
2 2
2 3
2 4
3 3
3 4
4 4
Output
2
2
1
0
1
2
1
2
1
2
1
1
2
1
-----Note-----
In the first test case for the first query you can remove the rods numbered $5$ and $8$, then the following set of rods will remain: +--+--++-++-. It is easy to see that here the sign-variable sum is zero.
In the second test case:
For the first query, we can remove the rods numbered $1$ and $11$, then the following set of rods will remain: --++---++---. It is easy to see that here the sign-variable sum is zero.
For the second query we can remove the rod numbered $9$, then the following set of rods will remain: ---++-. It is easy to see that here the variable sum is zero.
For the third query we can not remove the rods at all. | import sys
input = sys.stdin.readline
for _ in range(int(input())):
n, q = map(int, input().split())
rod = input()
dp = [0]
for i, j in enumerate(rod):
dp.append(dp[-1] + 1) if (j == "+") ^ (i % 2 == 0) else dp.append(dp[-1] - 1)
for i in range(q):
l, r = map(int, input().split())
if dp[r] - dp[l - 1] == 0:
print(0)
elif (dp[r] - dp[l - 1]) % 2 == 1:
print(1)
else:
print(2) | IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR VAR FUNC_CALL VAR VAR EXPR BIN_OP VAR STRING BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
This is the easy version of the problem. The difference between the versions is that the easy version does not require you to output the numbers of the rods to be removed. You can make hacks only if all versions of the problem are solved.
Stitch likes experimenting with different machines with his friend Sparky. Today they built another machine.
The main element of this machine are $n$ rods arranged along one straight line and numbered from $1$ to $n$ inclusive. Each of these rods must carry an electric charge quantitatively equal to either $1$ or $-1$ (otherwise the machine will not work). Another condition for this machine to work is that the sign-variable sum of the charge on all rods must be zero.
More formally, the rods can be represented as an array of $n$ numbers characterizing the charge: either $1$ or $-1$. Then the condition must hold: $a_1 - a_2 + a_3 - a_4 + \ldots = 0$, or $\sum\limits_{i=1}^n (-1)^{i-1} \cdot a_i = 0$.
Sparky charged all $n$ rods with an electric current, but unfortunately it happened that the rods were not charged correctly (the sign-variable sum of the charge is not zero). The friends decided to leave only some of the rods in the machine. Sparky has $q$ questions. In the $i$th question Sparky asks: if the machine consisted only of rods with numbers $l_i$ to $r_i$ inclusive, what minimal number of rods could be removed from the machine so that the sign-variable sum of charges on the remaining ones would be zero? Perhaps the friends got something wrong, and the sign-variable sum is already zero. In that case, you don't have to remove the rods at all.
If the number of rods is zero, we will assume that the sign-variable sum of charges is zero, that is, we can always remove all rods.
Help your friends and answer all of Sparky's questions!
-----Input-----
Each test contains multiple test cases.
The first line contains one positive integer $t$ ($1 \le t \le 10^3$), denoting the number of test cases. Description of the test cases follows.
The first line of each test case contains two positive integers $n$ and $q$ ($1 \le n, q \le 3 \cdot 10^5$) — the number of rods and the number of questions.
The second line of each test case contains a non-empty string $s$ of length $n$, where the charge of the $i$-th rod is $1$ if $s_i$ is the "+" symbol, or $-1$ if $s_i$ is the "-" symbol.
Each next line from the next $q$ lines contains two positive integers $l_i$ ans $r_i$ ($1 \le l_i \le r_i \le n$) — numbers, describing Sparky's questions.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$, and the sum of $q$ over all test cases does not exceed $3 \cdot 10^5$.
-----Output-----
For each test case, print a single integer — the minimal number of rods that can be removed.
-----Examples-----
Input
3
14 1
+--++---++-++-
1 14
14 3
+--++---+++---
1 14
6 12
3 10
4 10
+-+-
1 1
1 2
1 3
1 4
2 2
2 3
2 4
3 3
3 4
4 4
Output
2
2
1
0
1
2
1
2
1
2
1
1
2
1
-----Note-----
In the first test case for the first query you can remove the rods numbered $5$ and $8$, then the following set of rods will remain: +--+--++-++-. It is easy to see that here the sign-variable sum is zero.
In the second test case:
For the first query, we can remove the rods numbered $1$ and $11$, then the following set of rods will remain: --++---++---. It is easy to see that here the sign-variable sum is zero.
For the second query we can remove the rod numbered $9$, then the following set of rods will remain: ---++-. It is easy to see that here the variable sum is zero.
For the third query we can not remove the rods at all. | import sys
def fun(s, i, n, x, arr):
if i == n:
if s == 0:
return 0
return 100
elif x == 0:
return min(fun(s + arr[i], i + 1, n, 1, arr), 1 + fun(s, i + 1, n, 0, arr))
else:
return min(fun(s - arr[i], i + 1, n, 0, arr), 1 + fun(s, i + 1, n, 1, arr))
for _ in range(int(input())):
n, q = map(int, input().split())
arr = [0] * n
s = sys.stdin.readline()
pref = [0] * (n + 1)
st = 0
for i in range(n):
if s[i] == "+":
arr[i] = 1
else:
arr[i] = -1
st = 0
for i in range(n):
if i % 2 == 0:
st += arr[i]
else:
st += -arr[i]
pref[i] = st
for i in range(q):
x, y = map(int, sys.stdin.readline().split())
if pref[y - 1] - pref[x - 2] == 0:
ans = 0
elif (pref[y - 1] - pref[x - 2]) % 2 == 0:
ans = 2
else:
ans = 1
sys.stdout.write(str(ans) + "\n") | IMPORT FUNC_DEF IF VAR VAR IF VAR NUMBER RETURN NUMBER RETURN NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR RETURN FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING |
This is the easy version of the problem. The difference between the versions is that the easy version does not require you to output the numbers of the rods to be removed. You can make hacks only if all versions of the problem are solved.
Stitch likes experimenting with different machines with his friend Sparky. Today they built another machine.
The main element of this machine are $n$ rods arranged along one straight line and numbered from $1$ to $n$ inclusive. Each of these rods must carry an electric charge quantitatively equal to either $1$ or $-1$ (otherwise the machine will not work). Another condition for this machine to work is that the sign-variable sum of the charge on all rods must be zero.
More formally, the rods can be represented as an array of $n$ numbers characterizing the charge: either $1$ or $-1$. Then the condition must hold: $a_1 - a_2 + a_3 - a_4 + \ldots = 0$, or $\sum\limits_{i=1}^n (-1)^{i-1} \cdot a_i = 0$.
Sparky charged all $n$ rods with an electric current, but unfortunately it happened that the rods were not charged correctly (the sign-variable sum of the charge is not zero). The friends decided to leave only some of the rods in the machine. Sparky has $q$ questions. In the $i$th question Sparky asks: if the machine consisted only of rods with numbers $l_i$ to $r_i$ inclusive, what minimal number of rods could be removed from the machine so that the sign-variable sum of charges on the remaining ones would be zero? Perhaps the friends got something wrong, and the sign-variable sum is already zero. In that case, you don't have to remove the rods at all.
If the number of rods is zero, we will assume that the sign-variable sum of charges is zero, that is, we can always remove all rods.
Help your friends and answer all of Sparky's questions!
-----Input-----
Each test contains multiple test cases.
The first line contains one positive integer $t$ ($1 \le t \le 10^3$), denoting the number of test cases. Description of the test cases follows.
The first line of each test case contains two positive integers $n$ and $q$ ($1 \le n, q \le 3 \cdot 10^5$) — the number of rods and the number of questions.
The second line of each test case contains a non-empty string $s$ of length $n$, where the charge of the $i$-th rod is $1$ if $s_i$ is the "+" symbol, or $-1$ if $s_i$ is the "-" symbol.
Each next line from the next $q$ lines contains two positive integers $l_i$ ans $r_i$ ($1 \le l_i \le r_i \le n$) — numbers, describing Sparky's questions.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$, and the sum of $q$ over all test cases does not exceed $3 \cdot 10^5$.
-----Output-----
For each test case, print a single integer — the minimal number of rods that can be removed.
-----Examples-----
Input
3
14 1
+--++---++-++-
1 14
14 3
+--++---+++---
1 14
6 12
3 10
4 10
+-+-
1 1
1 2
1 3
1 4
2 2
2 3
2 4
3 3
3 4
4 4
Output
2
2
1
0
1
2
1
2
1
2
1
1
2
1
-----Note-----
In the first test case for the first query you can remove the rods numbered $5$ and $8$, then the following set of rods will remain: +--+--++-++-. It is easy to see that here the sign-variable sum is zero.
In the second test case:
For the first query, we can remove the rods numbered $1$ and $11$, then the following set of rods will remain: --++---++---. It is easy to see that here the sign-variable sum is zero.
For the second query we can remove the rod numbered $9$, then the following set of rods will remain: ---++-. It is easy to see that here the variable sum is zero.
For the third query we can not remove the rods at all. | import sys
input = sys.stdin.readline
pre = [0] * (3 * int(100000.0) + 5)
for _ in range(int(input())):
n, q = map(int, input().split())
s = input()
for i, c in enumerate(s):
base = 1 if i % 2 else -1
if c == "+":
pre[i + 1] = pre[i] + base
else:
pre[i + 1] = pre[i] - base
for i in range(q):
l, r = map(int, input().split())
if pre[r] - pre[l - 1] == 0:
print(0)
elif (r - l + 1) % 2 == 1:
print(1)
else:
print(2) | IMPORT ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR STRING ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
This is the easy version of the problem. The difference between the versions is that the easy version does not require you to output the numbers of the rods to be removed. You can make hacks only if all versions of the problem are solved.
Stitch likes experimenting with different machines with his friend Sparky. Today they built another machine.
The main element of this machine are $n$ rods arranged along one straight line and numbered from $1$ to $n$ inclusive. Each of these rods must carry an electric charge quantitatively equal to either $1$ or $-1$ (otherwise the machine will not work). Another condition for this machine to work is that the sign-variable sum of the charge on all rods must be zero.
More formally, the rods can be represented as an array of $n$ numbers characterizing the charge: either $1$ or $-1$. Then the condition must hold: $a_1 - a_2 + a_3 - a_4 + \ldots = 0$, or $\sum\limits_{i=1}^n (-1)^{i-1} \cdot a_i = 0$.
Sparky charged all $n$ rods with an electric current, but unfortunately it happened that the rods were not charged correctly (the sign-variable sum of the charge is not zero). The friends decided to leave only some of the rods in the machine. Sparky has $q$ questions. In the $i$th question Sparky asks: if the machine consisted only of rods with numbers $l_i$ to $r_i$ inclusive, what minimal number of rods could be removed from the machine so that the sign-variable sum of charges on the remaining ones would be zero? Perhaps the friends got something wrong, and the sign-variable sum is already zero. In that case, you don't have to remove the rods at all.
If the number of rods is zero, we will assume that the sign-variable sum of charges is zero, that is, we can always remove all rods.
Help your friends and answer all of Sparky's questions!
-----Input-----
Each test contains multiple test cases.
The first line contains one positive integer $t$ ($1 \le t \le 10^3$), denoting the number of test cases. Description of the test cases follows.
The first line of each test case contains two positive integers $n$ and $q$ ($1 \le n, q \le 3 \cdot 10^5$) — the number of rods and the number of questions.
The second line of each test case contains a non-empty string $s$ of length $n$, where the charge of the $i$-th rod is $1$ if $s_i$ is the "+" symbol, or $-1$ if $s_i$ is the "-" symbol.
Each next line from the next $q$ lines contains two positive integers $l_i$ ans $r_i$ ($1 \le l_i \le r_i \le n$) — numbers, describing Sparky's questions.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$, and the sum of $q$ over all test cases does not exceed $3 \cdot 10^5$.
-----Output-----
For each test case, print a single integer — the minimal number of rods that can be removed.
-----Examples-----
Input
3
14 1
+--++---++-++-
1 14
14 3
+--++---+++---
1 14
6 12
3 10
4 10
+-+-
1 1
1 2
1 3
1 4
2 2
2 3
2 4
3 3
3 4
4 4
Output
2
2
1
0
1
2
1
2
1
2
1
1
2
1
-----Note-----
In the first test case for the first query you can remove the rods numbered $5$ and $8$, then the following set of rods will remain: +--+--++-++-. It is easy to see that here the sign-variable sum is zero.
In the second test case:
For the first query, we can remove the rods numbered $1$ and $11$, then the following set of rods will remain: --++---++---. It is easy to see that here the sign-variable sum is zero.
For the second query we can remove the rod numbered $9$, then the following set of rods will remain: ---++-. It is easy to see that here the variable sum is zero.
For the third query we can not remove the rods at all. | from sys import stdin
input = stdin.readline
for _ in range(int(input())):
n, q = map(int, input().split())
s = input().rstrip()
ps = [0]
for x in range(n):
if x % 2 == 0:
if s[x] == "+":
ps.append(ps[-1] + 1)
else:
ps.append(ps[-1] - 1)
elif s[x] == "+":
ps.append(ps[-1] - 1)
else:
ps.append(ps[-1] + 1)
for x in range(q):
l, r = map(int, input().split())
now = ps[r] - ps[l - 1]
if now == 0:
print(0)
elif now % 2 == 0:
print(2)
else:
print(1) | ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
This is the easy version of the problem. The difference between the versions is that the easy version does not require you to output the numbers of the rods to be removed. You can make hacks only if all versions of the problem are solved.
Stitch likes experimenting with different machines with his friend Sparky. Today they built another machine.
The main element of this machine are $n$ rods arranged along one straight line and numbered from $1$ to $n$ inclusive. Each of these rods must carry an electric charge quantitatively equal to either $1$ or $-1$ (otherwise the machine will not work). Another condition for this machine to work is that the sign-variable sum of the charge on all rods must be zero.
More formally, the rods can be represented as an array of $n$ numbers characterizing the charge: either $1$ or $-1$. Then the condition must hold: $a_1 - a_2 + a_3 - a_4 + \ldots = 0$, or $\sum\limits_{i=1}^n (-1)^{i-1} \cdot a_i = 0$.
Sparky charged all $n$ rods with an electric current, but unfortunately it happened that the rods were not charged correctly (the sign-variable sum of the charge is not zero). The friends decided to leave only some of the rods in the machine. Sparky has $q$ questions. In the $i$th question Sparky asks: if the machine consisted only of rods with numbers $l_i$ to $r_i$ inclusive, what minimal number of rods could be removed from the machine so that the sign-variable sum of charges on the remaining ones would be zero? Perhaps the friends got something wrong, and the sign-variable sum is already zero. In that case, you don't have to remove the rods at all.
If the number of rods is zero, we will assume that the sign-variable sum of charges is zero, that is, we can always remove all rods.
Help your friends and answer all of Sparky's questions!
-----Input-----
Each test contains multiple test cases.
The first line contains one positive integer $t$ ($1 \le t \le 10^3$), denoting the number of test cases. Description of the test cases follows.
The first line of each test case contains two positive integers $n$ and $q$ ($1 \le n, q \le 3 \cdot 10^5$) — the number of rods and the number of questions.
The second line of each test case contains a non-empty string $s$ of length $n$, where the charge of the $i$-th rod is $1$ if $s_i$ is the "+" symbol, or $-1$ if $s_i$ is the "-" symbol.
Each next line from the next $q$ lines contains two positive integers $l_i$ ans $r_i$ ($1 \le l_i \le r_i \le n$) — numbers, describing Sparky's questions.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$, and the sum of $q$ over all test cases does not exceed $3 \cdot 10^5$.
-----Output-----
For each test case, print a single integer — the minimal number of rods that can be removed.
-----Examples-----
Input
3
14 1
+--++---++-++-
1 14
14 3
+--++---+++---
1 14
6 12
3 10
4 10
+-+-
1 1
1 2
1 3
1 4
2 2
2 3
2 4
3 3
3 4
4 4
Output
2
2
1
0
1
2
1
2
1
2
1
1
2
1
-----Note-----
In the first test case for the first query you can remove the rods numbered $5$ and $8$, then the following set of rods will remain: +--+--++-++-. It is easy to see that here the sign-variable sum is zero.
In the second test case:
For the first query, we can remove the rods numbered $1$ and $11$, then the following set of rods will remain: --++---++---. It is easy to see that here the sign-variable sum is zero.
For the second query we can remove the rod numbered $9$, then the following set of rods will remain: ---++-. It is easy to see that here the variable sum is zero.
For the third query we can not remove the rods at all. | from sys import stdin
input = stdin.readline
t = int(input())
for i in range(t):
n, q = map(int, input().split())
s = input()
dp = [[0, 0] for i in range(n + 1)]
for i in range(n):
a = 0
b = 0
if s[i] == "+":
if i % 2 == 0:
a = a + 1
else:
b = b + 1
elif i % 2 == 0:
b = b + 1
else:
a = a + 1
dp[i + 1][0] = dp[i][0] + a
dp[i + 1][1] = dp[i][1] + b
for i in range(q):
l, r = map(int, input().split())
a = dp[r][0] - dp[l - 1][0]
b = dp[r][1] - dp[l - 1][1]
if abs(a - b) <= 2:
print(abs(a - b))
elif abs(a - b) % 2 == 0:
print(2)
else:
print(1) | ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR STRING IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR IF BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
This is the easy version of the problem. The difference between the versions is that the easy version does not require you to output the numbers of the rods to be removed. You can make hacks only if all versions of the problem are solved.
Stitch likes experimenting with different machines with his friend Sparky. Today they built another machine.
The main element of this machine are $n$ rods arranged along one straight line and numbered from $1$ to $n$ inclusive. Each of these rods must carry an electric charge quantitatively equal to either $1$ or $-1$ (otherwise the machine will not work). Another condition for this machine to work is that the sign-variable sum of the charge on all rods must be zero.
More formally, the rods can be represented as an array of $n$ numbers characterizing the charge: either $1$ or $-1$. Then the condition must hold: $a_1 - a_2 + a_3 - a_4 + \ldots = 0$, or $\sum\limits_{i=1}^n (-1)^{i-1} \cdot a_i = 0$.
Sparky charged all $n$ rods with an electric current, but unfortunately it happened that the rods were not charged correctly (the sign-variable sum of the charge is not zero). The friends decided to leave only some of the rods in the machine. Sparky has $q$ questions. In the $i$th question Sparky asks: if the machine consisted only of rods with numbers $l_i$ to $r_i$ inclusive, what minimal number of rods could be removed from the machine so that the sign-variable sum of charges on the remaining ones would be zero? Perhaps the friends got something wrong, and the sign-variable sum is already zero. In that case, you don't have to remove the rods at all.
If the number of rods is zero, we will assume that the sign-variable sum of charges is zero, that is, we can always remove all rods.
Help your friends and answer all of Sparky's questions!
-----Input-----
Each test contains multiple test cases.
The first line contains one positive integer $t$ ($1 \le t \le 10^3$), denoting the number of test cases. Description of the test cases follows.
The first line of each test case contains two positive integers $n$ and $q$ ($1 \le n, q \le 3 \cdot 10^5$) — the number of rods and the number of questions.
The second line of each test case contains a non-empty string $s$ of length $n$, where the charge of the $i$-th rod is $1$ if $s_i$ is the "+" symbol, or $-1$ if $s_i$ is the "-" symbol.
Each next line from the next $q$ lines contains two positive integers $l_i$ ans $r_i$ ($1 \le l_i \le r_i \le n$) — numbers, describing Sparky's questions.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$, and the sum of $q$ over all test cases does not exceed $3 \cdot 10^5$.
-----Output-----
For each test case, print a single integer — the minimal number of rods that can be removed.
-----Examples-----
Input
3
14 1
+--++---++-++-
1 14
14 3
+--++---+++---
1 14
6 12
3 10
4 10
+-+-
1 1
1 2
1 3
1 4
2 2
2 3
2 4
3 3
3 4
4 4
Output
2
2
1
0
1
2
1
2
1
2
1
1
2
1
-----Note-----
In the first test case for the first query you can remove the rods numbered $5$ and $8$, then the following set of rods will remain: +--+--++-++-. It is easy to see that here the sign-variable sum is zero.
In the second test case:
For the first query, we can remove the rods numbered $1$ and $11$, then the following set of rods will remain: --++---++---. It is easy to see that here the sign-variable sum is zero.
For the second query we can remove the rod numbered $9$, then the following set of rods will remain: ---++-. It is easy to see that here the variable sum is zero.
For the third query we can not remove the rods at all. | from sys import gettrace, stdin
if gettrace():
def inputi():
return input()
else:
def input():
return next(stdin)[:-1]
def inputi():
return stdin.buffer.readline()
def solve():
n, q = map(int, input().split())
ss = input()
rs = [0]
for i, r in enumerate(ss):
if i % 2 == 0 and r == "+" or i % 2 == 1 and r == "-":
rs.append(rs[-1] + 1)
else:
rs.append(rs[-1] - 1)
for _ in range(q):
l, r = map(int, input().split())
if (r - l) % 2 == 0:
print(1)
elif rs[r] == rs[l - 1]:
print(0)
else:
print(2)
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 VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR STRING BIN_OP VAR NUMBER NUMBER VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR |
This is the easy version of the problem. The difference between the versions is that the easy version does not require you to output the numbers of the rods to be removed. You can make hacks only if all versions of the problem are solved.
Stitch likes experimenting with different machines with his friend Sparky. Today they built another machine.
The main element of this machine are $n$ rods arranged along one straight line and numbered from $1$ to $n$ inclusive. Each of these rods must carry an electric charge quantitatively equal to either $1$ or $-1$ (otherwise the machine will not work). Another condition for this machine to work is that the sign-variable sum of the charge on all rods must be zero.
More formally, the rods can be represented as an array of $n$ numbers characterizing the charge: either $1$ or $-1$. Then the condition must hold: $a_1 - a_2 + a_3 - a_4 + \ldots = 0$, or $\sum\limits_{i=1}^n (-1)^{i-1} \cdot a_i = 0$.
Sparky charged all $n$ rods with an electric current, but unfortunately it happened that the rods were not charged correctly (the sign-variable sum of the charge is not zero). The friends decided to leave only some of the rods in the machine. Sparky has $q$ questions. In the $i$th question Sparky asks: if the machine consisted only of rods with numbers $l_i$ to $r_i$ inclusive, what minimal number of rods could be removed from the machine so that the sign-variable sum of charges on the remaining ones would be zero? Perhaps the friends got something wrong, and the sign-variable sum is already zero. In that case, you don't have to remove the rods at all.
If the number of rods is zero, we will assume that the sign-variable sum of charges is zero, that is, we can always remove all rods.
Help your friends and answer all of Sparky's questions!
-----Input-----
Each test contains multiple test cases.
The first line contains one positive integer $t$ ($1 \le t \le 10^3$), denoting the number of test cases. Description of the test cases follows.
The first line of each test case contains two positive integers $n$ and $q$ ($1 \le n, q \le 3 \cdot 10^5$) — the number of rods and the number of questions.
The second line of each test case contains a non-empty string $s$ of length $n$, where the charge of the $i$-th rod is $1$ if $s_i$ is the "+" symbol, or $-1$ if $s_i$ is the "-" symbol.
Each next line from the next $q$ lines contains two positive integers $l_i$ ans $r_i$ ($1 \le l_i \le r_i \le n$) — numbers, describing Sparky's questions.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$, and the sum of $q$ over all test cases does not exceed $3 \cdot 10^5$.
-----Output-----
For each test case, print a single integer — the minimal number of rods that can be removed.
-----Examples-----
Input
3
14 1
+--++---++-++-
1 14
14 3
+--++---+++---
1 14
6 12
3 10
4 10
+-+-
1 1
1 2
1 3
1 4
2 2
2 3
2 4
3 3
3 4
4 4
Output
2
2
1
0
1
2
1
2
1
2
1
1
2
1
-----Note-----
In the first test case for the first query you can remove the rods numbered $5$ and $8$, then the following set of rods will remain: +--+--++-++-. It is easy to see that here the sign-variable sum is zero.
In the second test case:
For the first query, we can remove the rods numbered $1$ and $11$, then the following set of rods will remain: --++---++---. It is easy to see that here the sign-variable sum is zero.
For the second query we can remove the rod numbered $9$, then the following set of rods will remain: ---++-. It is easy to see that here the variable sum is zero.
For the third query we can not remove the rods at all. | import sys
input = sys.stdin.readline
for __ in range(int(input())):
n, t = map(int, input().split())
s = input()
z = [0]
a = 0
x = 1
for i in range(n):
if s[i] == "+":
a = 1
else:
a = -1
a = a * x
x = x * -1
z.append(z[-1] + a)
for _ in range(t):
s1, s2 = map(int, input().split())
s_ = z[s2] - z[s1 - 1]
if s_ == 0:
print(0)
elif s_ % 2 == 1:
print(1)
else:
print(2) | IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
This is the easy version of the problem. The difference between the versions is that the easy version does not require you to output the numbers of the rods to be removed. You can make hacks only if all versions of the problem are solved.
Stitch likes experimenting with different machines with his friend Sparky. Today they built another machine.
The main element of this machine are $n$ rods arranged along one straight line and numbered from $1$ to $n$ inclusive. Each of these rods must carry an electric charge quantitatively equal to either $1$ or $-1$ (otherwise the machine will not work). Another condition for this machine to work is that the sign-variable sum of the charge on all rods must be zero.
More formally, the rods can be represented as an array of $n$ numbers characterizing the charge: either $1$ or $-1$. Then the condition must hold: $a_1 - a_2 + a_3 - a_4 + \ldots = 0$, or $\sum\limits_{i=1}^n (-1)^{i-1} \cdot a_i = 0$.
Sparky charged all $n$ rods with an electric current, but unfortunately it happened that the rods were not charged correctly (the sign-variable sum of the charge is not zero). The friends decided to leave only some of the rods in the machine. Sparky has $q$ questions. In the $i$th question Sparky asks: if the machine consisted only of rods with numbers $l_i$ to $r_i$ inclusive, what minimal number of rods could be removed from the machine so that the sign-variable sum of charges on the remaining ones would be zero? Perhaps the friends got something wrong, and the sign-variable sum is already zero. In that case, you don't have to remove the rods at all.
If the number of rods is zero, we will assume that the sign-variable sum of charges is zero, that is, we can always remove all rods.
Help your friends and answer all of Sparky's questions!
-----Input-----
Each test contains multiple test cases.
The first line contains one positive integer $t$ ($1 \le t \le 10^3$), denoting the number of test cases. Description of the test cases follows.
The first line of each test case contains two positive integers $n$ and $q$ ($1 \le n, q \le 3 \cdot 10^5$) — the number of rods and the number of questions.
The second line of each test case contains a non-empty string $s$ of length $n$, where the charge of the $i$-th rod is $1$ if $s_i$ is the "+" symbol, or $-1$ if $s_i$ is the "-" symbol.
Each next line from the next $q$ lines contains two positive integers $l_i$ ans $r_i$ ($1 \le l_i \le r_i \le n$) — numbers, describing Sparky's questions.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$, and the sum of $q$ over all test cases does not exceed $3 \cdot 10^5$.
-----Output-----
For each test case, print a single integer — the minimal number of rods that can be removed.
-----Examples-----
Input
3
14 1
+--++---++-++-
1 14
14 3
+--++---+++---
1 14
6 12
3 10
4 10
+-+-
1 1
1 2
1 3
1 4
2 2
2 3
2 4
3 3
3 4
4 4
Output
2
2
1
0
1
2
1
2
1
2
1
1
2
1
-----Note-----
In the first test case for the first query you can remove the rods numbered $5$ and $8$, then the following set of rods will remain: +--+--++-++-. It is easy to see that here the sign-variable sum is zero.
In the second test case:
For the first query, we can remove the rods numbered $1$ and $11$, then the following set of rods will remain: --++---++---. It is easy to see that here the sign-variable sum is zero.
For the second query we can remove the rod numbered $9$, then the following set of rods will remain: ---++-. It is easy to see that here the variable sum is zero.
For the third query we can not remove the rods at all. | import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
n, q = map(int, input().split())
s = list(str(input()))
if n % 2:
cur = 1
else:
cur = -1
sums = []
sums.append(0)
for i in range(n - 1, -1, -1):
if s[i] == "+":
sums.append(sums[-1] + cur)
else:
sums.append(sums[-1] - cur)
cur *= -1
for _ in range(q):
a, b = map(int, input().split())
if sums[n - b] - sums[n - a + 1] == 0:
print(0)
elif (b - a + 1) % 2:
print(1)
else:
print(2) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
This is the easy version of the problem. The difference between the versions is that the easy version does not require you to output the numbers of the rods to be removed. You can make hacks only if all versions of the problem are solved.
Stitch likes experimenting with different machines with his friend Sparky. Today they built another machine.
The main element of this machine are $n$ rods arranged along one straight line and numbered from $1$ to $n$ inclusive. Each of these rods must carry an electric charge quantitatively equal to either $1$ or $-1$ (otherwise the machine will not work). Another condition for this machine to work is that the sign-variable sum of the charge on all rods must be zero.
More formally, the rods can be represented as an array of $n$ numbers characterizing the charge: either $1$ or $-1$. Then the condition must hold: $a_1 - a_2 + a_3 - a_4 + \ldots = 0$, or $\sum\limits_{i=1}^n (-1)^{i-1} \cdot a_i = 0$.
Sparky charged all $n$ rods with an electric current, but unfortunately it happened that the rods were not charged correctly (the sign-variable sum of the charge is not zero). The friends decided to leave only some of the rods in the machine. Sparky has $q$ questions. In the $i$th question Sparky asks: if the machine consisted only of rods with numbers $l_i$ to $r_i$ inclusive, what minimal number of rods could be removed from the machine so that the sign-variable sum of charges on the remaining ones would be zero? Perhaps the friends got something wrong, and the sign-variable sum is already zero. In that case, you don't have to remove the rods at all.
If the number of rods is zero, we will assume that the sign-variable sum of charges is zero, that is, we can always remove all rods.
Help your friends and answer all of Sparky's questions!
-----Input-----
Each test contains multiple test cases.
The first line contains one positive integer $t$ ($1 \le t \le 10^3$), denoting the number of test cases. Description of the test cases follows.
The first line of each test case contains two positive integers $n$ and $q$ ($1 \le n, q \le 3 \cdot 10^5$) — the number of rods and the number of questions.
The second line of each test case contains a non-empty string $s$ of length $n$, where the charge of the $i$-th rod is $1$ if $s_i$ is the "+" symbol, or $-1$ if $s_i$ is the "-" symbol.
Each next line from the next $q$ lines contains two positive integers $l_i$ ans $r_i$ ($1 \le l_i \le r_i \le n$) — numbers, describing Sparky's questions.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$, and the sum of $q$ over all test cases does not exceed $3 \cdot 10^5$.
-----Output-----
For each test case, print a single integer — the minimal number of rods that can be removed.
-----Examples-----
Input
3
14 1
+--++---++-++-
1 14
14 3
+--++---+++---
1 14
6 12
3 10
4 10
+-+-
1 1
1 2
1 3
1 4
2 2
2 3
2 4
3 3
3 4
4 4
Output
2
2
1
0
1
2
1
2
1
2
1
1
2
1
-----Note-----
In the first test case for the first query you can remove the rods numbered $5$ and $8$, then the following set of rods will remain: +--+--++-++-. It is easy to see that here the sign-variable sum is zero.
In the second test case:
For the first query, we can remove the rods numbered $1$ and $11$, then the following set of rods will remain: --++---++---. It is easy to see that here the sign-variable sum is zero.
For the second query we can remove the rod numbered $9$, then the following set of rods will remain: ---++-. It is easy to see that here the variable sum is zero.
For the third query we can not remove the rods at all. | import itertools as _itertools
import sys as _sys
def which_rods_should_remove_to_make_machine_charged_correctly(rods, queries):
rods = [(+x if i % 2 == 0 else -x) for i, x in enumerate(rods)]
prefix_sums = tuple(_itertools.accumulate(rods))
for query in queries:
yield tuple(_process_query(query, prefix_sums))
def _process_query(query, prefix_sums):
def get_prefix_sum(i):
return prefix_sums[i] if i >= 0 else 0
l, r = query
sum_before_prefix = get_prefix_sum(l - 1)
initial_sum = get_prefix_sum(r) - sum_before_prefix
if initial_sum == 0:
return
if (initial_sum - 1) % 2 != 0:
yield from _process_query((l, r - 1), prefix_sums)
yield r
return
def get_sum_by_deletion(i):
sum_before_deletion = get_prefix_sum(i - 1) - sum_before_prefix
sum_after_deletion = get_prefix_sum(r) - get_prefix_sum(i)
return sum_before_deletion - sum_after_deletion
first_sum = get_sum_by_deletion(r)
if first_sum == 0:
yield r
return
min_i_to_del = l
max_i_to_del = r
while min_i_to_del != max_i_to_del:
i = (min_i_to_del + max_i_to_del) // 2
current_sum = get_sum_by_deletion(i)
if current_sum * first_sum < 0:
min_i_to_del = i + 1
else:
max_i_to_del = i
i_to_del = min_i_to_del
yield i_to_del
def _main():
output = []
[tests_n] = _read_ints()
for i_test in range(tests_n):
[rods_n, queries_n] = _read_ints()
rods = _read_string()
assert len(rods) == rods_n and set(rods).issubset({"+", "-"})
rods = [(+1 if ch == "+" else -1) for ch in rods]
queries = [_read_query() for i_query in range(queries_n)]
results = which_rods_should_remove_to_make_machine_charged_correctly(
rods, queries
)
for result in results:
output.append(str(len(result)))
_sys.stdout.write("\n".join(output))
_sys.stdout.write("\n")
def _read_query(file=_sys.stdin):
[l, r] = _read_ints(file)
l -= 1
r -= 1
return l, r
def _read_ints(file=_sys.stdin):
return map(int, _read_string(file).split())
def _read_string(file=_sys.stdin):
[result] = file.readline().splitlines()
return result
_main() | IMPORT IMPORT FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_DEF FUNC_DEF RETURN VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN IF BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR VAR RETURN FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR VAR RETURN ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN LIST VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN LIST VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR STRING STRING ASSIGN VAR VAR STRING NUMBER NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR STRING FUNC_DEF VAR ASSIGN LIST VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR FUNC_DEF VAR RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_DEF VAR ASSIGN LIST VAR FUNC_CALL FUNC_CALL VAR RETURN VAR EXPR FUNC_CALL VAR |
This is the easy version of the problem. The difference between the versions is that the easy version does not require you to output the numbers of the rods to be removed. You can make hacks only if all versions of the problem are solved.
Stitch likes experimenting with different machines with his friend Sparky. Today they built another machine.
The main element of this machine are $n$ rods arranged along one straight line and numbered from $1$ to $n$ inclusive. Each of these rods must carry an electric charge quantitatively equal to either $1$ or $-1$ (otherwise the machine will not work). Another condition for this machine to work is that the sign-variable sum of the charge on all rods must be zero.
More formally, the rods can be represented as an array of $n$ numbers characterizing the charge: either $1$ or $-1$. Then the condition must hold: $a_1 - a_2 + a_3 - a_4 + \ldots = 0$, or $\sum\limits_{i=1}^n (-1)^{i-1} \cdot a_i = 0$.
Sparky charged all $n$ rods with an electric current, but unfortunately it happened that the rods were not charged correctly (the sign-variable sum of the charge is not zero). The friends decided to leave only some of the rods in the machine. Sparky has $q$ questions. In the $i$th question Sparky asks: if the machine consisted only of rods with numbers $l_i$ to $r_i$ inclusive, what minimal number of rods could be removed from the machine so that the sign-variable sum of charges on the remaining ones would be zero? Perhaps the friends got something wrong, and the sign-variable sum is already zero. In that case, you don't have to remove the rods at all.
If the number of rods is zero, we will assume that the sign-variable sum of charges is zero, that is, we can always remove all rods.
Help your friends and answer all of Sparky's questions!
-----Input-----
Each test contains multiple test cases.
The first line contains one positive integer $t$ ($1 \le t \le 10^3$), denoting the number of test cases. Description of the test cases follows.
The first line of each test case contains two positive integers $n$ and $q$ ($1 \le n, q \le 3 \cdot 10^5$) — the number of rods and the number of questions.
The second line of each test case contains a non-empty string $s$ of length $n$, where the charge of the $i$-th rod is $1$ if $s_i$ is the "+" symbol, or $-1$ if $s_i$ is the "-" symbol.
Each next line from the next $q$ lines contains two positive integers $l_i$ ans $r_i$ ($1 \le l_i \le r_i \le n$) — numbers, describing Sparky's questions.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$, and the sum of $q$ over all test cases does not exceed $3 \cdot 10^5$.
-----Output-----
For each test case, print a single integer — the minimal number of rods that can be removed.
-----Examples-----
Input
3
14 1
+--++---++-++-
1 14
14 3
+--++---+++---
1 14
6 12
3 10
4 10
+-+-
1 1
1 2
1 3
1 4
2 2
2 3
2 4
3 3
3 4
4 4
Output
2
2
1
0
1
2
1
2
1
2
1
1
2
1
-----Note-----
In the first test case for the first query you can remove the rods numbered $5$ and $8$, then the following set of rods will remain: +--+--++-++-. It is easy to see that here the sign-variable sum is zero.
In the second test case:
For the first query, we can remove the rods numbered $1$ and $11$, then the following set of rods will remain: --++---++---. It is easy to see that here the sign-variable sum is zero.
For the second query we can remove the rod numbered $9$, then the following set of rods will remain: ---++-. It is easy to see that here the variable sum is zero.
For the third query we can not remove the rods at all. | import sys
read = lambda: sys.stdin.readline()
for _ in range(int(read())):
n, q = map(int, read().split())
s = read()
s = s[: len(s) - 1]
pre = [(0) for i in range(n + 1)]
val = {"+": 1, "-": -1}
for i in range(n):
if i % 2 == 0:
pre[i + 1] = pre[i] + val[s[i]]
else:
pre[i + 1] = pre[i] - val[s[i]]
for i in range(q):
l, r = map(int, read().split())
tot = -1 * (pre[r] - pre[l - 1]) if l % 2 == 0 else pre[r] - pre[l - 1]
if (r - l + 1) % 2 == 1:
print(1)
continue
elif tot == 0:
print(0)
continue
else:
print(2)
continue | IMPORT ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR DICT STRING STRING NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
This is the easy version of the problem. The difference between the versions is that the easy version does not require you to output the numbers of the rods to be removed. You can make hacks only if all versions of the problem are solved.
Stitch likes experimenting with different machines with his friend Sparky. Today they built another machine.
The main element of this machine are $n$ rods arranged along one straight line and numbered from $1$ to $n$ inclusive. Each of these rods must carry an electric charge quantitatively equal to either $1$ or $-1$ (otherwise the machine will not work). Another condition for this machine to work is that the sign-variable sum of the charge on all rods must be zero.
More formally, the rods can be represented as an array of $n$ numbers characterizing the charge: either $1$ or $-1$. Then the condition must hold: $a_1 - a_2 + a_3 - a_4 + \ldots = 0$, or $\sum\limits_{i=1}^n (-1)^{i-1} \cdot a_i = 0$.
Sparky charged all $n$ rods with an electric current, but unfortunately it happened that the rods were not charged correctly (the sign-variable sum of the charge is not zero). The friends decided to leave only some of the rods in the machine. Sparky has $q$ questions. In the $i$th question Sparky asks: if the machine consisted only of rods with numbers $l_i$ to $r_i$ inclusive, what minimal number of rods could be removed from the machine so that the sign-variable sum of charges on the remaining ones would be zero? Perhaps the friends got something wrong, and the sign-variable sum is already zero. In that case, you don't have to remove the rods at all.
If the number of rods is zero, we will assume that the sign-variable sum of charges is zero, that is, we can always remove all rods.
Help your friends and answer all of Sparky's questions!
-----Input-----
Each test contains multiple test cases.
The first line contains one positive integer $t$ ($1 \le t \le 10^3$), denoting the number of test cases. Description of the test cases follows.
The first line of each test case contains two positive integers $n$ and $q$ ($1 \le n, q \le 3 \cdot 10^5$) — the number of rods and the number of questions.
The second line of each test case contains a non-empty string $s$ of length $n$, where the charge of the $i$-th rod is $1$ if $s_i$ is the "+" symbol, or $-1$ if $s_i$ is the "-" symbol.
Each next line from the next $q$ lines contains two positive integers $l_i$ ans $r_i$ ($1 \le l_i \le r_i \le n$) — numbers, describing Sparky's questions.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$, and the sum of $q$ over all test cases does not exceed $3 \cdot 10^5$.
-----Output-----
For each test case, print a single integer — the minimal number of rods that can be removed.
-----Examples-----
Input
3
14 1
+--++---++-++-
1 14
14 3
+--++---+++---
1 14
6 12
3 10
4 10
+-+-
1 1
1 2
1 3
1 4
2 2
2 3
2 4
3 3
3 4
4 4
Output
2
2
1
0
1
2
1
2
1
2
1
1
2
1
-----Note-----
In the first test case for the first query you can remove the rods numbered $5$ and $8$, then the following set of rods will remain: +--+--++-++-. It is easy to see that here the sign-variable sum is zero.
In the second test case:
For the first query, we can remove the rods numbered $1$ and $11$, then the following set of rods will remain: --++---++---. It is easy to see that here the sign-variable sum is zero.
For the second query we can remove the rod numbered $9$, then the following set of rods will remain: ---++-. It is easy to see that here the variable sum is zero.
For the third query we can not remove the rods at all. | import sys
input = sys.stdin.readline
for _ in range(int(input())):
n, q = map(int, input().split())
s = input().strip()
psum = [0] * (n + 1)
for i in range(n):
psum[i] = psum[i - 1] + (-1 + 2 * (i & 1 ^ 1)) * (-1 + 2 * (s[i] == "+"))
for i in range(q):
l, r = map(int, input().split())
r -= 1
l -= 1
if psum[r] - psum[l - 1] == 0:
print(0)
else:
print(1 + (r - l + 1 & 1 ^ 1)) | IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP NUMBER BIN_OP NUMBER VAR VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER |
This is the easy version of the problem. The difference between the versions is that the easy version does not require you to output the numbers of the rods to be removed. You can make hacks only if all versions of the problem are solved.
Stitch likes experimenting with different machines with his friend Sparky. Today they built another machine.
The main element of this machine are $n$ rods arranged along one straight line and numbered from $1$ to $n$ inclusive. Each of these rods must carry an electric charge quantitatively equal to either $1$ or $-1$ (otherwise the machine will not work). Another condition for this machine to work is that the sign-variable sum of the charge on all rods must be zero.
More formally, the rods can be represented as an array of $n$ numbers characterizing the charge: either $1$ or $-1$. Then the condition must hold: $a_1 - a_2 + a_3 - a_4 + \ldots = 0$, or $\sum\limits_{i=1}^n (-1)^{i-1} \cdot a_i = 0$.
Sparky charged all $n$ rods with an electric current, but unfortunately it happened that the rods were not charged correctly (the sign-variable sum of the charge is not zero). The friends decided to leave only some of the rods in the machine. Sparky has $q$ questions. In the $i$th question Sparky asks: if the machine consisted only of rods with numbers $l_i$ to $r_i$ inclusive, what minimal number of rods could be removed from the machine so that the sign-variable sum of charges on the remaining ones would be zero? Perhaps the friends got something wrong, and the sign-variable sum is already zero. In that case, you don't have to remove the rods at all.
If the number of rods is zero, we will assume that the sign-variable sum of charges is zero, that is, we can always remove all rods.
Help your friends and answer all of Sparky's questions!
-----Input-----
Each test contains multiple test cases.
The first line contains one positive integer $t$ ($1 \le t \le 10^3$), denoting the number of test cases. Description of the test cases follows.
The first line of each test case contains two positive integers $n$ and $q$ ($1 \le n, q \le 3 \cdot 10^5$) — the number of rods and the number of questions.
The second line of each test case contains a non-empty string $s$ of length $n$, where the charge of the $i$-th rod is $1$ if $s_i$ is the "+" symbol, or $-1$ if $s_i$ is the "-" symbol.
Each next line from the next $q$ lines contains two positive integers $l_i$ ans $r_i$ ($1 \le l_i \le r_i \le n$) — numbers, describing Sparky's questions.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$, and the sum of $q$ over all test cases does not exceed $3 \cdot 10^5$.
-----Output-----
For each test case, print a single integer — the minimal number of rods that can be removed.
-----Examples-----
Input
3
14 1
+--++---++-++-
1 14
14 3
+--++---+++---
1 14
6 12
3 10
4 10
+-+-
1 1
1 2
1 3
1 4
2 2
2 3
2 4
3 3
3 4
4 4
Output
2
2
1
0
1
2
1
2
1
2
1
1
2
1
-----Note-----
In the first test case for the first query you can remove the rods numbered $5$ and $8$, then the following set of rods will remain: +--+--++-++-. It is easy to see that here the sign-variable sum is zero.
In the second test case:
For the first query, we can remove the rods numbered $1$ and $11$, then the following set of rods will remain: --++---++---. It is easy to see that here the sign-variable sum is zero.
For the second query we can remove the rod numbered $9$, then the following set of rods will remain: ---++-. It is easy to see that here the variable sum is zero.
For the third query we can not remove the rods at all. | from sys import stdin
input = stdin.readline
def solution():
n, q = map(int, input().split())
S = input().rstrip()
arr = [0] * (n + 1)
sign = 1
for i in range(1, n + 1):
if S[i - 1] == "+":
arr[i] = arr[i - 1] + 1 * sign
else:
arr[i] = arr[i - 1] - 1 * sign
sign *= -1
for _ in range(q):
l, r = map(int, input().split())
ans = arr[r] - arr[l - 1]
if ans == 0:
print(0)
elif ans % 2 == 1:
print(1)
else:
print(2)
T = int(input())
for _ in range(T):
solution() | ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
This is the easy version of the problem. The difference between the versions is that the easy version does not require you to output the numbers of the rods to be removed. You can make hacks only if all versions of the problem are solved.
Stitch likes experimenting with different machines with his friend Sparky. Today they built another machine.
The main element of this machine are $n$ rods arranged along one straight line and numbered from $1$ to $n$ inclusive. Each of these rods must carry an electric charge quantitatively equal to either $1$ or $-1$ (otherwise the machine will not work). Another condition for this machine to work is that the sign-variable sum of the charge on all rods must be zero.
More formally, the rods can be represented as an array of $n$ numbers characterizing the charge: either $1$ or $-1$. Then the condition must hold: $a_1 - a_2 + a_3 - a_4 + \ldots = 0$, or $\sum\limits_{i=1}^n (-1)^{i-1} \cdot a_i = 0$.
Sparky charged all $n$ rods with an electric current, but unfortunately it happened that the rods were not charged correctly (the sign-variable sum of the charge is not zero). The friends decided to leave only some of the rods in the machine. Sparky has $q$ questions. In the $i$th question Sparky asks: if the machine consisted only of rods with numbers $l_i$ to $r_i$ inclusive, what minimal number of rods could be removed from the machine so that the sign-variable sum of charges on the remaining ones would be zero? Perhaps the friends got something wrong, and the sign-variable sum is already zero. In that case, you don't have to remove the rods at all.
If the number of rods is zero, we will assume that the sign-variable sum of charges is zero, that is, we can always remove all rods.
Help your friends and answer all of Sparky's questions!
-----Input-----
Each test contains multiple test cases.
The first line contains one positive integer $t$ ($1 \le t \le 10^3$), denoting the number of test cases. Description of the test cases follows.
The first line of each test case contains two positive integers $n$ and $q$ ($1 \le n, q \le 3 \cdot 10^5$) — the number of rods and the number of questions.
The second line of each test case contains a non-empty string $s$ of length $n$, where the charge of the $i$-th rod is $1$ if $s_i$ is the "+" symbol, or $-1$ if $s_i$ is the "-" symbol.
Each next line from the next $q$ lines contains two positive integers $l_i$ ans $r_i$ ($1 \le l_i \le r_i \le n$) — numbers, describing Sparky's questions.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$, and the sum of $q$ over all test cases does not exceed $3 \cdot 10^5$.
-----Output-----
For each test case, print a single integer — the minimal number of rods that can be removed.
-----Examples-----
Input
3
14 1
+--++---++-++-
1 14
14 3
+--++---+++---
1 14
6 12
3 10
4 10
+-+-
1 1
1 2
1 3
1 4
2 2
2 3
2 4
3 3
3 4
4 4
Output
2
2
1
0
1
2
1
2
1
2
1
1
2
1
-----Note-----
In the first test case for the first query you can remove the rods numbered $5$ and $8$, then the following set of rods will remain: +--+--++-++-. It is easy to see that here the sign-variable sum is zero.
In the second test case:
For the first query, we can remove the rods numbered $1$ and $11$, then the following set of rods will remain: --++---++---. It is easy to see that here the sign-variable sum is zero.
For the second query we can remove the rod numbered $9$, then the following set of rods will remain: ---++-. It is easy to see that here the variable sum is zero.
For the third query we can not remove the rods at all. | import sys
input = sys.stdin.readline
def inp():
return int(input())
def input_list():
return list(map(int, input().split()))
def input_string():
s = input()
return list(s[: len(s) - 1])
def input_int_gen():
return map(int, input().split())
tests = inp()
charge_dict = {"+": 1, "-": -1}
for _ in range(tests):
n, q = input_int_gen()
s = input_string()
s = [charge_dict[k] for k in s]
ps = [0]
for i, k in enumerate(s):
ps.append(ps[-1] + k * (-1 if i % 2 else 1))
for i in range(q):
l, r = input_int_gen()
charge = (ps[r] - ps[l - 1]) * (1 if l % 2 else -1)
if ps[r] - ps[l - 1] == 0:
print(0)
continue
print(1 if (r - l + 1) % 2 else 2) | IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT STRING STRING NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR LIST NUMBER FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER NUMBER |
This is the easy version of the problem. The difference between the versions is that the easy version does not require you to output the numbers of the rods to be removed. You can make hacks only if all versions of the problem are solved.
Stitch likes experimenting with different machines with his friend Sparky. Today they built another machine.
The main element of this machine are $n$ rods arranged along one straight line and numbered from $1$ to $n$ inclusive. Each of these rods must carry an electric charge quantitatively equal to either $1$ or $-1$ (otherwise the machine will not work). Another condition for this machine to work is that the sign-variable sum of the charge on all rods must be zero.
More formally, the rods can be represented as an array of $n$ numbers characterizing the charge: either $1$ or $-1$. Then the condition must hold: $a_1 - a_2 + a_3 - a_4 + \ldots = 0$, or $\sum\limits_{i=1}^n (-1)^{i-1} \cdot a_i = 0$.
Sparky charged all $n$ rods with an electric current, but unfortunately it happened that the rods were not charged correctly (the sign-variable sum of the charge is not zero). The friends decided to leave only some of the rods in the machine. Sparky has $q$ questions. In the $i$th question Sparky asks: if the machine consisted only of rods with numbers $l_i$ to $r_i$ inclusive, what minimal number of rods could be removed from the machine so that the sign-variable sum of charges on the remaining ones would be zero? Perhaps the friends got something wrong, and the sign-variable sum is already zero. In that case, you don't have to remove the rods at all.
If the number of rods is zero, we will assume that the sign-variable sum of charges is zero, that is, we can always remove all rods.
Help your friends and answer all of Sparky's questions!
-----Input-----
Each test contains multiple test cases.
The first line contains one positive integer $t$ ($1 \le t \le 10^3$), denoting the number of test cases. Description of the test cases follows.
The first line of each test case contains two positive integers $n$ and $q$ ($1 \le n, q \le 3 \cdot 10^5$) — the number of rods and the number of questions.
The second line of each test case contains a non-empty string $s$ of length $n$, where the charge of the $i$-th rod is $1$ if $s_i$ is the "+" symbol, or $-1$ if $s_i$ is the "-" symbol.
Each next line from the next $q$ lines contains two positive integers $l_i$ ans $r_i$ ($1 \le l_i \le r_i \le n$) — numbers, describing Sparky's questions.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$, and the sum of $q$ over all test cases does not exceed $3 \cdot 10^5$.
-----Output-----
For each test case, print a single integer — the minimal number of rods that can be removed.
-----Examples-----
Input
3
14 1
+--++---++-++-
1 14
14 3
+--++---+++---
1 14
6 12
3 10
4 10
+-+-
1 1
1 2
1 3
1 4
2 2
2 3
2 4
3 3
3 4
4 4
Output
2
2
1
0
1
2
1
2
1
2
1
1
2
1
-----Note-----
In the first test case for the first query you can remove the rods numbered $5$ and $8$, then the following set of rods will remain: +--+--++-++-. It is easy to see that here the sign-variable sum is zero.
In the second test case:
For the first query, we can remove the rods numbered $1$ and $11$, then the following set of rods will remain: --++---++---. It is easy to see that here the sign-variable sum is zero.
For the second query we can remove the rod numbered $9$, then the following set of rods will remain: ---++-. It is easy to see that here the variable sum is zero.
For the third query we can not remove the rods at all. | import sys
input = sys.stdin.readline
t = int(input())
for tests in range(t):
n, q = map(int, input().split())
S = input().strip()
A = []
for i in range(n):
if S[i] == "+":
x = 1
else:
x = -1
if i % 2 == 0:
A.append(x)
else:
A.append(-x)
S = [0]
for a in A:
S.append(S[-1] + a)
for queries in range(q):
l, r = map(int, input().split())
k = abs(S[r] - S[l - 1])
if k == 0:
print(0)
elif k % 2 == 0:
print(2)
else:
print(1) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
This is the easy version of the problem. The difference between the versions is that the easy version does not require you to output the numbers of the rods to be removed. You can make hacks only if all versions of the problem are solved.
Stitch likes experimenting with different machines with his friend Sparky. Today they built another machine.
The main element of this machine are $n$ rods arranged along one straight line and numbered from $1$ to $n$ inclusive. Each of these rods must carry an electric charge quantitatively equal to either $1$ or $-1$ (otherwise the machine will not work). Another condition for this machine to work is that the sign-variable sum of the charge on all rods must be zero.
More formally, the rods can be represented as an array of $n$ numbers characterizing the charge: either $1$ or $-1$. Then the condition must hold: $a_1 - a_2 + a_3 - a_4 + \ldots = 0$, or $\sum\limits_{i=1}^n (-1)^{i-1} \cdot a_i = 0$.
Sparky charged all $n$ rods with an electric current, but unfortunately it happened that the rods were not charged correctly (the sign-variable sum of the charge is not zero). The friends decided to leave only some of the rods in the machine. Sparky has $q$ questions. In the $i$th question Sparky asks: if the machine consisted only of rods with numbers $l_i$ to $r_i$ inclusive, what minimal number of rods could be removed from the machine so that the sign-variable sum of charges on the remaining ones would be zero? Perhaps the friends got something wrong, and the sign-variable sum is already zero. In that case, you don't have to remove the rods at all.
If the number of rods is zero, we will assume that the sign-variable sum of charges is zero, that is, we can always remove all rods.
Help your friends and answer all of Sparky's questions!
-----Input-----
Each test contains multiple test cases.
The first line contains one positive integer $t$ ($1 \le t \le 10^3$), denoting the number of test cases. Description of the test cases follows.
The first line of each test case contains two positive integers $n$ and $q$ ($1 \le n, q \le 3 \cdot 10^5$) — the number of rods and the number of questions.
The second line of each test case contains a non-empty string $s$ of length $n$, where the charge of the $i$-th rod is $1$ if $s_i$ is the "+" symbol, or $-1$ if $s_i$ is the "-" symbol.
Each next line from the next $q$ lines contains two positive integers $l_i$ ans $r_i$ ($1 \le l_i \le r_i \le n$) — numbers, describing Sparky's questions.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$, and the sum of $q$ over all test cases does not exceed $3 \cdot 10^5$.
-----Output-----
For each test case, print a single integer — the minimal number of rods that can be removed.
-----Examples-----
Input
3
14 1
+--++---++-++-
1 14
14 3
+--++---+++---
1 14
6 12
3 10
4 10
+-+-
1 1
1 2
1 3
1 4
2 2
2 3
2 4
3 3
3 4
4 4
Output
2
2
1
0
1
2
1
2
1
2
1
1
2
1
-----Note-----
In the first test case for the first query you can remove the rods numbered $5$ and $8$, then the following set of rods will remain: +--+--++-++-. It is easy to see that here the sign-variable sum is zero.
In the second test case:
For the first query, we can remove the rods numbered $1$ and $11$, then the following set of rods will remain: --++---++---. It is easy to see that here the sign-variable sum is zero.
For the second query we can remove the rod numbered $9$, then the following set of rods will remain: ---++-. It is easy to see that here the variable sum is zero.
For the third query we can not remove the rods at all. | import sys
input = sys.stdin.readline
for _ in range(int(input())):
n, q = map(int, input().split())
s = [i for i in input()]
for i in range(n):
if s[i] == "+":
s[i] = 1
else:
s[i] = -1
pre = [0] * (n + 1)
for i in range(n):
curr = 0
if i & 1:
curr = -1 * s[i]
else:
curr = s[i]
pre[i + 1] = pre[i] + curr
for i in range(q):
l, r = map(int, input().split())
l -= 1
if r - l & 1:
print(1)
elif pre[r] == pre[l]:
print(0)
else:
print(2) | IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
This is the easy version of the problem. The difference between the versions is that the easy version does not require you to output the numbers of the rods to be removed. You can make hacks only if all versions of the problem are solved.
Stitch likes experimenting with different machines with his friend Sparky. Today they built another machine.
The main element of this machine are $n$ rods arranged along one straight line and numbered from $1$ to $n$ inclusive. Each of these rods must carry an electric charge quantitatively equal to either $1$ or $-1$ (otherwise the machine will not work). Another condition for this machine to work is that the sign-variable sum of the charge on all rods must be zero.
More formally, the rods can be represented as an array of $n$ numbers characterizing the charge: either $1$ or $-1$. Then the condition must hold: $a_1 - a_2 + a_3 - a_4 + \ldots = 0$, or $\sum\limits_{i=1}^n (-1)^{i-1} \cdot a_i = 0$.
Sparky charged all $n$ rods with an electric current, but unfortunately it happened that the rods were not charged correctly (the sign-variable sum of the charge is not zero). The friends decided to leave only some of the rods in the machine. Sparky has $q$ questions. In the $i$th question Sparky asks: if the machine consisted only of rods with numbers $l_i$ to $r_i$ inclusive, what minimal number of rods could be removed from the machine so that the sign-variable sum of charges on the remaining ones would be zero? Perhaps the friends got something wrong, and the sign-variable sum is already zero. In that case, you don't have to remove the rods at all.
If the number of rods is zero, we will assume that the sign-variable sum of charges is zero, that is, we can always remove all rods.
Help your friends and answer all of Sparky's questions!
-----Input-----
Each test contains multiple test cases.
The first line contains one positive integer $t$ ($1 \le t \le 10^3$), denoting the number of test cases. Description of the test cases follows.
The first line of each test case contains two positive integers $n$ and $q$ ($1 \le n, q \le 3 \cdot 10^5$) — the number of rods and the number of questions.
The second line of each test case contains a non-empty string $s$ of length $n$, where the charge of the $i$-th rod is $1$ if $s_i$ is the "+" symbol, or $-1$ if $s_i$ is the "-" symbol.
Each next line from the next $q$ lines contains two positive integers $l_i$ ans $r_i$ ($1 \le l_i \le r_i \le n$) — numbers, describing Sparky's questions.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$, and the sum of $q$ over all test cases does not exceed $3 \cdot 10^5$.
-----Output-----
For each test case, print a single integer — the minimal number of rods that can be removed.
-----Examples-----
Input
3
14 1
+--++---++-++-
1 14
14 3
+--++---+++---
1 14
6 12
3 10
4 10
+-+-
1 1
1 2
1 3
1 4
2 2
2 3
2 4
3 3
3 4
4 4
Output
2
2
1
0
1
2
1
2
1
2
1
1
2
1
-----Note-----
In the first test case for the first query you can remove the rods numbered $5$ and $8$, then the following set of rods will remain: +--+--++-++-. It is easy to see that here the sign-variable sum is zero.
In the second test case:
For the first query, we can remove the rods numbered $1$ and $11$, then the following set of rods will remain: --++---++---. It is easy to see that here the sign-variable sum is zero.
For the second query we can remove the rod numbered $9$, then the following set of rods will remain: ---++-. It is easy to see that here the variable sum is zero.
For the third query we can not remove the rods at all. | from sys import stdin
input = stdin.readline
rn = lambda: int(input())
rns = lambda: map(int, input().split())
rl = lambda: list(map(int, input().split()))
rs = lambda: input().strip()
YN = lambda x: print("YES") if x else print("NO")
mod = 10**9 + 7
for _ in range(rn()):
n, q = rns()
s = rs()
suff = [(0) for i in range(n)]
for i in range(n - 1, -1, -1):
suff[i] += s[i] == ["+", "-"][i % 2]
if i != n - 1:
suff[i] += suff[i + 1]
suff.append(0)
for __ in range(q):
l, r = rns()
l -= 1
mis = suff[l] - suff[r]
diff = r - l
if mis == diff / 2:
print(0)
elif diff % 2 == 1:
print(1)
else:
print(2) | ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL 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 FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR VAR LIST STRING STRING BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
This is the easy version of the problem. The difference between the versions is that the easy version does not require you to output the numbers of the rods to be removed. You can make hacks only if all versions of the problem are solved.
Stitch likes experimenting with different machines with his friend Sparky. Today they built another machine.
The main element of this machine are $n$ rods arranged along one straight line and numbered from $1$ to $n$ inclusive. Each of these rods must carry an electric charge quantitatively equal to either $1$ or $-1$ (otherwise the machine will not work). Another condition for this machine to work is that the sign-variable sum of the charge on all rods must be zero.
More formally, the rods can be represented as an array of $n$ numbers characterizing the charge: either $1$ or $-1$. Then the condition must hold: $a_1 - a_2 + a_3 - a_4 + \ldots = 0$, or $\sum\limits_{i=1}^n (-1)^{i-1} \cdot a_i = 0$.
Sparky charged all $n$ rods with an electric current, but unfortunately it happened that the rods were not charged correctly (the sign-variable sum of the charge is not zero). The friends decided to leave only some of the rods in the machine. Sparky has $q$ questions. In the $i$th question Sparky asks: if the machine consisted only of rods with numbers $l_i$ to $r_i$ inclusive, what minimal number of rods could be removed from the machine so that the sign-variable sum of charges on the remaining ones would be zero? Perhaps the friends got something wrong, and the sign-variable sum is already zero. In that case, you don't have to remove the rods at all.
If the number of rods is zero, we will assume that the sign-variable sum of charges is zero, that is, we can always remove all rods.
Help your friends and answer all of Sparky's questions!
-----Input-----
Each test contains multiple test cases.
The first line contains one positive integer $t$ ($1 \le t \le 10^3$), denoting the number of test cases. Description of the test cases follows.
The first line of each test case contains two positive integers $n$ and $q$ ($1 \le n, q \le 3 \cdot 10^5$) — the number of rods and the number of questions.
The second line of each test case contains a non-empty string $s$ of length $n$, where the charge of the $i$-th rod is $1$ if $s_i$ is the "+" symbol, or $-1$ if $s_i$ is the "-" symbol.
Each next line from the next $q$ lines contains two positive integers $l_i$ ans $r_i$ ($1 \le l_i \le r_i \le n$) — numbers, describing Sparky's questions.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$, and the sum of $q$ over all test cases does not exceed $3 \cdot 10^5$.
-----Output-----
For each test case, print a single integer — the minimal number of rods that can be removed.
-----Examples-----
Input
3
14 1
+--++---++-++-
1 14
14 3
+--++---+++---
1 14
6 12
3 10
4 10
+-+-
1 1
1 2
1 3
1 4
2 2
2 3
2 4
3 3
3 4
4 4
Output
2
2
1
0
1
2
1
2
1
2
1
1
2
1
-----Note-----
In the first test case for the first query you can remove the rods numbered $5$ and $8$, then the following set of rods will remain: +--+--++-++-. It is easy to see that here the sign-variable sum is zero.
In the second test case:
For the first query, we can remove the rods numbered $1$ and $11$, then the following set of rods will remain: --++---++---. It is easy to see that here the sign-variable sum is zero.
For the second query we can remove the rod numbered $9$, then the following set of rods will remain: ---++-. It is easy to see that here the variable sum is zero.
For the third query we can not remove the rods at all. | from itertools import accumulate
from sys import stdin, stdout
input = stdin.readline
def print(x):
return stdout.write(str(x) + "\n")
for _ in range(int(input())):
n, q = map(int, input().split())
s = input()
nums = []
for i in range(n):
if s[i] == "+" and i % 2 == 0 or s[i] == "-" and i % 2 == 1:
nums.append(1)
else:
nums.append(0)
a = list(accumulate(nums))
for _ in range(q):
li, ri = map(int, input().split())
if (ri - li) % 2 == 0:
print(1)
elif li == 1 and a[ri - 1] == ri // 2:
print(0)
elif li != 1 and a[ri - 1] - a[li - 2] == (ri - li + 1) // 2:
print(0)
else:
print(2) | ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING BIN_OP VAR NUMBER NUMBER VAR VAR STRING BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
This is the easy version of the problem. The difference between the versions is that the easy version does not require you to output the numbers of the rods to be removed. You can make hacks only if all versions of the problem are solved.
Stitch likes experimenting with different machines with his friend Sparky. Today they built another machine.
The main element of this machine are $n$ rods arranged along one straight line and numbered from $1$ to $n$ inclusive. Each of these rods must carry an electric charge quantitatively equal to either $1$ or $-1$ (otherwise the machine will not work). Another condition for this machine to work is that the sign-variable sum of the charge on all rods must be zero.
More formally, the rods can be represented as an array of $n$ numbers characterizing the charge: either $1$ or $-1$. Then the condition must hold: $a_1 - a_2 + a_3 - a_4 + \ldots = 0$, or $\sum\limits_{i=1}^n (-1)^{i-1} \cdot a_i = 0$.
Sparky charged all $n$ rods with an electric current, but unfortunately it happened that the rods were not charged correctly (the sign-variable sum of the charge is not zero). The friends decided to leave only some of the rods in the machine. Sparky has $q$ questions. In the $i$th question Sparky asks: if the machine consisted only of rods with numbers $l_i$ to $r_i$ inclusive, what minimal number of rods could be removed from the machine so that the sign-variable sum of charges on the remaining ones would be zero? Perhaps the friends got something wrong, and the sign-variable sum is already zero. In that case, you don't have to remove the rods at all.
If the number of rods is zero, we will assume that the sign-variable sum of charges is zero, that is, we can always remove all rods.
Help your friends and answer all of Sparky's questions!
-----Input-----
Each test contains multiple test cases.
The first line contains one positive integer $t$ ($1 \le t \le 10^3$), denoting the number of test cases. Description of the test cases follows.
The first line of each test case contains two positive integers $n$ and $q$ ($1 \le n, q \le 3 \cdot 10^5$) — the number of rods and the number of questions.
The second line of each test case contains a non-empty string $s$ of length $n$, where the charge of the $i$-th rod is $1$ if $s_i$ is the "+" symbol, or $-1$ if $s_i$ is the "-" symbol.
Each next line from the next $q$ lines contains two positive integers $l_i$ ans $r_i$ ($1 \le l_i \le r_i \le n$) — numbers, describing Sparky's questions.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$, and the sum of $q$ over all test cases does not exceed $3 \cdot 10^5$.
-----Output-----
For each test case, print a single integer — the minimal number of rods that can be removed.
-----Examples-----
Input
3
14 1
+--++---++-++-
1 14
14 3
+--++---+++---
1 14
6 12
3 10
4 10
+-+-
1 1
1 2
1 3
1 4
2 2
2 3
2 4
3 3
3 4
4 4
Output
2
2
1
0
1
2
1
2
1
2
1
1
2
1
-----Note-----
In the first test case for the first query you can remove the rods numbered $5$ and $8$, then the following set of rods will remain: +--+--++-++-. It is easy to see that here the sign-variable sum is zero.
In the second test case:
For the first query, we can remove the rods numbered $1$ and $11$, then the following set of rods will remain: --++---++---. It is easy to see that here the sign-variable sum is zero.
For the second query we can remove the rod numbered $9$, then the following set of rods will remain: ---++-. It is easy to see that here the variable sum is zero.
For the third query we can not remove the rods at all. | import sys
input = sys.stdin.readline
for _ in range(int(input())):
n, q = map(int, input().split())
s = input().strip()
a = 0
p = [0] * (n + 1)
for i, j in enumerate(s):
a += 1 if (j == "+") ^ i % 2 else -1
p[i + 1] = a
for _ in range(q):
l, r = map(int, input().split())
a = p[r] - p[l - 1]
print(1 if a % 2 else 2 if a else 0) | IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR STRING BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER |
This is the easy version of the problem. The difference between the versions is that the easy version does not require you to output the numbers of the rods to be removed. You can make hacks only if all versions of the problem are solved.
Stitch likes experimenting with different machines with his friend Sparky. Today they built another machine.
The main element of this machine are $n$ rods arranged along one straight line and numbered from $1$ to $n$ inclusive. Each of these rods must carry an electric charge quantitatively equal to either $1$ or $-1$ (otherwise the machine will not work). Another condition for this machine to work is that the sign-variable sum of the charge on all rods must be zero.
More formally, the rods can be represented as an array of $n$ numbers characterizing the charge: either $1$ or $-1$. Then the condition must hold: $a_1 - a_2 + a_3 - a_4 + \ldots = 0$, or $\sum\limits_{i=1}^n (-1)^{i-1} \cdot a_i = 0$.
Sparky charged all $n$ rods with an electric current, but unfortunately it happened that the rods were not charged correctly (the sign-variable sum of the charge is not zero). The friends decided to leave only some of the rods in the machine. Sparky has $q$ questions. In the $i$th question Sparky asks: if the machine consisted only of rods with numbers $l_i$ to $r_i$ inclusive, what minimal number of rods could be removed from the machine so that the sign-variable sum of charges on the remaining ones would be zero? Perhaps the friends got something wrong, and the sign-variable sum is already zero. In that case, you don't have to remove the rods at all.
If the number of rods is zero, we will assume that the sign-variable sum of charges is zero, that is, we can always remove all rods.
Help your friends and answer all of Sparky's questions!
-----Input-----
Each test contains multiple test cases.
The first line contains one positive integer $t$ ($1 \le t \le 10^3$), denoting the number of test cases. Description of the test cases follows.
The first line of each test case contains two positive integers $n$ and $q$ ($1 \le n, q \le 3 \cdot 10^5$) — the number of rods and the number of questions.
The second line of each test case contains a non-empty string $s$ of length $n$, where the charge of the $i$-th rod is $1$ if $s_i$ is the "+" symbol, or $-1$ if $s_i$ is the "-" symbol.
Each next line from the next $q$ lines contains two positive integers $l_i$ ans $r_i$ ($1 \le l_i \le r_i \le n$) — numbers, describing Sparky's questions.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$, and the sum of $q$ over all test cases does not exceed $3 \cdot 10^5$.
-----Output-----
For each test case, print a single integer — the minimal number of rods that can be removed.
-----Examples-----
Input
3
14 1
+--++---++-++-
1 14
14 3
+--++---+++---
1 14
6 12
3 10
4 10
+-+-
1 1
1 2
1 3
1 4
2 2
2 3
2 4
3 3
3 4
4 4
Output
2
2
1
0
1
2
1
2
1
2
1
1
2
1
-----Note-----
In the first test case for the first query you can remove the rods numbered $5$ and $8$, then the following set of rods will remain: +--+--++-++-. It is easy to see that here the sign-variable sum is zero.
In the second test case:
For the first query, we can remove the rods numbered $1$ and $11$, then the following set of rods will remain: --++---++---. It is easy to see that here the sign-variable sum is zero.
For the second query we can remove the rod numbered $9$, then the following set of rods will remain: ---++-. It is easy to see that here the variable sum is zero.
For the third query we can not remove the rods at all. | answer = []
for i in range(int(input())):
_, question_count = map(int, input().split(" "))
sums = [0]
sign = 1
for charge in input():
change = sign if charge == "+" else -sign
sign *= -1
sums.append(sums[-1] + change)
for _ in range(question_count):
mn, mx = map(int, input().split(" "))
sm = sums[mx] - sums[mn - 1]
if sm == 0:
answer.append(0)
elif sm % 2:
answer.append(1)
else:
answer.append(2)
for i in answer:
print(i) | ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR ASSIGN VAR VAR STRING VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR |
This is the easy version of the problem. The difference between the versions is that the easy version does not require you to output the numbers of the rods to be removed. You can make hacks only if all versions of the problem are solved.
Stitch likes experimenting with different machines with his friend Sparky. Today they built another machine.
The main element of this machine are $n$ rods arranged along one straight line and numbered from $1$ to $n$ inclusive. Each of these rods must carry an electric charge quantitatively equal to either $1$ or $-1$ (otherwise the machine will not work). Another condition for this machine to work is that the sign-variable sum of the charge on all rods must be zero.
More formally, the rods can be represented as an array of $n$ numbers characterizing the charge: either $1$ or $-1$. Then the condition must hold: $a_1 - a_2 + a_3 - a_4 + \ldots = 0$, or $\sum\limits_{i=1}^n (-1)^{i-1} \cdot a_i = 0$.
Sparky charged all $n$ rods with an electric current, but unfortunately it happened that the rods were not charged correctly (the sign-variable sum of the charge is not zero). The friends decided to leave only some of the rods in the machine. Sparky has $q$ questions. In the $i$th question Sparky asks: if the machine consisted only of rods with numbers $l_i$ to $r_i$ inclusive, what minimal number of rods could be removed from the machine so that the sign-variable sum of charges on the remaining ones would be zero? Perhaps the friends got something wrong, and the sign-variable sum is already zero. In that case, you don't have to remove the rods at all.
If the number of rods is zero, we will assume that the sign-variable sum of charges is zero, that is, we can always remove all rods.
Help your friends and answer all of Sparky's questions!
-----Input-----
Each test contains multiple test cases.
The first line contains one positive integer $t$ ($1 \le t \le 10^3$), denoting the number of test cases. Description of the test cases follows.
The first line of each test case contains two positive integers $n$ and $q$ ($1 \le n, q \le 3 \cdot 10^5$) — the number of rods and the number of questions.
The second line of each test case contains a non-empty string $s$ of length $n$, where the charge of the $i$-th rod is $1$ if $s_i$ is the "+" symbol, or $-1$ if $s_i$ is the "-" symbol.
Each next line from the next $q$ lines contains two positive integers $l_i$ ans $r_i$ ($1 \le l_i \le r_i \le n$) — numbers, describing Sparky's questions.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$, and the sum of $q$ over all test cases does not exceed $3 \cdot 10^5$.
-----Output-----
For each test case, print a single integer — the minimal number of rods that can be removed.
-----Examples-----
Input
3
14 1
+--++---++-++-
1 14
14 3
+--++---+++---
1 14
6 12
3 10
4 10
+-+-
1 1
1 2
1 3
1 4
2 2
2 3
2 4
3 3
3 4
4 4
Output
2
2
1
0
1
2
1
2
1
2
1
1
2
1
-----Note-----
In the first test case for the first query you can remove the rods numbered $5$ and $8$, then the following set of rods will remain: +--+--++-++-. It is easy to see that here the sign-variable sum is zero.
In the second test case:
For the first query, we can remove the rods numbered $1$ and $11$, then the following set of rods will remain: --++---++---. It is easy to see that here the sign-variable sum is zero.
For the second query we can remove the rod numbered $9$, then the following set of rods will remain: ---++-. It is easy to see that here the variable sum is zero.
For the third query we can not remove the rods at all. | n = int(input())
i = 0
lo = []
def C(Q):
w = r = 0
at = [0]
while w < len(Q):
if Q[w] == "+":
r = r + 1 - 2 * (w % 2)
else:
r = r - 1 + 2 * (w % 2)
at.append(r)
w += 1
return at
while i < n:
i1 = input().split()
Q = input()
sp = C(Q)
u = 0
while u < int(i1[1]):
Z = list(map(int, input().split()))
z1 = Z[0]
z2 = Z[1]
s = sp[z2] - sp[z1 - 1]
if s == 0:
d = 0
else:
d = 2 - s % 2
lo.append(d)
u += 1
i += 1
i = 0
while i < len(lo):
print(lo[i])
i += 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR LIST NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR WHILE VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER |
This is the easy version of the problem. The difference between the versions is that the easy version does not require you to output the numbers of the rods to be removed. You can make hacks only if all versions of the problem are solved.
Stitch likes experimenting with different machines with his friend Sparky. Today they built another machine.
The main element of this machine are $n$ rods arranged along one straight line and numbered from $1$ to $n$ inclusive. Each of these rods must carry an electric charge quantitatively equal to either $1$ or $-1$ (otherwise the machine will not work). Another condition for this machine to work is that the sign-variable sum of the charge on all rods must be zero.
More formally, the rods can be represented as an array of $n$ numbers characterizing the charge: either $1$ or $-1$. Then the condition must hold: $a_1 - a_2 + a_3 - a_4 + \ldots = 0$, or $\sum\limits_{i=1}^n (-1)^{i-1} \cdot a_i = 0$.
Sparky charged all $n$ rods with an electric current, but unfortunately it happened that the rods were not charged correctly (the sign-variable sum of the charge is not zero). The friends decided to leave only some of the rods in the machine. Sparky has $q$ questions. In the $i$th question Sparky asks: if the machine consisted only of rods with numbers $l_i$ to $r_i$ inclusive, what minimal number of rods could be removed from the machine so that the sign-variable sum of charges on the remaining ones would be zero? Perhaps the friends got something wrong, and the sign-variable sum is already zero. In that case, you don't have to remove the rods at all.
If the number of rods is zero, we will assume that the sign-variable sum of charges is zero, that is, we can always remove all rods.
Help your friends and answer all of Sparky's questions!
-----Input-----
Each test contains multiple test cases.
The first line contains one positive integer $t$ ($1 \le t \le 10^3$), denoting the number of test cases. Description of the test cases follows.
The first line of each test case contains two positive integers $n$ and $q$ ($1 \le n, q \le 3 \cdot 10^5$) — the number of rods and the number of questions.
The second line of each test case contains a non-empty string $s$ of length $n$, where the charge of the $i$-th rod is $1$ if $s_i$ is the "+" symbol, or $-1$ if $s_i$ is the "-" symbol.
Each next line from the next $q$ lines contains two positive integers $l_i$ ans $r_i$ ($1 \le l_i \le r_i \le n$) — numbers, describing Sparky's questions.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$, and the sum of $q$ over all test cases does not exceed $3 \cdot 10^5$.
-----Output-----
For each test case, print a single integer — the minimal number of rods that can be removed.
-----Examples-----
Input
3
14 1
+--++---++-++-
1 14
14 3
+--++---+++---
1 14
6 12
3 10
4 10
+-+-
1 1
1 2
1 3
1 4
2 2
2 3
2 4
3 3
3 4
4 4
Output
2
2
1
0
1
2
1
2
1
2
1
1
2
1
-----Note-----
In the first test case for the first query you can remove the rods numbered $5$ and $8$, then the following set of rods will remain: +--+--++-++-. It is easy to see that here the sign-variable sum is zero.
In the second test case:
For the first query, we can remove the rods numbered $1$ and $11$, then the following set of rods will remain: --++---++---. It is easy to see that here the sign-variable sum is zero.
For the second query we can remove the rod numbered $9$, then the following set of rods will remain: ---++-. It is easy to see that here the variable sum is zero.
For the third query we can not remove the rods at all. | from sys import stdin, stdout
ip = lambda: stdin.readline().rstrip("\r\n")
ips = lambda: ip().split()
out = lambda x, end="\n": stdout.write(str(x) + end)
inf = float("inf")
for _ in range(int(ip())):
n, q = map(int, ips())
s = ip()
np = nn = 0
a = [None] * n
b = [None] * n
c = [None] * n
d = [None] * n
for i in range(n):
if i % 2 == 0:
if s[i] == "+":
np += 1
else:
nn += 1
elif s[i] == "+":
nn += 1
else:
np += 1
a[i] = np
b[i] = nn
for i in range(q):
l, r = map(int, ips())
l -= 1
r -= 1
if l == r:
out(1)
continue
nph = a[r]
nnh = b[r]
if l - 1 >= 0:
nph -= a[l - 1]
nnh -= b[l - 1]
if abs(nph - nnh) == 0:
out(0)
continue
if abs(nph - nnh) % 2 == 0:
out(2)
else:
out(1) | ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR STRING FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR BIN_OP LIST NONE VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
This is the easy version of the problem. The difference between the versions is that the easy version does not require you to output the numbers of the rods to be removed. You can make hacks only if all versions of the problem are solved.
Stitch likes experimenting with different machines with his friend Sparky. Today they built another machine.
The main element of this machine are $n$ rods arranged along one straight line and numbered from $1$ to $n$ inclusive. Each of these rods must carry an electric charge quantitatively equal to either $1$ or $-1$ (otherwise the machine will not work). Another condition for this machine to work is that the sign-variable sum of the charge on all rods must be zero.
More formally, the rods can be represented as an array of $n$ numbers characterizing the charge: either $1$ or $-1$. Then the condition must hold: $a_1 - a_2 + a_3 - a_4 + \ldots = 0$, or $\sum\limits_{i=1}^n (-1)^{i-1} \cdot a_i = 0$.
Sparky charged all $n$ rods with an electric current, but unfortunately it happened that the rods were not charged correctly (the sign-variable sum of the charge is not zero). The friends decided to leave only some of the rods in the machine. Sparky has $q$ questions. In the $i$th question Sparky asks: if the machine consisted only of rods with numbers $l_i$ to $r_i$ inclusive, what minimal number of rods could be removed from the machine so that the sign-variable sum of charges on the remaining ones would be zero? Perhaps the friends got something wrong, and the sign-variable sum is already zero. In that case, you don't have to remove the rods at all.
If the number of rods is zero, we will assume that the sign-variable sum of charges is zero, that is, we can always remove all rods.
Help your friends and answer all of Sparky's questions!
-----Input-----
Each test contains multiple test cases.
The first line contains one positive integer $t$ ($1 \le t \le 10^3$), denoting the number of test cases. Description of the test cases follows.
The first line of each test case contains two positive integers $n$ and $q$ ($1 \le n, q \le 3 \cdot 10^5$) — the number of rods and the number of questions.
The second line of each test case contains a non-empty string $s$ of length $n$, where the charge of the $i$-th rod is $1$ if $s_i$ is the "+" symbol, or $-1$ if $s_i$ is the "-" symbol.
Each next line from the next $q$ lines contains two positive integers $l_i$ ans $r_i$ ($1 \le l_i \le r_i \le n$) — numbers, describing Sparky's questions.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$, and the sum of $q$ over all test cases does not exceed $3 \cdot 10^5$.
-----Output-----
For each test case, print a single integer — the minimal number of rods that can be removed.
-----Examples-----
Input
3
14 1
+--++---++-++-
1 14
14 3
+--++---+++---
1 14
6 12
3 10
4 10
+-+-
1 1
1 2
1 3
1 4
2 2
2 3
2 4
3 3
3 4
4 4
Output
2
2
1
0
1
2
1
2
1
2
1
1
2
1
-----Note-----
In the first test case for the first query you can remove the rods numbered $5$ and $8$, then the following set of rods will remain: +--+--++-++-. It is easy to see that here the sign-variable sum is zero.
In the second test case:
For the first query, we can remove the rods numbered $1$ and $11$, then the following set of rods will remain: --++---++---. It is easy to see that here the sign-variable sum is zero.
For the second query we can remove the rod numbered $9$, then the following set of rods will remain: ---++-. It is easy to see that here the variable sum is zero.
For the third query we can not remove the rods at all. | import sys
input = sys.stdin.readline
for nt in range(int(input())):
n, q = map(int, input().split())
s = input()
a = []
for i in range(n):
if i % 2:
if s[i] == "+":
a.append(-1)
else:
a.append(1)
elif s[i] == "+":
a.append(1)
else:
a.append(-1)
p = [0]
for i in range(n):
p.append(p[-1] + a[i])
for i in range(q):
l, r = map(int, input().split())
if p[r] - p[l - 1] == 0:
print(0)
continue
if (r - l + 1) % 2:
print(1)
else:
print(2) | IMPORT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
This is the easy version of the problem. The difference between the versions is that the easy version does not require you to output the numbers of the rods to be removed. You can make hacks only if all versions of the problem are solved.
Stitch likes experimenting with different machines with his friend Sparky. Today they built another machine.
The main element of this machine are $n$ rods arranged along one straight line and numbered from $1$ to $n$ inclusive. Each of these rods must carry an electric charge quantitatively equal to either $1$ or $-1$ (otherwise the machine will not work). Another condition for this machine to work is that the sign-variable sum of the charge on all rods must be zero.
More formally, the rods can be represented as an array of $n$ numbers characterizing the charge: either $1$ or $-1$. Then the condition must hold: $a_1 - a_2 + a_3 - a_4 + \ldots = 0$, or $\sum\limits_{i=1}^n (-1)^{i-1} \cdot a_i = 0$.
Sparky charged all $n$ rods with an electric current, but unfortunately it happened that the rods were not charged correctly (the sign-variable sum of the charge is not zero). The friends decided to leave only some of the rods in the machine. Sparky has $q$ questions. In the $i$th question Sparky asks: if the machine consisted only of rods with numbers $l_i$ to $r_i$ inclusive, what minimal number of rods could be removed from the machine so that the sign-variable sum of charges on the remaining ones would be zero? Perhaps the friends got something wrong, and the sign-variable sum is already zero. In that case, you don't have to remove the rods at all.
If the number of rods is zero, we will assume that the sign-variable sum of charges is zero, that is, we can always remove all rods.
Help your friends and answer all of Sparky's questions!
-----Input-----
Each test contains multiple test cases.
The first line contains one positive integer $t$ ($1 \le t \le 10^3$), denoting the number of test cases. Description of the test cases follows.
The first line of each test case contains two positive integers $n$ and $q$ ($1 \le n, q \le 3 \cdot 10^5$) — the number of rods and the number of questions.
The second line of each test case contains a non-empty string $s$ of length $n$, where the charge of the $i$-th rod is $1$ if $s_i$ is the "+" symbol, or $-1$ if $s_i$ is the "-" symbol.
Each next line from the next $q$ lines contains two positive integers $l_i$ ans $r_i$ ($1 \le l_i \le r_i \le n$) — numbers, describing Sparky's questions.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$, and the sum of $q$ over all test cases does not exceed $3 \cdot 10^5$.
-----Output-----
For each test case, print a single integer — the minimal number of rods that can be removed.
-----Examples-----
Input
3
14 1
+--++---++-++-
1 14
14 3
+--++---+++---
1 14
6 12
3 10
4 10
+-+-
1 1
1 2
1 3
1 4
2 2
2 3
2 4
3 3
3 4
4 4
Output
2
2
1
0
1
2
1
2
1
2
1
1
2
1
-----Note-----
In the first test case for the first query you can remove the rods numbered $5$ and $8$, then the following set of rods will remain: +--+--++-++-. It is easy to see that here the sign-variable sum is zero.
In the second test case:
For the first query, we can remove the rods numbered $1$ and $11$, then the following set of rods will remain: --++---++---. It is easy to see that here the sign-variable sum is zero.
For the second query we can remove the rod numbered $9$, then the following set of rods will remain: ---++-. It is easy to see that here the variable sum is zero.
For the third query we can not remove the rods at all. | import sys
input = sys.stdin.readline
T = int(input())
for iii in range(T):
n, q = map(int, input().split())
a = [0] + list(input()[:-1])
for i in range(1, n + 1):
if a[i] == "+":
a[i] = 1
else:
a[i] = -1
s = [0] * (n + 1)
for i in range(1, n + 1):
if i % 2 == 1:
s[i] = s[i - 1] + a[i]
else:
a[i] *= -1
s[i] = s[i - 1] + a[i]
for _ in range(q):
l, r = map(int, input().split())
res = s[r] - s[l - 1]
if res == 0:
print(0)
elif res % 2 == 1:
print(1)
else:
print(2) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
This is the easy version of the problem. The difference between the versions is that the easy version does not require you to output the numbers of the rods to be removed. You can make hacks only if all versions of the problem are solved.
Stitch likes experimenting with different machines with his friend Sparky. Today they built another machine.
The main element of this machine are $n$ rods arranged along one straight line and numbered from $1$ to $n$ inclusive. Each of these rods must carry an electric charge quantitatively equal to either $1$ or $-1$ (otherwise the machine will not work). Another condition for this machine to work is that the sign-variable sum of the charge on all rods must be zero.
More formally, the rods can be represented as an array of $n$ numbers characterizing the charge: either $1$ or $-1$. Then the condition must hold: $a_1 - a_2 + a_3 - a_4 + \ldots = 0$, or $\sum\limits_{i=1}^n (-1)^{i-1} \cdot a_i = 0$.
Sparky charged all $n$ rods with an electric current, but unfortunately it happened that the rods were not charged correctly (the sign-variable sum of the charge is not zero). The friends decided to leave only some of the rods in the machine. Sparky has $q$ questions. In the $i$th question Sparky asks: if the machine consisted only of rods with numbers $l_i$ to $r_i$ inclusive, what minimal number of rods could be removed from the machine so that the sign-variable sum of charges on the remaining ones would be zero? Perhaps the friends got something wrong, and the sign-variable sum is already zero. In that case, you don't have to remove the rods at all.
If the number of rods is zero, we will assume that the sign-variable sum of charges is zero, that is, we can always remove all rods.
Help your friends and answer all of Sparky's questions!
-----Input-----
Each test contains multiple test cases.
The first line contains one positive integer $t$ ($1 \le t \le 10^3$), denoting the number of test cases. Description of the test cases follows.
The first line of each test case contains two positive integers $n$ and $q$ ($1 \le n, q \le 3 \cdot 10^5$) — the number of rods and the number of questions.
The second line of each test case contains a non-empty string $s$ of length $n$, where the charge of the $i$-th rod is $1$ if $s_i$ is the "+" symbol, or $-1$ if $s_i$ is the "-" symbol.
Each next line from the next $q$ lines contains two positive integers $l_i$ ans $r_i$ ($1 \le l_i \le r_i \le n$) — numbers, describing Sparky's questions.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$, and the sum of $q$ over all test cases does not exceed $3 \cdot 10^5$.
-----Output-----
For each test case, print a single integer — the minimal number of rods that can be removed.
-----Examples-----
Input
3
14 1
+--++---++-++-
1 14
14 3
+--++---+++---
1 14
6 12
3 10
4 10
+-+-
1 1
1 2
1 3
1 4
2 2
2 3
2 4
3 3
3 4
4 4
Output
2
2
1
0
1
2
1
2
1
2
1
1
2
1
-----Note-----
In the first test case for the first query you can remove the rods numbered $5$ and $8$, then the following set of rods will remain: +--+--++-++-. It is easy to see that here the sign-variable sum is zero.
In the second test case:
For the first query, we can remove the rods numbered $1$ and $11$, then the following set of rods will remain: --++---++---. It is easy to see that here the sign-variable sum is zero.
For the second query we can remove the rod numbered $9$, then the following set of rods will remain: ---++-. It is easy to see that here the variable sum is zero.
For the third query we can not remove the rods at all. | import sys
ii = 0
inp = sys.stdin.read().split()
out = []
t = int(inp[ii])
ii += 1
for _ in range(t):
n = int(inp[ii])
ii += 1
q = int(inp[ii])
ii += 1
s = inp[ii]
ii += 1
a = 0
p = [0]
for i, j in enumerate(s):
a += 1 if (j == "+") ^ i & 1 else -1
p.append(a)
for _ in range(q):
l = int(inp[ii])
ii += 1
r = int(inp[ii])
ii += 1
a = p[r] - p[l - 1]
out.append("1" if a % 2 else "2" if a else "0")
print("\n".join(out)) | IMPORT ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR STRING BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING VAR STRING STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
This is the easy version of the problem. The difference between the versions is that the easy version does not require you to output the numbers of the rods to be removed. You can make hacks only if all versions of the problem are solved.
Stitch likes experimenting with different machines with his friend Sparky. Today they built another machine.
The main element of this machine are $n$ rods arranged along one straight line and numbered from $1$ to $n$ inclusive. Each of these rods must carry an electric charge quantitatively equal to either $1$ or $-1$ (otherwise the machine will not work). Another condition for this machine to work is that the sign-variable sum of the charge on all rods must be zero.
More formally, the rods can be represented as an array of $n$ numbers characterizing the charge: either $1$ or $-1$. Then the condition must hold: $a_1 - a_2 + a_3 - a_4 + \ldots = 0$, or $\sum\limits_{i=1}^n (-1)^{i-1} \cdot a_i = 0$.
Sparky charged all $n$ rods with an electric current, but unfortunately it happened that the rods were not charged correctly (the sign-variable sum of the charge is not zero). The friends decided to leave only some of the rods in the machine. Sparky has $q$ questions. In the $i$th question Sparky asks: if the machine consisted only of rods with numbers $l_i$ to $r_i$ inclusive, what minimal number of rods could be removed from the machine so that the sign-variable sum of charges on the remaining ones would be zero? Perhaps the friends got something wrong, and the sign-variable sum is already zero. In that case, you don't have to remove the rods at all.
If the number of rods is zero, we will assume that the sign-variable sum of charges is zero, that is, we can always remove all rods.
Help your friends and answer all of Sparky's questions!
-----Input-----
Each test contains multiple test cases.
The first line contains one positive integer $t$ ($1 \le t \le 10^3$), denoting the number of test cases. Description of the test cases follows.
The first line of each test case contains two positive integers $n$ and $q$ ($1 \le n, q \le 3 \cdot 10^5$) — the number of rods and the number of questions.
The second line of each test case contains a non-empty string $s$ of length $n$, where the charge of the $i$-th rod is $1$ if $s_i$ is the "+" symbol, or $-1$ if $s_i$ is the "-" symbol.
Each next line from the next $q$ lines contains two positive integers $l_i$ ans $r_i$ ($1 \le l_i \le r_i \le n$) — numbers, describing Sparky's questions.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$, and the sum of $q$ over all test cases does not exceed $3 \cdot 10^5$.
-----Output-----
For each test case, print a single integer — the minimal number of rods that can be removed.
-----Examples-----
Input
3
14 1
+--++---++-++-
1 14
14 3
+--++---+++---
1 14
6 12
3 10
4 10
+-+-
1 1
1 2
1 3
1 4
2 2
2 3
2 4
3 3
3 4
4 4
Output
2
2
1
0
1
2
1
2
1
2
1
1
2
1
-----Note-----
In the first test case for the first query you can remove the rods numbered $5$ and $8$, then the following set of rods will remain: +--+--++-++-. It is easy to see that here the sign-variable sum is zero.
In the second test case:
For the first query, we can remove the rods numbered $1$ and $11$, then the following set of rods will remain: --++---++---. It is easy to see that here the sign-variable sum is zero.
For the second query we can remove the rod numbered $9$, then the following set of rods will remain: ---++-. It is easy to see that here the variable sum is zero.
For the third query we can not remove the rods at all. | import sys
input = sys.stdin.readline
def inp():
return int(input())
def st():
return input().rstrip("\n")
def lis():
return list(map(int, input().split()))
def ma():
return map(int, input().split())
t = inp()
while t:
t -= 1
n, q = ma()
s = st()
pos = [0]
neg = [0]
npos = [0]
nneg = [0]
for i in range(n):
if s[i] == "-" and i % 2 == 0:
npos.append(1 + npos[-1])
nneg.append(1 + nneg[-1])
elif s[i] == "-" and i % 2:
nneg.append(1 + nneg[-1])
npos.append(npos[-1])
else:
nneg.append(nneg[-1])
npos.append(npos[-1])
if s[i] == "+" and i % 2 == 0:
pos.append(pos[-1] + 1)
neg.append(neg[-1])
elif s[i] == "-" and i % 2:
pos.append(pos[-1] + 1)
neg.append(neg[-1])
else:
neg.append(neg[-1] + 1)
pos.append(pos[-1])
for i in range(q):
l, r = ma()
cur1 = pos[r] - pos[l - 1]
cur2 = neg[r] - neg[l - 1]
if (r - l) % 2 == 0:
print(1)
elif cur1 == cur2:
print(0)
else:
print(2) | IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR NUMBER IF VAR VAR STRING BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR VAR STRING BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER IF VAR VAR STRING BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
This is the easy version of the problem. The difference between the versions is that the easy version does not require you to output the numbers of the rods to be removed. You can make hacks only if all versions of the problem are solved.
Stitch likes experimenting with different machines with his friend Sparky. Today they built another machine.
The main element of this machine are $n$ rods arranged along one straight line and numbered from $1$ to $n$ inclusive. Each of these rods must carry an electric charge quantitatively equal to either $1$ or $-1$ (otherwise the machine will not work). Another condition for this machine to work is that the sign-variable sum of the charge on all rods must be zero.
More formally, the rods can be represented as an array of $n$ numbers characterizing the charge: either $1$ or $-1$. Then the condition must hold: $a_1 - a_2 + a_3 - a_4 + \ldots = 0$, or $\sum\limits_{i=1}^n (-1)^{i-1} \cdot a_i = 0$.
Sparky charged all $n$ rods with an electric current, but unfortunately it happened that the rods were not charged correctly (the sign-variable sum of the charge is not zero). The friends decided to leave only some of the rods in the machine. Sparky has $q$ questions. In the $i$th question Sparky asks: if the machine consisted only of rods with numbers $l_i$ to $r_i$ inclusive, what minimal number of rods could be removed from the machine so that the sign-variable sum of charges on the remaining ones would be zero? Perhaps the friends got something wrong, and the sign-variable sum is already zero. In that case, you don't have to remove the rods at all.
If the number of rods is zero, we will assume that the sign-variable sum of charges is zero, that is, we can always remove all rods.
Help your friends and answer all of Sparky's questions!
-----Input-----
Each test contains multiple test cases.
The first line contains one positive integer $t$ ($1 \le t \le 10^3$), denoting the number of test cases. Description of the test cases follows.
The first line of each test case contains two positive integers $n$ and $q$ ($1 \le n, q \le 3 \cdot 10^5$) — the number of rods and the number of questions.
The second line of each test case contains a non-empty string $s$ of length $n$, where the charge of the $i$-th rod is $1$ if $s_i$ is the "+" symbol, or $-1$ if $s_i$ is the "-" symbol.
Each next line from the next $q$ lines contains two positive integers $l_i$ ans $r_i$ ($1 \le l_i \le r_i \le n$) — numbers, describing Sparky's questions.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$, and the sum of $q$ over all test cases does not exceed $3 \cdot 10^5$.
-----Output-----
For each test case, print a single integer — the minimal number of rods that can be removed.
-----Examples-----
Input
3
14 1
+--++---++-++-
1 14
14 3
+--++---+++---
1 14
6 12
3 10
4 10
+-+-
1 1
1 2
1 3
1 4
2 2
2 3
2 4
3 3
3 4
4 4
Output
2
2
1
0
1
2
1
2
1
2
1
1
2
1
-----Note-----
In the first test case for the first query you can remove the rods numbered $5$ and $8$, then the following set of rods will remain: +--+--++-++-. It is easy to see that here the sign-variable sum is zero.
In the second test case:
For the first query, we can remove the rods numbered $1$ and $11$, then the following set of rods will remain: --++---++---. It is easy to see that here the sign-variable sum is zero.
For the second query we can remove the rod numbered $9$, then the following set of rods will remain: ---++-. It is easy to see that here the variable sum is zero.
For the third query we can not remove the rods at all. | test = int(input())
for _ in range(test):
n, q = map(int, input().split())
s = input()
sumi = 0
rangesumi = [0] * (n + 1)
for i in range(len(s)):
if i % 2 == 0:
if s[i] == "+"[0]:
sumi += 1
else:
sumi -= 1
elif s[i] == "+"[0]:
sumi -= 1
else:
sumi += 1
rangesumi[i + 1] = sumi
ans = ""
for i in range(q):
l, r = map(int, input().split())
sumi = rangesumi[r] - rangesumi[l - 1]
if sumi == 0:
ans += "0"
elif (r - l + 1) % 2 == 1:
ans += "1"
else:
ans += "2"
for i in ans:
print(i) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING NUMBER VAR NUMBER VAR NUMBER IF VAR VAR STRING NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR STRING IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER VAR STRING VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR |
This is the easy version of the problem. The difference between the versions is that the easy version does not require you to output the numbers of the rods to be removed. You can make hacks only if all versions of the problem are solved.
Stitch likes experimenting with different machines with his friend Sparky. Today they built another machine.
The main element of this machine are $n$ rods arranged along one straight line and numbered from $1$ to $n$ inclusive. Each of these rods must carry an electric charge quantitatively equal to either $1$ or $-1$ (otherwise the machine will not work). Another condition for this machine to work is that the sign-variable sum of the charge on all rods must be zero.
More formally, the rods can be represented as an array of $n$ numbers characterizing the charge: either $1$ or $-1$. Then the condition must hold: $a_1 - a_2 + a_3 - a_4 + \ldots = 0$, or $\sum\limits_{i=1}^n (-1)^{i-1} \cdot a_i = 0$.
Sparky charged all $n$ rods with an electric current, but unfortunately it happened that the rods were not charged correctly (the sign-variable sum of the charge is not zero). The friends decided to leave only some of the rods in the machine. Sparky has $q$ questions. In the $i$th question Sparky asks: if the machine consisted only of rods with numbers $l_i$ to $r_i$ inclusive, what minimal number of rods could be removed from the machine so that the sign-variable sum of charges on the remaining ones would be zero? Perhaps the friends got something wrong, and the sign-variable sum is already zero. In that case, you don't have to remove the rods at all.
If the number of rods is zero, we will assume that the sign-variable sum of charges is zero, that is, we can always remove all rods.
Help your friends and answer all of Sparky's questions!
-----Input-----
Each test contains multiple test cases.
The first line contains one positive integer $t$ ($1 \le t \le 10^3$), denoting the number of test cases. Description of the test cases follows.
The first line of each test case contains two positive integers $n$ and $q$ ($1 \le n, q \le 3 \cdot 10^5$) — the number of rods and the number of questions.
The second line of each test case contains a non-empty string $s$ of length $n$, where the charge of the $i$-th rod is $1$ if $s_i$ is the "+" symbol, or $-1$ if $s_i$ is the "-" symbol.
Each next line from the next $q$ lines contains two positive integers $l_i$ ans $r_i$ ($1 \le l_i \le r_i \le n$) — numbers, describing Sparky's questions.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$, and the sum of $q$ over all test cases does not exceed $3 \cdot 10^5$.
-----Output-----
For each test case, print a single integer — the minimal number of rods that can be removed.
-----Examples-----
Input
3
14 1
+--++---++-++-
1 14
14 3
+--++---+++---
1 14
6 12
3 10
4 10
+-+-
1 1
1 2
1 3
1 4
2 2
2 3
2 4
3 3
3 4
4 4
Output
2
2
1
0
1
2
1
2
1
2
1
1
2
1
-----Note-----
In the first test case for the first query you can remove the rods numbered $5$ and $8$, then the following set of rods will remain: +--+--++-++-. It is easy to see that here the sign-variable sum is zero.
In the second test case:
For the first query, we can remove the rods numbered $1$ and $11$, then the following set of rods will remain: --++---++---. It is easy to see that here the sign-variable sum is zero.
For the second query we can remove the rod numbered $9$, then the following set of rods will remain: ---++-. It is easy to see that here the variable sum is zero.
For the third query we can not remove the rods at all. | import sys
input = lambda: sys.stdin.readline()
for _ in range(int(input())):
n, q = map(int, input().split())
s = input()
a = [0]
t = -1
for i in s:
t *= -1
if i == "+":
t1 = 1
else:
t1 = -1
a.append(t1 * t)
sm = [0, a[1]]
for i in range(2, n + 1):
sm.append(sm[i - 1] + a[i])
ans = []
for i in range(q):
l, r = map(int, input().split())
if (r - l) % 2 == 0:
ans.append("1")
elif sm[r] - sm[l - 1] == 0:
ans.append("0")
else:
ans.append("2")
print("\n".join(ans)) | IMPORT ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR NUMBER IF VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR LIST NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
This is the easy version of the problem. The difference between the versions is that the easy version does not require you to output the numbers of the rods to be removed. You can make hacks only if all versions of the problem are solved.
Stitch likes experimenting with different machines with his friend Sparky. Today they built another machine.
The main element of this machine are $n$ rods arranged along one straight line and numbered from $1$ to $n$ inclusive. Each of these rods must carry an electric charge quantitatively equal to either $1$ or $-1$ (otherwise the machine will not work). Another condition for this machine to work is that the sign-variable sum of the charge on all rods must be zero.
More formally, the rods can be represented as an array of $n$ numbers characterizing the charge: either $1$ or $-1$. Then the condition must hold: $a_1 - a_2 + a_3 - a_4 + \ldots = 0$, or $\sum\limits_{i=1}^n (-1)^{i-1} \cdot a_i = 0$.
Sparky charged all $n$ rods with an electric current, but unfortunately it happened that the rods were not charged correctly (the sign-variable sum of the charge is not zero). The friends decided to leave only some of the rods in the machine. Sparky has $q$ questions. In the $i$th question Sparky asks: if the machine consisted only of rods with numbers $l_i$ to $r_i$ inclusive, what minimal number of rods could be removed from the machine so that the sign-variable sum of charges on the remaining ones would be zero? Perhaps the friends got something wrong, and the sign-variable sum is already zero. In that case, you don't have to remove the rods at all.
If the number of rods is zero, we will assume that the sign-variable sum of charges is zero, that is, we can always remove all rods.
Help your friends and answer all of Sparky's questions!
-----Input-----
Each test contains multiple test cases.
The first line contains one positive integer $t$ ($1 \le t \le 10^3$), denoting the number of test cases. Description of the test cases follows.
The first line of each test case contains two positive integers $n$ and $q$ ($1 \le n, q \le 3 \cdot 10^5$) — the number of rods and the number of questions.
The second line of each test case contains a non-empty string $s$ of length $n$, where the charge of the $i$-th rod is $1$ if $s_i$ is the "+" symbol, or $-1$ if $s_i$ is the "-" symbol.
Each next line from the next $q$ lines contains two positive integers $l_i$ ans $r_i$ ($1 \le l_i \le r_i \le n$) — numbers, describing Sparky's questions.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$, and the sum of $q$ over all test cases does not exceed $3 \cdot 10^5$.
-----Output-----
For each test case, print a single integer — the minimal number of rods that can be removed.
-----Examples-----
Input
3
14 1
+--++---++-++-
1 14
14 3
+--++---+++---
1 14
6 12
3 10
4 10
+-+-
1 1
1 2
1 3
1 4
2 2
2 3
2 4
3 3
3 4
4 4
Output
2
2
1
0
1
2
1
2
1
2
1
1
2
1
-----Note-----
In the first test case for the first query you can remove the rods numbered $5$ and $8$, then the following set of rods will remain: +--+--++-++-. It is easy to see that here the sign-variable sum is zero.
In the second test case:
For the first query, we can remove the rods numbered $1$ and $11$, then the following set of rods will remain: --++---++---. It is easy to see that here the sign-variable sum is zero.
For the second query we can remove the rod numbered $9$, then the following set of rods will remain: ---++-. It is easy to see that here the variable sum is zero.
For the third query we can not remove the rods at all. | from sys import stdin
input = stdin.readline
def f(s):
s = list(s)
d = {"+": 1, "-": -1}
d2 = {(1): "+", (-1): "-"}
d3 = {"+": 0, "-": 1}
pref = []
for i in range(len(s)):
pp = d2[d[s[i]] * pow(-1, i)]
if i == 0:
ll = [0, 0]
ll[d3[pp]] += 1
pref.append(ll)
else:
ll = pref[-1].copy()
ll[d3[pp]] += 1
pref.append(ll)
return pref
for t in range(int(input())):
n, m = map(int, input().strip().split())
s = input().strip()
pref = f(s)
for i in range(m):
l, r = map(int, input().strip().split())
l -= 1
r -= 1
ll = r - l + 1
lr = [0, 0]
if l != 0:
lr = pref[l - 1]
rr = pref[r]
cp = rr[0] - lr[0]
cp2 = rr[1] - lr[1]
if cp == cp2:
print(0)
elif ll % 2 == 0:
print(2)
else:
print(1) | ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT STRING STRING NUMBER NUMBER ASSIGN VAR DICT NUMBER NUMBER STRING STRING ASSIGN VAR DICT STRING STRING NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
This is the easy version of the problem. The difference between the versions is that the easy version does not require you to output the numbers of the rods to be removed. You can make hacks only if all versions of the problem are solved.
Stitch likes experimenting with different machines with his friend Sparky. Today they built another machine.
The main element of this machine are $n$ rods arranged along one straight line and numbered from $1$ to $n$ inclusive. Each of these rods must carry an electric charge quantitatively equal to either $1$ or $-1$ (otherwise the machine will not work). Another condition for this machine to work is that the sign-variable sum of the charge on all rods must be zero.
More formally, the rods can be represented as an array of $n$ numbers characterizing the charge: either $1$ or $-1$. Then the condition must hold: $a_1 - a_2 + a_3 - a_4 + \ldots = 0$, or $\sum\limits_{i=1}^n (-1)^{i-1} \cdot a_i = 0$.
Sparky charged all $n$ rods with an electric current, but unfortunately it happened that the rods were not charged correctly (the sign-variable sum of the charge is not zero). The friends decided to leave only some of the rods in the machine. Sparky has $q$ questions. In the $i$th question Sparky asks: if the machine consisted only of rods with numbers $l_i$ to $r_i$ inclusive, what minimal number of rods could be removed from the machine so that the sign-variable sum of charges on the remaining ones would be zero? Perhaps the friends got something wrong, and the sign-variable sum is already zero. In that case, you don't have to remove the rods at all.
If the number of rods is zero, we will assume that the sign-variable sum of charges is zero, that is, we can always remove all rods.
Help your friends and answer all of Sparky's questions!
-----Input-----
Each test contains multiple test cases.
The first line contains one positive integer $t$ ($1 \le t \le 10^3$), denoting the number of test cases. Description of the test cases follows.
The first line of each test case contains two positive integers $n$ and $q$ ($1 \le n, q \le 3 \cdot 10^5$) — the number of rods and the number of questions.
The second line of each test case contains a non-empty string $s$ of length $n$, where the charge of the $i$-th rod is $1$ if $s_i$ is the "+" symbol, or $-1$ if $s_i$ is the "-" symbol.
Each next line from the next $q$ lines contains two positive integers $l_i$ ans $r_i$ ($1 \le l_i \le r_i \le n$) — numbers, describing Sparky's questions.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$, and the sum of $q$ over all test cases does not exceed $3 \cdot 10^5$.
-----Output-----
For each test case, print a single integer — the minimal number of rods that can be removed.
-----Examples-----
Input
3
14 1
+--++---++-++-
1 14
14 3
+--++---+++---
1 14
6 12
3 10
4 10
+-+-
1 1
1 2
1 3
1 4
2 2
2 3
2 4
3 3
3 4
4 4
Output
2
2
1
0
1
2
1
2
1
2
1
1
2
1
-----Note-----
In the first test case for the first query you can remove the rods numbered $5$ and $8$, then the following set of rods will remain: +--+--++-++-. It is easy to see that here the sign-variable sum is zero.
In the second test case:
For the first query, we can remove the rods numbered $1$ and $11$, then the following set of rods will remain: --++---++---. It is easy to see that here the sign-variable sum is zero.
For the second query we can remove the rod numbered $9$, then the following set of rods will remain: ---++-. It is easy to see that here the variable sum is zero.
For the third query we can not remove the rods at all. | import sys
input = sys.stdin.readline
def main():
n, q = map(int, input().split())
S = input().strip()
cum = [0]
for i, s in enumerate(S):
if (s == "+") ^ (i % 2 == 0):
cum.append(cum[-1] + 1)
else:
cum.append(cum[-1] - 1)
for _ in range(q):
l, r = map(int, input().split())
d = cum[r] - cum[l - 1]
if d == 0:
print(0)
elif d % 2 == 1:
print(1)
else:
print(2)
for _ in range(int(input())):
main() | IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR STRING BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR |
This is the easy version of the problem. The difference between the versions is that the easy version does not require you to output the numbers of the rods to be removed. You can make hacks only if all versions of the problem are solved.
Stitch likes experimenting with different machines with his friend Sparky. Today they built another machine.
The main element of this machine are $n$ rods arranged along one straight line and numbered from $1$ to $n$ inclusive. Each of these rods must carry an electric charge quantitatively equal to either $1$ or $-1$ (otherwise the machine will not work). Another condition for this machine to work is that the sign-variable sum of the charge on all rods must be zero.
More formally, the rods can be represented as an array of $n$ numbers characterizing the charge: either $1$ or $-1$. Then the condition must hold: $a_1 - a_2 + a_3 - a_4 + \ldots = 0$, or $\sum\limits_{i=1}^n (-1)^{i-1} \cdot a_i = 0$.
Sparky charged all $n$ rods with an electric current, but unfortunately it happened that the rods were not charged correctly (the sign-variable sum of the charge is not zero). The friends decided to leave only some of the rods in the machine. Sparky has $q$ questions. In the $i$th question Sparky asks: if the machine consisted only of rods with numbers $l_i$ to $r_i$ inclusive, what minimal number of rods could be removed from the machine so that the sign-variable sum of charges on the remaining ones would be zero? Perhaps the friends got something wrong, and the sign-variable sum is already zero. In that case, you don't have to remove the rods at all.
If the number of rods is zero, we will assume that the sign-variable sum of charges is zero, that is, we can always remove all rods.
Help your friends and answer all of Sparky's questions!
-----Input-----
Each test contains multiple test cases.
The first line contains one positive integer $t$ ($1 \le t \le 10^3$), denoting the number of test cases. Description of the test cases follows.
The first line of each test case contains two positive integers $n$ and $q$ ($1 \le n, q \le 3 \cdot 10^5$) — the number of rods and the number of questions.
The second line of each test case contains a non-empty string $s$ of length $n$, where the charge of the $i$-th rod is $1$ if $s_i$ is the "+" symbol, or $-1$ if $s_i$ is the "-" symbol.
Each next line from the next $q$ lines contains two positive integers $l_i$ ans $r_i$ ($1 \le l_i \le r_i \le n$) — numbers, describing Sparky's questions.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$, and the sum of $q$ over all test cases does not exceed $3 \cdot 10^5$.
-----Output-----
For each test case, print a single integer — the minimal number of rods that can be removed.
-----Examples-----
Input
3
14 1
+--++---++-++-
1 14
14 3
+--++---+++---
1 14
6 12
3 10
4 10
+-+-
1 1
1 2
1 3
1 4
2 2
2 3
2 4
3 3
3 4
4 4
Output
2
2
1
0
1
2
1
2
1
2
1
1
2
1
-----Note-----
In the first test case for the first query you can remove the rods numbered $5$ and $8$, then the following set of rods will remain: +--+--++-++-. It is easy to see that here the sign-variable sum is zero.
In the second test case:
For the first query, we can remove the rods numbered $1$ and $11$, then the following set of rods will remain: --++---++---. It is easy to see that here the sign-variable sum is zero.
For the second query we can remove the rod numbered $9$, then the following set of rods will remain: ---++-. It is easy to see that here the variable sum is zero.
For the third query we can not remove the rods at all. | import sys
input = sys.stdin.readline
tests = int(input())
for test in range(tests):
n, q = map(int, input().split())
s = str.rstrip(input())
sum_list = [0]
oddeven = 1
sum = 0
for char in s:
if char == "+":
sum += oddeven
else:
sum -= oddeven
oddeven *= -1
sum_list.append(sum)
for question in range(q):
l, r = map(int, input().split())
if (r - l) % 2 == 0:
print(1)
elif sum_list[r] - sum_list[l - 1] == 0:
print(0)
else:
print(2) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
This is the easy version of the problem. The difference between the versions is that the easy version does not require you to output the numbers of the rods to be removed. You can make hacks only if all versions of the problem are solved.
Stitch likes experimenting with different machines with his friend Sparky. Today they built another machine.
The main element of this machine are $n$ rods arranged along one straight line and numbered from $1$ to $n$ inclusive. Each of these rods must carry an electric charge quantitatively equal to either $1$ or $-1$ (otherwise the machine will not work). Another condition for this machine to work is that the sign-variable sum of the charge on all rods must be zero.
More formally, the rods can be represented as an array of $n$ numbers characterizing the charge: either $1$ or $-1$. Then the condition must hold: $a_1 - a_2 + a_3 - a_4 + \ldots = 0$, or $\sum\limits_{i=1}^n (-1)^{i-1} \cdot a_i = 0$.
Sparky charged all $n$ rods with an electric current, but unfortunately it happened that the rods were not charged correctly (the sign-variable sum of the charge is not zero). The friends decided to leave only some of the rods in the machine. Sparky has $q$ questions. In the $i$th question Sparky asks: if the machine consisted only of rods with numbers $l_i$ to $r_i$ inclusive, what minimal number of rods could be removed from the machine so that the sign-variable sum of charges on the remaining ones would be zero? Perhaps the friends got something wrong, and the sign-variable sum is already zero. In that case, you don't have to remove the rods at all.
If the number of rods is zero, we will assume that the sign-variable sum of charges is zero, that is, we can always remove all rods.
Help your friends and answer all of Sparky's questions!
-----Input-----
Each test contains multiple test cases.
The first line contains one positive integer $t$ ($1 \le t \le 10^3$), denoting the number of test cases. Description of the test cases follows.
The first line of each test case contains two positive integers $n$ and $q$ ($1 \le n, q \le 3 \cdot 10^5$) — the number of rods and the number of questions.
The second line of each test case contains a non-empty string $s$ of length $n$, where the charge of the $i$-th rod is $1$ if $s_i$ is the "+" symbol, or $-1$ if $s_i$ is the "-" symbol.
Each next line from the next $q$ lines contains two positive integers $l_i$ ans $r_i$ ($1 \le l_i \le r_i \le n$) — numbers, describing Sparky's questions.
It is guaranteed that the sum of $n$ over all test cases does not exceed $3 \cdot 10^5$, and the sum of $q$ over all test cases does not exceed $3 \cdot 10^5$.
-----Output-----
For each test case, print a single integer — the minimal number of rods that can be removed.
-----Examples-----
Input
3
14 1
+--++---++-++-
1 14
14 3
+--++---+++---
1 14
6 12
3 10
4 10
+-+-
1 1
1 2
1 3
1 4
2 2
2 3
2 4
3 3
3 4
4 4
Output
2
2
1
0
1
2
1
2
1
2
1
1
2
1
-----Note-----
In the first test case for the first query you can remove the rods numbered $5$ and $8$, then the following set of rods will remain: +--+--++-++-. It is easy to see that here the sign-variable sum is zero.
In the second test case:
For the first query, we can remove the rods numbered $1$ and $11$, then the following set of rods will remain: --++---++---. It is easy to see that here the sign-variable sum is zero.
For the second query we can remove the rod numbered $9$, then the following set of rods will remain: ---++-. It is easy to see that here the variable sum is zero.
For the third query we can not remove the rods at all. | import sys
input = sys.stdin.readline
T = int(input())
for _ in range(T):
n, q = map(int, input().split())
s = input()
pre_sum = [0]
sign = 1
for i in s:
if i == "+":
x = sign
else:
x = -sign
pre_sum.append(pre_sum[-1] + x)
sign *= -1
for qq in range(q):
l, r = map(int, input().split())
sum = pre_sum[r] - pre_sum[l - 1]
if sum == 0:
print(0)
elif sum & 1:
print(1)
else:
print(2) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.
Example 1:
Input: matrix =
[
[0,1,1,1],
[1,1,1,1],
[0,1,1,1]
]
Output: 15
Explanation:
There are 10 squares of side 1.
There are 4 squares of side 2.
There is 1 square of side 3.
Total number of squares = 10 + 4 + 1 = 15.
Example 2:
Input: matrix =
[
[1,0,1],
[1,1,0],
[1,1,0]
]
Output: 7
Explanation:
There are 6 squares of side 1.
There is 1 square of side 2.
Total number of squares = 6 + 1 = 7.
Constraints:
1 <= arr.length <= 300
1 <= arr[0].length <= 300
0 <= arr[i][j] <= 1 | class Solution:
def countSquares(self, matrix: List[List[int]]) -> int:
vertMemo = [([0] * len(matrix[0])) for _ in range(len(matrix))]
horizMemo = [([0] * len(matrix[0])) for _ in range(len(matrix))]
diagMemo = [([0] * len(matrix[0])) for _ in range(len(matrix))]
squareMemo = [([0] * len(matrix[0])) for _ in range(len(matrix))]
if matrix[0][0] == 1:
vertMemo[0][0] = 1
diagMemo[0][0] = 1
horizMemo[0][0] = 1
squareMemo[0][0] = 1
for i in range(1, len(matrix[0])):
if matrix[0][i] == 1:
vertMemo[0][i] = 1
diagMemo[0][i] = 1
squareMemo[0][i] = 1
horizMemo[0][i] = horizMemo[0][i - 1] + 1
for i in range(1, len(matrix)):
if matrix[i][0] == 1:
vertMemo[i][0] = vertMemo[i - 1][0] + 1
diagMemo[i][0] = 1
squareMemo[i][0] = 1
horizMemo[i][0] = 1
for i in range(1, len(matrix)):
for j in range(1, len(matrix[0])):
if matrix[i][j] == 1:
vertMemo[i][j] = vertMemo[i - 1][j] + 1
horizMemo[i][j] = horizMemo[i][j - 1] + 1
diagMemo[i][j] = diagMemo[i - 1][j - 1] + 1
squareMemo[i][j] = min(
squareMemo[i - 1][j - 1] + 1, vertMemo[i][j], horizMemo[i][j]
)
print(squareMemo)
totSquares = 0
for i in range(len(matrix)):
for j in range(len(matrix[0])):
if squareMemo[i][j] != 0:
totSquares += squareMemo[i][j]
return totSquares | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR RETURN VAR VAR |
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.
Example 1:
Input: matrix =
[
[0,1,1,1],
[1,1,1,1],
[0,1,1,1]
]
Output: 15
Explanation:
There are 10 squares of side 1.
There are 4 squares of side 2.
There is 1 square of side 3.
Total number of squares = 10 + 4 + 1 = 15.
Example 2:
Input: matrix =
[
[1,0,1],
[1,1,0],
[1,1,0]
]
Output: 7
Explanation:
There are 6 squares of side 1.
There is 1 square of side 2.
Total number of squares = 6 + 1 = 7.
Constraints:
1 <= arr.length <= 300
1 <= arr[0].length <= 300
0 <= arr[i][j] <= 1 | class Solution:
def countSquares(self, matrix: List[List[int]]) -> int:
row, col = len(matrix), len(matrix[0])
f = [([0] * col) for _ in range(row)]
for i in range(row):
for j in range(col):
if matrix[i][j] == 0:
f[i][j] = 0
continue
if i == 0 or j == 0:
f[i][j] = matrix[i][j]
continue
f[i][j] = min(f[i - 1][j], f[i][j - 1], f[i - 1][j - 1]) + 1
return sum([sum(f[i]) for i in range(row)]) | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR |
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.
Example 1:
Input: matrix =
[
[0,1,1,1],
[1,1,1,1],
[0,1,1,1]
]
Output: 15
Explanation:
There are 10 squares of side 1.
There are 4 squares of side 2.
There is 1 square of side 3.
Total number of squares = 10 + 4 + 1 = 15.
Example 2:
Input: matrix =
[
[1,0,1],
[1,1,0],
[1,1,0]
]
Output: 7
Explanation:
There are 6 squares of side 1.
There is 1 square of side 2.
Total number of squares = 6 + 1 = 7.
Constraints:
1 <= arr.length <= 300
1 <= arr[0].length <= 300
0 <= arr[i][j] <= 1 | class Solution:
def countSquares(self, matrix: List[List[int]]) -> int:
if matrix is None or len(matrix) == 0 or len(matrix[0]) == 0:
return 0
height = len(matrix)
length = len(matrix[0])
one_locations, square_submatrices = self.get_one_locations(matrix)
current_count = 0
for size in range(2, min(height, length) + 1):
if size != 2 and current_count < 4:
return square_submatrices
current_count = 0
for location in one_locations:
h = location[0]
l = location[1]
if h + size - 1 < len(matrix) and l + size - 1 < len(matrix[0]):
if self.check_all_ones(matrix, h, l, size):
current_count += 1
square_submatrices += current_count
return square_submatrices
def check_all_ones(self, matrix, height, length, square_size):
for i in range(height, height + square_size):
for j in range(length, length + square_size):
if matrix[i][j] != 1:
return False
return True
def get_one_locations(self, matrix):
one_locations = set()
one_count = 0
for i, j in enumerate(matrix):
for m, n in enumerate(matrix[i]):
if n == 1:
one_locations.add((i, m))
one_count += 1
return one_locations, one_count | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NONE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR VAR RETURN VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR VAR |
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.
Example 1:
Input: matrix =
[
[0,1,1,1],
[1,1,1,1],
[0,1,1,1]
]
Output: 15
Explanation:
There are 10 squares of side 1.
There are 4 squares of side 2.
There is 1 square of side 3.
Total number of squares = 10 + 4 + 1 = 15.
Example 2:
Input: matrix =
[
[1,0,1],
[1,1,0],
[1,1,0]
]
Output: 7
Explanation:
There are 6 squares of side 1.
There is 1 square of side 2.
Total number of squares = 6 + 1 = 7.
Constraints:
1 <= arr.length <= 300
1 <= arr[0].length <= 300
0 <= arr[i][j] <= 1 | class Solution:
def countSquares(self, matrix: List[List[int]]) -> int:
count = 0
size = 1
maxSize = min(len(matrix), len(matrix[0]))
prevInd = []
for i in range(len(matrix)):
for j in range(len(matrix[0])):
if matrix[i][j] == 1:
count += 1
prevInd.append([i, j])
while size <= maxSize and prevInd:
newInd = []
for ind in prevInd:
row = ind[0]
col = ind[1]
add = True
if row + size < len(matrix) and col + size < len(matrix[0]):
for r in range(row, row + size + 1):
if not matrix[r][col + size]:
add = False
break
for c in range(col, col + size + 1):
if not matrix[row + size][c]:
add = False
break
if add:
count += 1
newInd.append([row, col])
prevInd = newInd
size += 1
return count | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR WHILE VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR VAR |
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.
Example 1:
Input: matrix =
[
[0,1,1,1],
[1,1,1,1],
[0,1,1,1]
]
Output: 15
Explanation:
There are 10 squares of side 1.
There are 4 squares of side 2.
There is 1 square of side 3.
Total number of squares = 10 + 4 + 1 = 15.
Example 2:
Input: matrix =
[
[1,0,1],
[1,1,0],
[1,1,0]
]
Output: 7
Explanation:
There are 6 squares of side 1.
There is 1 square of side 2.
Total number of squares = 6 + 1 = 7.
Constraints:
1 <= arr.length <= 300
1 <= arr[0].length <= 300
0 <= arr[i][j] <= 1 | class Solution:
def countSquares(self, matrix: List[List[int]]) -> int:
n, m = len(matrix), len(matrix[0])
sub_sol = matrix[0][:]
row, col = sub_sol[:], [0] * m
squares = sum(sub_sol)
for i in range(1, n):
sol = []
for j in range(m):
val = matrix[i][j]
if j == 0:
col.append(val)
elif val == 0:
col.append(0)
else:
col.append(col[-1] + 1)
if val == 0:
row[j] = 0
else:
row[j] += 1
sol.append(min(col[-1], row[j], 1 + sub_sol[j - 1] if j > 0 else 1))
sub_sol = sol
squares += sum(sub_sol)
return squares | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER BIN_OP NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR |
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.
Example 1:
Input: matrix =
[
[0,1,1,1],
[1,1,1,1],
[0,1,1,1]
]
Output: 15
Explanation:
There are 10 squares of side 1.
There are 4 squares of side 2.
There is 1 square of side 3.
Total number of squares = 10 + 4 + 1 = 15.
Example 2:
Input: matrix =
[
[1,0,1],
[1,1,0],
[1,1,0]
]
Output: 7
Explanation:
There are 6 squares of side 1.
There is 1 square of side 2.
Total number of squares = 6 + 1 = 7.
Constraints:
1 <= arr.length <= 300
1 <= arr[0].length <= 300
0 <= arr[i][j] <= 1 | class Solution:
def validate_sub_matrix(self, matrix, rstart, cstart, size):
for row in range(size + 1):
for col in range(size + 1):
if matrix[rstart + row][cstart + col] == 0:
return False
return True
def countSquares(self, matrix: List[List[int]]) -> int:
count = 0
num_rows = len(matrix)
num_cols = len(matrix[0])
for row in range(num_rows):
for col in range(num_cols):
if matrix[row][col] == 0:
continue
s_size = 0
while (
row + s_size < num_rows
and col + s_size < num_cols
and self.validate_sub_matrix(matrix, row, col, s_size)
):
count += 1
s_size += 1
return count | CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR |
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.
Example 1:
Input: matrix =
[
[0,1,1,1],
[1,1,1,1],
[0,1,1,1]
]
Output: 15
Explanation:
There are 10 squares of side 1.
There are 4 squares of side 2.
There is 1 square of side 3.
Total number of squares = 10 + 4 + 1 = 15.
Example 2:
Input: matrix =
[
[1,0,1],
[1,1,0],
[1,1,0]
]
Output: 7
Explanation:
There are 6 squares of side 1.
There is 1 square of side 2.
Total number of squares = 6 + 1 = 7.
Constraints:
1 <= arr.length <= 300
1 <= arr[0].length <= 300
0 <= arr[i][j] <= 1 | class Solution:
def countSquares(self, matrix: List[List[int]]) -> int:
if not matrix:
return 0
M, N = len(matrix), len(matrix[0])
if M == 1 or N == 1:
return 1
for m in range(1, M):
for n in range(1, N):
if matrix[m][n] == 1:
if matrix[m - 1][n] == matrix[m][n - 1] == matrix[m - 1][n - 1]:
matrix[m][n] = matrix[m - 1][n - 1] + 1
else:
matrix[m][n] = max(
min(
matrix[m - 1][n], matrix[m][n - 1], matrix[m - 1][n - 1]
)
+ 1,
matrix[m][n],
)
return sum(sum(matrix, [])) | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR LIST VAR |
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.
Example 1:
Input: matrix =
[
[0,1,1,1],
[1,1,1,1],
[0,1,1,1]
]
Output: 15
Explanation:
There are 10 squares of side 1.
There are 4 squares of side 2.
There is 1 square of side 3.
Total number of squares = 10 + 4 + 1 = 15.
Example 2:
Input: matrix =
[
[1,0,1],
[1,1,0],
[1,1,0]
]
Output: 7
Explanation:
There are 6 squares of side 1.
There is 1 square of side 2.
Total number of squares = 6 + 1 = 7.
Constraints:
1 <= arr.length <= 300
1 <= arr[0].length <= 300
0 <= arr[i][j] <= 1 | class Solution:
def countSquares(self, matrix: List[List[int]]) -> int:
def pref(x, y):
return 0 if x < 0 or y < 0 else f[x][y]
rows, cols = len(matrix), len(matrix[0]) if matrix else 0
res = 0
f = [[(0) for j in range(cols)] for i in range(rows)]
for i in range(rows):
for j in range(cols):
f[i][j] = (
0
if matrix[i][j] == 0
else min(pref(i - 1, j), pref(i, j - 1), pref(i - 1, j - 1)) + 1
)
res += f[i][j]
return res | CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF RETURN VAR NUMBER VAR NUMBER NUMBER VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR RETURN VAR VAR |
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.
Example 1:
Input: matrix =
[
[0,1,1,1],
[1,1,1,1],
[0,1,1,1]
]
Output: 15
Explanation:
There are 10 squares of side 1.
There are 4 squares of side 2.
There is 1 square of side 3.
Total number of squares = 10 + 4 + 1 = 15.
Example 2:
Input: matrix =
[
[1,0,1],
[1,1,0],
[1,1,0]
]
Output: 7
Explanation:
There are 6 squares of side 1.
There is 1 square of side 2.
Total number of squares = 6 + 1 = 7.
Constraints:
1 <= arr.length <= 300
1 <= arr[0].length <= 300
0 <= arr[i][j] <= 1 | class Solution:
def countSquares(self, matrix: List[List[int]]) -> int:
m, n = len(matrix), len(matrix[0])
diag = 0
square = [matrix[i][:] for i in range(m)]
total = 0
r, c = 0, 0
while diag < m + n - 1:
if 0 < r < m and 0 < c < n and matrix[r][c] > 0:
square[r][c] += min(
square[r][c - 1], square[r - 1][c], square[r - 1][c - 1]
)
total += square[r][c]
r += 1
c -= 1
if r > m - 1 or c < 0:
diag += 1
r = 0 if diag < n else diag - n + 1
c = diag - r
return total | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR BIN_OP BIN_OP VAR VAR NUMBER IF NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR RETURN VAR VAR |
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.
Example 1:
Input: matrix =
[
[0,1,1,1],
[1,1,1,1],
[0,1,1,1]
]
Output: 15
Explanation:
There are 10 squares of side 1.
There are 4 squares of side 2.
There is 1 square of side 3.
Total number of squares = 10 + 4 + 1 = 15.
Example 2:
Input: matrix =
[
[1,0,1],
[1,1,0],
[1,1,0]
]
Output: 7
Explanation:
There are 6 squares of side 1.
There is 1 square of side 2.
Total number of squares = 6 + 1 = 7.
Constraints:
1 <= arr.length <= 300
1 <= arr[0].length <= 300
0 <= arr[i][j] <= 1 | class Solution:
def countSquares(self, matrix: List[List[int]]) -> int:
total = sum(matrix[0])
for i in range(1, len(matrix)):
total += matrix[i][0]
for j in range(1, len(matrix[i])):
if matrix[i][j] == 0:
continue
matrix[i][j] = (
min(matrix[i - 1][j], matrix[i][j - 1], matrix[i - 1][j - 1]) + 1
)
total += matrix[i][j]
return total | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR RETURN VAR VAR |
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.
Example 1:
Input: matrix =
[
[0,1,1,1],
[1,1,1,1],
[0,1,1,1]
]
Output: 15
Explanation:
There are 10 squares of side 1.
There are 4 squares of side 2.
There is 1 square of side 3.
Total number of squares = 10 + 4 + 1 = 15.
Example 2:
Input: matrix =
[
[1,0,1],
[1,1,0],
[1,1,0]
]
Output: 7
Explanation:
There are 6 squares of side 1.
There is 1 square of side 2.
Total number of squares = 6 + 1 = 7.
Constraints:
1 <= arr.length <= 300
1 <= arr[0].length <= 300
0 <= arr[i][j] <= 1 | class Solution:
def countSquares(self, matrix: List[List[int]]) -> int:
res = [[(0) for _ in range(1 + len(matrix[0]))] for _ in range(1 + len(matrix))]
s = 0
m = 0
for i in range(1, len(res)):
for j in range(1, len(res[i])):
if matrix[i - 1][j - 1] == 1:
res[i][j] = 1 + min(res[i - 1][j], res[i][j - 1], res[i - 1][j - 1])
else:
res[i][j] = 0
s += res[i][j]
m = res[i][j] if res[i][j] > m else m
return s | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR |
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.
Example 1:
Input: matrix =
[
[0,1,1,1],
[1,1,1,1],
[0,1,1,1]
]
Output: 15
Explanation:
There are 10 squares of side 1.
There are 4 squares of side 2.
There is 1 square of side 3.
Total number of squares = 10 + 4 + 1 = 15.
Example 2:
Input: matrix =
[
[1,0,1],
[1,1,0],
[1,1,0]
]
Output: 7
Explanation:
There are 6 squares of side 1.
There is 1 square of side 2.
Total number of squares = 6 + 1 = 7.
Constraints:
1 <= arr.length <= 300
1 <= arr[0].length <= 300
0 <= arr[i][j] <= 1 | class Solution:
def countSquares(self, matrix: List[List[int]]) -> int:
cnt = 0
rows, cols = len(matrix), len(matrix[0])
dp = [([0] * (cols + 1)) for i in range(rows + 1)]
for i in range(1, rows + 1):
for j in range(1, cols + 1):
if matrix[i - 1][j - 1] == 1:
dp[i][j] = min(dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1]) + 1
cnt += dp[i][j]
return cnt | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR RETURN VAR VAR |
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.
Example 1:
Input: matrix =
[
[0,1,1,1],
[1,1,1,1],
[0,1,1,1]
]
Output: 15
Explanation:
There are 10 squares of side 1.
There are 4 squares of side 2.
There is 1 square of side 3.
Total number of squares = 10 + 4 + 1 = 15.
Example 2:
Input: matrix =
[
[1,0,1],
[1,1,0],
[1,1,0]
]
Output: 7
Explanation:
There are 6 squares of side 1.
There is 1 square of side 2.
Total number of squares = 6 + 1 = 7.
Constraints:
1 <= arr.length <= 300
1 <= arr[0].length <= 300
0 <= arr[i][j] <= 1 | class Solution:
def countSquares(self, matrix: List[List[int]]) -> int:
rows = len(matrix)
cols = len(matrix[0])
count = 0
for r in range(rows):
for c in range(cols):
if matrix[r][c] and r > 0 and c > 0:
matrix[r][c] = (
min(matrix[r - 1][c], matrix[r][c - 1], matrix[r - 1][c - 1])
+ 1
)
count += matrix[r][c]
return count | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR RETURN VAR VAR |
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.
Example 1:
Input: matrix =
[
[0,1,1,1],
[1,1,1,1],
[0,1,1,1]
]
Output: 15
Explanation:
There are 10 squares of side 1.
There are 4 squares of side 2.
There is 1 square of side 3.
Total number of squares = 10 + 4 + 1 = 15.
Example 2:
Input: matrix =
[
[1,0,1],
[1,1,0],
[1,1,0]
]
Output: 7
Explanation:
There are 6 squares of side 1.
There is 1 square of side 2.
Total number of squares = 6 + 1 = 7.
Constraints:
1 <= arr.length <= 300
1 <= arr[0].length <= 300
0 <= arr[i][j] <= 1 | class Solution:
def countSquares(self, matrix: List[List[int]]) -> int:
for row in range(1, len(matrix)):
for col in range(1, len(matrix[0])):
if matrix[row][col] == 1:
ul = matrix[row - 1][col - 1]
u = matrix[row - 1][col]
l = matrix[row][col - 1]
val = min(ul, u, l) + 1
matrix[row][col] = val
total = 0
for row in range(len(matrix)):
for col in range(len(matrix[0])):
total += matrix[row][col]
return total | CLASS_DEF FUNC_DEF VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR RETURN VAR VAR |
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.
Example 1:
Input: matrix =
[
[0,1,1,1],
[1,1,1,1],
[0,1,1,1]
]
Output: 15
Explanation:
There are 10 squares of side 1.
There are 4 squares of side 2.
There is 1 square of side 3.
Total number of squares = 10 + 4 + 1 = 15.
Example 2:
Input: matrix =
[
[1,0,1],
[1,1,0],
[1,1,0]
]
Output: 7
Explanation:
There are 6 squares of side 1.
There is 1 square of side 2.
Total number of squares = 6 + 1 = 7.
Constraints:
1 <= arr.length <= 300
1 <= arr[0].length <= 300
0 <= arr[i][j] <= 1 | class Solution:
def countSquares(self, matrix: List[List[int]]) -> int:
m = len(matrix)
n = len(matrix[0])
dp = []
for i in range(m):
temp = [0] * n
dp.append(temp)
for j in range(n):
if matrix[0][j] == 1:
dp[0][j] = 1
for i in range(1, m, 1):
if matrix[i][0] == 1:
dp[i][0] = 1
for i in range(1, m, 1):
for j in range(1, n, 1):
if matrix[i][j] == 1:
dp[i][j] = (
min(min(dp[i - 1][j], dp[i][j - 1]), dp[i - 1][j - 1]) + 1
)
ans = 0
for k in range(m):
for l in range(n):
ans += dp[k][l]
return ans | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR VAR |
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.
Example 1:
Input: matrix =
[
[0,1,1,1],
[1,1,1,1],
[0,1,1,1]
]
Output: 15
Explanation:
There are 10 squares of side 1.
There are 4 squares of side 2.
There is 1 square of side 3.
Total number of squares = 10 + 4 + 1 = 15.
Example 2:
Input: matrix =
[
[1,0,1],
[1,1,0],
[1,1,0]
]
Output: 7
Explanation:
There are 6 squares of side 1.
There is 1 square of side 2.
Total number of squares = 6 + 1 = 7.
Constraints:
1 <= arr.length <= 300
1 <= arr[0].length <= 300
0 <= arr[i][j] <= 1 | class Solution:
def countSquares(self, matrix: List[List[int]]) -> int:
rownum = len(matrix)
if rownum < 1:
return 0
colnum = len(matrix[0])
if colnum < 1:
return 0
aux3 = defaultdict(lambda: -1)
def check(r, c):
if r < 0 or r >= rownum or c < 0 or c >= colnum:
return 0
elif matrix[r][c] == 0:
return 0
elif aux3[r, c] != -1:
return aux3[r, c]
else:
c1 = check(r + 1, c)
c2 = check(r, c + 1)
c3 = check(r + 1, c + 1)
aux3[r, c] = min(c1, c2, c3) + 1
return aux3[r, c]
ret = 0
for r in range(0, rownum):
for c in range(0, colnum):
if matrix[r][c] == 1:
max_len = check(r, c)
ret += max_len
return ret | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER FUNC_DEF IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR VAR |
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.
Example 1:
Input: matrix =
[
[0,1,1,1],
[1,1,1,1],
[0,1,1,1]
]
Output: 15
Explanation:
There are 10 squares of side 1.
There are 4 squares of side 2.
There is 1 square of side 3.
Total number of squares = 10 + 4 + 1 = 15.
Example 2:
Input: matrix =
[
[1,0,1],
[1,1,0],
[1,1,0]
]
Output: 7
Explanation:
There are 6 squares of side 1.
There is 1 square of side 2.
Total number of squares = 6 + 1 = 7.
Constraints:
1 <= arr.length <= 300
1 <= arr[0].length <= 300
0 <= arr[i][j] <= 1 | class Solution:
def countSquares(self, matrix: List[List[int]]) -> int:
rows, cols = len(matrix), len(matrix[0])
dp = [(0) for i in range(cols + 1)]
tot = 0
for i in range(rows):
for j in range(cols):
if not matrix[i][j]:
dp[j + 1] = 0
continue
if dp[j + 1] == dp[j]:
dp[j + 1] += 1 if matrix[i - dp[j]][j - dp[j]] else 0
else:
dp[j + 1] = min(dp[j + 1], dp[j]) + 1
tot += dp[j + 1]
return tot | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR |
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.
Example 1:
Input: matrix =
[
[0,1,1,1],
[1,1,1,1],
[0,1,1,1]
]
Output: 15
Explanation:
There are 10 squares of side 1.
There are 4 squares of side 2.
There is 1 square of side 3.
Total number of squares = 10 + 4 + 1 = 15.
Example 2:
Input: matrix =
[
[1,0,1],
[1,1,0],
[1,1,0]
]
Output: 7
Explanation:
There are 6 squares of side 1.
There is 1 square of side 2.
Total number of squares = 6 + 1 = 7.
Constraints:
1 <= arr.length <= 300
1 <= arr[0].length <= 300
0 <= arr[i][j] <= 1 | class Solution:
def countSquares(self, dp):
if len(dp) == 0 or len(dp[0]) == 0:
return 0
for i, row in enumerate(dp):
for j, val in enumerate(row):
if val == 1:
left = dp[i][j - 1] if j > 0 else 0
up = dp[i - 1][j] if i > 0 else 0
diag = dp[i - 1][j - 1] if i > 0 and j > 0 else 0
dp[i][j] = min((left, up, diag)) + 1
else:
dp[i][j] = 0
return sum(map(sum, dp)) | CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER NUMBER RETURN NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.
Example 1:
Input: matrix =
[
[0,1,1,1],
[1,1,1,1],
[0,1,1,1]
]
Output: 15
Explanation:
There are 10 squares of side 1.
There are 4 squares of side 2.
There is 1 square of side 3.
Total number of squares = 10 + 4 + 1 = 15.
Example 2:
Input: matrix =
[
[1,0,1],
[1,1,0],
[1,1,0]
]
Output: 7
Explanation:
There are 6 squares of side 1.
There is 1 square of side 2.
Total number of squares = 6 + 1 = 7.
Constraints:
1 <= arr.length <= 300
1 <= arr[0].length <= 300
0 <= arr[i][j] <= 1 | class Solution:
def countSquares(self, matrix: List[List[int]]) -> int:
for i in range(len(matrix)):
for j in range(len(matrix[0])):
re = 0
if i > 0:
re += matrix[i - 1][j]
if j > 0:
re += matrix[i][j - 1]
if i > 0 and j > 0:
re -= matrix[i - 1][j - 1]
matrix[i][j] += re
print(matrix)
re = 0
for i in range(len(matrix)):
for j in range(len(matrix[0])):
l = 0
while i + l < len(matrix) and j + l < len(matrix[0]):
tmp = matrix[i + l][j + l]
if j > 0:
tmp -= matrix[i + l][j - 1]
if i > 0:
tmp -= matrix[i - 1][j + l]
if i > 0 and j > 0:
tmp += matrix[i - 1][j - 1]
if tmp != (l + 1) ** 2:
break
else:
re += 1
l += 1
return re | CLASS_DEF FUNC_DEF VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR |
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.
Example 1:
Input: matrix =
[
[0,1,1,1],
[1,1,1,1],
[0,1,1,1]
]
Output: 15
Explanation:
There are 10 squares of side 1.
There are 4 squares of side 2.
There is 1 square of side 3.
Total number of squares = 10 + 4 + 1 = 15.
Example 2:
Input: matrix =
[
[1,0,1],
[1,1,0],
[1,1,0]
]
Output: 7
Explanation:
There are 6 squares of side 1.
There is 1 square of side 2.
Total number of squares = 6 + 1 = 7.
Constraints:
1 <= arr.length <= 300
1 <= arr[0].length <= 300
0 <= arr[i][j] <= 1 | class Solution:
def countSquares(self, matrix: List[List[int]]) -> int:
num_squares = 0
for y in range(len(matrix)):
for x in range(len(matrix[0])):
num_squares += squaresAt(x, y, matrix)
return num_squares
def squaresAt(x: int, y: int, matrix: List[List[int]]) -> int:
size = 0
while True:
if y + size >= len(matrix):
break
if x + size >= len(matrix[0]):
break
good_row = checkRow(x, x + size, y + size, matrix)
good_col = checkCol(x + size, y, y + size, matrix)
if good_row and good_col:
size += 1
else:
break
return size
def checkRow(x1: int, x2: int, y: int, matrix: List[List[int]]) -> bool:
for x in range(x1, x2 + 1):
if matrix[y][x] == 0:
return False
return True
def checkCol(x: int, y1: int, y2: int, matrix: List[List[int]]) -> bool:
for y in range(y1, y2 + 1):
if matrix[y][x] == 0:
return False
return True | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR IF VAR VAR VAR NUMBER RETURN VAR VAR FUNC_DEF VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER VAR FUNC_DEF VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER VAR |
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.
Example 1:
Input: matrix =
[
[0,1,1,1],
[1,1,1,1],
[0,1,1,1]
]
Output: 15
Explanation:
There are 10 squares of side 1.
There are 4 squares of side 2.
There is 1 square of side 3.
Total number of squares = 10 + 4 + 1 = 15.
Example 2:
Input: matrix =
[
[1,0,1],
[1,1,0],
[1,1,0]
]
Output: 7
Explanation:
There are 6 squares of side 1.
There is 1 square of side 2.
Total number of squares = 6 + 1 = 7.
Constraints:
1 <= arr.length <= 300
1 <= arr[0].length <= 300
0 <= arr[i][j] <= 1 | class Solution:
def countSquares(self, matrix: List[List[int]]) -> int:
def get(i, j):
if i < 0 or j < 0:
return 0
elif i > len(matrix) or j > len(matrix[0]):
return 0
return matrix[i][j]
m = len(matrix)
n = len(matrix[0])
count = 0
for i in range(m):
for j in range(n):
if matrix[i][j] != 1:
continue
squares = min(get(i - 1, j - 1), get(i - 1, j), get(i, j - 1)) + 1
matrix[i][j] = squares
count += squares
return count | CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR VAR RETURN VAR VAR |
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.
Example 1:
Input: matrix =
[
[0,1,1,1],
[1,1,1,1],
[0,1,1,1]
]
Output: 15
Explanation:
There are 10 squares of side 1.
There are 4 squares of side 2.
There is 1 square of side 3.
Total number of squares = 10 + 4 + 1 = 15.
Example 2:
Input: matrix =
[
[1,0,1],
[1,1,0],
[1,1,0]
]
Output: 7
Explanation:
There are 6 squares of side 1.
There is 1 square of side 2.
Total number of squares = 6 + 1 = 7.
Constraints:
1 <= arr.length <= 300
1 <= arr[0].length <= 300
0 <= arr[i][j] <= 1 | class Solution:
def countSquares(self, matrix: List[List[int]]) -> int:
dp = matrix
r = len(dp)
c = len(dp[0])
ans = 0
for i in range(1, r):
for j in range(1, c):
if dp[i][j] == 0:
continue
if dp[i - 1][j] != 0 and dp[i - 1][j - 1] != 0 and dp[i][j - 1] != 0:
dp[i][j] = min(dp[i - 1][j - 1], dp[i][j - 1], dp[i - 1][j]) + 1
for i in dp:
ans += sum(i)
return ans | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR |
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.
Example 1:
Input: matrix =
[
[0,1,1,1],
[1,1,1,1],
[0,1,1,1]
]
Output: 15
Explanation:
There are 10 squares of side 1.
There are 4 squares of side 2.
There is 1 square of side 3.
Total number of squares = 10 + 4 + 1 = 15.
Example 2:
Input: matrix =
[
[1,0,1],
[1,1,0],
[1,1,0]
]
Output: 7
Explanation:
There are 6 squares of side 1.
There is 1 square of side 2.
Total number of squares = 6 + 1 = 7.
Constraints:
1 <= arr.length <= 300
1 <= arr[0].length <= 300
0 <= arr[i][j] <= 1 | class Solution:
def countSquares(self, matrix: List[List[int]]) -> int:
if not matrix:
return 0
count = 0
for row in range(len(matrix)):
for col in range(len(matrix[0])):
if matrix[row][col] == 1:
if row == 0 or col == 0:
count += 1
else:
cell = (
min(
matrix[row - 1][col],
matrix[row - 1][col - 1],
matrix[row][col - 1],
)
+ matrix[row][col]
)
count += cell
matrix[row][col] = cell
return count | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR |
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.
Example 1:
Input: matrix =
[
[0,1,1,1],
[1,1,1,1],
[0,1,1,1]
]
Output: 15
Explanation:
There are 10 squares of side 1.
There are 4 squares of side 2.
There is 1 square of side 3.
Total number of squares = 10 + 4 + 1 = 15.
Example 2:
Input: matrix =
[
[1,0,1],
[1,1,0],
[1,1,0]
]
Output: 7
Explanation:
There are 6 squares of side 1.
There is 1 square of side 2.
Total number of squares = 6 + 1 = 7.
Constraints:
1 <= arr.length <= 300
1 <= arr[0].length <= 300
0 <= arr[i][j] <= 1 | class Solution:
def countSquares(self, matrix: List[List[int]]) -> int:
def helper(x, y):
temp = 0
pre = [[x, y]]
side = 1
while True:
flag = 1
nxt = []
for i in range(len(pre)):
a, b = pre[i]
if (
a + 1 >= len(matrix)
or b + 1 >= len(matrix[0])
or matrix[a + 1][b + 1] != 1
or matrix[a + 1][b] != 1
or matrix[a][b + 1] != 1
):
flag = 0
break
if i - side == -1:
nxt.append([a, b + 1])
nxt.append([a + 1, b + 1])
nxt.append([a + 1, b])
elif i - side >= 0:
nxt.append([a + 1, b])
else:
nxt.append([a, b + 1])
if flag == 0:
break
temp += 1
side += 1
pre = nxt
return temp
res = 0
for i in range(len(matrix)):
for j in range(len(matrix[0])):
if matrix[i][j] == 0:
continue
res += 1
res += helper(i, j)
return res | CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST LIST VAR VAR ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR |
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.
Example 1:
Input: matrix =
[
[0,1,1,1],
[1,1,1,1],
[0,1,1,1]
]
Output: 15
Explanation:
There are 10 squares of side 1.
There are 4 squares of side 2.
There is 1 square of side 3.
Total number of squares = 10 + 4 + 1 = 15.
Example 2:
Input: matrix =
[
[1,0,1],
[1,1,0],
[1,1,0]
]
Output: 7
Explanation:
There are 6 squares of side 1.
There is 1 square of side 2.
Total number of squares = 6 + 1 = 7.
Constraints:
1 <= arr.length <= 300
1 <= arr[0].length <= 300
0 <= arr[i][j] <= 1 | class Solution:
def countSquares(self, matrix: List[List[int]]) -> int:
ans = 0
for row in range(len(matrix)):
for col in range(len(matrix[0])):
temp = matrix[row][col]
ans += matrix[row][col]
if row > 0:
matrix[row][col] += matrix[row - 1][col]
if col > 0:
matrix[row][col] += matrix[row][col - 1]
if row > 0 and col > 0:
matrix[row][col] -= matrix[row - 1][col - 1]
if not temp:
continue
side = 1
while row - side >= 0 and col - side >= 0:
s = matrix[row][col]
if col - side > 0:
s -= matrix[row][col - side - 1]
if row - side > 0:
s -= matrix[row - side - 1][col]
if col - side > 0 and row - side > 0:
s += matrix[row - side - 1][col - side - 1]
if s == (side + 1) * (side + 1):
ans += 1
else:
break
side += 1
return ans | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR |
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.
Example 1:
Input: matrix =
[
[0,1,1,1],
[1,1,1,1],
[0,1,1,1]
]
Output: 15
Explanation:
There are 10 squares of side 1.
There are 4 squares of side 2.
There is 1 square of side 3.
Total number of squares = 10 + 4 + 1 = 15.
Example 2:
Input: matrix =
[
[1,0,1],
[1,1,0],
[1,1,0]
]
Output: 7
Explanation:
There are 6 squares of side 1.
There is 1 square of side 2.
Total number of squares = 6 + 1 = 7.
Constraints:
1 <= arr.length <= 300
1 <= arr[0].length <= 300
0 <= arr[i][j] <= 1 | class Solution:
def countSquares(self, A: List[List[int]]) -> int:
for i in range(1, len(A)):
for j in range(1, len(A[0])):
A[i][j] *= min(A[i - 1][j], A[i][j - 1], A[i - 1][j - 1]) + 1
return sum(map(sum, A)) | CLASS_DEF FUNC_DEF VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR |
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.
Example 1:
Input: matrix =
[
[0,1,1,1],
[1,1,1,1],
[0,1,1,1]
]
Output: 15
Explanation:
There are 10 squares of side 1.
There are 4 squares of side 2.
There is 1 square of side 3.
Total number of squares = 10 + 4 + 1 = 15.
Example 2:
Input: matrix =
[
[1,0,1],
[1,1,0],
[1,1,0]
]
Output: 7
Explanation:
There are 6 squares of side 1.
There is 1 square of side 2.
Total number of squares = 6 + 1 = 7.
Constraints:
1 <= arr.length <= 300
1 <= arr[0].length <= 300
0 <= arr[i][j] <= 1 | class Solution:
def countSquares(self, matrix: List[List[int]]) -> int:
N = len(matrix)
M = len(matrix[0])
result = [[(0) for j in range(M)] for i in range(N)]
ss = 0
for i in range(N):
for j in range(M):
if i == 0 or j == 0:
result[i][j] = matrix[i][j]
else:
result[i][j] = matrix[i][j] * (
1
+ min(
min(result[i - 1][j], result[i][j - 1]),
result[i - 1][j - 1],
)
)
ss += result[i][j]
return ss | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN VAR VAR |
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.
Example 1:
Input: matrix =
[
[0,1,1,1],
[1,1,1,1],
[0,1,1,1]
]
Output: 15
Explanation:
There are 10 squares of side 1.
There are 4 squares of side 2.
There is 1 square of side 3.
Total number of squares = 10 + 4 + 1 = 15.
Example 2:
Input: matrix =
[
[1,0,1],
[1,1,0],
[1,1,0]
]
Output: 7
Explanation:
There are 6 squares of side 1.
There is 1 square of side 2.
Total number of squares = 6 + 1 = 7.
Constraints:
1 <= arr.length <= 300
1 <= arr[0].length <= 300
0 <= arr[i][j] <= 1 | class Solution:
def countSquares(self, matrix: List[List[int]]) -> int:
def countHelper(r: int, c: int, size: int = 0) -> int:
if r + size >= rMax or c + size >= cMax:
return 0
rowOnes = rSums[r + size][c + size]
if c > 0:
rowOnes -= rSums[r + size][c - 1]
colOnes = cSums[c + size][r + size]
if r > 0:
colOnes -= cSums[c + size][r - 1]
return (
1 + countHelper(r, c, size + 1) if rowOnes == colOnes == size + 1 else 0
)
rMax = len(matrix)
cMax = len(matrix[0])
rSums = [([0] * cMax) for _ in range(rMax)]
cSums = [([0] * rMax) for _ in range(cMax)]
for r, row in enumerate(matrix):
for c, sq in enumerate(row):
rSums[r][c] = cSums[c][r] = matrix[r][c]
if c > 0:
rSums[r][c] += rSums[r][c - 1]
if r > 0:
cSums[c][r] += cSums[c][r - 1]
totalSquares = 0
for r, row in enumerate(matrix):
for c, sq in enumerate(row):
if sq == 1:
totalSquares += countHelper(r, c)
return totalSquares | CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR BIN_OP VAR NUMBER BIN_OP NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR |
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.
Example 1:
Input: matrix =
[
[0,1,1,1],
[1,1,1,1],
[0,1,1,1]
]
Output: 15
Explanation:
There are 10 squares of side 1.
There are 4 squares of side 2.
There is 1 square of side 3.
Total number of squares = 10 + 4 + 1 = 15.
Example 2:
Input: matrix =
[
[1,0,1],
[1,1,0],
[1,1,0]
]
Output: 7
Explanation:
There are 6 squares of side 1.
There is 1 square of side 2.
Total number of squares = 6 + 1 = 7.
Constraints:
1 <= arr.length <= 300
1 <= arr[0].length <= 300
0 <= arr[i][j] <= 1 | class Solution:
def countSquares(self, matrix: List[List[int]]) -> int:
result = 0
for row in range(len(matrix)):
for col in range(len(matrix[0])):
search_space_increase = 0
while row + search_space_increase < len(
matrix
) and col + search_space_increase < len(matrix[0]):
end_row = row + search_space_increase
end_col = col + search_space_increase
is_valid = True
for start_row in range(row, end_row + 1):
for start_col in range(col, end_col + 1):
if matrix[start_row][start_col] != 1:
is_valid = False
break
if not is_valid:
break
if not is_valid:
break
result += 1
search_space_increase += 1
return result | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR IF VAR VAR NUMBER VAR NUMBER RETURN VAR VAR |
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.
Example 1:
Input: matrix =
[
[0,1,1,1],
[1,1,1,1],
[0,1,1,1]
]
Output: 15
Explanation:
There are 10 squares of side 1.
There are 4 squares of side 2.
There is 1 square of side 3.
Total number of squares = 10 + 4 + 1 = 15.
Example 2:
Input: matrix =
[
[1,0,1],
[1,1,0],
[1,1,0]
]
Output: 7
Explanation:
There are 6 squares of side 1.
There is 1 square of side 2.
Total number of squares = 6 + 1 = 7.
Constraints:
1 <= arr.length <= 300
1 <= arr[0].length <= 300
0 <= arr[i][j] <= 1 | class Solution:
def countSquares(self, matrix: List[List[int]]) -> int:
m = len(matrix)
n = len(matrix[0])
MAXINT = 1 << 30
most_recent_zero_to_left_or_here = [([None] * n) for _ in range(m)]
most_recent_zero_above_or_here = [([None] * n) for _ in range(m)]
for i in range(m):
for j in range(n):
if matrix[i][j] == 0:
most_recent_zero_to_left_or_here[i][j] = j
elif j == 0:
most_recent_zero_to_left_or_here[i][j] = -1
else:
most_recent_zero_to_left_or_here[i][j] = (
most_recent_zero_to_left_or_here[i][j - 1]
)
for j in range(n):
for i in range(m):
if matrix[i][j] == 0:
most_recent_zero_above_or_here[i][j] = i
elif i == 0:
most_recent_zero_above_or_here[i][j] = -1
else:
most_recent_zero_above_or_here[i][j] = (
most_recent_zero_above_or_here[i - 1][j]
)
dp = [([0] * n) for _ in range(m)]
for i in range(m):
dp[i][0] = matrix[i][0]
for j in range(n):
dp[0][j] = matrix[0][j]
for i in range(1, m):
for j in range(1, n):
limit = min(
i - most_recent_zero_above_or_here[i][j],
j - most_recent_zero_to_left_or_here[i][j],
)
old = dp[i - 1][j - 1]
dp[i][j] = min(1 + old, limit)
return sum(dp[i][j] for i in range(m) for j in range(n)) | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST NONE VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NONE VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR |
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.
Example 1:
Input: matrix =
[
[0,1,1,1],
[1,1,1,1],
[0,1,1,1]
]
Output: 15
Explanation:
There are 10 squares of side 1.
There are 4 squares of side 2.
There is 1 square of side 3.
Total number of squares = 10 + 4 + 1 = 15.
Example 2:
Input: matrix =
[
[1,0,1],
[1,1,0],
[1,1,0]
]
Output: 7
Explanation:
There are 6 squares of side 1.
There is 1 square of side 2.
Total number of squares = 6 + 1 = 7.
Constraints:
1 <= arr.length <= 300
1 <= arr[0].length <= 300
0 <= arr[i][j] <= 1 | class Solution:
class Graph:
def __init__(self, g):
self.r = len(g)
self.c = len(g[0])
self.graph = g
def isSafe(self, r, c, notvisited):
return (
r >= 0 and r < self.r and c >= 0,
c < self.c and notvisited[r][c] and self.graph[r][c] == 1,
)
def countSquares(self, matrix: List[List[int]]) -> int:
r, c = len(matrix), len(matrix[0])
dp = [([0] * (c + 1)) for _ in range(r + 1)]
cnt = 0
for i in range(r):
for j in range(c):
if matrix[i][j] == 1:
dp[i + 1][j + 1] = min(dp[i + 1][j], dp[i][j], dp[i][j + 1]) + 1
cnt += dp[i + 1][j + 1]
return cnt | CLASS_DEF CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_DEF RETURN VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR NUMBER FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR VAR |
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.
Example 1:
Input: matrix =
[
[0,1,1,1],
[1,1,1,1],
[0,1,1,1]
]
Output: 15
Explanation:
There are 10 squares of side 1.
There are 4 squares of side 2.
There is 1 square of side 3.
Total number of squares = 10 + 4 + 1 = 15.
Example 2:
Input: matrix =
[
[1,0,1],
[1,1,0],
[1,1,0]
]
Output: 7
Explanation:
There are 6 squares of side 1.
There is 1 square of side 2.
Total number of squares = 6 + 1 = 7.
Constraints:
1 <= arr.length <= 300
1 <= arr[0].length <= 300
0 <= arr[i][j] <= 1 | class Solution:
def countSquares(self, matrix: List[List[int]]) -> int:
if not matrix or not matrix[0]:
return 0
nrows = len(matrix)
ncols = len(matrix[0])
dp1 = [0] * ncols
dp2 = [0] * ncols
ans = 0
for row in range(nrows):
dp1, dp2 = dp2, [0] * ncols
for col in range(ncols):
if matrix[row][col] == 0:
continue
if row == 0 or col == 0:
dp2[col] = 1
else:
dp2[col] = 1 + min(dp1[col], dp1[col - 1], dp2[col - 1])
ans += dp2[col]
return ans | CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR VAR |
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.
Example 1:
Input: matrix =
[
[0,1,1,1],
[1,1,1,1],
[0,1,1,1]
]
Output: 15
Explanation:
There are 10 squares of side 1.
There are 4 squares of side 2.
There is 1 square of side 3.
Total number of squares = 10 + 4 + 1 = 15.
Example 2:
Input: matrix =
[
[1,0,1],
[1,1,0],
[1,1,0]
]
Output: 7
Explanation:
There are 6 squares of side 1.
There is 1 square of side 2.
Total number of squares = 6 + 1 = 7.
Constraints:
1 <= arr.length <= 300
1 <= arr[0].length <= 300
0 <= arr[i][j] <= 1 | class Solution:
def countSquares(self, matrix: List[List[int]]) -> int:
R, C = len(matrix), len(matrix[0])
dp = [[(0) for i in range(C)] for j in range(R)]
ans = 0
for i in range(R):
dp[i][0] = matrix[i][0]
ans += dp[i][0]
for j in range(C):
dp[0][j] = matrix[0][j]
ans += dp[0][j]
for i in range(1, R):
for j in range(1, C):
if matrix[i][j] == 0:
dp[i][j] = 0
else:
dp[i][j] = 1 + min(dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1])
ans += dp[i][j]
print(i, j, ans)
return ans if dp[0][0] == 0 else ans - 1 | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR NUMBER NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR |
Given a m * n matrix of ones and zeros, return how many square submatrices have all ones.
Example 1:
Input: matrix =
[
[0,1,1,1],
[1,1,1,1],
[0,1,1,1]
]
Output: 15
Explanation:
There are 10 squares of side 1.
There are 4 squares of side 2.
There is 1 square of side 3.
Total number of squares = 10 + 4 + 1 = 15.
Example 2:
Input: matrix =
[
[1,0,1],
[1,1,0],
[1,1,0]
]
Output: 7
Explanation:
There are 6 squares of side 1.
There is 1 square of side 2.
Total number of squares = 6 + 1 = 7.
Constraints:
1 <= arr.length <= 300
1 <= arr[0].length <= 300
0 <= arr[i][j] <= 1 | class Solution:
def countSquares(self, matrix: List[List[int]]) -> int:
answer = 0
for r in range(len(matrix)):
for c in range(len(matrix[0])):
if r > 0 and c > 0 and matrix[r][c] > 0:
matrix[r][c] = (
min(matrix[r][c - 1], matrix[r - 1][c], matrix[r - 1][c - 1])
+ 1
)
answer += matrix[r][c]
return answer | CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR RETURN VAR VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.