description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Jim has invented a new flying object called HZ42. HZ42 is like a broom and can only fly horizontally, independent of the environment. One day, Jim started his flight from Dubai's highest skyscraper, traveled some distance and landed on another skyscraper of same height! So much fun! But unfortunately, new skyscrapers have been built recently. Let us describe the problem in one dimensional space. We have in total $N$ skyscrapers aligned from left to right. The $\boldsymbol{i}$^{th} skyscraper has a height of $h_i$. A flying route can be described as $(i,j)$ with $i\neq j$, which means, Jim starts his HZ42 at the top of the skyscraper $\boldsymbol{i}$ and lands on the skyscraper $j$. Since HZ42 can only fly horizontally, Jim will remain at the height $h_i$ only. Thus the path $(i,j)$ can be valid, only if each of the skyscrapers $i,i+1,\ldots,j-1,j$ is not strictly greater than $h_i$ and if the height of the skyscraper he starts from and arrives on have the same height. Formally, $(i,j)$ is valid iff $\nexists k\in[i,j]:h_k>h_i$ and $h_i=h_j$. Help Jim in counting the number of valid paths represented by ordered pairs $(i,j)$. Input Format The first line contains $N$, the number of skyscrapers. The next line contains $N$ space separated integers representing the heights of the skyscrapers. Output Format Print an integer that denotes the number of valid routes. Constraints $1\leq N\leq3\cdot10^5$ and no skyscraper will have height greater than $10^{6}$ and less than $\mbox{1}$. Sample Input #00 6 3 2 1 2 3 3 Sample Output #00 8 Sample Input #01 3 1 1000 1 Sample Output #01 0 Explanation First testcase: (1, 5), (1, 6) (5, 6) and (2, 4) and the routes in the opposite directions are the only valid routes. Second testcase: (1, 3) and (3, 1) could have been valid, if there wasn't a big skyscraper with height 1000 between them.
N = int(input()) A = [int(x) for x in input().split()] height = [10**7] count = [1] paths = 0 for h in A: while h > height[-1]: height.pop() count.pop() if h == height[-1]: paths += count[-1] count[-1] += 1 else: height.append(h) count.append(1) print(2 * paths)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST BIN_OP NUMBER NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR VAR WHILE VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR
Jim has invented a new flying object called HZ42. HZ42 is like a broom and can only fly horizontally, independent of the environment. One day, Jim started his flight from Dubai's highest skyscraper, traveled some distance and landed on another skyscraper of same height! So much fun! But unfortunately, new skyscrapers have been built recently. Let us describe the problem in one dimensional space. We have in total $N$ skyscrapers aligned from left to right. The $\boldsymbol{i}$^{th} skyscraper has a height of $h_i$. A flying route can be described as $(i,j)$ with $i\neq j$, which means, Jim starts his HZ42 at the top of the skyscraper $\boldsymbol{i}$ and lands on the skyscraper $j$. Since HZ42 can only fly horizontally, Jim will remain at the height $h_i$ only. Thus the path $(i,j)$ can be valid, only if each of the skyscrapers $i,i+1,\ldots,j-1,j$ is not strictly greater than $h_i$ and if the height of the skyscraper he starts from and arrives on have the same height. Formally, $(i,j)$ is valid iff $\nexists k\in[i,j]:h_k>h_i$ and $h_i=h_j$. Help Jim in counting the number of valid paths represented by ordered pairs $(i,j)$. Input Format The first line contains $N$, the number of skyscrapers. The next line contains $N$ space separated integers representing the heights of the skyscrapers. Output Format Print an integer that denotes the number of valid routes. Constraints $1\leq N\leq3\cdot10^5$ and no skyscraper will have height greater than $10^{6}$ and less than $\mbox{1}$. Sample Input #00 6 3 2 1 2 3 3 Sample Output #00 8 Sample Input #01 3 1 1000 1 Sample Output #01 0 Explanation First testcase: (1, 5), (1, 6) (5, 6) and (2, 4) and the routes in the opposite directions are the only valid routes. Second testcase: (1, 3) and (3, 1) could have been valid, if there wasn't a big skyscraper with height 1000 between them.
import sys def run(hs): n = 0 cs = [] for h in hs: while cs and cs[-1][0] < h: cs.pop() if cs and cs[-1][0] == h: n += cs[-1][1] cs[-1][1] += 1 else: cs.append([h, 1]) return n * 2 def main(): n = input().strip().split(" ") hs = list(map(int, input().strip().split(" "))) print(run(hs)) main()
IMPORT FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR WHILE VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR IF VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER RETURN BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Jim has invented a new flying object called HZ42. HZ42 is like a broom and can only fly horizontally, independent of the environment. One day, Jim started his flight from Dubai's highest skyscraper, traveled some distance and landed on another skyscraper of same height! So much fun! But unfortunately, new skyscrapers have been built recently. Let us describe the problem in one dimensional space. We have in total $N$ skyscrapers aligned from left to right. The $\boldsymbol{i}$^{th} skyscraper has a height of $h_i$. A flying route can be described as $(i,j)$ with $i\neq j$, which means, Jim starts his HZ42 at the top of the skyscraper $\boldsymbol{i}$ and lands on the skyscraper $j$. Since HZ42 can only fly horizontally, Jim will remain at the height $h_i$ only. Thus the path $(i,j)$ can be valid, only if each of the skyscrapers $i,i+1,\ldots,j-1,j$ is not strictly greater than $h_i$ and if the height of the skyscraper he starts from and arrives on have the same height. Formally, $(i,j)$ is valid iff $\nexists k\in[i,j]:h_k>h_i$ and $h_i=h_j$. Help Jim in counting the number of valid paths represented by ordered pairs $(i,j)$. Input Format The first line contains $N$, the number of skyscrapers. The next line contains $N$ space separated integers representing the heights of the skyscrapers. Output Format Print an integer that denotes the number of valid routes. Constraints $1\leq N\leq3\cdot10^5$ and no skyscraper will have height greater than $10^{6}$ and less than $\mbox{1}$. Sample Input #00 6 3 2 1 2 3 3 Sample Output #00 8 Sample Input #01 3 1 1000 1 Sample Output #01 0 Explanation First testcase: (1, 5), (1, 6) (5, 6) and (2, 4) and the routes in the opposite directions are the only valid routes. Second testcase: (1, 3) and (3, 1) could have been valid, if there wasn't a big skyscraper with height 1000 between them.
import sys heights = [int(x) for x in sys.stdin.readlines()[1].split()] class Node: def __init__(self, value, frequency=1): self.value, self.frequency = value, frequency maxes, count = [], 0 for height in heights: while len(maxes) > 0 and maxes[-1].value < height: maxes.pop() if len(maxes) > 0 and maxes[-1].value == height: count += maxes[-1].frequency maxes[-1].frequency += 1 else: maxes.append(Node(height)) print(count * 2)
IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER CLASS_DEF FUNC_DEF NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR LIST NUMBER FOR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Jim has invented a new flying object called HZ42. HZ42 is like a broom and can only fly horizontally, independent of the environment. One day, Jim started his flight from Dubai's highest skyscraper, traveled some distance and landed on another skyscraper of same height! So much fun! But unfortunately, new skyscrapers have been built recently. Let us describe the problem in one dimensional space. We have in total $N$ skyscrapers aligned from left to right. The $\boldsymbol{i}$^{th} skyscraper has a height of $h_i$. A flying route can be described as $(i,j)$ with $i\neq j$, which means, Jim starts his HZ42 at the top of the skyscraper $\boldsymbol{i}$ and lands on the skyscraper $j$. Since HZ42 can only fly horizontally, Jim will remain at the height $h_i$ only. Thus the path $(i,j)$ can be valid, only if each of the skyscrapers $i,i+1,\ldots,j-1,j$ is not strictly greater than $h_i$ and if the height of the skyscraper he starts from and arrives on have the same height. Formally, $(i,j)$ is valid iff $\nexists k\in[i,j]:h_k>h_i$ and $h_i=h_j$. Help Jim in counting the number of valid paths represented by ordered pairs $(i,j)$. Input Format The first line contains $N$, the number of skyscrapers. The next line contains $N$ space separated integers representing the heights of the skyscrapers. Output Format Print an integer that denotes the number of valid routes. Constraints $1\leq N\leq3\cdot10^5$ and no skyscraper will have height greater than $10^{6}$ and less than $\mbox{1}$. Sample Input #00 6 3 2 1 2 3 3 Sample Output #00 8 Sample Input #01 3 1 1000 1 Sample Output #01 0 Explanation First testcase: (1, 5), (1, 6) (5, 6) and (2, 4) and the routes in the opposite directions are the only valid routes. Second testcase: (1, 3) and (3, 1) could have been valid, if there wasn't a big skyscraper with height 1000 between them.
N = int(input()) a = list(map(int, input().split())) res = 0 stack = [(a[0], 1)] for value in a[1:]: count = 1 while stack and value >= stack[-1][0]: prev_value, prev_count = stack.pop() if prev_value == value: count += prev_count else: res += prev_count * (prev_count - 1) // 2 stack.append((value, count)) for val, count in stack: res += count * (count - 1) // 2 print(2 * res)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST VAR NUMBER NUMBER FOR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR IF VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR
Jim has invented a new flying object called HZ42. HZ42 is like a broom and can only fly horizontally, independent of the environment. One day, Jim started his flight from Dubai's highest skyscraper, traveled some distance and landed on another skyscraper of same height! So much fun! But unfortunately, new skyscrapers have been built recently. Let us describe the problem in one dimensional space. We have in total $N$ skyscrapers aligned from left to right. The $\boldsymbol{i}$^{th} skyscraper has a height of $h_i$. A flying route can be described as $(i,j)$ with $i\neq j$, which means, Jim starts his HZ42 at the top of the skyscraper $\boldsymbol{i}$ and lands on the skyscraper $j$. Since HZ42 can only fly horizontally, Jim will remain at the height $h_i$ only. Thus the path $(i,j)$ can be valid, only if each of the skyscrapers $i,i+1,\ldots,j-1,j$ is not strictly greater than $h_i$ and if the height of the skyscraper he starts from and arrives on have the same height. Formally, $(i,j)$ is valid iff $\nexists k\in[i,j]:h_k>h_i$ and $h_i=h_j$. Help Jim in counting the number of valid paths represented by ordered pairs $(i,j)$. Input Format The first line contains $N$, the number of skyscrapers. The next line contains $N$ space separated integers representing the heights of the skyscrapers. Output Format Print an integer that denotes the number of valid routes. Constraints $1\leq N\leq3\cdot10^5$ and no skyscraper will have height greater than $10^{6}$ and less than $\mbox{1}$. Sample Input #00 6 3 2 1 2 3 3 Sample Output #00 8 Sample Input #01 3 1 1000 1 Sample Output #01 0 Explanation First testcase: (1, 5), (1, 6) (5, 6) and (2, 4) and the routes in the opposite directions are the only valid routes. Second testcase: (1, 3) and (3, 1) could have been valid, if there wasn't a big skyscraper with height 1000 between them.
READ_FROM_FILE = False if READ_FROM_FILE: inp = open("input.txt").readline else: inp = input n = int(inp().strip()) skyscrapers = list(map(int, inp().strip().split())) paths_count = 0 size = 0 heights = [0] * n amounts = [0] * n for skyscraper in skyscrapers: while size > 0 and heights[size - 1] < skyscraper: size -= 1 if size < 1 or heights[size - 1] > skyscraper: heights[size] = skyscraper amounts[size] = 0 size += 1 paths_count += amounts[size - 1] amounts[size - 1] += 1 print(paths_count * 2)
ASSIGN VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN 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 NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR WHILE VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Jim has invented a new flying object called HZ42. HZ42 is like a broom and can only fly horizontally, independent of the environment. One day, Jim started his flight from Dubai's highest skyscraper, traveled some distance and landed on another skyscraper of same height! So much fun! But unfortunately, new skyscrapers have been built recently. Let us describe the problem in one dimensional space. We have in total $N$ skyscrapers aligned from left to right. The $\boldsymbol{i}$^{th} skyscraper has a height of $h_i$. A flying route can be described as $(i,j)$ with $i\neq j$, which means, Jim starts his HZ42 at the top of the skyscraper $\boldsymbol{i}$ and lands on the skyscraper $j$. Since HZ42 can only fly horizontally, Jim will remain at the height $h_i$ only. Thus the path $(i,j)$ can be valid, only if each of the skyscrapers $i,i+1,\ldots,j-1,j$ is not strictly greater than $h_i$ and if the height of the skyscraper he starts from and arrives on have the same height. Formally, $(i,j)$ is valid iff $\nexists k\in[i,j]:h_k>h_i$ and $h_i=h_j$. Help Jim in counting the number of valid paths represented by ordered pairs $(i,j)$. Input Format The first line contains $N$, the number of skyscrapers. The next line contains $N$ space separated integers representing the heights of the skyscrapers. Output Format Print an integer that denotes the number of valid routes. Constraints $1\leq N\leq3\cdot10^5$ and no skyscraper will have height greater than $10^{6}$ and less than $\mbox{1}$. Sample Input #00 6 3 2 1 2 3 3 Sample Output #00 8 Sample Input #01 3 1 1000 1 Sample Output #01 0 Explanation First testcase: (1, 5), (1, 6) (5, 6) and (2, 4) and the routes in the opposite directions are the only valid routes. Second testcase: (1, 3) and (3, 1) could have been valid, if there wasn't a big skyscraper with height 1000 between them.
L = {} lines, nums, types = input(), list(map(int, input().split(" "))), [] for i in nums: if not i in L.keys(): L[i] = [0] L[i][-1] += 1 while len(types) != 0 and types[-1] < i: L[types[-1]] += [0] del types[-1] if len(types) == 0 or i < types[-1]: types += [i] total = 0 for i in L.keys(): total += int(sum(map(lambda x: x * (x - 1) / 2, L[i]))) * 2 print(total)
ASSIGN VAR DICT ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING LIST FOR VAR VAR IF VAR FUNC_CALL VAR ASSIGN VAR VAR LIST NUMBER VAR VAR NUMBER NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER LIST NUMBER VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR LIST VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Jim has invented a new flying object called HZ42. HZ42 is like a broom and can only fly horizontally, independent of the environment. One day, Jim started his flight from Dubai's highest skyscraper, traveled some distance and landed on another skyscraper of same height! So much fun! But unfortunately, new skyscrapers have been built recently. Let us describe the problem in one dimensional space. We have in total $N$ skyscrapers aligned from left to right. The $\boldsymbol{i}$^{th} skyscraper has a height of $h_i$. A flying route can be described as $(i,j)$ with $i\neq j$, which means, Jim starts his HZ42 at the top of the skyscraper $\boldsymbol{i}$ and lands on the skyscraper $j$. Since HZ42 can only fly horizontally, Jim will remain at the height $h_i$ only. Thus the path $(i,j)$ can be valid, only if each of the skyscrapers $i,i+1,\ldots,j-1,j$ is not strictly greater than $h_i$ and if the height of the skyscraper he starts from and arrives on have the same height. Formally, $(i,j)$ is valid iff $\nexists k\in[i,j]:h_k>h_i$ and $h_i=h_j$. Help Jim in counting the number of valid paths represented by ordered pairs $(i,j)$. Input Format The first line contains $N$, the number of skyscrapers. The next line contains $N$ space separated integers representing the heights of the skyscrapers. Output Format Print an integer that denotes the number of valid routes. Constraints $1\leq N\leq3\cdot10^5$ and no skyscraper will have height greater than $10^{6}$ and less than $\mbox{1}$. Sample Input #00 6 3 2 1 2 3 3 Sample Output #00 8 Sample Input #01 3 1 1000 1 Sample Output #01 0 Explanation First testcase: (1, 5), (1, 6) (5, 6) and (2, 4) and the routes in the opposite directions are the only valid routes. Second testcase: (1, 3) and (3, 1) could have been valid, if there wasn't a big skyscraper with height 1000 between them.
class Stack: def __init__(self): self.items = [] def isEmpty(self): return self.items == [] def push(self, item): self.items.append(item) def pop(self): return self.items.pop() def peek(self): return self.items[len(self.items) - 1] def size(self): return len(self.items) class Node: def __init__(self, data): self.data = data self.count = 1 n = int(input().strip()) stack = Stack() arr = list(map(int, input().strip().split(" "))) result = 0 for i in arr: while stack.size() > 0 and stack.peek().data < i: stack.pop() if stack.size() == 0 or stack.peek().data > i: stack.push(Node(i)) else: tail = stack.peek() result += tail.count tail.count += 1 print(result * 2)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF RETURN VAR LIST FUNC_DEF EXPR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF RETURN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR VAR WHILE FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Jim has invented a new flying object called HZ42. HZ42 is like a broom and can only fly horizontally, independent of the environment. One day, Jim started his flight from Dubai's highest skyscraper, traveled some distance and landed on another skyscraper of same height! So much fun! But unfortunately, new skyscrapers have been built recently. Let us describe the problem in one dimensional space. We have in total $N$ skyscrapers aligned from left to right. The $\boldsymbol{i}$^{th} skyscraper has a height of $h_i$. A flying route can be described as $(i,j)$ with $i\neq j$, which means, Jim starts his HZ42 at the top of the skyscraper $\boldsymbol{i}$ and lands on the skyscraper $j$. Since HZ42 can only fly horizontally, Jim will remain at the height $h_i$ only. Thus the path $(i,j)$ can be valid, only if each of the skyscrapers $i,i+1,\ldots,j-1,j$ is not strictly greater than $h_i$ and if the height of the skyscraper he starts from and arrives on have the same height. Formally, $(i,j)$ is valid iff $\nexists k\in[i,j]:h_k>h_i$ and $h_i=h_j$. Help Jim in counting the number of valid paths represented by ordered pairs $(i,j)$. Input Format The first line contains $N$, the number of skyscrapers. The next line contains $N$ space separated integers representing the heights of the skyscrapers. Output Format Print an integer that denotes the number of valid routes. Constraints $1\leq N\leq3\cdot10^5$ and no skyscraper will have height greater than $10^{6}$ and less than $\mbox{1}$. Sample Input #00 6 3 2 1 2 3 3 Sample Output #00 8 Sample Input #01 3 1 1000 1 Sample Output #01 0 Explanation First testcase: (1, 5), (1, 6) (5, 6) and (2, 4) and the routes in the opposite directions are the only valid routes. Second testcase: (1, 3) and (3, 1) could have been valid, if there wasn't a big skyscraper with height 1000 between them.
n = int(input()) heights = list(map(int, input().split(" "))) answer = 0 routes = [] cur_heights = {} for a in heights: if len(routes) == 0: routes.append(a) cur_heights[a] = 1 elif routes[0] >= a: while routes[-1] < a: cur_heights[routes.pop()] -= 1 routes.append(a) if a in cur_heights: answer += cur_heights[a] cur_heights[a] += 1 else: cur_heights[a] = 1 else: routes = [a] cur_heights = {a: 1} print(2 * answer)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR WHILE VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST VAR ASSIGN VAR DICT VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR
Jim has invented a new flying object called HZ42. HZ42 is like a broom and can only fly horizontally, independent of the environment. One day, Jim started his flight from Dubai's highest skyscraper, traveled some distance and landed on another skyscraper of same height! So much fun! But unfortunately, new skyscrapers have been built recently. Let us describe the problem in one dimensional space. We have in total $N$ skyscrapers aligned from left to right. The $\boldsymbol{i}$^{th} skyscraper has a height of $h_i$. A flying route can be described as $(i,j)$ with $i\neq j$, which means, Jim starts his HZ42 at the top of the skyscraper $\boldsymbol{i}$ and lands on the skyscraper $j$. Since HZ42 can only fly horizontally, Jim will remain at the height $h_i$ only. Thus the path $(i,j)$ can be valid, only if each of the skyscrapers $i,i+1,\ldots,j-1,j$ is not strictly greater than $h_i$ and if the height of the skyscraper he starts from and arrives on have the same height. Formally, $(i,j)$ is valid iff $\nexists k\in[i,j]:h_k>h_i$ and $h_i=h_j$. Help Jim in counting the number of valid paths represented by ordered pairs $(i,j)$. Input Format The first line contains $N$, the number of skyscrapers. The next line contains $N$ space separated integers representing the heights of the skyscrapers. Output Format Print an integer that denotes the number of valid routes. Constraints $1\leq N\leq3\cdot10^5$ and no skyscraper will have height greater than $10^{6}$ and less than $\mbox{1}$. Sample Input #00 6 3 2 1 2 3 3 Sample Output #00 8 Sample Input #01 3 1 1000 1 Sample Output #01 0 Explanation First testcase: (1, 5), (1, 6) (5, 6) and (2, 4) and the routes in the opposite directions are the only valid routes. Second testcase: (1, 3) and (3, 1) could have been valid, if there wasn't a big skyscraper with height 1000 between them.
N = int(input()) heights = [int(x) for x in input().split()] assert len(heights) == N stack = [] result = 0 for h in heights: while len(stack) > 0 and stack[-1][0] < h: stack.pop() if len(stack) > 0 and stack[-1][0] == h: result += stack[-1][1] stack[-1] = stack[-1][0], stack[-1][1] + 1 else: stack.append((h, 1)) print(2 * result)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR
Jim has invented a new flying object called HZ42. HZ42 is like a broom and can only fly horizontally, independent of the environment. One day, Jim started his flight from Dubai's highest skyscraper, traveled some distance and landed on another skyscraper of same height! So much fun! But unfortunately, new skyscrapers have been built recently. Let us describe the problem in one dimensional space. We have in total $N$ skyscrapers aligned from left to right. The $\boldsymbol{i}$^{th} skyscraper has a height of $h_i$. A flying route can be described as $(i,j)$ with $i\neq j$, which means, Jim starts his HZ42 at the top of the skyscraper $\boldsymbol{i}$ and lands on the skyscraper $j$. Since HZ42 can only fly horizontally, Jim will remain at the height $h_i$ only. Thus the path $(i,j)$ can be valid, only if each of the skyscrapers $i,i+1,\ldots,j-1,j$ is not strictly greater than $h_i$ and if the height of the skyscraper he starts from and arrives on have the same height. Formally, $(i,j)$ is valid iff $\nexists k\in[i,j]:h_k>h_i$ and $h_i=h_j$. Help Jim in counting the number of valid paths represented by ordered pairs $(i,j)$. Input Format The first line contains $N$, the number of skyscrapers. The next line contains $N$ space separated integers representing the heights of the skyscrapers. Output Format Print an integer that denotes the number of valid routes. Constraints $1\leq N\leq3\cdot10^5$ and no skyscraper will have height greater than $10^{6}$ and less than $\mbox{1}$. Sample Input #00 6 3 2 1 2 3 3 Sample Output #00 8 Sample Input #01 3 1 1000 1 Sample Output #01 0 Explanation First testcase: (1, 5), (1, 6) (5, 6) and (2, 4) and the routes in the opposite directions are the only valid routes. Second testcase: (1, 3) and (3, 1) could have been valid, if there wasn't a big skyscraper with height 1000 between them.
class Node: def __init__(self, height=None, count=1, nodeAbove=None): self.h = height self.n = count self.up = nodeAbove class ArrLinkedLists: def __init__(self): self.arr = [None] * 1001 self.lowest = 1001 def eat(self, h): iArr = h // 1000 if iArr < self.lowest: self.lowest = iArr self.arr[iArr] = Node(h) return 0 partial = 0 while self.lowest < iArr: node = self.arr[self.lowest] self.arr[self.lowest] = None while node: partial += node.n * (node.n - 1) node = node.up self.lowest += 1 node = self.arr[iArr] try: while node.h < h: partial += node.n * (node.n - 1) node = node.up except AttributeError: self.arr[iArr] = Node(h) else: if node.h > h: self.arr[iArr] = Node(h, 1, node) else: node.n += 1 self.arr[iArr] = node return partial ll = ArrLinkedLists() nRoutes = 0 n = int(input().strip()) for h in map(int, input().split()): nRoutes += ll.eat(h) nRoutes += ll.eat(1000001) print(nRoutes)
CLASS_DEF FUNC_DEF NONE NUMBER NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NONE NUMBER ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NONE WHILE VAR VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR
Jim has invented a new flying object called HZ42. HZ42 is like a broom and can only fly horizontally, independent of the environment. One day, Jim started his flight from Dubai's highest skyscraper, traveled some distance and landed on another skyscraper of same height! So much fun! But unfortunately, new skyscrapers have been built recently. Let us describe the problem in one dimensional space. We have in total $N$ skyscrapers aligned from left to right. The $\boldsymbol{i}$^{th} skyscraper has a height of $h_i$. A flying route can be described as $(i,j)$ with $i\neq j$, which means, Jim starts his HZ42 at the top of the skyscraper $\boldsymbol{i}$ and lands on the skyscraper $j$. Since HZ42 can only fly horizontally, Jim will remain at the height $h_i$ only. Thus the path $(i,j)$ can be valid, only if each of the skyscrapers $i,i+1,\ldots,j-1,j$ is not strictly greater than $h_i$ and if the height of the skyscraper he starts from and arrives on have the same height. Formally, $(i,j)$ is valid iff $\nexists k\in[i,j]:h_k>h_i$ and $h_i=h_j$. Help Jim in counting the number of valid paths represented by ordered pairs $(i,j)$. Input Format The first line contains $N$, the number of skyscrapers. The next line contains $N$ space separated integers representing the heights of the skyscrapers. Output Format Print an integer that denotes the number of valid routes. Constraints $1\leq N\leq3\cdot10^5$ and no skyscraper will have height greater than $10^{6}$ and less than $\mbox{1}$. Sample Input #00 6 3 2 1 2 3 3 Sample Output #00 8 Sample Input #01 3 1 1000 1 Sample Output #01 0 Explanation First testcase: (1, 5), (1, 6) (5, 6) and (2, 4) and the routes in the opposite directions are the only valid routes. Second testcase: (1, 3) and (3, 1) could have been valid, if there wasn't a big skyscraper with height 1000 between them.
def getans(a, v): le = -1 lc = 0 ans = 0 while len(a) > 0 and a[-1] < v: e = a.pop() if e == le: lc += 1 else: if lc > 1: ans += lc * (lc - 1) le = e lc = 1 if lc > 1: ans += lc * (lc - 1) return ans n = int(input()) l = list(map(int, input().split(" "))) a = [] ans = 0 for v in l: ans += getans(a, v) a.append(v) ans += getans(a, 10000000) print(ans)
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Jim has invented a new flying object called HZ42. HZ42 is like a broom and can only fly horizontally, independent of the environment. One day, Jim started his flight from Dubai's highest skyscraper, traveled some distance and landed on another skyscraper of same height! So much fun! But unfortunately, new skyscrapers have been built recently. Let us describe the problem in one dimensional space. We have in total $N$ skyscrapers aligned from left to right. The $\boldsymbol{i}$^{th} skyscraper has a height of $h_i$. A flying route can be described as $(i,j)$ with $i\neq j$, which means, Jim starts his HZ42 at the top of the skyscraper $\boldsymbol{i}$ and lands on the skyscraper $j$. Since HZ42 can only fly horizontally, Jim will remain at the height $h_i$ only. Thus the path $(i,j)$ can be valid, only if each of the skyscrapers $i,i+1,\ldots,j-1,j$ is not strictly greater than $h_i$ and if the height of the skyscraper he starts from and arrives on have the same height. Formally, $(i,j)$ is valid iff $\nexists k\in[i,j]:h_k>h_i$ and $h_i=h_j$. Help Jim in counting the number of valid paths represented by ordered pairs $(i,j)$. Input Format The first line contains $N$, the number of skyscrapers. The next line contains $N$ space separated integers representing the heights of the skyscrapers. Output Format Print an integer that denotes the number of valid routes. Constraints $1\leq N\leq3\cdot10^5$ and no skyscraper will have height greater than $10^{6}$ and less than $\mbox{1}$. Sample Input #00 6 3 2 1 2 3 3 Sample Output #00 8 Sample Input #01 3 1 1000 1 Sample Output #01 0 Explanation First testcase: (1, 5), (1, 6) (5, 6) and (2, 4) and the routes in the opposite directions are the only valid routes. Second testcase: (1, 3) and (3, 1) could have been valid, if there wasn't a big skyscraper with height 1000 between them.
N = int(input().strip()) scrapers = [int(num) for num in input().strip().split()] def unwind(stack, scraper): flights = 0 current = stack.pop() times = 1 while len(stack) > 0: if stack[-1] >= scraper: flights += times * (times - 1) stack.append(scraper) return flights if stack[-1] > current: flights += times * (times - 1) current = stack.pop() times = 1 continue times += 1 stack.pop() flights += times * (times - 1) stack.append(scraper) return flights stack = [] flights = 0 for scraper in scrapers: if len(stack) == 0: stack.append(scraper) continue if scraper <= stack[-1]: stack.append(scraper) continue flights += unwind(stack, scraper) flights += unwind(stack, 1000000.0 + 1) print(flights)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR IF VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Jim has invented a new flying object called HZ42. HZ42 is like a broom and can only fly horizontally, independent of the environment. One day, Jim started his flight from Dubai's highest skyscraper, traveled some distance and landed on another skyscraper of same height! So much fun! But unfortunately, new skyscrapers have been built recently. Let us describe the problem in one dimensional space. We have in total $N$ skyscrapers aligned from left to right. The $\boldsymbol{i}$^{th} skyscraper has a height of $h_i$. A flying route can be described as $(i,j)$ with $i\neq j$, which means, Jim starts his HZ42 at the top of the skyscraper $\boldsymbol{i}$ and lands on the skyscraper $j$. Since HZ42 can only fly horizontally, Jim will remain at the height $h_i$ only. Thus the path $(i,j)$ can be valid, only if each of the skyscrapers $i,i+1,\ldots,j-1,j$ is not strictly greater than $h_i$ and if the height of the skyscraper he starts from and arrives on have the same height. Formally, $(i,j)$ is valid iff $\nexists k\in[i,j]:h_k>h_i$ and $h_i=h_j$. Help Jim in counting the number of valid paths represented by ordered pairs $(i,j)$. Input Format The first line contains $N$, the number of skyscrapers. The next line contains $N$ space separated integers representing the heights of the skyscrapers. Output Format Print an integer that denotes the number of valid routes. Constraints $1\leq N\leq3\cdot10^5$ and no skyscraper will have height greater than $10^{6}$ and less than $\mbox{1}$. Sample Input #00 6 3 2 1 2 3 3 Sample Output #00 8 Sample Input #01 3 1 1000 1 Sample Output #01 0 Explanation First testcase: (1, 5), (1, 6) (5, 6) and (2, 4) and the routes in the opposite directions are the only valid routes. Second testcase: (1, 3) and (3, 1) could have been valid, if there wasn't a big skyscraper with height 1000 between them.
input() heights = list(map(int, input().split())) hs = [9**9] * 300000 ns = [0] * 300000 i = 0 count = 0 for height in heights: while height > hs[i]: i -= 1 if height < hs[i]: i += 1 hs[i] = height ns[i] = 1 else: count += ns[i] ns[i] += 1 print(2 * count)
EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR WHILE VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR
Jim has invented a new flying object called HZ42. HZ42 is like a broom and can only fly horizontally, independent of the environment. One day, Jim started his flight from Dubai's highest skyscraper, traveled some distance and landed on another skyscraper of same height! So much fun! But unfortunately, new skyscrapers have been built recently. Let us describe the problem in one dimensional space. We have in total $N$ skyscrapers aligned from left to right. The $\boldsymbol{i}$^{th} skyscraper has a height of $h_i$. A flying route can be described as $(i,j)$ with $i\neq j$, which means, Jim starts his HZ42 at the top of the skyscraper $\boldsymbol{i}$ and lands on the skyscraper $j$. Since HZ42 can only fly horizontally, Jim will remain at the height $h_i$ only. Thus the path $(i,j)$ can be valid, only if each of the skyscrapers $i,i+1,\ldots,j-1,j$ is not strictly greater than $h_i$ and if the height of the skyscraper he starts from and arrives on have the same height. Formally, $(i,j)$ is valid iff $\nexists k\in[i,j]:h_k>h_i$ and $h_i=h_j$. Help Jim in counting the number of valid paths represented by ordered pairs $(i,j)$. Input Format The first line contains $N$, the number of skyscrapers. The next line contains $N$ space separated integers representing the heights of the skyscrapers. Output Format Print an integer that denotes the number of valid routes. Constraints $1\leq N\leq3\cdot10^5$ and no skyscraper will have height greater than $10^{6}$ and less than $\mbox{1}$. Sample Input #00 6 3 2 1 2 3 3 Sample Output #00 8 Sample Input #01 3 1 1000 1 Sample Output #01 0 Explanation First testcase: (1, 5), (1, 6) (5, 6) and (2, 4) and the routes in the opposite directions are the only valid routes. Second testcase: (1, 3) and (3, 1) could have been valid, if there wasn't a big skyscraper with height 1000 between them.
n = int(input()) height = list(map(int, input().split())) ans = 0 seen = [] times = {} for i in height: if len(seen) == 0: seen.append(i) times[i] = 1 elif seen[len(seen) - 1] == i: times[i] += 1 elif seen[len(seen) - 1] > i: seen.append(i) if i in times: times[i] += 1 else: times[i] = 1 else: while len(seen) != 0 and seen[len(seen) - 1] < i: temp = seen.pop() ans += times[temp] * (times[temp] - 1) times[temp] = 0 if len(seen) == 0: seen.append(i) elif seen[len(seen) - 1] != i: seen.append(i) if i in times: times[i] += 1 else: times[i] = 1 while len(seen) != 0: temp = seen.pop() ans += times[temp] * (times[temp] - 1) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Jim has invented a new flying object called HZ42. HZ42 is like a broom and can only fly horizontally, independent of the environment. One day, Jim started his flight from Dubai's highest skyscraper, traveled some distance and landed on another skyscraper of same height! So much fun! But unfortunately, new skyscrapers have been built recently. Let us describe the problem in one dimensional space. We have in total $N$ skyscrapers aligned from left to right. The $\boldsymbol{i}$^{th} skyscraper has a height of $h_i$. A flying route can be described as $(i,j)$ with $i\neq j$, which means, Jim starts his HZ42 at the top of the skyscraper $\boldsymbol{i}$ and lands on the skyscraper $j$. Since HZ42 can only fly horizontally, Jim will remain at the height $h_i$ only. Thus the path $(i,j)$ can be valid, only if each of the skyscrapers $i,i+1,\ldots,j-1,j$ is not strictly greater than $h_i$ and if the height of the skyscraper he starts from and arrives on have the same height. Formally, $(i,j)$ is valid iff $\nexists k\in[i,j]:h_k>h_i$ and $h_i=h_j$. Help Jim in counting the number of valid paths represented by ordered pairs $(i,j)$. Input Format The first line contains $N$, the number of skyscrapers. The next line contains $N$ space separated integers representing the heights of the skyscrapers. Output Format Print an integer that denotes the number of valid routes. Constraints $1\leq N\leq3\cdot10^5$ and no skyscraper will have height greater than $10^{6}$ and less than $\mbox{1}$. Sample Input #00 6 3 2 1 2 3 3 Sample Output #00 8 Sample Input #01 3 1 1000 1 Sample Output #01 0 Explanation First testcase: (1, 5), (1, 6) (5, 6) and (2, 4) and the routes in the opposite directions are the only valid routes. Second testcase: (1, 3) and (3, 1) could have been valid, if there wasn't a big skyscraper with height 1000 between them.
class Interval: def __init__(self, start, end, number, count): self.start = start self.end = end self.number = number self.count = count self.parent = None self.left = None self.right = None def __str__(self): return "({0}, {1}, {2}, {3})".format( self.start, self.end, self.number, self.count ) class IntervalsManager: def __init__(self, n): self.intervals = [None for i in range(0, n)] self.n = n self.counter = 0 def insert_point(self, index, number): if index > 0 and self.intervals[index - 1] is not None: interval = self.intervals[index - 1] self.intervals[index] = interval interval.end = index if interval.number == number: self.counter = self.counter + 2 * interval.count interval.count = interval.count + 1 else: interval.number = number interval.count = 1 else: self.intervals[index] = Interval(index, index, number, 1) if index < self.n - 1 and self.intervals[index + 1] is not None: interval = self.intervals[index] interval_right = self.intervals[index + 1] interval.end = interval_right.end self.intervals[interval_right.end] = interval def print(self): for i in range(0, n): print(self.intervals[i]) n = int(input()) array = [int(x) for x in input().split()] custom_array = sorted([(n, i) for i, n in enumerate(array)]) intervalsManager = IntervalsManager(n) for i in range(0, n): number, index = custom_array[i] intervalsManager.insert_point(index, number) print(intervalsManager.counter)
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR NONE FUNC_DEF RETURN FUNC_CALL STRING VAR VAR VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR NONE VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR NUMBER VAR BIN_OP VAR NUMBER NONE ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NONE ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Jim has invented a new flying object called HZ42. HZ42 is like a broom and can only fly horizontally, independent of the environment. One day, Jim started his flight from Dubai's highest skyscraper, traveled some distance and landed on another skyscraper of same height! So much fun! But unfortunately, new skyscrapers have been built recently. Let us describe the problem in one dimensional space. We have in total $N$ skyscrapers aligned from left to right. The $\boldsymbol{i}$^{th} skyscraper has a height of $h_i$. A flying route can be described as $(i,j)$ with $i\neq j$, which means, Jim starts his HZ42 at the top of the skyscraper $\boldsymbol{i}$ and lands on the skyscraper $j$. Since HZ42 can only fly horizontally, Jim will remain at the height $h_i$ only. Thus the path $(i,j)$ can be valid, only if each of the skyscrapers $i,i+1,\ldots,j-1,j$ is not strictly greater than $h_i$ and if the height of the skyscraper he starts from and arrives on have the same height. Formally, $(i,j)$ is valid iff $\nexists k\in[i,j]:h_k>h_i$ and $h_i=h_j$. Help Jim in counting the number of valid paths represented by ordered pairs $(i,j)$. Input Format The first line contains $N$, the number of skyscrapers. The next line contains $N$ space separated integers representing the heights of the skyscrapers. Output Format Print an integer that denotes the number of valid routes. Constraints $1\leq N\leq3\cdot10^5$ and no skyscraper will have height greater than $10^{6}$ and less than $\mbox{1}$. Sample Input #00 6 3 2 1 2 3 3 Sample Output #00 8 Sample Input #01 3 1 1000 1 Sample Output #01 0 Explanation First testcase: (1, 5), (1, 6) (5, 6) and (2, 4) and the routes in the opposite directions are the only valid routes. Second testcase: (1, 3) and (3, 1) could have been valid, if there wasn't a big skyscraper with height 1000 between them.
import sys def getPathsNum(heights): current_result = {"num": 1, "path_num": 0, "pos": 0} result_stack = [] i = 1 while i < len(heights): if heights[i] == heights[current_result["pos"]]: current_result["num"] = current_result["num"] + 1 i = i + 1 elif heights[i] < heights[current_result["pos"]]: result_stack.append(current_result) current_result = {"num": 1, "path_num": 0, "pos": i} i = i + 1 elif len(result_stack): new_result = result_stack.pop() new_result["path_num"] = ( new_result["path_num"] + current_result["path_num"] + pow(current_result["num"], 2) - current_result["num"] ) current_result = new_result else: current_result["path_num"] = ( current_result["path_num"] + pow(current_result["num"], 2) - current_result["num"] ) current_result["pos"] = i current_result["num"] = 1 i = i + 1 path_num = ( current_result["path_num"] + pow(current_result["num"], 2) - current_result["num"] ) for result in result_stack: path_num = path_num + result["path_num"] + pow(result["num"], 2) - result["num"] return path_num sys.stdin.readline() print(getPathsNum(list(map(int, sys.stdin.readline().split()))))
IMPORT FUNC_DEF ASSIGN VAR DICT STRING STRING STRING NUMBER NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR STRING ASSIGN VAR STRING BIN_OP VAR STRING NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR DICT STRING STRING STRING NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING BIN_OP BIN_OP BIN_OP VAR STRING VAR STRING FUNC_CALL VAR VAR STRING NUMBER VAR STRING ASSIGN VAR VAR ASSIGN VAR STRING BIN_OP BIN_OP VAR STRING FUNC_CALL VAR VAR STRING NUMBER VAR STRING ASSIGN VAR STRING VAR ASSIGN VAR STRING NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR STRING FUNC_CALL VAR VAR STRING NUMBER VAR STRING FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR STRING FUNC_CALL VAR VAR STRING NUMBER VAR STRING RETURN VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR
Jim has invented a new flying object called HZ42. HZ42 is like a broom and can only fly horizontally, independent of the environment. One day, Jim started his flight from Dubai's highest skyscraper, traveled some distance and landed on another skyscraper of same height! So much fun! But unfortunately, new skyscrapers have been built recently. Let us describe the problem in one dimensional space. We have in total $N$ skyscrapers aligned from left to right. The $\boldsymbol{i}$^{th} skyscraper has a height of $h_i$. A flying route can be described as $(i,j)$ with $i\neq j$, which means, Jim starts his HZ42 at the top of the skyscraper $\boldsymbol{i}$ and lands on the skyscraper $j$. Since HZ42 can only fly horizontally, Jim will remain at the height $h_i$ only. Thus the path $(i,j)$ can be valid, only if each of the skyscrapers $i,i+1,\ldots,j-1,j$ is not strictly greater than $h_i$ and if the height of the skyscraper he starts from and arrives on have the same height. Formally, $(i,j)$ is valid iff $\nexists k\in[i,j]:h_k>h_i$ and $h_i=h_j$. Help Jim in counting the number of valid paths represented by ordered pairs $(i,j)$. Input Format The first line contains $N$, the number of skyscrapers. The next line contains $N$ space separated integers representing the heights of the skyscrapers. Output Format Print an integer that denotes the number of valid routes. Constraints $1\leq N\leq3\cdot10^5$ and no skyscraper will have height greater than $10^{6}$ and less than $\mbox{1}$. Sample Input #00 6 3 2 1 2 3 3 Sample Output #00 8 Sample Input #01 3 1 1000 1 Sample Output #01 0 Explanation First testcase: (1, 5), (1, 6) (5, 6) and (2, 4) and the routes in the opposite directions are the only valid routes. Second testcase: (1, 3) and (3, 1) could have been valid, if there wasn't a big skyscraper with height 1000 between them.
def countTot(di): count = 0 for k, v in di.items(): count += v * (v - 1) return count n = int(input()) arr = list(map(int, input().strip().split(" "))) stack = [] di = {} tot = 0 for a in arr: while len(stack) > 0 and stack[-1] < a: z = stack.pop() if z not in di: di[z] = 0 di[z] += 1 tot += countTot(di) di = {} stack.append(a) di = {} for a in stack: if a not in di: di[a] = 0 di[a] += 1 print(tot + countTot(di))
FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR DICT EXPR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR
Jim has invented a new flying object called HZ42. HZ42 is like a broom and can only fly horizontally, independent of the environment. One day, Jim started his flight from Dubai's highest skyscraper, traveled some distance and landed on another skyscraper of same height! So much fun! But unfortunately, new skyscrapers have been built recently. Let us describe the problem in one dimensional space. We have in total $N$ skyscrapers aligned from left to right. The $\boldsymbol{i}$^{th} skyscraper has a height of $h_i$. A flying route can be described as $(i,j)$ with $i\neq j$, which means, Jim starts his HZ42 at the top of the skyscraper $\boldsymbol{i}$ and lands on the skyscraper $j$. Since HZ42 can only fly horizontally, Jim will remain at the height $h_i$ only. Thus the path $(i,j)$ can be valid, only if each of the skyscrapers $i,i+1,\ldots,j-1,j$ is not strictly greater than $h_i$ and if the height of the skyscraper he starts from and arrives on have the same height. Formally, $(i,j)$ is valid iff $\nexists k\in[i,j]:h_k>h_i$ and $h_i=h_j$. Help Jim in counting the number of valid paths represented by ordered pairs $(i,j)$. Input Format The first line contains $N$, the number of skyscrapers. The next line contains $N$ space separated integers representing the heights of the skyscrapers. Output Format Print an integer that denotes the number of valid routes. Constraints $1\leq N\leq3\cdot10^5$ and no skyscraper will have height greater than $10^{6}$ and less than $\mbox{1}$. Sample Input #00 6 3 2 1 2 3 3 Sample Output #00 8 Sample Input #01 3 1 1000 1 Sample Output #01 0 Explanation First testcase: (1, 5), (1, 6) (5, 6) and (2, 4) and the routes in the opposite directions are the only valid routes. Second testcase: (1, 3) and (3, 1) could have been valid, if there wasn't a big skyscraper with height 1000 between them.
n = int(input().strip()) m = 1000000 h = list(map(int, input().strip().split())) h.append(m + 1) r = [0] * n for i in range(n - 1, -1, -1): t = i + 1 while h[i] >= h[t]: t = r[t] r[i] = t f = [] for i in range(m + 1): f.append([]) for i in range(n): f[h[i]].append(i) ans = 0 for i in range(m + 1): t = 0 for j in range(len(f[i]) - 1): if r[f[i][j]] > f[i][j + 1]: t += 1 else: ans += t * (t + 1) // 2 t = 0 ans += t * (t + 1) // 2 print(ans * 2)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Jim has invented a new flying object called HZ42. HZ42 is like a broom and can only fly horizontally, independent of the environment. One day, Jim started his flight from Dubai's highest skyscraper, traveled some distance and landed on another skyscraper of same height! So much fun! But unfortunately, new skyscrapers have been built recently. Let us describe the problem in one dimensional space. We have in total $N$ skyscrapers aligned from left to right. The $\boldsymbol{i}$^{th} skyscraper has a height of $h_i$. A flying route can be described as $(i,j)$ with $i\neq j$, which means, Jim starts his HZ42 at the top of the skyscraper $\boldsymbol{i}$ and lands on the skyscraper $j$. Since HZ42 can only fly horizontally, Jim will remain at the height $h_i$ only. Thus the path $(i,j)$ can be valid, only if each of the skyscrapers $i,i+1,\ldots,j-1,j$ is not strictly greater than $h_i$ and if the height of the skyscraper he starts from and arrives on have the same height. Formally, $(i,j)$ is valid iff $\nexists k\in[i,j]:h_k>h_i$ and $h_i=h_j$. Help Jim in counting the number of valid paths represented by ordered pairs $(i,j)$. Input Format The first line contains $N$, the number of skyscrapers. The next line contains $N$ space separated integers representing the heights of the skyscrapers. Output Format Print an integer that denotes the number of valid routes. Constraints $1\leq N\leq3\cdot10^5$ and no skyscraper will have height greater than $10^{6}$ and less than $\mbox{1}$. Sample Input #00 6 3 2 1 2 3 3 Sample Output #00 8 Sample Input #01 3 1 1000 1 Sample Output #01 0 Explanation First testcase: (1, 5), (1, 6) (5, 6) and (2, 4) and the routes in the opposite directions are the only valid routes. Second testcase: (1, 3) and (3, 1) could have been valid, if there wasn't a big skyscraper with height 1000 between them.
N = int(input()) arr = list(map(int, input().split())) ans = 0 ma = [(10**9, 1)] for i in range(len(arr) - 1, -1, -1): if arr[i] < ma[-1][0]: ma.append((arr[i], 1)) else: while arr[i] > ma[-1][0]: ma.pop(-1) if arr[i] == ma[-1][0]: c, v = ma.pop(-1) ans += v * 2 ma.append((c, v + 1)) else: ma.append((arr[i], 1)) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER WHILE VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Jim has invented a new flying object called HZ42. HZ42 is like a broom and can only fly horizontally, independent of the environment. One day, Jim started his flight from Dubai's highest skyscraper, traveled some distance and landed on another skyscraper of same height! So much fun! But unfortunately, new skyscrapers have been built recently. Let us describe the problem in one dimensional space. We have in total $N$ skyscrapers aligned from left to right. The $\boldsymbol{i}$^{th} skyscraper has a height of $h_i$. A flying route can be described as $(i,j)$ with $i\neq j$, which means, Jim starts his HZ42 at the top of the skyscraper $\boldsymbol{i}$ and lands on the skyscraper $j$. Since HZ42 can only fly horizontally, Jim will remain at the height $h_i$ only. Thus the path $(i,j)$ can be valid, only if each of the skyscrapers $i,i+1,\ldots,j-1,j$ is not strictly greater than $h_i$ and if the height of the skyscraper he starts from and arrives on have the same height. Formally, $(i,j)$ is valid iff $\nexists k\in[i,j]:h_k>h_i$ and $h_i=h_j$. Help Jim in counting the number of valid paths represented by ordered pairs $(i,j)$. Input Format The first line contains $N$, the number of skyscrapers. The next line contains $N$ space separated integers representing the heights of the skyscrapers. Output Format Print an integer that denotes the number of valid routes. Constraints $1\leq N\leq3\cdot10^5$ and no skyscraper will have height greater than $10^{6}$ and less than $\mbox{1}$. Sample Input #00 6 3 2 1 2 3 3 Sample Output #00 8 Sample Input #01 3 1 1000 1 Sample Output #01 0 Explanation First testcase: (1, 5), (1, 6) (5, 6) and (2, 4) and the routes in the opposite directions are the only valid routes. Second testcase: (1, 3) and (3, 1) could have been valid, if there wasn't a big skyscraper with height 1000 between them.
import sys def solution(arr): res = 0 st = [] for ind in range(len(arr)): while st and st[-1][0] < arr[ind]: st.pop() if st and arr[ind] == st[-1][0]: res += st[-1][1] st[-1][1] += 1 else: st.append([arr[ind], 1]) return 2 * res n = int(input().strip()) arr = [int(x) for x in input().strip().split()] print(solution(arr))
IMPORT FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR IF VAR VAR VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR VAR NUMBER RETURN BIN_OP NUMBER 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Jim has invented a new flying object called HZ42. HZ42 is like a broom and can only fly horizontally, independent of the environment. One day, Jim started his flight from Dubai's highest skyscraper, traveled some distance and landed on another skyscraper of same height! So much fun! But unfortunately, new skyscrapers have been built recently. Let us describe the problem in one dimensional space. We have in total $N$ skyscrapers aligned from left to right. The $\boldsymbol{i}$^{th} skyscraper has a height of $h_i$. A flying route can be described as $(i,j)$ with $i\neq j$, which means, Jim starts his HZ42 at the top of the skyscraper $\boldsymbol{i}$ and lands on the skyscraper $j$. Since HZ42 can only fly horizontally, Jim will remain at the height $h_i$ only. Thus the path $(i,j)$ can be valid, only if each of the skyscrapers $i,i+1,\ldots,j-1,j$ is not strictly greater than $h_i$ and if the height of the skyscraper he starts from and arrives on have the same height. Formally, $(i,j)$ is valid iff $\nexists k\in[i,j]:h_k>h_i$ and $h_i=h_j$. Help Jim in counting the number of valid paths represented by ordered pairs $(i,j)$. Input Format The first line contains $N$, the number of skyscrapers. The next line contains $N$ space separated integers representing the heights of the skyscrapers. Output Format Print an integer that denotes the number of valid routes. Constraints $1\leq N\leq3\cdot10^5$ and no skyscraper will have height greater than $10^{6}$ and less than $\mbox{1}$. Sample Input #00 6 3 2 1 2 3 3 Sample Output #00 8 Sample Input #01 3 1 1000 1 Sample Output #01 0 Explanation First testcase: (1, 5), (1, 6) (5, 6) and (2, 4) and the routes in the opposite directions are the only valid routes. Second testcase: (1, 3) and (3, 1) could have been valid, if there wasn't a big skyscraper with height 1000 between them.
HEIGHT = "height" COUNT = "count" def compute_num_paths(heights): total_paths = 0 height_stack = [] for i in heights: while len(height_stack) and height_stack[-1][HEIGHT] < i: count = height_stack.pop()[COUNT] total_paths += count * (count - 1) if len(height_stack) == 0 or height_stack[-1][HEIGHT] > i: height_stack.append({HEIGHT: i, COUNT: 1}) elif height_stack[-1][HEIGHT] == i: height_stack[-1][COUNT] += 1 while len(height_stack): count = height_stack.pop()[COUNT] total_paths += count * (count - 1) return total_paths input() heights = map(int, input().split(" ")) print(compute_num_paths(heights))
ASSIGN VAR STRING ASSIGN VAR STRING FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR WHILE FUNC_CALL VAR VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR DICT VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER WHILE FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Trie is an efficient information retrieval data structure. This data structure is used to store Strings and search strings, String stored can also be deleted. Given a Trie root for a larger string super and a string key, delete all the occurences of key in the Trie. Example 1: Input: N = 8 super = "the a there answer any by bye their" key = "the" Your Task: Complete the function deleteKey() to delete the given string key.The String key if exists in the larger string super, must be deleted from Trie root. The larger string super contains space separated small strings. And if the string is deleted successfully than 1 will be printed. If any other string other than String A is deleted, you will get wrong answer. Constraints: 1≤ T ≤200 1≤ N, |A| ≤20
class Solution: def deleteKey(self, root, key): self.remove(key, root) def remove(self, word, node, depth=0): if not node: return None if depth == len(word): return self.getUpdatedLastNodeOfRemovedWord(node) else: index = self.getIndexForLetter(word[depth]) node.children[index] = self.remove(word, node.children[index], depth + 1) if self.isEmpty(node) and not node.isEndOfWord: del node node = None return node def getUpdatedLastNodeOfRemovedWord(self, lastNode): lastNode.isEndOfWord = False if self.isEmpty(lastNode): del lastNode lastNode = None return lastNode def getIndexForLetter(self, letter): return ord(letter) - ord("a") def isEmpty(self, node): for i in range(26): if node.children[i]: return False return True
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR VAR VAR FUNC_DEF NUMBER IF VAR RETURN NONE IF VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NONE RETURN VAR FUNC_DEF ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR NONE RETURN VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER
Trie is an efficient information retrieval data structure. This data structure is used to store Strings and search strings, String stored can also be deleted. Given a Trie root for a larger string super and a string key, delete all the occurences of key in the Trie. Example 1: Input: N = 8 super = "the a there answer any by bye their" key = "the" Your Task: Complete the function deleteKey() to delete the given string key.The String key if exists in the larger string super, must be deleted from Trie root. The larger string super contains space separated small strings. And if the string is deleted successfully than 1 will be printed. If any other string other than String A is deleted, you will get wrong answer. Constraints: 1≤ T ≤200 1≤ N, |A| ≤20
class Solution: def deleteKey(self, root, key): curr = root for c in key: idx = ord(c) - ord("a") if curr.children[idx] != None: curr = curr.children[idx] curr.isEndOfWord = False
CLASS_DEF FUNC_DEF ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING IF VAR VAR NONE ASSIGN VAR VAR VAR ASSIGN VAR NUMBER
Trie is an efficient information retrieval data structure. This data structure is used to store Strings and search strings, String stored can also be deleted. Given a Trie root for a larger string super and a string key, delete all the occurences of key in the Trie. Example 1: Input: N = 8 super = "the a there answer any by bye their" key = "the" Your Task: Complete the function deleteKey() to delete the given string key.The String key if exists in the larger string super, must be deleted from Trie root. The larger string super contains space separated small strings. And if the string is deleted successfully than 1 will be printed. If any other string other than String A is deleted, you will get wrong answer. Constraints: 1≤ T ≤200 1≤ N, |A| ≤20
class Solution: def deleteKey(self, root, key): node = root for i in key: count = 0 for j in node.children: if j is None: count += 1 ind = ord(i) - ord("a") if count < 25: node = node.children[ind] if count == 25: node.children[ind] = None node.isEndOfWord = False
CLASS_DEF FUNC_DEF ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR NONE VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING IF VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR NONE ASSIGN VAR NUMBER
Trie is an efficient information retrieval data structure. This data structure is used to store Strings and search strings, String stored can also be deleted. Given a Trie root for a larger string super and a string key, delete all the occurences of key in the Trie. Example 1: Input: N = 8 super = "the a there answer any by bye their" key = "the" Your Task: Complete the function deleteKey() to delete the given string key.The String key if exists in the larger string super, must be deleted from Trie root. The larger string super contains space separated small strings. And if the string is deleted successfully than 1 will be printed. If any other string other than String A is deleted, you will get wrong answer. Constraints: 1≤ T ≤200 1≤ N, |A| ≤20
class Solution: def deleteKey(self, root, key): def empty(n): for i in range(26): if n.children[i]: return False return True def remove(n, key, i): if i == len(key): n.isEndOfWord = False return empty(n) == 0 id = ord(key[i]) - ord("a") if remove(n.children[id], key, i + 1): n.children[id] = None return True else: return False return remove(root, key, 0)
CLASS_DEF FUNC_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NONE RETURN NUMBER RETURN NUMBER RETURN FUNC_CALL VAR VAR VAR NUMBER
Trie is an efficient information retrieval data structure. This data structure is used to store Strings and search strings, String stored can also be deleted. Given a Trie root for a larger string super and a string key, delete all the occurences of key in the Trie. Example 1: Input: N = 8 super = "the a there answer any by bye their" key = "the" Your Task: Complete the function deleteKey() to delete the given string key.The String key if exists in the larger string super, must be deleted from Trie root. The larger string super contains space separated small strings. And if the string is deleted successfully than 1 will be printed. If any other string other than String A is deleted, you will get wrong answer. Constraints: 1≤ T ≤200 1≤ N, |A| ≤20
class Solution: def _isEmpty(self, root): for child in root.children: if child != None: return False return True def deleteKey(self, root, key): cur = root for c in key: idx = ord(c) - ord("a") cur = cur.children[idx] if cur == None: return cur.isEndOfWord = False def _deleteKey(self, root, key): print("key:", key) if root == None: return -1, -1 index = ord(key[0]) - ord("a") if len(key) == 1: if ( root.children[index] == None or root.children[index].isEndOfWord == False ): print("Word not found") return -1, -1 child = root.children[index] if self._isEmpty(child): print("Word found delete all") root.children[index] = None return 0, 0 else: print("Word found Dont delete", index) child.isEndofWord = False print(index, root and root.children[index] != None) return 0, -1 else: if root.children[index] == None: return -1, -1 found, delete = self.deleteKey(root.children[index], key[1:]) if delete == 0: root.children[index] = None print(index, root and root.children[index] != None) return found, delete
CLASS_DEF FUNC_DEF FOR VAR VAR IF VAR NONE RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR IF VAR NONE RETURN ASSIGN VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR STRING VAR IF VAR NONE RETURN NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING IF FUNC_CALL VAR VAR NUMBER IF VAR VAR NONE VAR VAR NUMBER EXPR FUNC_CALL VAR STRING RETURN NUMBER NUMBER ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR NONE RETURN NUMBER NUMBER EXPR FUNC_CALL VAR STRING VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NONE RETURN NUMBER NUMBER IF VAR VAR NONE RETURN NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NONE EXPR FUNC_CALL VAR VAR VAR VAR VAR NONE RETURN VAR VAR
Trie is an efficient information retrieval data structure. This data structure is used to store Strings and search strings, String stored can also be deleted. Given a Trie root for a larger string super and a string key, delete all the occurences of key in the Trie. Example 1: Input: N = 8 super = "the a there answer any by bye their" key = "the" Your Task: Complete the function deleteKey() to delete the given string key.The String key if exists in the larger string super, must be deleted from Trie root. The larger string super contains space separated small strings. And if the string is deleted successfully than 1 will be printed. If any other string other than String A is deleted, you will get wrong answer. Constraints: 1≤ T ≤200 1≤ N, |A| ≤20
class Solution: def deleteKey(self, root, key): def delete(word, pos, root): if not root: return if pos == len(word): if root.isEndOfWord: root.isEndOfWord = False return delete(word, pos + 1, root.children[ord(word[pos]) - ord("a")]) delete(key, 0, root)
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR RETURN IF VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR NUMBER VAR
Trie is an efficient information retrieval data structure. This data structure is used to store Strings and search strings, String stored can also be deleted. Given a Trie root for a larger string super and a string key, delete all the occurences of key in the Trie. Example 1: Input: N = 8 super = "the a there answer any by bye their" key = "the" Your Task: Complete the function deleteKey() to delete the given string key.The String key if exists in the larger string super, must be deleted from Trie root. The larger string super contains space separated small strings. And if the string is deleted successfully than 1 will be printed. If any other string other than String A is deleted, you will get wrong answer. Constraints: 1≤ T ≤200 1≤ N, |A| ≤20
class Solution: def deleteKey(self, root, key): length = len(key) def search(root, key): currentNode = root for i in range(length): index = ord(key[i]) - ord("a") if currentNode.children[index] is None: return False currentNode = currentNode.children[index] return currentNode.isEndOfWord if search(root, key): currentNode = root for i in range(length): index = ord(key[i]) - ord("a") currentNode = currentNode.children[index] if currentNode.isEndOfWord: currentNode.isEndOfWord = False return True return False
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING IF VAR VAR NONE RETURN NUMBER ASSIGN VAR VAR VAR RETURN VAR IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR IF VAR ASSIGN VAR NUMBER RETURN NUMBER RETURN NUMBER
Trie is an efficient information retrieval data structure. This data structure is used to store Strings and search strings, String stored can also be deleted. Given a Trie root for a larger string super and a string key, delete all the occurences of key in the Trie. Example 1: Input: N = 8 super = "the a there answer any by bye their" key = "the" Your Task: Complete the function deleteKey() to delete the given string key.The String key if exists in the larger string super, must be deleted from Trie root. The larger string super contains space separated small strings. And if the string is deleted successfully than 1 will be printed. If any other string other than String A is deleted, you will get wrong answer. Constraints: 1≤ T ≤200 1≤ N, |A| ≤20
class Solution: def deleteKey(self, root, key): curr = root for l in key: idx = charToIndex(l) if not curr.children[idx]: return curr = curr.children[idx] curr.isEndOfWord = False
CLASS_DEF FUNC_DEF ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN ASSIGN VAR VAR VAR ASSIGN VAR NUMBER
Trie is an efficient information retrieval data structure. This data structure is used to store Strings and search strings, String stored can also be deleted. Given a Trie root for a larger string super and a string key, delete all the occurences of key in the Trie. Example 1: Input: N = 8 super = "the a there answer any by bye their" key = "the" Your Task: Complete the function deleteKey() to delete the given string key.The String key if exists in the larger string super, must be deleted from Trie root. The larger string super contains space separated small strings. And if the string is deleted successfully than 1 will be printed. If any other string other than String A is deleted, you will get wrong answer. Constraints: 1≤ T ≤200 1≤ N, |A| ≤20
class Solution: def _deleteKey(self, root, key, index): if not root or index >= len(key): return newRoot = root.children[charToIndex(key[index])] self._deleteKey(newRoot, key, index + 1) if index == len(key) - 1 and newRoot.isEndOfWord: newRoot.isEndOfWord = False if self.isEmpty(root): root.children[charToIndex(key[index])] = None def isEmpty(self, root): count = 0 for i in range(len(root.children)): if root.children[i]: count += 1 if count > 1: return False return True def deleteKey(self, root, key): self._deleteKey(root, key, 0)
CLASS_DEF FUNC_DEF IF VAR VAR FUNC_CALL VAR VAR RETURN ASSIGN VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NONE FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER IF VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF EXPR FUNC_CALL VAR VAR VAR NUMBER
Trie is an efficient information retrieval data structure. This data structure is used to store Strings and search strings, String stored can also be deleted. Given a Trie root for a larger string super and a string key, delete all the occurences of key in the Trie. Example 1: Input: N = 8 super = "the a there answer any by bye their" key = "the" Your Task: Complete the function deleteKey() to delete the given string key.The String key if exists in the larger string super, must be deleted from Trie root. The larger string super contains space separated small strings. And if the string is deleted successfully than 1 will be printed. If any other string other than String A is deleted, you will get wrong answer. Constraints: 1≤ T ≤200 1≤ N, |A| ≤20
class Solution: def deleteKey(self, root, key): for i in range(len(key)): if root.children[ord(key[i]) - ord("a")] != None: root = root.children[ord(key[i]) - ord("a")] root.isEndOfWord = False
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NONE ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER
Trie is an efficient information retrieval data structure. This data structure is used to store Strings and search strings, String stored can also be deleted. Given a Trie root for a larger string super and a string key, delete all the occurences of key in the Trie. Example 1: Input: N = 8 super = "the a there answer any by bye their" key = "the" Your Task: Complete the function deleteKey() to delete the given string key.The String key if exists in the larger string super, must be deleted from Trie root. The larger string super contains space separated small strings. And if the string is deleted successfully than 1 will be printed. If any other string other than String A is deleted, you will get wrong answer. Constraints: 1≤ T ≤200 1≤ N, |A| ≤20
class Solution: def deleteKey(self, root, key): curr = root for ele in key: if not curr.children[ord(ele) - ord("a")]: return curr = curr.children[ord(ele) - ord("a")] curr.isEndOfWord = False
CLASS_DEF FUNC_DEF ASSIGN VAR VAR FOR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING RETURN ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER
Trie is an efficient information retrieval data structure. This data structure is used to store Strings and search strings, String stored can also be deleted. Given a Trie root for a larger string super and a string key, delete all the occurences of key in the Trie. Example 1: Input: N = 8 super = "the a there answer any by bye their" key = "the" Your Task: Complete the function deleteKey() to delete the given string key.The String key if exists in the larger string super, must be deleted from Trie root. The larger string super contains space separated small strings. And if the string is deleted successfully than 1 will be printed. If any other string other than String A is deleted, you will get wrong answer. Constraints: 1≤ T ≤200 1≤ N, |A| ≤20
class Solution: def cti(self, c: str): return ord(c) - ord("a") def deleteKey(self, root, key): for c in key: index = self.cti(c) if not root.children[index]: return root = root.children[index] root.isEndOfWord = False
CLASS_DEF FUNC_DEF VAR RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING FUNC_DEF FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN ASSIGN VAR VAR VAR ASSIGN VAR NUMBER
Trie is an efficient information retrieval data structure. This data structure is used to store Strings and search strings, String stored can also be deleted. Given a Trie root for a larger string super and a string key, delete all the occurences of key in the Trie. Example 1: Input: N = 8 super = "the a there answer any by bye their" key = "the" Your Task: Complete the function deleteKey() to delete the given string key.The String key if exists in the larger string super, must be deleted from Trie root. The larger string super contains space separated small strings. And if the string is deleted successfully than 1 will be printed. If any other string other than String A is deleted, you will get wrong answer. Constraints: 1≤ T ≤200 1≤ N, |A| ≤20
class Solution: def deleteKey(self, root, key): a = ord("a") for i in key: if root.children[ord(i) - a] == None: return False root = root.children[ord(i) - a] if root.isEndOfWord == False: return 0 root.isEndOfWord = False return True
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR VAR NONE RETURN NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER RETURN NUMBER
Trie is an efficient information retrieval data structure. This data structure is used to store Strings and search strings, String stored can also be deleted. Given a Trie root for a larger string super and a string key, delete all the occurences of key in the Trie. Example 1: Input: N = 8 super = "the a there answer any by bye their" key = "the" Your Task: Complete the function deleteKey() to delete the given string key.The String key if exists in the larger string super, must be deleted from Trie root. The larger string super contains space separated small strings. And if the string is deleted successfully than 1 will be printed. If any other string other than String A is deleted, you will get wrong answer. Constraints: 1≤ T ≤200 1≤ N, |A| ≤20
def c2i(s): return ord(s) - ord("a") class Solution: def deleteKey(self, root, key): pCrawl = root length = len(key) for level in range(length): index = c2i(key[level]) if pCrawl.children[index] is None: return pCrawl = pCrawl.children[index] pCrawl.isEndOfWord = False class TrieNode: def __init__(self): self.children = [None] * 26 self.isEndOfWord = False class Trie: def __init__(self): self.root = TrieNode() def charToIndex(ch): return ord(ch) - ord("a") def insert(root, key): for e in key: idx = charToIndex(e) if not root.children[idx]: root.children[idx] = TrieNode() root = root.children[idx] root.isEndOfWord = True def search(root, key): for e in key: idx = charToIndex(e) if not root.children[idx]: return root = root.children[idx] return root.isEndOfWord if __name__ == "__main__": t = int(input()) for tcs in range(t): n = int(input()) arr = input().strip().split() key = input() t = Trie() for s in arr: insert(t.root, s) Solution().deleteKey(t.root, key) if search(t.root, key): print(0) else: print(1)
FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR NONE RETURN ASSIGN VAR VAR VAR ASSIGN VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NONE NUMBER ASSIGN VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING FUNC_DEF FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FUNC_DEF FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN ASSIGN VAR VAR VAR RETURN VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
Trie is an efficient information retrieval data structure. This data structure is used to store Strings and search strings, String stored can also be deleted. Given a Trie root for a larger string super and a string key, delete all the occurences of key in the Trie. Example 1: Input: N = 8 super = "the a there answer any by bye their" key = "the" Your Task: Complete the function deleteKey() to delete the given string key.The String key if exists in the larger string super, must be deleted from Trie root. The larger string super contains space separated small strings. And if the string is deleted successfully than 1 will be printed. If any other string other than String A is deleted, you will get wrong answer. Constraints: 1≤ T ≤200 1≤ N, |A| ≤20
class Solution: def deleteKey1(self, root, key): temp = root for ch in key: idx = ord(ch) - ord("a") temp = temp.children[idx] if temp is None: return temp.isEndOfWord = False def has_child(self, root): for i, child in enumerate(root.children): if child is not None: return True return False def solve(self, root, key, i): if i == len(key): if root.isEndOfWord: root.isEndOfWord = False if not self.has_child(root): del root return idx = ord(key[i]) - ord("a") self.solve(root.children[idx], key, i + 1) if not root.isEndOfWord and not self.has_child(root): del root def deleteKey(self, root, key): self.solve(root, key, 0)
CLASS_DEF FUNC_DEF ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR IF VAR NONE RETURN ASSIGN VAR NUMBER FUNC_DEF FOR VAR VAR FUNC_CALL VAR VAR IF VAR NONE RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR RETURN ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR VAR VAR NUMBER
Trie is an efficient information retrieval data structure. This data structure is used to store Strings and search strings, String stored can also be deleted. Given a Trie root for a larger string super and a string key, delete all the occurences of key in the Trie. Example 1: Input: N = 8 super = "the a there answer any by bye their" key = "the" Your Task: Complete the function deleteKey() to delete the given string key.The String key if exists in the larger string super, must be deleted from Trie root. The larger string super contains space separated small strings. And if the string is deleted successfully than 1 will be printed. If any other string other than String A is deleted, you will get wrong answer. Constraints: 1≤ T ≤200 1≤ N, |A| ≤20
class Solution: def deleteKey(self, root, word): temp = root n = len(word) for i in range(n): idx = ord(word[i]) - 97 if not temp.children[idx]: return temp = temp.children[idx] temp.isEndOfWord = 0
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR RETURN ASSIGN VAR VAR VAR ASSIGN VAR NUMBER
Trie is an efficient information retrieval data structure. This data structure is used to store Strings and search strings, String stored can also be deleted. Given a Trie root for a larger string super and a string key, delete all the occurences of key in the Trie. Example 1: Input: N = 8 super = "the a there answer any by bye their" key = "the" Your Task: Complete the function deleteKey() to delete the given string key.The String key if exists in the larger string super, must be deleted from Trie root. The larger string super contains space separated small strings. And if the string is deleted successfully than 1 will be printed. If any other string other than String A is deleted, you will get wrong answer. Constraints: 1≤ T ≤200 1≤ N, |A| ≤20
class Solution: def is_empty(self, root): for i in range(26): if root.children[i]: return False return True def deleteKey(self, root, key, depth=0): try: if root is None: return if len(key) == depth: if root.isEndOfWord: root.isEndOfWord = False if self.is_empty(root): del root root = None return root idx = ord(key[depth]) - ord("a") root.children[idx] = self.deleteKey(root.children[idx], key, depth + 1) if root and self.is_empty(root) and not root.isEndOfWord: del root root = None return root except Exception as e: print(e)
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF NUMBER IF VAR NONE RETURN IF FUNC_CALL VAR VAR VAR IF VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR NONE RETURN VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NONE RETURN VAR VAR EXPR FUNC_CALL VAR VAR
Trie is an efficient information retrieval data structure. This data structure is used to store Strings and search strings, String stored can also be deleted. Given a Trie root for a larger string super and a string key, delete all the occurences of key in the Trie. Example 1: Input: N = 8 super = "the a there answer any by bye their" key = "the" Your Task: Complete the function deleteKey() to delete the given string key.The String key if exists in the larger string super, must be deleted from Trie root. The larger string super contains space separated small strings. And if the string is deleted successfully than 1 will be printed. If any other string other than String A is deleted, you will get wrong answer. Constraints: 1≤ T ≤200 1≤ N, |A| ≤20
class Solution: def delete(self, node, s, i): if i == len(s): if node.isEndOfWord == True: node.isEndOfWord = False return self.delete(node.children[ord(s[i]) - 97], s, i + 1) x = 0 for j in node.children: if j != None: x += 1 if x == 1: node.children[ord(s[i]) - 97] = None return def deleteKey(self, root, key): self.delete(root, key, 0)
CLASS_DEF FUNC_DEF IF VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NONE VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NONE RETURN FUNC_DEF EXPR FUNC_CALL VAR VAR VAR NUMBER
Trie is an efficient information retrieval data structure. This data structure is used to store Strings and search strings, String stored can also be deleted. Given a Trie root for a larger string super and a string key, delete all the occurences of key in the Trie. Example 1: Input: N = 8 super = "the a there answer any by bye their" key = "the" Your Task: Complete the function deleteKey() to delete the given string key.The String key if exists in the larger string super, must be deleted from Trie root. The larger string super contains space separated small strings. And if the string is deleted successfully than 1 will be printed. If any other string other than String A is deleted, you will get wrong answer. Constraints: 1≤ T ≤200 1≤ N, |A| ≤20
class Solution: def deleteKey(self, root, key): def delete(i, key, root): if i == len(key) and root.isEndOfWord: root.isEndOfWord = False return root.children idx = ord(key[i]) - ord("a") if root.children[idx]: root = delete(i + 1, key, root.children[idx]) return root delete(0, key, root)
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR NUMBER VAR VAR
Trie is an efficient information retrieval data structure. This data structure is used to store Strings and search strings, String stored can also be deleted. Given a Trie root for a larger string super and a string key, delete all the occurences of key in the Trie. Example 1: Input: N = 8 super = "the a there answer any by bye their" key = "the" Your Task: Complete the function deleteKey() to delete the given string key.The String key if exists in the larger string super, must be deleted from Trie root. The larger string super contains space separated small strings. And if the string is deleted successfully than 1 will be printed. If any other string other than String A is deleted, you will get wrong answer. Constraints: 1≤ T ≤200 1≤ N, |A| ≤20
class Solution: def deleteKey(self, root, key): depth = 0 self.delete(root, key, depth) def delete(self, root, key, depth): if root == None: return None if len(key) == depth: if root.isEndOfWord == True: root.isEndOfWord = False if self.isEmpty(root): root = None return root index = ord(key[depth]) - ord("a") root.children[index] = self.delete(root.children[index], key, depth + 1) if self.isEmpty(root) and root.isEndOfWord == False: return None return root def isEmpty(self, node): for i in range(len(node.children)): if node.children[i] != None: return False return True
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF IF VAR NONE RETURN NONE IF FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR NONE RETURN VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER RETURN NONE RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NONE RETURN NUMBER RETURN NUMBER
Trie is an efficient information retrieval data structure. This data structure is used to store Strings and search strings, String stored can also be deleted. Given a Trie root for a larger string super and a string key, delete all the occurences of key in the Trie. Example 1: Input: N = 8 super = "the a there answer any by bye their" key = "the" Your Task: Complete the function deleteKey() to delete the given string key.The String key if exists in the larger string super, must be deleted from Trie root. The larger string super contains space separated small strings. And if the string is deleted successfully than 1 will be printed. If any other string other than String A is deleted, you will get wrong answer. Constraints: 1≤ T ≤200 1≤ N, |A| ≤20
def chartoindex(s): return ord(s) - ord("a") class Solution: def deleteKey(self, root, key): curr = root for c in key: idx = chartoindex(c) if curr.children[idx]: curr = curr.children[idx] curr.isEndOfWord = False
FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING CLASS_DEF FUNC_DEF ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER
Trie is an efficient information retrieval data structure. This data structure is used to store Strings and search strings, String stored can also be deleted. Given a Trie root for a larger string super and a string key, delete all the occurences of key in the Trie. Example 1: Input: N = 8 super = "the a there answer any by bye their" key = "the" Your Task: Complete the function deleteKey() to delete the given string key.The String key if exists in the larger string super, must be deleted from Trie root. The larger string super contains space separated small strings. And if the string is deleted successfully than 1 will be printed. If any other string other than String A is deleted, you will get wrong answer. Constraints: 1≤ T ≤200 1≤ N, |A| ≤20
class Solution: def deleteKey(self, root, key): if len(key) == 0: if root.isEndOfWord: root.isEndOfWord = False return True return False index = ord(key[0]) - ord("a") if root.children[index] is not None: child = root.children[index] else: return False return self.deleteKey(child, key[1:])
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER IF VAR ASSIGN VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING IF VAR VAR NONE ASSIGN VAR VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR VAR VAR NUMBER
Trie is an efficient information retrieval data structure. This data structure is used to store Strings and search strings, String stored can also be deleted. Given a Trie root for a larger string super and a string key, delete all the occurences of key in the Trie. Example 1: Input: N = 8 super = "the a there answer any by bye their" key = "the" Your Task: Complete the function deleteKey() to delete the given string key.The String key if exists in the larger string super, must be deleted from Trie root. The larger string super contains space separated small strings. And if the string is deleted successfully than 1 will be printed. If any other string other than String A is deleted, you will get wrong answer. Constraints: 1≤ T ≤200 1≤ N, |A| ≤20
class Solution: def deleteKey(self, root, key): def isEmpty(r): for c in r.children: if c: return False return True def deleteK(r, k, depth): if not r: return None if depth == len(k): if r.isEndOfWord: r.isEndOfWord = False if isEmpty(r): del r r = None return r r.children[ord(k[depth]) - 97] = deleteK( r.children[ord(k[depth]) - 97], k, depth + 1 ) if isEmpty(r) and r.isEndOfWord == False: del r r = None return r deleteK(root, key, 0)
CLASS_DEF FUNC_DEF FUNC_DEF FOR VAR VAR IF VAR RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR RETURN NONE IF VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR NONE RETURN VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER VAR ASSIGN VAR NONE RETURN VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
Trie is an efficient information retrieval data structure. This data structure is used to store Strings and search strings, String stored can also be deleted. Given a Trie root for a larger string super and a string key, delete all the occurences of key in the Trie. Example 1: Input: N = 8 super = "the a there answer any by bye their" key = "the" Your Task: Complete the function deleteKey() to delete the given string key.The String key if exists in the larger string super, must be deleted from Trie root. The larger string super contains space separated small strings. And if the string is deleted successfully than 1 will be printed. If any other string other than String A is deleted, you will get wrong answer. Constraints: 1≤ T ≤200 1≤ N, |A| ≤20
class Solution: def deleteKey(self, root, key): if root != None: if len(key) == 0: root.isEndOfWord = 0 else: self.deleteKey(root.children[ord(key[0]) - 97], key[1:])
CLASS_DEF FUNC_DEF IF VAR NONE IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER
Trie is an efficient information retrieval data structure. This data structure is used to store Strings and search strings, String stored can also be deleted. Given a Trie root for a larger string super and a string key, delete all the occurences of key in the Trie. Example 1: Input: N = 8 super = "the a there answer any by bye their" key = "the" Your Task: Complete the function deleteKey() to delete the given string key.The String key if exists in the larger string super, must be deleted from Trie root. The larger string super contains space separated small strings. And if the string is deleted successfully than 1 will be printed. If any other string other than String A is deleted, you will get wrong answer. Constraints: 1≤ T ≤200 1≤ N, |A| ≤20
def idx(c): return ord(c) - ord("a") def child_count(node): return sum(1 for child in node.children if child) class Solution: def deleteKey(self, root, key): last_node = root, idx(key[0]) node = root for c in key: i = idx(c) if child_count(node) > 1: last_node = node, i node = node.children[i] if node is None: return node.isEndOfWord = False i = last_node[1] last_node[0].children[i] = None return
FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING FUNC_DEF RETURN FUNC_CALL VAR NUMBER VAR VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NONE RETURN ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR NONE RETURN
Trie is an efficient information retrieval data structure. This data structure is used to store Strings and search strings, String stored can also be deleted. Given a Trie root for a larger string super and a string key, delete all the occurences of key in the Trie. Example 1: Input: N = 8 super = "the a there answer any by bye their" key = "the" Your Task: Complete the function deleteKey() to delete the given string key.The String key if exists in the larger string super, must be deleted from Trie root. The larger string super contains space separated small strings. And if the string is deleted successfully than 1 will be printed. If any other string other than String A is deleted, you will get wrong answer. Constraints: 1≤ T ≤200 1≤ N, |A| ≤20
class Solution: def deleteKey(self, root, key): ki = 0 ke = len(key) snode = [] while root and root.isEndOfWord == False: v = charToIndex(key[ki]) if root.children[v]: snode.append([root, v]) root = root.children[v] ki += 1 if ki == ke: for n in snode: n[0].children[n[1]] = None ki = 0 else: next = None for c in root.children: if c: next = c break ki = 0 snode = [] root = next
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR FOR VAR VAR ASSIGN VAR NUMBER VAR NUMBER NONE ASSIGN VAR NUMBER ASSIGN VAR NONE FOR VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR
Trie is an efficient information retrieval data structure. This data structure is used to store Strings and search strings, String stored can also be deleted. Given a Trie root for a larger string super and a string key, delete all the occurences of key in the Trie. Example 1: Input: N = 8 super = "the a there answer any by bye their" key = "the" Your Task: Complete the function deleteKey() to delete the given string key.The String key if exists in the larger string super, must be deleted from Trie root. The larger string super contains space separated small strings. And if the string is deleted successfully than 1 will be printed. If any other string other than String A is deleted, you will get wrong answer. Constraints: 1≤ T ≤200 1≤ N, |A| ≤20
class Solution: def deleteKey(self, root, key): n = len(key) cur = root stacks = [cur] for i in range(n): idx = charToIndex(key[i]) if cur.children[idx] is None: return else: cur = cur.children[idx] stacks.append(cur) if i == n - 1 and cur.isEndOfWord: n_not_none = 0 for j in range(26): if cur.children[j] is not None: n_not_none += 1 if n_not_none > 0: cur.isEndOfWord = False else: stacks.pop() reverse_idx = n - 1 while stacks: top = stacks.pop() reverse_id = charToIndex(key[reverse_idx]) if top.isEndOfWord: top.children[reverse_id] = None break else: n_not_none = 0 for j in range(26): if cur.children[j] is not None: n_not_none += 1 if n_not_none == 1: top.children[reverse_id] = None reverse_id -= 1 else: top.children[reverse_id] = None break return
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR NONE RETURN ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NONE VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR ASSIGN VAR VAR NONE ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NONE VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR NONE VAR NUMBER ASSIGN VAR VAR NONE RETURN
Trie is an efficient information retrieval data structure. This data structure is used to store Strings and search strings, String stored can also be deleted. Given a Trie root for a larger string super and a string key, delete all the occurences of key in the Trie. Example 1: Input: N = 8 super = "the a there answer any by bye their" key = "the" Your Task: Complete the function deleteKey() to delete the given string key.The String key if exists in the larger string super, must be deleted from Trie root. The larger string super contains space separated small strings. And if the string is deleted successfully than 1 will be printed. If any other string other than String A is deleted, you will get wrong answer. Constraints: 1≤ T ≤200 1≤ N, |A| ≤20
class Solution: def deleteKey(self, root, key): temp = root for ch in key: idx = ord(ch) - ord("a") temp = temp.children[idx] if temp is None: return temp.isEndOfWord = False
CLASS_DEF FUNC_DEF ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR VAR IF VAR NONE RETURN ASSIGN VAR NUMBER
Trie is an efficient information retrieval data structure. This data structure is used to store Strings and search strings, String stored can also be deleted. Given a Trie root for a larger string super and a string key, delete all the occurences of key in the Trie. Example 1: Input: N = 8 super = "the a there answer any by bye their" key = "the" Your Task: Complete the function deleteKey() to delete the given string key.The String key if exists in the larger string super, must be deleted from Trie root. The larger string super contains space separated small strings. And if the string is deleted successfully than 1 will be printed. If any other string other than String A is deleted, you will get wrong answer. Constraints: 1≤ T ≤200 1≤ N, |A| ≤20
class Solution: def deleteKey(self, root, key): temp = root level = 0 if search(root, key): while level < len(key) and temp.children[ord(key[level]) - 97]: temp = temp.children[ord(key[level]) - 97] level += 1 temp.isEndOfWord = False
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER
Trie is an efficient information retrieval data structure. This data structure is used to store Strings and search strings, String stored can also be deleted. Given a Trie root for a larger string super and a string key, delete all the occurences of key in the Trie. Example 1: Input: N = 8 super = "the a there answer any by bye their" key = "the" Your Task: Complete the function deleteKey() to delete the given string key.The String key if exists in the larger string super, must be deleted from Trie root. The larger string super contains space separated small strings. And if the string is deleted successfully than 1 will be printed. If any other string other than String A is deleted, you will get wrong answer. Constraints: 1≤ T ≤200 1≤ N, |A| ≤20
class Solution: def deleteKey(self, root, key): if not search(root, key): return curr = root for w in key: idx = ord(w) - ord("a") if curr.children[idx] is None: return False curr = curr.children[idx] if curr.isEndOfWord: curr.isEndOfWord = False
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR VAR RETURN ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING IF VAR VAR NONE RETURN NUMBER ASSIGN VAR VAR VAR IF VAR ASSIGN VAR NUMBER
Trie is an efficient information retrieval data structure. This data structure is used to store Strings and search strings, String stored can also be deleted. Given a Trie root for a larger string super and a string key, delete all the occurences of key in the Trie. Example 1: Input: N = 8 super = "the a there answer any by bye their" key = "the" Your Task: Complete the function deleteKey() to delete the given string key.The String key if exists in the larger string super, must be deleted from Trie root. The larger string super contains space separated small strings. And if the string is deleted successfully than 1 will be printed. If any other string other than String A is deleted, you will get wrong answer. Constraints: 1≤ T ≤200 1≤ N, |A| ≤20
class Solution: def deleteKey(self, root, word): ROOT = root stack = [] for w in word: index = ord(w) - ord("a") if not root.children[index]: return False stack.append(root) root = root.children[index] if any(root.children): root.isEndOfWord = False else: while stack: for i in range(26): if stack[-1].children[i] == root: break if stack[-1].children[i] and root.isEndOfWord: stack[-1].children[i] = None if stack[-1].isEndOfWord == True: break root = stack.pop() class TrieNode: def __init__(self): self.children = [None] * 26 self.isEndOfWord = False class Trie: def __init__(self): self.root = TrieNode() def charToIndex(ch): return ord(ch) - ord("a") def insert(root, key): for e in key: idx = charToIndex(e) if not root.children[idx]: root.children[idx] = TrieNode() root = root.children[idx] root.isEndOfWord = True def search(root, key): for e in key: idx = charToIndex(e) if not root.children[idx]: return root = root.children[idx] return root.isEndOfWord if __name__ == "__main__": t = int(input()) for tcs in range(t): n = int(input()) arr = input().strip().split() key = input() t = Trie() for s in arr: insert(t.root, s) Solution().deleteKey(t.root, key) if search(t.root, key): print(0) else: print(1)
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING IF VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER VAR VAR IF VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR NONE IF VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NONE NUMBER ASSIGN VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING FUNC_DEF FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FUNC_DEF FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN ASSIGN VAR VAR VAR RETURN VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
Trie is an efficient information retrieval data structure. This data structure is used to store Strings and search strings, String stored can also be deleted. Given a Trie root for a larger string super and a string key, delete all the occurences of key in the Trie. Example 1: Input: N = 8 super = "the a there answer any by bye their" key = "the" Your Task: Complete the function deleteKey() to delete the given string key.The String key if exists in the larger string super, must be deleted from Trie root. The larger string super contains space separated small strings. And if the string is deleted successfully than 1 will be printed. If any other string other than String A is deleted, you will get wrong answer. Constraints: 1≤ T ≤200 1≤ N, |A| ≤20
class Solution: def _util(self, root, key, i): if not root: return None if i == len(key): root.isEndOfWord = False if self.isEmpty(root): root = None return root index = charToIndex(key[i]) root.children[index] = self._util(root.children[index], key, i + 1) if self.isEmpty(root) and root.isEndOfWord == False: root = None def isEmpty(self, root): for i in range(26): if root.children[i] != None: return False return True def deleteKey(self, root, key): return self._util(root, key, 0) class TrieNode: def __init__(self): self.children = [None] * 26 self.isEndOfWord = False class Trie: def __init__(self): self.root = TrieNode() def charToIndex(ch): return ord(ch) - ord("a") def insert(root, key): for e in key: idx = charToIndex(e) if not root.children[idx]: root.children[idx] = TrieNode() root = root.children[idx] root.isEndOfWord = True def search(root, key): for e in key: idx = charToIndex(e) if not root.children[idx]: return root = root.children[idx] return root.isEndOfWord if __name__ == "__main__": t = int(input()) for tcs in range(t): n = int(input()) arr = input().strip().split() key = input() t = Trie() for s in arr: insert(t.root, s) Solution().deleteKey(t.root, key) if search(t.root, key): print(0) else: print(1)
CLASS_DEF FUNC_DEF IF VAR RETURN NONE IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR NONE FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NONE RETURN NUMBER RETURN NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NONE NUMBER ASSIGN VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING FUNC_DEF FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FUNC_DEF FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN ASSIGN VAR VAR VAR RETURN VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER
Trie is an efficient information retrieval data structure. This data structure is used to store Strings and search strings, String stored can also be deleted. Given a Trie root for a larger string super and a string key, delete all the occurences of key in the Trie. Example 1: Input: N = 8 super = "the a there answer any by bye their" key = "the" Your Task: Complete the function deleteKey() to delete the given string key.The String key if exists in the larger string super, must be deleted from Trie root. The larger string super contains space separated small strings. And if the string is deleted successfully than 1 will be printed. If any other string other than String A is deleted, you will get wrong answer. Constraints: 1≤ T ≤200 1≤ N, |A| ≤20
class Solution: def deleteKey(self, root, key): return self.deleteUtil(root, key, 0) def deleteUtil(self, root, word, depth): if root is None: return None if depth == len(word): if root.isEndOfWord == True: root.isEndOfWord = False isPrefix = False for node in root.children: if node is not None: isPrefix = True if not isPrefix: root = None return root index = charToIndex(word[depth]) root.children[index] = self.deleteUtil(root.children[index], word, depth + 1) isPrefix = False for node in root.children: if node is not None: isPrefix = True break if not isPrefix and root.isEndOfWord == False: root = None return root
CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR VAR NUMBER FUNC_DEF IF VAR NONE RETURN NONE IF VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NONE ASSIGN VAR NUMBER IF VAR ASSIGN VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NONE ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NONE RETURN VAR
Trie is an efficient information retrieval data structure. This data structure is used to store Strings and search strings, String stored can also be deleted. Given a Trie root for a larger string super and a string key, delete all the occurences of key in the Trie. Example 1: Input: N = 8 super = "the a there answer any by bye their" key = "the" Your Task: Complete the function deleteKey() to delete the given string key.The String key if exists in the larger string super, must be deleted from Trie root. The larger string super contains space separated small strings. And if the string is deleted successfully than 1 will be printed. If any other string other than String A is deleted, you will get wrong answer. Constraints: 1≤ T ≤200 1≤ N, |A| ≤20
class Solution: def deleteKey(self, root, key): if len(key) == 0: root.isEndOfWord = False return index = ord(key[0]) - ord("a") if root.children[index]: self.deleteKey(root.children[index], key[1:])
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER RETURN ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR STRING IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER
Trie is an efficient information retrieval data structure. This data structure is used to store Strings and search strings, String stored can also be deleted. Given a Trie root for a larger string super and a string key, delete all the occurences of key in the Trie. Example 1: Input: N = 8 super = "the a there answer any by bye their" key = "the" Your Task: Complete the function deleteKey() to delete the given string key.The String key if exists in the larger string super, must be deleted from Trie root. The larger string super contains space separated small strings. And if the string is deleted successfully than 1 will be printed. If any other string other than String A is deleted, you will get wrong answer. Constraints: 1≤ T ≤200 1≤ N, |A| ≤20
class Solution: def deleteKey(self, root, key): self.dele(root, key, 0) def dele(self, root, key, i): if i == len(key): return index = ord(key[i]) - ord("a") if not root.children[index]: return self.dele(root.children[index], key, i + 1) x = set(root.children[index].children) if len(x) > 1: root.children[index].isEndOfWord = False else: root.children[index] = None
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR VAR VAR NUMBER FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING IF VAR VAR RETURN EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NONE
Trie is an efficient information retrieval data structure. This data structure is used to store Strings and search strings, String stored can also be deleted. Given a Trie root for a larger string super and a string key, delete all the occurences of key in the Trie. Example 1: Input: N = 8 super = "the a there answer any by bye their" key = "the" Your Task: Complete the function deleteKey() to delete the given string key.The String key if exists in the larger string super, must be deleted from Trie root. The larger string super contains space separated small strings. And if the string is deleted successfully than 1 will be printed. If any other string other than String A is deleted, you will get wrong answer. Constraints: 1≤ T ≤200 1≤ N, |A| ≤20
class Solution: def deleteKey(self, root, key): r = root cnt = 1 for i in key: if r.children[ord(i) - ord("a")] == None: return root if cnt == len(key): if r.children[ord(i) - ord("a")].isEndOfWord: r.children[ord(i) - ord("a")].isEndOfWord = False r = r.children[ord(i) - ord("a")] cnt += 1 return root
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NONE RETURN VAR IF VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING VAR NUMBER RETURN VAR
Trie is an efficient information retrieval data structure. This data structure is used to store Strings and search strings, String stored can also be deleted. Given a Trie root for a larger string super and a string key, delete all the occurences of key in the Trie. Example 1: Input: N = 8 super = "the a there answer any by bye their" key = "the" Your Task: Complete the function deleteKey() to delete the given string key.The String key if exists in the larger string super, must be deleted from Trie root. The larger string super contains space separated small strings. And if the string is deleted successfully than 1 will be printed. If any other string other than String A is deleted, you will get wrong answer. Constraints: 1≤ T ≤200 1≤ N, |A| ≤20
class Solution: def deleteKey(self, root, key): if search(root, key): pcrawl = root length = len(key) for level in range(length): index = charToIndex(key[level]) pcrawl = pcrawl.children[index] pcrawl.isEndOfWord = False
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER
Trie is an efficient information retrieval data structure. This data structure is used to store Strings and search strings, String stored can also be deleted. Given a Trie root for a larger string super and a string key, delete all the occurences of key in the Trie. Example 1: Input: N = 8 super = "the a there answer any by bye their" key = "the" Your Task: Complete the function deleteKey() to delete the given string key.The String key if exists in the larger string super, must be deleted from Trie root. The larger string super contains space separated small strings. And if the string is deleted successfully than 1 will be printed. If any other string other than String A is deleted, you will get wrong answer. Constraints: 1≤ T ≤200 1≤ N, |A| ≤20
class Solution: def delete(self, CurrNode, key, pos): if CurrNode == None: return if pos == len(key): CurrNode.isEndOfWord = False return self.delete(CurrNode.children[ord(key[pos]) - ord("a")], key, pos + 1) def deleteKey(self, root, key): self.delete(root, key, 0)
CLASS_DEF FUNC_DEF IF VAR NONE RETURN IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING VAR BIN_OP VAR NUMBER FUNC_DEF EXPR FUNC_CALL VAR VAR VAR NUMBER
You are given a permutation p_1, p_2, ... , p_n (an array where each integer from 1 to n appears exactly once). The weight of the i-th element of this permutation is a_i. At first, you separate your permutation into two non-empty sets — prefix and suffix. More formally, the first set contains elements p_1, p_2, ... , p_k, the second — p_{k+1}, p_{k+2}, ... , p_n, where 1 ≤ k < n. After that, you may move elements between sets. The operation you are allowed to do is to choose some element of the first set and move it to the second set, or vice versa (move from the second set to the first). You have to pay a_i dollars to move the element p_i. Your goal is to make it so that each element of the first set is less than each element of the second set. Note that if one of the sets is empty, this condition is met. For example, if p = [3, 1, 2] and a = [7, 1, 4], then the optimal strategy is: separate p into two parts [3, 1] and [2] and then move the 2-element into first set (it costs 4). And if p = [3, 5, 1, 6, 2, 4], a = [9, 1, 9, 9, 1, 9], then the optimal strategy is: separate p into two parts [3, 5, 1] and [6, 2, 4], and then move the 2-element into first set (it costs 1), and 5-element into second set (it also costs 1). Calculate the minimum number of dollars you have to spend. Input The first line contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the length of permutation. The second line contains n integers p_1, p_2, ... , p_n (1 ≤ p_i ≤ n). It's guaranteed that this sequence contains each element from 1 to n exactly once. The third line contains n integers a_1, a_2, ... , a_n (1 ≤ a_i ≤ 10^9). Output Print one integer — the minimum number of dollars you have to spend. Examples Input 3 3 1 2 7 1 4 Output 4 Input 4 2 4 1 3 5 9 8 3 Output 3 Input 6 3 5 1 6 2 4 9 1 9 9 1 9 Output 2
import sys def main(): import sys input = sys.stdin.buffer.readline N = int(input()) P = list(map(int, input().split())) A = list(map(float, input().split())) class LazySegTree: def __init__(self, A, initialize=True, segfunc=min, ident=2000000000): self.N = len(A) self.LV = (self.N - 1).bit_length() self.N0 = 1 << self.LV self.segfunc = segfunc self.ident = ident if initialize: self.data = ( [self.ident] * self.N0 + A + [self.ident] * (self.N0 - self.N) ) for i in range(self.N0 - 1, 0, -1): self.data[i] = segfunc(self.data[i * 2], self.data[i * 2 + 1]) else: self.data = [self.ident] * (self.N0 * 2) self.lazy = [0] * (self.N0 * 2) def _ascend(self, i): for _ in range(i.bit_length() - 1): i >>= 1 self.data[i] = ( self.segfunc(self.data[i * 2], self.data[i * 2 + 1]) + self.lazy[i] ) def _descend(self, idx): lv = idx.bit_length() for j in range(lv - 1, 0, -1): i = idx >> j x = self.lazy[i] if not x: continue self.lazy[i] = 0 self.lazy[i * 2] += x self.lazy[i * 2 + 1] += x self.data[i * 2] += x self.data[i * 2 + 1] += x def add(self, l, r, x): l += self.N0 - 1 r += self.N0 - 1 l_ori = l r_ori = r while l < r: if l & 1: self.data[l] += x self.lazy[l] += x l += 1 if r & 1: r -= 1 self.data[r] += x self.lazy[r] += x l >>= 1 r >>= 1 self._ascend(l_ori // (l_ori & -l_ori)) self._ascend(r_ori // (r_ori & -r_ori) - 1) def query(self, l, r): l += self.N0 - 1 r += self.N0 - 1 self._descend(l // (l & -l)) self._descend(r // (r & -r) - 1) ret = self.ident while l < r: if l & 1: ret = self.segfunc(ret, self.data[l]) l += 1 if r & 1: ret = self.segfunc(self.data[r - 1], ret) r -= 1 l >>= 1 r >>= 1 return ret p2i = {} for i, p in enumerate(P): p2i[p] = i S = 0 init = [0.0] * N for i in range(1, N): S += A[i - 1] init[i - 1] = S segtree = LazySegTree(init, ident=float("inf")) ans = A[0] cnt = 0 for p in range(1, N + 1): i = p2i[p] a = A[i] cnt += a segtree.add(i + 1, N, -a * 2) ans = min(ans, segtree.query(1, N) + cnt) print(int(ans)) main()
IMPORT FUNC_DEF IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR CLASS_DEF FUNC_DEF NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP LIST VAR VAR VAR BIN_OP LIST VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR IF VAR ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR FUNC_DEF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER FUNC_DEF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given a permutation p_1, p_2, ... , p_n (an array where each integer from 1 to n appears exactly once). The weight of the i-th element of this permutation is a_i. At first, you separate your permutation into two non-empty sets — prefix and suffix. More formally, the first set contains elements p_1, p_2, ... , p_k, the second — p_{k+1}, p_{k+2}, ... , p_n, where 1 ≤ k < n. After that, you may move elements between sets. The operation you are allowed to do is to choose some element of the first set and move it to the second set, or vice versa (move from the second set to the first). You have to pay a_i dollars to move the element p_i. Your goal is to make it so that each element of the first set is less than each element of the second set. Note that if one of the sets is empty, this condition is met. For example, if p = [3, 1, 2] and a = [7, 1, 4], then the optimal strategy is: separate p into two parts [3, 1] and [2] and then move the 2-element into first set (it costs 4). And if p = [3, 5, 1, 6, 2, 4], a = [9, 1, 9, 9, 1, 9], then the optimal strategy is: separate p into two parts [3, 5, 1] and [6, 2, 4], and then move the 2-element into first set (it costs 1), and 5-element into second set (it also costs 1). Calculate the minimum number of dollars you have to spend. Input The first line contains one integer n (2 ≤ n ≤ 2 ⋅ 10^5) — the length of permutation. The second line contains n integers p_1, p_2, ... , p_n (1 ≤ p_i ≤ n). It's guaranteed that this sequence contains each element from 1 to n exactly once. The third line contains n integers a_1, a_2, ... , a_n (1 ≤ a_i ≤ 10^9). Output Print one integer — the minimum number of dollars you have to spend. Examples Input 3 3 1 2 7 1 4 Output 4 Input 4 2 4 1 3 5 9 8 3 Output 3 Input 6 3 5 1 6 2 4 9 1 9 9 1 9 Output 2
import sys from itertools import accumulate input = sys.stdin.readline n = int(input()) P = tuple(map(int, input().split())) A = tuple(map(int, input().split())) seg_el = 1 << (n + 1).bit_length() SEGMIN = [0] * (2 * seg_el) SEGADD = [0] * (2 * seg_el) for i in range(n): SEGMIN[P[i] + seg_el] = A[i] SEGMIN = list(accumulate(SEGMIN)) for i in range(seg_el - 1, 0, -1): SEGMIN[i] = min(SEGMIN[i * 2], SEGMIN[i * 2 + 1]) def adds(l, r, x): L = l + seg_el R = r + seg_el while L < R: if L & 1: SEGADD[L] += x L += 1 if R & 1: R -= 1 SEGADD[R] += x L >>= 1 R >>= 1 L = l + seg_el >> 1 R = r + seg_el - 1 >> 1 while L != R: if L > R: SEGMIN[L] = min( SEGMIN[L * 2] + SEGADD[L * 2], SEGMIN[1 + L * 2] + SEGADD[1 + L * 2] ) L >>= 1 else: SEGMIN[R] = min( SEGMIN[R * 2] + SEGADD[R * 2], SEGMIN[1 + R * 2] + SEGADD[1 + R * 2] ) R >>= 1 while L != 0: SEGMIN[L] = min( SEGMIN[L * 2] + SEGADD[L * 2], SEGMIN[1 + L * 2] + SEGADD[1 + L * 2] ) L >>= 1 S = tuple(accumulate(A)) ANS = 1 << 31 for i in range(n - 1): adds(P[i], n + 1, -A[i] * 2) ANS = min(ANS, SEGMIN[1] + S[i]) print(ANS)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR WHILE VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER WHILE VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR BIN_OP NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR
Given inorder and postorder traversals of a Binary Tree in the arrays in[] and post[] respectively. The task is to construct the binary tree from these traversals. Example 1: Input: N = 8 in[] = 4 8 2 5 1 6 3 7 post[] =8 4 5 2 6 7 3 1 Output: 1 2 4 8 5 3 6 7 Explanation: For the given postorder and inorder traversal of tree the resultant binary tree will be 1 / \ 2 3 / \ / \ 4 5 6 7 \ 8 Example 2: Input: N = 5 in[] = 9 5 2 3 4 post[] = 5 9 3 4 2 Output: 2 9 5 4 3 Explanation: the resultant binary tree will be 2 / \ 9 4 \ / 5 3 Your Task: You do not need to read input or print anything. Complete the function buildTree() which takes the inorder, postorder traversals and the number of nodes in the tree as input parameters and returns the root node of the newly constructed Binary Tree. The generated output contains the preorder traversal of the constructed tree. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 <= N <= 10^{3} 1 <= in[i], post[i] <= 10^{3}
def Search(start, end, value, inorder): for indx in range(start, end + 1): if value == inorder[indx]: return indx return -1 def BuildTree(In, post, postindx, instart, inend): if instart > inend: return None if postindx[0] < 0: return None root = Node(post[postindx[0]]) inindx = Search(instart, inend, post[postindx[0]], In) postindx[0] -= 1 root.right = BuildTree(In, post, postindx, inindx + 1, inend) root.left = BuildTree(In, post, postindx, instart, inindx - 1) return root def buildTree(In, post, n): postindx = [n - 1] return BuildTree(In, post, postindx, 0, n - 1)
FUNC_DEF FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR RETURN VAR RETURN NUMBER FUNC_DEF IF VAR VAR RETURN NONE IF VAR NUMBER NUMBER RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR LIST BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR NUMBER BIN_OP VAR NUMBER
Given inorder and postorder traversals of a Binary Tree in the arrays in[] and post[] respectively. The task is to construct the binary tree from these traversals. Example 1: Input: N = 8 in[] = 4 8 2 5 1 6 3 7 post[] =8 4 5 2 6 7 3 1 Output: 1 2 4 8 5 3 6 7 Explanation: For the given postorder and inorder traversal of tree the resultant binary tree will be 1 / \ 2 3 / \ / \ 4 5 6 7 \ 8 Example 2: Input: N = 5 in[] = 9 5 2 3 4 post[] = 5 9 3 4 2 Output: 2 9 5 4 3 Explanation: the resultant binary tree will be 2 / \ 9 4 \ / 5 3 Your Task: You do not need to read input or print anything. Complete the function buildTree() which takes the inorder, postorder traversals and the number of nodes in the tree as input parameters and returns the root node of the newly constructed Binary Tree. The generated output contains the preorder traversal of the constructed tree. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 <= N <= 10^{3} 1 <= in[i], post[i] <= 10^{3}
def buildTree(io, po, n): if len(io) != len(po): return None def util(d, io, io1, io2, po, po1, po2): if io1 > io2 or po1 > po2: return None root = Node(po[po2]) ind = d[po[po2]] if io1 != io2: root.left = util(d, io, io1, ind - 1, po, po1, po1 + ind - io1 - 1) root.right = util(d, io, ind + 1, io2, po, po1 + ind - io1, po2 - 1) return root n = len(io) d = {} for i in range(n): d[io[i]] = i return util(d, io, 0, n - 1, po, 0, n - 1)
FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NONE FUNC_DEF IF VAR VAR VAR VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER
Given inorder and postorder traversals of a Binary Tree in the arrays in[] and post[] respectively. The task is to construct the binary tree from these traversals. Example 1: Input: N = 8 in[] = 4 8 2 5 1 6 3 7 post[] =8 4 5 2 6 7 3 1 Output: 1 2 4 8 5 3 6 7 Explanation: For the given postorder and inorder traversal of tree the resultant binary tree will be 1 / \ 2 3 / \ / \ 4 5 6 7 \ 8 Example 2: Input: N = 5 in[] = 9 5 2 3 4 post[] = 5 9 3 4 2 Output: 2 9 5 4 3 Explanation: the resultant binary tree will be 2 / \ 9 4 \ / 5 3 Your Task: You do not need to read input or print anything. Complete the function buildTree() which takes the inorder, postorder traversals and the number of nodes in the tree as input parameters and returns the root node of the newly constructed Binary Tree. The generated output contains the preorder traversal of the constructed tree. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 <= N <= 10^{3} 1 <= in[i], post[i] <= 10^{3}
def buildTree(In, post, n=None): if not In or not post: return None root_value = post[-1] root = Node(root_value) root_index = In.index(root_value) if n is None: n = len(In) leftsize = root_index rightsize = n - root_index - 1 root.left = buildTree(In[:root_index], post[:root_index], leftsize) root.right = buildTree( In[root_index + 1 :], post[leftsize : leftsize + rightsize], rightsize ) return root
FUNC_DEF NONE IF VAR VAR RETURN NONE ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR RETURN VAR
Given inorder and postorder traversals of a Binary Tree in the arrays in[] and post[] respectively. The task is to construct the binary tree from these traversals. Example 1: Input: N = 8 in[] = 4 8 2 5 1 6 3 7 post[] =8 4 5 2 6 7 3 1 Output: 1 2 4 8 5 3 6 7 Explanation: For the given postorder and inorder traversal of tree the resultant binary tree will be 1 / \ 2 3 / \ / \ 4 5 6 7 \ 8 Example 2: Input: N = 5 in[] = 9 5 2 3 4 post[] = 5 9 3 4 2 Output: 2 9 5 4 3 Explanation: the resultant binary tree will be 2 / \ 9 4 \ / 5 3 Your Task: You do not need to read input or print anything. Complete the function buildTree() which takes the inorder, postorder traversals and the number of nodes in the tree as input parameters and returns the root node of the newly constructed Binary Tree. The generated output contains the preorder traversal of the constructed tree. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 <= N <= 10^{3} 1 <= in[i], post[i] <= 10^{3}
def build(ino, post, h, ind, i, j): if i > j: return None root = Node(post[ind[0]]) ind[0] -= 1 if i == j: return root root.right = build(ino, post, h, ind, h[root.data] + 1, j) root.left = build(ino, post, h, ind, i, h[root.data] - 1) return root def buildTree(ino, post, n): posind = n - 1 h = {} for i in range(len(ino)): h[ino[i]] = i return build(ino, post, h, [posind], 0, n - 1)
FUNC_DEF IF VAR VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER NUMBER IF VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR LIST VAR NUMBER BIN_OP VAR NUMBER
Given inorder and postorder traversals of a Binary Tree in the arrays in[] and post[] respectively. The task is to construct the binary tree from these traversals. Example 1: Input: N = 8 in[] = 4 8 2 5 1 6 3 7 post[] =8 4 5 2 6 7 3 1 Output: 1 2 4 8 5 3 6 7 Explanation: For the given postorder and inorder traversal of tree the resultant binary tree will be 1 / \ 2 3 / \ / \ 4 5 6 7 \ 8 Example 2: Input: N = 5 in[] = 9 5 2 3 4 post[] = 5 9 3 4 2 Output: 2 9 5 4 3 Explanation: the resultant binary tree will be 2 / \ 9 4 \ / 5 3 Your Task: You do not need to read input or print anything. Complete the function buildTree() which takes the inorder, postorder traversals and the number of nodes in the tree as input parameters and returns the root node of the newly constructed Binary Tree. The generated output contains the preorder traversal of the constructed tree. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 <= N <= 10^{3} 1 <= in[i], post[i] <= 10^{3}
def buildTree(inorder, postorder, n): inomap = {} for i, el in enumerate(inorder): inomap[el] = i def get_tree(isi, iei, ppsi, ppei): if ppei < ppsi: return None inidx = inomap[postorder[ppei]] l = inidx - isi root = Node(postorder[ppei]) if ppsi == ppei: return root root.left = get_tree(isi, inidx - 1, ppsi, ppsi + l - 1) root.right = get_tree(inidx + 1, iei, ppsi + l, ppei - 1) return root return get_tree(0, len(inorder) - 1, 0, len(postorder) - 1)
FUNC_DEF ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_DEF IF VAR VAR RETURN NONE ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER
Given inorder and postorder traversals of a Binary Tree in the arrays in[] and post[] respectively. The task is to construct the binary tree from these traversals. Example 1: Input: N = 8 in[] = 4 8 2 5 1 6 3 7 post[] =8 4 5 2 6 7 3 1 Output: 1 2 4 8 5 3 6 7 Explanation: For the given postorder and inorder traversal of tree the resultant binary tree will be 1 / \ 2 3 / \ / \ 4 5 6 7 \ 8 Example 2: Input: N = 5 in[] = 9 5 2 3 4 post[] = 5 9 3 4 2 Output: 2 9 5 4 3 Explanation: the resultant binary tree will be 2 / \ 9 4 \ / 5 3 Your Task: You do not need to read input or print anything. Complete the function buildTree() which takes the inorder, postorder traversals and the number of nodes in the tree as input parameters and returns the root node of the newly constructed Binary Tree. The generated output contains the preorder traversal of the constructed tree. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 <= N <= 10^{3} 1 <= in[i], post[i] <= 10^{3}
def buildTree(In, post, n): i = [n - 1] mydict = {} for j in range(n): mydict[In[j]] = j def construct(start, end): if start > end: return None root = Node(post[i[0]]) i[0] -= 1 if start == end: return root middle = mydict[root.data] root.right = construct(middle + 1, end) root.left = construct(start, middle - 1) return root return construct(0, n - 1)
FUNC_DEF ASSIGN VAR LIST BIN_OP VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_DEF IF VAR VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER NUMBER IF VAR VAR RETURN VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER
Given inorder and postorder traversals of a Binary Tree in the arrays in[] and post[] respectively. The task is to construct the binary tree from these traversals. Example 1: Input: N = 8 in[] = 4 8 2 5 1 6 3 7 post[] =8 4 5 2 6 7 3 1 Output: 1 2 4 8 5 3 6 7 Explanation: For the given postorder and inorder traversal of tree the resultant binary tree will be 1 / \ 2 3 / \ / \ 4 5 6 7 \ 8 Example 2: Input: N = 5 in[] = 9 5 2 3 4 post[] = 5 9 3 4 2 Output: 2 9 5 4 3 Explanation: the resultant binary tree will be 2 / \ 9 4 \ / 5 3 Your Task: You do not need to read input or print anything. Complete the function buildTree() which takes the inorder, postorder traversals and the number of nodes in the tree as input parameters and returns the root node of the newly constructed Binary Tree. The generated output contains the preorder traversal of the constructed tree. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 <= N <= 10^{3} 1 <= in[i], post[i] <= 10^{3}
def _buildtree(postorder, poststart, postend, inorder, instart, inend, hashmap): if instart > inend or poststart > postend: return None root = Node(postorder[postend]) inroot = hashmap[postorder[postend]] numsleft = inroot - instart root.left = _buildtree( postorder, poststart, poststart + numsleft - 1, inorder, instart, inroot - 1, hashmap, ) root.right = _buildtree( postorder, poststart + numsleft, postend - 1, inorder, inroot + 1, inend, hashmap, ) return root def buildTree(inorder, postorder, n): if len(inorder) != len(postorder): return None hashmap = {} for i, num in enumerate(inorder): hashmap[num] = i return _buildtree(postorder, 0, n - 1, inorder, 0, n - 1, hashmap)
FUNC_DEF IF VAR VAR VAR VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR FUNC_DEF IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NONE ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR RETURN FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR
Given inorder and postorder traversals of a Binary Tree in the arrays in[] and post[] respectively. The task is to construct the binary tree from these traversals. Example 1: Input: N = 8 in[] = 4 8 2 5 1 6 3 7 post[] =8 4 5 2 6 7 3 1 Output: 1 2 4 8 5 3 6 7 Explanation: For the given postorder and inorder traversal of tree the resultant binary tree will be 1 / \ 2 3 / \ / \ 4 5 6 7 \ 8 Example 2: Input: N = 5 in[] = 9 5 2 3 4 post[] = 5 9 3 4 2 Output: 2 9 5 4 3 Explanation: the resultant binary tree will be 2 / \ 9 4 \ / 5 3 Your Task: You do not need to read input or print anything. Complete the function buildTree() which takes the inorder, postorder traversals and the number of nodes in the tree as input parameters and returns the root node of the newly constructed Binary Tree. The generated output contains the preorder traversal of the constructed tree. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 <= N <= 10^{3} 1 <= in[i], post[i] <= 10^{3}
def buildTree(inorder, postorder, n): postorder = postorder[::-1] def sol(post, ino): if not post and not ino: return None mid_val = ino.index(post[0]) root = Node(post[0]) root.left = sol(post[len(post) - mid_val :], ino[:mid_val]) root.right = sol(post[1 : len(post) - mid_val], ino[mid_val + 1 :]) return root return sol(postorder, inorder)
FUNC_DEF ASSIGN VAR VAR NUMBER FUNC_DEF IF VAR VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR VAR
Given inorder and postorder traversals of a Binary Tree in the arrays in[] and post[] respectively. The task is to construct the binary tree from these traversals. Example 1: Input: N = 8 in[] = 4 8 2 5 1 6 3 7 post[] =8 4 5 2 6 7 3 1 Output: 1 2 4 8 5 3 6 7 Explanation: For the given postorder and inorder traversal of tree the resultant binary tree will be 1 / \ 2 3 / \ / \ 4 5 6 7 \ 8 Example 2: Input: N = 5 in[] = 9 5 2 3 4 post[] = 5 9 3 4 2 Output: 2 9 5 4 3 Explanation: the resultant binary tree will be 2 / \ 9 4 \ / 5 3 Your Task: You do not need to read input or print anything. Complete the function buildTree() which takes the inorder, postorder traversals and the number of nodes in the tree as input parameters and returns the root node of the newly constructed Binary Tree. The generated output contains the preorder traversal of the constructed tree. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 <= N <= 10^{3} 1 <= in[i], post[i] <= 10^{3}
def buildTree(In, post, n): poststart = instart = 0 postend = inend = n - 1 return constructTree(post, poststart, postend, In, instart, inend) def search(In, instart, inend, val): for i in range(instart, inend + 1): if In[i] == val: return i def constructTree(post, poststart, postend, In, instart, inend): if poststart > postend or instart > inend: return None root = Node(post[postend]) elem = search(In, instart, inend, root.data) nelem = elem - instart root.left = constructTree( post, poststart, poststart + nelem - 1, In, instart, elem - 1 ) root.right = constructTree( post, poststart + nelem, postend - 1, In, elem + 1, inend ) return root
FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR RETURN VAR FUNC_DEF IF VAR VAR VAR VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR RETURN VAR
Given inorder and postorder traversals of a Binary Tree in the arrays in[] and post[] respectively. The task is to construct the binary tree from these traversals. Example 1: Input: N = 8 in[] = 4 8 2 5 1 6 3 7 post[] =8 4 5 2 6 7 3 1 Output: 1 2 4 8 5 3 6 7 Explanation: For the given postorder and inorder traversal of tree the resultant binary tree will be 1 / \ 2 3 / \ / \ 4 5 6 7 \ 8 Example 2: Input: N = 5 in[] = 9 5 2 3 4 post[] = 5 9 3 4 2 Output: 2 9 5 4 3 Explanation: the resultant binary tree will be 2 / \ 9 4 \ / 5 3 Your Task: You do not need to read input or print anything. Complete the function buildTree() which takes the inorder, postorder traversals and the number of nodes in the tree as input parameters and returns the root node of the newly constructed Binary Tree. The generated output contains the preorder traversal of the constructed tree. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 <= N <= 10^{3} 1 <= in[i], post[i] <= 10^{3}
def buildTree(In, post, n): inorderMap = {} for i, e in enumerate(In): inorderMap[e] = i i = n - 1 def buildTreeUtil(l, r): nonlocal i, inorderMap if l >= r: return None else: n = Node(post[i]) i -= 1 n.right = buildTreeUtil(inorderMap[n.data] + 1, r) n.left = buildTreeUtil(l, inorderMap[n.data]) return n return buildTreeUtil(0, n)
FUNC_DEF ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR NUMBER VAR
Given inorder and postorder traversals of a Binary Tree in the arrays in[] and post[] respectively. The task is to construct the binary tree from these traversals. Example 1: Input: N = 8 in[] = 4 8 2 5 1 6 3 7 post[] =8 4 5 2 6 7 3 1 Output: 1 2 4 8 5 3 6 7 Explanation: For the given postorder and inorder traversal of tree the resultant binary tree will be 1 / \ 2 3 / \ / \ 4 5 6 7 \ 8 Example 2: Input: N = 5 in[] = 9 5 2 3 4 post[] = 5 9 3 4 2 Output: 2 9 5 4 3 Explanation: the resultant binary tree will be 2 / \ 9 4 \ / 5 3 Your Task: You do not need to read input or print anything. Complete the function buildTree() which takes the inorder, postorder traversals and the number of nodes in the tree as input parameters and returns the root node of the newly constructed Binary Tree. The generated output contains the preorder traversal of the constructed tree. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 <= N <= 10^{3} 1 <= in[i], post[i] <= 10^{3}
import sys def buildTree(inorder, postorder, n): inMap = {} import sys sys.setrecursionlimit(1000000) for i, num in enumerate(inorder): if inMap.get(num, sys.maxsize) == sys.maxsize: inMap[num] = [i] else: inMap[num].append(i) return buildTreeUtil( inorder, 0, len(inorder) - 1, postorder, 0, len(postorder) - 1, inMap ) def buildTreeUtil(inorder, inStart, inEnd, postorder, postStart, postEnd, inMap): if inStart > inEnd or postStart > postEnd: return None num = postorder[postEnd] node = Node(num) inRoot = findInRoot(num, inStart, inMap) numsLeft = inRoot - inStart node.left = buildTreeUtil( inorder, inStart, inRoot - 1, postorder, postStart, postStart + numsLeft - 1, inMap, ) node.right = buildTreeUtil( inorder, inRoot + 1, inEnd, postorder, postStart + numsLeft, postEnd - 1, inMap ) return node def findInRoot(num, inStart, inMap): inFind = inMap[num] if len(inFind) == 1: return inFind[0] else: for each in inFind: if each >= inStart: return each
IMPORT FUNC_DEF ASSIGN VAR DICT IMPORT EXPR FUNC_CALL VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR LIST VAR EXPR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_DEF IF VAR VAR VAR VAR RETURN NONE ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER FOR VAR VAR IF VAR VAR RETURN VAR
Given inorder and postorder traversals of a Binary Tree in the arrays in[] and post[] respectively. The task is to construct the binary tree from these traversals. Example 1: Input: N = 8 in[] = 4 8 2 5 1 6 3 7 post[] =8 4 5 2 6 7 3 1 Output: 1 2 4 8 5 3 6 7 Explanation: For the given postorder and inorder traversal of tree the resultant binary tree will be 1 / \ 2 3 / \ / \ 4 5 6 7 \ 8 Example 2: Input: N = 5 in[] = 9 5 2 3 4 post[] = 5 9 3 4 2 Output: 2 9 5 4 3 Explanation: the resultant binary tree will be 2 / \ 9 4 \ / 5 3 Your Task: You do not need to read input or print anything. Complete the function buildTree() which takes the inorder, postorder traversals and the number of nodes in the tree as input parameters and returns the root node of the newly constructed Binary Tree. The generated output contains the preorder traversal of the constructed tree. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 <= N <= 10^{3} 1 <= in[i], post[i] <= 10^{3}
def buildTree(ino, post, n): if not ino or not post: return None root = Node(post[-1]) m = post.index(root.data) k = ino.index(root.data) root.left = buildTree(ino[:k], post[:k], n) root.right = buildTree(ino[k + 1 :], post[k:m], n) return root
FUNC_DEF IF VAR VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN VAR
Given inorder and postorder traversals of a Binary Tree in the arrays in[] and post[] respectively. The task is to construct the binary tree from these traversals. Example 1: Input: N = 8 in[] = 4 8 2 5 1 6 3 7 post[] =8 4 5 2 6 7 3 1 Output: 1 2 4 8 5 3 6 7 Explanation: For the given postorder and inorder traversal of tree the resultant binary tree will be 1 / \ 2 3 / \ / \ 4 5 6 7 \ 8 Example 2: Input: N = 5 in[] = 9 5 2 3 4 post[] = 5 9 3 4 2 Output: 2 9 5 4 3 Explanation: the resultant binary tree will be 2 / \ 9 4 \ / 5 3 Your Task: You do not need to read input or print anything. Complete the function buildTree() which takes the inorder, postorder traversals and the number of nodes in the tree as input parameters and returns the root node of the newly constructed Binary Tree. The generated output contains the preorder traversal of the constructed tree. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 <= N <= 10^{3} 1 <= in[i], post[i] <= 10^{3}
def build(In, s1, e1, post, s2, e2): if s1 > e1: return None node = Node(post[e2]) idx = In.index(post[e2]) node.left = build(In, s1, idx - 1, post, s2, idx - 1 - s1 + s2) node.right = build(In, idx + 1, e1, post, e2 - e1 + idx, e2 - 1) return node def buildTree(In, post, n): return build(In, 0, n - 1, post, 0, n - 1)
FUNC_DEF IF VAR VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER
Given inorder and postorder traversals of a Binary Tree in the arrays in[] and post[] respectively. The task is to construct the binary tree from these traversals. Example 1: Input: N = 8 in[] = 4 8 2 5 1 6 3 7 post[] =8 4 5 2 6 7 3 1 Output: 1 2 4 8 5 3 6 7 Explanation: For the given postorder and inorder traversal of tree the resultant binary tree will be 1 / \ 2 3 / \ / \ 4 5 6 7 \ 8 Example 2: Input: N = 5 in[] = 9 5 2 3 4 post[] = 5 9 3 4 2 Output: 2 9 5 4 3 Explanation: the resultant binary tree will be 2 / \ 9 4 \ / 5 3 Your Task: You do not need to read input or print anything. Complete the function buildTree() which takes the inorder, postorder traversals and the number of nodes in the tree as input parameters and returns the root node of the newly constructed Binary Tree. The generated output contains the preorder traversal of the constructed tree. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 <= N <= 10^{3} 1 <= in[i], post[i] <= 10^{3}
def buildTree(In, post, n): def buildtree(ino, ist, ied, pos, pst, ped, mp): if ist > ied or pst > ped: return None root = Node(pos[ped]) inroot = mp[root.data] numsleft = inroot - ist root.left = buildtree(ino, ist, inroot - 1, pos, pst, pst + numsleft - 1, mp) root.right = buildtree(ino, inroot + 1, ied, pos, pst + numsleft, ped - 1, mp) return root def build(ino, pos, n): mp = {} for i in range(n): mp[ino[i]] = mp.get(ino[i], 0) mp[ino[i]] = i root = buildtree(ino, 0, n - 1, pos, 0, n - 1, mp) return root return build(In, post, n)
FUNC_DEF FUNC_DEF IF VAR VAR VAR VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR RETURN VAR RETURN FUNC_CALL VAR VAR VAR VAR
Given inorder and postorder traversals of a Binary Tree in the arrays in[] and post[] respectively. The task is to construct the binary tree from these traversals. Example 1: Input: N = 8 in[] = 4 8 2 5 1 6 3 7 post[] =8 4 5 2 6 7 3 1 Output: 1 2 4 8 5 3 6 7 Explanation: For the given postorder and inorder traversal of tree the resultant binary tree will be 1 / \ 2 3 / \ / \ 4 5 6 7 \ 8 Example 2: Input: N = 5 in[] = 9 5 2 3 4 post[] = 5 9 3 4 2 Output: 2 9 5 4 3 Explanation: the resultant binary tree will be 2 / \ 9 4 \ / 5 3 Your Task: You do not need to read input or print anything. Complete the function buildTree() which takes the inorder, postorder traversals and the number of nodes in the tree as input parameters and returns the root node of the newly constructed Binary Tree. The generated output contains the preorder traversal of the constructed tree. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 <= N <= 10^{3} 1 <= in[i], post[i] <= 10^{3}
def buildTree(In, post, n): inorderidx = {v: i for i, v in enumerate(In)} def helper(l, r): if l > r: return None root = Node(post.pop()) idx = inorderidx[root.data] root.right = helper(idx + 1, r) root.left = helper(l, idx - 1) return root return helper(0, len(In) - 1)
FUNC_DEF ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER
Given inorder and postorder traversals of a Binary Tree in the arrays in[] and post[] respectively. The task is to construct the binary tree from these traversals. Example 1: Input: N = 8 in[] = 4 8 2 5 1 6 3 7 post[] =8 4 5 2 6 7 3 1 Output: 1 2 4 8 5 3 6 7 Explanation: For the given postorder and inorder traversal of tree the resultant binary tree will be 1 / \ 2 3 / \ / \ 4 5 6 7 \ 8 Example 2: Input: N = 5 in[] = 9 5 2 3 4 post[] = 5 9 3 4 2 Output: 2 9 5 4 3 Explanation: the resultant binary tree will be 2 / \ 9 4 \ / 5 3 Your Task: You do not need to read input or print anything. Complete the function buildTree() which takes the inorder, postorder traversals and the number of nodes in the tree as input parameters and returns the root node of the newly constructed Binary Tree. The generated output contains the preorder traversal of the constructed tree. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 <= N <= 10^{3} 1 <= in[i], post[i] <= 10^{3}
def buildTree(In, post, n): d = {} for i in range(n): if In[i] in d: d[In[i]].append(i) else: d[In[i]] = [i] def helper(inorder, postorder, li, ri, lp, rp): if li > ri or lp > rp: return None root = Node(postorder[rp]) indices = d[postorder[rp]] for i in indices: if li <= i <= ri: index = i break rightSize, leftSize = ri - index, index - li root.left = helper(inorder, postorder, li, index - 1, lp, lp + leftSize - 1) root.right = helper(inorder, postorder, index + 1, ri, lp + leftSize, rp - 1) return root return helper(In, post, 0, n - 1, 0, n - 1)
FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR FUNC_DEF IF VAR VAR VAR VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER
Given inorder and postorder traversals of a Binary Tree in the arrays in[] and post[] respectively. The task is to construct the binary tree from these traversals. Example 1: Input: N = 8 in[] = 4 8 2 5 1 6 3 7 post[] =8 4 5 2 6 7 3 1 Output: 1 2 4 8 5 3 6 7 Explanation: For the given postorder and inorder traversal of tree the resultant binary tree will be 1 / \ 2 3 / \ / \ 4 5 6 7 \ 8 Example 2: Input: N = 5 in[] = 9 5 2 3 4 post[] = 5 9 3 4 2 Output: 2 9 5 4 3 Explanation: the resultant binary tree will be 2 / \ 9 4 \ / 5 3 Your Task: You do not need to read input or print anything. Complete the function buildTree() which takes the inorder, postorder traversals and the number of nodes in the tree as input parameters and returns the root node of the newly constructed Binary Tree. The generated output contains the preorder traversal of the constructed tree. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 <= N <= 10^{3} 1 <= in[i], post[i] <= 10^{3}
def buildTree(In, post, n): if not In or not post: return None root = Node(post[n - 1]) index = In.index(post[n - 1]) root.left = buildTree(In[:index], post[:index], len(post[:index])) root.right = buildTree( In[index + 1 :], post[index : n - 1], len(post[index : n - 1]) ) return root
FUNC_DEF IF VAR VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR
Given inorder and postorder traversals of a Binary Tree in the arrays in[] and post[] respectively. The task is to construct the binary tree from these traversals. Example 1: Input: N = 8 in[] = 4 8 2 5 1 6 3 7 post[] =8 4 5 2 6 7 3 1 Output: 1 2 4 8 5 3 6 7 Explanation: For the given postorder and inorder traversal of tree the resultant binary tree will be 1 / \ 2 3 / \ / \ 4 5 6 7 \ 8 Example 2: Input: N = 5 in[] = 9 5 2 3 4 post[] = 5 9 3 4 2 Output: 2 9 5 4 3 Explanation: the resultant binary tree will be 2 / \ 9 4 \ / 5 3 Your Task: You do not need to read input or print anything. Complete the function buildTree() which takes the inorder, postorder traversals and the number of nodes in the tree as input parameters and returns the root node of the newly constructed Binary Tree. The generated output contains the preorder traversal of the constructed tree. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 <= N <= 10^{3} 1 <= in[i], post[i] <= 10^{3}
pind = 0 def buildTree(inorder, postorder, n=8): if not inorder or not postorder: return None root_val = postorder[-1] root = Node(root_val) idx = inorder.index(root_val) root.left = buildTree(inorder[:idx], postorder[:idx]) root.right = buildTree(inorder[idx + 1 :], postorder[idx:-1]) return root
ASSIGN VAR NUMBER FUNC_DEF NUMBER IF VAR VAR RETURN NONE ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER RETURN VAR
Given inorder and postorder traversals of a Binary Tree in the arrays in[] and post[] respectively. The task is to construct the binary tree from these traversals. Example 1: Input: N = 8 in[] = 4 8 2 5 1 6 3 7 post[] =8 4 5 2 6 7 3 1 Output: 1 2 4 8 5 3 6 7 Explanation: For the given postorder and inorder traversal of tree the resultant binary tree will be 1 / \ 2 3 / \ / \ 4 5 6 7 \ 8 Example 2: Input: N = 5 in[] = 9 5 2 3 4 post[] = 5 9 3 4 2 Output: 2 9 5 4 3 Explanation: the resultant binary tree will be 2 / \ 9 4 \ / 5 3 Your Task: You do not need to read input or print anything. Complete the function buildTree() which takes the inorder, postorder traversals and the number of nodes in the tree as input parameters and returns the root node of the newly constructed Binary Tree. The generated output contains the preorder traversal of the constructed tree. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 <= N <= 10^{3} 1 <= in[i], post[i] <= 10^{3}
def buildTree(In, post, n): if not In or not post: return None idx = In.index(post[-1]) root = Node(post.pop()) root.right = buildTree(In[idx + 1 :], post, n) root.left = buildTree(In[:idx], post, n) return root
FUNC_DEF IF VAR VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR
Given inorder and postorder traversals of a Binary Tree in the arrays in[] and post[] respectively. The task is to construct the binary tree from these traversals. Example 1: Input: N = 8 in[] = 4 8 2 5 1 6 3 7 post[] =8 4 5 2 6 7 3 1 Output: 1 2 4 8 5 3 6 7 Explanation: For the given postorder and inorder traversal of tree the resultant binary tree will be 1 / \ 2 3 / \ / \ 4 5 6 7 \ 8 Example 2: Input: N = 5 in[] = 9 5 2 3 4 post[] = 5 9 3 4 2 Output: 2 9 5 4 3 Explanation: the resultant binary tree will be 2 / \ 9 4 \ / 5 3 Your Task: You do not need to read input or print anything. Complete the function buildTree() which takes the inorder, postorder traversals and the number of nodes in the tree as input parameters and returns the root node of the newly constructed Binary Tree. The generated output contains the preorder traversal of the constructed tree. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 <= N <= 10^{3} 1 <= in[i], post[i] <= 10^{3}
def build(inorder, postorder, start_in, end_in, start_post, end_post, dic): if start_post > end_post or start_in > end_in: return None root = Node(postorder[end_post]) index_root = [x for x in dic[root.data] if start_in <= x <= end_in] num_left = index_root[0] - start_in root.left = build( inorder, postorder, start_in, index_root[0] - 1, start_post, start_post + num_left - 1, dic, ) root.right = build( inorder, postorder, index_root[0] + 1, end_in, start_post + num_left, end_post - 1, dic, ) return root def buildTree(inorder, postorder, n): dic = {} for i in range(n): if inorder[i] in dic: dic[inorder[i]].append(i) else: dic[inorder[i]] = [i] return build(inorder, postorder, 0, n - 1, 0, n - 1, dic)
FUNC_DEF IF VAR VAR VAR VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR LIST VAR RETURN FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR
Given inorder and postorder traversals of a Binary Tree in the arrays in[] and post[] respectively. The task is to construct the binary tree from these traversals. Example 1: Input: N = 8 in[] = 4 8 2 5 1 6 3 7 post[] =8 4 5 2 6 7 3 1 Output: 1 2 4 8 5 3 6 7 Explanation: For the given postorder and inorder traversal of tree the resultant binary tree will be 1 / \ 2 3 / \ / \ 4 5 6 7 \ 8 Example 2: Input: N = 5 in[] = 9 5 2 3 4 post[] = 5 9 3 4 2 Output: 2 9 5 4 3 Explanation: the resultant binary tree will be 2 / \ 9 4 \ / 5 3 Your Task: You do not need to read input or print anything. Complete the function buildTree() which takes the inorder, postorder traversals and the number of nodes in the tree as input parameters and returns the root node of the newly constructed Binary Tree. The generated output contains the preorder traversal of the constructed tree. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 <= N <= 10^{3} 1 <= in[i], post[i] <= 10^{3}
def buildTree(In, post, n): return helper(0, n - 1, post, In, n - 1)[0] def helper(start, stop, post, In, p): if p < 0: return None, -1 if start > stop: return None, p for i in range(start, stop + 1): if post[p] == In[i]: right, p = helper(i + 1, stop, post, In, p - 1) left, p = helper(start, i - 1, post, In, p) tree = Node(In[i]) tree.left = left tree.right = right return tree, p return None
FUNC_DEF RETURN FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_DEF IF VAR NUMBER RETURN NONE NUMBER IF VAR VAR RETURN NONE VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR VAR RETURN NONE
Given inorder and postorder traversals of a Binary Tree in the arrays in[] and post[] respectively. The task is to construct the binary tree from these traversals. Example 1: Input: N = 8 in[] = 4 8 2 5 1 6 3 7 post[] =8 4 5 2 6 7 3 1 Output: 1 2 4 8 5 3 6 7 Explanation: For the given postorder and inorder traversal of tree the resultant binary tree will be 1 / \ 2 3 / \ / \ 4 5 6 7 \ 8 Example 2: Input: N = 5 in[] = 9 5 2 3 4 post[] = 5 9 3 4 2 Output: 2 9 5 4 3 Explanation: the resultant binary tree will be 2 / \ 9 4 \ / 5 3 Your Task: You do not need to read input or print anything. Complete the function buildTree() which takes the inorder, postorder traversals and the number of nodes in the tree as input parameters and returns the root node of the newly constructed Binary Tree. The generated output contains the preorder traversal of the constructed tree. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 <= N <= 10^{3} 1 <= in[i], post[i] <= 10^{3}
def buildTree(inorder, postorder, n): if inorder == None or len(inorder) != len(postorder): return None a = {} for i in range(len(inorder)): a[inorder[i]] = i def createTree(inorder, ip, ie, postorder, pp, pe, a): root = Node(postorder[pe]) if pp > pe or ip > ie: return None inRoot = a[postorder[pe]] numsLeft = inRoot - ip root.left = createTree( inorder, ip, inRoot - 1, postorder, pp, pp + numsLeft - 1, a ) root.right = createTree( inorder, inRoot + 1, ie, postorder, pp + numsLeft, pe - 1, a ) return root return createTree(inorder, 0, len(inorder) - 1, postorder, 0, len(postorder) - 1, a)
FUNC_DEF IF VAR NONE FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN NONE ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR RETURN NONE ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR RETURN FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR
Given inorder and postorder traversals of a Binary Tree in the arrays in[] and post[] respectively. The task is to construct the binary tree from these traversals. Example 1: Input: N = 8 in[] = 4 8 2 5 1 6 3 7 post[] =8 4 5 2 6 7 3 1 Output: 1 2 4 8 5 3 6 7 Explanation: For the given postorder and inorder traversal of tree the resultant binary tree will be 1 / \ 2 3 / \ / \ 4 5 6 7 \ 8 Example 2: Input: N = 5 in[] = 9 5 2 3 4 post[] = 5 9 3 4 2 Output: 2 9 5 4 3 Explanation: the resultant binary tree will be 2 / \ 9 4 \ / 5 3 Your Task: You do not need to read input or print anything. Complete the function buildTree() which takes the inorder, postorder traversals and the number of nodes in the tree as input parameters and returns the root node of the newly constructed Binary Tree. The generated output contains the preorder traversal of the constructed tree. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 <= N <= 10^{3} 1 <= in[i], post[i] <= 10^{3}
def buildTree(In, post, n): if not In or not post: return index = In.index(post[-1]) root = Node(post[-1]) root.left = buildTree(In[0:index], post[0:index], n) root.right = buildTree(In[index + 1 :], post[index:-1], n) return root
FUNC_DEF IF VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR RETURN VAR
Given inorder and postorder traversals of a Binary Tree in the arrays in[] and post[] respectively. The task is to construct the binary tree from these traversals. Example 1: Input: N = 8 in[] = 4 8 2 5 1 6 3 7 post[] =8 4 5 2 6 7 3 1 Output: 1 2 4 8 5 3 6 7 Explanation: For the given postorder and inorder traversal of tree the resultant binary tree will be 1 / \ 2 3 / \ / \ 4 5 6 7 \ 8 Example 2: Input: N = 5 in[] = 9 5 2 3 4 post[] = 5 9 3 4 2 Output: 2 9 5 4 3 Explanation: the resultant binary tree will be 2 / \ 9 4 \ / 5 3 Your Task: You do not need to read input or print anything. Complete the function buildTree() which takes the inorder, postorder traversals and the number of nodes in the tree as input parameters and returns the root node of the newly constructed Binary Tree. The generated output contains the preorder traversal of the constructed tree. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 <= N <= 10^{3} 1 <= in[i], post[i] <= 10^{3}
def buildTree(In, post, n): global postindex postindex = n - 1 def SearchArray(arr, start, end, value): for i in range(start, end + 1): if arr[i] == value: return i return -1 def BuildTree_IPos(inOrder, postOrder, instart, inend): global postindex if instart > inend: return None tnode = Node(postOrder[postindex]) postindex -= 1 if instart == inend: return tnode inindex = SearchArray(inOrder, instart, inend, tnode.data) tnode.right = BuildTree_IPos(inOrder, postOrder, inindex + 1, inend) tnode.left = BuildTree_IPos(inOrder, postOrder, instart, inindex - 1) return tnode return BuildTree_IPos(In, post, 0, n - 1)
FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR RETURN VAR RETURN NUMBER FUNC_DEF IF VAR VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER
Given inorder and postorder traversals of a Binary Tree in the arrays in[] and post[] respectively. The task is to construct the binary tree from these traversals. Example 1: Input: N = 8 in[] = 4 8 2 5 1 6 3 7 post[] =8 4 5 2 6 7 3 1 Output: 1 2 4 8 5 3 6 7 Explanation: For the given postorder and inorder traversal of tree the resultant binary tree will be 1 / \ 2 3 / \ / \ 4 5 6 7 \ 8 Example 2: Input: N = 5 in[] = 9 5 2 3 4 post[] = 5 9 3 4 2 Output: 2 9 5 4 3 Explanation: the resultant binary tree will be 2 / \ 9 4 \ / 5 3 Your Task: You do not need to read input or print anything. Complete the function buildTree() which takes the inorder, postorder traversals and the number of nodes in the tree as input parameters and returns the root node of the newly constructed Binary Tree. The generated output contains the preorder traversal of the constructed tree. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 <= N <= 10^{3} 1 <= in[i], post[i] <= 10^{3}
def find(inorder, post, instart, inend): if instart > inend: return root = Node(post[find.postindex]) find.postindex -= 1 for i in range(instart, inend + 1): if inorder[i] == root.data: break root.right = find(inorder, post, i + 1, inend) root.left = find(inorder, post, instart, i - 1) return root def buildTree(inorder, post, n): find.postindex = n - 1 return find(inorder, post, 0, n - 1)
FUNC_DEF IF VAR VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER
Given inorder and postorder traversals of a Binary Tree in the arrays in[] and post[] respectively. The task is to construct the binary tree from these traversals. Example 1: Input: N = 8 in[] = 4 8 2 5 1 6 3 7 post[] =8 4 5 2 6 7 3 1 Output: 1 2 4 8 5 3 6 7 Explanation: For the given postorder and inorder traversal of tree the resultant binary tree will be 1 / \ 2 3 / \ / \ 4 5 6 7 \ 8 Example 2: Input: N = 5 in[] = 9 5 2 3 4 post[] = 5 9 3 4 2 Output: 2 9 5 4 3 Explanation: the resultant binary tree will be 2 / \ 9 4 \ / 5 3 Your Task: You do not need to read input or print anything. Complete the function buildTree() which takes the inorder, postorder traversals and the number of nodes in the tree as input parameters and returns the root node of the newly constructed Binary Tree. The generated output contains the preorder traversal of the constructed tree. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 <= N <= 10^{3} 1 <= in[i], post[i] <= 10^{3}
def buildTree(In, post, n): root = post[-1] rootNode = Node(root) rootIndex = In.index(root) if In[0:rootIndex]: rootNode.left = buildTree(In[0:rootIndex], post[0:rootIndex], rootIndex) if In[rootIndex + 1 :]: rootNode.right = buildTree( In[rootIndex + 1 :], post[rootIndex : n - 1], n - 1 - rootIndex ) return rootNode
FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR RETURN VAR
Given inorder and postorder traversals of a Binary Tree in the arrays in[] and post[] respectively. The task is to construct the binary tree from these traversals. Example 1: Input: N = 8 in[] = 4 8 2 5 1 6 3 7 post[] =8 4 5 2 6 7 3 1 Output: 1 2 4 8 5 3 6 7 Explanation: For the given postorder and inorder traversal of tree the resultant binary tree will be 1 / \ 2 3 / \ / \ 4 5 6 7 \ 8 Example 2: Input: N = 5 in[] = 9 5 2 3 4 post[] = 5 9 3 4 2 Output: 2 9 5 4 3 Explanation: the resultant binary tree will be 2 / \ 9 4 \ / 5 3 Your Task: You do not need to read input or print anything. Complete the function buildTree() which takes the inorder, postorder traversals and the number of nodes in the tree as input parameters and returns the root node of the newly constructed Binary Tree. The generated output contains the preorder traversal of the constructed tree. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 <= N <= 10^{3} 1 <= in[i], post[i] <= 10^{3}
def buildTree(In, post, n): if n <= 0: return None root = Node(post[n - 1]) for i in range(n): if In[i] == post[n - 1]: break root.left = buildTree(In, post, i) root.right = buildTree(In[i + 1 :], post[i:], n - i - 1) return root
FUNC_DEF IF VAR NUMBER RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR
Given inorder and postorder traversals of a Binary Tree in the arrays in[] and post[] respectively. The task is to construct the binary tree from these traversals. Example 1: Input: N = 8 in[] = 4 8 2 5 1 6 3 7 post[] =8 4 5 2 6 7 3 1 Output: 1 2 4 8 5 3 6 7 Explanation: For the given postorder and inorder traversal of tree the resultant binary tree will be 1 / \ 2 3 / \ / \ 4 5 6 7 \ 8 Example 2: Input: N = 5 in[] = 9 5 2 3 4 post[] = 5 9 3 4 2 Output: 2 9 5 4 3 Explanation: the resultant binary tree will be 2 / \ 9 4 \ / 5 3 Your Task: You do not need to read input or print anything. Complete the function buildTree() which takes the inorder, postorder traversals and the number of nodes in the tree as input parameters and returns the root node of the newly constructed Binary Tree. The generated output contains the preorder traversal of the constructed tree. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 <= N <= 10^{3} 1 <= in[i], post[i] <= 10^{3}
def buildTree(In, post, n): def recursive(In, post, idx=n - 1, lo=0, hi=n - 1): if idx < 0: return None, idx cur = post[idx] root = None for i in range(lo, hi + 1): if In[i] == cur: root = Node(cur) break if root: root.right, idx = recursive(In, post, idx - 1, i + 1, hi) root.left, idx = recursive(In, post, idx, lo, i - 1) return root, idx root, idx = recursive(In, post) return root
FUNC_DEF FUNC_DEF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER IF VAR NUMBER RETURN NONE VAR ASSIGN VAR VAR VAR ASSIGN VAR NONE FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given inorder and postorder traversals of a Binary Tree in the arrays in[] and post[] respectively. The task is to construct the binary tree from these traversals. Example 1: Input: N = 8 in[] = 4 8 2 5 1 6 3 7 post[] =8 4 5 2 6 7 3 1 Output: 1 2 4 8 5 3 6 7 Explanation: For the given postorder and inorder traversal of tree the resultant binary tree will be 1 / \ 2 3 / \ / \ 4 5 6 7 \ 8 Example 2: Input: N = 5 in[] = 9 5 2 3 4 post[] = 5 9 3 4 2 Output: 2 9 5 4 3 Explanation: the resultant binary tree will be 2 / \ 9 4 \ / 5 3 Your Task: You do not need to read input or print anything. Complete the function buildTree() which takes the inorder, postorder traversals and the number of nodes in the tree as input parameters and returns the root node of the newly constructed Binary Tree. The generated output contains the preorder traversal of the constructed tree. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 <= N <= 10^{3} 1 <= in[i], post[i] <= 10^{3}
def buildTree(In, post, n): def dum(In, post): if not In: return None p = post.pop() root = Node(p) i = In.index(p) root.right = dum(In[i + 1 :], post) root.left = dum(In[:i], post) return root return dum(In, post)
FUNC_DEF FUNC_DEF IF VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR VAR
Given inorder and postorder traversals of a Binary Tree in the arrays in[] and post[] respectively. The task is to construct the binary tree from these traversals. Example 1: Input: N = 8 in[] = 4 8 2 5 1 6 3 7 post[] =8 4 5 2 6 7 3 1 Output: 1 2 4 8 5 3 6 7 Explanation: For the given postorder and inorder traversal of tree the resultant binary tree will be 1 / \ 2 3 / \ / \ 4 5 6 7 \ 8 Example 2: Input: N = 5 in[] = 9 5 2 3 4 post[] = 5 9 3 4 2 Output: 2 9 5 4 3 Explanation: the resultant binary tree will be 2 / \ 9 4 \ / 5 3 Your Task: You do not need to read input or print anything. Complete the function buildTree() which takes the inorder, postorder traversals and the number of nodes in the tree as input parameters and returns the root node of the newly constructed Binary Tree. The generated output contains the preorder traversal of the constructed tree. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 <= N <= 10^{3} 1 <= in[i], post[i] <= 10^{3}
def buildTree(In, post, n): post_idx = [n - 1] return buildUtil(In, 0, n - 1, post, post_idx) def buildUtil(inorder, start, end, postorder, post_idx): if start > end: return None i = post_idx[0] root = postorder[i] post_idx[0] -= 1 for idx in range(start, end + 1): if inorder[idx] == root: break n = Node(root) n.right = buildUtil(inorder, idx + 1, end, postorder, post_idx) n.left = buildUtil(inorder, start, idx - 1, postorder, post_idx) return n
FUNC_DEF ASSIGN VAR LIST BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR FUNC_DEF IF VAR VAR RETURN NONE ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR
Given inorder and postorder traversals of a Binary Tree in the arrays in[] and post[] respectively. The task is to construct the binary tree from these traversals. Example 1: Input: N = 8 in[] = 4 8 2 5 1 6 3 7 post[] =8 4 5 2 6 7 3 1 Output: 1 2 4 8 5 3 6 7 Explanation: For the given postorder and inorder traversal of tree the resultant binary tree will be 1 / \ 2 3 / \ / \ 4 5 6 7 \ 8 Example 2: Input: N = 5 in[] = 9 5 2 3 4 post[] = 5 9 3 4 2 Output: 2 9 5 4 3 Explanation: the resultant binary tree will be 2 / \ 9 4 \ / 5 3 Your Task: You do not need to read input or print anything. Complete the function buildTree() which takes the inorder, postorder traversals and the number of nodes in the tree as input parameters and returns the root node of the newly constructed Binary Tree. The generated output contains the preorder traversal of the constructed tree. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 <= N <= 10^{3} 1 <= in[i], post[i] <= 10^{3}
mp = {} def buildTreeUtil(In, post, instart, inend, preIndex): if instart > inend: return None new = Node(post[preIndex[0]]) preIndex[0] -= 1 if instart == inend: return new inIndex = mp[new.data] new.right = buildTreeUtil(In, post, inIndex + 1, inend, preIndex) new.left = buildTreeUtil(In, post, instart, inIndex - 1, preIndex) return new def buildTree(In, post, n): global mp for i in range(n): mp[In[i]] = i preIndex = [n - 1] return buildTreeUtil(In, post, 0, n - 1, preIndex)
ASSIGN VAR DICT FUNC_DEF IF VAR VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER NUMBER IF VAR VAR RETURN VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR
Given inorder and postorder traversals of a Binary Tree in the arrays in[] and post[] respectively. The task is to construct the binary tree from these traversals. Example 1: Input: N = 8 in[] = 4 8 2 5 1 6 3 7 post[] =8 4 5 2 6 7 3 1 Output: 1 2 4 8 5 3 6 7 Explanation: For the given postorder and inorder traversal of tree the resultant binary tree will be 1 / \ 2 3 / \ / \ 4 5 6 7 \ 8 Example 2: Input: N = 5 in[] = 9 5 2 3 4 post[] = 5 9 3 4 2 Output: 2 9 5 4 3 Explanation: the resultant binary tree will be 2 / \ 9 4 \ / 5 3 Your Task: You do not need to read input or print anything. Complete the function buildTree() which takes the inorder, postorder traversals and the number of nodes in the tree as input parameters and returns the root node of the newly constructed Binary Tree. The generated output contains the preorder traversal of the constructed tree. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 <= N <= 10^{3} 1 <= in[i], post[i] <= 10^{3}
def buildTree(In, post, n): if n == 0: return pos = In.index(post[-1]) node = Node(post[-1]) node.left = buildTree(In[:pos], post[:pos], pos) node.right = buildTree(In[pos + 1 :], post[pos : n - 1], n - 1 - pos) return node
FUNC_DEF IF VAR NUMBER RETURN ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR RETURN VAR
Given inorder and postorder traversals of a Binary Tree in the arrays in[] and post[] respectively. The task is to construct the binary tree from these traversals. Example 1: Input: N = 8 in[] = 4 8 2 5 1 6 3 7 post[] =8 4 5 2 6 7 3 1 Output: 1 2 4 8 5 3 6 7 Explanation: For the given postorder and inorder traversal of tree the resultant binary tree will be 1 / \ 2 3 / \ / \ 4 5 6 7 \ 8 Example 2: Input: N = 5 in[] = 9 5 2 3 4 post[] = 5 9 3 4 2 Output: 2 9 5 4 3 Explanation: the resultant binary tree will be 2 / \ 9 4 \ / 5 3 Your Task: You do not need to read input or print anything. Complete the function buildTree() which takes the inorder, postorder traversals and the number of nodes in the tree as input parameters and returns the root node of the newly constructed Binary Tree. The generated output contains the preorder traversal of the constructed tree. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 <= N <= 10^{3} 1 <= in[i], post[i] <= 10^{3}
def buildTree(inorder, postorder, n): def builder(inorder, postorder, in_start, in_end, post_start, post_end): if in_start > in_end or post_start > post_end: return None node = Node(postorder[post_end]) for node_pos in range(in_start, in_end + 1): if inorder[node_pos] == postorder[post_end]: break left_total = node_pos - in_start - 1 node.left = builder( inorder, postorder, in_start, node_pos - 1, post_start, post_start + left_total, ) node.right = builder( inorder, postorder, node_pos + 1, in_end, post_start + left_total + 1, post_end - 1, ) return node return builder(inorder, postorder, 0, n - 1, 0, n - 1)
FUNC_DEF FUNC_DEF IF VAR VAR VAR VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER
Given inorder and postorder traversals of a Binary Tree in the arrays in[] and post[] respectively. The task is to construct the binary tree from these traversals. Example 1: Input: N = 8 in[] = 4 8 2 5 1 6 3 7 post[] =8 4 5 2 6 7 3 1 Output: 1 2 4 8 5 3 6 7 Explanation: For the given postorder and inorder traversal of tree the resultant binary tree will be 1 / \ 2 3 / \ / \ 4 5 6 7 \ 8 Example 2: Input: N = 5 in[] = 9 5 2 3 4 post[] = 5 9 3 4 2 Output: 2 9 5 4 3 Explanation: the resultant binary tree will be 2 / \ 9 4 \ / 5 3 Your Task: You do not need to read input or print anything. Complete the function buildTree() which takes the inorder, postorder traversals and the number of nodes in the tree as input parameters and returns the root node of the newly constructed Binary Tree. The generated output contains the preorder traversal of the constructed tree. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 <= N <= 10^{3} 1 <= in[i], post[i] <= 10^{3}
class Node: def __init__(self, data): self.data = data self.left = self.right = None def buildTree(In, post, n): mp = {} for i in range(n): if In[i] not in mp: mp[In[i]] = [] mp[In[i]].append(i) postS = [n - 1] inS = 0 inE = n - 1 return solution(In, post, postS, inS, inE, mp) def solution(inorder, postorder, postS, inS, inE, mp): if postS[0] < 0 or inS > inE: return None val = postorder[postS[0]] postS[0] -= 1 root = Node(val) index = mp[val].pop(0) root.right = solution(inorder, postorder, postS, index + 1, inE, mp) root.left = solution(inorder, postorder, postS, inS, index - 1, mp) return root
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR NONE FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FUNC_DEF IF VAR NUMBER NUMBER VAR VAR RETURN NONE ASSIGN VAR VAR VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR
Given inorder and postorder traversals of a Binary Tree in the arrays in[] and post[] respectively. The task is to construct the binary tree from these traversals. Example 1: Input: N = 8 in[] = 4 8 2 5 1 6 3 7 post[] =8 4 5 2 6 7 3 1 Output: 1 2 4 8 5 3 6 7 Explanation: For the given postorder and inorder traversal of tree the resultant binary tree will be 1 / \ 2 3 / \ / \ 4 5 6 7 \ 8 Example 2: Input: N = 5 in[] = 9 5 2 3 4 post[] = 5 9 3 4 2 Output: 2 9 5 4 3 Explanation: the resultant binary tree will be 2 / \ 9 4 \ / 5 3 Your Task: You do not need to read input or print anything. Complete the function buildTree() which takes the inorder, postorder traversals and the number of nodes in the tree as input parameters and returns the root node of the newly constructed Binary Tree. The generated output contains the preorder traversal of the constructed tree. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 <= N <= 10^{3} 1 <= in[i], post[i] <= 10^{3}
def buildTree(In, post, n): return solve(In, post, 0, n - 1, n) def solve(In, post, start, end, n): if n == 0: return None rootdata = post[n - 1] root = Node(rootdata) rootindexin = -1 for i in range(start, end + 1): if In[i] == rootdata: rootindexin = i break if rootindexin == -1: return None leftorder = In[start:rootindexin] rightorder = In[rootindexin + 1 : end + 1] lenleftsubtree = len(leftorder) leftpost = post[:lenleftsubtree] rightpost = post[lenleftsubtree : n - 1] leftchild = solve(In, leftpost, start, start + lenleftsubtree - 1, lenleftsubtree) rightchild = solve(In, rightpost, rootindexin + 1, end, n - 1 - lenleftsubtree) root.left = leftchild root.right = rightchild return root
FUNC_DEF RETURN FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_DEF IF VAR NUMBER RETURN NONE ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER RETURN NONE ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
Given inorder and postorder traversals of a Binary Tree in the arrays in[] and post[] respectively. The task is to construct the binary tree from these traversals. Example 1: Input: N = 8 in[] = 4 8 2 5 1 6 3 7 post[] =8 4 5 2 6 7 3 1 Output: 1 2 4 8 5 3 6 7 Explanation: For the given postorder and inorder traversal of tree the resultant binary tree will be 1 / \ 2 3 / \ / \ 4 5 6 7 \ 8 Example 2: Input: N = 5 in[] = 9 5 2 3 4 post[] = 5 9 3 4 2 Output: 2 9 5 4 3 Explanation: the resultant binary tree will be 2 / \ 9 4 \ / 5 3 Your Task: You do not need to read input or print anything. Complete the function buildTree() which takes the inorder, postorder traversals and the number of nodes in the tree as input parameters and returns the root node of the newly constructed Binary Tree. The generated output contains the preorder traversal of the constructed tree. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 <= N <= 10^{3} 1 <= in[i], post[i] <= 10^{3}
def buildTree(In, post, n): post_ind = [n - 1] def build(post=post, io=In, isi=0, iei=n - 1): if isi > iei: return None root = Node(post[post_ind[0]]) post_ind[0] -= 1 if isi == iei: return root for i in range(isi, iei + 1): if io[i] == root.data: break root.right = build(post, io, i + 1, iei) root.left = build(post, io, isi, i - 1) return root return build()
FUNC_DEF ASSIGN VAR LIST BIN_OP VAR NUMBER FUNC_DEF VAR VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER NUMBER IF VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR
Given inorder and postorder traversals of a Binary Tree in the arrays in[] and post[] respectively. The task is to construct the binary tree from these traversals. Example 1: Input: N = 8 in[] = 4 8 2 5 1 6 3 7 post[] =8 4 5 2 6 7 3 1 Output: 1 2 4 8 5 3 6 7 Explanation: For the given postorder and inorder traversal of tree the resultant binary tree will be 1 / \ 2 3 / \ / \ 4 5 6 7 \ 8 Example 2: Input: N = 5 in[] = 9 5 2 3 4 post[] = 5 9 3 4 2 Output: 2 9 5 4 3 Explanation: the resultant binary tree will be 2 / \ 9 4 \ / 5 3 Your Task: You do not need to read input or print anything. Complete the function buildTree() which takes the inorder, postorder traversals and the number of nodes in the tree as input parameters and returns the root node of the newly constructed Binary Tree. The generated output contains the preorder traversal of the constructed tree. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 <= N <= 10^{3} 1 <= in[i], post[i] <= 10^{3}
def buildTree(In, post, n): def builder(pi, pj, l, r): if pj < pi: return None root = Node(post[pj]) i = l while i <= r: if root.data == In[i]: break i += 1 root.right = builder(pj - (r - i), pj - 1, i + 1, r) root.left = builder(pi, pj - (r - i) - 1, l, i - 1) return root return builder(0, len(post) - 1, 0, len(In) - 1)
FUNC_DEF FUNC_DEF IF VAR VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER
Given inorder and postorder traversals of a Binary Tree in the arrays in[] and post[] respectively. The task is to construct the binary tree from these traversals. Example 1: Input: N = 8 in[] = 4 8 2 5 1 6 3 7 post[] =8 4 5 2 6 7 3 1 Output: 1 2 4 8 5 3 6 7 Explanation: For the given postorder and inorder traversal of tree the resultant binary tree will be 1 / \ 2 3 / \ / \ 4 5 6 7 \ 8 Example 2: Input: N = 5 in[] = 9 5 2 3 4 post[] = 5 9 3 4 2 Output: 2 9 5 4 3 Explanation: the resultant binary tree will be 2 / \ 9 4 \ / 5 3 Your Task: You do not need to read input or print anything. Complete the function buildTree() which takes the inorder, postorder traversals and the number of nodes in the tree as input parameters and returns the root node of the newly constructed Binary Tree. The generated output contains the preorder traversal of the constructed tree. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 <= N <= 10^{3} 1 <= in[i], post[i] <= 10^{3}
def solve(In, post, post_idx, in_start_idx, in_end_idx, node_map): if post_idx[0] < 0 or in_start_idx > in_end_idx: return None element = post[post_idx[0]] root = Node(element) post_idx[0] -= 1 pos = node_map[element] root.right = solve(In, post, post_idx, pos + 1, in_end_idx, node_map) root.left = solve(In, post, post_idx, in_start_idx, pos - 1, node_map) return root def buildTree(In, post, n): node_map = {} for i, x in enumerate(In): node_map[x] = i post_idx = [n - 1] in_start_idx, in_end_idx = 0, n - 1 root = solve(In, post, post_idx, in_start_idx, in_end_idx, node_map) return root
FUNC_DEF IF VAR NUMBER NUMBER VAR VAR RETURN NONE ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR FUNC_DEF ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN VAR
Given inorder and postorder traversals of a Binary Tree in the arrays in[] and post[] respectively. The task is to construct the binary tree from these traversals. Example 1: Input: N = 8 in[] = 4 8 2 5 1 6 3 7 post[] =8 4 5 2 6 7 3 1 Output: 1 2 4 8 5 3 6 7 Explanation: For the given postorder and inorder traversal of tree the resultant binary tree will be 1 / \ 2 3 / \ / \ 4 5 6 7 \ 8 Example 2: Input: N = 5 in[] = 9 5 2 3 4 post[] = 5 9 3 4 2 Output: 2 9 5 4 3 Explanation: the resultant binary tree will be 2 / \ 9 4 \ / 5 3 Your Task: You do not need to read input or print anything. Complete the function buildTree() which takes the inorder, postorder traversals and the number of nodes in the tree as input parameters and returns the root node of the newly constructed Binary Tree. The generated output contains the preorder traversal of the constructed tree. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N) Constraints: 1 <= N <= 10^{3} 1 <= in[i], post[i] <= 10^{3}
mp = {} def buildTree(In, post, n): global post_ind, mp post_ind = len(post) - 1 def recur(In, post, isi, iei): if isi > iei: return None global post_ind, mp curr = post[post_ind] root = Node(curr) post_ind -= 1 if isi == iei: return root i = mp[curr] root.right = recur(In, post, i + 1, iei) root.left = recur(In, post, isi, i - 1) return root for i in range(len(In)): mp[In[i]] = i return recur(In, post, 0, len(In) - 1)
ASSIGN VAR DICT FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF IF VAR VAR RETURN NONE ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR RETURN VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER