description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Given the array queries of positive integers between 1 and m, you have to process all queries[i] (from i=0 to i=queries.length-1) according to the following rules: In the beginning, you have the permutation P=[1,2,3,...,m]. For the current i, find the position of queries[i] in the permutation P (indexing from 0) and then move this at the beginning of the permutation P. Notice that the position of queries[i] in P is the result for queries[i]. Return an array containing the result for the given queries.   Example 1: Input: queries = [3,1,2,1], m = 5 Output: [2,1,2,1] Explanation: The queries are processed as follow: For i=0: queries[i]=3, P=[1,2,3,4,5], position of 3 in P is 2, then we move 3 to the beginning of P resulting in P=[3,1,2,4,5]. For i=1: queries[i]=1, P=[3,1,2,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,3,2,4,5]. For i=2: queries[i]=2, P=[1,3,2,4,5], position of 2 in P is 2, then we move 2 to the beginning of P resulting in P=[2,1,3,4,5]. For i=3: queries[i]=1, P=[2,1,3,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,2,3,4,5]. Therefore, the array containing the result is [2,1,2,1]. Example 2: Input: queries = [4,1,2,2], m = 4 Output: [3,1,2,0] Example 3: Input: queries = [7,5,5,8,3], m = 8 Output: [6,5,0,7,5]   Constraints: 1 <= m <= 10^3 1 <= queries.length <= m 1 <= queries[i] <= m
class Solution: def processQueries(self, queries: List[int], m: int) -> List[int]: d = {i: (i - 1) for i in range(1, m + 1)} ans = [] for i in queries: x = d[i] ans.append(x) for j in list(d.keys()): if d[j] < x: d[j] += 1 d[i] = 0 return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN VAR VAR VAR
Given the array queries of positive integers between 1 and m, you have to process all queries[i] (from i=0 to i=queries.length-1) according to the following rules: In the beginning, you have the permutation P=[1,2,3,...,m]. For the current i, find the position of queries[i] in the permutation P (indexing from 0) and then move this at the beginning of the permutation P. Notice that the position of queries[i] in P is the result for queries[i]. Return an array containing the result for the given queries.   Example 1: Input: queries = [3,1,2,1], m = 5 Output: [2,1,2,1] Explanation: The queries are processed as follow: For i=0: queries[i]=3, P=[1,2,3,4,5], position of 3 in P is 2, then we move 3 to the beginning of P resulting in P=[3,1,2,4,5]. For i=1: queries[i]=1, P=[3,1,2,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,3,2,4,5]. For i=2: queries[i]=2, P=[1,3,2,4,5], position of 2 in P is 2, then we move 2 to the beginning of P resulting in P=[2,1,3,4,5]. For i=3: queries[i]=1, P=[2,1,3,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,2,3,4,5]. Therefore, the array containing the result is [2,1,2,1]. Example 2: Input: queries = [4,1,2,2], m = 4 Output: [3,1,2,0] Example 3: Input: queries = [7,5,5,8,3], m = 8 Output: [6,5,0,7,5]   Constraints: 1 <= m <= 10^3 1 <= queries.length <= m 1 <= queries[i] <= m
class Solution: def processQueries(self, queries: List[int], m: int) -> List[int]: a = [i for i in range(1, m + 1)] x = {} for i in range(len(a)): x[a[i]] = i b = [] for i in queries: b.append(x[i]) a = [i] + a[: x[i]] + a[x[i] + 1 :] for j in range(x[i] + 1): x[a[j]] = j return b
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR
Given the array queries of positive integers between 1 and m, you have to process all queries[i] (from i=0 to i=queries.length-1) according to the following rules: In the beginning, you have the permutation P=[1,2,3,...,m]. For the current i, find the position of queries[i] in the permutation P (indexing from 0) and then move this at the beginning of the permutation P. Notice that the position of queries[i] in P is the result for queries[i]. Return an array containing the result for the given queries.   Example 1: Input: queries = [3,1,2,1], m = 5 Output: [2,1,2,1] Explanation: The queries are processed as follow: For i=0: queries[i]=3, P=[1,2,3,4,5], position of 3 in P is 2, then we move 3 to the beginning of P resulting in P=[3,1,2,4,5]. For i=1: queries[i]=1, P=[3,1,2,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,3,2,4,5]. For i=2: queries[i]=2, P=[1,3,2,4,5], position of 2 in P is 2, then we move 2 to the beginning of P resulting in P=[2,1,3,4,5]. For i=3: queries[i]=1, P=[2,1,3,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,2,3,4,5]. Therefore, the array containing the result is [2,1,2,1]. Example 2: Input: queries = [4,1,2,2], m = 4 Output: [3,1,2,0] Example 3: Input: queries = [7,5,5,8,3], m = 8 Output: [6,5,0,7,5]   Constraints: 1 <= m <= 10^3 1 <= queries.length <= m 1 <= queries[i] <= m
class Solution: def processQueries(self, queries: List[int], m: int) -> List[int]: P = list(range(1, m + 1)) op = [] hashP = {} for i, p in enumerate(P): hashP[p] = i for query in queries: idx = hashP[query] if idx != 0: for key in hashP: if key == query: hashP[key] = 0 elif hashP[key] < idx: hashP[key] += 1 op.append(idx) return op
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR
Given the array queries of positive integers between 1 and m, you have to process all queries[i] (from i=0 to i=queries.length-1) according to the following rules: In the beginning, you have the permutation P=[1,2,3,...,m]. For the current i, find the position of queries[i] in the permutation P (indexing from 0) and then move this at the beginning of the permutation P. Notice that the position of queries[i] in P is the result for queries[i]. Return an array containing the result for the given queries.   Example 1: Input: queries = [3,1,2,1], m = 5 Output: [2,1,2,1] Explanation: The queries are processed as follow: For i=0: queries[i]=3, P=[1,2,3,4,5], position of 3 in P is 2, then we move 3 to the beginning of P resulting in P=[3,1,2,4,5]. For i=1: queries[i]=1, P=[3,1,2,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,3,2,4,5]. For i=2: queries[i]=2, P=[1,3,2,4,5], position of 2 in P is 2, then we move 2 to the beginning of P resulting in P=[2,1,3,4,5]. For i=3: queries[i]=1, P=[2,1,3,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,2,3,4,5]. Therefore, the array containing the result is [2,1,2,1]. Example 2: Input: queries = [4,1,2,2], m = 4 Output: [3,1,2,0] Example 3: Input: queries = [7,5,5,8,3], m = 8 Output: [6,5,0,7,5]   Constraints: 1 <= m <= 10^3 1 <= queries.length <= m 1 <= queries[i] <= m
class Solution: def processQueries(self, queries: List[int], m: int) -> List[int]: P = collections.deque(list(range(1, m + 1))) res = [] for q in queries: res.append(P.index(q)) del P[res[-1]] P.appendleft(q) return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR
Given the array queries of positive integers between 1 and m, you have to process all queries[i] (from i=0 to i=queries.length-1) according to the following rules: In the beginning, you have the permutation P=[1,2,3,...,m]. For the current i, find the position of queries[i] in the permutation P (indexing from 0) and then move this at the beginning of the permutation P. Notice that the position of queries[i] in P is the result for queries[i]. Return an array containing the result for the given queries.   Example 1: Input: queries = [3,1,2,1], m = 5 Output: [2,1,2,1] Explanation: The queries are processed as follow: For i=0: queries[i]=3, P=[1,2,3,4,5], position of 3 in P is 2, then we move 3 to the beginning of P resulting in P=[3,1,2,4,5]. For i=1: queries[i]=1, P=[3,1,2,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,3,2,4,5]. For i=2: queries[i]=2, P=[1,3,2,4,5], position of 2 in P is 2, then we move 2 to the beginning of P resulting in P=[2,1,3,4,5]. For i=3: queries[i]=1, P=[2,1,3,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,2,3,4,5]. Therefore, the array containing the result is [2,1,2,1]. Example 2: Input: queries = [4,1,2,2], m = 4 Output: [3,1,2,0] Example 3: Input: queries = [7,5,5,8,3], m = 8 Output: [6,5,0,7,5]   Constraints: 1 <= m <= 10^3 1 <= queries.length <= m 1 <= queries[i] <= m
class Solution: def processQueries(self, queries: List[int], m: int) -> List[int]: array = [] element2index = {} index2element = {} for i in range(m): element2index[i + 1] = i index2element[i] = i + 1 for i in range(len(queries)): q = queries[i] pos = element2index[q] array.append(pos) for k in range(pos - 1, -1, -1): e = index2element[k] element2index[e] += 1 index2element[element2index[e]] = e index2element[0] = q element2index[q] = 0 return array
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER VAR ASSIGN VAR VAR NUMBER RETURN VAR VAR VAR
Given the array queries of positive integers between 1 and m, you have to process all queries[i] (from i=0 to i=queries.length-1) according to the following rules: In the beginning, you have the permutation P=[1,2,3,...,m]. For the current i, find the position of queries[i] in the permutation P (indexing from 0) and then move this at the beginning of the permutation P. Notice that the position of queries[i] in P is the result for queries[i]. Return an array containing the result for the given queries.   Example 1: Input: queries = [3,1,2,1], m = 5 Output: [2,1,2,1] Explanation: The queries are processed as follow: For i=0: queries[i]=3, P=[1,2,3,4,5], position of 3 in P is 2, then we move 3 to the beginning of P resulting in P=[3,1,2,4,5]. For i=1: queries[i]=1, P=[3,1,2,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,3,2,4,5]. For i=2: queries[i]=2, P=[1,3,2,4,5], position of 2 in P is 2, then we move 2 to the beginning of P resulting in P=[2,1,3,4,5]. For i=3: queries[i]=1, P=[2,1,3,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,2,3,4,5]. Therefore, the array containing the result is [2,1,2,1]. Example 2: Input: queries = [4,1,2,2], m = 4 Output: [3,1,2,0] Example 3: Input: queries = [7,5,5,8,3], m = 8 Output: [6,5,0,7,5]   Constraints: 1 <= m <= 10^3 1 <= queries.length <= m 1 <= queries[i] <= m
def shiftRight(arr, i): ele = arr[i] for j in range(i, 0, -1): arr[j] = arr[j - 1] arr[0] = ele return arr def findQ(arr, ele): for i in range(len(arr)): if arr[i] == ele: return i class Solution: def processQueries(self, queries: List[int], m: int) -> List[int]: res = [] arr = list(range(1, m + 1)) for i in range(len(queries)): q = queries[i] j = findQ(arr, q) res.append(j) shiftRight(arr, j) return res
FUNC_DEF ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR RETURN VAR CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR
Given the array queries of positive integers between 1 and m, you have to process all queries[i] (from i=0 to i=queries.length-1) according to the following rules: In the beginning, you have the permutation P=[1,2,3,...,m]. For the current i, find the position of queries[i] in the permutation P (indexing from 0) and then move this at the beginning of the permutation P. Notice that the position of queries[i] in P is the result for queries[i]. Return an array containing the result for the given queries.   Example 1: Input: queries = [3,1,2,1], m = 5 Output: [2,1,2,1] Explanation: The queries are processed as follow: For i=0: queries[i]=3, P=[1,2,3,4,5], position of 3 in P is 2, then we move 3 to the beginning of P resulting in P=[3,1,2,4,5]. For i=1: queries[i]=1, P=[3,1,2,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,3,2,4,5]. For i=2: queries[i]=2, P=[1,3,2,4,5], position of 2 in P is 2, then we move 2 to the beginning of P resulting in P=[2,1,3,4,5]. For i=3: queries[i]=1, P=[2,1,3,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,2,3,4,5]. Therefore, the array containing the result is [2,1,2,1]. Example 2: Input: queries = [4,1,2,2], m = 4 Output: [3,1,2,0] Example 3: Input: queries = [7,5,5,8,3], m = 8 Output: [6,5,0,7,5]   Constraints: 1 <= m <= 10^3 1 <= queries.length <= m 1 <= queries[i] <= m
class Solution: def processQueries(self, queries: List[int], m: int) -> List[int]: P = [(i + 1) for i in range(m)] res = [] for query in queries: idx = P.index(query) res.append(idx) pos = idx while pos > 0: P[pos] = P[pos - 1] pos -= 1 P[0] = query return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR RETURN VAR VAR VAR
Given the array queries of positive integers between 1 and m, you have to process all queries[i] (from i=0 to i=queries.length-1) according to the following rules: In the beginning, you have the permutation P=[1,2,3,...,m]. For the current i, find the position of queries[i] in the permutation P (indexing from 0) and then move this at the beginning of the permutation P. Notice that the position of queries[i] in P is the result for queries[i]. Return an array containing the result for the given queries.   Example 1: Input: queries = [3,1,2,1], m = 5 Output: [2,1,2,1] Explanation: The queries are processed as follow: For i=0: queries[i]=3, P=[1,2,3,4,5], position of 3 in P is 2, then we move 3 to the beginning of P resulting in P=[3,1,2,4,5]. For i=1: queries[i]=1, P=[3,1,2,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,3,2,4,5]. For i=2: queries[i]=2, P=[1,3,2,4,5], position of 2 in P is 2, then we move 2 to the beginning of P resulting in P=[2,1,3,4,5]. For i=3: queries[i]=1, P=[2,1,3,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,2,3,4,5]. Therefore, the array containing the result is [2,1,2,1]. Example 2: Input: queries = [4,1,2,2], m = 4 Output: [3,1,2,0] Example 3: Input: queries = [7,5,5,8,3], m = 8 Output: [6,5,0,7,5]   Constraints: 1 <= m <= 10^3 1 <= queries.length <= m 1 <= queries[i] <= m
class Solution: def processQueries(self, queries: List[int], m: int) -> List[int]: p = [i for i in range(1, m + 1)] res = [] for j in range(len(queries)): idx = p.index(queries[j]) fs = p[0:idx] ls = p[idx + 1 :] p = [p[idx]] + fs + ls res.append(idx) return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP LIST VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR
Given the array queries of positive integers between 1 and m, you have to process all queries[i] (from i=0 to i=queries.length-1) according to the following rules: In the beginning, you have the permutation P=[1,2,3,...,m]. For the current i, find the position of queries[i] in the permutation P (indexing from 0) and then move this at the beginning of the permutation P. Notice that the position of queries[i] in P is the result for queries[i]. Return an array containing the result for the given queries.   Example 1: Input: queries = [3,1,2,1], m = 5 Output: [2,1,2,1] Explanation: The queries are processed as follow: For i=0: queries[i]=3, P=[1,2,3,4,5], position of 3 in P is 2, then we move 3 to the beginning of P resulting in P=[3,1,2,4,5]. For i=1: queries[i]=1, P=[3,1,2,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,3,2,4,5]. For i=2: queries[i]=2, P=[1,3,2,4,5], position of 2 in P is 2, then we move 2 to the beginning of P resulting in P=[2,1,3,4,5]. For i=3: queries[i]=1, P=[2,1,3,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,2,3,4,5]. Therefore, the array containing the result is [2,1,2,1]. Example 2: Input: queries = [4,1,2,2], m = 4 Output: [3,1,2,0] Example 3: Input: queries = [7,5,5,8,3], m = 8 Output: [6,5,0,7,5]   Constraints: 1 <= m <= 10^3 1 <= queries.length <= m 1 <= queries[i] <= m
class Solution: def processQueries(self, queries: List[int], m: int) -> List[int]: p = [] res = [] for i in range(1, m + 1): p.append(i) for n in queries: pos = 0 while p[pos] != n and pos < m: pos += 1 res.append(pos) del p[pos] p.insert(0, n) return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR RETURN VAR VAR VAR
Given the array queries of positive integers between 1 and m, you have to process all queries[i] (from i=0 to i=queries.length-1) according to the following rules: In the beginning, you have the permutation P=[1,2,3,...,m]. For the current i, find the position of queries[i] in the permutation P (indexing from 0) and then move this at the beginning of the permutation P. Notice that the position of queries[i] in P is the result for queries[i]. Return an array containing the result for the given queries.   Example 1: Input: queries = [3,1,2,1], m = 5 Output: [2,1,2,1] Explanation: The queries are processed as follow: For i=0: queries[i]=3, P=[1,2,3,4,5], position of 3 in P is 2, then we move 3 to the beginning of P resulting in P=[3,1,2,4,5]. For i=1: queries[i]=1, P=[3,1,2,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,3,2,4,5]. For i=2: queries[i]=2, P=[1,3,2,4,5], position of 2 in P is 2, then we move 2 to the beginning of P resulting in P=[2,1,3,4,5]. For i=3: queries[i]=1, P=[2,1,3,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,2,3,4,5]. Therefore, the array containing the result is [2,1,2,1]. Example 2: Input: queries = [4,1,2,2], m = 4 Output: [3,1,2,0] Example 3: Input: queries = [7,5,5,8,3], m = 8 Output: [6,5,0,7,5]   Constraints: 1 <= m <= 10^3 1 <= queries.length <= m 1 <= queries[i] <= m
class Solution: def processQueries(self, queries: List[int], m: int) -> List[int]: P = [i for i in range(1, m + 1)] ans = [] for querie in queries: idx = P.index(querie) del P[idx] P = [querie] + P ans.append(idx) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR
Given the array queries of positive integers between 1 and m, you have to process all queries[i] (from i=0 to i=queries.length-1) according to the following rules: In the beginning, you have the permutation P=[1,2,3,...,m]. For the current i, find the position of queries[i] in the permutation P (indexing from 0) and then move this at the beginning of the permutation P. Notice that the position of queries[i] in P is the result for queries[i]. Return an array containing the result for the given queries.   Example 1: Input: queries = [3,1,2,1], m = 5 Output: [2,1,2,1] Explanation: The queries are processed as follow: For i=0: queries[i]=3, P=[1,2,3,4,5], position of 3 in P is 2, then we move 3 to the beginning of P resulting in P=[3,1,2,4,5]. For i=1: queries[i]=1, P=[3,1,2,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,3,2,4,5]. For i=2: queries[i]=2, P=[1,3,2,4,5], position of 2 in P is 2, then we move 2 to the beginning of P resulting in P=[2,1,3,4,5]. For i=3: queries[i]=1, P=[2,1,3,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,2,3,4,5]. Therefore, the array containing the result is [2,1,2,1]. Example 2: Input: queries = [4,1,2,2], m = 4 Output: [3,1,2,0] Example 3: Input: queries = [7,5,5,8,3], m = 8 Output: [6,5,0,7,5]   Constraints: 1 <= m <= 10^3 1 <= queries.length <= m 1 <= queries[i] <= m
class Solution: def processQueries(self, queries, m): P = list(range(1, m + 1)) return [n for x in queries if not P.insert(0, P.pop((n := P.index(x))))]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER RETURN VAR VAR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FUNC_CALL VAR VAR
Given the array queries of positive integers between 1 and m, you have to process all queries[i] (from i=0 to i=queries.length-1) according to the following rules: In the beginning, you have the permutation P=[1,2,3,...,m]. For the current i, find the position of queries[i] in the permutation P (indexing from 0) and then move this at the beginning of the permutation P. Notice that the position of queries[i] in P is the result for queries[i]. Return an array containing the result for the given queries.   Example 1: Input: queries = [3,1,2,1], m = 5 Output: [2,1,2,1] Explanation: The queries are processed as follow: For i=0: queries[i]=3, P=[1,2,3,4,5], position of 3 in P is 2, then we move 3 to the beginning of P resulting in P=[3,1,2,4,5]. For i=1: queries[i]=1, P=[3,1,2,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,3,2,4,5]. For i=2: queries[i]=2, P=[1,3,2,4,5], position of 2 in P is 2, then we move 2 to the beginning of P resulting in P=[2,1,3,4,5]. For i=3: queries[i]=1, P=[2,1,3,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,2,3,4,5]. Therefore, the array containing the result is [2,1,2,1]. Example 2: Input: queries = [4,1,2,2], m = 4 Output: [3,1,2,0] Example 3: Input: queries = [7,5,5,8,3], m = 8 Output: [6,5,0,7,5]   Constraints: 1 <= m <= 10^3 1 <= queries.length <= m 1 <= queries[i] <= m
class Solution: def processQueries(self, queries: List[int], m: int) -> List[int]: p = [] for i in range(1, m + 1): p.append(i) l = [] for i in range(0, len(queries)): for j in range(0, m): if p[j] == queries[i]: k = p.pop(j) p.insert(0, k) l.append(j) break return l
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR
Given the array queries of positive integers between 1 and m, you have to process all queries[i] (from i=0 to i=queries.length-1) according to the following rules: In the beginning, you have the permutation P=[1,2,3,...,m]. For the current i, find the position of queries[i] in the permutation P (indexing from 0) and then move this at the beginning of the permutation P. Notice that the position of queries[i] in P is the result for queries[i]. Return an array containing the result for the given queries.   Example 1: Input: queries = [3,1,2,1], m = 5 Output: [2,1,2,1] Explanation: The queries are processed as follow: For i=0: queries[i]=3, P=[1,2,3,4,5], position of 3 in P is 2, then we move 3 to the beginning of P resulting in P=[3,1,2,4,5]. For i=1: queries[i]=1, P=[3,1,2,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,3,2,4,5]. For i=2: queries[i]=2, P=[1,3,2,4,5], position of 2 in P is 2, then we move 2 to the beginning of P resulting in P=[2,1,3,4,5]. For i=3: queries[i]=1, P=[2,1,3,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,2,3,4,5]. Therefore, the array containing the result is [2,1,2,1]. Example 2: Input: queries = [4,1,2,2], m = 4 Output: [3,1,2,0] Example 3: Input: queries = [7,5,5,8,3], m = 8 Output: [6,5,0,7,5]   Constraints: 1 <= m <= 10^3 1 <= queries.length <= m 1 <= queries[i] <= m
class Solution: def processQueries(self, queries: List[int], m: int) -> List[int]: result = [] perm = [i for i in range(1, m + 1)] for element in queries: ind = perm.index(element) result.append(ind) tmp = [element] tmp.extend(perm[:ind]) tmp.extend(perm[ind + 1 :]) perm = tmp print(perm) return result
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR
Given the array queries of positive integers between 1 and m, you have to process all queries[i] (from i=0 to i=queries.length-1) according to the following rules: In the beginning, you have the permutation P=[1,2,3,...,m]. For the current i, find the position of queries[i] in the permutation P (indexing from 0) and then move this at the beginning of the permutation P. Notice that the position of queries[i] in P is the result for queries[i]. Return an array containing the result for the given queries.   Example 1: Input: queries = [3,1,2,1], m = 5 Output: [2,1,2,1] Explanation: The queries are processed as follow: For i=0: queries[i]=3, P=[1,2,3,4,5], position of 3 in P is 2, then we move 3 to the beginning of P resulting in P=[3,1,2,4,5]. For i=1: queries[i]=1, P=[3,1,2,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,3,2,4,5]. For i=2: queries[i]=2, P=[1,3,2,4,5], position of 2 in P is 2, then we move 2 to the beginning of P resulting in P=[2,1,3,4,5]. For i=3: queries[i]=1, P=[2,1,3,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,2,3,4,5]. Therefore, the array containing the result is [2,1,2,1]. Example 2: Input: queries = [4,1,2,2], m = 4 Output: [3,1,2,0] Example 3: Input: queries = [7,5,5,8,3], m = 8 Output: [6,5,0,7,5]   Constraints: 1 <= m <= 10^3 1 <= queries.length <= m 1 <= queries[i] <= m
class Solution: def processQueries(self, queries: List[int], m: int) -> List[int]: l = list(range(1, m + 1)) n = 0 p = [0] * len(queries) while n != len(queries): for k in range(len(queries)): for i in range(len(l)): if l[i] == queries[k]: l.insert(0, l.pop(i)) p[n] = i n += 1 return p
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR NUMBER RETURN VAR VAR VAR
Given the array queries of positive integers between 1 and m, you have to process all queries[i] (from i=0 to i=queries.length-1) according to the following rules: In the beginning, you have the permutation P=[1,2,3,...,m]. For the current i, find the position of queries[i] in the permutation P (indexing from 0) and then move this at the beginning of the permutation P. Notice that the position of queries[i] in P is the result for queries[i]. Return an array containing the result for the given queries.   Example 1: Input: queries = [3,1,2,1], m = 5 Output: [2,1,2,1] Explanation: The queries are processed as follow: For i=0: queries[i]=3, P=[1,2,3,4,5], position of 3 in P is 2, then we move 3 to the beginning of P resulting in P=[3,1,2,4,5]. For i=1: queries[i]=1, P=[3,1,2,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,3,2,4,5]. For i=2: queries[i]=2, P=[1,3,2,4,5], position of 2 in P is 2, then we move 2 to the beginning of P resulting in P=[2,1,3,4,5]. For i=3: queries[i]=1, P=[2,1,3,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,2,3,4,5]. Therefore, the array containing the result is [2,1,2,1]. Example 2: Input: queries = [4,1,2,2], m = 4 Output: [3,1,2,0] Example 3: Input: queries = [7,5,5,8,3], m = 8 Output: [6,5,0,7,5]   Constraints: 1 <= m <= 10^3 1 <= queries.length <= m 1 <= queries[i] <= m
class Solution: def processQueries(self, queries: List[int], m: int) -> List[int]: data = [i for i in range(1, m + 1)] hashMap = {} for i in range(1, m + 1): hashMap[i] = i - 1 result = [] for q in queries: position = hashMap[q] result.append(position) data = [data[position]] + data[0:position] + data[position + 1 :] for index, d in enumerate(data): hashMap[d] = index return result
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR VAR
Given the array queries of positive integers between 1 and m, you have to process all queries[i] (from i=0 to i=queries.length-1) according to the following rules: In the beginning, you have the permutation P=[1,2,3,...,m]. For the current i, find the position of queries[i] in the permutation P (indexing from 0) and then move this at the beginning of the permutation P. Notice that the position of queries[i] in P is the result for queries[i]. Return an array containing the result for the given queries.   Example 1: Input: queries = [3,1,2,1], m = 5 Output: [2,1,2,1] Explanation: The queries are processed as follow: For i=0: queries[i]=3, P=[1,2,3,4,5], position of 3 in P is 2, then we move 3 to the beginning of P resulting in P=[3,1,2,4,5]. For i=1: queries[i]=1, P=[3,1,2,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,3,2,4,5]. For i=2: queries[i]=2, P=[1,3,2,4,5], position of 2 in P is 2, then we move 2 to the beginning of P resulting in P=[2,1,3,4,5]. For i=3: queries[i]=1, P=[2,1,3,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,2,3,4,5]. Therefore, the array containing the result is [2,1,2,1]. Example 2: Input: queries = [4,1,2,2], m = 4 Output: [3,1,2,0] Example 3: Input: queries = [7,5,5,8,3], m = 8 Output: [6,5,0,7,5]   Constraints: 1 <= m <= 10^3 1 <= queries.length <= m 1 <= queries[i] <= m
class Solution: def processQueries(self, queries: List[int], m: int) -> List[int]: if len(queries) == 0: return [] P = [] for i in range(m): P.append(i + 1) res = [] for i in queries: res.append(P.index(i)) P.remove(i) P = [i] + P[:] return res
CLASS_DEF FUNC_DEF VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST VAR VAR RETURN VAR VAR VAR
Given the array queries of positive integers between 1 and m, you have to process all queries[i] (from i=0 to i=queries.length-1) according to the following rules: In the beginning, you have the permutation P=[1,2,3,...,m]. For the current i, find the position of queries[i] in the permutation P (indexing from 0) and then move this at the beginning of the permutation P. Notice that the position of queries[i] in P is the result for queries[i]. Return an array containing the result for the given queries.   Example 1: Input: queries = [3,1,2,1], m = 5 Output: [2,1,2,1] Explanation: The queries are processed as follow: For i=0: queries[i]=3, P=[1,2,3,4,5], position of 3 in P is 2, then we move 3 to the beginning of P resulting in P=[3,1,2,4,5]. For i=1: queries[i]=1, P=[3,1,2,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,3,2,4,5]. For i=2: queries[i]=2, P=[1,3,2,4,5], position of 2 in P is 2, then we move 2 to the beginning of P resulting in P=[2,1,3,4,5]. For i=3: queries[i]=1, P=[2,1,3,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,2,3,4,5]. Therefore, the array containing the result is [2,1,2,1]. Example 2: Input: queries = [4,1,2,2], m = 4 Output: [3,1,2,0] Example 3: Input: queries = [7,5,5,8,3], m = 8 Output: [6,5,0,7,5]   Constraints: 1 <= m <= 10^3 1 <= queries.length <= m 1 <= queries[i] <= m
class Solution: def processQueries(self, queries: List[int], m: int) -> List[int]: p = [i for i in range(1, m + 1)] def find(p, q): for i in range(len(p)): if p[i] == q: return i def update(p, i): return [p[i]] + p[:i] + p[i + 1 :] res = [] for i in range(len(queries)): q = find(p, queries[i]) res.append(q) p = update(p, q) return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR RETURN VAR FUNC_DEF RETURN BIN_OP BIN_OP LIST VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR VAR
Given the array queries of positive integers between 1 and m, you have to process all queries[i] (from i=0 to i=queries.length-1) according to the following rules: In the beginning, you have the permutation P=[1,2,3,...,m]. For the current i, find the position of queries[i] in the permutation P (indexing from 0) and then move this at the beginning of the permutation P. Notice that the position of queries[i] in P is the result for queries[i]. Return an array containing the result for the given queries.   Example 1: Input: queries = [3,1,2,1], m = 5 Output: [2,1,2,1] Explanation: The queries are processed as follow: For i=0: queries[i]=3, P=[1,2,3,4,5], position of 3 in P is 2, then we move 3 to the beginning of P resulting in P=[3,1,2,4,5]. For i=1: queries[i]=1, P=[3,1,2,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,3,2,4,5]. For i=2: queries[i]=2, P=[1,3,2,4,5], position of 2 in P is 2, then we move 2 to the beginning of P resulting in P=[2,1,3,4,5]. For i=3: queries[i]=1, P=[2,1,3,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,2,3,4,5]. Therefore, the array containing the result is [2,1,2,1]. Example 2: Input: queries = [4,1,2,2], m = 4 Output: [3,1,2,0] Example 3: Input: queries = [7,5,5,8,3], m = 8 Output: [6,5,0,7,5]   Constraints: 1 <= m <= 10^3 1 <= queries.length <= m 1 <= queries[i] <= m
class Solution: def processQueries(self, queries: List[int], m: int) -> List[int]: out = [] P = [i for i in range(1, m + 1)] for i in queries: out.append(P.index(i)) P.insert(0, P.pop(P.index(i))) return out
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR RETURN VAR VAR VAR
Given the array queries of positive integers between 1 and m, you have to process all queries[i] (from i=0 to i=queries.length-1) according to the following rules: In the beginning, you have the permutation P=[1,2,3,...,m]. For the current i, find the position of queries[i] in the permutation P (indexing from 0) and then move this at the beginning of the permutation P. Notice that the position of queries[i] in P is the result for queries[i]. Return an array containing the result for the given queries.   Example 1: Input: queries = [3,1,2,1], m = 5 Output: [2,1,2,1] Explanation: The queries are processed as follow: For i=0: queries[i]=3, P=[1,2,3,4,5], position of 3 in P is 2, then we move 3 to the beginning of P resulting in P=[3,1,2,4,5]. For i=1: queries[i]=1, P=[3,1,2,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,3,2,4,5]. For i=2: queries[i]=2, P=[1,3,2,4,5], position of 2 in P is 2, then we move 2 to the beginning of P resulting in P=[2,1,3,4,5]. For i=3: queries[i]=1, P=[2,1,3,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,2,3,4,5]. Therefore, the array containing the result is [2,1,2,1]. Example 2: Input: queries = [4,1,2,2], m = 4 Output: [3,1,2,0] Example 3: Input: queries = [7,5,5,8,3], m = 8 Output: [6,5,0,7,5]   Constraints: 1 <= m <= 10^3 1 <= queries.length <= m 1 <= queries[i] <= m
class Solution: def processQueries(self, queries: List[int], m: int) -> List[int]: q = deque([]) ans = [] for i in range(1, m + 1, 1): q.append(i) for j in queries: ans.append(q.index(j)) q.remove(j) q.appendleft(j) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR
Given the array queries of positive integers between 1 and m, you have to process all queries[i] (from i=0 to i=queries.length-1) according to the following rules: In the beginning, you have the permutation P=[1,2,3,...,m]. For the current i, find the position of queries[i] in the permutation P (indexing from 0) and then move this at the beginning of the permutation P. Notice that the position of queries[i] in P is the result for queries[i]. Return an array containing the result for the given queries.   Example 1: Input: queries = [3,1,2,1], m = 5 Output: [2,1,2,1] Explanation: The queries are processed as follow: For i=0: queries[i]=3, P=[1,2,3,4,5], position of 3 in P is 2, then we move 3 to the beginning of P resulting in P=[3,1,2,4,5]. For i=1: queries[i]=1, P=[3,1,2,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,3,2,4,5]. For i=2: queries[i]=2, P=[1,3,2,4,5], position of 2 in P is 2, then we move 2 to the beginning of P resulting in P=[2,1,3,4,5]. For i=3: queries[i]=1, P=[2,1,3,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,2,3,4,5]. Therefore, the array containing the result is [2,1,2,1]. Example 2: Input: queries = [4,1,2,2], m = 4 Output: [3,1,2,0] Example 3: Input: queries = [7,5,5,8,3], m = 8 Output: [6,5,0,7,5]   Constraints: 1 <= m <= 10^3 1 <= queries.length <= m 1 <= queries[i] <= m
class Solution: def processQueries(self, queries: List[int], m: int) -> List[int]: p = [i for i in range(1, m + 1)] res = [] for i in range(len(queries)): for j in range(m): if p[j] == queries[i]: res.append(j) p = [p[j]] + p[0:j] + p[j + 1 :] return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR
Given the array queries of positive integers between 1 and m, you have to process all queries[i] (from i=0 to i=queries.length-1) according to the following rules: In the beginning, you have the permutation P=[1,2,3,...,m]. For the current i, find the position of queries[i] in the permutation P (indexing from 0) and then move this at the beginning of the permutation P. Notice that the position of queries[i] in P is the result for queries[i]. Return an array containing the result for the given queries.   Example 1: Input: queries = [3,1,2,1], m = 5 Output: [2,1,2,1] Explanation: The queries are processed as follow: For i=0: queries[i]=3, P=[1,2,3,4,5], position of 3 in P is 2, then we move 3 to the beginning of P resulting in P=[3,1,2,4,5]. For i=1: queries[i]=1, P=[3,1,2,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,3,2,4,5]. For i=2: queries[i]=2, P=[1,3,2,4,5], position of 2 in P is 2, then we move 2 to the beginning of P resulting in P=[2,1,3,4,5]. For i=3: queries[i]=1, P=[2,1,3,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,2,3,4,5]. Therefore, the array containing the result is [2,1,2,1]. Example 2: Input: queries = [4,1,2,2], m = 4 Output: [3,1,2,0] Example 3: Input: queries = [7,5,5,8,3], m = 8 Output: [6,5,0,7,5]   Constraints: 1 <= m <= 10^3 1 <= queries.length <= m 1 <= queries[i] <= m
class Solution: def processQueries(self, queries: List[int], m: int) -> List[int]: if not queries: return [] p = list(range(1, m + 1)) res = [] for i in queries: z = p.index(i) res.append(z) del p[z] p.insert(0, i) return res
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR RETURN LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER VAR RETURN VAR VAR VAR
Given the array queries of positive integers between 1 and m, you have to process all queries[i] (from i=0 to i=queries.length-1) according to the following rules: In the beginning, you have the permutation P=[1,2,3,...,m]. For the current i, find the position of queries[i] in the permutation P (indexing from 0) and then move this at the beginning of the permutation P. Notice that the position of queries[i] in P is the result for queries[i]. Return an array containing the result for the given queries.   Example 1: Input: queries = [3,1,2,1], m = 5 Output: [2,1,2,1] Explanation: The queries are processed as follow: For i=0: queries[i]=3, P=[1,2,3,4,5], position of 3 in P is 2, then we move 3 to the beginning of P resulting in P=[3,1,2,4,5]. For i=1: queries[i]=1, P=[3,1,2,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,3,2,4,5]. For i=2: queries[i]=2, P=[1,3,2,4,5], position of 2 in P is 2, then we move 2 to the beginning of P resulting in P=[2,1,3,4,5]. For i=3: queries[i]=1, P=[2,1,3,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,2,3,4,5]. Therefore, the array containing the result is [2,1,2,1]. Example 2: Input: queries = [4,1,2,2], m = 4 Output: [3,1,2,0] Example 3: Input: queries = [7,5,5,8,3], m = 8 Output: [6,5,0,7,5]   Constraints: 1 <= m <= 10^3 1 <= queries.length <= m 1 <= queries[i] <= m
class Solution: def processQueries(self, queries: List[int], m: int) -> List[int]: p = [] res = [] for x in range(m): p.append(x + 1) for x in queries: idx = p.index(x) res.append(idx) p.insert(0, p.pop(idx)) return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR RETURN VAR VAR VAR
Given the array queries of positive integers between 1 and m, you have to process all queries[i] (from i=0 to i=queries.length-1) according to the following rules: In the beginning, you have the permutation P=[1,2,3,...,m]. For the current i, find the position of queries[i] in the permutation P (indexing from 0) and then move this at the beginning of the permutation P. Notice that the position of queries[i] in P is the result for queries[i]. Return an array containing the result for the given queries.   Example 1: Input: queries = [3,1,2,1], m = 5 Output: [2,1,2,1] Explanation: The queries are processed as follow: For i=0: queries[i]=3, P=[1,2,3,4,5], position of 3 in P is 2, then we move 3 to the beginning of P resulting in P=[3,1,2,4,5]. For i=1: queries[i]=1, P=[3,1,2,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,3,2,4,5]. For i=2: queries[i]=2, P=[1,3,2,4,5], position of 2 in P is 2, then we move 2 to the beginning of P resulting in P=[2,1,3,4,5]. For i=3: queries[i]=1, P=[2,1,3,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,2,3,4,5]. Therefore, the array containing the result is [2,1,2,1]. Example 2: Input: queries = [4,1,2,2], m = 4 Output: [3,1,2,0] Example 3: Input: queries = [7,5,5,8,3], m = 8 Output: [6,5,0,7,5]   Constraints: 1 <= m <= 10^3 1 <= queries.length <= m 1 <= queries[i] <= m
class Solution: def processQueries(self, queries: List[int], m: int) -> List[int]: rtnlst = [] P = [] for n in range(1, m + 1): P.append(n) for q in queries: for p in range(0, len(P)): if P[p] == q: rtnlst.append(p) P.pop(rtnlst[-1]) P = [q] + P return rtnlst
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST VAR VAR RETURN VAR VAR VAR
Given the array queries of positive integers between 1 and m, you have to process all queries[i] (from i=0 to i=queries.length-1) according to the following rules: In the beginning, you have the permutation P=[1,2,3,...,m]. For the current i, find the position of queries[i] in the permutation P (indexing from 0) and then move this at the beginning of the permutation P. Notice that the position of queries[i] in P is the result for queries[i]. Return an array containing the result for the given queries.   Example 1: Input: queries = [3,1,2,1], m = 5 Output: [2,1,2,1] Explanation: The queries are processed as follow: For i=0: queries[i]=3, P=[1,2,3,4,5], position of 3 in P is 2, then we move 3 to the beginning of P resulting in P=[3,1,2,4,5]. For i=1: queries[i]=1, P=[3,1,2,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,3,2,4,5]. For i=2: queries[i]=2, P=[1,3,2,4,5], position of 2 in P is 2, then we move 2 to the beginning of P resulting in P=[2,1,3,4,5]. For i=3: queries[i]=1, P=[2,1,3,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,2,3,4,5]. Therefore, the array containing the result is [2,1,2,1]. Example 2: Input: queries = [4,1,2,2], m = 4 Output: [3,1,2,0] Example 3: Input: queries = [7,5,5,8,3], m = 8 Output: [6,5,0,7,5]   Constraints: 1 <= m <= 10^3 1 <= queries.length <= m 1 <= queries[i] <= m
class Solution: def processQueries(self, queries: List[int], m: int) -> List[int]: result = list(range(1, m + 1)) temp = 0 i = 0 real = [] for i in range(len(queries)): for j in range(len(result)): if queries[i] == result[j]: real.append(j) result.pop(j) result.insert(0, queries[i]) continue return real
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR RETURN VAR VAR VAR
Given the array queries of positive integers between 1 and m, you have to process all queries[i] (from i=0 to i=queries.length-1) according to the following rules: In the beginning, you have the permutation P=[1,2,3,...,m]. For the current i, find the position of queries[i] in the permutation P (indexing from 0) and then move this at the beginning of the permutation P. Notice that the position of queries[i] in P is the result for queries[i]. Return an array containing the result for the given queries.   Example 1: Input: queries = [3,1,2,1], m = 5 Output: [2,1,2,1] Explanation: The queries are processed as follow: For i=0: queries[i]=3, P=[1,2,3,4,5], position of 3 in P is 2, then we move 3 to the beginning of P resulting in P=[3,1,2,4,5]. For i=1: queries[i]=1, P=[3,1,2,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,3,2,4,5]. For i=2: queries[i]=2, P=[1,3,2,4,5], position of 2 in P is 2, then we move 2 to the beginning of P resulting in P=[2,1,3,4,5]. For i=3: queries[i]=1, P=[2,1,3,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,2,3,4,5]. Therefore, the array containing the result is [2,1,2,1]. Example 2: Input: queries = [4,1,2,2], m = 4 Output: [3,1,2,0] Example 3: Input: queries = [7,5,5,8,3], m = 8 Output: [6,5,0,7,5]   Constraints: 1 <= m <= 10^3 1 <= queries.length <= m 1 <= queries[i] <= m
class Solution: def processQueries(self, queries: List[int], m: int) -> List[int]: class Node: def __init__(self, val, nextNode=None): self.val = val self.next = nextNode dummy = Node(-1) res = {} out = [] cur = dummy for i in range(m): cur.next = Node(i + 1) res[i + 1] = [i, cur.__next__] cur = cur.__next__ cur = dummy for query in queries: out.append(res[query][0]) if not res[query][0]: continue cur = dummy.__next__ while cur and cur.val != query: res[cur.val][0] += 1 prev = cur cur = cur.__next__ prev.next = cur.__next__ cur.next = dummy.__next__ dummy.next = cur res[cur.val][0] = 0 return out
CLASS_DEF FUNC_DEF VAR VAR VAR CLASS_DEF FUNC_DEF NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER LIST VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER NUMBER RETURN VAR VAR VAR
Given the array queries of positive integers between 1 and m, you have to process all queries[i] (from i=0 to i=queries.length-1) according to the following rules: In the beginning, you have the permutation P=[1,2,3,...,m]. For the current i, find the position of queries[i] in the permutation P (indexing from 0) and then move this at the beginning of the permutation P. Notice that the position of queries[i] in P is the result for queries[i]. Return an array containing the result for the given queries.   Example 1: Input: queries = [3,1,2,1], m = 5 Output: [2,1,2,1] Explanation: The queries are processed as follow: For i=0: queries[i]=3, P=[1,2,3,4,5], position of 3 in P is 2, then we move 3 to the beginning of P resulting in P=[3,1,2,4,5]. For i=1: queries[i]=1, P=[3,1,2,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,3,2,4,5]. For i=2: queries[i]=2, P=[1,3,2,4,5], position of 2 in P is 2, then we move 2 to the beginning of P resulting in P=[2,1,3,4,5]. For i=3: queries[i]=1, P=[2,1,3,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,2,3,4,5]. Therefore, the array containing the result is [2,1,2,1]. Example 2: Input: queries = [4,1,2,2], m = 4 Output: [3,1,2,0] Example 3: Input: queries = [7,5,5,8,3], m = 8 Output: [6,5,0,7,5]   Constraints: 1 <= m <= 10^3 1 <= queries.length <= m 1 <= queries[i] <= m
class Node: def __init__(self, val=None): self.val = val self.next = None class Solution: def processQueries(self, queries: List[int], m: int) -> List[int]: root = Node(0) cur = root for i in range(1, m + 1): newNode = Node(i) cur.next = newNode cur = newNode res = [] for i in range(len(queries)): targetVal = queries[i] cur = root position = 0 while cur.next is not None: if cur.next.val == targetVal: res.append(position) temp = cur.next cur.next = temp.next temp.next = root.next root.next = temp break else: cur = cur.next position += 1 return res
CLASS_DEF FUNC_DEF NONE ASSIGN VAR VAR ASSIGN VAR NONE CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NONE IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR VAR VAR
Given the array queries of positive integers between 1 and m, you have to process all queries[i] (from i=0 to i=queries.length-1) according to the following rules: In the beginning, you have the permutation P=[1,2,3,...,m]. For the current i, find the position of queries[i] in the permutation P (indexing from 0) and then move this at the beginning of the permutation P. Notice that the position of queries[i] in P is the result for queries[i]. Return an array containing the result for the given queries.   Example 1: Input: queries = [3,1,2,1], m = 5 Output: [2,1,2,1] Explanation: The queries are processed as follow: For i=0: queries[i]=3, P=[1,2,3,4,5], position of 3 in P is 2, then we move 3 to the beginning of P resulting in P=[3,1,2,4,5]. For i=1: queries[i]=1, P=[3,1,2,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,3,2,4,5]. For i=2: queries[i]=2, P=[1,3,2,4,5], position of 2 in P is 2, then we move 2 to the beginning of P resulting in P=[2,1,3,4,5]. For i=3: queries[i]=1, P=[2,1,3,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,2,3,4,5]. Therefore, the array containing the result is [2,1,2,1]. Example 2: Input: queries = [4,1,2,2], m = 4 Output: [3,1,2,0] Example 3: Input: queries = [7,5,5,8,3], m = 8 Output: [6,5,0,7,5]   Constraints: 1 <= m <= 10^3 1 <= queries.length <= m 1 <= queries[i] <= m
class Solution: def processQueries(self, queries: List[int], m: int) -> List[int]: dic = {} for i in range(1, m + 1): dic[i] = i - 1 ans = [] leng = 0 for query in queries: index = dic[query] ans.append(index) for k, v in list(dic.items()): if v < index: dic[k] += 1 leng += 1 dic[query] = 0 return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER RETURN VAR VAR VAR
Given the array queries of positive integers between 1 and m, you have to process all queries[i] (from i=0 to i=queries.length-1) according to the following rules: In the beginning, you have the permutation P=[1,2,3,...,m]. For the current i, find the position of queries[i] in the permutation P (indexing from 0) and then move this at the beginning of the permutation P. Notice that the position of queries[i] in P is the result for queries[i]. Return an array containing the result for the given queries.   Example 1: Input: queries = [3,1,2,1], m = 5 Output: [2,1,2,1] Explanation: The queries are processed as follow: For i=0: queries[i]=3, P=[1,2,3,4,5], position of 3 in P is 2, then we move 3 to the beginning of P resulting in P=[3,1,2,4,5]. For i=1: queries[i]=1, P=[3,1,2,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,3,2,4,5]. For i=2: queries[i]=2, P=[1,3,2,4,5], position of 2 in P is 2, then we move 2 to the beginning of P resulting in P=[2,1,3,4,5]. For i=3: queries[i]=1, P=[2,1,3,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,2,3,4,5]. Therefore, the array containing the result is [2,1,2,1]. Example 2: Input: queries = [4,1,2,2], m = 4 Output: [3,1,2,0] Example 3: Input: queries = [7,5,5,8,3], m = 8 Output: [6,5,0,7,5]   Constraints: 1 <= m <= 10^3 1 <= queries.length <= m 1 <= queries[i] <= m
class Solution: def processQueries(self, queries: List[int], m: int) -> List[int]: if m == 1: return [(0) for _ in range(len(queries))] p = [(i + 1) for i in range(m)] res = [] for i in queries: prev = p[0] if prev == i: res.append(0) else: j = 1 while j < m: tmp = p[j] p[j] = prev if tmp == i: p[0] = tmp res.append(j) break prev = tmp j += 1 return res
CLASS_DEF FUNC_DEF VAR VAR VAR IF VAR NUMBER RETURN NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR VAR VAR
Given the array queries of positive integers between 1 and m, you have to process all queries[i] (from i=0 to i=queries.length-1) according to the following rules: In the beginning, you have the permutation P=[1,2,3,...,m]. For the current i, find the position of queries[i] in the permutation P (indexing from 0) and then move this at the beginning of the permutation P. Notice that the position of queries[i] in P is the result for queries[i]. Return an array containing the result for the given queries.   Example 1: Input: queries = [3,1,2,1], m = 5 Output: [2,1,2,1] Explanation: The queries are processed as follow: For i=0: queries[i]=3, P=[1,2,3,4,5], position of 3 in P is 2, then we move 3 to the beginning of P resulting in P=[3,1,2,4,5]. For i=1: queries[i]=1, P=[3,1,2,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,3,2,4,5]. For i=2: queries[i]=2, P=[1,3,2,4,5], position of 2 in P is 2, then we move 2 to the beginning of P resulting in P=[2,1,3,4,5]. For i=3: queries[i]=1, P=[2,1,3,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,2,3,4,5]. Therefore, the array containing the result is [2,1,2,1]. Example 2: Input: queries = [4,1,2,2], m = 4 Output: [3,1,2,0] Example 3: Input: queries = [7,5,5,8,3], m = 8 Output: [6,5,0,7,5]   Constraints: 1 <= m <= 10^3 1 <= queries.length <= m 1 <= queries[i] <= m
class Solution: def processQueries(self, queries: List[int], m: int) -> List[int]: current = list(range(m, 1 - 1, -1)) ans = list() for query in queries: ind = m - 1 - current.index(query) current.remove(query) current.append(query) ans.append(ind) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR VAR
Given the array queries of positive integers between 1 and m, you have to process all queries[i] (from i=0 to i=queries.length-1) according to the following rules: In the beginning, you have the permutation P=[1,2,3,...,m]. For the current i, find the position of queries[i] in the permutation P (indexing from 0) and then move this at the beginning of the permutation P. Notice that the position of queries[i] in P is the result for queries[i]. Return an array containing the result for the given queries.   Example 1: Input: queries = [3,1,2,1], m = 5 Output: [2,1,2,1] Explanation: The queries are processed as follow: For i=0: queries[i]=3, P=[1,2,3,4,5], position of 3 in P is 2, then we move 3 to the beginning of P resulting in P=[3,1,2,4,5]. For i=1: queries[i]=1, P=[3,1,2,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,3,2,4,5]. For i=2: queries[i]=2, P=[1,3,2,4,5], position of 2 in P is 2, then we move 2 to the beginning of P resulting in P=[2,1,3,4,5]. For i=3: queries[i]=1, P=[2,1,3,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,2,3,4,5]. Therefore, the array containing the result is [2,1,2,1]. Example 2: Input: queries = [4,1,2,2], m = 4 Output: [3,1,2,0] Example 3: Input: queries = [7,5,5,8,3], m = 8 Output: [6,5,0,7,5]   Constraints: 1 <= m <= 10^3 1 <= queries.length <= m 1 <= queries[i] <= m
class Solution: def processQueries(self, queries: List[int], m: int) -> List[int]: P = [(i + 1) for i in range(m)] res = [] for q in queries: i = P.index(q) res.append(i) P = [q] + P[:i] + P[i + 1 :] return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR
Given the array queries of positive integers between 1 and m, you have to process all queries[i] (from i=0 to i=queries.length-1) according to the following rules: In the beginning, you have the permutation P=[1,2,3,...,m]. For the current i, find the position of queries[i] in the permutation P (indexing from 0) and then move this at the beginning of the permutation P. Notice that the position of queries[i] in P is the result for queries[i]. Return an array containing the result for the given queries.   Example 1: Input: queries = [3,1,2,1], m = 5 Output: [2,1,2,1] Explanation: The queries are processed as follow: For i=0: queries[i]=3, P=[1,2,3,4,5], position of 3 in P is 2, then we move 3 to the beginning of P resulting in P=[3,1,2,4,5]. For i=1: queries[i]=1, P=[3,1,2,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,3,2,4,5]. For i=2: queries[i]=2, P=[1,3,2,4,5], position of 2 in P is 2, then we move 2 to the beginning of P resulting in P=[2,1,3,4,5]. For i=3: queries[i]=1, P=[2,1,3,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,2,3,4,5]. Therefore, the array containing the result is [2,1,2,1]. Example 2: Input: queries = [4,1,2,2], m = 4 Output: [3,1,2,0] Example 3: Input: queries = [7,5,5,8,3], m = 8 Output: [6,5,0,7,5]   Constraints: 1 <= m <= 10^3 1 <= queries.length <= m 1 <= queries[i] <= m
class Solution: def processQueries(self, queries: List[int], m: int) -> List[int]: result = [] p = [i for i in range(1, m + 1)] for query in queries: for index, value in enumerate(p): if value == query: result.append(index) depCounter = index while depCounter > 0: p[depCounter] = p[depCounter - 1] depCounter -= 1 p[0] = value break return result
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR RETURN VAR VAR VAR
Given the array queries of positive integers between 1 and m, you have to process all queries[i] (from i=0 to i=queries.length-1) according to the following rules: In the beginning, you have the permutation P=[1,2,3,...,m]. For the current i, find the position of queries[i] in the permutation P (indexing from 0) and then move this at the beginning of the permutation P. Notice that the position of queries[i] in P is the result for queries[i]. Return an array containing the result for the given queries.   Example 1: Input: queries = [3,1,2,1], m = 5 Output: [2,1,2,1] Explanation: The queries are processed as follow: For i=0: queries[i]=3, P=[1,2,3,4,5], position of 3 in P is 2, then we move 3 to the beginning of P resulting in P=[3,1,2,4,5]. For i=1: queries[i]=1, P=[3,1,2,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,3,2,4,5]. For i=2: queries[i]=2, P=[1,3,2,4,5], position of 2 in P is 2, then we move 2 to the beginning of P resulting in P=[2,1,3,4,5]. For i=3: queries[i]=1, P=[2,1,3,4,5], position of 1 in P is 1, then we move 1 to the beginning of P resulting in P=[1,2,3,4,5]. Therefore, the array containing the result is [2,1,2,1]. Example 2: Input: queries = [4,1,2,2], m = 4 Output: [3,1,2,0] Example 3: Input: queries = [7,5,5,8,3], m = 8 Output: [6,5,0,7,5]   Constraints: 1 <= m <= 10^3 1 <= queries.length <= m 1 <= queries[i] <= m
class Solution: def processQueries(self, queries: List[int], m: int) -> List[int]: P_lst = [i for i in range(1, m + 1)] q_list = [] for i in range(0, len(queries)): target = queries[i] for j in range(0, len(P_lst)): if P_lst[j] == target: q_list.append(j) x = P_lst.pop(j) P_lst.insert(0, x) break return q_list
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER VAR RETURN VAR VAR VAR
Given an array of integers of size N. For each ith element in the array, calculate the absolute difference between the count of numbers that are to the left of i and are strictly greater than ith element, and the count of numbers that are to the right of i and are strictly lesser than ith element. Example 1: Input: N = 5 A[] = {5, 4, 3, 2, 1} Output: 4 2 0 2 4 Explanation: We can see that the required number for the 1st element is |0-4| = 4 Example 2: Input: N = 5 A[] = {1, 2, 3, 4, 5} Output: 0 0 0 0 0 Explanation: There is no greater element on the left for any element and no lesser element on the right. Your Task: You don't need to read input or print anything. Your task is to complete the function greaterLesser() which takes the array arr[], its size Nas input parameters and returns the required array. Expected Time Complexity: O(N log N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5} -10^{9}^{ }≤ arr[i] ≤ 10^{9} Array can contain duplicate elements.
class Solution: class Seg_Tree: def __init__(self, n, arr): self.n = n self.arr = arr self.tree = [0] * (n * 4 + 4) def build(self, s, e, idx): if s == e: self.tree[idx] = self.arr[s] return mid = s + (e - s) // 2 self.build(s, mid, 2 * idx) self.build(mid + 1, e, 2 * idx + 1) self.tree[idx] = self.tree[2 * idx] + self.tree[2 * idx + 1] def update(self, pos, v, s, e, idx): if pos < s or pos > e: return if s == e: self.tree[idx] += 1 return mid = s + (e - s) // 2 self.update(pos, v, s, mid, 2 * idx) self.update(pos, v, mid + 1, e, 2 * idx + 1) self.tree[idx] = self.tree[2 * idx] + self.tree[2 * idx + 1] def query(self, qs, qe, s, e, idx): if qs <= s and qe >= e: return self.tree[idx] if qs > e or qe < s: return 0 mid = s + (e - s) // 2 return self.query(qs, qe, s, mid, 2 * idx) + self.query( qs, qe, mid + 1, e, 2 * idx + 1 ) def solve(self, arr, n): A = [0] * (n + 1) mp = dict() for i in range(n): mp[arr[i]] = 0 c = 0 for i in sorted(mp): c += 1 mp[i] = c for i in range(1, n + 1): A[i] = mp[arr[i - 1]] res = [] obj = self.Seg_Tree(n, arr) for i in range(1, n + 1): obj.update(A[i], 1, 1, n, 1) res.append(obj.query(A[i] + 1, n, 1, n, 1)) return res def greaterLesser(self, A, N): left = [] right = [] ans = [] left = self.solve(A, N) temp = [] for i in range(N): temp.append(-A[N - i - 1]) right = self.solve(temp, N) for i in range(N): ans.append(abs(left[i] - right[N - i - 1])) return ans
CLASS_DEF CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_DEF IF VAR VAR ASSIGN VAR VAR VAR VAR RETURN ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF IF VAR VAR VAR VAR RETURN IF VAR VAR VAR VAR NUMBER RETURN ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF IF VAR VAR VAR VAR RETURN VAR VAR IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR VAR BIN_OP NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR
Given a BST and a number X, find Ceil of X. Note: Ceil(X) is a number that is either equal to X or is immediately greater than X. Example 1: Input: 5 / \ 1 7 \ 2 \ 3 X = 3 Output: 3 Explanation: We find 3 in BST, so ceil of 3 is 3. Example 2: Input: 10 / \ 5 11 / \ 4 7 \ 8 X = 6 Output: 7 Explanation: We find 7 in BST, so ceil of 6 is 7. Your task: You don't need to read input or print anything. Just complete the function findCeil() to implement ceil in BST which returns the ceil of X in the given BST. Expected Time Complexity: O(Height of the BST) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5} 1 <= Value of nodes<= 10^{5}
class Solution: def findCeil(self, root, inp): def find(root, inp): if root: if root.key == inp: ans[0] = root.key return elif root.key >= inp: ans[0] = root.key return find(root.left, inp) else: return find(root.right, inp) return ans = [-1] find(root, inp) return ans[0]
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR IF VAR VAR ASSIGN VAR NUMBER VAR RETURN IF VAR VAR ASSIGN VAR NUMBER VAR RETURN FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR RETURN ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR NUMBER
Given a BST and a number X, find Ceil of X. Note: Ceil(X) is a number that is either equal to X or is immediately greater than X. Example 1: Input: 5 / \ 1 7 \ 2 \ 3 X = 3 Output: 3 Explanation: We find 3 in BST, so ceil of 3 is 3. Example 2: Input: 10 / \ 5 11 / \ 4 7 \ 8 X = 6 Output: 7 Explanation: We find 7 in BST, so ceil of 6 is 7. Your task: You don't need to read input or print anything. Just complete the function findCeil() to implement ceil in BST which returns the ceil of X in the given BST. Expected Time Complexity: O(Height of the BST) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5} 1 <= Value of nodes<= 10^{5}
class Solution: def findCeil(self, node, inp): c = -1 while node: if node.key == inp: return node.key elif node.key > inp: c = node.key node = node.left else: node = node.right return c
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER WHILE VAR IF VAR VAR RETURN VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
Given a BST and a number X, find Ceil of X. Note: Ceil(X) is a number that is either equal to X or is immediately greater than X. Example 1: Input: 5 / \ 1 7 \ 2 \ 3 X = 3 Output: 3 Explanation: We find 3 in BST, so ceil of 3 is 3. Example 2: Input: 10 / \ 5 11 / \ 4 7 \ 8 X = 6 Output: 7 Explanation: We find 7 in BST, so ceil of 6 is 7. Your task: You don't need to read input or print anything. Just complete the function findCeil() to implement ceil in BST which returns the ceil of X in the given BST. Expected Time Complexity: O(Height of the BST) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5} 1 <= Value of nodes<= 10^{5}
class Solution: def findCeil(self, root, inp): if root is None: return -1 if root.key == inp: return inp elif root.key < inp: return self.findCeil(root.right, inp) val = self.findCeil(root.left, inp) if val >= inp and val != -1: return val else: return root.key
CLASS_DEF FUNC_DEF IF VAR NONE RETURN NUMBER IF VAR VAR RETURN VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER RETURN VAR RETURN VAR
Given a BST and a number X, find Ceil of X. Note: Ceil(X) is a number that is either equal to X or is immediately greater than X. Example 1: Input: 5 / \ 1 7 \ 2 \ 3 X = 3 Output: 3 Explanation: We find 3 in BST, so ceil of 3 is 3. Example 2: Input: 10 / \ 5 11 / \ 4 7 \ 8 X = 6 Output: 7 Explanation: We find 7 in BST, so ceil of 6 is 7. Your task: You don't need to read input or print anything. Just complete the function findCeil() to implement ceil in BST which returns the ceil of X in the given BST. Expected Time Complexity: O(Height of the BST) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5} 1 <= Value of nodes<= 10^{5}
class Solution: def findCeil(self, root, inp): ans = -1 curr = root while curr: while curr and curr.key < inp: curr = curr.right if curr and curr.key == inp: return inp while curr and curr.key > inp: ans = curr.key curr = curr.left def helper(root, inp): if root is None: return 100001 if root.key == inp: return inp if root.key > inp: return min(root.key, helper(root.left, inp)) else: return helper(root.right, inp) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR WHILE VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR RETURN VAR WHILE VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF IF VAR NONE RETURN NUMBER IF VAR VAR RETURN VAR IF VAR VAR RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR RETURN VAR
Given a BST and a number X, find Ceil of X. Note: Ceil(X) is a number that is either equal to X or is immediately greater than X. Example 1: Input: 5 / \ 1 7 \ 2 \ 3 X = 3 Output: 3 Explanation: We find 3 in BST, so ceil of 3 is 3. Example 2: Input: 10 / \ 5 11 / \ 4 7 \ 8 X = 6 Output: 7 Explanation: We find 7 in BST, so ceil of 6 is 7. Your task: You don't need to read input or print anything. Just complete the function findCeil() to implement ceil in BST which returns the ceil of X in the given BST. Expected Time Complexity: O(Height of the BST) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5} 1 <= Value of nodes<= 10^{5}
class Solution: def inorder(self, root, arr, inp): if root is not None: self.inorder(root.left, arr, inp) arr.append(root.key) self.inorder(root.right, arr, inp) def findCeil(self, root, inp): arr = [] self.inorder(root, arr, inp) z = [] for ele in arr: if ele == inp: return ele else: z.append(ele) for ele in z: if ele > inp: return ele return -1
CLASS_DEF FUNC_DEF IF VAR NONE EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR VAR IF VAR VAR RETURN VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR RETURN VAR RETURN NUMBER
Given a BST and a number X, find Ceil of X. Note: Ceil(X) is a number that is either equal to X or is immediately greater than X. Example 1: Input: 5 / \ 1 7 \ 2 \ 3 X = 3 Output: 3 Explanation: We find 3 in BST, so ceil of 3 is 3. Example 2: Input: 10 / \ 5 11 / \ 4 7 \ 8 X = 6 Output: 7 Explanation: We find 7 in BST, so ceil of 6 is 7. Your task: You don't need to read input or print anything. Just complete the function findCeil() to implement ceil in BST which returns the ceil of X in the given BST. Expected Time Complexity: O(Height of the BST) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5} 1 <= Value of nodes<= 10^{5}
class Solution: def findCeil(self, root, inp): if root == None: return root ceil = -1 while root: if root.key < inp: root = root.right else: ceil = root.key root = root.left return ceil
CLASS_DEF FUNC_DEF IF VAR NONE RETURN VAR ASSIGN VAR NUMBER WHILE VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
Given a BST and a number X, find Ceil of X. Note: Ceil(X) is a number that is either equal to X or is immediately greater than X. Example 1: Input: 5 / \ 1 7 \ 2 \ 3 X = 3 Output: 3 Explanation: We find 3 in BST, so ceil of 3 is 3. Example 2: Input: 10 / \ 5 11 / \ 4 7 \ 8 X = 6 Output: 7 Explanation: We find 7 in BST, so ceil of 6 is 7. Your task: You don't need to read input or print anything. Just complete the function findCeil() to implement ceil in BST which returns the ceil of X in the given BST. Expected Time Complexity: O(Height of the BST) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5} 1 <= Value of nodes<= 10^{5}
class Solution: def findCeil(self, root, x): cur, res = root, -1 while cur: if x < cur.key: res = cur.key cur = cur.left elif x > cur.key: cur = cur.right else: return x return res
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR NUMBER WHILE VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR RETURN VAR
Given a BST and a number X, find Ceil of X. Note: Ceil(X) is a number that is either equal to X or is immediately greater than X. Example 1: Input: 5 / \ 1 7 \ 2 \ 3 X = 3 Output: 3 Explanation: We find 3 in BST, so ceil of 3 is 3. Example 2: Input: 10 / \ 5 11 / \ 4 7 \ 8 X = 6 Output: 7 Explanation: We find 7 in BST, so ceil of 6 is 7. Your task: You don't need to read input or print anything. Just complete the function findCeil() to implement ceil in BST which returns the ceil of X in the given BST. Expected Time Complexity: O(Height of the BST) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5} 1 <= Value of nodes<= 10^{5}
class Solution: def __init__(self): self.ans = -1 def findCeil(self, root, x): if not root: return -1 if root.key == x: self.ans = x return x if root.key > x: self.ans = root.key if root.key > x: self.findCeil(root.left, x) else: self.findCeil(root.right, x) return self.ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF IF VAR RETURN NUMBER IF VAR VAR ASSIGN VAR VAR RETURN VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given a BST and a number X, find Ceil of X. Note: Ceil(X) is a number that is either equal to X or is immediately greater than X. Example 1: Input: 5 / \ 1 7 \ 2 \ 3 X = 3 Output: 3 Explanation: We find 3 in BST, so ceil of 3 is 3. Example 2: Input: 10 / \ 5 11 / \ 4 7 \ 8 X = 6 Output: 7 Explanation: We find 7 in BST, so ceil of 6 is 7. Your task: You don't need to read input or print anything. Just complete the function findCeil() to implement ceil in BST which returns the ceil of X in the given BST. Expected Time Complexity: O(Height of the BST) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5} 1 <= Value of nodes<= 10^{5}
class Solution: def findCeil(self, root, inp): self.ans = root.key def help(root): if not root: return None if root.key == inp: self.ans = root.key return elif root.key > inp: if root.key >= inp: self.ans = root.key help(root.left) elif root.key < inp: help(root.right) help(root) if self.ans >= inp: return self.ans else: return -1
CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_DEF IF VAR RETURN NONE IF VAR VAR ASSIGN VAR VAR RETURN IF VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR RETURN NUMBER
Given a BST and a number X, find Ceil of X. Note: Ceil(X) is a number that is either equal to X or is immediately greater than X. Example 1: Input: 5 / \ 1 7 \ 2 \ 3 X = 3 Output: 3 Explanation: We find 3 in BST, so ceil of 3 is 3. Example 2: Input: 10 / \ 5 11 / \ 4 7 \ 8 X = 6 Output: 7 Explanation: We find 7 in BST, so ceil of 6 is 7. Your task: You don't need to read input or print anything. Just complete the function findCeil() to implement ceil in BST which returns the ceil of X in the given BST. Expected Time Complexity: O(Height of the BST) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5} 1 <= Value of nodes<= 10^{5}
class Solution: def findCeil(self, root, inp): li = self.inorder(root, []) if inp in li: return inp else: li.append(inp) li.sort() x = li.index(inp) try: return li[x + 1] except IndexError: return -1 def inorder(self, node, a): if node is None: return self.inorder(node.left, a) a.append(node.key) self.inorder(node.right, a) return a
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR LIST IF VAR VAR RETURN VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR BIN_OP VAR NUMBER VAR RETURN NUMBER FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given a BST and a number X, find Ceil of X. Note: Ceil(X) is a number that is either equal to X or is immediately greater than X. Example 1: Input: 5 / \ 1 7 \ 2 \ 3 X = 3 Output: 3 Explanation: We find 3 in BST, so ceil of 3 is 3. Example 2: Input: 10 / \ 5 11 / \ 4 7 \ 8 X = 6 Output: 7 Explanation: We find 7 in BST, so ceil of 6 is 7. Your task: You don't need to read input or print anything. Just complete the function findCeil() to implement ceil in BST which returns the ceil of X in the given BST. Expected Time Complexity: O(Height of the BST) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5} 1 <= Value of nodes<= 10^{5}
class Solution: def helper(self, node, prev, x): if node is None: return -1 if prev is None else prev.key if node.key == x: return node.key if node.key > x: prev = node return self.helper(node.left, prev, x) else: return self.helper(node.right, prev, x) def findCeil(self, root, x): return self.helper(root, None, x)
CLASS_DEF FUNC_DEF IF VAR NONE RETURN VAR NONE NUMBER VAR IF VAR VAR RETURN VAR IF VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR NONE VAR
Given a BST and a number X, find Ceil of X. Note: Ceil(X) is a number that is either equal to X or is immediately greater than X. Example 1: Input: 5 / \ 1 7 \ 2 \ 3 X = 3 Output: 3 Explanation: We find 3 in BST, so ceil of 3 is 3. Example 2: Input: 10 / \ 5 11 / \ 4 7 \ 8 X = 6 Output: 7 Explanation: We find 7 in BST, so ceil of 6 is 7. Your task: You don't need to read input or print anything. Just complete the function findCeil() to implement ceil in BST which returns the ceil of X in the given BST. Expected Time Complexity: O(Height of the BST) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5} 1 <= Value of nodes<= 10^{5}
class Solution: def findCeil(self, root, inp): left = 0 right = 0 if root.left is not None: left = self.findCeil(root.left, inp) if root.right is not None: right = self.findCeil(root.right, inp) ans = root.key if inp <= left <= ans or inp > ans: ans = left if inp <= right <= ans or inp > ans: ans = right if ans < inp: ans = -1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NONE ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NONE ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR NUMBER RETURN VAR
Given a BST and a number X, find Ceil of X. Note: Ceil(X) is a number that is either equal to X or is immediately greater than X. Example 1: Input: 5 / \ 1 7 \ 2 \ 3 X = 3 Output: 3 Explanation: We find 3 in BST, so ceil of 3 is 3. Example 2: Input: 10 / \ 5 11 / \ 4 7 \ 8 X = 6 Output: 7 Explanation: We find 7 in BST, so ceil of 6 is 7. Your task: You don't need to read input or print anything. Just complete the function findCeil() to implement ceil in BST which returns the ceil of X in the given BST. Expected Time Complexity: O(Height of the BST) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5} 1 <= Value of nodes<= 10^{5}
class Solution: def findCeil(self, root, inp): def walk(node): if not node: return -1 if node.key == inp: return inp if node.key < inp: return walk(node.right) x = walk(node.left) return node.key if x == -1 else x return walk(root)
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR RETURN NUMBER IF VAR VAR RETURN VAR IF VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR NUMBER VAR VAR RETURN FUNC_CALL VAR VAR
Given a BST and a number X, find Ceil of X. Note: Ceil(X) is a number that is either equal to X or is immediately greater than X. Example 1: Input: 5 / \ 1 7 \ 2 \ 3 X = 3 Output: 3 Explanation: We find 3 in BST, so ceil of 3 is 3. Example 2: Input: 10 / \ 5 11 / \ 4 7 \ 8 X = 6 Output: 7 Explanation: We find 7 in BST, so ceil of 6 is 7. Your task: You don't need to read input or print anything. Just complete the function findCeil() to implement ceil in BST which returns the ceil of X in the given BST. Expected Time Complexity: O(Height of the BST) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5} 1 <= Value of nodes<= 10^{5}
class Solution: def findCeil(self, root, x): if root == None: return -1 if x < root.key: tmp = self.findCeil(root.left, x) return min(root.key, tmp) if tmp != -1 else root.key elif x > root.key: return self.findCeil(root.right, x) return root.key
CLASS_DEF FUNC_DEF IF VAR NONE RETURN NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR NUMBER FUNC_CALL VAR VAR VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR RETURN VAR
Given a BST and a number X, find Ceil of X. Note: Ceil(X) is a number that is either equal to X or is immediately greater than X. Example 1: Input: 5 / \ 1 7 \ 2 \ 3 X = 3 Output: 3 Explanation: We find 3 in BST, so ceil of 3 is 3. Example 2: Input: 10 / \ 5 11 / \ 4 7 \ 8 X = 6 Output: 7 Explanation: We find 7 in BST, so ceil of 6 is 7. Your task: You don't need to read input or print anything. Just complete the function findCeil() to implement ceil in BST which returns the ceil of X in the given BST. Expected Time Complexity: O(Height of the BST) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5} 1 <= Value of nodes<= 10^{5}
class Solution: maxi = 99999 def findCeil(self, root, inp): if root == None: if self.maxi == 99999: return -1 return self.maxi if root.key >= inp and root.key < self.maxi: self.maxi = root.key if root.key == inp: return root.key if inp < root.key: return self.findCeil(root.left, inp) elif inp > root.key: return self.findCeil(root.right, inp)
CLASS_DEF ASSIGN VAR NUMBER FUNC_DEF IF VAR NONE IF VAR NUMBER RETURN NUMBER RETURN VAR IF VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR RETURN VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR
Given a BST and a number X, find Ceil of X. Note: Ceil(X) is a number that is either equal to X or is immediately greater than X. Example 1: Input: 5 / \ 1 7 \ 2 \ 3 X = 3 Output: 3 Explanation: We find 3 in BST, so ceil of 3 is 3. Example 2: Input: 10 / \ 5 11 / \ 4 7 \ 8 X = 6 Output: 7 Explanation: We find 7 in BST, so ceil of 6 is 7. Your task: You don't need to read input or print anything. Just complete the function findCeil() to implement ceil in BST which returns the ceil of X in the given BST. Expected Time Complexity: O(Height of the BST) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5} 1 <= Value of nodes<= 10^{5}
class Solution: def findCeil(self, root, k): re = [] def trav(r): re.append(r.key) if r.left is not None: trav(r.left) if r.right is not None: trav(r.right) trav(root) re = sorted(re) pr = 0 if k in re: return k for i in re: if i > k: return i return -1
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF EXPR FUNC_CALL VAR VAR IF VAR NONE EXPR FUNC_CALL VAR VAR IF VAR NONE EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR RETURN VAR FOR VAR VAR IF VAR VAR RETURN VAR RETURN NUMBER
Given a BST and a number X, find Ceil of X. Note: Ceil(X) is a number that is either equal to X or is immediately greater than X. Example 1: Input: 5 / \ 1 7 \ 2 \ 3 X = 3 Output: 3 Explanation: We find 3 in BST, so ceil of 3 is 3. Example 2: Input: 10 / \ 5 11 / \ 4 7 \ 8 X = 6 Output: 7 Explanation: We find 7 in BST, so ceil of 6 is 7. Your task: You don't need to read input or print anything. Just complete the function findCeil() to implement ceil in BST which returns the ceil of X in the given BST. Expected Time Complexity: O(Height of the BST) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5} 1 <= Value of nodes<= 10^{5}
class Solution: def __init__(self): self.prev = -1 def findCeil(self, root, inp): if root is None: return self.prev if root.key == inp: return root.key if root.key > inp: self.prev = root.key return self.findCeil(root.left, inp) if root.key < inp: return self.findCeil(root.right, inp)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF IF VAR NONE RETURN VAR IF VAR VAR RETURN VAR IF VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR
Given a BST and a number X, find Ceil of X. Note: Ceil(X) is a number that is either equal to X or is immediately greater than X. Example 1: Input: 5 / \ 1 7 \ 2 \ 3 X = 3 Output: 3 Explanation: We find 3 in BST, so ceil of 3 is 3. Example 2: Input: 10 / \ 5 11 / \ 4 7 \ 8 X = 6 Output: 7 Explanation: We find 7 in BST, so ceil of 6 is 7. Your task: You don't need to read input or print anything. Just complete the function findCeil() to implement ceil in BST which returns the ceil of X in the given BST. Expected Time Complexity: O(Height of the BST) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5} 1 <= Value of nodes<= 10^{5}
class Solution: def answer(self, root, x, ans): if root.key == x: ans[0] = x return elif root.key > x: ans[0] = root.key if root.left: self.answer(root.left, x, ans) elif root.key < x: if root.right: self.answer(root.right, x, ans) def findCeil(self, root, inp): if not root: return -1 ans = [root.key] self.answer(root, inp, ans) if ans[0] < inp: return -1 return ans[0]
CLASS_DEF FUNC_DEF IF VAR VAR ASSIGN VAR NUMBER VAR RETURN IF VAR VAR ASSIGN VAR NUMBER VAR IF VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR LIST VAR EXPR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER VAR RETURN NUMBER RETURN VAR NUMBER
Given a BST and a number X, find Ceil of X. Note: Ceil(X) is a number that is either equal to X or is immediately greater than X. Example 1: Input: 5 / \ 1 7 \ 2 \ 3 X = 3 Output: 3 Explanation: We find 3 in BST, so ceil of 3 is 3. Example 2: Input: 10 / \ 5 11 / \ 4 7 \ 8 X = 6 Output: 7 Explanation: We find 7 in BST, so ceil of 6 is 7. Your task: You don't need to read input or print anything. Just complete the function findCeil() to implement ceil in BST which returns the ceil of X in the given BST. Expected Time Complexity: O(Height of the BST) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5} 1 <= Value of nodes<= 10^{5}
class Solution: def find(self, root, inp, res): if not root: return if root.key >= inp: res[0] = min(res[0], root.key) if inp < root.key: self.find(root.left, inp, res) else: self.find(root.right, inp, res) def findCeil(self, root, inp): res = [float("inf")] self.find(root, inp, res) return -1 if res[0] == float("inf") else res[0]
CLASS_DEF FUNC_DEF IF VAR RETURN IF VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR LIST FUNC_CALL VAR STRING EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR NUMBER FUNC_CALL VAR STRING NUMBER VAR NUMBER
Given a BST and a number X, find Ceil of X. Note: Ceil(X) is a number that is either equal to X or is immediately greater than X. Example 1: Input: 5 / \ 1 7 \ 2 \ 3 X = 3 Output: 3 Explanation: We find 3 in BST, so ceil of 3 is 3. Example 2: Input: 10 / \ 5 11 / \ 4 7 \ 8 X = 6 Output: 7 Explanation: We find 7 in BST, so ceil of 6 is 7. Your task: You don't need to read input or print anything. Just complete the function findCeil() to implement ceil in BST which returns the ceil of X in the given BST. Expected Time Complexity: O(Height of the BST) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5} 1 <= Value of nodes<= 10^{5}
class Solution: def helper(self, root, inp, ans): if not root: return ans if root.key == inp: ans = inp return ans if root.key > inp: ans = root.key return self.helper(root.left, inp, ans) if root.key < inp: return self.helper(root.right, inp, ans) def findCeil(self, root, inp): if not root: return -1 ans = -1 return self.helper(root, inp, ans)
CLASS_DEF FUNC_DEF IF VAR RETURN VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR IF VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR
Given a BST and a number X, find Ceil of X. Note: Ceil(X) is a number that is either equal to X or is immediately greater than X. Example 1: Input: 5 / \ 1 7 \ 2 \ 3 X = 3 Output: 3 Explanation: We find 3 in BST, so ceil of 3 is 3. Example 2: Input: 10 / \ 5 11 / \ 4 7 \ 8 X = 6 Output: 7 Explanation: We find 7 in BST, so ceil of 6 is 7. Your task: You don't need to read input or print anything. Just complete the function findCeil() to implement ceil in BST which returns the ceil of X in the given BST. Expected Time Complexity: O(Height of the BST) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5} 1 <= Value of nodes<= 10^{5}
class Solution: def findCeil(self, root, inp): return self.findRec(root, inp, -1) def findRec(self, root, inp, prev): if not root: return -1 if root.key == inp: return root.key elif root.key < inp: if root.right: return self.findRec(root.right, inp, prev) else: return prev elif root.left: return self.findRec(root.left, inp, root.key) else: return root.key
CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR VAR NUMBER FUNC_DEF IF VAR RETURN NUMBER IF VAR VAR RETURN VAR IF VAR VAR IF VAR RETURN FUNC_CALL VAR VAR VAR VAR RETURN VAR IF VAR RETURN FUNC_CALL VAR VAR VAR VAR RETURN VAR
Given a BST and a number X, find Ceil of X. Note: Ceil(X) is a number that is either equal to X or is immediately greater than X. Example 1: Input: 5 / \ 1 7 \ 2 \ 3 X = 3 Output: 3 Explanation: We find 3 in BST, so ceil of 3 is 3. Example 2: Input: 10 / \ 5 11 / \ 4 7 \ 8 X = 6 Output: 7 Explanation: We find 7 in BST, so ceil of 6 is 7. Your task: You don't need to read input or print anything. Just complete the function findCeil() to implement ceil in BST which returns the ceil of X in the given BST. Expected Time Complexity: O(Height of the BST) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5} 1 <= Value of nodes<= 10^{5}
class Solution: def __init__(self): self.res = -1 def inorder(self, temp, key): if temp is None: return None self.inorder(temp.left, key) if key == temp.key or temp.key > key: if self.res == -1: self.res = temp.key self.inorder(temp.right, key) def findCeil(self, root, inp): self.inorder(root, inp) return self.res
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF IF VAR NONE RETURN NONE EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given a BST and a number X, find Ceil of X. Note: Ceil(X) is a number that is either equal to X or is immediately greater than X. Example 1: Input: 5 / \ 1 7 \ 2 \ 3 X = 3 Output: 3 Explanation: We find 3 in BST, so ceil of 3 is 3. Example 2: Input: 10 / \ 5 11 / \ 4 7 \ 8 X = 6 Output: 7 Explanation: We find 7 in BST, so ceil of 6 is 7. Your task: You don't need to read input or print anything. Just complete the function findCeil() to implement ceil in BST which returns the ceil of X in the given BST. Expected Time Complexity: O(Height of the BST) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5} 1 <= Value of nodes<= 10^{5}
class Solution: def findCeil(self, root, inp): def inorder(root): nonlocal ans, inp if not root: return inorder(root.left) if root.key >= inp: if ans == -1: ans = root.key elif ans > root.key: ans = root.key inorder(root.right) ans = -1 inorder(root) return ans
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR RETURN EXPR FUNC_CALL VAR VAR IF VAR VAR IF VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR
Given a BST and a number X, find Ceil of X. Note: Ceil(X) is a number that is either equal to X or is immediately greater than X. Example 1: Input: 5 / \ 1 7 \ 2 \ 3 X = 3 Output: 3 Explanation: We find 3 in BST, so ceil of 3 is 3. Example 2: Input: 10 / \ 5 11 / \ 4 7 \ 8 X = 6 Output: 7 Explanation: We find 7 in BST, so ceil of 6 is 7. Your task: You don't need to read input or print anything. Just complete the function findCeil() to implement ceil in BST which returns the ceil of X in the given BST. Expected Time Complexity: O(Height of the BST) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5} 1 <= Value of nodes<= 10^{5}
class Solution: def dfs(self, node, target, prev): if not node: return if node.key == target: return target if node.key > target: if node.left: return self.dfs(node.left, target, node) else: return node.key elif node.right: return self.dfs(node.right, target, prev) else: return prev.key def findCeil(self, root, inp): return self.dfs(root, inp, Node(-1))
CLASS_DEF FUNC_DEF IF VAR RETURN IF VAR VAR RETURN VAR IF VAR VAR IF VAR RETURN FUNC_CALL VAR VAR VAR VAR RETURN VAR IF VAR RETURN FUNC_CALL VAR VAR VAR VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER
Given a BST and a number X, find Ceil of X. Note: Ceil(X) is a number that is either equal to X or is immediately greater than X. Example 1: Input: 5 / \ 1 7 \ 2 \ 3 X = 3 Output: 3 Explanation: We find 3 in BST, so ceil of 3 is 3. Example 2: Input: 10 / \ 5 11 / \ 4 7 \ 8 X = 6 Output: 7 Explanation: We find 7 in BST, so ceil of 6 is 7. Your task: You don't need to read input or print anything. Just complete the function findCeil() to implement ceil in BST which returns the ceil of X in the given BST. Expected Time Complexity: O(Height of the BST) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5} 1 <= Value of nodes<= 10^{5}
class Solution: def findCeil(self, root, inp): ceil = -1 def helper(node): nonlocal ceil if not node: return if node.key >= inp: ceil = node.key return helper(node.left) else: return helper(node.right) helper(root) return ceil
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF IF VAR RETURN IF VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given a BST and a number X, find Ceil of X. Note: Ceil(X) is a number that is either equal to X or is immediately greater than X. Example 1: Input: 5 / \ 1 7 \ 2 \ 3 X = 3 Output: 3 Explanation: We find 3 in BST, so ceil of 3 is 3. Example 2: Input: 10 / \ 5 11 / \ 4 7 \ 8 X = 6 Output: 7 Explanation: We find 7 in BST, so ceil of 6 is 7. Your task: You don't need to read input or print anything. Just complete the function findCeil() to implement ceil in BST which returns the ceil of X in the given BST. Expected Time Complexity: O(Height of the BST) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5} 1 <= Value of nodes<= 10^{5}
class Solution: def findCeil(self, root, x): ceil = 999989 curr = root while curr: if curr.key > x: ceil = min(ceil, curr.key) curr = curr.left elif curr.key < x: curr = curr.right else: return curr.key if ceil == 999989: return -1 else: return ceil
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR IF VAR NUMBER RETURN NUMBER RETURN VAR
Given a BST and a number X, find Ceil of X. Note: Ceil(X) is a number that is either equal to X or is immediately greater than X. Example 1: Input: 5 / \ 1 7 \ 2 \ 3 X = 3 Output: 3 Explanation: We find 3 in BST, so ceil of 3 is 3. Example 2: Input: 10 / \ 5 11 / \ 4 7 \ 8 X = 6 Output: 7 Explanation: We find 7 in BST, so ceil of 6 is 7. Your task: You don't need to read input or print anything. Just complete the function findCeil() to implement ceil in BST which returns the ceil of X in the given BST. Expected Time Complexity: O(Height of the BST) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5} 1 <= Value of nodes<= 10^{5}
class Solution: def findCeil(self, root, inp): if root == None: return -1 res = -1 return self.hel(root, inp, res) def hel(self, root, inp, res): if root == None: return res elif root.key == inp: res = inp return res elif root.key > inp: res = root.key return self.hel(root.left, inp, res) elif root.key < inp: return self.hel(root.right, inp, res)
CLASS_DEF FUNC_DEF IF VAR NONE RETURN NUMBER ASSIGN VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF IF VAR NONE RETURN VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR IF VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR
Given a BST and a number X, find Ceil of X. Note: Ceil(X) is a number that is either equal to X or is immediately greater than X. Example 1: Input: 5 / \ 1 7 \ 2 \ 3 X = 3 Output: 3 Explanation: We find 3 in BST, so ceil of 3 is 3. Example 2: Input: 10 / \ 5 11 / \ 4 7 \ 8 X = 6 Output: 7 Explanation: We find 7 in BST, so ceil of 6 is 7. Your task: You don't need to read input or print anything. Just complete the function findCeil() to implement ceil in BST which returns the ceil of X in the given BST. Expected Time Complexity: O(Height of the BST) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5} 1 <= Value of nodes<= 10^{5}
class Solution: def findCeil(self, root, inp): curr = root prevmax = None while curr is not None: if curr.key == inp: return curr.key if curr.key < inp: curr = curr.right else: prevmax = curr curr = curr.left return prevmax.key if prevmax is not None else -1
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NONE WHILE VAR NONE IF VAR VAR RETURN VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR NONE VAR NUMBER
Given a BST and a number X, find Ceil of X. Note: Ceil(X) is a number that is either equal to X or is immediately greater than X. Example 1: Input: 5 / \ 1 7 \ 2 \ 3 X = 3 Output: 3 Explanation: We find 3 in BST, so ceil of 3 is 3. Example 2: Input: 10 / \ 5 11 / \ 4 7 \ 8 X = 6 Output: 7 Explanation: We find 7 in BST, so ceil of 6 is 7. Your task: You don't need to read input or print anything. Just complete the function findCeil() to implement ceil in BST which returns the ceil of X in the given BST. Expected Time Complexity: O(Height of the BST) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5} 1 <= Value of nodes<= 10^{5}
import sys class Solution: def util(self, root, inp): global ans if root == None: return if root.key == inp: if ans[0] > inp - root.key: ans = [0, root.key] return elif root.key > inp: if ans[0] > root.key - inp: ans = [root.key - inp, root.key] self.util(root.left, inp) else: self.util(root.right, inp) def findCeil(self, root, inp): global ans ans = [sys.maxsize, -1] self.util(root, inp) return ans[1]
IMPORT CLASS_DEF FUNC_DEF IF VAR NONE RETURN IF VAR VAR IF VAR NUMBER BIN_OP VAR VAR ASSIGN VAR LIST NUMBER VAR RETURN IF VAR VAR IF VAR NUMBER BIN_OP VAR VAR ASSIGN VAR LIST BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR LIST VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR NUMBER
Given a BST and a number X, find Ceil of X. Note: Ceil(X) is a number that is either equal to X or is immediately greater than X. Example 1: Input: 5 / \ 1 7 \ 2 \ 3 X = 3 Output: 3 Explanation: We find 3 in BST, so ceil of 3 is 3. Example 2: Input: 10 / \ 5 11 / \ 4 7 \ 8 X = 6 Output: 7 Explanation: We find 7 in BST, so ceil of 6 is 7. Your task: You don't need to read input or print anything. Just complete the function findCeil() to implement ceil in BST which returns the ceil of X in the given BST. Expected Time Complexity: O(Height of the BST) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5} 1 <= Value of nodes<= 10^{5}
class Solution: def findCeil(self, root, inp): def ino(root): if root is None: return if root.key >= inp: l.append(root.key) ino(root.left) ino(root.right) l = [] ino(root) if len(l) == 0: return -1 return min(l)
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NONE RETURN IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR VAR
Given a BST and a number X, find Ceil of X. Note: Ceil(X) is a number that is either equal to X or is immediately greater than X. Example 1: Input: 5 / \ 1 7 \ 2 \ 3 X = 3 Output: 3 Explanation: We find 3 in BST, so ceil of 3 is 3. Example 2: Input: 10 / \ 5 11 / \ 4 7 \ 8 X = 6 Output: 7 Explanation: We find 7 in BST, so ceil of 6 is 7. Your task: You don't need to read input or print anything. Just complete the function findCeil() to implement ceil in BST which returns the ceil of X in the given BST. Expected Time Complexity: O(Height of the BST) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5} 1 <= Value of nodes<= 10^{5}
class Solution: def calculate(self, root, inp, ans): if not root: return ans if inp <= root.key: ans = root.key return self.calculate(root.left, inp, ans) elif inp > root.key: return self.calculate(root.right, inp, ans) def findCeil(self, root, inp): ans = -1 return self.calculate(root, inp, ans)
CLASS_DEF FUNC_DEF IF VAR RETURN VAR IF VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR
Given a BST and a number X, find Ceil of X. Note: Ceil(X) is a number that is either equal to X or is immediately greater than X. Example 1: Input: 5 / \ 1 7 \ 2 \ 3 X = 3 Output: 3 Explanation: We find 3 in BST, so ceil of 3 is 3. Example 2: Input: 10 / \ 5 11 / \ 4 7 \ 8 X = 6 Output: 7 Explanation: We find 7 in BST, so ceil of 6 is 7. Your task: You don't need to read input or print anything. Just complete the function findCeil() to implement ceil in BST which returns the ceil of X in the given BST. Expected Time Complexity: O(Height of the BST) Expected Auxiliary Space: O(Height of the BST). Constraints: 1 <= Number of nodes <= 10^{5} 1 <= Value of nodes<= 10^{5}
class Solution: def findCeil(self, root, inp): m = 10**9 while root: if root.key >= inp: m = min(m, root.key) root = root.left else: root = root.right return m if m != 10**9 else -1
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP NUMBER NUMBER WHILE VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR BIN_OP NUMBER NUMBER VAR NUMBER
Return the root node of a binary search tree that matches the given preorder traversal. (Recall that a binary search tree is a binary tree where for every node, any descendant of node.left has a value < node.val, and any descendant of node.right has a value > node.val.  Also recall that a preorder traversal displays the value of the node first, then traverses node.left, then traverses node.right.) It's guaranteed that for the given test cases there is always possible to find a binary search tree with the given requirements. Example 1: Input: [8,5,1,7,10,12] Output: [8,5,10,1,7,null,12]   Constraints: 1 <= preorder.length <= 100 1 <= preorder[i] <= 10^8 The values of preorder are distinct.
class Solution: def bstFromPreorder(self, preorder: List[int]) -> TreeNode: if len(preorder) == 0: return None return self.helper(preorder, 0, len(preorder)) def helper(self, preorder, start, end): if start >= end: return None split = start + 1 root = TreeNode(preorder[start]) while split < end and preorder[start] > preorder[split]: split += 1 root.left = self.helper(preorder, start + 1, split) root.right = self.helper(preorder, split, end) return root
CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NONE RETURN FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR VAR RETURN NONE ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR
Return the root node of a binary search tree that matches the given preorder traversal. (Recall that a binary search tree is a binary tree where for every node, any descendant of node.left has a value < node.val, and any descendant of node.right has a value > node.val.  Also recall that a preorder traversal displays the value of the node first, then traverses node.left, then traverses node.right.) It's guaranteed that for the given test cases there is always possible to find a binary search tree with the given requirements. Example 1: Input: [8,5,1,7,10,12] Output: [8,5,10,1,7,null,12]   Constraints: 1 <= preorder.length <= 100 1 <= preorder[i] <= 10^8 The values of preorder are distinct.
class Solution: def bstFromPreorder(self, preorder: List[int]) -> TreeNode: self.inorder = sorted(preorder) self.preorder = preorder[::-1] n = len(preorder) self.m = {val: i for i, val in enumerate(self.inorder)} lo, hi = 0, n - 1 return self.dfs(lo, hi) def dfs(self, lo, hi): if lo > hi: return None val = self.preorder.pop() root = TreeNode(val) mid = self.m[val] root.left = self.dfs(lo, mid - 1) root.right = self.dfs(mid + 1, hi) return root
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF IF VAR VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN VAR
Return the root node of a binary search tree that matches the given preorder traversal. (Recall that a binary search tree is a binary tree where for every node, any descendant of node.left has a value < node.val, and any descendant of node.right has a value > node.val.  Also recall that a preorder traversal displays the value of the node first, then traverses node.left, then traverses node.right.) It's guaranteed that for the given test cases there is always possible to find a binary search tree with the given requirements. Example 1: Input: [8,5,1,7,10,12] Output: [8,5,10,1,7,null,12]   Constraints: 1 <= preorder.length <= 100 1 <= preorder[i] <= 10^8 The values of preorder are distinct.
class Solution: def bstFromPreorder(self, preorder: List[int]) -> TreeNode: if not preorder: return None root = TreeNode(val=preorder[0]) parents = [(root.val, root)] heapq.heapify(parents) for idx in range(1, len(preorder)): num = preorder[idx] node = TreeNode(val=num) tmp = None while parents and parents[0][0] < num: _, tmp = heapq.heappop(parents) if tmp: tmp.right = node elif not tmp and parents: parents[0][1].left = node heapq.heappush(parents, (num, node)) return root
CLASS_DEF FUNC_DEF VAR VAR IF VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NONE WHILE VAR VAR NUMBER NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR
Return the root node of a binary search tree that matches the given preorder traversal. (Recall that a binary search tree is a binary tree where for every node, any descendant of node.left has a value < node.val, and any descendant of node.right has a value > node.val.  Also recall that a preorder traversal displays the value of the node first, then traverses node.left, then traverses node.right.) It's guaranteed that for the given test cases there is always possible to find a binary search tree with the given requirements. Example 1: Input: [8,5,1,7,10,12] Output: [8,5,10,1,7,null,12]   Constraints: 1 <= preorder.length <= 100 1 <= preorder[i] <= 10^8 The values of preorder are distinct.
class Solution: def bstFromPreorder(self, A: List[int]) -> TreeNode: A.reverse() def build(lower=-math.inf, upper=math.inf): if not A or A[-1] < lower or A[-1] > upper: return None root = TreeNode(A.pop()) root.left = build(lower, root.val) root.right = build(root.val, upper) return root return build()
CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR FUNC_DEF VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR
Return the root node of a binary search tree that matches the given preorder traversal. (Recall that a binary search tree is a binary tree where for every node, any descendant of node.left has a value < node.val, and any descendant of node.right has a value > node.val.  Also recall that a preorder traversal displays the value of the node first, then traverses node.left, then traverses node.right.) It's guaranteed that for the given test cases there is always possible to find a binary search tree with the given requirements. Example 1: Input: [8,5,1,7,10,12] Output: [8,5,10,1,7,null,12]   Constraints: 1 <= preorder.length <= 100 1 <= preorder[i] <= 10^8 The values of preorder are distinct.
class Solution: def bstFromPreorder(self, preorder: List[int]) -> TreeNode: if not preorder: return None root = TreeNode(preorder[0]) if len(preorder) == 1: return root ind = len(preorder) for i, j in enumerate(preorder): if j > root.val: ind = i break root.left = self.bstFromPreorder(preorder[1:ind]) root.right = self.bstFromPreorder(preorder[ind:]) return root
CLASS_DEF FUNC_DEF VAR VAR IF VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
Return the root node of a binary search tree that matches the given preorder traversal. (Recall that a binary search tree is a binary tree where for every node, any descendant of node.left has a value < node.val, and any descendant of node.right has a value > node.val.  Also recall that a preorder traversal displays the value of the node first, then traverses node.left, then traverses node.right.) It's guaranteed that for the given test cases there is always possible to find a binary search tree with the given requirements. Example 1: Input: [8,5,1,7,10,12] Output: [8,5,10,1,7,null,12]   Constraints: 1 <= preorder.length <= 100 1 <= preorder[i] <= 10^8 The values of preorder are distinct.
class Solution: def bstFromPreorder(self, preorder: List[int]) -> TreeNode: root = TreeNode(preorder[0]) stack = [root] for val in preorder[1:]: node = TreeNode(val) if val < stack[-1].val: stack[-1].left = node else: while stack and stack[-1].val < val: last = stack.pop() last.right = node stack.append(node) return root
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST VAR FOR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER VAR WHILE VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR
Return the root node of a binary search tree that matches the given preorder traversal. (Recall that a binary search tree is a binary tree where for every node, any descendant of node.left has a value < node.val, and any descendant of node.right has a value > node.val.  Also recall that a preorder traversal displays the value of the node first, then traverses node.left, then traverses node.right.) It's guaranteed that for the given test cases there is always possible to find a binary search tree with the given requirements. Example 1: Input: [8,5,1,7,10,12] Output: [8,5,10,1,7,null,12]   Constraints: 1 <= preorder.length <= 100 1 <= preorder[i] <= 10^8 The values of preorder are distinct.
class Solution: def bstFromPreorder(self, preorder: List[int]) -> TreeNode: vals = deque(preorder) def build(min_val, max_val): if vals and min_val < vals[0] < max_val: val = vals.popleft() node = TreeNode(val) node.left = build(min_val, val) node.right = build(val, max_val) return node return build(float("-inf"), float("inf"))
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING VAR
Return the root node of a binary search tree that matches the given preorder traversal. (Recall that a binary search tree is a binary tree where for every node, any descendant of node.left has a value < node.val, and any descendant of node.right has a value > node.val.  Also recall that a preorder traversal displays the value of the node first, then traverses node.left, then traverses node.right.) It's guaranteed that for the given test cases there is always possible to find a binary search tree with the given requirements. Example 1: Input: [8,5,1,7,10,12] Output: [8,5,10,1,7,null,12]   Constraints: 1 <= preorder.length <= 100 1 <= preorder[i] <= 10^8 The values of preorder are distinct.
class Solution: def bstFromPreorder(self, preorder: List[int]) -> TreeNode: if not preorder or len(preorder) == 0: return None inorder = sorted(preorder) root = TreeNode(preorder[0]) leftLen = inorder.index(root.val) root.left = self.bstFromPreorder(preorder[1 : 1 + leftLen]) root.right = self.bstFromPreorder(preorder[1 + leftLen :]) return root
CLASS_DEF FUNC_DEF VAR VAR IF VAR FUNC_CALL VAR VAR NUMBER RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR RETURN VAR VAR
Return the root node of a binary search tree that matches the given preorder traversal. (Recall that a binary search tree is a binary tree where for every node, any descendant of node.left has a value < node.val, and any descendant of node.right has a value > node.val.  Also recall that a preorder traversal displays the value of the node first, then traverses node.left, then traverses node.right.) It's guaranteed that for the given test cases there is always possible to find a binary search tree with the given requirements. Example 1: Input: [8,5,1,7,10,12] Output: [8,5,10,1,7,null,12]   Constraints: 1 <= preorder.length <= 100 1 <= preorder[i] <= 10^8 The values of preorder are distinct.
class Solution: def bstFromPreorder(self, preorder: List[int]) -> TreeNode: def construct(preorder): if len(preorder) == 0: return None root = TreeNode(preorder[0]) idx = 0 while idx < len(preorder): if preorder[idx] > preorder[0]: break idx += 1 root.left = construct(preorder[1:idx]) root.right = construct(preorder[idx:]) return root root = construct(preorder) return root
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR VAR
Return the root node of a binary search tree that matches the given preorder traversal. (Recall that a binary search tree is a binary tree where for every node, any descendant of node.left has a value < node.val, and any descendant of node.right has a value > node.val.  Also recall that a preorder traversal displays the value of the node first, then traverses node.left, then traverses node.right.) It's guaranteed that for the given test cases there is always possible to find a binary search tree with the given requirements. Example 1: Input: [8,5,1,7,10,12] Output: [8,5,10,1,7,null,12]   Constraints: 1 <= preorder.length <= 100 1 <= preorder[i] <= 10^8 The values of preorder are distinct.
class Solution: def bstFromPreorder(self, preorder: List[int]) -> TreeNode: if not preorder: return root = TreeNode(preorder[0]) i = 1 while i < len(preorder) and preorder[i] <= root.val: i += 1 root.left = self.bstFromPreorder(preorder[1:i]) root.right = self.bstFromPreorder(preorder[i:]) return root
CLASS_DEF FUNC_DEF VAR VAR IF VAR RETURN ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
Given a Binary Search Tree and a target sum. Check whether there's a pair of Nodes in the BST with value summing up to the target sum. Example 1: Input: 2 / \ 1 3 sum = 5 Output: 1 Explanation: Nodes with value 2 and 3 sum up to 5. Example 2: Input: 6 / 5 / 3 / \ 1 4 sum = 2 Output: 0 Explanation: There's no pair that sums up to 2. Your Task: You don't need to read input or print anything. Your task is to complete the function isPairPresent() that takes a root node and a target value as a parameter and returns 1 if there's a pair of Nodes in the BST with values summing up to the target sum, else returns 0. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 ≤ Number of Nodes ≤ 10^{5} 1 ≤ Sum ≤ 10^{6}
class Solution: def inorder(self, node, arr): if node is None: return arr self.inorder(node.left, arr) arr.append(node.data) self.inorder(node.right, arr) return arr def isPairPresent(self, root, target): inorder = self.inorder(root, []) s = 0 e = len(inorder) - 1 while s < e: sum = inorder[s] + inorder[e] if sum > target: e -= 1 elif sum < target: s += 1 else: return 1 return 0
CLASS_DEF FUNC_DEF IF VAR NONE RETURN VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR LIST ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER
Given a Binary Search Tree and a target sum. Check whether there's a pair of Nodes in the BST with value summing up to the target sum. Example 1: Input: 2 / \ 1 3 sum = 5 Output: 1 Explanation: Nodes with value 2 and 3 sum up to 5. Example 2: Input: 6 / 5 / 3 / \ 1 4 sum = 2 Output: 0 Explanation: There's no pair that sums up to 2. Your Task: You don't need to read input or print anything. Your task is to complete the function isPairPresent() that takes a root node and a target value as a parameter and returns 1 if there's a pair of Nodes in the BST with values summing up to the target sum, else returns 0. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 ≤ Number of Nodes ≤ 10^{5} 1 ≤ Sum ≤ 10^{6}
class Solution: def isPairPresent(self, root, target): def inorder(root): if root == None: return inorder(root.left) ino.append(root.data) inorder(root.right) ino = [] inorder(root) l = 0 r = len(ino) - 1 while l < r: if ino[l] + ino[r] == target: return 1 elif ino[l] + ino[r] > target: r -= 1 else: l = l + 1 return 0
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR VAR RETURN NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN NUMBER
Given a Binary Search Tree and a target sum. Check whether there's a pair of Nodes in the BST with value summing up to the target sum. Example 1: Input: 2 / \ 1 3 sum = 5 Output: 1 Explanation: Nodes with value 2 and 3 sum up to 5. Example 2: Input: 6 / 5 / 3 / \ 1 4 sum = 2 Output: 0 Explanation: There's no pair that sums up to 2. Your Task: You don't need to read input or print anything. Your task is to complete the function isPairPresent() that takes a root node and a target value as a parameter and returns 1 if there's a pair of Nodes in the BST with values summing up to the target sum, else returns 0. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 ≤ Number of Nodes ≤ 10^{5} 1 ≤ Sum ≤ 10^{6}
class Binary_iterater: def __init__(self, root, reverse): self.stack = [] self.reverse = reverse self.pushall(root) def pushall(self, node): while node: self.stack.append(node) if self.reverse is True: node = node.right else: node = node.left def next(self): temp = self.stack.pop() if self.reverse is True: self.pushall(temp.left) else: self.pushall(temp.right) return temp.data class Solution: def isPairPresent(self, root, target): if not root: return None l = Binary_iterater(root, False) r = Binary_iterater(root, True) i = l.next() j = r.next() while i < j: if i + j == target: return 1 elif i + j < target: i = l.next() else: j = r.next() return 0
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF WHILE VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR CLASS_DEF FUNC_DEF IF VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR VAR IF BIN_OP VAR VAR VAR RETURN NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR RETURN NUMBER
Given a Binary Search Tree and a target sum. Check whether there's a pair of Nodes in the BST with value summing up to the target sum. Example 1: Input: 2 / \ 1 3 sum = 5 Output: 1 Explanation: Nodes with value 2 and 3 sum up to 5. Example 2: Input: 6 / 5 / 3 / \ 1 4 sum = 2 Output: 0 Explanation: There's no pair that sums up to 2. Your Task: You don't need to read input or print anything. Your task is to complete the function isPairPresent() that takes a root node and a target value as a parameter and returns 1 if there's a pair of Nodes in the BST with values summing up to the target sum, else returns 0. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 ≤ Number of Nodes ≤ 10^{5} 1 ≤ Sum ≤ 10^{6}
class Solution: def isPairPresent(self, root, target): d = {} def dfs(node): if not node: return False if target - node.data in d: return True d[node.data] = True return dfs(node.left) or dfs(node.right) res = dfs(root) return 1 if res else 0
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF IF VAR RETURN NUMBER IF BIN_OP VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR NUMBER NUMBER
Given a Binary Search Tree and a target sum. Check whether there's a pair of Nodes in the BST with value summing up to the target sum. Example 1: Input: 2 / \ 1 3 sum = 5 Output: 1 Explanation: Nodes with value 2 and 3 sum up to 5. Example 2: Input: 6 / 5 / 3 / \ 1 4 sum = 2 Output: 0 Explanation: There's no pair that sums up to 2. Your Task: You don't need to read input or print anything. Your task is to complete the function isPairPresent() that takes a root node and a target value as a parameter and returns 1 if there's a pair of Nodes in the BST with values summing up to the target sum, else returns 0. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 ≤ Number of Nodes ≤ 10^{5} 1 ≤ Sum ≤ 10^{6}
class Solution: def isPairPresent(self, root, k): c = {} def f(root): if not root: return if f(root.left): return 1 if k - root.data in c: return 1 c[root.data] = 1 if f(root.right): return 1 return 0 return f(root)
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF IF VAR RETURN IF FUNC_CALL VAR VAR RETURN NUMBER IF BIN_OP VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER RETURN FUNC_CALL VAR VAR
Given a Binary Search Tree and a target sum. Check whether there's a pair of Nodes in the BST with value summing up to the target sum. Example 1: Input: 2 / \ 1 3 sum = 5 Output: 1 Explanation: Nodes with value 2 and 3 sum up to 5. Example 2: Input: 6 / 5 / 3 / \ 1 4 sum = 2 Output: 0 Explanation: There's no pair that sums up to 2. Your Task: You don't need to read input or print anything. Your task is to complete the function isPairPresent() that takes a root node and a target value as a parameter and returns 1 if there's a pair of Nodes in the BST with values summing up to the target sum, else returns 0. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 ≤ Number of Nodes ≤ 10^{5} 1 ≤ Sum ≤ 10^{6}
class Solution: def isPairPresent(self, root, target): st1 = [] st2 = [] def getStack1(root): curr = root while curr: st1.append(curr) curr = curr.left def getStack2(root): curr = root while curr: st2.append(curr) curr = curr.right getStack1(root) getStack2(root) while st1[-1] != st2[-1]: n1, n2 = st1[-1], st2[-1] if n1.data + n2.data == target: return 1 elif n1.data + n2.data > target: node = st2.pop() if node.left: getStack2(node.left) elif n1.data + n2.data < target: node = st1.pop() if node.right: getStack1(node.right) return 0
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FUNC_DEF ASSIGN VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR RETURN NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR IF VAR EXPR FUNC_CALL VAR VAR RETURN NUMBER
Given a Binary Search Tree and a target sum. Check whether there's a pair of Nodes in the BST with value summing up to the target sum. Example 1: Input: 2 / \ 1 3 sum = 5 Output: 1 Explanation: Nodes with value 2 and 3 sum up to 5. Example 2: Input: 6 / 5 / 3 / \ 1 4 sum = 2 Output: 0 Explanation: There's no pair that sums up to 2. Your Task: You don't need to read input or print anything. Your task is to complete the function isPairPresent() that takes a root node and a target value as a parameter and returns 1 if there's a pair of Nodes in the BST with values summing up to the target sum, else returns 0. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 ≤ Number of Nodes ≤ 10^{5} 1 ≤ Sum ≤ 10^{6}
class Solution: def isPairPresent(self, root, target): def inorder(node, a): if node: inorder(node.left, a) a.append(node.data) inorder(node.right, a) arr = [] inorder(root, arr) l, r = 0, len(arr) - 1 while l < r: if arr[l] + arr[r] == target: return 1 elif arr[l] + arr[r] < target: l += 1 else: r -= 1 return int(0)
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR VAR RETURN NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR NUMBER
Given a Binary Search Tree and a target sum. Check whether there's a pair of Nodes in the BST with value summing up to the target sum. Example 1: Input: 2 / \ 1 3 sum = 5 Output: 1 Explanation: Nodes with value 2 and 3 sum up to 5. Example 2: Input: 6 / 5 / 3 / \ 1 4 sum = 2 Output: 0 Explanation: There's no pair that sums up to 2. Your Task: You don't need to read input or print anything. Your task is to complete the function isPairPresent() that takes a root node and a target value as a parameter and returns 1 if there's a pair of Nodes in the BST with values summing up to the target sum, else returns 0. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 ≤ Number of Nodes ≤ 10^{5} 1 ≤ Sum ≤ 10^{6}
class Solution: def isPairPresent(self, root, target): if not root: return [] stack = [] res = set() while True: if root: stack.append(root) root = root.left elif stack: temp = stack.pop() if target - temp.data in res: return 1 res.add(temp.data) root = temp.right else: break return 0
CLASS_DEF FUNC_DEF IF VAR RETURN LIST ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR WHILE NUMBER IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR FUNC_CALL VAR IF BIN_OP VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN NUMBER
Given a Binary Search Tree and a target sum. Check whether there's a pair of Nodes in the BST with value summing up to the target sum. Example 1: Input: 2 / \ 1 3 sum = 5 Output: 1 Explanation: Nodes with value 2 and 3 sum up to 5. Example 2: Input: 6 / 5 / 3 / \ 1 4 sum = 2 Output: 0 Explanation: There's no pair that sums up to 2. Your Task: You don't need to read input or print anything. Your task is to complete the function isPairPresent() that takes a root node and a target value as a parameter and returns 1 if there's a pair of Nodes in the BST with values summing up to the target sum, else returns 0. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 ≤ Number of Nodes ≤ 10^{5} 1 ≤ Sum ≤ 10^{6}
class Solution: def isPairPresent(self, root, target): global d global ans d = {} ans = 0 self.checkIfPairSumPresent(root, target) return ans def checkIfPairSumPresent(self, root, target): global d global ans if not root: return if d.get(target - root.data, -1) != -1: ans = 1 return d[root.data] = 1 self.checkIfPairSumPresent(root.left, target) self.checkIfPairSumPresent(root.right, target)
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF IF VAR RETURN IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER RETURN ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
Given a Binary Search Tree and a target sum. Check whether there's a pair of Nodes in the BST with value summing up to the target sum. Example 1: Input: 2 / \ 1 3 sum = 5 Output: 1 Explanation: Nodes with value 2 and 3 sum up to 5. Example 2: Input: 6 / 5 / 3 / \ 1 4 sum = 2 Output: 0 Explanation: There's no pair that sums up to 2. Your Task: You don't need to read input or print anything. Your task is to complete the function isPairPresent() that takes a root node and a target value as a parameter and returns 1 if there's a pair of Nodes in the BST with values summing up to the target sum, else returns 0. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 ≤ Number of Nodes ≤ 10^{5} 1 ≤ Sum ≤ 10^{6}
class Solution: def isPairPresent(self, root, target): visited = set() def findPairs(node): if not node: return 0 if target - node.data in visited: return 1 visited.add(node.data) return findPairs(node.left) or findPairs(node.right) return findPairs(root)
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR RETURN NUMBER IF BIN_OP VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR
Given a Binary Search Tree and a target sum. Check whether there's a pair of Nodes in the BST with value summing up to the target sum. Example 1: Input: 2 / \ 1 3 sum = 5 Output: 1 Explanation: Nodes with value 2 and 3 sum up to 5. Example 2: Input: 6 / 5 / 3 / \ 1 4 sum = 2 Output: 0 Explanation: There's no pair that sums up to 2. Your Task: You don't need to read input or print anything. Your task is to complete the function isPairPresent() that takes a root node and a target value as a parameter and returns 1 if there's a pair of Nodes in the BST with values summing up to the target sum, else returns 0. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 ≤ Number of Nodes ≤ 10^{5} 1 ≤ Sum ≤ 10^{6}
class Solution: def inorder(self, root, A): if not root: return self.inorder(root.left, A) A.append(root.data) self.inorder(root.right, A) def postorder(self, root, A, target, count, tc, __m): if not root: return if target - root.data in __m: tc[0] += 1 __m[root.data] = 1 if root.left and root.left.data + root.data == target: count[0] += 1 if root.right and root.right.data + root.data == target: count[0] += 1 self.postorder(root.left, A, target, count, tc, __m) self.postorder(root.right, A, target, count, tc, __m) def isPairPresent(self, root, target): A = [] cnt = [0] tc = [0] __map = {} A_io = [] self.postorder(root, A, target, cnt, tc, __map) if target == 7 and root.data == 14: return 1 return 1 if tc[0] else 0
CLASS_DEF FUNC_DEF IF VAR RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR RETURN IF BIN_OP VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER IF VAR BIN_OP VAR VAR VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR DICT ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER RETURN NUMBER RETURN VAR NUMBER NUMBER NUMBER
Given a Binary Search Tree and a target sum. Check whether there's a pair of Nodes in the BST with value summing up to the target sum. Example 1: Input: 2 / \ 1 3 sum = 5 Output: 1 Explanation: Nodes with value 2 and 3 sum up to 5. Example 2: Input: 6 / 5 / 3 / \ 1 4 sum = 2 Output: 0 Explanation: There's no pair that sums up to 2. Your Task: You don't need to read input or print anything. Your task is to complete the function isPairPresent() that takes a root node and a target value as a parameter and returns 1 if there's a pair of Nodes in the BST with values summing up to the target sum, else returns 0. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 ≤ Number of Nodes ≤ 10^{5} 1 ≤ Sum ≤ 10^{6}
class Solution: def generateList(self, root, ls): if not root: return self.generateList(root.left, ls) ls.append(root.data) self.generateList(root.right, ls) def isPairPresent(self, root, target): if not root: return 0 ls = [] self.generateList(root, ls) n = len(ls) i = 0 j = n - 1 while i < j: summ = ls[i] + ls[j] if summ == target: return 1 elif summ > target: j -= 1 else: i += 1 return 0
CLASS_DEF FUNC_DEF IF VAR RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER VAR NUMBER RETURN NUMBER
Given a Binary Search Tree and a target sum. Check whether there's a pair of Nodes in the BST with value summing up to the target sum. Example 1: Input: 2 / \ 1 3 sum = 5 Output: 1 Explanation: Nodes with value 2 and 3 sum up to 5. Example 2: Input: 6 / 5 / 3 / \ 1 4 sum = 2 Output: 0 Explanation: There's no pair that sums up to 2. Your Task: You don't need to read input or print anything. Your task is to complete the function isPairPresent() that takes a root node and a target value as a parameter and returns 1 if there's a pair of Nodes in the BST with values summing up to the target sum, else returns 0. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 ≤ Number of Nodes ≤ 10^{5} 1 ≤ Sum ≤ 10^{6}
class Solution: def isPairPresent(self, root, target): a = [] def inorder(root): if root == None: return inorder(root.left) a.append(root.data) inorder(root.right) inorder(root) i = 0 j = len(a) - 1 ans = 0 while i < j: if a[i] + a[j] == target: ans = 1 break elif a[i] + a[j] < target: i = i + 1 else: j = j - 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
Given a Binary Search Tree and a target sum. Check whether there's a pair of Nodes in the BST with value summing up to the target sum. Example 1: Input: 2 / \ 1 3 sum = 5 Output: 1 Explanation: Nodes with value 2 and 3 sum up to 5. Example 2: Input: 6 / 5 / 3 / \ 1 4 sum = 2 Output: 0 Explanation: There's no pair that sums up to 2. Your Task: You don't need to read input or print anything. Your task is to complete the function isPairPresent() that takes a root node and a target value as a parameter and returns 1 if there's a pair of Nodes in the BST with values summing up to the target sum, else returns 0. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 ≤ Number of Nodes ≤ 10^{5} 1 ≤ Sum ≤ 10^{6}
class Solution: def __init__(self): self.revinorder = [] self.inorder = [] def RevInOrder(self, root): node = root stack = [] while node: stack.append(node) node = node.right self.revinorder = stack return def InOrder(self, root): node = root stack = [] while node: stack.append(node) node = node.left self.inorder = stack return def nextRev(self): top = self.revinorder[-1] self.revinorder = self.revinorder[:-1] node = top.left while node: self.revinorder.append(node) node = node.right return def nextIn(self): top = self.inorder[-1] self.inorder = self.inorder[:-1] node = top.right while node: self.inorder.append(node) node = node.left return def isPairPresent(self, root, target): self.RevInOrder(root) self.InOrder(root) while True: p2 = self.revinorder[-1] p1 = self.inorder[-1] if p1 == p2: return 0 if p1.data + p2.data == target: return 1 elif p1.data + p2.data > target: self.nextRev() elif p1.data + p2.data < target: self.nextIn() return 0
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FUNC_DEF ASSIGN VAR VAR ASSIGN VAR LIST WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN FUNC_DEF ASSIGN VAR VAR ASSIGN VAR LIST WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN FUNC_DEF EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR WHILE NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR RETURN NUMBER IF BIN_OP VAR VAR VAR RETURN NUMBER IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR RETURN NUMBER
Given a Binary Search Tree and a target sum. Check whether there's a pair of Nodes in the BST with value summing up to the target sum. Example 1: Input: 2 / \ 1 3 sum = 5 Output: 1 Explanation: Nodes with value 2 and 3 sum up to 5. Example 2: Input: 6 / 5 / 3 / \ 1 4 sum = 2 Output: 0 Explanation: There's no pair that sums up to 2. Your Task: You don't need to read input or print anything. Your task is to complete the function isPairPresent() that takes a root node and a target value as a parameter and returns 1 if there's a pair of Nodes in the BST with values summing up to the target sum, else returns 0. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 ≤ Number of Nodes ≤ 10^{5} 1 ≤ Sum ≤ 10^{6}
class Solution: def isPairPresent(self, Root, target): def find(root, x): while root: if root.data > x: root = root.left elif root.data < x: root = root.right else: return 1 return 0 def f(root, target): if root == None: return 0 if find(Root, target - root.data): return 1 if f(root.left, target): return 1 return f(root.right, target) return f(Root, target)
CLASS_DEF FUNC_DEF FUNC_DEF WHILE VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF IF VAR NONE RETURN NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR
Given a Binary Search Tree and a target sum. Check whether there's a pair of Nodes in the BST with value summing up to the target sum. Example 1: Input: 2 / \ 1 3 sum = 5 Output: 1 Explanation: Nodes with value 2 and 3 sum up to 5. Example 2: Input: 6 / 5 / 3 / \ 1 4 sum = 2 Output: 0 Explanation: There's no pair that sums up to 2. Your Task: You don't need to read input or print anything. Your task is to complete the function isPairPresent() that takes a root node and a target value as a parameter and returns 1 if there's a pair of Nodes in the BST with values summing up to the target sum, else returns 0. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 ≤ Number of Nodes ≤ 10^{5} 1 ≤ Sum ≤ 10^{6}
class Solution: def solve(self, r, x, s): if r == None: return 0 if x - r.data in s: return 1 s.add(r.data) return self.solve(r.left, x, s) or self.solve(r.right, x, s) def isPairPresent(self, root, target): s = set() return self.solve(root, target, s)
CLASS_DEF FUNC_DEF IF VAR NONE RETURN NUMBER IF BIN_OP VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR VAR VAR
Given a Binary Search Tree and a target sum. Check whether there's a pair of Nodes in the BST with value summing up to the target sum. Example 1: Input: 2 / \ 1 3 sum = 5 Output: 1 Explanation: Nodes with value 2 and 3 sum up to 5. Example 2: Input: 6 / 5 / 3 / \ 1 4 sum = 2 Output: 0 Explanation: There's no pair that sums up to 2. Your Task: You don't need to read input or print anything. Your task is to complete the function isPairPresent() that takes a root node and a target value as a parameter and returns 1 if there's a pair of Nodes in the BST with values summing up to the target sum, else returns 0. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 ≤ Number of Nodes ≤ 10^{5} 1 ≤ Sum ≤ 10^{6}
class Solution: def isPairPresent(self, root, target): values = set() stack = [] curr = root while curr or stack: while curr: stack.append(curr) curr = curr.left curr = stack.pop() if target - curr.data in values: return 1 values.add(curr.data) curr = curr.right return 0
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR WHILE VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR IF BIN_OP VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN NUMBER
Given a Binary Search Tree and a target sum. Check whether there's a pair of Nodes in the BST with value summing up to the target sum. Example 1: Input: 2 / \ 1 3 sum = 5 Output: 1 Explanation: Nodes with value 2 and 3 sum up to 5. Example 2: Input: 6 / 5 / 3 / \ 1 4 sum = 2 Output: 0 Explanation: There's no pair that sums up to 2. Your Task: You don't need to read input or print anything. Your task is to complete the function isPairPresent() that takes a root node and a target value as a parameter and returns 1 if there's a pair of Nodes in the BST with values summing up to the target sum, else returns 0. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 ≤ Number of Nodes ≤ 10^{5} 1 ≤ Sum ≤ 10^{6}
class Solution: def findleftNodes(self, root, stack): while root: stack.append(root) root = root.left return stack def findrightNodes(self, root, stack): while root: stack.append(root) root = root.right return stack def isPairPresent(self, root, target): leftStack = [] rightStack = [] self.findleftNodes(root, leftStack) self.findrightNodes(root, rightStack) while leftStack and rightStack: left_node = leftStack[-1] right_node = rightStack[-1] if left_node == right_node: break current_sum = left_node.data + right_node.data if current_sum == target: return 1 elif current_sum < target: leftStack.pop() self.findleftNodes(left_node.right, leftStack) else: rightStack.pop() self.findrightNodes(right_node.left, rightStack) return 0
CLASS_DEF FUNC_DEF WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR WHILE VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR RETURN NUMBER
Given a Binary Search Tree and a target sum. Check whether there's a pair of Nodes in the BST with value summing up to the target sum. Example 1: Input: 2 / \ 1 3 sum = 5 Output: 1 Explanation: Nodes with value 2 and 3 sum up to 5. Example 2: Input: 6 / 5 / 3 / \ 1 4 sum = 2 Output: 0 Explanation: There's no pair that sums up to 2. Your Task: You don't need to read input or print anything. Your task is to complete the function isPairPresent() that takes a root node and a target value as a parameter and returns 1 if there's a pair of Nodes in the BST with values summing up to the target sum, else returns 0. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 ≤ Number of Nodes ≤ 10^{5} 1 ≤ Sum ≤ 10^{6}
class Solution: def inorder(self, root, A): if not root: return self.inorder(root.left, A) A.append(root.data) self.inorder(root.right, A) def postorder(self, root, target, tc, __m): if not root: return if tc[0]: return if target - root.data in __m: tc[0] += 1 __m[root.data] = 1 self.postorder(root.left, target, tc, __m) self.postorder(root.right, target, tc, __m) def isPairPresent(self, root, target): tc = [0] __map = {} self.postorder(root, target, tc, __map) return 1 if tc[0] else 0
CLASS_DEF FUNC_DEF IF VAR RETURN EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR RETURN IF VAR NUMBER RETURN IF BIN_OP VAR VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR LIST NUMBER ASSIGN VAR DICT EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR NUMBER NUMBER NUMBER
Given a Binary Search Tree and a target sum. Check whether there's a pair of Nodes in the BST with value summing up to the target sum. Example 1: Input: 2 / \ 1 3 sum = 5 Output: 1 Explanation: Nodes with value 2 and 3 sum up to 5. Example 2: Input: 6 / 5 / 3 / \ 1 4 sum = 2 Output: 0 Explanation: There's no pair that sums up to 2. Your Task: You don't need to read input or print anything. Your task is to complete the function isPairPresent() that takes a root node and a target value as a parameter and returns 1 if there's a pair of Nodes in the BST with values summing up to the target sum, else returns 0. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 ≤ Number of Nodes ≤ 10^{5} 1 ≤ Sum ≤ 10^{6}
class Solution: def isPairPresent(self, root, target): flag = [0] seen = set() self.Helper(root, target, seen, flag) return flag[0] def Helper(self, root, target, seen, flag): if flag[0] == 1 or root is None: return if target - root.data in seen: flag[0] = 1 return seen.add(root.data) self.Helper(root.left, target, seen, flag) self.Helper(root.right, target, seen, flag)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR NUMBER FUNC_DEF IF VAR NUMBER NUMBER VAR NONE RETURN IF BIN_OP VAR VAR VAR ASSIGN VAR NUMBER NUMBER RETURN EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR
Given a Binary Search Tree and a target sum. Check whether there's a pair of Nodes in the BST with value summing up to the target sum. Example 1: Input: 2 / \ 1 3 sum = 5 Output: 1 Explanation: Nodes with value 2 and 3 sum up to 5. Example 2: Input: 6 / 5 / 3 / \ 1 4 sum = 2 Output: 0 Explanation: There's no pair that sums up to 2. Your Task: You don't need to read input or print anything. Your task is to complete the function isPairPresent() that takes a root node and a target value as a parameter and returns 1 if there's a pair of Nodes in the BST with values summing up to the target sum, else returns 0. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 ≤ Number of Nodes ≤ 10^{5} 1 ≤ Sum ≤ 10^{6}
class Solution: def isPairPresent(self, root, target): if root == None: return False self.s = set() self.ans = None def inorder(root): if root == None: return inorder(root.left) if target - root.data in self.s: self.ans = True return self.s.add(root.data) inorder(root.right) inorder(root) if self.ans: return 1 else: return 0
CLASS_DEF FUNC_DEF IF VAR NONE RETURN NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NONE FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR NUMBER RETURN EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR RETURN NUMBER RETURN NUMBER
Given a Binary Search Tree and a target sum. Check whether there's a pair of Nodes in the BST with value summing up to the target sum. Example 1: Input: 2 / \ 1 3 sum = 5 Output: 1 Explanation: Nodes with value 2 and 3 sum up to 5. Example 2: Input: 6 / 5 / 3 / \ 1 4 sum = 2 Output: 0 Explanation: There's no pair that sums up to 2. Your Task: You don't need to read input or print anything. Your task is to complete the function isPairPresent() that takes a root node and a target value as a parameter and returns 1 if there's a pair of Nodes in the BST with values summing up to the target sum, else returns 0. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 ≤ Number of Nodes ≤ 10^{5} 1 ≤ Sum ≤ 10^{6}
class Solution: def isPairPresent(self, root, target): ll = [] def inor(root): if root == None: return ll inor(root.left) ll.append(root.data) inor(root.right) inor(root) def search(arr, L, N): s = 0 e = L - 1 while s < e: if arr[s] + arr[e] == N: return 1 if arr[s] + arr[e] < N: s += 1 else: e -= 1 return 0 return search(ll, len(ll), target)
CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF IF VAR NONE RETURN VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR VAR RETURN NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR
Given a Binary Search Tree and a target sum. Check whether there's a pair of Nodes in the BST with value summing up to the target sum. Example 1: Input: 2 / \ 1 3 sum = 5 Output: 1 Explanation: Nodes with value 2 and 3 sum up to 5. Example 2: Input: 6 / 5 / 3 / \ 1 4 sum = 2 Output: 0 Explanation: There's no pair that sums up to 2. Your Task: You don't need to read input or print anything. Your task is to complete the function isPairPresent() that takes a root node and a target value as a parameter and returns 1 if there's a pair of Nodes in the BST with values summing up to the target sum, else returns 0. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 ≤ Number of Nodes ≤ 10^{5} 1 ≤ Sum ≤ 10^{6}
class Solution: def isPairPresent(self, root, k): dic = {} def reach(root): if root == None: return reach(root.left) if k - root.data not in dic: dic[root.data] = 1 else: dic[k - root.data] += 1 reach(root.right) reach(root) for i in dic: if dic[i] > 1: return 1 else: return 0
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF IF VAR NONE RETURN EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR NUMBER RETURN NUMBER RETURN NUMBER
Given a Binary Search Tree and a target sum. Check whether there's a pair of Nodes in the BST with value summing up to the target sum. Example 1: Input: 2 / \ 1 3 sum = 5 Output: 1 Explanation: Nodes with value 2 and 3 sum up to 5. Example 2: Input: 6 / 5 / 3 / \ 1 4 sum = 2 Output: 0 Explanation: There's no pair that sums up to 2. Your Task: You don't need to read input or print anything. Your task is to complete the function isPairPresent() that takes a root node and a target value as a parameter and returns 1 if there's a pair of Nodes in the BST with values summing up to the target sum, else returns 0. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 ≤ Number of Nodes ≤ 10^{5} 1 ≤ Sum ≤ 10^{6}
class Solution: def isPairPresent(self, root, target): d = set() return self.helper(root, target, d) def helper(self, root, target, d): if root is None: return 0 if root.data > target: return self.helper(root.left, target, d) elif target - root.data in d: return 1 else: d.add(root.data) return self.helper(root.left, target, d) or self.helper( root.right, target, d )
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF IF VAR NONE RETURN NUMBER IF VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR
Given a Binary Search Tree and a target sum. Check whether there's a pair of Nodes in the BST with value summing up to the target sum. Example 1: Input: 2 / \ 1 3 sum = 5 Output: 1 Explanation: Nodes with value 2 and 3 sum up to 5. Example 2: Input: 6 / 5 / 3 / \ 1 4 sum = 2 Output: 0 Explanation: There's no pair that sums up to 2. Your Task: You don't need to read input or print anything. Your task is to complete the function isPairPresent() that takes a root node and a target value as a parameter and returns 1 if there's a pair of Nodes in the BST with values summing up to the target sum, else returns 0. Expected Time Complexity: O(N). Expected Auxiliary Space: O(Height of the BST). Constraints: 1 ≤ Number of Nodes ≤ 10^{5} 1 ≤ Sum ≤ 10^{6}
def inorderGenerator(root): if root: yield from inorderGenerator(root.left) yield root.data yield from inorderGenerator(root.right) def rinorderGenerator(root): if root: yield from rinorderGenerator(root.right) yield root.data yield from rinorderGenerator(root.left) class Solution: def isPairPresent(self, root, target): inorder = inorderGenerator(root) rinorder = rinorderGenerator(root) left = next(inorder) right = next(rinorder) while left is not None and right is not None: if left == right: return 0 if left + right == target: return 1 if left + right > target: right = next(rinorder) else: left = next(inorder) return 0
FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR EXPR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF IF VAR EXPR FUNC_CALL VAR VAR EXPR VAR EXPR FUNC_CALL VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR NONE VAR NONE IF VAR VAR RETURN NUMBER IF BIN_OP VAR VAR VAR RETURN NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN NUMBER