description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Our geek loves to play with strings, Currently, he is trying to reduce the size of a string by recursively removing all the consecutive duplicate pairs. In other words, He can apply the below operations any number of times. Remove all the consecutive duplicate pairs and concatenate the remaining string to replace the original string. Your task is to find the string with minimum length after applying the above operations. Note: If the string length become zero after applying operations, return "-1" as a string. Example 1: Input: aaabbaaccd Output: ad Explanation: Remove (aa)abbaaccd =>abbaaccd Remove a(bb)aaccd => aaaccd Remove (aa)accd => accd Remove a(cc)d => ad Example 2: Input: aaaa Output: Empty String Explanation: Remove (aa)aa => aa Again removing pair of duplicates then (aa) will be removed and we will get 'Empty String'. Your Task: This is a function problem. You only need to complete the function removePair() that takes a string as a parameter and returns the modified string. Return "-1" if the whole string is deleted. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 <= |str| <= 10^{4}
class Solution: def removePair(self, s): stack = [] ans = "" for i in range(len(s)): if stack and stack[-1] == s[i]: stack.pop() else: stack.append(s[i]) if len(stack) == 0: return -1 else: while len(stack) > 0: ans += stack[-1] stack.pop() return ans[::-1]
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR RETURN VAR NUMBER
Our geek loves to play with strings, Currently, he is trying to reduce the size of a string by recursively removing all the consecutive duplicate pairs. In other words, He can apply the below operations any number of times. Remove all the consecutive duplicate pairs and concatenate the remaining string to replace the original string. Your task is to find the string with minimum length after applying the above operations. Note: If the string length become zero after applying operations, return "-1" as a string. Example 1: Input: aaabbaaccd Output: ad Explanation: Remove (aa)abbaaccd =>abbaaccd Remove a(bb)aaccd => aaaccd Remove (aa)accd => accd Remove a(cc)d => ad Example 2: Input: aaaa Output: Empty String Explanation: Remove (aa)aa => aa Again removing pair of duplicates then (aa) will be removed and we will get 'Empty String'. Your Task: This is a function problem. You only need to complete the function removePair() that takes a string as a parameter and returns the modified string. Return "-1" if the whole string is deleted. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 <= |str| <= 10^{4}
class Solution: def removePair(self, s): li = [] lis = list(s) for item in lis: if li and li[-1] == item: li.pop() else: li.append(item) result = "".join(li) if result: return result return -1
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING VAR IF VAR RETURN VAR RETURN NUMBER
Our geek loves to play with strings, Currently, he is trying to reduce the size of a string by recursively removing all the consecutive duplicate pairs. In other words, He can apply the below operations any number of times. Remove all the consecutive duplicate pairs and concatenate the remaining string to replace the original string. Your task is to find the string with minimum length after applying the above operations. Note: If the string length become zero after applying operations, return "-1" as a string. Example 1: Input: aaabbaaccd Output: ad Explanation: Remove (aa)abbaaccd =>abbaaccd Remove a(bb)aaccd => aaaccd Remove (aa)accd => accd Remove a(cc)d => ad Example 2: Input: aaaa Output: Empty String Explanation: Remove (aa)aa => aa Again removing pair of duplicates then (aa) will be removed and we will get 'Empty String'. Your Task: This is a function problem. You only need to complete the function removePair() that takes a string as a parameter and returns the modified string. Return "-1" if the whole string is deleted. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 <= |str| <= 10^{4}
class Solution: def removePair(self, S): s = [] for i in S: if s and s[-1] == i: s.pop() else: s.append(i) if len(s) == 0: return -1 else: return "".join(s)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN FUNC_CALL STRING VAR
Our geek loves to play with strings, Currently, he is trying to reduce the size of a string by recursively removing all the consecutive duplicate pairs. In other words, He can apply the below operations any number of times. Remove all the consecutive duplicate pairs and concatenate the remaining string to replace the original string. Your task is to find the string with minimum length after applying the above operations. Note: If the string length become zero after applying operations, return "-1" as a string. Example 1: Input: aaabbaaccd Output: ad Explanation: Remove (aa)abbaaccd =>abbaaccd Remove a(bb)aaccd => aaaccd Remove (aa)accd => accd Remove a(cc)d => ad Example 2: Input: aaaa Output: Empty String Explanation: Remove (aa)aa => aa Again removing pair of duplicates then (aa) will be removed and we will get 'Empty String'. Your Task: This is a function problem. You only need to complete the function removePair() that takes a string as a parameter and returns the modified string. Return "-1" if the whole string is deleted. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 <= |str| <= 10^{4}
class Solution: def removePair(self, s): l1 = [] for i in range(len(s)): if l1 == []: l1.append(s[i]) elif l1[-1] == s[i]: l1.pop() else: l1.append(s[i]) s1 = "".join(l1) if s1 == "": return -1 else: return s1
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR LIST EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL STRING VAR IF VAR STRING RETURN NUMBER RETURN VAR
Our geek loves to play with strings, Currently, he is trying to reduce the size of a string by recursively removing all the consecutive duplicate pairs. In other words, He can apply the below operations any number of times. Remove all the consecutive duplicate pairs and concatenate the remaining string to replace the original string. Your task is to find the string with minimum length after applying the above operations. Note: If the string length become zero after applying operations, return "-1" as a string. Example 1: Input: aaabbaaccd Output: ad Explanation: Remove (aa)abbaaccd =>abbaaccd Remove a(bb)aaccd => aaaccd Remove (aa)accd => accd Remove a(cc)d => ad Example 2: Input: aaaa Output: Empty String Explanation: Remove (aa)aa => aa Again removing pair of duplicates then (aa) will be removed and we will get 'Empty String'. Your Task: This is a function problem. You only need to complete the function removePair() that takes a string as a parameter and returns the modified string. Return "-1" if the whole string is deleted. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 <= |str| <= 10^{4}
class Solution: def removePair(self, s): li = list(s) i = 0 while i < len(li) - 1: if li[i] == li[i + 1]: li.pop(i + 1) li.pop(i) if i != 0: i = i - 1 else: i = i + 1 if len(li) != 0: return "".join(li) else: return -1
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL STRING VAR RETURN NUMBER
Our geek loves to play with strings, Currently, he is trying to reduce the size of a string by recursively removing all the consecutive duplicate pairs. In other words, He can apply the below operations any number of times. Remove all the consecutive duplicate pairs and concatenate the remaining string to replace the original string. Your task is to find the string with minimum length after applying the above operations. Note: If the string length become zero after applying operations, return "-1" as a string. Example 1: Input: aaabbaaccd Output: ad Explanation: Remove (aa)abbaaccd =>abbaaccd Remove a(bb)aaccd => aaaccd Remove (aa)accd => accd Remove a(cc)d => ad Example 2: Input: aaaa Output: Empty String Explanation: Remove (aa)aa => aa Again removing pair of duplicates then (aa) will be removed and we will get 'Empty String'. Your Task: This is a function problem. You only need to complete the function removePair() that takes a string as a parameter and returns the modified string. Return "-1" if the whole string is deleted. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 <= |str| <= 10^{4}
class Solution: def removePair(self, s): d = [] p = "" for i in range(len(s)): if len(d) == 0: d.append(s[i]) elif d[-1] != s[i]: d.append(s[i]) elif d[-1] == s[i]: d.pop() for j in d: p += j if p: return p else: return -1
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR VAR VAR IF VAR RETURN VAR RETURN NUMBER
Our geek loves to play with strings, Currently, he is trying to reduce the size of a string by recursively removing all the consecutive duplicate pairs. In other words, He can apply the below operations any number of times. Remove all the consecutive duplicate pairs and concatenate the remaining string to replace the original string. Your task is to find the string with minimum length after applying the above operations. Note: If the string length become zero after applying operations, return "-1" as a string. Example 1: Input: aaabbaaccd Output: ad Explanation: Remove (aa)abbaaccd =>abbaaccd Remove a(bb)aaccd => aaaccd Remove (aa)accd => accd Remove a(cc)d => ad Example 2: Input: aaaa Output: Empty String Explanation: Remove (aa)aa => aa Again removing pair of duplicates then (aa) will be removed and we will get 'Empty String'. Your Task: This is a function problem. You only need to complete the function removePair() that takes a string as a parameter and returns the modified string. Return "-1" if the whole string is deleted. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 <= |str| <= 10^{4}
class Solution: def removePair(self, s): dup_index = self.checkDuplicates(s) while dup_index < len(s) - 1: s = s[0:dup_index] + s[dup_index + 2 :] dup_index = self.checkDuplicates(s) if len(s) > 0: return s else: return -1 def checkDuplicates(self, s): i = 0 for i in range(0, len(s) - 1): if s[i] == s[i + 1]: return i return i + 1
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR RETURN BIN_OP VAR NUMBER
Chef had an interesting dream last night. He dreamed of a new revolutionary chicken recipe. When he woke up today he tried very hard to reconstruct the ingredient list. But, he could only remember certain ingredients. To simplify the problem, the ingredient list can be represented by a string of lowercase characters 'a' - 'z'. Chef can recall some characters of the ingredient list, all the others, he has forgotten. However, he is quite sure that the ingredient list was a palindrome. You are given the ingredient list Chef dreamed last night. The forgotten characters are represented by a question mark ('?'). Count the number of ways Chef can replace the forgotten characters with characters 'a' - 'z' in such a way that resulting ingredient list is a palindrome. -----Input----- The first line of input contains a single integer T, the number of test cases. T lines follow, each containing a single non-empty string - the ingredient list as recalled by Chef. Whatever letters he couldn't recall are represented by a '?'. -----Output----- For each test case, output a single line containing the number of valid ways the ingredient list could be completed. Since the answers can be very large, output each answer modulo 10,000,009. -----Example----- Input: 5 ? ?? ab? a?c aba Output: 26 26 1 0 1 -----Constraints----- 1 ≤ T ≤ 20 1 ≤ sum of length of all input strings ≤ 1,000,000 Each input string contains only lowercase roman letters ('a' - 'z') or question marks.
T = int(input()) for i in range(T): s = input() x = 0 y = len(s) - 1 z = 1 while x <= y: if s[x] == "?" == s[y]: z = z * 26 % 10000009 elif s[x] != "?" and s[y] != "?" and s[x] != s[y]: z = 0 break x += 1 y -= 1 print(z)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR STRING VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING VAR VAR STRING VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Chef had an interesting dream last night. He dreamed of a new revolutionary chicken recipe. When he woke up today he tried very hard to reconstruct the ingredient list. But, he could only remember certain ingredients. To simplify the problem, the ingredient list can be represented by a string of lowercase characters 'a' - 'z'. Chef can recall some characters of the ingredient list, all the others, he has forgotten. However, he is quite sure that the ingredient list was a palindrome. You are given the ingredient list Chef dreamed last night. The forgotten characters are represented by a question mark ('?'). Count the number of ways Chef can replace the forgotten characters with characters 'a' - 'z' in such a way that resulting ingredient list is a palindrome. -----Input----- The first line of input contains a single integer T, the number of test cases. T lines follow, each containing a single non-empty string - the ingredient list as recalled by Chef. Whatever letters he couldn't recall are represented by a '?'. -----Output----- For each test case, output a single line containing the number of valid ways the ingredient list could be completed. Since the answers can be very large, output each answer modulo 10,000,009. -----Example----- Input: 5 ? ?? ab? a?c aba Output: 26 26 1 0 1 -----Constraints----- 1 ≤ T ≤ 20 1 ≤ sum of length of all input strings ≤ 1,000,000 Each input string contains only lowercase roman letters ('a' - 'z') or question marks.
MOD = 10000009 def solve(): s = input() ls = len(s) res = 1 if ls == 1: if s[0] == "?": print(26) else: print(1) return if ls & 1: for i in range(ls // 2): first_ch = s[i] last_ch = s[ls - i - 1] if first_ch == "?" and last_ch == "?": res = res * 26 % MOD elif first_ch == "?" or last_ch == "?": res = res * 1 % MOD elif first_ch != last_ch: res = res * 0 % MOD if s[ls // 2] == "?": res = res * 26 % MOD else: for i in range(ls // 2): first_ch = s[i] last_ch = s[ls - i - 1] if first_ch == "?" and last_ch == "?": res = res * 26 % MOD elif first_ch == "?" or last_ch == "?": res = res * 1 % MOD elif first_ch != last_ch: res = res * 0 % MOD print(res) for _ in range(int(input())): solve()
ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN IF BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR STRING VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR STRING VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR STRING VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR STRING VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
Chef had an interesting dream last night. He dreamed of a new revolutionary chicken recipe. When he woke up today he tried very hard to reconstruct the ingredient list. But, he could only remember certain ingredients. To simplify the problem, the ingredient list can be represented by a string of lowercase characters 'a' - 'z'. Chef can recall some characters of the ingredient list, all the others, he has forgotten. However, he is quite sure that the ingredient list was a palindrome. You are given the ingredient list Chef dreamed last night. The forgotten characters are represented by a question mark ('?'). Count the number of ways Chef can replace the forgotten characters with characters 'a' - 'z' in such a way that resulting ingredient list is a palindrome. -----Input----- The first line of input contains a single integer T, the number of test cases. T lines follow, each containing a single non-empty string - the ingredient list as recalled by Chef. Whatever letters he couldn't recall are represented by a '?'. -----Output----- For each test case, output a single line containing the number of valid ways the ingredient list could be completed. Since the answers can be very large, output each answer modulo 10,000,009. -----Example----- Input: 5 ? ?? ab? a?c aba Output: 26 26 1 0 1 -----Constraints----- 1 ≤ T ≤ 20 1 ≤ sum of length of all input strings ≤ 1,000,000 Each input string contains only lowercase roman letters ('a' - 'z') or question marks.
t = int(input()) for _ in range(t): s = input() n = len(s) if n == 1 and s[0] == "?": print(26) else: count = 1 left = 0 right = n - 1 while left <= right: if s[left] == "?" and s[right] == "?": left += 1 right -= 1 count = count * 26 % 10000009 elif s[left] == "?" or s[right] == "?" or s[left] == s[right]: left += 1 right -= 1 continue else: count = 0 break print(count)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR STRING VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING VAR VAR STRING VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
Chef had an interesting dream last night. He dreamed of a new revolutionary chicken recipe. When he woke up today he tried very hard to reconstruct the ingredient list. But, he could only remember certain ingredients. To simplify the problem, the ingredient list can be represented by a string of lowercase characters 'a' - 'z'. Chef can recall some characters of the ingredient list, all the others, he has forgotten. However, he is quite sure that the ingredient list was a palindrome. You are given the ingredient list Chef dreamed last night. The forgotten characters are represented by a question mark ('?'). Count the number of ways Chef can replace the forgotten characters with characters 'a' - 'z' in such a way that resulting ingredient list is a palindrome. -----Input----- The first line of input contains a single integer T, the number of test cases. T lines follow, each containing a single non-empty string - the ingredient list as recalled by Chef. Whatever letters he couldn't recall are represented by a '?'. -----Output----- For each test case, output a single line containing the number of valid ways the ingredient list could be completed. Since the answers can be very large, output each answer modulo 10,000,009. -----Example----- Input: 5 ? ?? ab? a?c aba Output: 26 26 1 0 1 -----Constraints----- 1 ≤ T ≤ 20 1 ≤ sum of length of all input strings ≤ 1,000,000 Each input string contains only lowercase roman letters ('a' - 'z') or question marks.
mod = 10000009 for _ in range(int(input())): st = input() c = 1 i = 0 j = len(st) - 1 while i <= j: if st[i] == "?" and st[j] == "?": c = c * 26 % mod elif st[i] == "?" or st[j] == "?": pass elif st[i] != st[j]: c = 0 break i = i + 1 j = j - 1 print(c % mod)
ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF VAR VAR STRING VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR STRING VAR VAR STRING IF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
Chef had an interesting dream last night. He dreamed of a new revolutionary chicken recipe. When he woke up today he tried very hard to reconstruct the ingredient list. But, he could only remember certain ingredients. To simplify the problem, the ingredient list can be represented by a string of lowercase characters 'a' - 'z'. Chef can recall some characters of the ingredient list, all the others, he has forgotten. However, he is quite sure that the ingredient list was a palindrome. You are given the ingredient list Chef dreamed last night. The forgotten characters are represented by a question mark ('?'). Count the number of ways Chef can replace the forgotten characters with characters 'a' - 'z' in such a way that resulting ingredient list is a palindrome. -----Input----- The first line of input contains a single integer T, the number of test cases. T lines follow, each containing a single non-empty string - the ingredient list as recalled by Chef. Whatever letters he couldn't recall are represented by a '?'. -----Output----- For each test case, output a single line containing the number of valid ways the ingredient list could be completed. Since the answers can be very large, output each answer modulo 10,000,009. -----Example----- Input: 5 ? ?? ab? a?c aba Output: 26 26 1 0 1 -----Constraints----- 1 ≤ T ≤ 20 1 ≤ sum of length of all input strings ≤ 1,000,000 Each input string contains only lowercase roman letters ('a' - 'z') or question marks.
t = int(input()) mod = 10000009 while t: s = input() m = len(s) ans = 1 for i in range(m // 2 + 1): if i == m // 2 and m & 1 == 0: break if (s[i] != "?" and s[-(i + 1)] != "?") and s[i] != s[-(i + 1)]: ans = 0 break if s[i] == s[-(i + 1)] == "?": ans = ans * 26 % mod print(ans % mod) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER
Chef had an interesting dream last night. He dreamed of a new revolutionary chicken recipe. When he woke up today he tried very hard to reconstruct the ingredient list. But, he could only remember certain ingredients. To simplify the problem, the ingredient list can be represented by a string of lowercase characters 'a' - 'z'. Chef can recall some characters of the ingredient list, all the others, he has forgotten. However, he is quite sure that the ingredient list was a palindrome. You are given the ingredient list Chef dreamed last night. The forgotten characters are represented by a question mark ('?'). Count the number of ways Chef can replace the forgotten characters with characters 'a' - 'z' in such a way that resulting ingredient list is a palindrome. -----Input----- The first line of input contains a single integer T, the number of test cases. T lines follow, each containing a single non-empty string - the ingredient list as recalled by Chef. Whatever letters he couldn't recall are represented by a '?'. -----Output----- For each test case, output a single line containing the number of valid ways the ingredient list could be completed. Since the answers can be very large, output each answer modulo 10,000,009. -----Example----- Input: 5 ? ?? ab? a?c aba Output: 26 26 1 0 1 -----Constraints----- 1 ≤ T ≤ 20 1 ≤ sum of length of all input strings ≤ 1,000,000 Each input string contains only lowercase roman letters ('a' - 'z') or question marks.
k = 10**7 + 9 for _ in range(int(input())): s = input() n = len(s) i, j = 0, n - 1 c, f = 1, 0 while i <= j: if s[i] == s[j]: if s[i] == "?": c = c % k * 26 % k elif s[i] != s[j]: if s[i] == "?" or s[j] == "?": pass else: c = 0 break i += 1 j -= 1 print(c)
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR IF VAR VAR VAR VAR IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR VAR VAR VAR IF VAR VAR STRING VAR VAR STRING ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Chef had an interesting dream last night. He dreamed of a new revolutionary chicken recipe. When he woke up today he tried very hard to reconstruct the ingredient list. But, he could only remember certain ingredients. To simplify the problem, the ingredient list can be represented by a string of lowercase characters 'a' - 'z'. Chef can recall some characters of the ingredient list, all the others, he has forgotten. However, he is quite sure that the ingredient list was a palindrome. You are given the ingredient list Chef dreamed last night. The forgotten characters are represented by a question mark ('?'). Count the number of ways Chef can replace the forgotten characters with characters 'a' - 'z' in such a way that resulting ingredient list is a palindrome. -----Input----- The first line of input contains a single integer T, the number of test cases. T lines follow, each containing a single non-empty string - the ingredient list as recalled by Chef. Whatever letters he couldn't recall are represented by a '?'. -----Output----- For each test case, output a single line containing the number of valid ways the ingredient list could be completed. Since the answers can be very large, output each answer modulo 10,000,009. -----Example----- Input: 5 ? ?? ab? a?c aba Output: 26 26 1 0 1 -----Constraints----- 1 ≤ T ≤ 20 1 ≤ sum of length of all input strings ≤ 1,000,000 Each input string contains only lowercase roman letters ('a' - 'z') or question marks.
for _ in range(int(input())): s1 = str(input()) n = len(s1) ans = 1 for i in range((n + 1) // 2): if s1[i] == "?" and s1[n - i - 1] == "?": ans = ans * 26 % 10000009 elif s1[i] != "?" and s1[n - i - 1] != "?" and s1[i] != s1[n - i - 1]: ans = 0 break print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING VAR BIN_OP BIN_OP VAR VAR NUMBER STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING VAR BIN_OP BIN_OP VAR VAR NUMBER STRING VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
Chef had an interesting dream last night. He dreamed of a new revolutionary chicken recipe. When he woke up today he tried very hard to reconstruct the ingredient list. But, he could only remember certain ingredients. To simplify the problem, the ingredient list can be represented by a string of lowercase characters 'a' - 'z'. Chef can recall some characters of the ingredient list, all the others, he has forgotten. However, he is quite sure that the ingredient list was a palindrome. You are given the ingredient list Chef dreamed last night. The forgotten characters are represented by a question mark ('?'). Count the number of ways Chef can replace the forgotten characters with characters 'a' - 'z' in such a way that resulting ingredient list is a palindrome. -----Input----- The first line of input contains a single integer T, the number of test cases. T lines follow, each containing a single non-empty string - the ingredient list as recalled by Chef. Whatever letters he couldn't recall are represented by a '?'. -----Output----- For each test case, output a single line containing the number of valid ways the ingredient list could be completed. Since the answers can be very large, output each answer modulo 10,000,009. -----Example----- Input: 5 ? ?? ab? a?c aba Output: 26 26 1 0 1 -----Constraints----- 1 ≤ T ≤ 20 1 ≤ sum of length of all input strings ≤ 1,000,000 Each input string contains only lowercase roman letters ('a' - 'z') or question marks.
for j in range(int(input())): s = input() l = len(s) ans = 1 mod = 10000009 for i in range((l + 1) // 2): if s[i] != s[l - 1 - i] and s[i] != "?" and s[l - 1 - i] != "?": ans = 0 break elif s[i] == "?" and s[l - 1 - i] == "?": ans = ans * 26 % mod print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR STRING VAR BIN_OP BIN_OP VAR NUMBER VAR STRING ASSIGN VAR NUMBER IF VAR VAR STRING VAR BIN_OP BIN_OP VAR NUMBER VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
Chef had an interesting dream last night. He dreamed of a new revolutionary chicken recipe. When he woke up today he tried very hard to reconstruct the ingredient list. But, he could only remember certain ingredients. To simplify the problem, the ingredient list can be represented by a string of lowercase characters 'a' - 'z'. Chef can recall some characters of the ingredient list, all the others, he has forgotten. However, he is quite sure that the ingredient list was a palindrome. You are given the ingredient list Chef dreamed last night. The forgotten characters are represented by a question mark ('?'). Count the number of ways Chef can replace the forgotten characters with characters 'a' - 'z' in such a way that resulting ingredient list is a palindrome. -----Input----- The first line of input contains a single integer T, the number of test cases. T lines follow, each containing a single non-empty string - the ingredient list as recalled by Chef. Whatever letters he couldn't recall are represented by a '?'. -----Output----- For each test case, output a single line containing the number of valid ways the ingredient list could be completed. Since the answers can be very large, output each answer modulo 10,000,009. -----Example----- Input: 5 ? ?? ab? a?c aba Output: 26 26 1 0 1 -----Constraints----- 1 ≤ T ≤ 20 1 ≤ sum of length of all input strings ≤ 1,000,000 Each input string contains only lowercase roman letters ('a' - 'z') or question marks.
for u in range(int(input())): s = input() i = 0 j = len(s) - 1 c = 1 m = 10**7 + 9 while i <= j: if s[i] == s[j] == "?": c = 26 % m * (c % m) % m i += 1 j -= 1 elif s[i] == "?" or s[j] == "?" or s[i] == s[j]: i += 1 j -= 1 else: c = 0 break print(c % m)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER WHILE VAR VAR IF VAR VAR VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
Chef had an interesting dream last night. He dreamed of a new revolutionary chicken recipe. When he woke up today he tried very hard to reconstruct the ingredient list. But, he could only remember certain ingredients. To simplify the problem, the ingredient list can be represented by a string of lowercase characters 'a' - 'z'. Chef can recall some characters of the ingredient list, all the others, he has forgotten. However, he is quite sure that the ingredient list was a palindrome. You are given the ingredient list Chef dreamed last night. The forgotten characters are represented by a question mark ('?'). Count the number of ways Chef can replace the forgotten characters with characters 'a' - 'z' in such a way that resulting ingredient list is a palindrome. -----Input----- The first line of input contains a single integer T, the number of test cases. T lines follow, each containing a single non-empty string - the ingredient list as recalled by Chef. Whatever letters he couldn't recall are represented by a '?'. -----Output----- For each test case, output a single line containing the number of valid ways the ingredient list could be completed. Since the answers can be very large, output each answer modulo 10,000,009. -----Example----- Input: 5 ? ?? ab? a?c aba Output: 26 26 1 0 1 -----Constraints----- 1 ≤ T ≤ 20 1 ≤ sum of length of all input strings ≤ 1,000,000 Each input string contains only lowercase roman letters ('a' - 'z') or question marks.
from sys import stdin, stdout mod = 10**7 + 9 for _ in range(int(stdin.readline())): s = input().strip() n = len(s) m = 1 for i in range(n // 2): if s[i] == s[n - i - 1] and s[i] == "?": m = m * 26 % mod elif s[i] != s[n - i - 1] and (s[i] == "?" or s[n - i - 1] == "?"): pass elif s[i] == s[n - i - 1]: pass else: print(0) break else: if n % 2 == 1 and s[n // 2] == "?": m = m * 26 % mod print(m)
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR STRING VAR BIN_OP BIN_OP VAR VAR NUMBER STRING IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
Chef had an interesting dream last night. He dreamed of a new revolutionary chicken recipe. When he woke up today he tried very hard to reconstruct the ingredient list. But, he could only remember certain ingredients. To simplify the problem, the ingredient list can be represented by a string of lowercase characters 'a' - 'z'. Chef can recall some characters of the ingredient list, all the others, he has forgotten. However, he is quite sure that the ingredient list was a palindrome. You are given the ingredient list Chef dreamed last night. The forgotten characters are represented by a question mark ('?'). Count the number of ways Chef can replace the forgotten characters with characters 'a' - 'z' in such a way that resulting ingredient list is a palindrome. -----Input----- The first line of input contains a single integer T, the number of test cases. T lines follow, each containing a single non-empty string - the ingredient list as recalled by Chef. Whatever letters he couldn't recall are represented by a '?'. -----Output----- For each test case, output a single line containing the number of valid ways the ingredient list could be completed. Since the answers can be very large, output each answer modulo 10,000,009. -----Example----- Input: 5 ? ?? ab? a?c aba Output: 26 26 1 0 1 -----Constraints----- 1 ≤ T ≤ 20 1 ≤ sum of length of all input strings ≤ 1,000,000 Each input string contains only lowercase roman letters ('a' - 'z') or question marks.
tests = int(input()) for i in range(tests): string = input() length = len(string) ans = 1 end = (length - 1) // 2 + 1 for j in range(end): if string[j] == "?" and string[-j - 1] == "?": ans *= 26 elif string[j] == "?" or string[-j - 1] == "?": ans *= 1 elif string[j] != string[-j - 1]: ans = 0 break ans %= 10000009 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Chef had an interesting dream last night. He dreamed of a new revolutionary chicken recipe. When he woke up today he tried very hard to reconstruct the ingredient list. But, he could only remember certain ingredients. To simplify the problem, the ingredient list can be represented by a string of lowercase characters 'a' - 'z'. Chef can recall some characters of the ingredient list, all the others, he has forgotten. However, he is quite sure that the ingredient list was a palindrome. You are given the ingredient list Chef dreamed last night. The forgotten characters are represented by a question mark ('?'). Count the number of ways Chef can replace the forgotten characters with characters 'a' - 'z' in such a way that resulting ingredient list is a palindrome. -----Input----- The first line of input contains a single integer T, the number of test cases. T lines follow, each containing a single non-empty string - the ingredient list as recalled by Chef. Whatever letters he couldn't recall are represented by a '?'. -----Output----- For each test case, output a single line containing the number of valid ways the ingredient list could be completed. Since the answers can be very large, output each answer modulo 10,000,009. -----Example----- Input: 5 ? ?? ab? a?c aba Output: 26 26 1 0 1 -----Constraints----- 1 ≤ T ≤ 20 1 ≤ sum of length of all input strings ≤ 1,000,000 Each input string contains only lowercase roman letters ('a' - 'z') or question marks.
t = int(input()) for i in range(t): s = input() c = 1 d = len(s) for i in range((d + 1) // 2): j = d - 1 - i if s[i] != "?" and s[j] != "?" and s[i] != s[j]: c = 0 break elif s[i] == "?" and s[j] == "?": c = c * 26 % 10000009 print(c)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR STRING VAR VAR STRING VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR STRING VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Chef had an interesting dream last night. He dreamed of a new revolutionary chicken recipe. When he woke up today he tried very hard to reconstruct the ingredient list. But, he could only remember certain ingredients. To simplify the problem, the ingredient list can be represented by a string of lowercase characters 'a' - 'z'. Chef can recall some characters of the ingredient list, all the others, he has forgotten. However, he is quite sure that the ingredient list was a palindrome. You are given the ingredient list Chef dreamed last night. The forgotten characters are represented by a question mark ('?'). Count the number of ways Chef can replace the forgotten characters with characters 'a' - 'z' in such a way that resulting ingredient list is a palindrome. -----Input----- The first line of input contains a single integer T, the number of test cases. T lines follow, each containing a single non-empty string - the ingredient list as recalled by Chef. Whatever letters he couldn't recall are represented by a '?'. -----Output----- For each test case, output a single line containing the number of valid ways the ingredient list could be completed. Since the answers can be very large, output each answer modulo 10,000,009. -----Example----- Input: 5 ? ?? ab? a?c aba Output: 26 26 1 0 1 -----Constraints----- 1 ≤ T ≤ 20 1 ≤ sum of length of all input strings ≤ 1,000,000 Each input string contains only lowercase roman letters ('a' - 'z') or question marks.
MOD = 10000009 def count_palindromes(s): n = len(s) count = 1 for i in range(n // 2): j = n - i - 1 if s[i] == "?" and s[j] == "?": count = count * 26 % MOD elif s[i] == s[j] or s[i] == "?" or s[j] == "?": continue else: return 0 if n % 2 == 1 and s[n // 2] == "?": count = count * 26 % MOD return count t = int(input()) for _ in range(t): s = input().strip() print(count_palindromes(s))
ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR STRING VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR STRING VAR VAR STRING RETURN NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Chef had an interesting dream last night. He dreamed of a new revolutionary chicken recipe. When he woke up today he tried very hard to reconstruct the ingredient list. But, he could only remember certain ingredients. To simplify the problem, the ingredient list can be represented by a string of lowercase characters 'a' - 'z'. Chef can recall some characters of the ingredient list, all the others, he has forgotten. However, he is quite sure that the ingredient list was a palindrome. You are given the ingredient list Chef dreamed last night. The forgotten characters are represented by a question mark ('?'). Count the number of ways Chef can replace the forgotten characters with characters 'a' - 'z' in such a way that resulting ingredient list is a palindrome. -----Input----- The first line of input contains a single integer T, the number of test cases. T lines follow, each containing a single non-empty string - the ingredient list as recalled by Chef. Whatever letters he couldn't recall are represented by a '?'. -----Output----- For each test case, output a single line containing the number of valid ways the ingredient list could be completed. Since the answers can be very large, output each answer modulo 10,000,009. -----Example----- Input: 5 ? ?? ab? a?c aba Output: 26 26 1 0 1 -----Constraints----- 1 ≤ T ≤ 20 1 ≤ sum of length of all input strings ≤ 1,000,000 Each input string contains only lowercase roman letters ('a' - 'z') or question marks.
t = 0 try: t = int(input()) except: pass for _ in range(t): l = input() n = len(l) prod = 1 for k in range(n // 2): i = l[k] j = l[n - k - 1] if i != j and (i != "?" and j != "?"): prod = 0 break elif i == j and i == "?": prod *= 26 prod = prod % 10000009 if n % 2 != 0: if l[n // 2] == "?": prod *= 26 print(prod)
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR STRING VAR STRING ASSIGN VAR NUMBER IF VAR VAR VAR STRING VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR NUMBER EXPR FUNC_CALL VAR VAR
Chef had an interesting dream last night. He dreamed of a new revolutionary chicken recipe. When he woke up today he tried very hard to reconstruct the ingredient list. But, he could only remember certain ingredients. To simplify the problem, the ingredient list can be represented by a string of lowercase characters 'a' - 'z'. Chef can recall some characters of the ingredient list, all the others, he has forgotten. However, he is quite sure that the ingredient list was a palindrome. You are given the ingredient list Chef dreamed last night. The forgotten characters are represented by a question mark ('?'). Count the number of ways Chef can replace the forgotten characters with characters 'a' - 'z' in such a way that resulting ingredient list is a palindrome. -----Input----- The first line of input contains a single integer T, the number of test cases. T lines follow, each containing a single non-empty string - the ingredient list as recalled by Chef. Whatever letters he couldn't recall are represented by a '?'. -----Output----- For each test case, output a single line containing the number of valid ways the ingredient list could be completed. Since the answers can be very large, output each answer modulo 10,000,009. -----Example----- Input: 5 ? ?? ab? a?c aba Output: 26 26 1 0 1 -----Constraints----- 1 ≤ T ≤ 20 1 ≤ sum of length of all input strings ≤ 1,000,000 Each input string contains only lowercase roman letters ('a' - 'z') or question marks.
for t in range(int(input())): s = input() n = len(s) c, k = 1, 0 for i in range((len(s) - 1) // 2 + 1): if s[i] == "?" and s[n - i - 1] == "?": c = c * 26 % 10000009 elif s[i] == "?" or s[n - i - 1] == "?": pass elif s[i] != s[n - i - 1]: k = 1 break if k == 1: print(0) else: print(c)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR BIN_OP BIN_OP VAR VAR NUMBER STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING VAR BIN_OP BIN_OP VAR VAR NUMBER STRING IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
Chef had an interesting dream last night. He dreamed of a new revolutionary chicken recipe. When he woke up today he tried very hard to reconstruct the ingredient list. But, he could only remember certain ingredients. To simplify the problem, the ingredient list can be represented by a string of lowercase characters 'a' - 'z'. Chef can recall some characters of the ingredient list, all the others, he has forgotten. However, he is quite sure that the ingredient list was a palindrome. You are given the ingredient list Chef dreamed last night. The forgotten characters are represented by a question mark ('?'). Count the number of ways Chef can replace the forgotten characters with characters 'a' - 'z' in such a way that resulting ingredient list is a palindrome. -----Input----- The first line of input contains a single integer T, the number of test cases. T lines follow, each containing a single non-empty string - the ingredient list as recalled by Chef. Whatever letters he couldn't recall are represented by a '?'. -----Output----- For each test case, output a single line containing the number of valid ways the ingredient list could be completed. Since the answers can be very large, output each answer modulo 10,000,009. -----Example----- Input: 5 ? ?? ab? a?c aba Output: 26 26 1 0 1 -----Constraints----- 1 ≤ T ≤ 20 1 ≤ sum of length of all input strings ≤ 1,000,000 Each input string contains only lowercase roman letters ('a' - 'z') or question marks.
mod = 10000009 t = int(input()) for _ in range(t): s = input() ln = len(s) dx = 1 for i in range(ln // 2): xn = ln - i - 1 x = s[i] y = s[xn] if x == "?" and y == "?": dx = dx * 26 % mod elif x == y: continue elif x == "?" or y == "?": continue else: dx = 0 break if ln % 2 == 1 and ln > 1: if s[ln // 2] == "?": dx = dx * 26 % mod elif ln == 1: if s == "?": dx = 26 % mod print(dx)
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR STRING VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR IF VAR STRING VAR STRING ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR NUMBER IF VAR STRING ASSIGN VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR
Chef had an interesting dream last night. He dreamed of a new revolutionary chicken recipe. When he woke up today he tried very hard to reconstruct the ingredient list. But, he could only remember certain ingredients. To simplify the problem, the ingredient list can be represented by a string of lowercase characters 'a' - 'z'. Chef can recall some characters of the ingredient list, all the others, he has forgotten. However, he is quite sure that the ingredient list was a palindrome. You are given the ingredient list Chef dreamed last night. The forgotten characters are represented by a question mark ('?'). Count the number of ways Chef can replace the forgotten characters with characters 'a' - 'z' in such a way that resulting ingredient list is a palindrome. -----Input----- The first line of input contains a single integer T, the number of test cases. T lines follow, each containing a single non-empty string - the ingredient list as recalled by Chef. Whatever letters he couldn't recall are represented by a '?'. -----Output----- For each test case, output a single line containing the number of valid ways the ingredient list could be completed. Since the answers can be very large, output each answer modulo 10,000,009. -----Example----- Input: 5 ? ?? ab? a?c aba Output: 26 26 1 0 1 -----Constraints----- 1 ≤ T ≤ 20 1 ≤ sum of length of all input strings ≤ 1,000,000 Each input string contains only lowercase roman letters ('a' - 'z') or question marks.
for _ in range(int(input())): str1 = str(input()) n = len(str1) a, b, c = 0, n - 1, 1 while a <= b: if str1[a] == "?" == str1[b]: c = c * 26 % 10000009 elif str1[a] != "?" and str1[b] != "?" and str1[a] != str1[b]: c = 0 break a += 1 b -= 1 print(c)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER WHILE VAR VAR IF VAR VAR STRING VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING VAR VAR STRING VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Chef had an interesting dream last night. He dreamed of a new revolutionary chicken recipe. When he woke up today he tried very hard to reconstruct the ingredient list. But, he could only remember certain ingredients. To simplify the problem, the ingredient list can be represented by a string of lowercase characters 'a' - 'z'. Chef can recall some characters of the ingredient list, all the others, he has forgotten. However, he is quite sure that the ingredient list was a palindrome. You are given the ingredient list Chef dreamed last night. The forgotten characters are represented by a question mark ('?'). Count the number of ways Chef can replace the forgotten characters with characters 'a' - 'z' in such a way that resulting ingredient list is a palindrome. -----Input----- The first line of input contains a single integer T, the number of test cases. T lines follow, each containing a single non-empty string - the ingredient list as recalled by Chef. Whatever letters he couldn't recall are represented by a '?'. -----Output----- For each test case, output a single line containing the number of valid ways the ingredient list could be completed. Since the answers can be very large, output each answer modulo 10,000,009. -----Example----- Input: 5 ? ?? ab? a?c aba Output: 26 26 1 0 1 -----Constraints----- 1 ≤ T ≤ 20 1 ≤ sum of length of all input strings ≤ 1,000,000 Each input string contains only lowercase roman letters ('a' - 'z') or question marks.
for _ in range(int(input())): s = input() nWays = 1 n = len(s) for i in range(n // 2): if s[i] == "?" and s[-1 - i] == "?": nWays = nWays * 26 % 10000009 elif s[i] != "?" and s[-1 - i] != "?" and s[i] != s[-i - 1]: nWays = 0 break if n % 2 and s[n // 2] == "?": nWays = nWays * 26 % (10**7 + 9) print(nWays)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR BIN_OP NUMBER VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING VAR BIN_OP NUMBER VAR STRING VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Chef had an interesting dream last night. He dreamed of a new revolutionary chicken recipe. When he woke up today he tried very hard to reconstruct the ingredient list. But, he could only remember certain ingredients. To simplify the problem, the ingredient list can be represented by a string of lowercase characters 'a' - 'z'. Chef can recall some characters of the ingredient list, all the others, he has forgotten. However, he is quite sure that the ingredient list was a palindrome. You are given the ingredient list Chef dreamed last night. The forgotten characters are represented by a question mark ('?'). Count the number of ways Chef can replace the forgotten characters with characters 'a' - 'z' in such a way that resulting ingredient list is a palindrome. -----Input----- The first line of input contains a single integer T, the number of test cases. T lines follow, each containing a single non-empty string - the ingredient list as recalled by Chef. Whatever letters he couldn't recall are represented by a '?'. -----Output----- For each test case, output a single line containing the number of valid ways the ingredient list could be completed. Since the answers can be very large, output each answer modulo 10,000,009. -----Example----- Input: 5 ? ?? ab? a?c aba Output: 26 26 1 0 1 -----Constraints----- 1 ≤ T ≤ 20 1 ≤ sum of length of all input strings ≤ 1,000,000 Each input string contains only lowercase roman letters ('a' - 'z') or question marks.
for _ in range(int(input())): pal = input().strip() mul = 0 flag = True if len(pal) % 2 == 0: till = len(pal) // 2 else: till = len(pal) // 2 + 1 for i in range(till): left, right = pal[i], pal[len(pal) - i - 1] if left == "?": if right == "?": mul += 1 elif right == "?": continue elif left != right: flag = False break if flag: print(max(26**mul, 1) % 10000009) else: print("0")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR STRING IF VAR STRING VAR NUMBER IF VAR STRING IF VAR VAR ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING
Chef had an interesting dream last night. He dreamed of a new revolutionary chicken recipe. When he woke up today he tried very hard to reconstruct the ingredient list. But, he could only remember certain ingredients. To simplify the problem, the ingredient list can be represented by a string of lowercase characters 'a' - 'z'. Chef can recall some characters of the ingredient list, all the others, he has forgotten. However, he is quite sure that the ingredient list was a palindrome. You are given the ingredient list Chef dreamed last night. The forgotten characters are represented by a question mark ('?'). Count the number of ways Chef can replace the forgotten characters with characters 'a' - 'z' in such a way that resulting ingredient list is a palindrome. -----Input----- The first line of input contains a single integer T, the number of test cases. T lines follow, each containing a single non-empty string - the ingredient list as recalled by Chef. Whatever letters he couldn't recall are represented by a '?'. -----Output----- For each test case, output a single line containing the number of valid ways the ingredient list could be completed. Since the answers can be very large, output each answer modulo 10,000,009. -----Example----- Input: 5 ? ?? ab? a?c aba Output: 26 26 1 0 1 -----Constraints----- 1 ≤ T ≤ 20 1 ≤ sum of length of all input strings ≤ 1,000,000 Each input string contains only lowercase roman letters ('a' - 'z') or question marks.
a = int(input()) for _ in range(a): b = list(input()) s = 1 for i in range(len(b) // 2): if b[i] == "?" and b[len(b) - 1 - i] == "?": s = s * 26 % 10000009 elif b[i] == "?" or b[len(b) - 1 - i] == "?": s *= 1 elif b[i] != b[len(b) - 1 - i]: s = 0 c = True break if len(b) % 2 == 1: if b[len(b) // 2] == "?": s = s * 26 % 10000009 print(s)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR STRING VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR STRING VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Chef had an interesting dream last night. He dreamed of a new revolutionary chicken recipe. When he woke up today he tried very hard to reconstruct the ingredient list. But, he could only remember certain ingredients. To simplify the problem, the ingredient list can be represented by a string of lowercase characters 'a' - 'z'. Chef can recall some characters of the ingredient list, all the others, he has forgotten. However, he is quite sure that the ingredient list was a palindrome. You are given the ingredient list Chef dreamed last night. The forgotten characters are represented by a question mark ('?'). Count the number of ways Chef can replace the forgotten characters with characters 'a' - 'z' in such a way that resulting ingredient list is a palindrome. -----Input----- The first line of input contains a single integer T, the number of test cases. T lines follow, each containing a single non-empty string - the ingredient list as recalled by Chef. Whatever letters he couldn't recall are represented by a '?'. -----Output----- For each test case, output a single line containing the number of valid ways the ingredient list could be completed. Since the answers can be very large, output each answer modulo 10,000,009. -----Example----- Input: 5 ? ?? ab? a?c aba Output: 26 26 1 0 1 -----Constraints----- 1 ≤ T ≤ 20 1 ≤ sum of length of all input strings ≤ 1,000,000 Each input string contains only lowercase roman letters ('a' - 'z') or question marks.
mod = 10000009 def solve(): s = input() n = len(s) c, k = 1, 0 for i in range((len(s) - 1) // 2 + 1): if s[i] == "?" and s[n - i - 1] == "?": c = c * 26 % 10000009 elif s[i] == "?" or s[n - i - 1] == "?": pass elif s[i] != s[n - i - 1]: k = 1 break if k == 1: print(0) else: print(c) def __starting_point(): t = int(input()) while t != 0: solve() t -= 1 __starting_point()
ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR BIN_OP BIN_OP VAR VAR NUMBER STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING VAR BIN_OP BIN_OP VAR VAR NUMBER STRING IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR
Chef had an interesting dream last night. He dreamed of a new revolutionary chicken recipe. When he woke up today he tried very hard to reconstruct the ingredient list. But, he could only remember certain ingredients. To simplify the problem, the ingredient list can be represented by a string of lowercase characters 'a' - 'z'. Chef can recall some characters of the ingredient list, all the others, he has forgotten. However, he is quite sure that the ingredient list was a palindrome. You are given the ingredient list Chef dreamed last night. The forgotten characters are represented by a question mark ('?'). Count the number of ways Chef can replace the forgotten characters with characters 'a' - 'z' in such a way that resulting ingredient list is a palindrome. -----Input----- The first line of input contains a single integer T, the number of test cases. T lines follow, each containing a single non-empty string - the ingredient list as recalled by Chef. Whatever letters he couldn't recall are represented by a '?'. -----Output----- For each test case, output a single line containing the number of valid ways the ingredient list could be completed. Since the answers can be very large, output each answer modulo 10,000,009. -----Example----- Input: 5 ? ?? ab? a?c aba Output: 26 26 1 0 1 -----Constraints----- 1 ≤ T ≤ 20 1 ≤ sum of length of all input strings ≤ 1,000,000 Each input string contains only lowercase roman letters ('a' - 'z') or question marks.
t = int(input()) while t > 0: t -= 1 s = input() n = len(s) l = 0 r = n - 1 count = 0 imposible = False while l <= r: if s[l] == "?" and s[r] == "?": count += 1 elif s[l] != "?" and s[r] != "?" and s[l] != s[r]: imposible = True l += 1 r -= 1 if imposible == True: print("0") else: ans = 1 for i in range(count): ans = ans * 26 % 10000009 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR STRING VAR VAR STRING VAR NUMBER IF VAR VAR STRING VAR VAR STRING VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Chef had an interesting dream last night. He dreamed of a new revolutionary chicken recipe. When he woke up today he tried very hard to reconstruct the ingredient list. But, he could only remember certain ingredients. To simplify the problem, the ingredient list can be represented by a string of lowercase characters 'a' - 'z'. Chef can recall some characters of the ingredient list, all the others, he has forgotten. However, he is quite sure that the ingredient list was a palindrome. You are given the ingredient list Chef dreamed last night. The forgotten characters are represented by a question mark ('?'). Count the number of ways Chef can replace the forgotten characters with characters 'a' - 'z' in such a way that resulting ingredient list is a palindrome. -----Input----- The first line of input contains a single integer T, the number of test cases. T lines follow, each containing a single non-empty string - the ingredient list as recalled by Chef. Whatever letters he couldn't recall are represented by a '?'. -----Output----- For each test case, output a single line containing the number of valid ways the ingredient list could be completed. Since the answers can be very large, output each answer modulo 10,000,009. -----Example----- Input: 5 ? ?? ab? a?c aba Output: 26 26 1 0 1 -----Constraints----- 1 ≤ T ≤ 20 1 ≤ sum of length of all input strings ≤ 1,000,000 Each input string contains only lowercase roman letters ('a' - 'z') or question marks.
for _ in range(int(input())): s = input() n = len(s) count = 0 for i in range(n // 2): if s[i] != s[n - i - 1]: if s[i] != "?" and s[n - 1 - i] != "?": print(0) break elif s[i] == "?" and s[n - i - 1] == "?": count += 1 else: if n & 1 == 1: if s[n // 2] == "?": count += 1 print(pow(26, count, 10000009))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR STRING VAR BIN_OP BIN_OP VAR NUMBER VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR VAR STRING VAR BIN_OP BIN_OP VAR VAR NUMBER STRING VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR NUMBER
Chef had an interesting dream last night. He dreamed of a new revolutionary chicken recipe. When he woke up today he tried very hard to reconstruct the ingredient list. But, he could only remember certain ingredients. To simplify the problem, the ingredient list can be represented by a string of lowercase characters 'a' - 'z'. Chef can recall some characters of the ingredient list, all the others, he has forgotten. However, he is quite sure that the ingredient list was a palindrome. You are given the ingredient list Chef dreamed last night. The forgotten characters are represented by a question mark ('?'). Count the number of ways Chef can replace the forgotten characters with characters 'a' - 'z' in such a way that resulting ingredient list is a palindrome. -----Input----- The first line of input contains a single integer T, the number of test cases. T lines follow, each containing a single non-empty string - the ingredient list as recalled by Chef. Whatever letters he couldn't recall are represented by a '?'. -----Output----- For each test case, output a single line containing the number of valid ways the ingredient list could be completed. Since the answers can be very large, output each answer modulo 10,000,009. -----Example----- Input: 5 ? ?? ab? a?c aba Output: 26 26 1 0 1 -----Constraints----- 1 ≤ T ≤ 20 1 ≤ sum of length of all input strings ≤ 1,000,000 Each input string contains only lowercase roman letters ('a' - 'z') or question marks.
for _ in range(int(input())): s = list(input()) n = len(s) an = 1 mod = 10000009 for i in range(n): if s[i] == "?" and s[n - i - 1] == "?": an = an * 26 % mod s[i] = "." s[n - i - 1] = "." elif s[i] != "?" and s[n - i - 1] != "?": if s[i] != s[n - i - 1]: an = 0 break print(an % mod)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR BIN_OP BIN_OP VAR VAR NUMBER STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER STRING IF VAR VAR STRING VAR BIN_OP BIN_OP VAR VAR NUMBER STRING IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
Chef had an interesting dream last night. He dreamed of a new revolutionary chicken recipe. When he woke up today he tried very hard to reconstruct the ingredient list. But, he could only remember certain ingredients. To simplify the problem, the ingredient list can be represented by a string of lowercase characters 'a' - 'z'. Chef can recall some characters of the ingredient list, all the others, he has forgotten. However, he is quite sure that the ingredient list was a palindrome. You are given the ingredient list Chef dreamed last night. The forgotten characters are represented by a question mark ('?'). Count the number of ways Chef can replace the forgotten characters with characters 'a' - 'z' in such a way that resulting ingredient list is a palindrome. -----Input----- The first line of input contains a single integer T, the number of test cases. T lines follow, each containing a single non-empty string - the ingredient list as recalled by Chef. Whatever letters he couldn't recall are represented by a '?'. -----Output----- For each test case, output a single line containing the number of valid ways the ingredient list could be completed. Since the answers can be very large, output each answer modulo 10,000,009. -----Example----- Input: 5 ? ?? ab? a?c aba Output: 26 26 1 0 1 -----Constraints----- 1 ≤ T ≤ 20 1 ≤ sum of length of all input strings ≤ 1,000,000 Each input string contains only lowercase roman letters ('a' - 'z') or question marks.
for _ in range(int(input())): s = input() i = 0 j = len(s) - 1 cnt = 1 f = 1 while i <= j: if s[i] == "?" and s[j] == "?": cnt = cnt * 26 % 10000009 elif s[i] == "?" or s[j] == "?": pass elif s[i] != s[j]: f = -1 break i += 1 j -= 1 if f == -1: print(0) else: print(cnt)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR STRING VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING VAR VAR STRING IF VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
Chef had an interesting dream last night. He dreamed of a new revolutionary chicken recipe. When he woke up today he tried very hard to reconstruct the ingredient list. But, he could only remember certain ingredients. To simplify the problem, the ingredient list can be represented by a string of lowercase characters 'a' - 'z'. Chef can recall some characters of the ingredient list, all the others, he has forgotten. However, he is quite sure that the ingredient list was a palindrome. You are given the ingredient list Chef dreamed last night. The forgotten characters are represented by a question mark ('?'). Count the number of ways Chef can replace the forgotten characters with characters 'a' - 'z' in such a way that resulting ingredient list is a palindrome. -----Input----- The first line of input contains a single integer T, the number of test cases. T lines follow, each containing a single non-empty string - the ingredient list as recalled by Chef. Whatever letters he couldn't recall are represented by a '?'. -----Output----- For each test case, output a single line containing the number of valid ways the ingredient list could be completed. Since the answers can be very large, output each answer modulo 10,000,009. -----Example----- Input: 5 ? ?? ab? a?c aba Output: 26 26 1 0 1 -----Constraints----- 1 ≤ T ≤ 20 1 ≤ sum of length of all input strings ≤ 1,000,000 Each input string contains only lowercase roman letters ('a' - 'z') or question marks.
for _ in range(int(input())): s = input() l = len(s) flag = False count = 0 if l % 2 == 1: if s[l // 2] == "?": count += 1 for i in range(0, l // 2): if s[i] == "?" and s[l - i - 1] == "?": count += 1 elif s[i] != s[l - i - 1] and s[i] != "?" and s[l - i - 1] != "?": flag = True break if flag: print(0) else: ans = 1 for i in range(count): ans = ans * 26 % 10000009 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER STRING VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR STRING VAR BIN_OP BIN_OP VAR VAR NUMBER STRING VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR STRING VAR BIN_OP BIN_OP VAR VAR NUMBER STRING ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Chef had an interesting dream last night. He dreamed of a new revolutionary chicken recipe. When he woke up today he tried very hard to reconstruct the ingredient list. But, he could only remember certain ingredients. To simplify the problem, the ingredient list can be represented by a string of lowercase characters 'a' - 'z'. Chef can recall some characters of the ingredient list, all the others, he has forgotten. However, he is quite sure that the ingredient list was a palindrome. You are given the ingredient list Chef dreamed last night. The forgotten characters are represented by a question mark ('?'). Count the number of ways Chef can replace the forgotten characters with characters 'a' - 'z' in such a way that resulting ingredient list is a palindrome. -----Input----- The first line of input contains a single integer T, the number of test cases. T lines follow, each containing a single non-empty string - the ingredient list as recalled by Chef. Whatever letters he couldn't recall are represented by a '?'. -----Output----- For each test case, output a single line containing the number of valid ways the ingredient list could be completed. Since the answers can be very large, output each answer modulo 10,000,009. -----Example----- Input: 5 ? ?? ab? a?c aba Output: 26 26 1 0 1 -----Constraints----- 1 ≤ T ≤ 20 1 ≤ sum of length of all input strings ≤ 1,000,000 Each input string contains only lowercase roman letters ('a' - 'z') or question marks.
tc = int(input()) for _ in range(tc): s = input() n = len(s) ans = 1 for i in range(n // 2): x = (s[i] == "?") + (s[n - i - 1] == "?") if x == 2: ans = ans * 26 % 10000009 elif x == 0 and s[i] != s[n - i - 1]: ans = 0 break if n % 2 == 1 and s[n // 2] == "?": ans *= 26 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR STRING VAR BIN_OP BIN_OP VAR VAR NUMBER STRING IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER STRING VAR NUMBER EXPR FUNC_CALL VAR VAR
Chef had an interesting dream last night. He dreamed of a new revolutionary chicken recipe. When he woke up today he tried very hard to reconstruct the ingredient list. But, he could only remember certain ingredients. To simplify the problem, the ingredient list can be represented by a string of lowercase characters 'a' - 'z'. Chef can recall some characters of the ingredient list, all the others, he has forgotten. However, he is quite sure that the ingredient list was a palindrome. You are given the ingredient list Chef dreamed last night. The forgotten characters are represented by a question mark ('?'). Count the number of ways Chef can replace the forgotten characters with characters 'a' - 'z' in such a way that resulting ingredient list is a palindrome. -----Input----- The first line of input contains a single integer T, the number of test cases. T lines follow, each containing a single non-empty string - the ingredient list as recalled by Chef. Whatever letters he couldn't recall are represented by a '?'. -----Output----- For each test case, output a single line containing the number of valid ways the ingredient list could be completed. Since the answers can be very large, output each answer modulo 10,000,009. -----Example----- Input: 5 ? ?? ab? a?c aba Output: 26 26 1 0 1 -----Constraints----- 1 ≤ T ≤ 20 1 ≤ sum of length of all input strings ≤ 1,000,000 Each input string contains only lowercase roman letters ('a' - 'z') or question marks.
t = int(input()) for _ in range(t): s = input() s = [i for i in s] n = len(s) c = 1 mc = 1 for i in range(len(s)): if s[i] == "?" and s[abs(n - 1 - i)] == "?": s[abs(n - 1 - i)] = s[i] = "a" c *= 26 elif s[i] != s[abs(n - 1 - i)] and s[i] != "?" and s[abs(n - 1 - i)] != "?": c = 0 mc = 0 break c = c % 10000009 print(c)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR STRING VAR NUMBER IF VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR STRING VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Chef had an interesting dream last night. He dreamed of a new revolutionary chicken recipe. When he woke up today he tried very hard to reconstruct the ingredient list. But, he could only remember certain ingredients. To simplify the problem, the ingredient list can be represented by a string of lowercase characters 'a' - 'z'. Chef can recall some characters of the ingredient list, all the others, he has forgotten. However, he is quite sure that the ingredient list was a palindrome. You are given the ingredient list Chef dreamed last night. The forgotten characters are represented by a question mark ('?'). Count the number of ways Chef can replace the forgotten characters with characters 'a' - 'z' in such a way that resulting ingredient list is a palindrome. -----Input----- The first line of input contains a single integer T, the number of test cases. T lines follow, each containing a single non-empty string - the ingredient list as recalled by Chef. Whatever letters he couldn't recall are represented by a '?'. -----Output----- For each test case, output a single line containing the number of valid ways the ingredient list could be completed. Since the answers can be very large, output each answer modulo 10,000,009. -----Example----- Input: 5 ? ?? ab? a?c aba Output: 26 26 1 0 1 -----Constraints----- 1 ≤ T ≤ 20 1 ≤ sum of length of all input strings ≤ 1,000,000 Each input string contains only lowercase roman letters ('a' - 'z') or question marks.
def solve(): s = input() n = len(s) c = 1 for i in range((n - 1) // 2 + 1): if s[i] == "?" and s[n - i - 1] == "?": c = c * 26 % 10000009 elif s[i] == "?" or s[n - i - 1] == "?": c = c * 1 elif s[i] != s[n - i - 1]: c = 0 break print(c) t = int(input()) while t > 0: solve() t = t - 1
FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR BIN_OP BIN_OP VAR VAR NUMBER STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING VAR BIN_OP BIN_OP VAR VAR NUMBER STRING ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER
Chef had an interesting dream last night. He dreamed of a new revolutionary chicken recipe. When he woke up today he tried very hard to reconstruct the ingredient list. But, he could only remember certain ingredients. To simplify the problem, the ingredient list can be represented by a string of lowercase characters 'a' - 'z'. Chef can recall some characters of the ingredient list, all the others, he has forgotten. However, he is quite sure that the ingredient list was a palindrome. You are given the ingredient list Chef dreamed last night. The forgotten characters are represented by a question mark ('?'). Count the number of ways Chef can replace the forgotten characters with characters 'a' - 'z' in such a way that resulting ingredient list is a palindrome. -----Input----- The first line of input contains a single integer T, the number of test cases. T lines follow, each containing a single non-empty string - the ingredient list as recalled by Chef. Whatever letters he couldn't recall are represented by a '?'. -----Output----- For each test case, output a single line containing the number of valid ways the ingredient list could be completed. Since the answers can be very large, output each answer modulo 10,000,009. -----Example----- Input: 5 ? ?? ab? a?c aba Output: 26 26 1 0 1 -----Constraints----- 1 ≤ T ≤ 20 1 ≤ sum of length of all input strings ≤ 1,000,000 Each input string contains only lowercase roman letters ('a' - 'z') or question marks.
for _ in range(int(input())): s = input() nWays = 1 n = len(s) f = True for i in range(n // 2): if s[i] == s[-i - 1]: if s[i] == "?": nWays = nWays * 26 % (10**7 + 9) elif s[i] == "?" and s[-i - 1] != "?" or s[i] != "?" and s[-i - 1] == "?": continue else: nWays = 0 f = False break if f and n % 2 and s[n // 2] == "?": nWays = nWays * 26 % (10**7 + 9) print(nWays)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR VAR STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Chef had an interesting dream last night. He dreamed of a new revolutionary chicken recipe. When he woke up today he tried very hard to reconstruct the ingredient list. But, he could only remember certain ingredients. To simplify the problem, the ingredient list can be represented by a string of lowercase characters 'a' - 'z'. Chef can recall some characters of the ingredient list, all the others, he has forgotten. However, he is quite sure that the ingredient list was a palindrome. You are given the ingredient list Chef dreamed last night. The forgotten characters are represented by a question mark ('?'). Count the number of ways Chef can replace the forgotten characters with characters 'a' - 'z' in such a way that resulting ingredient list is a palindrome. -----Input----- The first line of input contains a single integer T, the number of test cases. T lines follow, each containing a single non-empty string - the ingredient list as recalled by Chef. Whatever letters he couldn't recall are represented by a '?'. -----Output----- For each test case, output a single line containing the number of valid ways the ingredient list could be completed. Since the answers can be very large, output each answer modulo 10,000,009. -----Example----- Input: 5 ? ?? ab? a?c aba Output: 26 26 1 0 1 -----Constraints----- 1 ≤ T ≤ 20 1 ≤ sum of length of all input strings ≤ 1,000,000 Each input string contains only lowercase roman letters ('a' - 'z') or question marks.
test = int(input()) for _ in range(test): strText = input() n = len(strText) ans = 1 rangeCheck = n if n % 2 == 0: rangeCheck = rangeCheck // 2 else: rangeCheck = rangeCheck // 2 + 1 if len(strText) == strText.count("?"): ans = ans * 26**rangeCheck % (10**7 + 9) else: for i in range(rangeCheck): start = strText[i] end = strText[n - 1 - i] if start == "?" and end == "?": ans = ans * 26 % (10**7 + 9) elif start != end: if start != "?" and end != "?": ans = 0 break print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR STRING VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER IF VAR VAR IF VAR STRING VAR STRING ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
Chef had an interesting dream last night. He dreamed of a new revolutionary chicken recipe. When he woke up today he tried very hard to reconstruct the ingredient list. But, he could only remember certain ingredients. To simplify the problem, the ingredient list can be represented by a string of lowercase characters 'a' - 'z'. Chef can recall some characters of the ingredient list, all the others, he has forgotten. However, he is quite sure that the ingredient list was a palindrome. You are given the ingredient list Chef dreamed last night. The forgotten characters are represented by a question mark ('?'). Count the number of ways Chef can replace the forgotten characters with characters 'a' - 'z' in such a way that resulting ingredient list is a palindrome. -----Input----- The first line of input contains a single integer T, the number of test cases. T lines follow, each containing a single non-empty string - the ingredient list as recalled by Chef. Whatever letters he couldn't recall are represented by a '?'. -----Output----- For each test case, output a single line containing the number of valid ways the ingredient list could be completed. Since the answers can be very large, output each answer modulo 10,000,009. -----Example----- Input: 5 ? ?? ab? a?c aba Output: 26 26 1 0 1 -----Constraints----- 1 ≤ T ≤ 20 1 ≤ sum of length of all input strings ≤ 1,000,000 Each input string contains only lowercase roman letters ('a' - 'z') or question marks.
for t in range(int(input())): s = input() ans = 1 b = 0 du = len(s) if du == 1: if s[0] == "?": print(26) else: print(1) else: for i in range(du // 2): if s[i] == "?" and s[-i - 1] == "?": ans = ans * 26 % 10000009 elif (s[i] == "?" or s[-i - 1] == "?") or s[i] == s[-i - 1]: ans = ans elif s[i] != s[-i - 1]: ans = 0 b = 1 print(ans % 10000009) break if du % 2 != 0: if s[du // 2] == "?": ans = ans * 26 % 10000009 if b == 0: print(ans % 10000009)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR NUMBER STRING EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies( self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int], ) -> int: total_candies = 0 open_boxes = [box for box in initialBoxes if status[box] == 1] locked_boxes = [box for box in initialBoxes if status[box] == 0] obtained_keys = set([]) while len(open_boxes): box = open_boxes.pop() total_candies += candies[box] for key in keys[box]: if key in locked_boxes: locked_boxes.remove(key) open_boxes.append(key) else: obtained_keys.add(key) for cbox in containedBoxes[box]: if status[cbox] == 1 or cbox in obtained_keys: open_boxes.append(cbox) else: locked_boxes.append(cbox) return total_candies
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST WHILE FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies( self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int], ) -> int: currkeys, ret, taken = set(), 0, [0] * len(status) q = collections.deque(initialBoxes) while q: l = len(q) opened = False for _ in range(l): curr = q.popleft() taken[curr] = 1 if curr not in currkeys and status[curr] == 0: q.append(curr) else: opened = True ret += candies[curr] if keys[curr]: currkeys |= set([key for key in keys[curr]]) for b in containedBoxes[curr]: if not taken[b]: q.append(b) if not opened: break return ret
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR RETURN VAR VAR
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies( self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int], ) -> int: open_box = [] close_box = set() key_set = set([i for i in range(len(status)) if status[i] == 1]) visited = [(False) for _ in range(len(status))] count = 0 for box in initialBoxes: if status[box] == 1: open_box.append(box) else: close_box.add(box) while open_box: box = open_box.pop() visited[box] = True count += candies[box] for key in keys[box]: key_set.add(key) for c_box in containedBoxes[box]: if visited[c_box]: continue else: close_box.add(c_box) for key in key_set: if key in close_box: open_box.append(key) close_box.remove(key) return count
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies( self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int], ) -> int: q = initialBoxes has_key = [0] * len(status) has_open = [0] * len(status) res = 0 while q: qq = [] for s in q: if status[s] or has_key[s]: res += candies[s] has_open[s] = 1 for b in containedBoxes[s]: if has_open[b]: continue qq.append(b) for k in keys[s]: has_key[k] = 1 else: qq.append(s) if tuple(qq) == tuple(q): break q, qq = qq, [] return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR LIST FOR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR LIST RETURN VAR VAR
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies( self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int], ) -> int: candy_count = 0 my_boxes = initialBoxes my_keys = [] while my_boxes: l = len(my_boxes) opened_new = False for b_i in range(l - 1, -1, -1): my_box = my_boxes[b_i] if status[my_box] or my_box in my_keys: my_boxes.pop(b_i) my_boxes.extend(containedBoxes[my_box]) candy_count += candies[my_box] my_keys.extend(keys[my_box]) opened_new = True if not opened_new: return candy_count return candy_count
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR RETURN VAR RETURN VAR VAR
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Box: def __init__(self, status, candies, keys, boxes): self.status = status self.candies = candies self.keys = keys self.containedBoxes = boxes def openboxes(unopened, keys, Q): for u in unopened: if u in keys: Q.append(u) unopened.remove(u) class Solution: def maxCandies( self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int], ) -> int: mykeys = [] unopened = [] candySum = 0 Q = [] for i in initialBoxes: mykeys.extend(keys[i]) if status[i] == 1: Q.append(i) else: unopened.append(i) openboxes(unopened, mykeys, Q) while Q != []: box = Q.pop(0) candySum += candies[box] for b in containedBoxes[box]: mykeys.extend(keys[b]) if status[b] == 1: Q.append(b) else: unopened.append(b) openboxes(unopened, mykeys, Q) return candySum
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR WHILE VAR LIST ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies( self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int], ) -> int: boxes = set(initialBoxes) queue = collections.deque() for box in boxes: if status[box] == 1: queue.append(box) ans = 0 seen = set() while queue: box = queue.popleft() if box not in seen: seen.add(box) ans += candies[box] for i in containedBoxes[box]: boxes.add(i) if status[i] == 1: queue.append(i) for j in keys[box]: if status[j] == 0 and j in boxes: queue.append(j) status[j] = 1 return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER RETURN VAR VAR
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies( self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int], ) -> int: key_set = set() box_set = set() used = set() queue = collections.deque(initialBoxes) ans = 0 while queue: box = queue.popleft() if box in used: continue if status[box] == 1 or box in key_set: used.add(box) if box in key_set: key_set.remove(box) if box in box_set: box_set.remove(box) ans += candies[box] for futureBox in containedBoxes[box]: box_set.add(futureBox) for futureKey in keys[box]: key_set.add(futureKey) for futureBox in box_set: if futureBox in key_set or status[futureBox] == 1: queue.append(futureBox) else: box_set.add(box) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR IF VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies( self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int], ) -> int: N = len(status) scores = [0] * N for i in range(N): scores[i] += status[i] level = [] for b in initialBoxes: scores[b] += N + 1 if scores[b] >= N + 2: level.append(b) scores[b] = -sys.maxsize res = 0 while level: n_level = [] for b in level: res += candies[b] for k in containedBoxes[b]: scores[k] += N + 1 if scores[k] >= N + 2: n_level.append(k) scores[k] = -sys.maxsize for k in keys[b]: scores[k] += 1 if scores[k] >= N + 2: n_level.append(k) scores[k] = -sys.maxsize level = n_level return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR LIST FOR VAR VAR VAR VAR VAR FOR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR VAR VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR RETURN VAR VAR
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies( self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int], ) -> int: boxes = set(initialBoxes) openedBox = set() bfs = [i for i in boxes if status[i]] while bfs: size = len(bfs) for _ in range(size): box = bfs.pop(0) openedBox.add(box) for eachContainedBox in containedBoxes[box]: if status[eachContainedBox]: bfs.append(eachContainedBox) boxes.add(eachContainedBox) for eachKey in keys[box]: if status[eachKey] == 0 and eachKey in boxes: bfs.append(eachKey) status[eachKey] = 1 return sum(candies[box] for box in openedBox)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies( self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int], ) -> int: reachable = [False] * len(status) visited = [False] * len(status) for box in initialBoxes: reachable[box] = True for i in range(len(containedBoxes)): for inside in containedBoxes[i]: reachable[inside] = False queue = initialBoxes target = [] ret = 0 while queue: for box in queue: if status[box] == 1 and reachable[box] and not visited[box]: ret += candies[box] visited[box] = True for key in keys[box]: if status[key] == 0: status[key] = 1 if reachable[key]: target.append(key) for inside in containedBoxes[box]: reachable[inside] = True if status[inside] == 1: target.append(inside) else: target.append(box) if target == queue: break queue = target target = [] return ret
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR FOR VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST RETURN VAR VAR
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies( self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int], ) -> int: s = [b for b in initialBoxes if status[b]] found_boxes = set([b for b in initialBoxes if not status[b]]) r = 0 while s: box = s.pop() r += candies[box] for key in keys[box]: status[key] = 1 tbr = set() for b in found_boxes: if status[b]: s.append(b) tbr.add(b) found_boxes -= tbr for child in containedBoxes[box]: if status[child]: s.append(child) else: found_boxes.add(child) return r
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies( self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int], ) -> int: res, bcache, kcache = 0, set(initialBoxes), set() for _ in range(1000): tmp = set() for b in bcache: if status[b] == 1 or b in kcache: tmp |= set(containedBoxes[b]) kcache |= set(keys[b]) res += candies[b] else: tmp.add(b) bcache = tmp return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR VAR
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies( self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int], ) -> int: OPEN, CLOSED = 1, 0 q = deque(initialBoxes) keys_obtained = set(i for i in range(len(status)) if status[i] == OPEN) count = 0 while set(q) & keys_obtained: q2 = deque() while q: box = q.popleft() if box in keys_obtained: count += candies[box] keys_obtained |= set(keys[box]) q.extend(containedBoxes[box]) else: q2.append(box) q = q2 return count
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR VAR
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies( self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int], ) -> int: reached = set() visited = set() q = initialBoxes yummy = 0 while q: new_q = [] for box in q: reached.add(box) if status[box] and box not in visited: visited.add(box) new_q.extend(containedBoxes[box]) new_q.extend( unlockedBox for unlockedBox in keys[box] if unlockedBox in reached ) yummy += candies[box] for unlockedBox in keys[box]: status[unlockedBox] = 1 q = new_q return yummy
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies( self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int], ) -> int: reachable = [(False) for _ in status] for i in initialBoxes: reachable[i] = True can_open = [(val == 1) for val in status] nodes_to_visit = [b for b in range(len(status)) if reachable[b] and can_open[b]] total_candies = 0 while len(nodes_to_visit) >= 1: curr_node = nodes_to_visit[0] nodes_to_visit = nodes_to_visit[1:] total_candies += candies[curr_node] for k in keys[curr_node]: if not can_open[k] and reachable[k]: nodes_to_visit.append(k) can_open[k] = True for b in containedBoxes[curr_node]: if not reachable[b] and can_open[b]: nodes_to_visit.append(b) reachable[b] = True return total_candies
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR VAR FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR FOR VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER RETURN VAR VAR
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies( self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int], ) -> int: ret = 0 n = len(status) available_to_open = [box for box in initialBoxes if status[box]] reachable_boxes = set([box for box in initialBoxes if not status[box]]) while len(available_to_open) > 0: box = available_to_open.pop() if status[box] == 2: continue ret += candies[box] status[box] = 2 for containedBox in containedBoxes[box]: reachable_boxes.add(containedBox) for key in keys[box]: if status[key] == 0: status[key] = 1 for containedBox in containedBoxes[box]: if status[containedBox] == 1: available_to_open.append(containedBox) for key in keys[box]: if key in reachable_boxes: available_to_open.append(key) return ret return ret
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR RETURN VAR VAR
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies( self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int], ) -> int: if not initialBoxes: return 0 visited = set() boxes = set() keys_found = set() n_candies = 0 opn = [] for i in initialBoxes: if status[i] != 1: boxes.add(i) else: opn.append(i) initialBoxes = opn while initialBoxes: i = initialBoxes.pop(0) n_candies += candies[i] visited.add(i) if keys[i] != []: for j in keys[i]: keys_found.add(j) if containedBoxes[i] != []: for c in containedBoxes[i]: if status[c] == 1: initialBoxes.append(c) else: boxes.add(c) for k in boxes: if k in visited: continue if k in keys_found: initialBoxes.append(k) visited.add(k) return n_candies
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR LIST FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR LIST FOR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies( self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int], ) -> int: n = len(status) visited = [0] * n unopened = [0] * n opened = [] ans = 0 for b in initialBoxes: if status[b] == 1: opened.append(b) visited[b] = 1 else: unopened[b] = 1 while opened: q2 = [] for b in opened: ans += candies[b] for bb in containedBoxes[b]: if visited[bb] == 0: if status[bb] == 1: q2.append(bb) else: unopened[bb] = 1 for k in keys[b]: status[k] = 1 if unopened[k] == 1 and visited[k] == 0: unopened[k] = 0 visited[k] = 1 q2.append(k) opened = q2 return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR ASSIGN VAR LIST FOR VAR VAR VAR VAR VAR FOR VAR VAR VAR IF VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR VAR
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies( self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int], ) -> int: foundBoxes = set(initialBoxes) openBoxes = [box for box in foundBoxes if status[box] == 1] for i in openBoxes: for x in containedBoxes[i]: foundBoxes.add(x) if status[x] == 1: openBoxes.append(x) for x in keys[i]: if status[x] == 0 and x in foundBoxes: openBoxes.append(x) status[x] = 1 return sum([candies[i] for i in openBoxes])
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER FOR VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies( self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int], ) -> int: N = len(status) queue = list() unopened = set() for i in initialBoxes: if status[i] == 1: queue.append(i) else: unopened.add(i) candiesCount = 0 while len(queue) > 0: top = queue.pop(0) candiesCount += candies[top] for k in keys[top]: status[k] = 1 if {k}.issubset(unopened): queue.append(k) unopened.remove(k) for c in containedBoxes[top]: if status[c] == 1: queue.append(c) else: unopened.add(c) return candiesCount
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies( self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int], ) -> int: if len(initialBoxes) == 0: return 0 else: boxdict = {} candysum = 0 for initialbox in initialBoxes: boxdict[initialbox] = [0, 0] p = len(status) ** 2 while p > 0: for i in range(len(containedBoxes)): if i in boxdict: for containedbox in containedBoxes[i]: boxdict[containedbox] = [0, 0] p -= 1 for j in range(len(keys)): for boxedkeys in keys[j]: if boxedkeys in boxdict: boxdict[boxedkeys][1] = 1 for l in range(len(status)): if status[l] == 1 and l in boxdict: boxdict[l][0] = 1 for key in boxdict: if ( boxdict[key][0] == 1 and boxdict[key][1] == 1 or (boxdict[key][0] == 1 or boxdict[key][1] == 1) ): candysum += candies[key] return candysum
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR LIST NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR LIST NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR IF VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR VAR VAR RETURN VAR VAR
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies( self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int], ) -> int: n = len(status) hold = initialBoxes res = 0 key = set() flag = True while flag: nxt = [] flag = False for i in range(len(hold)): cur = hold.pop() if status[cur] == 1 or status[cur] == 0 and cur in key: flag = True res += candies[cur] for k in keys[cur]: key.add(k) for b in containedBoxes[cur]: nxt.append(b) else: nxt.append(cur) hold = nxt return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR VAR
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies( self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int], ) -> int: locked_boxes = set() unused_keys = set() candy_count = 0 cur_box = [] for box in initialBoxes: if status[box] or box in unused_keys: candy_count += candies[box] for i in keys[box]: if not status[i]: if i not in locked_boxes: unused_keys.add(i) else: locked_boxes.remove(i) cur_box.append(i) for i in containedBoxes[box]: if not status[i] and i not in unused_keys: locked_boxes.add(i) else: cur_box.append(i) else: locked_boxes.add(box) while cur_box: box = cur_box.pop() candy_count += candies[box] for i in keys[box]: if not status[i]: if i not in locked_boxes: unused_keys.add(i) else: locked_boxes.remove(i) cur_box.append(i) for i in containedBoxes[box]: if not status[i] and i not in unused_keys: locked_boxes.add(i) else: cur_box.append(i) if i in unused_keys: unused_keys.remove(i) if box in unused_keys: unused_keys.remove(box) return candy_count
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR FOR VAR VAR VAR IF VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR VAR IF VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies( self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int], ) -> int: active_set = initialBoxes total_candies = 0 changed = True while changed: changed = False next_active_set = [] for b in active_set: if status[b]: changed = True total_candies += candies[b] for k in keys[b]: status[k] = 1 next_active_set += containedBoxes[b] else: next_active_set.append(b) active_set = next_active_set return total_candies def SLOWmaxCandies( self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int], ) -> int: open_boxes = set() closed_boxes = set() for b in initialBoxes: if status[b]: open_boxes.add(b) else: closed_boxes.add(b) total_candies = 0 while len(open_boxes) > 0: b = open_boxes.pop() total_candies += candies[b] for key in keys[b]: status[key] = 1 if key in closed_boxes: closed_boxes.remove(key) open_boxes.add(key) for bb in containedBoxes[b]: if status[bb]: open_boxes.add(bb) else: closed_boxes.add(bb) return total_candies
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR VAR FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies(self, status, candies, keys, containedBoxes, initialBoxes): n = len(status) access = [0] * n source = [] visited = set() for i in initialBoxes: access[i] = 1 visited.add(i) source.append(i) ans = 0 while source: i = source.pop() ans += candies[i] for j in containedBoxes[i]: access[j] = 1 for j in keys[i]: status[j] = 1 for j in containedBoxes[i]: if j not in visited and status[j]: visited.add(j) source.append(j) for j in keys[i]: if j not in visited and access[j]: visited.add(j) source.append(j) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies( self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int], ) -> int: q = collections.deque() keys_set = set([i for i in range(len(status)) if status[i] == 1]) boxes_set = set(initialBoxes[:]) for box in initialBoxes: if box in keys_set: q.append(box) res = 0 visited = set() while q: boxi = q.popleft() if boxi in visited: continue else: visited.add(boxi) res += candies[boxi] for nei in containedBoxes[boxi]: if nei in keys_set: q.append(nei) boxes_set.add(nei) for key in keys[boxi]: if key in boxes_set and key not in keys_set: q.append(key) keys_set.add(key) return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies( self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int], ) -> int: n = len(status) self.count = 0 queue = collections.deque([i for i in initialBoxes if status[i] == 1]) boxSet = set(initialBoxes) keySet = set(i for i in range(n) if status[i] == 1) opened = set() while queue: l = len(queue) for i in range(l): cur = queue.popleft() self.count += candies[cur] opened.add(cur) for key in keys[cur]: keySet.add(key) if key not in opened and key in boxSet and key not in queue: queue.append(key) for box in containedBoxes[cur]: boxSet.add(box) if box not in opened and box in keySet and box not in queue: queue.append(box) return self.count
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies( self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int], ) -> int: totalBox = len(status) gotBoxes = set(initialBoxes) i = 0 ans = 0 visited = set() while i < len(initialBoxes): boxNow = initialBoxes[i] if boxNow not in visited: if status[boxNow] == 1: visited.add(boxNow) ans += candies[boxNow] for newBox in containedBoxes[boxNow]: if status[newBox] == 1: initialBoxes.append(newBox) else: gotBoxes.add(newBox) for keyBox in keys[boxNow]: status[keyBox] = 1 if keyBox in gotBoxes: initialBoxes.append(keyBox) gotBoxes.remove(keyBox) else: gotBoxes.add(boxNow) i += 1 return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies( self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int], ) -> int: total = 0 boxes = initialBoxes[:] locked = [] while boxes: if not any([status[temp] for temp in boxes]): break box = boxes.pop(0) if status[box]: total += candies[box] for contained in containedBoxes[box]: boxes.append(contained) for key in keys[box]: status[key] = 1 else: boxes.append(box) return total
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST WHILE VAR IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR VAR
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies( self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int], ) -> int: q = collections.deque() keys_set = set() boxes_set = set() for box in initialBoxes: if status[box] == 1 or box in keys_set: q.append(box) for key in keys[box]: keys_set.add(key) if key in boxes_set: q.append(key) boxes_set.remove(key) else: boxes_set.add(box) res = 0 visited = set() while q: boxi = q.popleft() if boxi in visited: continue else: visited.add(boxi) res += candies[boxi] for nei in containedBoxes[boxi]: if status[nei] == 1 or nei in keys_set: q.append(nei) for key in keys[nei]: keys_set.add(key) if key in boxes_set: q.append(key) boxes_set.remove(key) else: boxes_set.add(nei) return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR VAR VAR IF VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies( self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int], ) -> int: found = [0] * len(status) hasKeys = status queue = collections.deque() for box in initialBoxes: found[box] = 1 if hasKeys[box]: queue.append(box) res = 0 while queue: box = queue.popleft() res += candies[box] for t in containedBoxes[box]: found[t] = 1 if hasKeys[t]: queue.append(t) for t in keys[box]: if not hasKeys[t] and found[t]: queue.append(t) hasKeys[t] = 1 return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER RETURN VAR VAR
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies( self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int], ) -> int: q = collections.deque() for box in initialBoxes: q.append(box) keys_found = set() ans = 0 found = True while q and found: size = len(q) found = False for _ in range(size): cur = q.popleft() if status[cur] == 0 and cur not in keys_found: q.append(cur) else: found = True ans += candies[cur] for box in containedBoxes[cur]: q.append(box) for key in keys[cur]: keys_found.add(key) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies(self, status, candies, keys, containedBoxes, initialBoxes): ret = 0 collected_keys = set() opened = set() queue = initialBoxes while queue: cont = False for i in range(len(queue)): box_index = queue.pop(0) if status[box_index] or box_index in collected_keys: if box_index not in opened: cont = True opened.add(box_index) ret += candies[box_index] queue += containedBoxes[box_index] collected_keys.update(keys[box_index]) else: queue.append(box_index) if not cont: break return ret
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR WHILE VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR RETURN VAR
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies( self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int], ) -> int: if not initialBoxes: return 0 if 1 not in status: return 0 boxes = [] result = 0 while True: flag = False for b in initialBoxes: if status[b] == 1: flag = True result += candies[b] boxes += containedBoxes[b] for j in keys[b]: status[j] = 1 initialBoxes.remove(b) initialBoxes += boxes boxes = [] if not flag: break return result
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR RETURN NUMBER IF NUMBER VAR RETURN NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR VAR VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST IF VAR RETURN VAR VAR
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies( self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int], ) -> int: if not initialBoxes: return 0 unused_keys = set() boxes = set([i for i in initialBoxes if status[i] == 0]) Q = collections.deque([i for i in initialBoxes if status[i] == 1]) res = 0 while Q: cur = Q.popleft() res += candies[cur] for k in keys[cur]: if k in boxes: Q.append(k) boxes.discard(k) else: unused_keys.add(k) for b in containedBoxes[cur]: if b in unused_keys or status[b] == 1: Q.append(b) unused_keys.discard(b) else: boxes.add(b) return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies( self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int], ) -> int: open = initialBoxes x_keys = set() locked = set() result = 0 seen = set() while len(open) != 0: i = open.pop(0) if i in seen: continue for key in keys[i]: x_keys.add(key) for box in containedBoxes[i]: if box in x_keys or status[box] == 1: open.append(box) else: locked.add(box) for box in locked: if box in x_keys: open.append(box) result += candies[i] seen.add(i) return result
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies( self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int], ) -> int: n = len(status) boxes = [] unboxes = set() for b in initialBoxes: if status[b]: boxes.append(b) else: unboxes.add(b) visited = set() got = 0 while boxes: ns = set() for box in boxes: if box not in visited: got += candies[box] visited.add(box) for key in keys[box]: status[key] |= 1 for box in boxes: for adj in containedBoxes[box]: unboxes.add(adj) newBoxes = set() for box in unboxes: if status[box] and box not in visited: ns.add(box) newBoxes.add(box) for box in newBoxes: unboxes.remove(box) boxes = list(ns) return got
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR VAR NUMBER FOR VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR VAR
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies( self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int], ) -> int: def dfs(i): if i in visited: return if status[i] == 0: missedKey.add(i) return visited.add(i) self.ans += candies[i] for k in keys[i]: status[k] = 1 if k in missedKey: missedKey.discard(k) dfs(k) for j in containedBoxes[i]: dfs(j) visited = set() missedKey = set() self.ans = 0 for i in initialBoxes: dfs(i) return self.ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR FUNC_DEF IF VAR VAR RETURN IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN EXPR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies( self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int], ) -> int: done = set() to_open = set() current_keys = set() opened = list() for box in initialBoxes: if status[box] == 1: opened.append(box) else: to_open.add(box) total = 0 while opened: box = opened.pop() if box in done: continue total += candies[box] done.add(box) for openable in keys[box]: if openable in to_open: opened.append(openable) to_open.remove(openable) else: current_keys.add(openable) for contained in containedBoxes[box]: if status[contained] == 1: opened.append(contained) elif contained in current_keys: opened.append(contained) current_keys.remove(contained) else: to_open.add(contained) return total
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies( self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int], ) -> int: maximum = 0 cur = [] closeddic = dict() size = 0 for i in initialBoxes: if status[i] == 1: cur.append(i) size += 1 else: closeddic[i] = 1 consumed = dict() keysdic = dict() while size > 0: nex = [] nsize = 0 for i in cur: maximum += candies[i] consumed[i] = True for j in containedBoxes[i]: if (status[j] == 1 or j in keysdic) and j not in consumed: nex.append(j) nsize += 1 elif status[j] == 0: closeddic[j] = 1 for j in keys[i]: keysdic[j] = 1 if j in closeddic and j not in consumed: del closeddic[j] nex.append(j) nsize += 1 size = nsize cur = nex return maximum
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR VAR
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies( self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int], ) -> int: self.keys_found = set() self.boxes_found = set() self.boxes_found.update(initialBoxes) n = len(status) visited = [0] * n for box in initialBoxes: self.dfs(box, keys, containedBoxes, visited) ans = 0 for i in range(n): if i in self.boxes_found and (i in self.keys_found or status[i] == 1): ans += candies[i] return ans def dfs(self, cur, keys, containedBoxes, visited): if visited[cur] == 1: return visited[cur] = 1 boxes = containedBoxes[cur] self.keys_found.update(keys[cur]) self.boxes_found.update(boxes) for box in boxes: self.dfs(box, keys, containedBoxes, visited)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR RETURN VAR VAR FUNC_DEF IF VAR VAR NUMBER RETURN ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies( self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int], ) -> int: q = [] res = 0 vis = set() collected = set() for i in initialBoxes: q.append(i) while q: cur = q.pop(0) vis.add(cur) if status[cur] != 1 or cur in collected: continue res += candies[cur] collected.add(cur) for i in keys[cur]: status[i] = 1 if i in vis: q.append(i) for j in containedBoxes[cur]: q.append(j) return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies( self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int], ) -> int: stack = deque(initialBoxes) seen = set() kseen = set() while stack: for _ in range(len(stack)): box = stack.popleft() seen.add(box) for key in keys[box]: status[key] = 1 for tbox in containedBoxes[box]: if tbox not in seen: stack.append(tbox) res = 0 for box in seen: if candies[box] and status[box]: res += candies[box] return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies(self, status, candies, keys, containedBoxes, initialBoxes): boxes = set(initialBoxes) bfs = [i for i in boxes if status[i]] for i in bfs: for j in containedBoxes[i]: boxes.add(j) if status[j]: bfs.append(j) for j in keys[i]: if status[j] == 0 and j in boxes: bfs.append(j) status[j] = 1 return sum(candies[i] for i in bfs)
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR FOR VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR VAR
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies( self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int], ) -> int: keyHold = set([idx for idx, b in enumerate(status) if b == 1]) boxHold = set(initialBoxes) opened = set() while True: flag = False newKey = set() newBox = set() for b in boxHold: if b in keyHold: opened.add(b) flag = True for k in keys[b]: newKey.add(k) for nb in containedBoxes[b]: if nb not in opened: newBox.add(nb) if not flag: break for k in newKey: keyHold.add(k) for b in newBox: boxHold.add(b) for b in opened: boxHold.discard(b) return sum([c for idx, c in enumerate(candies) if idx in opened])
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR WHILE NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR IF VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies( self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int], ) -> int: d = dict() arr = [] key = [] for i in range(0, len(status)): arr.append(-1) key.append(-1) d[i] = [status[i], candies[i], keys[i], containedBoxes[i]] if status[i] == 1: key[-1] = 1 c = 0 res = [] while 1 != -1: tmp = [] for x in initialBoxes: if arr[x] == -1 and key[x] == 1: arr[x] = 1 c += d[x][1] for y in d[x][2]: key[y] = 1 for y in d[x][3]: if key[y] == 1: tmp.append(y) else: res.append(y) elif key[x] == -1: res.append(x) initialBoxes = [] for y in res: if key[y] == 1 and arr[y] == -1: initialBoxes.append(y) initialBoxes += tmp if initialBoxes == []: break return c
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR LIST VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE NUMBER NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR VAR NUMBER FOR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR IF VAR LIST RETURN VAR VAR
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies( self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int], ) -> int: maxCandy = 0 keyset = set() numbox = len(status) q = list(initialBoxes) unopenedBOXES = [] while q: for _ in range(len(q)): thisbox = q.pop(0) if status[thisbox] == 1 or thisbox in keyset: maxCandy += candies[thisbox] candies[thisbox] = 0 for key in keys[thisbox]: keyset.add(key) else: unopenedBOXES.append(thisbox) for x in containedBoxes[thisbox]: q.append(x) for unopenedBOX in unopenedBOXES: if unopenedBOX in keyset: maxCandy += candies[unopenedBOX] return maxCandy
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST WHILE VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR VAR VAR VAR RETURN VAR VAR
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies( self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int], ) -> int: parent = collections.defaultdict(int) for i, row in enumerate(containedBoxes): for j in row: parent[j] = i n = len(status) bfs = collections.deque([(i, False) for i in initialBoxes]) res = 0 seen = set() while bfs: box, st = bfs.popleft() if box in seen: continue if status[box] == 0: if st: return res bfs.append((box, True)) continue seen.add(box) res += candies[box] for key in keys[box]: status[key] = 1 for cbox in containedBoxes[box]: bfs.append((cbox, False)) return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR VAR IF VAR VAR NUMBER IF VAR RETURN VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR VAR
Given n boxes, each box is given in the format [status, candies, keys, containedBoxes] where: status[i]: an integer which is 1 if box[i] is open and 0 if box[i] is closed. candies[i]: an integer representing the number of candies in box[i]. keys[i]: an array contains the indices of the boxes you can open with the key in box[i]. containedBoxes[i]: an array contains the indices of the boxes found in box[i]. You will start with some boxes given in initialBoxes array. You can take all the candies in any open box and you can use the keys in it to open new boxes and you also can use the boxes you find in it. Return the maximum number of candies you can get following the rules above.   Example 1: Input: status = [1,0,1,0], candies = [7,5,4,100], keys = [[],[],[1],[]], containedBoxes = [[1,2],[3],[],[]], initialBoxes = [0] Output: 16 Explanation: You will be initially given box 0. You will find 7 candies in it and boxes 1 and 2. Box 1 is closed and you don't have a key for it so you will open box 2. You will find 4 candies and a key to box 1 in box 2. In box 1, you will find 5 candies and box 3 but you will not find a key to box 3 so box 3 will remain closed. Total number of candies collected = 7 + 4 + 5 = 16 candy. Example 2: Input: status = [1,0,0,0,0,0], candies = [1,1,1,1,1,1], keys = [[1,2,3,4,5],[],[],[],[],[]], containedBoxes = [[1,2,3,4,5],[],[],[],[],[]], initialBoxes = [0] Output: 6 Explanation: You have initially box 0. Opening it you can find boxes 1,2,3,4 and 5 and their keys. The total number of candies will be 6. Example 3: Input: status = [1,1,1], candies = [100,1,100], keys = [[],[0,2],[]], containedBoxes = [[],[],[]], initialBoxes = [1] Output: 1 Example 4: Input: status = [1], candies = [100], keys = [[]], containedBoxes = [[]], initialBoxes = [] Output: 0 Example 5: Input: status = [1,1,1], candies = [2,3,2], keys = [[],[],[]], containedBoxes = [[],[],[]], initialBoxes = [2,1,0] Output: 7   Constraints: 1 <= status.length <= 1000 status.length == candies.length == keys.length == containedBoxes.length == n status[i] is 0 or 1. 1 <= candies[i] <= 1000 0 <= keys[i].length <= status.length 0 <= keys[i][j] < status.length All values in keys[i] are unique. 0 <= containedBoxes[i].length <= status.length 0 <= containedBoxes[i][j] < status.length All values in containedBoxes[i] are unique. Each box is contained in one box at most. 0 <= initialBoxes.length <= status.length 0 <= initialBoxes[i] < status.length
class Solution: def maxCandies( self, status: List[int], candies: List[int], keys: List[List[int]], containedBoxes: List[List[int]], initialBoxes: List[int], ) -> int: q, res = collections.deque(initialBoxes), 0 while q: l = len(q) for _ in range(l): b, changed = q.popleft(), False if status[b]: changed = True res += candies[b] q.extend(containedBoxes[b]) for i in keys[b]: status[i] = 1 else: q.append(b) if not changed: return res return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR RETURN VAR RETURN VAR VAR
Do you know that The Chef has a special interest in palindromes? Yes he does! Almost all of the dishes in his restaurant is named by a palindrome strings. The problem is that a name of a dish should not be too long, so The Chef has only limited choices when naming a new dish. For the given positive integer N, your task is to calculate the number of palindrome strings of length not exceeding N, that contain only lowercase letters of English alphabet (letters from 'a' to 'z', inclusive). Recall that a palindrome is a string that reads the same left to right as right to left (as in "radar"). For example: - For N = 1, we have 26 different palindromes of length not exceeding N: "a", "b", ..., "z". - For N = 2 we have 52 different palindromes of length not exceeding N: "a", "b", ..., "z", "aa", "bb", ..., "zz". - For N = 3 we have 728 different palindromes of length not exceeding N: "a", "b", ..., "z", "aa", "bb", ..., "zz", "aaa", "aba", ..., "aza", "bab", "bbb", ..., "bzb", ..., "zaz", "zbz", ..., "zzz". Since the answer can be quite large you should output it modulo 1000000007 (109 + 7). Yes, we know, most of you already hate this modulo, but there is nothing we can do with it :) -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a single integer N. -----Output----- For each test case, output a single line containing the answer for the corresponding test case. -----Constrains----- - 1 ≤ T ≤ 1000 - 1 ≤ N ≤ 109 -----Example----- Input: 5 1 2 3 4 100 Output: 26 52 728 1404 508533804 -----Explanation----- The first three examples are explained in the problem statement above.
def power(n): if n == 0: return 1 else: tmp = power(n // 2) % 1000000007 % 1000000007 if n % 2 == 1: return 26 * tmp * tmp % 1000000007 else: return tmp * tmp % 1000000007 def powernext(n): if n == 0: return 1 else: tmp = powernext(n // 2) % 1000000007 % 1000000007 if n % 2 == 1: return 25 * tmp * tmp % 1000000007 else: return tmp * tmp % 1000000007 a = powernext(1000000005) % 1000000007 t = int(input()) for i in range(t): n = int(input()) if n % 2 == 1: print((power(n // 2 + 1) + 52 * (power(n // 2) - 1) * a) % 1000000007) else: print(52 * (power(n // 2) - 1) * a % 1000000007)
FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER
Do you know that The Chef has a special interest in palindromes? Yes he does! Almost all of the dishes in his restaurant is named by a palindrome strings. The problem is that a name of a dish should not be too long, so The Chef has only limited choices when naming a new dish. For the given positive integer N, your task is to calculate the number of palindrome strings of length not exceeding N, that contain only lowercase letters of English alphabet (letters from 'a' to 'z', inclusive). Recall that a palindrome is a string that reads the same left to right as right to left (as in "radar"). For example: - For N = 1, we have 26 different palindromes of length not exceeding N: "a", "b", ..., "z". - For N = 2 we have 52 different palindromes of length not exceeding N: "a", "b", ..., "z", "aa", "bb", ..., "zz". - For N = 3 we have 728 different palindromes of length not exceeding N: "a", "b", ..., "z", "aa", "bb", ..., "zz", "aaa", "aba", ..., "aza", "bab", "bbb", ..., "bzb", ..., "zaz", "zbz", ..., "zzz". Since the answer can be quite large you should output it modulo 1000000007 (109 + 7). Yes, we know, most of you already hate this modulo, but there is nothing we can do with it :) -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a single integer N. -----Output----- For each test case, output a single line containing the answer for the corresponding test case. -----Constrains----- - 1 ≤ T ≤ 1000 - 1 ≤ N ≤ 109 -----Example----- Input: 5 1 2 3 4 100 Output: 26 52 728 1404 508533804 -----Explanation----- The first three examples are explained in the problem statement above.
M = 10**9 + 7 for _ in range(int(input())): n = int(input()) ans = 52 * (pow(26, n // 2, M) - 1) * pow(25, M - 2, M) if n % 2 == 1: ans += pow(26, n // 2 + 1, M) print(ans % M)
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Do you know that The Chef has a special interest in palindromes? Yes he does! Almost all of the dishes in his restaurant is named by a palindrome strings. The problem is that a name of a dish should not be too long, so The Chef has only limited choices when naming a new dish. For the given positive integer N, your task is to calculate the number of palindrome strings of length not exceeding N, that contain only lowercase letters of English alphabet (letters from 'a' to 'z', inclusive). Recall that a palindrome is a string that reads the same left to right as right to left (as in "radar"). For example: - For N = 1, we have 26 different palindromes of length not exceeding N: "a", "b", ..., "z". - For N = 2 we have 52 different palindromes of length not exceeding N: "a", "b", ..., "z", "aa", "bb", ..., "zz". - For N = 3 we have 728 different palindromes of length not exceeding N: "a", "b", ..., "z", "aa", "bb", ..., "zz", "aaa", "aba", ..., "aza", "bab", "bbb", ..., "bzb", ..., "zaz", "zbz", ..., "zzz". Since the answer can be quite large you should output it modulo 1000000007 (109 + 7). Yes, we know, most of you already hate this modulo, but there is nothing we can do with it :) -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a single integer N. -----Output----- For each test case, output a single line containing the answer for the corresponding test case. -----Constrains----- - 1 ≤ T ≤ 1000 - 1 ≤ N ≤ 109 -----Example----- Input: 5 1 2 3 4 100 Output: 26 52 728 1404 508533804 -----Explanation----- The first three examples are explained in the problem statement above.
mod = 1000000007 for _ in range(int(input())): n = int(input()) if n == 1: print(26) elif n == 2: print(52) else: k = -(-n // 2) if n % 2 == 0: ans = 26 * ( 1 + 26 * (pow(26, k - 1, mod) - 1) * pow(26 - 1, mod - 2, mod) ) + 26 * (1 + 26 * (pow(26, k - 1, mod) - 1) * pow(26 - 1, mod - 2, mod)) else: ans = 26 * ( 1 + 26 * (pow(26, k - 1, mod) - 1) * pow(26 - 1, mod - 2, mod) ) + 26 * (1 + 26 * (pow(26, k - 2, mod) - 1) * pow(26 - 1, mod - 2, mod)) print(ans % mod)
ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER BIN_OP BIN_OP NUMBER BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP NUMBER NUMBER BIN_OP VAR NUMBER VAR BIN_OP NUMBER BIN_OP NUMBER BIN_OP BIN_OP NUMBER BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP NUMBER NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER BIN_OP BIN_OP NUMBER BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP NUMBER NUMBER BIN_OP VAR NUMBER VAR BIN_OP NUMBER BIN_OP NUMBER BIN_OP BIN_OP NUMBER BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR BIN_OP NUMBER NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Do you know that The Chef has a special interest in palindromes? Yes he does! Almost all of the dishes in his restaurant is named by a palindrome strings. The problem is that a name of a dish should not be too long, so The Chef has only limited choices when naming a new dish. For the given positive integer N, your task is to calculate the number of palindrome strings of length not exceeding N, that contain only lowercase letters of English alphabet (letters from 'a' to 'z', inclusive). Recall that a palindrome is a string that reads the same left to right as right to left (as in "radar"). For example: - For N = 1, we have 26 different palindromes of length not exceeding N: "a", "b", ..., "z". - For N = 2 we have 52 different palindromes of length not exceeding N: "a", "b", ..., "z", "aa", "bb", ..., "zz". - For N = 3 we have 728 different palindromes of length not exceeding N: "a", "b", ..., "z", "aa", "bb", ..., "zz", "aaa", "aba", ..., "aza", "bab", "bbb", ..., "bzb", ..., "zaz", "zbz", ..., "zzz". Since the answer can be quite large you should output it modulo 1000000007 (109 + 7). Yes, we know, most of you already hate this modulo, but there is nothing we can do with it :) -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a single integer N. -----Output----- For each test case, output a single line containing the answer for the corresponding test case. -----Constrains----- - 1 ≤ T ≤ 1000 - 1 ≤ N ≤ 109 -----Example----- Input: 5 1 2 3 4 100 Output: 26 52 728 1404 508533804 -----Explanation----- The first three examples are explained in the problem statement above.
mdl = 1000000007 i25 = pow(25, mdl - 2, mdl) for _ in range(int(input())): n = int(input()) k = n // 2 + 1 ko = ke = pow(26, k, mdl) - 1 if 1 == n % 2: ko = ((ko + 1) * 26 - 1) % mdl tot = ((ko + ke) * i25 - 2) % mdl if tot < 0: tot += mdl print(tot)
ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR NUMBER VAR VAR NUMBER IF NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Do you know that The Chef has a special interest in palindromes? Yes he does! Almost all of the dishes in his restaurant is named by a palindrome strings. The problem is that a name of a dish should not be too long, so The Chef has only limited choices when naming a new dish. For the given positive integer N, your task is to calculate the number of palindrome strings of length not exceeding N, that contain only lowercase letters of English alphabet (letters from 'a' to 'z', inclusive). Recall that a palindrome is a string that reads the same left to right as right to left (as in "radar"). For example: - For N = 1, we have 26 different palindromes of length not exceeding N: "a", "b", ..., "z". - For N = 2 we have 52 different palindromes of length not exceeding N: "a", "b", ..., "z", "aa", "bb", ..., "zz". - For N = 3 we have 728 different palindromes of length not exceeding N: "a", "b", ..., "z", "aa", "bb", ..., "zz", "aaa", "aba", ..., "aza", "bab", "bbb", ..., "bzb", ..., "zaz", "zbz", ..., "zzz". Since the answer can be quite large you should output it modulo 1000000007 (109 + 7). Yes, we know, most of you already hate this modulo, but there is nothing we can do with it :) -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a single integer N. -----Output----- For each test case, output a single line containing the answer for the corresponding test case. -----Constrains----- - 1 ≤ T ≤ 1000 - 1 ≤ N ≤ 109 -----Example----- Input: 5 1 2 3 4 100 Output: 26 52 728 1404 508533804 -----Explanation----- The first three examples are explained in the problem statement above.
def topowermod(v, e, m): op = 1 v %= m be = bin(e) for c in be[2:]: op = op * op % m if "1" == c: op = op * v % m return op def modinv(v, m): v %= m tb = [(m, 1, 0), (v, 0, 1)] while tb[-1][0] > 0: q = tb[-2][0] // tb[-1][0] nr = list(tb[-2]) for e in range(3): nr[e] -= q * tb[-1][e] tb.append(tuple(nr)) if tb[-2][0] == 1: a = tb[-2][2] while a < 0: a += m return a % m mdl = 1000000007 i25 = modinv(25, mdl) for _ in range(int(input())): n = int(input()) k = n // 2 ke = topowermod(26, k + 1, mdl) ko = ke if 1 == n % 2: ko = 26 * ke % mdl ans = ((ko - 1 + ke - 1) * i25 + mdl - 2) % mdl print(ans)
FUNC_DEF ASSIGN VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF STRING VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR FUNC_DEF VAR VAR ASSIGN VAR LIST VAR NUMBER NUMBER VAR NUMBER NUMBER WHILE VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR NUMBER VAR VAR RETURN BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR IF NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
Do you know that The Chef has a special interest in palindromes? Yes he does! Almost all of the dishes in his restaurant is named by a palindrome strings. The problem is that a name of a dish should not be too long, so The Chef has only limited choices when naming a new dish. For the given positive integer N, your task is to calculate the number of palindrome strings of length not exceeding N, that contain only lowercase letters of English alphabet (letters from 'a' to 'z', inclusive). Recall that a palindrome is a string that reads the same left to right as right to left (as in "radar"). For example: - For N = 1, we have 26 different palindromes of length not exceeding N: "a", "b", ..., "z". - For N = 2 we have 52 different palindromes of length not exceeding N: "a", "b", ..., "z", "aa", "bb", ..., "zz". - For N = 3 we have 728 different palindromes of length not exceeding N: "a", "b", ..., "z", "aa", "bb", ..., "zz", "aaa", "aba", ..., "aza", "bab", "bbb", ..., "bzb", ..., "zaz", "zbz", ..., "zzz". Since the answer can be quite large you should output it modulo 1000000007 (109 + 7). Yes, we know, most of you already hate this modulo, but there is nothing we can do with it :) -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a single integer N. -----Output----- For each test case, output a single line containing the answer for the corresponding test case. -----Constrains----- - 1 ≤ T ≤ 1000 - 1 ≤ N ≤ 109 -----Example----- Input: 5 1 2 3 4 100 Output: 26 52 728 1404 508533804 -----Explanation----- The first three examples are explained in the problem statement above.
T = int(input()) inv = 280000002 mod = 1000000007 for i in range(T): n = int(input()) if n % 2 == 0: n = n // 2 + 1 a = 2 * ((pow(26, n, mod) - 1) * inv - 1) % mod print(a) else: n = n // 2 + 2 a = 2 * ((pow(26, n, mod) - 1) * inv - 1) % mod a = (a - pow(26, n - 1, mod)) % mod print(a)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP BIN_OP BIN_OP FUNC_CALL VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP BIN_OP BIN_OP FUNC_CALL VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Do you know that The Chef has a special interest in palindromes? Yes he does! Almost all of the dishes in his restaurant is named by a palindrome strings. The problem is that a name of a dish should not be too long, so The Chef has only limited choices when naming a new dish. For the given positive integer N, your task is to calculate the number of palindrome strings of length not exceeding N, that contain only lowercase letters of English alphabet (letters from 'a' to 'z', inclusive). Recall that a palindrome is a string that reads the same left to right as right to left (as in "radar"). For example: - For N = 1, we have 26 different palindromes of length not exceeding N: "a", "b", ..., "z". - For N = 2 we have 52 different palindromes of length not exceeding N: "a", "b", ..., "z", "aa", "bb", ..., "zz". - For N = 3 we have 728 different palindromes of length not exceeding N: "a", "b", ..., "z", "aa", "bb", ..., "zz", "aaa", "aba", ..., "aza", "bab", "bbb", ..., "bzb", ..., "zaz", "zbz", ..., "zzz". Since the answer can be quite large you should output it modulo 1000000007 (109 + 7). Yes, we know, most of you already hate this modulo, but there is nothing we can do with it :) -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a single integer N. -----Output----- For each test case, output a single line containing the answer for the corresponding test case. -----Constrains----- - 1 ≤ T ≤ 1000 - 1 ≤ N ≤ 109 -----Example----- Input: 5 1 2 3 4 100 Output: 26 52 728 1404 508533804 -----Explanation----- The first three examples are explained in the problem statement above.
def exp(a, n): if n == 1: return a elif n % 2: temp = exp(a, n // 2) return temp % 1000000007 * temp % 1000000007 * a % 1000000007 else: temp = exp(a, n // 2) return temp % 1000000007 * temp % 1000000007 % 1000000007 def ans(n): if n == 1: return 26 elif n == 2: return 52 if n % 2 == 0: temp = exp(26, n / 2) return (temp - 1) * 560000006 % 1000000007 else: temp = exp(26, (n + 1) / 2) return ((temp - 1) * 560000006 - temp) % 1000000007 t = int(input()) for index in range(0, t): n = int(input()) print(ans(n))
FUNC_DEF IF VAR NUMBER RETURN VAR IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER RETURN BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Do you know that The Chef has a special interest in palindromes? Yes he does! Almost all of the dishes in his restaurant is named by a palindrome strings. The problem is that a name of a dish should not be too long, so The Chef has only limited choices when naming a new dish. For the given positive integer N, your task is to calculate the number of palindrome strings of length not exceeding N, that contain only lowercase letters of English alphabet (letters from 'a' to 'z', inclusive). Recall that a palindrome is a string that reads the same left to right as right to left (as in "radar"). For example: - For N = 1, we have 26 different palindromes of length not exceeding N: "a", "b", ..., "z". - For N = 2 we have 52 different palindromes of length not exceeding N: "a", "b", ..., "z", "aa", "bb", ..., "zz". - For N = 3 we have 728 different palindromes of length not exceeding N: "a", "b", ..., "z", "aa", "bb", ..., "zz", "aaa", "aba", ..., "aza", "bab", "bbb", ..., "bzb", ..., "zaz", "zbz", ..., "zzz". Since the answer can be quite large you should output it modulo 1000000007 (109 + 7). Yes, we know, most of you already hate this modulo, but there is nothing we can do with it :) -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a single integer N. -----Output----- For each test case, output a single line containing the answer for the corresponding test case. -----Constrains----- - 1 ≤ T ≤ 1000 - 1 ≤ N ≤ 109 -----Example----- Input: 5 1 2 3 4 100 Output: 26 52 728 1404 508533804 -----Explanation----- The first three examples are explained in the problem statement above.
mod = 10**9 + 7 inv = pow(25, mod - 2, mod) for i in range(int(input())): N = int(input()) if N % 2 == 0: r = 2 * 26 * (pow(26, N // 2, mod) - 1) % mod * inv % mod else: r = (27 * pow(26, N // 2 + 1, mod) - 52) % mod * inv % mod print(r)
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP NUMBER NUMBER BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP NUMBER FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
Do you know that The Chef has a special interest in palindromes? Yes he does! Almost all of the dishes in his restaurant is named by a palindrome strings. The problem is that a name of a dish should not be too long, so The Chef has only limited choices when naming a new dish. For the given positive integer N, your task is to calculate the number of palindrome strings of length not exceeding N, that contain only lowercase letters of English alphabet (letters from 'a' to 'z', inclusive). Recall that a palindrome is a string that reads the same left to right as right to left (as in "radar"). For example: - For N = 1, we have 26 different palindromes of length not exceeding N: "a", "b", ..., "z". - For N = 2 we have 52 different palindromes of length not exceeding N: "a", "b", ..., "z", "aa", "bb", ..., "zz". - For N = 3 we have 728 different palindromes of length not exceeding N: "a", "b", ..., "z", "aa", "bb", ..., "zz", "aaa", "aba", ..., "aza", "bab", "bbb", ..., "bzb", ..., "zaz", "zbz", ..., "zzz". Since the answer can be quite large you should output it modulo 1000000007 (109 + 7). Yes, we know, most of you already hate this modulo, but there is nothing we can do with it :) -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a single integer N. -----Output----- For each test case, output a single line containing the answer for the corresponding test case. -----Constrains----- - 1 ≤ T ≤ 1000 - 1 ≤ N ≤ 109 -----Example----- Input: 5 1 2 3 4 100 Output: 26 52 728 1404 508533804 -----Explanation----- The first three examples are explained in the problem statement above.
def permutation(n, p): r = 26 if n == 1: return 26 elif n == 2: return 52 elif n == 3: return 728 elif n % 2 == 0: return 2 * (bin_expo(r, n // 2 + 1, p) - r) * bin_expo(25, 1000000005, p) % p else: n = n + 1 return ( 2 * ((bin_expo(r, n // 2 + 1, p) - r) * bin_expo(r - 1, 1000000005, p)) - bin_expo(26, n // 2, p) ) % p def bin_expo(x, n, p): if n == 0: return 1 elif n == 1: return x % p else: temp = bin_expo(x, n // 2, p) temp = temp * temp % p if n % 2 == 0: return temp else: return x % p * temp % p test = int(input()) for _ in range(test): n = int(input()) p = 1000000007 print(int(permutation(n, p)))
FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER NUMBER RETURN BIN_OP BIN_OP BIN_OP NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR FUNC_CALL VAR NUMBER NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP BIN_OP NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN VAR RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Do you know that The Chef has a special interest in palindromes? Yes he does! Almost all of the dishes in his restaurant is named by a palindrome strings. The problem is that a name of a dish should not be too long, so The Chef has only limited choices when naming a new dish. For the given positive integer N, your task is to calculate the number of palindrome strings of length not exceeding N, that contain only lowercase letters of English alphabet (letters from 'a' to 'z', inclusive). Recall that a palindrome is a string that reads the same left to right as right to left (as in "radar"). For example: - For N = 1, we have 26 different palindromes of length not exceeding N: "a", "b", ..., "z". - For N = 2 we have 52 different palindromes of length not exceeding N: "a", "b", ..., "z", "aa", "bb", ..., "zz". - For N = 3 we have 728 different palindromes of length not exceeding N: "a", "b", ..., "z", "aa", "bb", ..., "zz", "aaa", "aba", ..., "aza", "bab", "bbb", ..., "bzb", ..., "zaz", "zbz", ..., "zzz". Since the answer can be quite large you should output it modulo 1000000007 (109 + 7). Yes, we know, most of you already hate this modulo, but there is nothing we can do with it :) -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a single integer N. -----Output----- For each test case, output a single line containing the answer for the corresponding test case. -----Constrains----- - 1 ≤ T ≤ 1000 - 1 ≤ N ≤ 109 -----Example----- Input: 5 1 2 3 4 100 Output: 26 52 728 1404 508533804 -----Explanation----- The first three examples are explained in the problem statement above.
T = int(input()) for t in range(T): N = int(input()) if N % 2 == 0: print(int(560000006 * (pow(26, N // 2, 1000000007) - 1)) % 1000000007) else: print( ( int(560000006 * (pow(26, N // 2, 1000000007) - 1)) + pow(26, N // 2 + 1, 1000000007) ) % 1000000007 )
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP NUMBER BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER NUMBER
Do you know that The Chef has a special interest in palindromes? Yes he does! Almost all of the dishes in his restaurant is named by a palindrome strings. The problem is that a name of a dish should not be too long, so The Chef has only limited choices when naming a new dish. For the given positive integer N, your task is to calculate the number of palindrome strings of length not exceeding N, that contain only lowercase letters of English alphabet (letters from 'a' to 'z', inclusive). Recall that a palindrome is a string that reads the same left to right as right to left (as in "radar"). For example: - For N = 1, we have 26 different palindromes of length not exceeding N: "a", "b", ..., "z". - For N = 2 we have 52 different palindromes of length not exceeding N: "a", "b", ..., "z", "aa", "bb", ..., "zz". - For N = 3 we have 728 different palindromes of length not exceeding N: "a", "b", ..., "z", "aa", "bb", ..., "zz", "aaa", "aba", ..., "aza", "bab", "bbb", ..., "bzb", ..., "zaz", "zbz", ..., "zzz". Since the answer can be quite large you should output it modulo 1000000007 (109 + 7). Yes, we know, most of you already hate this modulo, but there is nothing we can do with it :) -----Input----- The first line of the input contains an integer T denoting the number of test cases. The description of T test cases follows. The only line of each test case contains a single integer N. -----Output----- For each test case, output a single line containing the answer for the corresponding test case. -----Constrains----- - 1 ≤ T ≤ 1000 - 1 ≤ N ≤ 109 -----Example----- Input: 5 1 2 3 4 100 Output: 26 52 728 1404 508533804 -----Explanation----- The first three examples are explained in the problem statement above.
mbase = 10**9 + 7 inv_25 = 280000002 t = int(input()) for it in range(t): n = int(input()) m = n // 2 pow26mp1 = pow(26, m + 1, mbase) res = 2 * (pow26mp1 - 26) * inv_25 if n % 2 == 1: res += pow26mp1 res %= mbase print(res)
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR