description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Given a list of words, each word consists of English lowercase letters. Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2.  For example, "abc" is a predecessor of "abac". A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on. Return the longest possible length of a word chain with words chosen from the given list of words.   Example 1: Input: ["a","b","ba","bca","bda","bdca"] Output: 4 Explanation: one of the longest word chain is "a","ba","bda","bdca".   Note: 1 <= words.length <= 1000 1 <= words[i].length <= 16 words[i] only consists of English lowercase letters.
class Solution: def longestStrChain(self, words: List[str]) -> int: words_len = [[] for _ in range(16)] graph = {word: [] for word in words} for word in words: words_len[len(word) - 1].append(word) def predecessor(word1, word2): for i in range(len(word2)): if word2[:i] + word2[i + 1 :] == word1: return True return False for i in range(len(words_len) - 1): words_1 = words_len[i] words_2 = words_len[i + 1] for word1 in words_1: for word2 in words_2: if predecessor(word1, word2): graph[word1].append(word2) memo = {word: (-1) for word in words} def dfs(word): t = 1 if memo[word] == -1: for w in graph[word]: t = max(t, 1 + dfs(w)) memo[word] = t return memo[word] lpl = 0 for words in words_len: for word in words: lpl = max(lpl, dfs(word)) return lpl
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR LIST VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR VAR FOR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR NUMBER IF VAR VAR NUMBER FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR
Given a list of words, each word consists of English lowercase letters. Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2.  For example, "abc" is a predecessor of "abac". A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on. Return the longest possible length of a word chain with words chosen from the given list of words.   Example 1: Input: ["a","b","ba","bca","bda","bdca"] Output: 4 Explanation: one of the longest word chain is "a","ba","bda","bdca".   Note: 1 <= words.length <= 1000 1 <= words[i].length <= 16 words[i] only consists of English lowercase letters.
class Solution: def isChild(self, parent, possible_child): for i in range(len(possible_child)): if possible_child[:i] + possible_child[i + 1 :] == parent: return True return False def longestStrChain(self, words: List[str]) -> int: graph = {} word_lens = [[] for _ in range(17)] for w in words: graph[w] = [] word_lens[len(w)].append(w) for i in range(1, 16): parents = word_lens[i] possible_children = word_lens[i + 1] for p in parents: for c in possible_children: if self.isChild(p, c): graph[p].append(c) longest_len = 0 visited = {} sorted_keys = sorted(graph.keys()) for node in sorted_keys: to_visit = [(node, 1)] while len(to_visit) != 0: cur_node, depth = to_visit.pop() visited[cur_node] = depth longest_len = max(depth, longest_len) for child in graph[cur_node]: if child not in visited: to_visit.append((child, depth + 1)) elif depth + 1 > visited[child]: to_visit.append((child, depth + 1)) return longest_len
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN NUMBER RETURN NUMBER FUNC_DEF VAR VAR ASSIGN VAR DICT ASSIGN VAR LIST VAR FUNC_CALL VAR NUMBER FOR VAR VAR ASSIGN VAR VAR LIST EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR VAR FOR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR LIST VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR
Given a list of words, each word consists of English lowercase letters. Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2.  For example, "abc" is a predecessor of "abac". A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on. Return the longest possible length of a word chain with words chosen from the given list of words.   Example 1: Input: ["a","b","ba","bca","bda","bdca"] Output: 4 Explanation: one of the longest word chain is "a","ba","bda","bdca".   Note: 1 <= words.length <= 1000 1 <= words[i].length <= 16 words[i] only consists of English lowercase letters.
class Solution: def longestStrChain(self, words: List[str]) -> int: dp = {} res = 1 for word in sorted(words, key=len): for i in range(len(word)): if word[:i] + word[i + 1 :] in words: dp[word] = dp.get(word[:i] + word[i + 1 :], 1) + 1 res = max(res, dp[word]) return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR
Given a list of words, each word consists of English lowercase letters. Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2.  For example, "abc" is a predecessor of "abac". A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on. Return the longest possible length of a word chain with words chosen from the given list of words.   Example 1: Input: ["a","b","ba","bca","bda","bdca"] Output: 4 Explanation: one of the longest word chain is "a","ba","bda","bdca".   Note: 1 <= words.length <= 1000 1 <= words[i].length <= 16 words[i] only consists of English lowercase letters.
class Solution: def longestStrChain(self, words: List[str]) -> int: def is_predessor(word1, word2): if abs(len(word1) - len(word2)) > 1: return False else: count = 0 word1_cnt = 0 word2_cnt = 0 while word1 and word2: if word1[0] == word2[0]: word1 = word1[1:] word2 = word2[1:] else: count += 1 word1 = word1[1:] if count > 1: return False return True words = sorted(words, key=lambda x: len(x)) M = [0] * len(words) M[0] = 1 overall_max = float("-inf") for i in range(1, len(M)): max_val = float("-inf") for j in range(i): if len(words[i]) != len(words[j]) and is_predessor(words[i], words[j]): max_val = max(max_val, 1 + M[j]) M[i] = max(1, max_val) overall_max = max(overall_max, M[i]) return overall_max
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR
Given a list of words, each word consists of English lowercase letters. Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2.  For example, "abc" is a predecessor of "abac". A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on. Return the longest possible length of a word chain with words chosen from the given list of words.   Example 1: Input: ["a","b","ba","bca","bda","bdca"] Output: 4 Explanation: one of the longest word chain is "a","ba","bda","bdca".   Note: 1 <= words.length <= 1000 1 <= words[i].length <= 16 words[i] only consists of English lowercase letters.
class Solution: def isPredecessor(self, prev_word, cur_word): if len(prev_word) != len(cur_word) - 1: return False skipped_char = False for i in range(len(prev_word)): if not skipped_char and prev_word[i] != cur_word[i]: skipped_char = True if skipped_char and prev_word[i] != cur_word[i + 1]: return False return True def longestStrChain(self, words: List[str]) -> int: words.sort(key=lambda word: len(word)) num_words = len(words) chain_lengths = [(1) for _ in range(num_words)] max_chain_length = 0 for i in range(num_words): for j in range(0, i): if ( len(words[j]) == len(words[i]) - 1 and chain_lengths[j] + 1 > chain_lengths[i] and self.isPredecessor(words[j], words[i]) ): chain_lengths[i] = chain_lengths[j] + 1 max_chain_length = max(max_chain_length, chain_lengths[i]) return max_chain_length
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR
Given a list of words, each word consists of English lowercase letters. Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2.  For example, "abc" is a predecessor of "abac". A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on. Return the longest possible length of a word chain with words chosen from the given list of words.   Example 1: Input: ["a","b","ba","bca","bda","bdca"] Output: 4 Explanation: one of the longest word chain is "a","ba","bda","bdca".   Note: 1 <= words.length <= 1000 1 <= words[i].length <= 16 words[i] only consists of English lowercase letters.
class Solution: def longestStrChain(self, words: List[str]) -> int: children = collections.defaultdict(set) for i in range(len(words)): for j in range(len(words)): if len(words[i]) == len(words[j]) + 1: for k in range(len(words[i])): if words[i][:k] + words[i][k + 1 :] == words[j]: children[words[j]].add(words[i]) print(children) def dfs(w, count): visited.add(w) if not children[w]: self.res = max(self.res, count) else: for v in children[w]: dfs(v, count + 1) visited = set() self.res = 0 for u in words: if u not in visited: dfs(u, 1) return self.res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR VAR
Given a list of words, each word consists of English lowercase letters. Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2.  For example, "abc" is a predecessor of "abac". A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on. Return the longest possible length of a word chain with words chosen from the given list of words.   Example 1: Input: ["a","b","ba","bca","bda","bdca"] Output: 4 Explanation: one of the longest word chain is "a","ba","bda","bdca".   Note: 1 <= words.length <= 1000 1 <= words[i].length <= 16 words[i] only consists of English lowercase letters.
class Solution: def longestStrChain(self, words: List[str]) -> int: def is_predecessor(w1, w2): skip = 0 for i in range(len(w2)): j = i - skip if j == len(w1): break if w1[j] != w2[i]: skip += 1 if skip >= 2: return False return True words.sort(key=len, reverse=True) len_words = len(words) combos = [0] * len_words for i in range(len_words): for j in range(0, i): if ( len(words[i]) + 1 == len(words[j]) and combos[i] < combos[j] + 1 and is_predecessor(words[i], words[j]) ): combos[i] = combos[j] + 1 for i in range(len_words): print(combos[i], end=", ") print(words[i]) return max(combos) + 1
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR NUMBER VAR
Given a list of words, each word consists of English lowercase letters. Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2.  For example, "abc" is a predecessor of "abac". A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on. Return the longest possible length of a word chain with words chosen from the given list of words.   Example 1: Input: ["a","b","ba","bca","bda","bdca"] Output: 4 Explanation: one of the longest word chain is "a","ba","bda","bdca".   Note: 1 <= words.length <= 1000 1 <= words[i].length <= 16 words[i] only consists of English lowercase letters.
class Solution: def longestStrChain(self, words: List[str]) -> int: words.sort(key=len) n = len(words) dp = [1] * n for i in range(n): for j in reversed(list(range(i))): if len(words[i]) == len(words[j]): continue elif len(words[i]) > len(words[j]) + 1: break if self.helper(words[j], words[i]): dp[i] = max(dp[i], dp[j] + 1) return max(dp) def helper(self, w1, w2): i = 0 while i < len(w1) and w1[i] == w2[i]: i += 1 while i < len(w1) and w1[i] == w2[i + 1]: i += 1 return i == len(w1)
CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_CALL VAR VAR
Given a list of words, each word consists of English lowercase letters. Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2.  For example, "abc" is a predecessor of "abac". A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on. Return the longest possible length of a word chain with words chosen from the given list of words.   Example 1: Input: ["a","b","ba","bca","bda","bdca"] Output: 4 Explanation: one of the longest word chain is "a","ba","bda","bdca".   Note: 1 <= words.length <= 1000 1 <= words[i].length <= 16 words[i] only consists of English lowercase letters.
class Solution: def check(self, word1, word2) -> bool: if len(word1) + 1 != len(word2): return False for i in range(len(word1) + 1): if i < len(word1) and word1[i] == word2[i]: continue else: word2 = word2[:i] + word2[i + 1 :] return word1 == word2 def longestStrChain(self, words: List[str]) -> int: words.sort(key=len) output = [(0) for i in range(len(words))] for j in range(-1, -len(words) - 1, -1): temp_len = 1 k = j + 1 while k <= -1: if temp_len < output[k] + 1: if self.check(words[j], words[k]): temp_len = output[k] + 1 k += 1 output[j] = temp_len return max(output)
CLASS_DEF FUNC_DEF IF BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR FUNC_DEF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR
Given a list of words, each word consists of English lowercase letters. Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2.  For example, "abc" is a predecessor of "abac". A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on. Return the longest possible length of a word chain with words chosen from the given list of words.   Example 1: Input: ["a","b","ba","bca","bda","bdca"] Output: 4 Explanation: one of the longest word chain is "a","ba","bda","bdca".   Note: 1 <= words.length <= 1000 1 <= words[i].length <= 16 words[i] only consists of English lowercase letters.
class Solution: def longestStrChain(self, words: List[str]) -> int: hashmap = collections.defaultdict(list) for index, word in enumerate(words): hashmap[len(word)].append(index) n = len(words) graph = [[(False) for _ in range(n)] for _ in range(n)] def predecessor(a, b): mistake = 0 for i in range(len(a)): if a[i] != b[i + mistake]: mistake += 1 if mistake > 1 or a[i] != b[i + mistake]: return False return True for index, word in enumerate(words): for candidate in hashmap[len(word) + 1]: if predecessor(word, words[candidate]): graph[index][candidate] = True dp = [(-1) for _ in range(n)] def dfs(i): if dp[i] != -1: return dp[i] answer = 0 for j in range(n): if graph[i][j]: answer = max(answer, dfs(j)) dp[i] = answer + 1 return answer + 1 answer = 0 for i in range(n): answer = max(answer, dfs(i)) return answer
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR BIN_OP VAR VAR RETURN NUMBER RETURN NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR
Given a list of words, each word consists of English lowercase letters. Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2.  For example, "abc" is a predecessor of "abac". A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on. Return the longest possible length of a word chain with words chosen from the given list of words.   Example 1: Input: ["a","b","ba","bca","bda","bdca"] Output: 4 Explanation: one of the longest word chain is "a","ba","bda","bdca".   Note: 1 <= words.length <= 1000 1 <= words[i].length <= 16 words[i] only consists of English lowercase letters.
class Solution: def longestStrChain(self, words: List[str]) -> int: def is_predecessor(str1, str2): if len(str1) + 1 != len(str2): return False else: for i in range(len(str2)): if str1 == str2[:i] + str2[i + 1 :]: return True return False l = len(words) words.sort(key=len) mat = [[(0) for j in range(l)] for i in range(l)] mat_max = 0 for i in range(l): for j in range(i, l): if is_predecessor(words[i], words[j]): mat[i][j] = mat[i][i] + 1 mat[j][j] = max(mat[j][j], mat[i][j]) mat_max = max(mat_max, mat[i][j]) return mat_max + 1
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN BIN_OP VAR NUMBER VAR
Given a list of words, each word consists of English lowercase letters. Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2.  For example, "abc" is a predecessor of "abac". A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on. Return the longest possible length of a word chain with words chosen from the given list of words.   Example 1: Input: ["a","b","ba","bca","bda","bdca"] Output: 4 Explanation: one of the longest word chain is "a","ba","bda","bdca".   Note: 1 <= words.length <= 1000 1 <= words[i].length <= 16 words[i] only consists of English lowercase letters.
class Solution: def longestStrChain(self, words: List[str]) -> int: def pre(word2, word): if len(word2) + 1 == len(word): for i in range(len(word)): if word[:i] + word[i + 1 :] == word2: return True return False words = sorted(words, key=lambda x: -len(x)) best = 0 n = len(words) length = [0] * n for i in range(n): for j in range(i + 1, n): if pre(words[j], words[i]): length[j] = max(length[j], length[i] + 1) best = max(best, length[j]) return best + 1
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN BIN_OP VAR NUMBER VAR
Given a list of words, each word consists of English lowercase letters. Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2.  For example, "abc" is a predecessor of "abac". A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on. Return the longest possible length of a word chain with words chosen from the given list of words.   Example 1: Input: ["a","b","ba","bca","bda","bdca"] Output: 4 Explanation: one of the longest word chain is "a","ba","bda","bdca".   Note: 1 <= words.length <= 1000 1 <= words[i].length <= 16 words[i] only consists of English lowercase letters.
class Solution: def longestStrChain(self, words: List[str]) -> int: words.sort(key=len) wordCounter = [collections.Counter(x) for x in words] dp = [(1) for _ in range(len(words))] for i in range(len(words) - 2, -1, -1): for j in range(i + 1, len(words)): if len(words[i]) == len(words[j]): continue if len(words[j]) - len(words[i]) > 1: break if len(wordCounter[j] - wordCounter[i]) == 1: dp[i] = max(dp[i], dp[j] + 1) return max(dp)
CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR
Given a list of words, each word consists of English lowercase letters. Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2.  For example, "abc" is a predecessor of "abac". A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on. Return the longest possible length of a word chain with words chosen from the given list of words.   Example 1: Input: ["a","b","ba","bca","bda","bdca"] Output: 4 Explanation: one of the longest word chain is "a","ba","bda","bdca".   Note: 1 <= words.length <= 1000 1 <= words[i].length <= 16 words[i] only consists of English lowercase letters.
class Solution: def longestStrChain(self, words: List[str]) -> int: words.sort(key=len) print(words) sequence = collections.defaultdict(list) dp = [1] * len(words) for i in range(len(words)): for j in range(0, i): predecessor = self.checkPredecessor(words[j], words[i]) if predecessor: sequence[words[i]].append(words[j]) dp[i] = max(dp[j] + 1, dp[i]) print(sequence) print(list(zip(words, dp))) return max(dp) def checkPredecessor(self, word1, word2): if len(word2) - len(word1) > 1: return False elif len(word1) == len(word2): return False i = j = 0 diff = False while i < len(word1): if word1[i] == word2[j]: i += 1 j += 1 elif not diff: diff = True j += 1 else: return False if i == len(word1) and j < len(word2) and diff: return False return True
CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN NUMBER
Given a list of words, each word consists of English lowercase letters. Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2.  For example, "abc" is a predecessor of "abac". A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on. Return the longest possible length of a word chain with words chosen from the given list of words.   Example 1: Input: ["a","b","ba","bca","bda","bdca"] Output: 4 Explanation: one of the longest word chain is "a","ba","bda","bdca".   Note: 1 <= words.length <= 1000 1 <= words[i].length <= 16 words[i] only consists of English lowercase letters.
class Solution: def longestStrChain(self, words: List[str]) -> int: graph = collections.defaultdict(list) maxlen = float("-inf") minlen = float("inf") for word in words: graph[len(word)].append(word) maxlen = max(maxlen, len(word)) minlen = min(minlen, len(word)) memo = {} def predecessor(word1, word2): if not word1: return True if len(word1) + 1 != len(word2): return False if (word1, word2) in memo: return memo[word1, word2] fill = 1 left, right = 0, 0 ans = True while left < len(word1) and right < len(word2): if word1[left] != word2[right]: if fill: right += 1 fill -= 1 continue else: ans = False break left += 1 right += 1 memo[word1, word2] = ans return ans def chain(length, s=""): if length not in graph: return 0 ans = 0 for w in graph[length]: if predecessor(s, w): ans = max(ans, 1 + chain(length + 1, w)) return ans return max(chain(l) for l in range(minlen, maxlen + 1))
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR RETURN NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_DEF STRING IF VAR VAR RETURN NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR
Given a list of words, each word consists of English lowercase letters. Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2.  For example, "abc" is a predecessor of "abac". A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on. Return the longest possible length of a word chain with words chosen from the given list of words.   Example 1: Input: ["a","b","ba","bca","bda","bdca"] Output: 4 Explanation: one of the longest word chain is "a","ba","bda","bdca".   Note: 1 <= words.length <= 1000 1 <= words[i].length <= 16 words[i] only consists of English lowercase letters.
class Solution: def longestStrChain(self, words: List[str]) -> int: if not words: return 0 def isPred(p, q, words): w1 = words[p] w2 = words[q] n2 = len(w2) res = False for i in range(n2): if w1 == w2[0:i] + w2[i + 1 :]: res = True return res n = len(words) dpR = [1] * n wR = sorted(words, key=len) for i in range(n): cur = 1 for j in range(i): n1 = len(wR[j]) n2 = len(wR[i]) if n2 - n1 != 1: continue if isPred(j, i, wR): cur = max(cur, dpR[j] + 1) dpR[i] = cur return max(dpR)
CLASS_DEF FUNC_DEF VAR VAR IF VAR RETURN NUMBER FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR
Given a list of words, each word consists of English lowercase letters. Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2.  For example, "abc" is a predecessor of "abac". A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on. Return the longest possible length of a word chain with words chosen from the given list of words.   Example 1: Input: ["a","b","ba","bca","bda","bdca"] Output: 4 Explanation: one of the longest word chain is "a","ba","bda","bdca".   Note: 1 <= words.length <= 1000 1 <= words[i].length <= 16 words[i] only consists of English lowercase letters.
class Solution: def longestStrChain(self, words: List[str]) -> int: m = collections.defaultdict(list) words = sorted(words, key=len) for w in words: m[len(w)].append("".join(sorted(w))) if len(m) == 1: return 1 def equal(x, y): for i in range(len(x)): if x[i] != y[i]: return x == y[:i] + y[i + 1 :] return True def dfs(w): k = len(w) + 1 if k not in m: return 0 return max([(dfs(x) + 1) for x in m[k] if equal(w, x)] or [0]) return max(dfs(x) for y in list(m.keys())[:-1] for x in m[y]) + 1
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL STRING FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR RETURN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR VAR LIST NUMBER RETURN BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR VAR VAR NUMBER VAR
Given a list of words, each word consists of English lowercase letters. Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2.  For example, "abc" is a predecessor of "abac". A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on. Return the longest possible length of a word chain with words chosen from the given list of words.   Example 1: Input: ["a","b","ba","bca","bda","bdca"] Output: 4 Explanation: one of the longest word chain is "a","ba","bda","bdca".   Note: 1 <= words.length <= 1000 1 <= words[i].length <= 16 words[i] only consists of English lowercase letters.
class Solution: def longestStrChain(self, words: List[str]) -> int: words.sort(key=len) dp = [1] * len(words) m = 1 for i in range(1, len(words)): j = i - 1 cl = len(words[i]) while len(words[j]) >= cl - 1 and j > -1: if len(words[j]) == cl: j -= 1 continue f = 0 w1, w2 = words[i], words[j] li, lj = 0, 0 while f < 2 and lj < len(w2): if w1[li] == w2[lj]: li += 1 lj += 1 else: f += 1 li += 1 if f != 2: dp[i] = max(dp[i], 1 + dp[j]) m = max(dp[i], m) j -= 1 return m
CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR
Given a list of words, each word consists of English lowercase letters. Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2.  For example, "abc" is a predecessor of "abac". A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on. Return the longest possible length of a word chain with words chosen from the given list of words.   Example 1: Input: ["a","b","ba","bca","bda","bdca"] Output: 4 Explanation: one of the longest word chain is "a","ba","bda","bdca".   Note: 1 <= words.length <= 1000 1 <= words[i].length <= 16 words[i] only consists of English lowercase letters.
class Solution: def longestStrChain(self, words: List[str]) -> int: def check(w1, w2): idx = 0 for i in range(len(w2)): if w2[i] == w1[idx]: idx += 1 if idx == len(w1): return True return False n = len(words) words.sort(key=lambda w: len(w)) G = [[] for _ in range(n)] for i in range(n): for j in range(i + 1, n): if len(words[j]) - len(words[i]) > 1: break if len(words[j]) - len(words[i]) == 1 and check(words[i], words[j]): G[i].append(j) def rec(v): if memo[v] != -1: return memo[v] res = 1 for nv in G[v]: res = max(res, rec(nv) + 1) memo[v] = res return res memo = [-1] * n ans = 0 for i in range(n): ans = max(ans, rec(i)) return ans
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR
Given a list of words, each word consists of English lowercase letters. Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2.  For example, "abc" is a predecessor of "abac". A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on. Return the longest possible length of a word chain with words chosen from the given list of words.   Example 1: Input: ["a","b","ba","bca","bda","bdca"] Output: 4 Explanation: one of the longest word chain is "a","ba","bda","bdca".   Note: 1 <= words.length <= 1000 1 <= words[i].length <= 16 words[i] only consists of English lowercase letters.
class Solution: def longestStrChain(self, words: List[str]) -> int: self.res = 0 def compare(w1, w2): if len(w1) > len(w2): w1, w2 = w2, w1 if len(w2) - len(w1) != 1: return False i, j = 0, 0 flag = False while i < len(w1) or j < len(w2): if i < len(w1) and w1[i] == w2[j]: i += 1 j += 1 elif not flag: flag = True j += 1 else: return False return True @lru_cache(maxsize=None) def dfs(w): length = 0 for word in words: if len(word) - len(w) == 1 and compare(w, word): length = max(dfs(word), length) ret = length + 1 self.res = max(ret, self.res) return ret for w in words: dfs(w) return self.res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_CALL VAR NONE FOR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR
Given a list of words, each word consists of English lowercase letters. Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2.  For example, "abc" is a predecessor of "abac". A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on. Return the longest possible length of a word chain with words chosen from the given list of words.   Example 1: Input: ["a","b","ba","bca","bda","bdca"] Output: 4 Explanation: one of the longest word chain is "a","ba","bda","bdca".   Note: 1 <= words.length <= 1000 1 <= words[i].length <= 16 words[i] only consists of English lowercase letters.
class Solution: def longestStrChain(self, words: List[str]) -> int: auxList = [(len(item), item) for item in set(words)] auxList.sort() auxGraph = collections.defaultdict(set) seen = dict() for idx, (wordLen, word) in enumerate(auxList): seen[word] = False for jdx in range(idx + 1, len(auxList)): cWordLen, cWord = auxList[jdx] if cWordLen > wordLen + 1: break elif cWordLen == wordLen: pass else: for pivot in range(len(cWord)): if cWord[:pivot] + cWord[pivot + 1 :] == word: auxGraph[word].add(cWord) ans = 0 auxDeque = collections.deque() for _, word in auxList: if seen[word]: pass else: seen[word] = True auxDeque.append((1, word)) while len(auxDeque) > 0: cLen, cnode = auxDeque.pop() ans = max(ans, cLen) for eachNeighbor in auxGraph[cnode]: if not seen[eachNeighbor]: seen[eachNeighbor] = True auxDeque.append((cLen + 1, eachNeighbor)) return ans
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR
Given a list of words, each word consists of English lowercase letters. Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2.  For example, "abc" is a predecessor of "abac". A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on. Return the longest possible length of a word chain with words chosen from the given list of words.   Example 1: Input: ["a","b","ba","bca","bda","bdca"] Output: 4 Explanation: one of the longest word chain is "a","ba","bda","bdca".   Note: 1 <= words.length <= 1000 1 <= words[i].length <= 16 words[i] only consists of English lowercase letters.
class Solution: def longestStrChain(self, words: List[str]) -> int: def isPred(word1, word2): for i in range(len(word2)): if word2[0:i] + word2[i + 1 :] == word1: return True return False graph = {} for i in range(len(words) - 1): for j in range(i + 1, len(words)): w1, w2 = words[i], words[j] if len(w2) - len(w1) == 1 and isPred(w1, w2): if w1 in graph: graph[w1].append(w2) else: graph[w1] = [w2] elif len(w2) - len(w1) == -1 and isPred(w2, w1): if w2 in graph: graph[w2].append(w1) else: graph[w2] = [w1] self.res = 0 def dfs(word, lev): self.res = max(self.res, lev) if word not in graph: return for i in graph[word]: dfs(i, lev + 1) for i_word in words: dfs(i_word, 1) return self.res
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR RETURN FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER RETURN VAR VAR
Given a list of words, each word consists of English lowercase letters. Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2.  For example, "abc" is a predecessor of "abac". A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on. Return the longest possible length of a word chain with words chosen from the given list of words.   Example 1: Input: ["a","b","ba","bca","bda","bdca"] Output: 4 Explanation: one of the longest word chain is "a","ba","bda","bdca".   Note: 1 <= words.length <= 1000 1 <= words[i].length <= 16 words[i] only consists of English lowercase letters.
class Solution: def longestStrChain(self, words: List[str]) -> int: dic = defaultdict(list) words.sort(key=len, reverse=True) for w in words: for i in range(len(w)): if w[:i] + w[i + 1 :] in words: dic[w].append(w[:i] + w[i + 1 :]) def bt(x): return ( [[x]] if x not in dic else [([x] + rest) for y in dic[x] for rest in bt(y)] ) ct = 1 for word in words: ct = max(ct, len(max(bt(word), key=len))) return ct
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FUNC_DEF RETURN VAR VAR LIST LIST VAR BIN_OP LIST VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
Given a list of words, each word consists of English lowercase letters. Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2.  For example, "abc" is a predecessor of "abac". A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on. Return the longest possible length of a word chain with words chosen from the given list of words.   Example 1: Input: ["a","b","ba","bca","bda","bdca"] Output: 4 Explanation: one of the longest word chain is "a","ba","bda","bdca".   Note: 1 <= words.length <= 1000 1 <= words[i].length <= 16 words[i] only consists of English lowercase letters.
class Solution: def longestStrChain(self, words: List[str]) -> int: def pred(origin, dest): if len(dest) - len(origin) != 1: return False misWord = False oIndex = 0 for index in range(len(dest)): if oIndex == len(origin): if misWord == False: return True return False if dest[index] != origin[oIndex]: if misWord: return False misWord = True continue oIndex += 1 return True table = {} def helper(node): longest = 0 if node in table: return table[node] for word in words: if pred(node, word): longest = max(helper(word), longest) table[node] = longest + 1 return longest + 1 maxPos = 0 for word in words: maxPos = max(maxPos, helper(word)) return maxPos
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER RETURN NUMBER IF VAR VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR DICT FUNC_DEF ASSIGN VAR NUMBER IF VAR VAR RETURN VAR VAR FOR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR
Given a list of words, each word consists of English lowercase letters. Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2.  For example, "abc" is a predecessor of "abac". A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on. Return the longest possible length of a word chain with words chosen from the given list of words.   Example 1: Input: ["a","b","ba","bca","bda","bdca"] Output: 4 Explanation: one of the longest word chain is "a","ba","bda","bdca".   Note: 1 <= words.length <= 1000 1 <= words[i].length <= 16 words[i] only consists of English lowercase letters.
class Solution: def longestStrChain(self, words: List[str]) -> int: self.ans = 0 words = sorted(words, key=lambda x: len(x)) self.n = len(words) self.m = {} for i in range(self.n): if words[i] in self.m: self.m[words[i]] = i else: self.m[words[i]] = i for i in range(self.n - 1, -1, -1): self.findLongest(words, i, 0) return self.ans def findLongest(self, words, i, cur_ans): for j in range(len(words[i])): if words[i][:j] + words[i][j + 1 :] in self.m: self.findLongest( words, self.m[words[i][:j] + words[i][j + 1 :]], cur_ans + 1 ) self.ans = max(self.ans, cur_ans + 1)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER
Given a list of words, each word consists of English lowercase letters. Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2.  For example, "abc" is a predecessor of "abac". A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on. Return the longest possible length of a word chain with words chosen from the given list of words.   Example 1: Input: ["a","b","ba","bca","bda","bdca"] Output: 4 Explanation: one of the longest word chain is "a","ba","bda","bdca".   Note: 1 <= words.length <= 1000 1 <= words[i].length <= 16 words[i] only consists of English lowercase letters.
class Solution: def longestStrChain(self, words: List[str]) -> int: n = len(words) dp = [1] * n words.sort(key=len) for i in range(1, n): for j in range(i): if self.is_predecessor(words[i], words[j]): dp[i] = max(dp[i], dp[j] + 1) return max(dp) def is_predecessor(self, word1, word2): if len(word1) - len(word2) != 1: return False i1 = 0 i2 = 0 diff = 0 while i1 < len(word1) and i2 < len(word2): if word1[i1] == word2[i2]: i1 += 1 i2 += 1 else: i1 += 1 diff += 1 if diff > 1: return False return True
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR FUNC_DEF IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER
Given a list of words, each word consists of English lowercase letters. Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2.  For example, "abc" is a predecessor of "abac". A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on. Return the longest possible length of a word chain with words chosen from the given list of words.   Example 1: Input: ["a","b","ba","bca","bda","bdca"] Output: 4 Explanation: one of the longest word chain is "a","ba","bda","bdca".   Note: 1 <= words.length <= 1000 1 <= words[i].length <= 16 words[i] only consists of English lowercase letters.
class Solution: def longestStrChain(self, words: List[str]) -> int: lengths = [len(word) for word in words] wordsSorted = [x for _, x in sorted(zip(lengths, words), reverse=True)] print(wordsSorted) L = [1] * len(wordsSorted) maxLength = max(lengths) for k in range(0, len(L)): lenWord = len(wordsSorted[k]) if lenWord == maxLength: L[k] = 1 else: for l in range(k): if len(wordsSorted[l]) == lenWord + 1 and set(wordsSorted[l]) & set( wordsSorted[k] ) == set(wordsSorted[k]): L[k] = max(L[k], 1 + L[l]) return max(L)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR VAR RETURN FUNC_CALL VAR VAR VAR
Given a list of words, each word consists of English lowercase letters. Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2.  For example, "abc" is a predecessor of "abac". A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on. Return the longest possible length of a word chain with words chosen from the given list of words.   Example 1: Input: ["a","b","ba","bca","bda","bdca"] Output: 4 Explanation: one of the longest word chain is "a","ba","bda","bdca".   Note: 1 <= words.length <= 1000 1 <= words[i].length <= 16 words[i] only consists of English lowercase letters.
class Solution: def longestStrChain(self, words: List[str]) -> int: d = {} for word in words: if len(word) in d: d[len(word)].append(word) else: d[len(word)] = [word] visited = {} res = 0 for word in sorted(words): res = max(res, self.dfs(word, visited, d)) return res def checkPredecessor(self, word1, word2): return any(word1 == word2[:i] + word2[i + 1 :] for i in range(len(word2))) def dfs(self, word, visited, d): if word in visited: return visited[word] if len(word) + 1 not in d: visited[word] = 1 return 1 res = 1 for w in d[len(word) + 1]: if self.checkPredecessor(word, w): res = max(res, self.dfs(w, visited, d) + 1) visited[word] = res return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FOR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR LIST VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR RETURN VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR
Given a list of words, each word consists of English lowercase letters. Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2.  For example, "abc" is a predecessor of "abac". A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on. Return the longest possible length of a word chain with words chosen from the given list of words.   Example 1: Input: ["a","b","ba","bca","bda","bdca"] Output: 4 Explanation: one of the longest word chain is "a","ba","bda","bdca".   Note: 1 <= words.length <= 1000 1 <= words[i].length <= 16 words[i] only consists of English lowercase letters.
class Solution: def longestStrChain(self, words: List[str]) -> int: def isPredecessor(w1, l1, w2, l2): if l2 == l1 + 1: for i in w1: if i not in w2: return False return True return False words.sort(key=lambda x: len(x)) L = [len(i) for i in words] DP = [(1) for i in range(len(L))] ans = 1 for i in range(1, len(L)): for j in range(0, i): if isPredecessor(words[j], L[j], words[i], L[i]): DP[i] = max(DP[j] + 1, DP[i]) ans = max(ans, DP[i]) return ans
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF VAR BIN_OP VAR NUMBER FOR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER RETURN NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR
Given a list of words, each word consists of English lowercase letters. Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2.  For example, "abc" is a predecessor of "abac". A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on. Return the longest possible length of a word chain with words chosen from the given list of words.   Example 1: Input: ["a","b","ba","bca","bda","bdca"] Output: 4 Explanation: one of the longest word chain is "a","ba","bda","bdca".   Note: 1 <= words.length <= 1000 1 <= words[i].length <= 16 words[i] only consists of English lowercase letters.
class Solution: def longestStrChain(self, words: List[str]) -> int: def is_edit(a, b): if abs(len(a) - len(b)) > 1: return False if len(a) == len(b): return False for i in range(min(len(a), len(b))): if a[i] != b[i]: return a == b[:i] + b[i + 1 :] or b == a[:i] + a[i + 1 :] return True words.sort(key=lambda x: len(x)) n = len(words) memo = [(1) for _ in range(n)] ans = 1 for i in range(n - 1, -1, -1): max_ = 0 for j in range(i + 1, n): if is_edit(words[i], words[j]): max_ = max(max_, memo[j]) memo[i] = 1 + max_ ans = max(memo[i], ans) return ans
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR RETURN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR
Given a list of words, each word consists of English lowercase letters. Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2.  For example, "abc" is a predecessor of "abac". A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on. Return the longest possible length of a word chain with words chosen from the given list of words.   Example 1: Input: ["a","b","ba","bca","bda","bdca"] Output: 4 Explanation: one of the longest word chain is "a","ba","bda","bdca".   Note: 1 <= words.length <= 1000 1 <= words[i].length <= 16 words[i] only consists of English lowercase letters.
class Solution: def longestStrChain(self, words: List[str]) -> int: def check(str1, str2): for i in range(len(str2)): temp = str2[:i] + str2[i + 1 :] if temp == str1: return True return False words.sort(key=len) min_length = len(words[0]) dp = [1] * len(words) for i in range(len(words)): if len(words[i]) == min_length: continue for j in range(len(words)): if len(words[j]) == len(words[i]) - 1: if check(words[j], words[i]): dp[i] = max(dp[i], dp[j] + 1) elif len(words[j]) >= len(words[i]): break return max(dp)
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR
Given a list of words, each word consists of English lowercase letters. Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2.  For example, "abc" is a predecessor of "abac". A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on. Return the longest possible length of a word chain with words chosen from the given list of words.   Example 1: Input: ["a","b","ba","bca","bda","bdca"] Output: 4 Explanation: one of the longest word chain is "a","ba","bda","bdca".   Note: 1 <= words.length <= 1000 1 <= words[i].length <= 16 words[i] only consists of English lowercase letters.
def is_pred(w1, w2): skip_count = 0 if len(w1) + 1 != len(w2): return False for i in range(0, len(w1)): if w1[i] == w2[i + skip_count]: pass elif skip_count == 0 and w1[i] == w2[i + 1]: skip_count += 1 else: return False return True def tests(): assert is_pred("a", "ab") assert not is_pred("a", "a") assert not is_pred("a", "bc") assert not is_pred("ab", "a") class Solution: def longestStrChain(self, words: List[str]) -> int: words.sort(key=lambda w: len(w)) chains = {} current_max = 0 for w in words: chains[w] = 1 current_max = max(current_max, 1) for chain, chain_len in list(chains.items()): if is_pred(chain, w): chains[w] = max(chains[w], chain_len + 1) current_max = max(current_max, chains[w]) return current_max
FUNC_DEF ASSIGN VAR NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF FUNC_CALL VAR STRING STRING FUNC_CALL VAR STRING STRING FUNC_CALL VAR STRING STRING FUNC_CALL VAR STRING STRING CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR
Given a list of words, each word consists of English lowercase letters. Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2.  For example, "abc" is a predecessor of "abac". A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on. Return the longest possible length of a word chain with words chosen from the given list of words.   Example 1: Input: ["a","b","ba","bca","bda","bdca"] Output: 4 Explanation: one of the longest word chain is "a","ba","bda","bdca".   Note: 1 <= words.length <= 1000 1 <= words[i].length <= 16 words[i] only consists of English lowercase letters.
class Solution: def longestStrChain(self, words: List[str]) -> int: if len(words) <= 0: return 0 words.sort(key=len) numberOfWords = len(words) longestLens = [1] * len(words) index = numberOfWords - 1 while index >= 0: if index == 0 or len(words[index]) != len(words[index - 1]): index -= 1 break index -= 1 while index >= 0: maxLen = 1 index2 = index + 1 while index2 < numberOfWords and len(words[index]) + 1 >= len( words[index2] ): if self.isPredecessor(words[index], words[index2]): if maxLen < longestLens[index2] + 1: maxLen = longestLens[index2] + 1 index2 += 1 longestLens[index] = maxLen index -= 1 maxLen = longestLens[0] for longestLen in longestLens: if maxLen < longestLen: maxLen = longestLen return maxLen def isPredecessor(self, word1: str, word2: str) -> bool: if len(word2) - len(word1) != 1: return False for i, ch in enumerate(word2): newWord = word2[:i] + word2[i + 1 :] if newWord == word1: return True return False
CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR IF VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR VAR FUNC_DEF VAR VAR IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER VAR
Given a list of words, each word consists of English lowercase letters. Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2.  For example, "abc" is a predecessor of "abac". A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on. Return the longest possible length of a word chain with words chosen from the given list of words.   Example 1: Input: ["a","b","ba","bca","bda","bdca"] Output: 4 Explanation: one of the longest word chain is "a","ba","bda","bdca".   Note: 1 <= words.length <= 1000 1 <= words[i].length <= 16 words[i] only consists of English lowercase letters.
class Solution: def longestStrChain(self, words: List[str]) -> int: words.sort(key=lambda x: len(x)) mark = {} queue = [] for index, word in enumerate(words): mark[word] = collections.Counter(word) queue.append([[word], index, len(word)]) ans = 0 while queue: s, index, l = queue.pop(0) flag = True for i in range(index + 1, len(words)): if len(words[i]) > l + 1: break if len(words[i]) == l + 1: add = True sum1 = 0 sum2 = 0 for key in list(mark[s[-1]].keys()): if key not in mark[words[i]]: add = False break sum1 += mark[s[-1]][key] sum2 += mark[words[i]][key] if add and (sum1 == sum2 or sum2 - sum1 == 1): queue.append([s + [words[i]], i, l + 1]) flag = False if flag: ans = max(ans, len(s)) return ans
CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST LIST VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR LIST VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR
Given a list of words, each word consists of English lowercase letters. Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2.  For example, "abc" is a predecessor of "abac". A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on. Return the longest possible length of a word chain with words chosen from the given list of words.   Example 1: Input: ["a","b","ba","bca","bda","bdca"] Output: 4 Explanation: one of the longest word chain is "a","ba","bda","bdca".   Note: 1 <= words.length <= 1000 1 <= words[i].length <= 16 words[i] only consists of English lowercase letters.
class Solution: def longestStrChain(self, words: List[str]) -> int: if not words: return 0 graph = defaultdict(list) in_degrees = defaultdict(int) for w1 in words: for w2 in words: if self.isPredecessor(w1, w2): graph[w2].append(w1) in_degrees[w1] += 1 max_len = 1 queue = deque([(w, 1) for w in graph if in_degrees[w] == 0]) while queue: word, path_len = queue.popleft() for child in graph[word]: in_degrees[child] -= 1 if in_degrees[child] == 0: queue.append((child, path_len + 1)) max_len = path_len return max_len def isPredecessor(self, w1, w2): if len(w1) != len(w2) + 1: return False i1 = 0 i2 = 0 added = False while i1 < len(w1) and i2 < len(w2): if w1[i1] != w2[i2] and added: return False elif w1[i1] != w2[i2]: i1 += 1 added = True else: i1 += 1 i2 += 1 return True
CLASS_DEF FUNC_DEF VAR VAR IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR NUMBER WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR FOR VAR VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER
Given a list of words, each word consists of English lowercase letters. Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2.  For example, "abc" is a predecessor of "abac". A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on. Return the longest possible length of a word chain with words chosen from the given list of words.   Example 1: Input: ["a","b","ba","bca","bda","bdca"] Output: 4 Explanation: one of the longest word chain is "a","ba","bda","bdca".   Note: 1 <= words.length <= 1000 1 <= words[i].length <= 16 words[i] only consists of English lowercase letters.
class Solution: def longestStrChain(self, words: List[str]) -> int: words.sort(key=lambda x: len(x)) dp = [1] * len(words) for i in range(len(words)): curr = words[i] for j in range(i): prev = words[j] if self.isPredecessor(curr, prev): dp[i] = max(dp[i], dp[j] + 1) return max(dp) def isPredecessor(self, curr, prev): if len(curr) - len(prev) != 1: return False i = j = 0 diff = False while i < len(curr) and j < len(prev): if curr[i] == prev[j]: i += 1 j += 1 else: if diff: return False diff = True i += 1 return True
CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR FUNC_DEF IF BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR RETURN NUMBER ASSIGN VAR NUMBER VAR NUMBER RETURN NUMBER
Given a list of words, each word consists of English lowercase letters. Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2.  For example, "abc" is a predecessor of "abac". A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on. Return the longest possible length of a word chain with words chosen from the given list of words.   Example 1: Input: ["a","b","ba","bca","bda","bdca"] Output: 4 Explanation: one of the longest word chain is "a","ba","bda","bdca".   Note: 1 <= words.length <= 1000 1 <= words[i].length <= 16 words[i] only consists of English lowercase letters.
class Solution: def longestStrChain(self, words: List[str]) -> int: def isPredecessor(word1, word2): if len(word1) + 1 != len(word2): return False i, j, count = 0, 0, 0 while i < len(word1): if word1[i] != word2[j]: count += 1 j += 1 if count > 1: return False continue i += 1 j += 1 return True numPredecessor = [0] * len(words) graph = defaultdict(lambda: []) for i in range(len(words)): for j in range(i + 1, len(words)): if isPredecessor(words[i], words[j]): graph[i].append(j) numPredecessor[j] += 1 elif isPredecessor(words[j], words[i]): graph[j].append(i) numPredecessor[i] += 1 def dfs(node, path): nonlocal out path.append(node) out = max(out, len(path)) for nei in graph[node]: dfs(nei, path) path.pop() out = 0 for node in [i for i in range(len(words)) if not numPredecessor[i]]: dfs(node, []) return out
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER RETURN NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR LIST RETURN VAR VAR
Given a list of words, each word consists of English lowercase letters. Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2.  For example, "abc" is a predecessor of "abac". A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on. Return the longest possible length of a word chain with words chosen from the given list of words.   Example 1: Input: ["a","b","ba","bca","bda","bdca"] Output: 4 Explanation: one of the longest word chain is "a","ba","bda","bdca".   Note: 1 <= words.length <= 1000 1 <= words[i].length <= 16 words[i] only consists of English lowercase letters.
class Solution: def longestStrChain(self, words: List[str]) -> int: auxList = [(len(item), item) for item in set(words)] auxList.sort() auxCntList = [1] * len(auxList) ans = 1 for idx, (wordLen, word) in enumerate(auxList): for jdx in range(idx + 1, len(auxList)): cWordLen, cWord = auxList[jdx] if cWordLen > wordLen + 1: break elif cWordLen == wordLen: pass else: for pivot in range(len(cWord)): if cWord[:pivot] + cWord[pivot + 1 :] == word: auxCntList[jdx] = auxCntList[idx] + 1 ans = max(ans, auxCntList[jdx]) return ans
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR
Given a list of words, each word consists of English lowercase letters. Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2.  For example, "abc" is a predecessor of "abac". A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on. Return the longest possible length of a word chain with words chosen from the given list of words.   Example 1: Input: ["a","b","ba","bca","bda","bdca"] Output: 4 Explanation: one of the longest word chain is "a","ba","bda","bdca".   Note: 1 <= words.length <= 1000 1 <= words[i].length <= 16 words[i] only consists of English lowercase letters.
class Solution: def longestStrChain(self, words: List[str]) -> int: n = len(words) a = sorted(words, key=len) dp = [1] * n def helper(a1, b1): s = 0 n = len(b1) i = 0 j = 0 while i < n and j < n + 1: if a1[j] == b1[i]: i += 1 j += 1 elif a1[j] != b1[i] and s == 0: s += 1 j += 1 else: return 0 return 1 for i in range(1, n): for j in range(i - 1, -1, -1): if len(a[j]) == len(a[i]): continue elif len(a[j]) < len(a[i]) - 1: break x = helper(a[i], a[j]) if x == 1: dp[i] = max(dp[i], dp[j] + 1) return max(dp)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR
Given a list of words, each word consists of English lowercase letters. Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2.  For example, "abc" is a predecessor of "abac". A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on. Return the longest possible length of a word chain with words chosen from the given list of words.   Example 1: Input: ["a","b","ba","bca","bda","bdca"] Output: 4 Explanation: one of the longest word chain is "a","ba","bda","bdca".   Note: 1 <= words.length <= 1000 1 <= words[i].length <= 16 words[i] only consists of English lowercase letters.
class Solution: def longestStrChain(self, words: List[str]) -> int: wordsbylen = collections.defaultdict(list) for w in words: wordsbylen[len(w)].append(w) def get_longest_derivate(word): potential = wordsbylen[len(word) + 1] possible = [1] for p in potential: i, j = 0, 0 while i < len(p) and j < len(word): if p[i] == word[j]: i += 1 j += 1 elif i != j: break else: i += 1 if j == len(word): possible.append(1 + get_longest_derivate(p)) return max(possible) return max(map(get_longest_derivate, words))
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Given a list of words, each word consists of English lowercase letters. Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2.  For example, "abc" is a predecessor of "abac". A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on. Return the longest possible length of a word chain with words chosen from the given list of words.   Example 1: Input: ["a","b","ba","bca","bda","bdca"] Output: 4 Explanation: one of the longest word chain is "a","ba","bda","bdca".   Note: 1 <= words.length <= 1000 1 <= words[i].length <= 16 words[i] only consists of English lowercase letters.
class Solution: def longestStrChain(self, words: List[str]) -> int: def is_predecessor(word_short, word_long) -> bool: if len(word_short) + 1 != len(word_long): return False i_s = 0 can_skip = True for i_l in range(len(word_long)): if i_s > len(word_short) - 1: return True c_s = word_short[i_s] c_l = word_long[i_l] if c_s == c_l: i_s += 1 elif can_skip: can_skip = False pass else: return False return True if not words: return 0 words.sort(key=lambda x: len(x)) dp = [1] * len(words) for r in range(1, len(words)): for l in range(r): if is_predecessor(words[l], words[r]): dp[r] = max(dp[r], dp[l] + 1) return max(dp)
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR ASSIGN VAR NUMBER RETURN NUMBER RETURN NUMBER VAR IF VAR RETURN NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR
Given a list of words, each word consists of English lowercase letters. Let's say word1 is a predecessor of word2 if and only if we can add exactly one letter anywhere in word1 to make it equal to word2.  For example, "abc" is a predecessor of "abac". A word chain is a sequence of words [word_1, word_2, ..., word_k] with k >= 1, where word_1 is a predecessor of word_2, word_2 is a predecessor of word_3, and so on. Return the longest possible length of a word chain with words chosen from the given list of words.   Example 1: Input: ["a","b","ba","bca","bda","bdca"] Output: 4 Explanation: one of the longest word chain is "a","ba","bda","bdca".   Note: 1 <= words.length <= 1000 1 <= words[i].length <= 16 words[i] only consists of English lowercase letters.
class Solution: def longestStrChain(self, words: List[str]) -> int: def helper(a, b): i, j = 0, 0 diff = 0 while i < len(a) and j < len(b): if a[i] == b[j]: i += 1 j += 1 else: i += 1 diff += 1 if i < len(a): diff += len(a) - i if j < len(b): diff += len(b) - j if diff > 1: return False else: return True dp = [1] * len(words) words = sorted(words, key=len) for i in range(len(words)): for j in range(i): if len(words[i]) - len(words[j]) == 1 and helper(words[i], words[j]): dp[i] = max(dp[i], dp[j] + 1) print(dp) return max(dp)
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR
This is the easy version of the problem. The only difference is that in this version k = 0. There is an array a_1, a_2, …, a_n of n positive integers. You should divide it into a minimal number of continuous segments, such that in each segment there are no two numbers (on different positions), whose product is a perfect square. Moreover, it is allowed to do at most k such operations before the division: choose a number in the array and change its value to any positive integer. But in this version k = 0, so it is not important. What is the minimum number of continuous segments you should use if you will make changes optimally? Input The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases. The first line of each test case contains two integers n, k (1 ≤ n ≤ 2 ⋅ 10^5, k = 0). The second line of each test case contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^7). It's guaranteed that the sum of n over all test cases does not exceed 2 ⋅ 10^5. Output For each test case print a single integer — the answer to the problem. Example Input 3 5 0 18 6 2 4 1 5 0 6 8 1 24 8 1 0 1 Output 3 2 1 Note In the first test case the division may be as follows: * [18, 6] * [2, 4] * [1]
NN = 4000 isPrime = [True] * (NN + 1) isPrime[1] = False i = 2 primes = [] while i * i <= NN: if isPrime[i]: for j in range(i * i, NN + 1, i): isPrime[j] = False i += 1 primes = [] for i in range(2, NN + 1): if isPrime[i]: primes.append(i) for _ in range(int(input())): n, k = map(int, input().split()) arr = list(map(int, input().split())) ans = 1 s = set() for a in arr: pi = 0 need = 1 while a > 1 and pi < len(primes): picnt = 0 while a % primes[pi] == 0: a /= primes[pi] picnt += 1 if picnt % 2 == 1: need *= primes[pi] pi += 1 if a > 1: need *= a if need in s: ans += 1 s = set() s.add(need) else: s.add(need) print(ans)
ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE BIN_OP VAR VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
This is the easy version of the problem. The only difference is that in this version k = 0. There is an array a_1, a_2, …, a_n of n positive integers. You should divide it into a minimal number of continuous segments, such that in each segment there are no two numbers (on different positions), whose product is a perfect square. Moreover, it is allowed to do at most k such operations before the division: choose a number in the array and change its value to any positive integer. But in this version k = 0, so it is not important. What is the minimum number of continuous segments you should use if you will make changes optimally? Input The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases. The first line of each test case contains two integers n, k (1 ≤ n ≤ 2 ⋅ 10^5, k = 0). The second line of each test case contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^7). It's guaranteed that the sum of n over all test cases does not exceed 2 ⋅ 10^5. Output For each test case print a single integer — the answer to the problem. Example Input 3 5 0 18 6 2 4 1 5 0 6 8 1 24 8 1 0 1 Output 3 2 1 Note In the first test case the division may be as follows: * [18, 6] * [2, 4] * [1]
import sys input = sys.stdin.readline def is_Prime(i): if i == 2: return True if i % 2 == 0 or i == 1: return False s = int(i ** (1 / 2)) while True: if s == 1: return True if i % s == 0: return False s -= 1 t = int(input()) sq = [pow(2, 2)] for i in range(3, pow(10, 4), 2): if is_Prime(i): sq.append(pow(i, 2)) inf = pow(10, 7) for _ in range(t): n, k = map(int, input().split()) a = list(map(int, input().split())) ng = set() ans = 1 for i in a: x = i for j in sq: if x < j: break while not x % j: x //= j if not x in ng: ng.add(x) else: ans += 1 ng.clear() ng.add(x) print(ans)
IMPORT ASSIGN VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP NUMBER NUMBER WHILE NUMBER IF VAR NUMBER RETURN NUMBER IF BIN_OP VAR VAR NUMBER RETURN NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR FOR VAR VAR IF VAR VAR WHILE BIN_OP VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
This is the easy version of the problem. The only difference is that in this version k = 0. There is an array a_1, a_2, …, a_n of n positive integers. You should divide it into a minimal number of continuous segments, such that in each segment there are no two numbers (on different positions), whose product is a perfect square. Moreover, it is allowed to do at most k such operations before the division: choose a number in the array and change its value to any positive integer. But in this version k = 0, so it is not important. What is the minimum number of continuous segments you should use if you will make changes optimally? Input The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases. The first line of each test case contains two integers n, k (1 ≤ n ≤ 2 ⋅ 10^5, k = 0). The second line of each test case contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^7). It's guaranteed that the sum of n over all test cases does not exceed 2 ⋅ 10^5. Output For each test case print a single integer — the answer to the problem. Example Input 3 5 0 18 6 2 4 1 5 0 6 8 1 24 8 1 0 1 Output 3 2 1 Note In the first test case the division may be as follows: * [18, 6] * [2, 4] * [1]
import sys input = sys.stdin.readline def prime(num): a = [False, False] + [True] * num p = [] for i in range(2, num + 1): if a[i]: p.append(i) for j in range(2 * i, num + 1, i): a[j] = False return p def solve(n, k): ans = 1 p = prime(3200) square_list = [(i**2) for i in p] square_list = square_list[::-1] for i in range(n): for j in square_list: if arr[i] % j == 0: arr[i] //= j s = set() for i in arr: if i not in s: s.add(i) else: ans += 1 s = set([i]) return ans t = int(input()) for i in range(t): n, k = map(int, input().split()) arr = list(map(int, input().split())) print(solve(n, k))
IMPORT ASSIGN VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR VAR IF BIN_OP VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
This is the easy version of the problem. The only difference is that in this version k = 0. There is an array a_1, a_2, …, a_n of n positive integers. You should divide it into a minimal number of continuous segments, such that in each segment there are no two numbers (on different positions), whose product is a perfect square. Moreover, it is allowed to do at most k such operations before the division: choose a number in the array and change its value to any positive integer. But in this version k = 0, so it is not important. What is the minimum number of continuous segments you should use if you will make changes optimally? Input The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases. The first line of each test case contains two integers n, k (1 ≤ n ≤ 2 ⋅ 10^5, k = 0). The second line of each test case contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^7). It's guaranteed that the sum of n over all test cases does not exceed 2 ⋅ 10^5. Output For each test case print a single integer — the answer to the problem. Example Input 3 5 0 18 6 2 4 1 5 0 6 8 1 24 8 1 0 1 Output 3 2 1 Note In the first test case the division may be as follows: * [18, 6] * [2, 4] * [1]
T = int(input()) r = 1 prime = [2] for i in range(3, 4 * 10**3, 2): flag = False if i % 2 == 0: continue for j in range(3, int(i**0.5) + 1, 2): if i % j == 0: flag = True break if not flag: prime.append(i) def primefactor(num): index = 0 output = [] while num >= prime[index] ** 2: times = 0 while num % prime[index] == 0: num = num // prime[index] times += 1 if times & 1: output.append(prime[index]) index += 1 if num > 1: output.append(num) return tuple(output) while r <= T: n, k = map(int, input().split()) arr = list(map(int, input().split())) ans = 1 fact = {} for i in range(n): num = arr[i] factnum = primefactor(num) if factnum in fact: fact = {factnum: 1} ans += 1 else: fact[factnum] = 1 print(ans) r += 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE VAR BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR DICT VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
This is the easy version of the problem. The only difference is that in this version k = 0. There is an array a_1, a_2, …, a_n of n positive integers. You should divide it into a minimal number of continuous segments, such that in each segment there are no two numbers (on different positions), whose product is a perfect square. Moreover, it is allowed to do at most k such operations before the division: choose a number in the array and change its value to any positive integer. But in this version k = 0, so it is not important. What is the minimum number of continuous segments you should use if you will make changes optimally? Input The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases. The first line of each test case contains two integers n, k (1 ≤ n ≤ 2 ⋅ 10^5, k = 0). The second line of each test case contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^7). It's guaranteed that the sum of n over all test cases does not exceed 2 ⋅ 10^5. Output For each test case print a single integer — the answer to the problem. Example Input 3 5 0 18 6 2 4 1 5 0 6 8 1 24 8 1 0 1 Output 3 2 1 Note In the first test case the division may be as follows: * [18, 6] * [2, 4] * [1]
import sys input = sys.stdin.readline p = [2, 3] i = 5 while i * i < 10000000: pr = 1 for x in p: if i % x == 0: pr = 0 break if pr: p += [i] i += 2 for f in range(int(input())): n, k = map(int, input().split()) a = list(map(int, input().split())) s = set() ans = 1 for x in a: prs = [] for pr in p: nb = 0 while x % pr == 0: x //= pr nb += 1 if nb % 2: prs += [pr] if x > 1: prs += [x] prs = tuple(prs) if prs in s: ans += 1 s = set() s.add(prs) print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR LIST VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR LIST VAR IF VAR NUMBER VAR LIST VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
This is the easy version of the problem. The only difference is that in this version k = 0. There is an array a_1, a_2, …, a_n of n positive integers. You should divide it into a minimal number of continuous segments, such that in each segment there are no two numbers (on different positions), whose product is a perfect square. Moreover, it is allowed to do at most k such operations before the division: choose a number in the array and change its value to any positive integer. But in this version k = 0, so it is not important. What is the minimum number of continuous segments you should use if you will make changes optimally? Input The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases. The first line of each test case contains two integers n, k (1 ≤ n ≤ 2 ⋅ 10^5, k = 0). The second line of each test case contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^7). It's guaranteed that the sum of n over all test cases does not exceed 2 ⋅ 10^5. Output For each test case print a single integer — the answer to the problem. Example Input 3 5 0 18 6 2 4 1 5 0 6 8 1 24 8 1 0 1 Output 3 2 1 Note In the first test case the division may be as follows: * [18, 6] * [2, 4] * [1]
d = {} mask = {} for _ in range(int(input())): n, k = list(map(int, input().split())) A = list(map(int, input().split())) for ele in A: if ele in d: continue c = {} x = ele for i in range(2, int(ele**0.5) + 2): if ele % i == 0: c[i] = 0 while ele % i == 0: ele //= i c[i] += 1 c[i] %= 2 if ele != 1: c[ele] = 1 d[x] = c B = [] for ele in A: cur = 1 if ele not in mask: for val in d[ele]: cur *= val ** d[ele][val] mask[ele] = cur B.append(mask[ele]) s = set() cnt = 1 for ele in B: if ele in s: cnt += 1 s = set() s.add(ele) print(cnt)
ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR IF VAR VAR ASSIGN VAR DICT ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR NUMBER IF VAR VAR FOR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
This is the easy version of the problem. The only difference is that in this version k = 0. There is an array a_1, a_2, …, a_n of n positive integers. You should divide it into a minimal number of continuous segments, such that in each segment there are no two numbers (on different positions), whose product is a perfect square. Moreover, it is allowed to do at most k such operations before the division: choose a number in the array and change its value to any positive integer. But in this version k = 0, so it is not important. What is the minimum number of continuous segments you should use if you will make changes optimally? Input The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases. The first line of each test case contains two integers n, k (1 ≤ n ≤ 2 ⋅ 10^5, k = 0). The second line of each test case contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^7). It's guaranteed that the sum of n over all test cases does not exceed 2 ⋅ 10^5. Output For each test case print a single integer — the answer to the problem. Example Input 3 5 0 18 6 2 4 1 5 0 6 8 1 24 8 1 0 1 Output 3 2 1 Note In the first test case the division may be as follows: * [18, 6] * [2, 4] * [1]
def one_mnogetel(a): for i in range(2, int(a**0.5) + 1): if a % i == 0: return i return a def F(a): ans = 1 while one_mnogetel(a) != a: g = one_mnogetel(a) if ans % g == 0: ans //= g else: ans *= g a = a // g if ans % a == 0: ans //= a else: ans *= a return ans for _ in range(int(input())): n, m = map(int, input().split()) A = list(map(int, input().split())) B = [] for i in range(len(A)): B.append((F(int(A[i])), i)) A[i] = A[i], -10 B.sort() for i in range(1, len(B)): if B[i][0] == B[i - 1][0]: A[B[i][1]] = A[B[i][1]][0], B[i - 1][1] Ans = 0 last = -1 for i in range(1, len(A)): if last < A[i][1]: Ans += 1 last = i - 1 print(Ans + 1)
FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER RETURN VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
This is the easy version of the problem. The only difference is that in this version k = 0. There is an array a_1, a_2, …, a_n of n positive integers. You should divide it into a minimal number of continuous segments, such that in each segment there are no two numbers (on different positions), whose product is a perfect square. Moreover, it is allowed to do at most k such operations before the division: choose a number in the array and change its value to any positive integer. But in this version k = 0, so it is not important. What is the minimum number of continuous segments you should use if you will make changes optimally? Input The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases. The first line of each test case contains two integers n, k (1 ≤ n ≤ 2 ⋅ 10^5, k = 0). The second line of each test case contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^7). It's guaranteed that the sum of n over all test cases does not exceed 2 ⋅ 10^5. Output For each test case print a single integer — the answer to the problem. Example Input 3 5 0 18 6 2 4 1 5 0 6 8 1 24 8 1 0 1 Output 3 2 1 Note In the first test case the division may be as follows: * [18, 6] * [2, 4] * [1]
temp = [True] * 3163 prime = [] for i in range(2, 3163): if not temp[i]: continue prime.append(i) k = 2 while k * i < 3163: temp[k * i] = False k += 1 prime = list(map(lambda x: x**2, prime)) t = int(input()) inp = lambda: map(int, input().split()) squares = [] for _ in range(t): n, k = inp() a = list(inp()) for i in range(n): temp = a[i] for j in prime: if j > temp: break if temp % j == 0: temp = temp // j a[i] = temp nums = {} for i in a: nums[i] = -1 num = [] for i in range(n): num.append(nums[a[i]]) nums[a[i]] = i p = 0 ans = 1 for i in range(n): if num[i] >= p: ans += 1 p = i print(ans)
ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR IF VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
This is the easy version of the problem. The only difference is that in this version k = 0. There is an array a_1, a_2, …, a_n of n positive integers. You should divide it into a minimal number of continuous segments, such that in each segment there are no two numbers (on different positions), whose product is a perfect square. Moreover, it is allowed to do at most k such operations before the division: choose a number in the array and change its value to any positive integer. But in this version k = 0, so it is not important. What is the minimum number of continuous segments you should use if you will make changes optimally? Input The first line contains a single integer t (1 ≤ t ≤ 1000) — the number of test cases. The first line of each test case contains two integers n, k (1 ≤ n ≤ 2 ⋅ 10^5, k = 0). The second line of each test case contains n integers a_1, a_2, …, a_n (1 ≤ a_i ≤ 10^7). It's guaranteed that the sum of n over all test cases does not exceed 2 ⋅ 10^5. Output For each test case print a single integer — the answer to the problem. Example Input 3 5 0 18 6 2 4 1 5 0 6 8 1 24 8 1 0 1 Output 3 2 1 Note In the first test case the division may be as follows: * [18, 6] * [2, 4] * [1]
import sys input = sys.stdin.readline max_ = 10**4 tf = [True] * (max_ + 1) tf[0] = False tf[1] = False for i in range(2, int((max_ + 1) ** 0.5 + 1)): if not tf[i]: continue for j in range(i * i, max_ + 1, i): tf[j] = False primes = [] for i in range(2, max_ + 1): if tf[i]: primes.append(i) def main(): n, k = map(int, input().split()) alst = list(map(int, input().split())) ans = 1 se = set() for a in alst: for p in primes: pp = p * p if pp > a: break while a % pp == 0: a //= pp if a in se: ans += 1 se = set() se.add(a) print(ans) for _ in range(int(input())): main()
IMPORT ASSIGN VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR WHILE BIN_OP VAR VAR NUMBER VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR
After Fox Ciel got off a bus, she found that the bus she was on was a wrong bus and she lost her way in a strange town. However, she fortunately met her friend Beaver Taro and asked which way to go to her castle. Taro's response to her was a string s, and she tried to remember the string s correctly. However, Ciel feels n strings b1, b2, ... , bn are really boring, and unfortunately she dislikes to remember a string that contains a boring substring. To make the thing worse, what she can remember is only the contiguous substring of s. Determine the longest contiguous substring of s that does not contain any boring string, so that she can remember the longest part of Taro's response. Input In the first line there is a string s. The length of s will be between 1 and 105, inclusive. In the second line there is a single integer n (1 ≤ n ≤ 10). Next n lines, there is a string bi (1 ≤ i ≤ n). Each length of bi will be between 1 and 10, inclusive. Each character of the given strings will be either a English alphabet (both lowercase and uppercase) or a underscore ('_') or a digit. Assume that these strings are case-sensitive. Output Output in the first line two space-separated integers len and pos: the length of the longest contiguous substring of s that does not contain any bi, and the first position of the substring (0-indexed). The position pos must be between 0 and |s| - len inclusive, where |s| is the length of string s. If there are several solutions, output any. Examples Input Go_straight_along_this_street 5 str long tree biginteger ellipse Output 12 4 Input IhaveNoIdea 9 I h a v e N o I d Output 0 0 Input unagioisii 2 ioi unagi Output 5 5 Note In the first sample, the solution is traight_alon. In the second sample, the solution is an empty string, so the output can be «0 0», «0 1», «0 2», and so on. In the third sample, the solution is either nagio or oisii.
chaine = input() n = int(input()) all_borings = sorted([input() for _ in range(n)], key=lambda s: len(s)) chaine_uniforme = True first = chaine[0] for c in chaine: if c != first: chaine_uniforme = False break if chaine_uniforme: plus_court = len(chaine) for bi in all_borings: uniforme = True for c in bi: if c != first: uniforme = False break if uniforme: plus_court = len(bi) - 1 break print(plus_court, 0) exit() borings = [] for s in all_borings: li = len(s) - 1 for s2, l2 in borings: pos = s.find(s2) if pos != -1: li = min(li, pos + l2) borings.append((s, li)) def allOcc(chaine, s, li, occ): n = 0 prev = -1 while n != len(occ): n = len(occ) pos = chaine.find(s, prev + 1) if pos != -1: occ.append((pos, li)) prev = pos return occ prev_pos = -2 def f(pos): global prev_pos if pos[0] != prev_pos: prev_pos = pos[0] return True return False all_occ = [(len(chaine) - 1, 1)] [allOcc(chaine, bi, li, all_occ) for bi, li in borings] all_occ.sort() all_pos = [occ for occ in all_occ if f(occ)] prev = -1 best = 0 best_pos = 0 for pos, li in all_pos: n_len = pos + li - prev - 1 if n_len > best: best = n_len best_pos = prev + 1 prev = pos print(best, best_pos)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR NUMBER VAR ASSIGN VAR VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR LIST BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR
After Fox Ciel got off a bus, she found that the bus she was on was a wrong bus and she lost her way in a strange town. However, she fortunately met her friend Beaver Taro and asked which way to go to her castle. Taro's response to her was a string s, and she tried to remember the string s correctly. However, Ciel feels n strings b1, b2, ... , bn are really boring, and unfortunately she dislikes to remember a string that contains a boring substring. To make the thing worse, what she can remember is only the contiguous substring of s. Determine the longest contiguous substring of s that does not contain any boring string, so that she can remember the longest part of Taro's response. Input In the first line there is a string s. The length of s will be between 1 and 105, inclusive. In the second line there is a single integer n (1 ≤ n ≤ 10). Next n lines, there is a string bi (1 ≤ i ≤ n). Each length of bi will be between 1 and 10, inclusive. Each character of the given strings will be either a English alphabet (both lowercase and uppercase) or a underscore ('_') or a digit. Assume that these strings are case-sensitive. Output Output in the first line two space-separated integers len and pos: the length of the longest contiguous substring of s that does not contain any bi, and the first position of the substring (0-indexed). The position pos must be between 0 and |s| - len inclusive, where |s| is the length of string s. If there are several solutions, output any. Examples Input Go_straight_along_this_street 5 str long tree biginteger ellipse Output 12 4 Input IhaveNoIdea 9 I h a v e N o I d Output 0 0 Input unagioisii 2 ioi unagi Output 5 5 Note In the first sample, the solution is traight_alon. In the second sample, the solution is an empty string, so the output can be «0 0», «0 1», «0 2», and so on. In the third sample, the solution is either nagio or oisii.
S = str(input()) n = int(input()) b = str(input()) M = [b] m = 11 for i in range(n - 1): f = 0 b = str(input()) for i in range(len(M)): if len(M[i]) >= len(b): M = M[:i] + [b] + M[i:] f = 1 break if f == 0: M += [b] f = 0 L = [] for i in M: f = 0 for j in M: if j != i and j in i: f = 1 if f == 0: L += [i] f = 0 k = 0 p = 0 v = 0 s = 0 for i in range(len(S)): if k == 0: s = i for j in L: if S[i : i + len(j)] == j: k += len(j) - 1 if k > p: f = 1 p = k v = s k = -1 break k += 1 if f == 0 or k > p: p, v = k, s print(p, v)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR LIST VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR LIST VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR LIST VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR FOR VAR VAR IF VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
After Fox Ciel got off a bus, she found that the bus she was on was a wrong bus and she lost her way in a strange town. However, she fortunately met her friend Beaver Taro and asked which way to go to her castle. Taro's response to her was a string s, and she tried to remember the string s correctly. However, Ciel feels n strings b1, b2, ... , bn are really boring, and unfortunately she dislikes to remember a string that contains a boring substring. To make the thing worse, what she can remember is only the contiguous substring of s. Determine the longest contiguous substring of s that does not contain any boring string, so that she can remember the longest part of Taro's response. Input In the first line there is a string s. The length of s will be between 1 and 105, inclusive. In the second line there is a single integer n (1 ≤ n ≤ 10). Next n lines, there is a string bi (1 ≤ i ≤ n). Each length of bi will be between 1 and 10, inclusive. Each character of the given strings will be either a English alphabet (both lowercase and uppercase) or a underscore ('_') or a digit. Assume that these strings are case-sensitive. Output Output in the first line two space-separated integers len and pos: the length of the longest contiguous substring of s that does not contain any bi, and the first position of the substring (0-indexed). The position pos must be between 0 and |s| - len inclusive, where |s| is the length of string s. If there are several solutions, output any. Examples Input Go_straight_along_this_street 5 str long tree biginteger ellipse Output 12 4 Input IhaveNoIdea 9 I h a v e N o I d Output 0 0 Input unagioisii 2 ioi unagi Output 5 5 Note In the first sample, the solution is traight_alon. In the second sample, the solution is an empty string, so the output can be «0 0», «0 1», «0 2», and so on. In the third sample, the solution is either nagio or oisii.
s, n = input(), int(input()) t = [input() for i in range(n)] def f(i): global t for j in range(n): if i < j: if len(t[j]) < len(t[i]) and t[j] in t[i]: return False elif j < i and t[j] in t[i]: return False return True t = [t[i] for i in range(n) if f(i)] n = len(s) r = [0] * n for p in t: i, m = -1, len(p) - 1 while True: i = s.find(p, i + 1) if i == -1: break r[i] += 1 r[i + m] += 2 d, j, q = 0, -1, [-1] for i in range(n): if r[i] & 1: q.append(i) if r[i] & 2: j = q.pop(0) if i - j > d: d, k = i - j, j j = q.pop(0) if n - j > d: d, k = n - j, j print(d - 1, k + 1)
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER LIST NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER
After Fox Ciel got off a bus, she found that the bus she was on was a wrong bus and she lost her way in a strange town. However, she fortunately met her friend Beaver Taro and asked which way to go to her castle. Taro's response to her was a string s, and she tried to remember the string s correctly. However, Ciel feels n strings b1, b2, ... , bn are really boring, and unfortunately she dislikes to remember a string that contains a boring substring. To make the thing worse, what she can remember is only the contiguous substring of s. Determine the longest contiguous substring of s that does not contain any boring string, so that she can remember the longest part of Taro's response. Input In the first line there is a string s. The length of s will be between 1 and 105, inclusive. In the second line there is a single integer n (1 ≤ n ≤ 10). Next n lines, there is a string bi (1 ≤ i ≤ n). Each length of bi will be between 1 and 10, inclusive. Each character of the given strings will be either a English alphabet (both lowercase and uppercase) or a underscore ('_') or a digit. Assume that these strings are case-sensitive. Output Output in the first line two space-separated integers len and pos: the length of the longest contiguous substring of s that does not contain any bi, and the first position of the substring (0-indexed). The position pos must be between 0 and |s| - len inclusive, where |s| is the length of string s. If there are several solutions, output any. Examples Input Go_straight_along_this_street 5 str long tree biginteger ellipse Output 12 4 Input IhaveNoIdea 9 I h a v e N o I d Output 0 0 Input unagioisii 2 ioi unagi Output 5 5 Note In the first sample, the solution is traight_alon. In the second sample, the solution is an empty string, so the output can be «0 0», «0 1», «0 2», and so on. In the third sample, the solution is either nagio or oisii.
R = lambda: map(int, input().split()) s = input() n = int(input()) words = [input() for i in range(n)] dp = [-1] * len(s) for w in words: si = 0 while si != -1: si = s.find(w, si) if si >= 0: dp[si + len(w) - 1] = max(si + 1, dp[si + len(w) - 1]) si += 1 fl = 0 fr = 0 l = 0 found = False for r in range(len(s)): if dp[r] >= 0: l = max(l, dp[r]) if r - l >= fr - fl: found = True fl, fr = l, r print(fr - fl + found, fl)
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR
After Fox Ciel got off a bus, she found that the bus she was on was a wrong bus and she lost her way in a strange town. However, she fortunately met her friend Beaver Taro and asked which way to go to her castle. Taro's response to her was a string s, and she tried to remember the string s correctly. However, Ciel feels n strings b1, b2, ... , bn are really boring, and unfortunately she dislikes to remember a string that contains a boring substring. To make the thing worse, what she can remember is only the contiguous substring of s. Determine the longest contiguous substring of s that does not contain any boring string, so that she can remember the longest part of Taro's response. Input In the first line there is a string s. The length of s will be between 1 and 105, inclusive. In the second line there is a single integer n (1 ≤ n ≤ 10). Next n lines, there is a string bi (1 ≤ i ≤ n). Each length of bi will be between 1 and 10, inclusive. Each character of the given strings will be either a English alphabet (both lowercase and uppercase) or a underscore ('_') or a digit. Assume that these strings are case-sensitive. Output Output in the first line two space-separated integers len and pos: the length of the longest contiguous substring of s that does not contain any bi, and the first position of the substring (0-indexed). The position pos must be between 0 and |s| - len inclusive, where |s| is the length of string s. If there are several solutions, output any. Examples Input Go_straight_along_this_street 5 str long tree biginteger ellipse Output 12 4 Input IhaveNoIdea 9 I h a v e N o I d Output 0 0 Input unagioisii 2 ioi unagi Output 5 5 Note In the first sample, the solution is traight_alon. In the second sample, the solution is an empty string, so the output can be «0 0», «0 1», «0 2», and so on. In the third sample, the solution is either nagio or oisii.
def word_present(words, text, s): for word in words: if word == text[s : s + len(word)]: return word def smallest_ends(words, text): words = list(sorted(words, key=len)) d = [float("inf")] * len(text) for i in range(0, len(text)): word = word_present(words, text, i) if word: d[i] = min(len(word), d[i]) return d text = input() n = int(input()) words = [input() for _ in range(n)] ends = smallest_ends(words, text) longuest_starting = [0] * (len(text) + 1) for i in range(len(text) - 1, -1, -1): longuest_starting[i] = min(1 + longuest_starting[i + 1], ends[i] - 1) i_max = max(range(len(text), -1, -1), key=lambda i: longuest_starting[i]) print(longuest_starting[i_max], i_max if longuest_starting[i_max] else 0)
FUNC_DEF FOR VAR VAR IF VAR VAR VAR BIN_OP VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER
After Fox Ciel got off a bus, she found that the bus she was on was a wrong bus and she lost her way in a strange town. However, she fortunately met her friend Beaver Taro and asked which way to go to her castle. Taro's response to her was a string s, and she tried to remember the string s correctly. However, Ciel feels n strings b1, b2, ... , bn are really boring, and unfortunately she dislikes to remember a string that contains a boring substring. To make the thing worse, what she can remember is only the contiguous substring of s. Determine the longest contiguous substring of s that does not contain any boring string, so that she can remember the longest part of Taro's response. Input In the first line there is a string s. The length of s will be between 1 and 105, inclusive. In the second line there is a single integer n (1 ≤ n ≤ 10). Next n lines, there is a string bi (1 ≤ i ≤ n). Each length of bi will be between 1 and 10, inclusive. Each character of the given strings will be either a English alphabet (both lowercase and uppercase) or a underscore ('_') or a digit. Assume that these strings are case-sensitive. Output Output in the first line two space-separated integers len and pos: the length of the longest contiguous substring of s that does not contain any bi, and the first position of the substring (0-indexed). The position pos must be between 0 and |s| - len inclusive, where |s| is the length of string s. If there are several solutions, output any. Examples Input Go_straight_along_this_street 5 str long tree biginteger ellipse Output 12 4 Input IhaveNoIdea 9 I h a v e N o I d Output 0 0 Input unagioisii 2 ioi unagi Output 5 5 Note In the first sample, the solution is traight_alon. In the second sample, the solution is an empty string, so the output can be «0 0», «0 1», «0 2», and so on. In the third sample, the solution is either nagio or oisii.
s = input() n = int(input()) b = [] for i in range(n): b.append(input()) s_original = s s_dict = {s: 1} for bi in b: while True: new_s_dict = {} for s in s_dict.keys(): part_list = s.split(bi) if len(part_list) <= 1: new_s_dict[s] = 1 continue for i, part in enumerate(part_list): if i == 0: new_s_dict[part + bi[:-1]] = 1 if 0 < i < len(part_list) - 1: new_s_dict[bi[1:] + part + bi[:-1]] = 1 if i == len(part_list) - 1: new_s_dict[bi[1:] + part] = 1 if s_dict == new_s_dict: break s_dict = new_s_dict max_length = 0 result = "" for s in s_dict.keys(): if max_length < len(s): max_length = len(s) result = s print(max_length, s_original.index(result))
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR DICT VAR NUMBER FOR VAR VAR WHILE NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER IF NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR
You are playing a game called Slime Escape. The game takes place on a number line. Initially, there are $n$ slimes. For all positive integers $i$ where $1 \le i \le n$, the $i$-th slime is located at position $i$ and has health $a_i$. You are controlling the slime at position $k$. There are two escapes located at positions $0$ and $n+1$. Your goal is to reach any one of the two escapes by performing any number of game moves. In one game move, you move your slime to the left or right by one position. However, if there is another slime in the new position, you must absorb it. When absorbing a slime, the health of your slime would be increased by the health of the absorbed slime, then the absorbed slime would be removed from the game. Note that some slimes might have negative health, so your health would decrease when absorbing such slimes. You lose the game immediately if your slime has negative health at any moment during the game. Can you reach one of two escapes by performing any number of game moves, without ever losing the game? -----Input----- Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 20000$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two positive integers $n$, $k$ ($3 \leq n \leq 200000$, $1 \leq k \leq n$) — the number of slimes and the position of your slime. The second line of each test case contains $n$ integers, $a_1, a_2, \ldots, a_n$ ($-10^9 \leq a_i \leq 10^9$) — the health of the slimes. It is guaranteed that health of your slime is non-negative ($a_k \geq 0$), and all other slimes have non-zero health ($a_i \ne 0$ for $i \ne k$). It is guaranteed that the sum of $n$ over all test cases does not exceed $200000$. -----Output----- For each test case, print "YES" (without quotes) if you can escape without losing, and "NO" (without quotes) otherwise. -----Examples----- Input 6 7 4 -1 -2 -3 6 -2 -3 -1 3 1 232 -500 -700 7 4 -1 -2 -4 6 -2 -4 -1 8 4 -100 10 -7 6 -2 -3 6 -10 8 2 -999 0 -2 3 4 5 6 7 7 3 7 3 3 4 2 1 1 Output YES YES NO YES NO YES -----Note----- In the first test case, you control the slime at position $4$ with health $6$. One way to escape is to absorb the slimes at positions $5$, $6$, and $7$. Your slime escapes with $0$ health at position $8$. In the second test case, you control the slime with $232$ health at position $1$. Since your slime is already located next to the escape at position $0$, you can move to it without absorbing any slime. In the third test case, it can be shown that your slime would always have a negative health before reaching any one of two escapes. In the fourth test case, you control the slime at position $4$ with health $6$. The following describes a possible sequence of moves to win: Absorb the slimes at positions $5$, $6$, and $7$: your health becomes $4$ after absorbing the slime with health $-2$; becomes $1$ after absorbing the slime with health $-3$; and becomes $7$ after absorbing the slime with health $6$. Absorb the slimes at positions $3$, and $2$: your health becomes $7-7+10=10$. Absorb the slime at position $8$: your health becomes $10-10=0$. Use the escape at position $9$. Since your slime has maintained non-negative health at all times, you have won.
import sys input = lambda: sys.stdin.readline().strip() INF = float("inf") for _ in range(int(input())): n, k = map(int, input().split()) a = list(map(int, input().split())) k -= 1 L, R = [], [] sm, mn = 0, INF for i in range(k - 1, -1, -1): sm += a[i] mn = min(mn, sm) if sm >= 0 or i == 0: L.append([sm, mn]) sm, mn = 0, INF for i in range(k + 1, n, +1): sm += a[i] mn = min(mn, sm) if sm >= 0 or i == n - 1: R.append([sm, mn]) sm, mn = 0, INF L.reverse(), R.reverse() h = a[k] while len(L) > 0 and len(R) > 0: if L[-1][1] + h >= 0: h += L[-1][0] L.pop() elif R[-1][1] + h >= 0: h += R[-1][0] R.pop() else: break print("YES" if len(L) == 0 or len(R) == 0 else "NO")
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR LIST LIST ASSIGN VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER STRING STRING
You are playing a game called Slime Escape. The game takes place on a number line. Initially, there are $n$ slimes. For all positive integers $i$ where $1 \le i \le n$, the $i$-th slime is located at position $i$ and has health $a_i$. You are controlling the slime at position $k$. There are two escapes located at positions $0$ and $n+1$. Your goal is to reach any one of the two escapes by performing any number of game moves. In one game move, you move your slime to the left or right by one position. However, if there is another slime in the new position, you must absorb it. When absorbing a slime, the health of your slime would be increased by the health of the absorbed slime, then the absorbed slime would be removed from the game. Note that some slimes might have negative health, so your health would decrease when absorbing such slimes. You lose the game immediately if your slime has negative health at any moment during the game. Can you reach one of two escapes by performing any number of game moves, without ever losing the game? -----Input----- Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 20000$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two positive integers $n$, $k$ ($3 \leq n \leq 200000$, $1 \leq k \leq n$) — the number of slimes and the position of your slime. The second line of each test case contains $n$ integers, $a_1, a_2, \ldots, a_n$ ($-10^9 \leq a_i \leq 10^9$) — the health of the slimes. It is guaranteed that health of your slime is non-negative ($a_k \geq 0$), and all other slimes have non-zero health ($a_i \ne 0$ for $i \ne k$). It is guaranteed that the sum of $n$ over all test cases does not exceed $200000$. -----Output----- For each test case, print "YES" (without quotes) if you can escape without losing, and "NO" (without quotes) otherwise. -----Examples----- Input 6 7 4 -1 -2 -3 6 -2 -3 -1 3 1 232 -500 -700 7 4 -1 -2 -4 6 -2 -4 -1 8 4 -100 10 -7 6 -2 -3 6 -10 8 2 -999 0 -2 3 4 5 6 7 7 3 7 3 3 4 2 1 1 Output YES YES NO YES NO YES -----Note----- In the first test case, you control the slime at position $4$ with health $6$. One way to escape is to absorb the slimes at positions $5$, $6$, and $7$. Your slime escapes with $0$ health at position $8$. In the second test case, you control the slime with $232$ health at position $1$. Since your slime is already located next to the escape at position $0$, you can move to it without absorbing any slime. In the third test case, it can be shown that your slime would always have a negative health before reaching any one of two escapes. In the fourth test case, you control the slime at position $4$ with health $6$. The following describes a possible sequence of moves to win: Absorb the slimes at positions $5$, $6$, and $7$: your health becomes $4$ after absorbing the slime with health $-2$; becomes $1$ after absorbing the slime with health $-3$; and becomes $7$ after absorbing the slime with health $6$. Absorb the slimes at positions $3$, and $2$: your health becomes $7-7+10=10$. Absorb the slime at position $8$: your health becomes $10-10=0$. Use the escape at position $9$. Since your slime has maintained non-negative health at all times, you have won.
for _ in range(int(input())): n, p = map(int, input().split()) a = list(map(int, input().split())) if p == 1 or p == n: print("YES") else: x = a[: p - 1] x = list(reversed(x)) y = a[p:] s = a[p - 1] i = 0 j = 0 nx = len(x) ny = len(y) r = "YES" mx = a[p - 1] xi = -1 yi = -1 while True: mc = 0 s = mx for i in range(xi + 1, nx): s += x[i] if s > mx: mx = s xi = i mc = 1 elif s < 0: break if s >= 0: break else: s = mx for i in range(yi + 1, ny): s += y[i] if s > mx: mx = s yi = i mc = 1 elif s < 0: break if s >= 0: break elif mc == 0: r = "NO" break print(r)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER IF VAR NUMBER IF VAR NUMBER ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR
You are playing a game called Slime Escape. The game takes place on a number line. Initially, there are $n$ slimes. For all positive integers $i$ where $1 \le i \le n$, the $i$-th slime is located at position $i$ and has health $a_i$. You are controlling the slime at position $k$. There are two escapes located at positions $0$ and $n+1$. Your goal is to reach any one of the two escapes by performing any number of game moves. In one game move, you move your slime to the left or right by one position. However, if there is another slime in the new position, you must absorb it. When absorbing a slime, the health of your slime would be increased by the health of the absorbed slime, then the absorbed slime would be removed from the game. Note that some slimes might have negative health, so your health would decrease when absorbing such slimes. You lose the game immediately if your slime has negative health at any moment during the game. Can you reach one of two escapes by performing any number of game moves, without ever losing the game? -----Input----- Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 20000$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two positive integers $n$, $k$ ($3 \leq n \leq 200000$, $1 \leq k \leq n$) — the number of slimes and the position of your slime. The second line of each test case contains $n$ integers, $a_1, a_2, \ldots, a_n$ ($-10^9 \leq a_i \leq 10^9$) — the health of the slimes. It is guaranteed that health of your slime is non-negative ($a_k \geq 0$), and all other slimes have non-zero health ($a_i \ne 0$ for $i \ne k$). It is guaranteed that the sum of $n$ over all test cases does not exceed $200000$. -----Output----- For each test case, print "YES" (without quotes) if you can escape without losing, and "NO" (without quotes) otherwise. -----Examples----- Input 6 7 4 -1 -2 -3 6 -2 -3 -1 3 1 232 -500 -700 7 4 -1 -2 -4 6 -2 -4 -1 8 4 -100 10 -7 6 -2 -3 6 -10 8 2 -999 0 -2 3 4 5 6 7 7 3 7 3 3 4 2 1 1 Output YES YES NO YES NO YES -----Note----- In the first test case, you control the slime at position $4$ with health $6$. One way to escape is to absorb the slimes at positions $5$, $6$, and $7$. Your slime escapes with $0$ health at position $8$. In the second test case, you control the slime with $232$ health at position $1$. Since your slime is already located next to the escape at position $0$, you can move to it without absorbing any slime. In the third test case, it can be shown that your slime would always have a negative health before reaching any one of two escapes. In the fourth test case, you control the slime at position $4$ with health $6$. The following describes a possible sequence of moves to win: Absorb the slimes at positions $5$, $6$, and $7$: your health becomes $4$ after absorbing the slime with health $-2$; becomes $1$ after absorbing the slime with health $-3$; and becomes $7$ after absorbing the slime with health $6$. Absorb the slimes at positions $3$, and $2$: your health becomes $7-7+10=10$. Absorb the slime at position $8$: your health becomes $10-10=0$. Use the escape at position $9$. Since your slime has maintained non-negative health at all times, you have won.
t = int(input()) for q in range(t): n, k = map(int, input().split()) a = list(map(int, input().split())) k -= 1 m1 = a[k] m2 = a[k] s = a[k] f1 = True f2 = True dk1 = k sd1 = s dk2 = k sd2 = s DL1 = dk1 DL2 = dk2 DL11 = 0 DL22 = 0 while f1 and f2: while sd1 + DL22 >= 0 and dk1 < n - 1: dk1 += 1 sd1 += a[dk1] if sd1 > m1: m1 = sd1 DL11 = m1 - s if dk1 == n - 1 and sd1 + DL22 >= 0: f2 = False sd1 -= a[dk1] dk1 -= 1 while sd2 + DL11 >= 0 and dk2 > 0: dk2 -= 1 sd2 += a[dk2] if sd2 > m2: m2 = sd2 DL22 = m2 - s if dk2 == 0 and sd2 + DL11 >= 0: f2 = False sd2 -= a[dk2] dk2 += 1 if DL2 == dk2 and DL1 == dk1: f1 = False DL1 = dk1 DL2 = dk2 if not f2: print("YES") else: print("NO")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR WHILE BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
You are playing a game called Slime Escape. The game takes place on a number line. Initially, there are $n$ slimes. For all positive integers $i$ where $1 \le i \le n$, the $i$-th slime is located at position $i$ and has health $a_i$. You are controlling the slime at position $k$. There are two escapes located at positions $0$ and $n+1$. Your goal is to reach any one of the two escapes by performing any number of game moves. In one game move, you move your slime to the left or right by one position. However, if there is another slime in the new position, you must absorb it. When absorbing a slime, the health of your slime would be increased by the health of the absorbed slime, then the absorbed slime would be removed from the game. Note that some slimes might have negative health, so your health would decrease when absorbing such slimes. You lose the game immediately if your slime has negative health at any moment during the game. Can you reach one of two escapes by performing any number of game moves, without ever losing the game? -----Input----- Each test contains multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 20000$) — the number of test cases. Description of the test cases follows. The first line of each test case contains two positive integers $n$, $k$ ($3 \leq n \leq 200000$, $1 \leq k \leq n$) — the number of slimes and the position of your slime. The second line of each test case contains $n$ integers, $a_1, a_2, \ldots, a_n$ ($-10^9 \leq a_i \leq 10^9$) — the health of the slimes. It is guaranteed that health of your slime is non-negative ($a_k \geq 0$), and all other slimes have non-zero health ($a_i \ne 0$ for $i \ne k$). It is guaranteed that the sum of $n$ over all test cases does not exceed $200000$. -----Output----- For each test case, print "YES" (without quotes) if you can escape without losing, and "NO" (without quotes) otherwise. -----Examples----- Input 6 7 4 -1 -2 -3 6 -2 -3 -1 3 1 232 -500 -700 7 4 -1 -2 -4 6 -2 -4 -1 8 4 -100 10 -7 6 -2 -3 6 -10 8 2 -999 0 -2 3 4 5 6 7 7 3 7 3 3 4 2 1 1 Output YES YES NO YES NO YES -----Note----- In the first test case, you control the slime at position $4$ with health $6$. One way to escape is to absorb the slimes at positions $5$, $6$, and $7$. Your slime escapes with $0$ health at position $8$. In the second test case, you control the slime with $232$ health at position $1$. Since your slime is already located next to the escape at position $0$, you can move to it without absorbing any slime. In the third test case, it can be shown that your slime would always have a negative health before reaching any one of two escapes. In the fourth test case, you control the slime at position $4$ with health $6$. The following describes a possible sequence of moves to win: Absorb the slimes at positions $5$, $6$, and $7$: your health becomes $4$ after absorbing the slime with health $-2$; becomes $1$ after absorbing the slime with health $-3$; and becomes $7$ after absorbing the slime with health $6$. Absorb the slimes at positions $3$, and $2$: your health becomes $7-7+10=10$. Absorb the slime at position $8$: your health becomes $10-10=0$. Use the escape at position $9$. Since your slime has maintained non-negative health at all times, you have won.
for _ in range(int(input())): n, k = map(int, input().split()) a = list(map(int, input().split())) r = k k -= 1 l = k - 1 x = y = 0 mr = ml = 0 while l >= 0 and r < n: if r < n and a[k] + a[r] + y + ml >= 0: y += a[r] mr = max(y, mr) r += 1 elif l >= 0 and a[k] + a[l] + x + mr >= 0: x += a[l] ml = max(x, ml) l -= 1 else: break if l == -1 or r == n: print("YES") else: print("NO")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR NUMBER VAR VAR IF VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING
$n$ players are playing a game. There are two different maps in the game. For each player, we know his strength on each map. When two players fight on a specific map, the player with higher strength on that map always wins. No two players have the same strength on the same map. You are the game master and want to organize a tournament. There will be a total of $n-1$ battles. While there is more than one player in the tournament, choose any map and any two remaining players to fight on it. The player who loses will be eliminated from the tournament. In the end, exactly one player will remain, and he is declared the winner of the tournament. For each player determine if he can win the tournament. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The description of test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of players. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$, $a_i \neq a_j$ for $i \neq j$), where $a_i$ is the strength of the $i$-th player on the first map. The third line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \leq b_i \leq 10^9$, $b_i \neq b_j$ for $i \neq j$), where $b_i$ is the strength of the $i$-th player on the second map. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print a string of length $n$. $i$-th character should be "1" if the $i$-th player can win the tournament, or "0" otherwise. -----Examples----- Input 3 4 1 2 3 4 1 2 3 4 4 11 12 20 21 44 22 11 30 1 1000000000 1000000000 Output 0001 1111 1 -----Note----- In the first test case, the $4$-th player will beat any other player on any game, so he will definitely win the tournament. In the second test case, everyone can be a winner. In the third test case, there is only one player. Clearly, he will win the tournament.
t = int(input()) out = "" for _ in range(t): n = int(input()) a = [[int(x)] for x in input().split()] b = [[int(x)] for x in input().split()] order = [[a[i][0], b[i][0]] for i in range(n)] for i in range(n): a[i].append(b[i][0]) b[i].append(a[i][0]) a.sort() b.sort() i = n - 1 j = n - 1 min1 = a[i][0] min2 = b[i][0] while i >= 0 or j >= 0: if i >= 0 and a[i][0] >= min1: min2 = min(min2, a[i][1]) i -= 1 elif j >= 0 and b[j][0] >= min2: min1 = min(min1, b[j][1]) j -= 1 else: break for g1, g2 in order: out += str(int(g1 >= min1 or g2 >= min2)) out += "\n" print(out)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR VAR NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER FOR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR
$n$ players are playing a game. There are two different maps in the game. For each player, we know his strength on each map. When two players fight on a specific map, the player with higher strength on that map always wins. No two players have the same strength on the same map. You are the game master and want to organize a tournament. There will be a total of $n-1$ battles. While there is more than one player in the tournament, choose any map and any two remaining players to fight on it. The player who loses will be eliminated from the tournament. In the end, exactly one player will remain, and he is declared the winner of the tournament. For each player determine if he can win the tournament. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The description of test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of players. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$, $a_i \neq a_j$ for $i \neq j$), where $a_i$ is the strength of the $i$-th player on the first map. The third line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \leq b_i \leq 10^9$, $b_i \neq b_j$ for $i \neq j$), where $b_i$ is the strength of the $i$-th player on the second map. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print a string of length $n$. $i$-th character should be "1" if the $i$-th player can win the tournament, or "0" otherwise. -----Examples----- Input 3 4 1 2 3 4 1 2 3 4 4 11 12 20 21 44 22 11 30 1 1000000000 1000000000 Output 0001 1111 1 -----Note----- In the first test case, the $4$-th player will beat any other player on any game, so he will definitely win the tournament. In the second test case, everyone can be a winner. In the third test case, there is only one player. Clearly, he will win the tournament.
for _ in range(int(input())): n = int(input()) x = [(int(i[1]), i[0]) for i in enumerate(input().split())] y = [(int(i[1]), i[0]) for i in enumerate(input().split())] x = sorted(x, reverse=True) y = sorted(y, reverse=True) q = set() for i in range(n): q.add(x[i][1]) q.add(y[i][1]) if len(q) == i + 1: break ans = ["0"] * n for i in q: ans[i] = "1" print("".join(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 NUMBER VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST STRING VAR FOR VAR VAR ASSIGN VAR VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
$n$ players are playing a game. There are two different maps in the game. For each player, we know his strength on each map. When two players fight on a specific map, the player with higher strength on that map always wins. No two players have the same strength on the same map. You are the game master and want to organize a tournament. There will be a total of $n-1$ battles. While there is more than one player in the tournament, choose any map and any two remaining players to fight on it. The player who loses will be eliminated from the tournament. In the end, exactly one player will remain, and he is declared the winner of the tournament. For each player determine if he can win the tournament. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The description of test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of players. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$, $a_i \neq a_j$ for $i \neq j$), where $a_i$ is the strength of the $i$-th player on the first map. The third line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \leq b_i \leq 10^9$, $b_i \neq b_j$ for $i \neq j$), where $b_i$ is the strength of the $i$-th player on the second map. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print a string of length $n$. $i$-th character should be "1" if the $i$-th player can win the tournament, or "0" otherwise. -----Examples----- Input 3 4 1 2 3 4 1 2 3 4 4 11 12 20 21 44 22 11 30 1 1000000000 1000000000 Output 0001 1111 1 -----Note----- In the first test case, the $4$-th player will beat any other player on any game, so he will definitely win the tournament. In the second test case, everyone can be a winner. In the third test case, there is only one player. Clearly, he will win the tournament.
def main(): t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) if n == 1: print(1) continue for i in range(n): a[i] = a[i], i a.sort() ans = ["0"] * n miw = b[a[-1][1]] ma = [0] * n ma[0] = b[a[0][1]] for i in range(1, n): ma[i] = max(ma[i - 1], b[a[i][1]]) for i in range(n - 1, -1, -1): if ma[i] >= miw: ans[a[i][1]] = "1" miw = min(miw, b[a[i][1]]) print("".join(ans)) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR
$n$ players are playing a game. There are two different maps in the game. For each player, we know his strength on each map. When two players fight on a specific map, the player with higher strength on that map always wins. No two players have the same strength on the same map. You are the game master and want to organize a tournament. There will be a total of $n-1$ battles. While there is more than one player in the tournament, choose any map and any two remaining players to fight on it. The player who loses will be eliminated from the tournament. In the end, exactly one player will remain, and he is declared the winner of the tournament. For each player determine if he can win the tournament. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The description of test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of players. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$, $a_i \neq a_j$ for $i \neq j$), where $a_i$ is the strength of the $i$-th player on the first map. The third line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \leq b_i \leq 10^9$, $b_i \neq b_j$ for $i \neq j$), where $b_i$ is the strength of the $i$-th player on the second map. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print a string of length $n$. $i$-th character should be "1" if the $i$-th player can win the tournament, or "0" otherwise. -----Examples----- Input 3 4 1 2 3 4 1 2 3 4 4 11 12 20 21 44 22 11 30 1 1000000000 1000000000 Output 0001 1111 1 -----Note----- In the first test case, the $4$-th player will beat any other player on any game, so he will definitely win the tournament. In the second test case, everyone can be a winner. In the third test case, there is only one player. Clearly, he will win the tournament.
import sys input = sys.stdin.readline for _ in " " * int(input()): n = int(input()) L1 = list(map(int, input().split())) L2 = list(map(int, input().split())) Lpair = [] for i in range(n): Lpair.append([L1[i], L2[i]]) Lpair.sort() d1 = dict() mi = 10**9 + 1 for i in range(n - 1, -1, -1): mi = min(mi, Lpair[i][1]) d1[Lpair[i][0]] = mi for i in range(n): Lpair[i][0], Lpair[i][1] = Lpair[i][1], Lpair[i][0] Lpair.sort() d2 = dict() mi = 10**9 + 1 for i in range(n - 1, -1, -1): mi = min(mi, Lpair[i][1]) d2[Lpair[i][0]] = mi amin = Lpair[-1][1] bmin = 10**9 + 1 while True: bperm = min(d1[amin], bmin) aperm = min(d2[bperm], amin) if (aperm, bperm) == (amin, bmin): break amin, bmin = aperm, bperm for i in range(n): if L1[i] >= amin or L2[i] >= bmin: print("1", end="") else: print("0", end="") print()
IMPORT ASSIGN VAR VAR FOR VAR BIN_OP STRING FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER WHILE NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR
$n$ players are playing a game. There are two different maps in the game. For each player, we know his strength on each map. When two players fight on a specific map, the player with higher strength on that map always wins. No two players have the same strength on the same map. You are the game master and want to organize a tournament. There will be a total of $n-1$ battles. While there is more than one player in the tournament, choose any map and any two remaining players to fight on it. The player who loses will be eliminated from the tournament. In the end, exactly one player will remain, and he is declared the winner of the tournament. For each player determine if he can win the tournament. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The description of test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of players. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$, $a_i \neq a_j$ for $i \neq j$), where $a_i$ is the strength of the $i$-th player on the first map. The third line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \leq b_i \leq 10^9$, $b_i \neq b_j$ for $i \neq j$), where $b_i$ is the strength of the $i$-th player on the second map. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print a string of length $n$. $i$-th character should be "1" if the $i$-th player can win the tournament, or "0" otherwise. -----Examples----- Input 3 4 1 2 3 4 1 2 3 4 4 11 12 20 21 44 22 11 30 1 1000000000 1000000000 Output 0001 1111 1 -----Note----- In the first test case, the $4$-th player will beat any other player on any game, so he will definitely win the tournament. In the second test case, everyone can be a winner. In the third test case, there is only one player. Clearly, he will win the tournament.
T = int(input()) for testcase in range(1, T + 1): n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) aa = a.copy() aa.sort() ai = [i[0] for i in sorted(enumerate(a), key=lambda x: x[1])] bb = b.copy() bb.sort() bi = [i[0] for i in sorted(enumerate(b), key=lambda x: x[1])] res = [(0) for i in range(n)] awin = a[bi[n - 1]] bwin = b[ai[n - 1]] res[ai[n - 1]] = 1 res[bi[n - 1]] = 1 aindex = n - 2 bindex = n - 2 found = True while found: found = False while aa[aindex] > awin: res[ai[aindex]] = 1 if b[ai[aindex]] < bwin: bwin = b[ai[aindex]] found = True aindex -= 1 while bb[bindex] > bwin: res[bi[bindex]] = 1 if a[bi[bindex]] < awin: awin = a[bi[bindex]] found = True bindex -= 1 result = "".join(str(i) for i in res) print(result)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER WHILE VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
$n$ players are playing a game. There are two different maps in the game. For each player, we know his strength on each map. When two players fight on a specific map, the player with higher strength on that map always wins. No two players have the same strength on the same map. You are the game master and want to organize a tournament. There will be a total of $n-1$ battles. While there is more than one player in the tournament, choose any map and any two remaining players to fight on it. The player who loses will be eliminated from the tournament. In the end, exactly one player will remain, and he is declared the winner of the tournament. For each player determine if he can win the tournament. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The description of test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of players. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$, $a_i \neq a_j$ for $i \neq j$), where $a_i$ is the strength of the $i$-th player on the first map. The third line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \leq b_i \leq 10^9$, $b_i \neq b_j$ for $i \neq j$), where $b_i$ is the strength of the $i$-th player on the second map. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print a string of length $n$. $i$-th character should be "1" if the $i$-th player can win the tournament, or "0" otherwise. -----Examples----- Input 3 4 1 2 3 4 1 2 3 4 4 11 12 20 21 44 22 11 30 1 1000000000 1000000000 Output 0001 1111 1 -----Note----- In the first test case, the $4$-th player will beat any other player on any game, so he will definitely win the tournament. In the second test case, everyone can be a winner. In the third test case, there is only one player. Clearly, he will win the tournament.
import sys input = sys.stdin.readline def inp(): return int(input()) def st(): return input().rstrip("\n") def lis(): return list(map(int, input().split())) def ma(): return map(int, input().split()) t = inp() while t: t -= 1 n = inp() a = lis() b = lis() r = [] r1 = [] for i in range(n): r.append([a[i], i]) r1.append([b[i], i]) r.sort(reverse=True) r1.sort(reverse=True) fi = set() se = set() vis = [0] * n vis[r[0][1]] = 1 vis[r1[0][1]] = 1 for i in range(n): if r[i][1] != r1[i][1]: vis[r[i][1]] = 1 vis[r1[i][1]] = 1 if r[i][1] in se: se.remove(r[i][1]) else: fi.add(r[i][1]) if r1[i][1] in fi: fi.remove(r1[i][1]) else: se.add(r1[i][1]) else: vis[r[i][1]] = 1 if len(fi) == len(se) and len(fi) == 0: break print(*vis, sep="")
IMPORT ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR STRING
$n$ players are playing a game. There are two different maps in the game. For each player, we know his strength on each map. When two players fight on a specific map, the player with higher strength on that map always wins. No two players have the same strength on the same map. You are the game master and want to organize a tournament. There will be a total of $n-1$ battles. While there is more than one player in the tournament, choose any map and any two remaining players to fight on it. The player who loses will be eliminated from the tournament. In the end, exactly one player will remain, and he is declared the winner of the tournament. For each player determine if he can win the tournament. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The description of test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of players. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$, $a_i \neq a_j$ for $i \neq j$), where $a_i$ is the strength of the $i$-th player on the first map. The third line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \leq b_i \leq 10^9$, $b_i \neq b_j$ for $i \neq j$), where $b_i$ is the strength of the $i$-th player on the second map. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print a string of length $n$. $i$-th character should be "1" if the $i$-th player can win the tournament, or "0" otherwise. -----Examples----- Input 3 4 1 2 3 4 1 2 3 4 4 11 12 20 21 44 22 11 30 1 1000000000 1000000000 Output 0001 1111 1 -----Note----- In the first test case, the $4$-th player will beat any other player on any game, so he will definitely win the tournament. In the second test case, everyone can be a winner. In the third test case, there is only one player. Clearly, he will win the tournament.
from sys import stdin, stdout def solve_test(n: int, a: list, b: list): sorted_first = sorted([(a, i) for i, a in enumerate(a)]) sorted_second = sorted([(b, i) for i, b in enumerate(b)]) graph = [set() for _ in range(n)] for index, (x, i) in enumerate(sorted_first): if index + 1 < n: graph[i].add(sorted_first[index + 1][1]) for index, (x, i) in enumerate(sorted_second): if index + 1 < n: graph[i].add(sorted_second[index + 1][1]) visited = [False] * n stack = [sorted_first[n - 1][1]] while len(stack) > 0: index = stack.pop() visited[index] = True for neighbour in graph[index]: if not visited[neighbour]: stack.append(neighbour) stdout.write("".join([str(int(value)) for value in visited]) + "\n") def read_from_file(): with open("input.txt") as f: tests = int(f.readline().strip()) for test in range(tests): n = int(f.readline().strip()) lista = [int(x) for x in f.readline().strip().split()] listb = [int(x) for x in f.readline().strip().split()] solve_test(n, lista, listb) def read_from_input(): tests = int(stdin.readline().strip()) for test in range(tests): n = int(input()) lista = [int(x) for x in stdin.readline().strip().split()] listb = [int(x) for x in stdin.readline().strip().split()] solve_test(n, lista, listb) read_from_input()
FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST VAR BIN_OP VAR NUMBER NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR STRING FUNC_DEF FUNC_CALL VAR STRING VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR
$n$ players are playing a game. There are two different maps in the game. For each player, we know his strength on each map. When two players fight on a specific map, the player with higher strength on that map always wins. No two players have the same strength on the same map. You are the game master and want to organize a tournament. There will be a total of $n-1$ battles. While there is more than one player in the tournament, choose any map and any two remaining players to fight on it. The player who loses will be eliminated from the tournament. In the end, exactly one player will remain, and he is declared the winner of the tournament. For each player determine if he can win the tournament. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The description of test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of players. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$, $a_i \neq a_j$ for $i \neq j$), where $a_i$ is the strength of the $i$-th player on the first map. The third line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \leq b_i \leq 10^9$, $b_i \neq b_j$ for $i \neq j$), where $b_i$ is the strength of the $i$-th player on the second map. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print a string of length $n$. $i$-th character should be "1" if the $i$-th player can win the tournament, or "0" otherwise. -----Examples----- Input 3 4 1 2 3 4 1 2 3 4 4 11 12 20 21 44 22 11 30 1 1000000000 1000000000 Output 0001 1111 1 -----Note----- In the first test case, the $4$-th player will beat any other player on any game, so he will definitely win the tournament. In the second test case, everyone can be a winner. In the third test case, there is only one player. Clearly, he will win the tournament.
def solve(): n = int(input()) a = list(map(int, input().split())) b = list(map(int, input().split())) arr1 = [] arr2 = [] for i in range(n): arr1.append([a[i], i]) arr2.append([b[i], i]) arr1.sort() arr2.sort() d = {} ind = -1 for i in range(n - 1, -1, -1): d[arr1[i][1]] = 1 d[arr2[i][1]] = 1 if len(d) == n - i: ind = i break ans = [(0) for i in range(n)] for i in range(ind, n): ans[arr1[i][1]] = 1 print(*ans, sep="") t = int(input()) for tc in range(t): solve()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
$n$ players are playing a game. There are two different maps in the game. For each player, we know his strength on each map. When two players fight on a specific map, the player with higher strength on that map always wins. No two players have the same strength on the same map. You are the game master and want to organize a tournament. There will be a total of $n-1$ battles. While there is more than one player in the tournament, choose any map and any two remaining players to fight on it. The player who loses will be eliminated from the tournament. In the end, exactly one player will remain, and he is declared the winner of the tournament. For each player determine if he can win the tournament. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The description of test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of players. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$, $a_i \neq a_j$ for $i \neq j$), where $a_i$ is the strength of the $i$-th player on the first map. The third line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \leq b_i \leq 10^9$, $b_i \neq b_j$ for $i \neq j$), where $b_i$ is the strength of the $i$-th player on the second map. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print a string of length $n$. $i$-th character should be "1" if the $i$-th player can win the tournament, or "0" otherwise. -----Examples----- Input 3 4 1 2 3 4 1 2 3 4 4 11 12 20 21 44 22 11 30 1 1000000000 1000000000 Output 0001 1111 1 -----Note----- In the first test case, the $4$-th player will beat any other player on any game, so he will definitely win the tournament. In the second test case, everyone can be a winner. In the third test case, there is only one player. Clearly, he will win the tournament.
import sys T = int(sys.stdin.readline().strip()) for t in range(T): n = int(sys.stdin.readline().strip()) a = list(map(int, sys.stdin.readline().strip().split())) b = list(map(int, sys.stdin.readline().strip().split())) ai = [(a[i], i) for i in range(0, n)] bi = [(b[i], i) for i in range(0, n)] ai.sort(reverse=True) bi.sort(reverse=True) x = 0 y = 0 L = [ai[0][1], bi[0][1]] while len(L) > 0: i = L.pop() if x == n - 1 or y == n - 1: L = [] print("1" * n) break while ai[x + 1][0] >= a[i]: x = x + 1 L.append(ai[x][1]) if x == n - 1: break while bi[y + 1][0] >= b[i]: y = y + 1 L.append(bi[y][1]) if y == n - 1: break if x != n - 1 and y != n - 1: wina = [ai[i][1] for i in range(0, x + 1)] winb = [bi[i][1] for i in range(0, y + 1)] win = set(wina + winb) print("".join([str(int(i in win)) for i in range(0, n)]))
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST VAR NUMBER NUMBER VAR NUMBER NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR BIN_OP STRING VAR WHILE VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER WHILE VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR NUMBER VAR
$n$ players are playing a game. There are two different maps in the game. For each player, we know his strength on each map. When two players fight on a specific map, the player with higher strength on that map always wins. No two players have the same strength on the same map. You are the game master and want to organize a tournament. There will be a total of $n-1$ battles. While there is more than one player in the tournament, choose any map and any two remaining players to fight on it. The player who loses will be eliminated from the tournament. In the end, exactly one player will remain, and he is declared the winner of the tournament. For each player determine if he can win the tournament. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The description of test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of players. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$, $a_i \neq a_j$ for $i \neq j$), where $a_i$ is the strength of the $i$-th player on the first map. The third line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \leq b_i \leq 10^9$, $b_i \neq b_j$ for $i \neq j$), where $b_i$ is the strength of the $i$-th player on the second map. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print a string of length $n$. $i$-th character should be "1" if the $i$-th player can win the tournament, or "0" otherwise. -----Examples----- Input 3 4 1 2 3 4 1 2 3 4 4 11 12 20 21 44 22 11 30 1 1000000000 1000000000 Output 0001 1111 1 -----Note----- In the first test case, the $4$-th player will beat any other player on any game, so he will definitely win the tournament. In the second test case, everyone can be a winner. In the third test case, there is only one player. Clearly, he will win the tournament.
import sys printer = [] for _ in range(int(sys.stdin.readline())): n = int(sys.stdin.readline()) a = sorted( [(int(s), i) for i, s in enumerate(sys.stdin.readline().split())], reverse=True ) b = sorted( [(int(s), i) for i, s in enumerate(sys.stdin.readline().split())], reverse=True ) nodes = set() for i in range(n): nodes.add(a[i][1]) nodes.add(b[i][1]) if len(nodes) == i + 1: break printer.append("".join([("1" if i in nodes else "0") for i in range(n)])) print("\n".join(printer))
IMPORT ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR STRING STRING VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
$n$ players are playing a game. There are two different maps in the game. For each player, we know his strength on each map. When two players fight on a specific map, the player with higher strength on that map always wins. No two players have the same strength on the same map. You are the game master and want to organize a tournament. There will be a total of $n-1$ battles. While there is more than one player in the tournament, choose any map and any two remaining players to fight on it. The player who loses will be eliminated from the tournament. In the end, exactly one player will remain, and he is declared the winner of the tournament. For each player determine if he can win the tournament. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The description of test cases follows. The first line of each test case contains a single integer $n$ ($1 \leq n \leq 10^5$) — the number of players. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \leq a_i \leq 10^9$, $a_i \neq a_j$ for $i \neq j$), where $a_i$ is the strength of the $i$-th player on the first map. The third line of each test case contains $n$ integers $b_1, b_2, \dots, b_n$ ($1 \leq b_i \leq 10^9$, $b_i \neq b_j$ for $i \neq j$), where $b_i$ is the strength of the $i$-th player on the second map. It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$. -----Output----- For each test case print a string of length $n$. $i$-th character should be "1" if the $i$-th player can win the tournament, or "0" otherwise. -----Examples----- Input 3 4 1 2 3 4 1 2 3 4 4 11 12 20 21 44 22 11 30 1 1000000000 1000000000 Output 0001 1111 1 -----Note----- In the first test case, the $4$-th player will beat any other player on any game, so he will definitely win the tournament. In the second test case, everyone can be a winner. In the third test case, there is only one player. Clearly, he will win the tournament.
import sys inpu = sys.stdin.readline prin = sys.stdout.write t = int(inpu()) for _ in range(t): n = int(inpu()) a = list(map(int, inpu().split())) b = list(map(int, inpu().split())) tups = [] tups1 = [] for i in range(n): tups.append((a[i], b[i], i)) tups1.append((b[i], a[i], i)) tups.sort(reverse=True) tups1.sort(reverse=True) unseen = set() unseen1 = set() for i in range(n): if tups[i][2] in unseen1: unseen1.remove(tups[i][2]) else: unseen.add(tups[i][2]) if tups1[i][2] in unseen: unseen.remove(tups1[i][2]) else: unseen1.add(tups1[i][2]) if len(unseen) == 0: break out = [0] * n for j in range(i + 1): out[tups[j][2]] = 1 print("".join(str(guy) for guy in out))
IMPORT ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR
You are given two sorted arrays of distinct integers nums1 and nums2. A valid path is defined as follows: Choose array nums1 or nums2 to traverse (from index-0). Traverse the current array from left to right. If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path). Score is defined as the sum of uniques values in a valid path. Return the maximum score you can obtain of all possible valid paths. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9] Output: 30 Explanation: Valid paths: [2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1) [4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2) The maximum is obtained with the path in green [2,4,6,8,10]. Example 2: Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100] Output: 109 Explanation: Maximum sum is obtained with the path [1,3,5,100]. Example 3: Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10] Output: 40 Explanation: There are no common elements between nums1 and nums2. Maximum sum is obtained with the path [6,7,8,9,10]. Example 4: Input: nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12] Output: 61   Constraints: 1 <= nums1.length <= 10^5 1 <= nums2.length <= 10^5 1 <= nums1[i], nums2[i] <= 10^7 nums1 and nums2 are strictly increasing.
class Solution: def maxSum(self, nums1: List[int], nums2: List[int]) -> int: dict1 = {} dict2 = {} for i in range(len(nums1)): dict1[nums1[i]] = i for i in range(len(nums2)): dict2[nums2[i]] = i dp1 = [(0) for _ in nums1] dp2 = [(0) for _ in nums2] def sol(index, curr): if curr == 1: if index == len(nums1): return 0 if not dp1[index]: if nums1[index] in dict2: indx2 = dict2[nums1[index]] dp1[index] = ( max(sol(index + 1, curr), sol(indx2 + 1, 2)) + nums1[index] ) else: dp1[index] = sol(index + 1, curr) + nums1[index] return dp1[index] else: if index == len(nums2): return 0 if not dp2[index]: if nums2[index] in dict1: indx2 = dict1[nums2[index]] dp2[index] = ( max(sol(index + 1, curr), sol(indx2 + 1, 1)) + nums2[index] ) else: dp2[index] = sol(index + 1, curr) + nums2[index] return dp2[index] sol(0, 1) sol(0, 2) return max(dp1[0], dp2[0]) % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR VAR FUNC_DEF IF VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR VAR IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR VAR EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER RETURN BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
You are given two sorted arrays of distinct integers nums1 and nums2. A valid path is defined as follows: Choose array nums1 or nums2 to traverse (from index-0). Traverse the current array from left to right. If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path). Score is defined as the sum of uniques values in a valid path. Return the maximum score you can obtain of all possible valid paths. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9] Output: 30 Explanation: Valid paths: [2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1) [4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2) The maximum is obtained with the path in green [2,4,6,8,10]. Example 2: Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100] Output: 109 Explanation: Maximum sum is obtained with the path [1,3,5,100]. Example 3: Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10] Output: 40 Explanation: There are no common elements between nums1 and nums2. Maximum sum is obtained with the path [6,7,8,9,10]. Example 4: Input: nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12] Output: 61   Constraints: 1 <= nums1.length <= 10^5 1 <= nums2.length <= 10^5 1 <= nums1[i], nums2[i] <= 10^7 nums1 and nums2 are strictly increasing.
class Solution: def maxSum(self, nums1, nums2): M, N = len(nums1), len(nums2) sum1, sum2 = 0, 0 i, j = 0, 0 res = 0 while i < M and j < N: if nums1[i] < nums2[j]: sum1 += nums1[i] i += 1 elif nums1[i] > nums2[j]: sum2 += nums2[j] j += 1 else: res += max(sum1, sum2) + nums1[i] i += 1 j += 1 sum1 = 0 sum2 = 0 while i < M: sum1 += nums1[i] i += 1 while j < N: sum2 += nums2[j] j += 1 return (res + max(sum1, sum2)) % 1000000007
CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER
You are given two sorted arrays of distinct integers nums1 and nums2. A valid path is defined as follows: Choose array nums1 or nums2 to traverse (from index-0). Traverse the current array from left to right. If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path). Score is defined as the sum of uniques values in a valid path. Return the maximum score you can obtain of all possible valid paths. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9] Output: 30 Explanation: Valid paths: [2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1) [4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2) The maximum is obtained with the path in green [2,4,6,8,10]. Example 2: Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100] Output: 109 Explanation: Maximum sum is obtained with the path [1,3,5,100]. Example 3: Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10] Output: 40 Explanation: There are no common elements between nums1 and nums2. Maximum sum is obtained with the path [6,7,8,9,10]. Example 4: Input: nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12] Output: 61   Constraints: 1 <= nums1.length <= 10^5 1 <= nums2.length <= 10^5 1 <= nums1[i], nums2[i] <= 10^7 nums1 and nums2 are strictly increasing.
class Solution: def maxSum(self, nums1: List[int], nums2: List[int]) -> int: memo1 = defaultdict(lambda: 0) memo2 = defaultdict(lambda: 0) nums1.append(0) nums2.append(0) i = len(nums1) - 2 j = len(nums2) - 2 while i >= 0 or j >= 0: n1 = nums1[i] if i >= 0 else 0 n2 = nums2[j] if j >= 0 else 0 if n1 == n2: memo1[n1] = max(n1 + memo1[nums1[i + 1]], n2 + memo2[nums2[j + 1]]) memo2[n2] = memo1[n1] i -= 1 j -= 1 elif n1 > n2: memo1[n1] = max(n1 + memo1[nums1[i + 1]], memo2[n1]) i -= 1 else: memo2[n2] = max(n2 + memo2[nums2[j + 1]], memo1[n2]) j -= 1 return max(memo1[nums1[0]], memo2[nums2[0]]) % int(1000000000.0 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER FUNC_CALL VAR BIN_OP NUMBER NUMBER VAR
You are given two sorted arrays of distinct integers nums1 and nums2. A valid path is defined as follows: Choose array nums1 or nums2 to traverse (from index-0). Traverse the current array from left to right. If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path). Score is defined as the sum of uniques values in a valid path. Return the maximum score you can obtain of all possible valid paths. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9] Output: 30 Explanation: Valid paths: [2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1) [4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2) The maximum is obtained with the path in green [2,4,6,8,10]. Example 2: Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100] Output: 109 Explanation: Maximum sum is obtained with the path [1,3,5,100]. Example 3: Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10] Output: 40 Explanation: There are no common elements between nums1 and nums2. Maximum sum is obtained with the path [6,7,8,9,10]. Example 4: Input: nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12] Output: 61   Constraints: 1 <= nums1.length <= 10^5 1 <= nums2.length <= 10^5 1 <= nums1[i], nums2[i] <= 10^7 nums1 and nums2 are strictly increasing.
class Solution: def maxSum(self, nums1: List[int], nums2: List[int]) -> int: cross = [] n, m = len(nums1), len(nums2) i, j = 0, 0 M = 10**9 + 7 while i < n and j < m: if nums1[i] > nums2[j]: j += 1 elif nums2[j] > nums1[i]: i += 1 else: cross.append((i, j)) i += 1 j += 1 l = len(cross) if l == 0: return max(sum(nums1), sum(nums2)) % M ans = max(sum(nums1[cross[-1][0] :]), sum(nums2[cross[-1][1] :])) ans %= M for i in range(l - 2, -1, -1): ans += max( sum(nums1[cross[i][0] : cross[i + 1][0]]), sum(nums2[cross[i][1] : cross[i + 1][1]]), ) ans %= M ans += max(sum(nums1[: cross[0][0]]), sum(nums2[: cross[0][1]])) ans %= M return ans % M
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR VAR RETURN BIN_OP VAR VAR VAR
You are given two sorted arrays of distinct integers nums1 and nums2. A valid path is defined as follows: Choose array nums1 or nums2 to traverse (from index-0). Traverse the current array from left to right. If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path). Score is defined as the sum of uniques values in a valid path. Return the maximum score you can obtain of all possible valid paths. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9] Output: 30 Explanation: Valid paths: [2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1) [4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2) The maximum is obtained with the path in green [2,4,6,8,10]. Example 2: Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100] Output: 109 Explanation: Maximum sum is obtained with the path [1,3,5,100]. Example 3: Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10] Output: 40 Explanation: There are no common elements between nums1 and nums2. Maximum sum is obtained with the path [6,7,8,9,10]. Example 4: Input: nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12] Output: 61   Constraints: 1 <= nums1.length <= 10^5 1 <= nums2.length <= 10^5 1 <= nums1[i], nums2[i] <= 10^7 nums1 and nums2 are strictly increasing.
class Solution: def maxSum(self, nums1: List[int], nums2: List[int]) -> int: i, j, n, m = 0, 0, len(nums1), len(nums2) a, b = 0, 0 result = 0 while i < n or j < m: if i < n and (j == m or nums1[i] < nums2[j]): a += nums1[i] i += 1 elif j < m and (i == n or nums1[i] > nums2[j]): b += nums2[j] j += 1 else: a = b = max(a, b) + nums1[i] i += 1 j += 1 return max(a, b) % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
You are given two sorted arrays of distinct integers nums1 and nums2. A valid path is defined as follows: Choose array nums1 or nums2 to traverse (from index-0). Traverse the current array from left to right. If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path). Score is defined as the sum of uniques values in a valid path. Return the maximum score you can obtain of all possible valid paths. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9] Output: 30 Explanation: Valid paths: [2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1) [4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2) The maximum is obtained with the path in green [2,4,6,8,10]. Example 2: Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100] Output: 109 Explanation: Maximum sum is obtained with the path [1,3,5,100]. Example 3: Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10] Output: 40 Explanation: There are no common elements between nums1 and nums2. Maximum sum is obtained with the path [6,7,8,9,10]. Example 4: Input: nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12] Output: 61   Constraints: 1 <= nums1.length <= 10^5 1 <= nums2.length <= 10^5 1 <= nums1[i], nums2[i] <= 10^7 nums1 and nums2 are strictly increasing.
class Solution: def maxSum(self, nums1: List[int], nums2: List[int]) -> int: sumMax = 0 nLen1 = len(nums1) nLen2 = len(nums2) loopLen = min(nLen1, nLen2) maxVal = 10**9 + 7 idx1 = {nums1[0]: 0} idx2 = {nums2[0]: 0} ps1 = 0 ps2 = 0 pe1 = 1 pe2 = 1 for i in range(1, loopLen): v1 = nums1[i] v2 = nums2[i] sumT = 0 if v1 == v2: pe1 = i + 1 pe2 = i + 1 sum1 = sum(nums1[ps1:pe1]) sum2 = sum(nums2[ps2:pe2]) sumT = max(sum1, sum2) ps1 = pe1 ps2 = pe2 if v1 in idx2: pe1 = i + 1 pe2 = idx2[v1] + 1 sum1 = sum(nums1[ps1:pe1]) sum2 = sum(nums2[ps2:pe2]) sumT = max(sum1, sum2) ps1 = pe1 ps2 = pe2 elif v2 in idx1: pe2 = i + 1 pe1 = idx1[v2] + 1 sum1 = sum(nums1[ps1:pe1]) sum2 = sum(nums2[ps2:pe2]) sumT = max(sum1, sum2) ps1 = pe1 ps2 = pe2 sumMax += sumT sumMax = sumMax % maxVal idx1[v1] = i idx2[v2] = i if nLen1 > nLen2: for i in range(loopLen, nLen1): v1 = nums1[i] if v1 in idx2: pe1 = i + 1 pe2 = idx2[v1] + 1 sum1 = sum(nums1[ps1:pe1]) sum2 = sum(nums2[ps2:pe2]) sumT = max(sum1, sum2) ps1 = pe1 ps2 = pe2 else: sumT = 0 sumMax += sumT sumMax = sumMax % maxVal idx1[v1] = i if v1 > nums2[-1]: break elif nLen2 > nLen1: for i in range(loopLen, nLen2): v2 = nums2[i] if v2 in idx1: pe2 = i + 1 pe1 = idx1[v2] + 1 sum1 = sum(nums1[ps1:pe1]) sum2 = sum(nums2[ps2:pe2]) sumT = max(sum1, sum2) ps1 = pe1 ps2 = pe2 else: sumT = 0 sumMax += sumT sumMax = sumMax % maxVal idx2[v2] = i if v2 > nums1[-1]: break if ps1 < nLen1: sum1 = sum(nums1[ps1:]) else: sum1 = 0 if ps2 < nLen2: sum2 = sum(nums2[ps2:]) else: sum2 = 0 sumT = max(sum1, sum2) sumMax += sumT sumMax = sumMax % maxVal return sumMax
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR DICT VAR NUMBER NUMBER ASSIGN VAR DICT VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR IF VAR VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR IF VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN VAR VAR
You are given two sorted arrays of distinct integers nums1 and nums2. A valid path is defined as follows: Choose array nums1 or nums2 to traverse (from index-0). Traverse the current array from left to right. If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path). Score is defined as the sum of uniques values in a valid path. Return the maximum score you can obtain of all possible valid paths. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9] Output: 30 Explanation: Valid paths: [2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1) [4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2) The maximum is obtained with the path in green [2,4,6,8,10]. Example 2: Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100] Output: 109 Explanation: Maximum sum is obtained with the path [1,3,5,100]. Example 3: Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10] Output: 40 Explanation: There are no common elements between nums1 and nums2. Maximum sum is obtained with the path [6,7,8,9,10]. Example 4: Input: nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12] Output: 61   Constraints: 1 <= nums1.length <= 10^5 1 <= nums2.length <= 10^5 1 <= nums1[i], nums2[i] <= 10^7 nums1 and nums2 are strictly increasing.
class Solution: def maxSum(self, nums1: List[int], nums2: List[int]) -> int: common = set(nums1).intersection(set(nums2)) ans = 0 segments1 = [0] * (len(common) + 1) tmp = 0 j = 0 for i in nums1: tmp += i if i in common: segments1[j] = tmp j += 1 tmp = 0 segments1[j] = tmp j = 0 tmp = 0 for i in nums2: tmp += i if i in common: ans += max(segments1[j], tmp) j += 1 tmp = 0 ans += max(segments1[j], tmp) return ans % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
You are given two sorted arrays of distinct integers nums1 and nums2. A valid path is defined as follows: Choose array nums1 or nums2 to traverse (from index-0). Traverse the current array from left to right. If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path). Score is defined as the sum of uniques values in a valid path. Return the maximum score you can obtain of all possible valid paths. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9] Output: 30 Explanation: Valid paths: [2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1) [4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2) The maximum is obtained with the path in green [2,4,6,8,10]. Example 2: Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100] Output: 109 Explanation: Maximum sum is obtained with the path [1,3,5,100]. Example 3: Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10] Output: 40 Explanation: There are no common elements between nums1 and nums2. Maximum sum is obtained with the path [6,7,8,9,10]. Example 4: Input: nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12] Output: 61   Constraints: 1 <= nums1.length <= 10^5 1 <= nums2.length <= 10^5 1 <= nums1[i], nums2[i] <= 10^7 nums1 and nums2 are strictly increasing.
class Solution: def maxSum(self, nums1: List[int], nums2: List[int]) -> int: m = len(nums1) n = len(nums2) i = m - 1 j = n - 1 s1 = 0 s2 = 0 while i >= 0 and j >= 0: if nums1[i] > nums2[j]: s1 += nums1[i] i -= 1 elif nums2[j] > nums1[i]: s2 += nums2[j] j -= 1 else: s1 = max(s1, s2) + nums1[i] s2 = s1 i -= 1 j -= 1 if i != -1: s1 += sum(nums1[: i + 1]) if j != -1: s2 += sum(nums2[: j + 1]) return max(s1, s2) % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
You are given two sorted arrays of distinct integers nums1 and nums2. A valid path is defined as follows: Choose array nums1 or nums2 to traverse (from index-0). Traverse the current array from left to right. If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path). Score is defined as the sum of uniques values in a valid path. Return the maximum score you can obtain of all possible valid paths. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9] Output: 30 Explanation: Valid paths: [2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1) [4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2) The maximum is obtained with the path in green [2,4,6,8,10]. Example 2: Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100] Output: 109 Explanation: Maximum sum is obtained with the path [1,3,5,100]. Example 3: Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10] Output: 40 Explanation: There are no common elements between nums1 and nums2. Maximum sum is obtained with the path [6,7,8,9,10]. Example 4: Input: nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12] Output: 61   Constraints: 1 <= nums1.length <= 10^5 1 <= nums2.length <= 10^5 1 <= nums1[i], nums2[i] <= 10^7 nums1 and nums2 are strictly increasing.
class Solution: def maxSum(self, nums1: List[int], nums2: List[int]) -> int: prev = collections.defaultdict(set) MOD = 10**9 + 7 if not nums1: return sum(nums2) elif not nums2: return sum(num1) ans = 0 dp = collections.defaultdict(lambda: 0) dp[-1] = 0 for i in range(len(nums1)): if i == 0: prev[nums1[i]].add(-1) prev[nums1[i]].add(nums1[i - 1]) for i in range(len(nums2)): if i == 0: prev[nums2[i]].add(-1) prev[nums2[i]].add(nums2[i - 1]) i = j = 0 while i < len(nums1) or j < len(nums2): target = -1 if j >= len(nums2) or i < len(nums1) and nums1[i] < nums2[j]: target = nums1[i] i += 1 else: target = nums2[j] j += 1 for p in prev[target]: dp[target] = max(dp[p] + target, dp[target]) ans = max(ans, dp[target]) return ans % MOD
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER IF VAR RETURN FUNC_CALL VAR VAR IF VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER FOR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN BIN_OP VAR VAR VAR
You are given two sorted arrays of distinct integers nums1 and nums2. A valid path is defined as follows: Choose array nums1 or nums2 to traverse (from index-0). Traverse the current array from left to right. If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path). Score is defined as the sum of uniques values in a valid path. Return the maximum score you can obtain of all possible valid paths. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9] Output: 30 Explanation: Valid paths: [2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1) [4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2) The maximum is obtained with the path in green [2,4,6,8,10]. Example 2: Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100] Output: 109 Explanation: Maximum sum is obtained with the path [1,3,5,100]. Example 3: Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10] Output: 40 Explanation: There are no common elements between nums1 and nums2. Maximum sum is obtained with the path [6,7,8,9,10]. Example 4: Input: nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12] Output: 61   Constraints: 1 <= nums1.length <= 10^5 1 <= nums2.length <= 10^5 1 <= nums1[i], nums2[i] <= 10^7 nums1 and nums2 are strictly increasing.
class Solution: def maxSum(self, nums1: List[int], nums2: List[int]) -> int: one = 0 two = 0 i = 0 j = 0 while i < len(nums1) and j < len(nums2): if nums1[i] == nums2[j]: one = two = max(one, two) + nums1[i] i += 1 j += 1 elif nums1[i] > nums2[j]: two += nums2[j] j += 1 else: one += nums1[i] i += 1 while i < len(nums1): one += nums1[i] i += 1 while j < len(nums2): two += nums2[j] j += 1 return max(one, two) % 1000000007
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR
You are given two sorted arrays of distinct integers nums1 and nums2. A valid path is defined as follows: Choose array nums1 or nums2 to traverse (from index-0). Traverse the current array from left to right. If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path). Score is defined as the sum of uniques values in a valid path. Return the maximum score you can obtain of all possible valid paths. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9] Output: 30 Explanation: Valid paths: [2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1) [4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2) The maximum is obtained with the path in green [2,4,6,8,10]. Example 2: Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100] Output: 109 Explanation: Maximum sum is obtained with the path [1,3,5,100]. Example 3: Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10] Output: 40 Explanation: There are no common elements between nums1 and nums2. Maximum sum is obtained with the path [6,7,8,9,10]. Example 4: Input: nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12] Output: 61   Constraints: 1 <= nums1.length <= 10^5 1 <= nums2.length <= 10^5 1 <= nums1[i], nums2[i] <= 10^7 nums1 and nums2 are strictly increasing.
class Solution: def maxSum(self, nums1: List[int], nums2: List[int]) -> int: def getSum(i, nums1, j, nums2): total = 0 while i < len(nums1): total += nums1[i] i += 1 while i < len(nums1) and j < len(nums2) and nums2[j] < nums1[i]: j += 1 if i < len(nums1) and j < len(nums2) and nums1[i] == nums2[j]: if nums1[i] not in dp: dp[nums1[i]] = max( getSum(i, nums1, j, nums2), getSum(j, nums2, i, nums1) ) return total + dp[nums1[i]] return total dp = {} return max(getSum(0, nums1, 0, nums2), getSum(0, nums2, 0, nums1)) % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN BIN_OP VAR VAR VAR VAR RETURN VAR ASSIGN VAR DICT RETURN BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
You are given two sorted arrays of distinct integers nums1 and nums2. A valid path is defined as follows: Choose array nums1 or nums2 to traverse (from index-0). Traverse the current array from left to right. If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path). Score is defined as the sum of uniques values in a valid path. Return the maximum score you can obtain of all possible valid paths. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9] Output: 30 Explanation: Valid paths: [2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1) [4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2) The maximum is obtained with the path in green [2,4,6,8,10]. Example 2: Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100] Output: 109 Explanation: Maximum sum is obtained with the path [1,3,5,100]. Example 3: Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10] Output: 40 Explanation: There are no common elements between nums1 and nums2. Maximum sum is obtained with the path [6,7,8,9,10]. Example 4: Input: nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12] Output: 61   Constraints: 1 <= nums1.length <= 10^5 1 <= nums2.length <= 10^5 1 <= nums1[i], nums2[i] <= 10^7 nums1 and nums2 are strictly increasing.
class Solution: def maxSum(self, nums1: List[int], nums2: List[int]) -> int: i, j = 0, 0 a, b = 0, 0 while i < len(nums1) and j < len(nums2): if nums1[i] < nums2[j]: a += nums1[i] i += 1 elif nums1[i] > nums2[j]: b += nums2[j] j += 1 else: a = b = max(a, b) + nums1[i] i += 1 j += 1 if i != len(nums1): a = max(b, a + sum(nums1[i:])) elif j != len(nums2): a = max(a, b + sum(nums2[j:])) return a % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
You are given two sorted arrays of distinct integers nums1 and nums2. A valid path is defined as follows: Choose array nums1 or nums2 to traverse (from index-0). Traverse the current array from left to right. If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path). Score is defined as the sum of uniques values in a valid path. Return the maximum score you can obtain of all possible valid paths. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9] Output: 30 Explanation: Valid paths: [2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1) [4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2) The maximum is obtained with the path in green [2,4,6,8,10]. Example 2: Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100] Output: 109 Explanation: Maximum sum is obtained with the path [1,3,5,100]. Example 3: Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10] Output: 40 Explanation: There are no common elements between nums1 and nums2. Maximum sum is obtained with the path [6,7,8,9,10]. Example 4: Input: nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12] Output: 61   Constraints: 1 <= nums1.length <= 10^5 1 <= nums2.length <= 10^5 1 <= nums1[i], nums2[i] <= 10^7 nums1 and nums2 are strictly increasing.
MOD = 1000000007 class Solution: def maxSum(self, nums1, nums2): sum1, sum2, res = 0, 0, 0 p1, p2 = 0, 0 while p1 <= len(nums1) - 1 and p2 <= len(nums2) - 1: if nums1[p1] == nums2[p2]: res += max(sum1, sum2) + nums1[p1] sum1, sum2 = 0, 0 p1 += 1 p2 += 1 elif nums1[p1] < nums2[p2]: sum1 += nums1[p1] p1 += 1 else: sum2 += nums2[p2] p2 += 1 if p1 <= len(nums1) - 1: sum1 += sum(nums1[p1:]) if p2 <= len(nums2) - 1: sum2 += sum(nums2[p2:]) return (res + max(sum1, sum2)) % MOD
ASSIGN VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR VAR
You are given two sorted arrays of distinct integers nums1 and nums2. A valid path is defined as follows: Choose array nums1 or nums2 to traverse (from index-0). Traverse the current array from left to right. If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path). Score is defined as the sum of uniques values in a valid path. Return the maximum score you can obtain of all possible valid paths. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9] Output: 30 Explanation: Valid paths: [2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1) [4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2) The maximum is obtained with the path in green [2,4,6,8,10]. Example 2: Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100] Output: 109 Explanation: Maximum sum is obtained with the path [1,3,5,100]. Example 3: Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10] Output: 40 Explanation: There are no common elements between nums1 and nums2. Maximum sum is obtained with the path [6,7,8,9,10]. Example 4: Input: nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12] Output: 61   Constraints: 1 <= nums1.length <= 10^5 1 <= nums2.length <= 10^5 1 <= nums1[i], nums2[i] <= 10^7 nums1 and nums2 are strictly increasing.
class Solution: def maxSum(self, nums1: List[int], nums2: List[int]) -> int: n1 = len(nums1) n2 = len(nums2) a1 = [(-1) for _ in range(n1)] a2 = [(-1) for _ in range(n2)] i, j = 0, 0 while i < n1 and j < n2: if nums1[i] < nums2[j]: i += 1 elif nums1[i] > nums2[j]: j += 1 else: a1[i] = j a2[j] = i i += 1 j += 1 cache1 = {} cache2 = {} def process1(idx): if idx == n1: return 0 if not idx in cache1: ans = 0 if a1[idx] == -1: ans = nums1[idx] + process1(idx + 1) else: ans = nums1[idx] + max(process1(idx + 1), process2(a1[idx] + 1)) cache1[idx] = ans return cache1[idx] def process2(idx): if idx == n2: return 0 if not idx in cache2: ans = 0 if a2[idx] == -1: ans = nums2[idx] + process2(idx + 1) else: ans = nums2[idx] + max(process2(idx + 1), process1(a2[idx] + 1)) cache2[idx] = ans return cache2[idx] return max(process1(0), process2(0)) % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR DICT ASSIGN VAR DICT FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR VAR RETURN BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
You are given two sorted arrays of distinct integers nums1 and nums2. A valid path is defined as follows: Choose array nums1 or nums2 to traverse (from index-0). Traverse the current array from left to right. If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path). Score is defined as the sum of uniques values in a valid path. Return the maximum score you can obtain of all possible valid paths. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9] Output: 30 Explanation: Valid paths: [2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1) [4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2) The maximum is obtained with the path in green [2,4,6,8,10]. Example 2: Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100] Output: 109 Explanation: Maximum sum is obtained with the path [1,3,5,100]. Example 3: Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10] Output: 40 Explanation: There are no common elements between nums1 and nums2. Maximum sum is obtained with the path [6,7,8,9,10]. Example 4: Input: nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12] Output: 61   Constraints: 1 <= nums1.length <= 10^5 1 <= nums2.length <= 10^5 1 <= nums1[i], nums2[i] <= 10^7 nums1 and nums2 are strictly increasing.
class Solution: def maxSum(self, nums1: List[int], nums2: List[int]) -> int: score = 0 total1 = 0 total2 = 0 pos1 = 0 pos2 = 0 while pos1 < len(nums1) or pos2 < len(nums2): if pos1 < len(nums1) and (pos2 >= len(nums2) or nums1[pos1] < nums2[pos2]): total1 += nums1[pos1] pos1 += 1 continue if pos2 < len(nums2) and (pos1 >= len(nums1) or nums1[pos1] > nums2[pos2]): total2 += nums2[pos2] pos2 += 1 continue score += nums1[pos1] + max(total1, total2) pos1 += 1 pos2 += 1 total1 = 0 total2 = 0 score += max(total1, total2) return score % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
You are given two sorted arrays of distinct integers nums1 and nums2. A valid path is defined as follows: Choose array nums1 or nums2 to traverse (from index-0). Traverse the current array from left to right. If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path). Score is defined as the sum of uniques values in a valid path. Return the maximum score you can obtain of all possible valid paths. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9] Output: 30 Explanation: Valid paths: [2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1) [4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2) The maximum is obtained with the path in green [2,4,6,8,10]. Example 2: Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100] Output: 109 Explanation: Maximum sum is obtained with the path [1,3,5,100]. Example 3: Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10] Output: 40 Explanation: There are no common elements between nums1 and nums2. Maximum sum is obtained with the path [6,7,8,9,10]. Example 4: Input: nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12] Output: 61   Constraints: 1 <= nums1.length <= 10^5 1 <= nums2.length <= 10^5 1 <= nums1[i], nums2[i] <= 10^7 nums1 and nums2 are strictly increasing.
class Solution: def maxSum(self, nums1: List[int], nums2: List[int]) -> int: m = len(nums1) n = len(nums2) mod = 10**9 + 7 pre1 = [0] * (m + 1) pre2 = [0] * (n + 1) for i in range(m): pre1[i + 1] = pre1[i] + nums1[i] for i in range(n): pre2[i + 1] = pre2[i] + nums2[i] cm1 = [] cm2 = [] st = set() d = {} for i in range(m): d[nums1[i]] = i st1 = set(nums1) for i in range(n): if nums2[i] in st1: cm1.append(d[nums2[i]]) cm2.append(i) ans = 0 for i in range(len(cm1)): idx1 = cm1[i] idx2 = cm2[i] if i == 0: sm1 = pre1[idx1] sm2 = pre2[idx2] else: sm1 = pre1[idx1] - pre1[cm1[i - 1] + 1] sm2 = pre2[idx2] - pre2[cm2[i - 1] + 1] ans += max(sm1, sm2) ans %= mod op1 = 0 op2 = 0 if len(cm1) and cm1[-1] + 1 < m: op1 = sum(nums1[cm1[-1] + 1 :]) if len(cm2) and cm2[-1] + 1 < n: op2 = sum(nums2[cm2[-1] + 1 :]) if not len(cm1): return max(sum(nums1), sum(nums2)) % mod ans += max(op1, op2) ans %= mod return (ans + sum(nums1[i] for i in cm1)) % mod
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR RETURN BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR
You are given two sorted arrays of distinct integers nums1 and nums2. A valid path is defined as follows: Choose array nums1 or nums2 to traverse (from index-0). Traverse the current array from left to right. If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path). Score is defined as the sum of uniques values in a valid path. Return the maximum score you can obtain of all possible valid paths. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9] Output: 30 Explanation: Valid paths: [2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1) [4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2) The maximum is obtained with the path in green [2,4,6,8,10]. Example 2: Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100] Output: 109 Explanation: Maximum sum is obtained with the path [1,3,5,100]. Example 3: Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10] Output: 40 Explanation: There are no common elements between nums1 and nums2. Maximum sum is obtained with the path [6,7,8,9,10]. Example 4: Input: nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12] Output: 61   Constraints: 1 <= nums1.length <= 10^5 1 <= nums2.length <= 10^5 1 <= nums1[i], nums2[i] <= 10^7 nums1 and nums2 are strictly increasing.
class Solution: def maxSum(self, nums1: List[int], nums2: List[int]) -> int: p1 = p2 = 0 mx1 = 0 mx2 = 0 while p1 < len(nums1) and p2 < len(nums2): if nums1[p1] == nums2[p2]: mx1 = mx2 = max(mx1, mx2) + nums1[p1] p1 += 1 p2 += 1 while p1 < len(nums1) and p2 < len(nums2) and nums1[p1] < nums2[p2]: mx1 += nums1[p1] p1 += 1 while p1 < len(nums1) and p2 < len(nums2) and nums2[p2] < nums1[p1]: mx2 += nums2[p2] p2 += 1 while p1 < len(nums1): mx1 += nums1[p1] p1 += 1 while p2 < len(nums2): mx2 += nums2[p2] p2 += 1 return max(mx1, mx2) % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
You are given two sorted arrays of distinct integers nums1 and nums2. A valid path is defined as follows: Choose array nums1 or nums2 to traverse (from index-0). Traverse the current array from left to right. If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path). Score is defined as the sum of uniques values in a valid path. Return the maximum score you can obtain of all possible valid paths. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9] Output: 30 Explanation: Valid paths: [2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1) [4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2) The maximum is obtained with the path in green [2,4,6,8,10]. Example 2: Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100] Output: 109 Explanation: Maximum sum is obtained with the path [1,3,5,100]. Example 3: Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10] Output: 40 Explanation: There are no common elements between nums1 and nums2. Maximum sum is obtained with the path [6,7,8,9,10]. Example 4: Input: nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12] Output: 61   Constraints: 1 <= nums1.length <= 10^5 1 <= nums2.length <= 10^5 1 <= nums1[i], nums2[i] <= 10^7 nums1 and nums2 are strictly increasing.
class Solution: def maxSum(self, nums1: List[int], nums2: List[int]) -> int: N = len(nums1) M = len(nums2) def get_pos(nums): pos = defaultdict(lambda: -1) for i, x in enumerate(nums): pos[x] = i return pos pos1 = get_pos(nums1) pos2 = get_pos(nums2) dp1 = [0] * (N + 1) dp2 = [0] * (M + 1) i, j = N - 1, M - 1 while i >= 0 or j >= 0: while i >= 0 and pos2[nums1[i]] < 0: dp1[i] = nums1[i] + dp1[i + 1] i -= 1 while j >= 0 and pos1[nums2[j]] < 0: dp2[j] = nums2[j] + dp2[j + 1] j -= 1 if i >= 0: p = pos2[nums1[i]] dp1[i] = nums1[i] + max(dp1[i + 1], dp2[p + 1]) i -= 1 if j >= 0: p = pos1[nums2[j]] dp2[j] = nums2[j] + max(dp2[j + 1], dp1[p + 1]) j -= 1 res = max(dp1[0], dp2[0]) return res % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER WHILE VAR NUMBER VAR NUMBER WHILE VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
You are given two sorted arrays of distinct integers nums1 and nums2. A valid path is defined as follows: Choose array nums1 or nums2 to traverse (from index-0). Traverse the current array from left to right. If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path). Score is defined as the sum of uniques values in a valid path. Return the maximum score you can obtain of all possible valid paths. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9] Output: 30 Explanation: Valid paths: [2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1) [4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2) The maximum is obtained with the path in green [2,4,6,8,10]. Example 2: Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100] Output: 109 Explanation: Maximum sum is obtained with the path [1,3,5,100]. Example 3: Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10] Output: 40 Explanation: There are no common elements between nums1 and nums2. Maximum sum is obtained with the path [6,7,8,9,10]. Example 4: Input: nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12] Output: 61   Constraints: 1 <= nums1.length <= 10^5 1 <= nums2.length <= 10^5 1 <= nums1[i], nums2[i] <= 10^7 nums1 and nums2 are strictly increasing.
class Solution: def maxSum(self, nums1: List[int], nums2: List[int]) -> int: indeg = defaultdict(int) g = defaultdict(list) for i in range(len(nums1) - 1): g[nums1[i]].append(nums1[i + 1]) indeg[nums1[i + 1]] += 1 for i in range(len(nums2) - 1): g[nums2[i]].append(nums2[i + 1]) indeg[nums2[i + 1]] += 1 q = deque() ans = 0 dists = defaultdict(int) for num in g: if indeg[num] == 0: q.append((num, num)) dists[num] = num while q: num, score = q.popleft() ans = max(ans, score) for nei in g[num]: indeg[nei] -= 1 dists[nei] = max(dists[nei], score) if indeg[nei] == 0: q.append((nei, nei + dists[nei])) return ans % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
You are given two sorted arrays of distinct integers nums1 and nums2. A valid path is defined as follows: Choose array nums1 or nums2 to traverse (from index-0). Traverse the current array from left to right. If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path). Score is defined as the sum of uniques values in a valid path. Return the maximum score you can obtain of all possible valid paths. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9] Output: 30 Explanation: Valid paths: [2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1) [4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2) The maximum is obtained with the path in green [2,4,6,8,10]. Example 2: Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100] Output: 109 Explanation: Maximum sum is obtained with the path [1,3,5,100]. Example 3: Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10] Output: 40 Explanation: There are no common elements between nums1 and nums2. Maximum sum is obtained with the path [6,7,8,9,10]. Example 4: Input: nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12] Output: 61   Constraints: 1 <= nums1.length <= 10^5 1 <= nums2.length <= 10^5 1 <= nums1[i], nums2[i] <= 10^7 nums1 and nums2 are strictly increasing.
class Solution: def maxSum(self, nums1: List[int], nums2: List[int]) -> int: @lru_cache(None) def dp(idx1, idx2, top: bool): if top: if idx1 < len(nums1): while idx2 < len(nums2) and nums2[idx2] < nums1[idx1]: idx2 += 1 if idx2 >= len(nums2): return sum(nums1[idx1:]) if nums1[idx1] == nums2[idx2]: return nums1[idx1] + max( dp(idx1 + 1, idx2 + 1, True), dp(idx1 + 1, idx2 + 1, False) ) else: return nums1[idx1] + dp(idx1 + 1, idx2, True) else: return sum(nums1[idx1:]) elif idx2 < len(nums2): while idx1 < len(nums1) and nums1[idx1] < nums2[idx2]: idx1 += 1 if idx1 >= len(nums1): return sum(nums2[idx2:]) if nums1[idx1] == nums2[idx2]: return nums2[idx2] + max( dp(idx1 + 1, idx2 + 1, True), dp(idx1 + 1, idx2 + 1, False) ) else: return nums2[idx2] + dp(idx1, idx2 + 1, False) else: return sum(nums2[idx2:]) return max(dp(0, 0, True), dp(0, 0, False)) % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR FUNC_DEF VAR IF VAR IF VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR RETURN BIN_OP VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR VAR IF VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR RETURN BIN_OP VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR VAR FUNC_CALL VAR NONE RETURN BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER NUMBER NUMBER FUNC_CALL VAR NUMBER NUMBER NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
You are given two sorted arrays of distinct integers nums1 and nums2. A valid path is defined as follows: Choose array nums1 or nums2 to traverse (from index-0). Traverse the current array from left to right. If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path). Score is defined as the sum of uniques values in a valid path. Return the maximum score you can obtain of all possible valid paths. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9] Output: 30 Explanation: Valid paths: [2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1) [4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2) The maximum is obtained with the path in green [2,4,6,8,10]. Example 2: Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100] Output: 109 Explanation: Maximum sum is obtained with the path [1,3,5,100]. Example 3: Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10] Output: 40 Explanation: There are no common elements between nums1 and nums2. Maximum sum is obtained with the path [6,7,8,9,10]. Example 4: Input: nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12] Output: 61   Constraints: 1 <= nums1.length <= 10^5 1 <= nums2.length <= 10^5 1 <= nums1[i], nums2[i] <= 10^7 nums1 and nums2 are strictly increasing.
class Solution: def maxSum(self, nums1: List[int], nums2: List[int]) -> int: d2 = {nums2[i]: i for i in range(len(nums2))} _nums1 = [] _nums2 = [] prev_i, prev_j = 0, 0 for i in range(len(nums1)): if nums1[i] in d2: _nums1.append(sum(nums1[prev_i:i])) _nums2.append(sum(nums2[prev_j : d2[nums1[i]]])) _nums1.append(nums1[i]) _nums2.append(nums1[i]) prev_i = i + 1 prev_j = d2[nums1[i]] + 1 _nums1.append(sum(nums1[prev_i:])) _nums2.append(sum(nums2[prev_j:])) print(_nums1) print(_nums2) n = len(_nums1) ans = 0 for i in range(n): ans += max(_nums1[i], _nums2[i]) return ans % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
You are given two sorted arrays of distinct integers nums1 and nums2. A valid path is defined as follows: Choose array nums1 or nums2 to traverse (from index-0). Traverse the current array from left to right. If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path). Score is defined as the sum of uniques values in a valid path. Return the maximum score you can obtain of all possible valid paths. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9] Output: 30 Explanation: Valid paths: [2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1) [4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2) The maximum is obtained with the path in green [2,4,6,8,10]. Example 2: Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100] Output: 109 Explanation: Maximum sum is obtained with the path [1,3,5,100]. Example 3: Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10] Output: 40 Explanation: There are no common elements between nums1 and nums2. Maximum sum is obtained with the path [6,7,8,9,10]. Example 4: Input: nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12] Output: 61   Constraints: 1 <= nums1.length <= 10^5 1 <= nums2.length <= 10^5 1 <= nums1[i], nums2[i] <= 10^7 nums1 and nums2 are strictly increasing.
class Solution: def maxSum(self, nums1: List[int], nums2: List[int]) -> int: l1, l2 = len(nums1), len(nums2) g = defaultdict(set) for i in range(1, l1): g[nums1[i - 1]].add(nums1[i]) for i in range(1, l2): g[nums2[i - 1]].add(nums2[i]) @lru_cache(None) def dfs(n): x = 0 for i in g[n]: x = max(x, dfs(i)) return n + x res = max(dfs(nums1[0]), dfs(nums2[0])) return res % 1000000007
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN BIN_OP VAR VAR FUNC_CALL VAR NONE ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER RETURN BIN_OP VAR NUMBER VAR
You are given two sorted arrays of distinct integers nums1 and nums2. A valid path is defined as follows: Choose array nums1 or nums2 to traverse (from index-0). Traverse the current array from left to right. If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path). Score is defined as the sum of uniques values in a valid path. Return the maximum score you can obtain of all possible valid paths. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9] Output: 30 Explanation: Valid paths: [2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1) [4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2) The maximum is obtained with the path in green [2,4,6,8,10]. Example 2: Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100] Output: 109 Explanation: Maximum sum is obtained with the path [1,3,5,100]. Example 3: Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10] Output: 40 Explanation: There are no common elements between nums1 and nums2. Maximum sum is obtained with the path [6,7,8,9,10]. Example 4: Input: nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12] Output: 61   Constraints: 1 <= nums1.length <= 10^5 1 <= nums2.length <= 10^5 1 <= nums1[i], nums2[i] <= 10^7 nums1 and nums2 are strictly increasing.
class Solution: def binary_search(self, arr, low, high, x): if high >= low: mid = (high + low) // 2 if arr[mid] == x: return mid elif arr[mid] > x: return self.binary_search(arr, low, mid - 1, x) else: return self.binary_search(arr, mid + 1, high, x) else: return -1 def maxSum(self, nums1: List[int], nums2: List[int]) -> int: modulo = 1000000007 num1Sum = 0 maxSum = 0 num2sum = 0 startIndex = 0 len2 = len(nums2) for num1 in nums1: num1InNum2 = self.binary_search(nums2, startIndex, len2 - 1, num1) if num1InNum2 != -1: num2sum = sum(nums2[startIndex:num1InNum2]) maxSum += num1Sum if num1Sum > num2sum else num2sum maxSum += num1 num2sum = num1Sum = 0 startIndex = num1InNum2 + 1 else: num1Sum += num1 num2sum = sum(nums2[startIndex:]) maxSum += num1Sum if num1Sum > num2sum else num2sum num2sum = num1Sum = 0 maxSum = maxSum % modulo return maxSum
CLASS_DEF FUNC_DEF IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR RETURN VAR IF VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN NUMBER FUNC_DEF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR RETURN VAR VAR
You are given two sorted arrays of distinct integers nums1 and nums2. A valid path is defined as follows: Choose array nums1 or nums2 to traverse (from index-0). Traverse the current array from left to right. If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path). Score is defined as the sum of uniques values in a valid path. Return the maximum score you can obtain of all possible valid paths. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9] Output: 30 Explanation: Valid paths: [2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1) [4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2) The maximum is obtained with the path in green [2,4,6,8,10]. Example 2: Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100] Output: 109 Explanation: Maximum sum is obtained with the path [1,3,5,100]. Example 3: Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10] Output: 40 Explanation: There are no common elements between nums1 and nums2. Maximum sum is obtained with the path [6,7,8,9,10]. Example 4: Input: nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12] Output: 61   Constraints: 1 <= nums1.length <= 10^5 1 <= nums2.length <= 10^5 1 <= nums1[i], nums2[i] <= 10^7 nums1 and nums2 are strictly increasing.
class Solution: def maxSum(self, A: List[int], B: List[int]) -> int: g = collections.defaultdict(list) for i in range(len(A) - 1): g[A[i]].append(A[i + 1]) for i in range(len(B) - 1): g[B[i]].append(B[i + 1]) g[A[-1]] g[B[-1]] dp = collections.defaultdict(int) for node in sorted(g, reverse=True): dp[node] = node if g[node]: dp[node] += max(dp[nei] for nei in g[node]) return max(dp.values()) % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR VAR VAR NUMBER EXPR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
You are given two sorted arrays of distinct integers nums1 and nums2. A valid path is defined as follows: Choose array nums1 or nums2 to traverse (from index-0). Traverse the current array from left to right. If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path). Score is defined as the sum of uniques values in a valid path. Return the maximum score you can obtain of all possible valid paths. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9] Output: 30 Explanation: Valid paths: [2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1) [4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2) The maximum is obtained with the path in green [2,4,6,8,10]. Example 2: Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100] Output: 109 Explanation: Maximum sum is obtained with the path [1,3,5,100]. Example 3: Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10] Output: 40 Explanation: There are no common elements between nums1 and nums2. Maximum sum is obtained with the path [6,7,8,9,10]. Example 4: Input: nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12] Output: 61   Constraints: 1 <= nums1.length <= 10^5 1 <= nums2.length <= 10^5 1 <= nums1[i], nums2[i] <= 10^7 nums1 and nums2 are strictly increasing.
class Solution: def maxSum(self, nums1: List[int], nums2: List[int]) -> int: self.num1_idx = {x: i for i, x in enumerate(nums1)} self.num2_idx = {x: i for i, x in enumerate(nums2)} self.memo = {} s1 = self.helper(nums1, nums2, nums1[0]) s2 = self.helper(nums1, nums2, nums2[0]) return max(s1, s2) % (10**9 + 7) def helper(self, nums1, nums2, val): if val in self.memo: return self.memo[val] ans1, ans2 = 0, 0 if val in self.num1_idx: idx = self.num1_idx[val] if idx + 1 < len(nums1): ans1 = self.helper(nums1, nums2, nums1[idx + 1]) if val in self.num2_idx: idx = self.num2_idx[val] if idx + 1 < len(nums2): ans2 = self.helper(nums1, nums2, nums2[idx + 1]) res = max(ans1, ans2) + val self.memo[val] = res return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR FUNC_DEF IF VAR VAR RETURN VAR VAR ASSIGN VAR VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR
You are given two sorted arrays of distinct integers nums1 and nums2. A valid path is defined as follows: Choose array nums1 or nums2 to traverse (from index-0). Traverse the current array from left to right. If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path). Score is defined as the sum of uniques values in a valid path. Return the maximum score you can obtain of all possible valid paths. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9] Output: 30 Explanation: Valid paths: [2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1) [4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2) The maximum is obtained with the path in green [2,4,6,8,10]. Example 2: Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100] Output: 109 Explanation: Maximum sum is obtained with the path [1,3,5,100]. Example 3: Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10] Output: 40 Explanation: There are no common elements between nums1 and nums2. Maximum sum is obtained with the path [6,7,8,9,10]. Example 4: Input: nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12] Output: 61   Constraints: 1 <= nums1.length <= 10^5 1 <= nums2.length <= 10^5 1 <= nums1[i], nums2[i] <= 10^7 nums1 and nums2 are strictly increasing.
class Solution: def maxSum(self, nums1: List[int], nums2: List[int]) -> int: p1, p2, sum1, sum2, result = 0, 0, 0, 0, 0 while p1 < len(nums1) and p2 < len(nums2): if nums1[p1] == nums2[p2]: result += max(sum1, sum2) + nums1[p1] sum1, sum2 = 0, 0 p1, p2 = p1 + 1, p2 + 1 elif nums1[p1] < nums2[p2]: sum1 += nums1[p1] p1 += 1 else: sum2 += nums2[p2] p2 += 1 while p1 < len(nums1): sum1 += nums1[p1] p1 += 1 while p2 < len(nums2): sum2 += nums2[p2] p2 += 1 return (result + max(sum1, sum2)) % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
You are given two sorted arrays of distinct integers nums1 and nums2. A valid path is defined as follows: Choose array nums1 or nums2 to traverse (from index-0). Traverse the current array from left to right. If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path). Score is defined as the sum of uniques values in a valid path. Return the maximum score you can obtain of all possible valid paths. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9] Output: 30 Explanation: Valid paths: [2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1) [4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2) The maximum is obtained with the path in green [2,4,6,8,10]. Example 2: Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100] Output: 109 Explanation: Maximum sum is obtained with the path [1,3,5,100]. Example 3: Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10] Output: 40 Explanation: There are no common elements between nums1 and nums2. Maximum sum is obtained with the path [6,7,8,9,10]. Example 4: Input: nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12] Output: 61   Constraints: 1 <= nums1.length <= 10^5 1 <= nums2.length <= 10^5 1 <= nums1[i], nums2[i] <= 10^7 nums1 and nums2 are strictly increasing.
class Solution: def maxSum(self, nums1: List[int], nums2: List[int]) -> int: self.mod_value = pow(10, 9) + 7 n = len(nums1) m = len(nums2) i = 0 j = 0 format_nums1 = [] format_nums2 = [] while i < n and j < m: if nums1[i] == nums2[j]: if len(format_nums1) == 0 or format_nums1[-1][1] == 0: format_nums1.append([[0, 0], 1]) if len(format_nums2) == 0 or format_nums2[-1][1] == 0: format_nums2.append([[0, 0], 1]) format_nums1.append([[0, nums1[i]], 0]) format_nums2.append([[0, nums2[j]], 0]) i += 1 j += 1 elif nums1[i] < nums2[j]: if len(format_nums1) == 0 or format_nums1[-1][1] == 0: format_nums1.append([[0, nums1[i]], 1]) else: format_nums1[-1][0] = self.add_value(format_nums1[-1][0], nums1[i]) i += 1 else: if len(format_nums2) == 0 or format_nums2[-1][1] == 0: format_nums2.append([[0, nums2[j]], 1]) else: format_nums2[-1][0] = self.add_value(format_nums2[-1][0], nums2[j]) j += 1 while i < n: if len(format_nums1) == 0 or format_nums1[-1][1] == 0: format_nums1.append([[0, nums1[i]], 1]) else: format_nums1[-1][0] = self.add_value(format_nums1[-1][0], nums1[i]) i += 1 while j < m: if len(format_nums2) == 0 or format_nums2[-1][1] == 0: format_nums2.append([[0, nums2[j]], 1]) else: format_nums2[-1][0] = self.add_value(format_nums2[-1][0], nums2[j]) j += 1 if format_nums1[-1][1] == 0: format_nums1.append([[0, 0], 1]) if format_nums2[-1][1] == 0: format_nums2.append([[0, 0], 1]) max_value1 = [0, 0] max_value2 = [0, 0] n = len(format_nums1) for i in range(n): if format_nums1[i][1] == 0 or i == 0: max_value1 = self.add_value_pair(max_value1, format_nums1[i][0]) max_value2 = self.add_value_pair(max_value2, format_nums2[i][0]) else: temp1 = self.add_value_pair(max_value2, format_nums1[i][0]) temp2 = self.add_value_pair(max_value1, format_nums2[i][0]) max_value1 = self.max( self.add_value_pair(max_value1, format_nums1[i][0]), temp1 ) max_value2 = self.max( self.add_value_pair(max_value2, format_nums2[i][0]), temp2 ) return self.max(max_value1, max_value2)[1] def add_value(self, value_pair, value): value_pair[1] += value while value_pair[1] >= self.mod_value: value_pair[0] += 1 value_pair[1] -= self.mod_value return value_pair def add_value_pair(self, value_pair1, value_pair2): result = [value_pair1[0] + value_pair2[0], value_pair1[1] + value_pair2[1]] while result[1] >= self.mod_value: result[0] += 1 result[1] -= self.mod_value return result def max(self, value_pair1, value_pair2): if value_pair1[0] == value_pair2[0]: if value_pair1[1] > value_pair2[1]: return value_pair1 elif value_pair1[0] > value_pair2[0]: return value_pair1 return value_pair2
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST LIST NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST LIST NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST LIST NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR LIST LIST NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST LIST NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST LIST NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER WHILE VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST LIST NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER WHILE VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST LIST NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER IF VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST LIST NUMBER NUMBER NUMBER IF VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST LIST NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR RETURN FUNC_CALL VAR VAR VAR NUMBER VAR FUNC_DEF VAR NUMBER VAR WHILE VAR NUMBER VAR VAR NUMBER NUMBER VAR NUMBER VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER WHILE VAR NUMBER VAR VAR NUMBER NUMBER VAR NUMBER VAR RETURN VAR FUNC_DEF IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN VAR IF VAR NUMBER VAR NUMBER RETURN VAR RETURN VAR
You are given two sorted arrays of distinct integers nums1 and nums2. A valid path is defined as follows: Choose array nums1 or nums2 to traverse (from index-0). Traverse the current array from left to right. If you are reading any value that is present in nums1 and nums2 you are allowed to change your path to the other array. (Only one repeated value is considered in the valid path). Score is defined as the sum of uniques values in a valid path. Return the maximum score you can obtain of all possible valid paths. Since the answer may be too large, return it modulo 10^9 + 7.   Example 1: Input: nums1 = [2,4,5,8,10], nums2 = [4,6,8,9] Output: 30 Explanation: Valid paths: [2,4,5,8,10], [2,4,5,8,9], [2,4,6,8,9], [2,4,6,8,10], (starting from nums1) [4,6,8,9], [4,5,8,10], [4,5,8,9], [4,6,8,10] (starting from nums2) The maximum is obtained with the path in green [2,4,6,8,10]. Example 2: Input: nums1 = [1,3,5,7,9], nums2 = [3,5,100] Output: 109 Explanation: Maximum sum is obtained with the path [1,3,5,100]. Example 3: Input: nums1 = [1,2,3,4,5], nums2 = [6,7,8,9,10] Output: 40 Explanation: There are no common elements between nums1 and nums2. Maximum sum is obtained with the path [6,7,8,9,10]. Example 4: Input: nums1 = [1,4,5,8,9,11,19], nums2 = [2,3,4,11,12] Output: 61   Constraints: 1 <= nums1.length <= 10^5 1 <= nums2.length <= 10^5 1 <= nums1[i], nums2[i] <= 10^7 nums1 and nums2 are strictly increasing.
class Solution: def maxSum(self, nums1: List[int], nums2: List[int]) -> int: sum1 = 0 sum2 = 0 s1 = 0 s2 = 0 total = 0 l1 = len(nums1) l2 = len(nums2) while s1 < l1 and s2 < l2: if nums1[s1] < nums2[s2]: sum1 += nums1[s1] s1 += 1 elif nums1[s1] > nums2[s2]: sum2 += nums2[s2] s2 += 1 else: total += max(sum1, sum2) + nums1[s1] sum1 = sum2 = 0 s1 += 1 s2 += 1 if s1 < l1: while s1 < l1: sum1 += nums1[s1] s1 += 1 total += max(sum1, sum2) if s2 < l2: while s2 < l2: sum2 += nums2[s2] s2 += 1 total += max(sum1, sum2) return total % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR IF VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR