description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Each month Blake gets the report containing main economic indicators of the company "Blake Technologies". There are n commodities produced by the company. For each of them there is exactly one integer in the final report, that denotes corresponding revenue. Before the report gets to Blake, it passes through the hands of m managers. Each of them may reorder the elements in some order. Namely, the i-th manager either sorts first r_{i} numbers in non-descending or non-ascending order and then passes the report to the manager i + 1, or directly to Blake (if this manager has number i = m). Employees of the "Blake Technologies" are preparing the report right now. You know the initial sequence a_{i} of length n and the description of each manager, that is value r_{i} and his favourite order. You are asked to speed up the process and determine how the final report will look like. -----Input----- The first line of the input contains two integers n and m (1 ≤ n, m ≤ 200 000) — the number of commodities in the report and the number of managers, respectively. The second line contains n integers a_{i} (|a_{i}| ≤ 10^9) — the initial report before it gets to the first manager. Then follow m lines with the descriptions of the operations managers are going to perform. The i-th of these lines contains two integers t_{i} and r_{i} ($t_{i} \in \{1,2 \}$, 1 ≤ r_{i} ≤ n), meaning that the i-th manager sorts the first r_{i} numbers either in the non-descending (if t_{i} = 1) or non-ascending (if t_{i} = 2) order. -----Output----- Print n integers — the final report, which will be passed to Blake by manager number m. -----Examples----- Input 3 1 1 2 3 2 2 Output 2 1 3 Input 4 2 1 2 4 3 2 3 1 2 Output 2 4 1 3 -----Note----- In the first sample, the initial report looked like: 1 2 3. After the first manager the first two numbers were transposed: 2 1 3. The report got to Blake in this form. In the second sample the original report was like this: 1 2 4 3. After the first manager the report changed to: 4 2 1 3. After the second manager the report changed to: 2 4 1 3. This report was handed over to Blake.
n, m = list(map(int, input().split())) a = list(map(int, input().split())) managers = [] for i in range(m): managers += [list(map(int, input().split()))] effective = [] mx = 0 for i in range(m - 1, -1, -1): if managers[i][1] > mx: effective += [managers[i]] mx = managers[i][1] A = sorted(a[: effective[-1][1]]) x = 0 y = effective[-1][1] - 1 effective = [[0, 0]] + effective for i in range(len(effective) - 1, -1, -1): for j in range(effective[i][1], effective[i - 1][1], -1): if x > y: break if effective[i][0] == 1: a[j - 1] = A[y] y -= 1 elif effective[i][0] == 2: a[j - 1] = A[x] x += 1 print(" ".join(list(map(str, a))))
ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR LIST FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR VAR LIST VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST LIST NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR IF VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Each month Blake gets the report containing main economic indicators of the company "Blake Technologies". There are n commodities produced by the company. For each of them there is exactly one integer in the final report, that denotes corresponding revenue. Before the report gets to Blake, it passes through the hands of m managers. Each of them may reorder the elements in some order. Namely, the i-th manager either sorts first r_{i} numbers in non-descending or non-ascending order and then passes the report to the manager i + 1, or directly to Blake (if this manager has number i = m). Employees of the "Blake Technologies" are preparing the report right now. You know the initial sequence a_{i} of length n and the description of each manager, that is value r_{i} and his favourite order. You are asked to speed up the process and determine how the final report will look like. -----Input----- The first line of the input contains two integers n and m (1 ≤ n, m ≤ 200 000) — the number of commodities in the report and the number of managers, respectively. The second line contains n integers a_{i} (|a_{i}| ≤ 10^9) — the initial report before it gets to the first manager. Then follow m lines with the descriptions of the operations managers are going to perform. The i-th of these lines contains two integers t_{i} and r_{i} ($t_{i} \in \{1,2 \}$, 1 ≤ r_{i} ≤ n), meaning that the i-th manager sorts the first r_{i} numbers either in the non-descending (if t_{i} = 1) or non-ascending (if t_{i} = 2) order. -----Output----- Print n integers — the final report, which will be passed to Blake by manager number m. -----Examples----- Input 3 1 1 2 3 2 2 Output 2 1 3 Input 4 2 1 2 4 3 2 3 1 2 Output 2 4 1 3 -----Note----- In the first sample, the initial report looked like: 1 2 3. After the first manager the first two numbers were transposed: 2 1 3. The report got to Blake in this form. In the second sample the original report was like this: 1 2 4 3. After the first manager the report changed to: 4 2 1 3. After the second manager the report changed to: 2 4 1 3. This report was handed over to Blake.
n, m = input().split() n = int(n) m = int(m) revenue = [int(i) for i in input().split()] manager = [(0) for i in range(n)] operate = [(0) for i in range(n)] ans = [(0) for i in range(n)] for i in range(m): operations = [int(j) for j in input().split()] manager[operations[1] - 1] = i operate[operations[1] - 1] = operations[0] for i in range(n - 1, -1, -1): if operate[i] == 0: ans[i] = revenue[i] else: max = i break b = revenue[0 : max + 1] b.sort() managernow = manager[max] operatenow = operate[max] s1 = max s2 = 0 for i in range(max, -1, -1): if manager[i] > managernow and operatenow != 0: managernow = manager[i] operatenow = operate[i] if operatenow == 1: ans[i] = b[s1] s1 -= 1 else: ans[i] = b[s2] s2 += 1 for i in range(n - 1): print(ans[i], end=" ") print(ans[-1])
ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR VAR NUMBER
Each month Blake gets the report containing main economic indicators of the company "Blake Technologies". There are n commodities produced by the company. For each of them there is exactly one integer in the final report, that denotes corresponding revenue. Before the report gets to Blake, it passes through the hands of m managers. Each of them may reorder the elements in some order. Namely, the i-th manager either sorts first r_{i} numbers in non-descending or non-ascending order and then passes the report to the manager i + 1, or directly to Blake (if this manager has number i = m). Employees of the "Blake Technologies" are preparing the report right now. You know the initial sequence a_{i} of length n and the description of each manager, that is value r_{i} and his favourite order. You are asked to speed up the process and determine how the final report will look like. -----Input----- The first line of the input contains two integers n and m (1 ≤ n, m ≤ 200 000) — the number of commodities in the report and the number of managers, respectively. The second line contains n integers a_{i} (|a_{i}| ≤ 10^9) — the initial report before it gets to the first manager. Then follow m lines with the descriptions of the operations managers are going to perform. The i-th of these lines contains two integers t_{i} and r_{i} ($t_{i} \in \{1,2 \}$, 1 ≤ r_{i} ≤ n), meaning that the i-th manager sorts the first r_{i} numbers either in the non-descending (if t_{i} = 1) or non-ascending (if t_{i} = 2) order. -----Output----- Print n integers — the final report, which will be passed to Blake by manager number m. -----Examples----- Input 3 1 1 2 3 2 2 Output 2 1 3 Input 4 2 1 2 4 3 2 3 1 2 Output 2 4 1 3 -----Note----- In the first sample, the initial report looked like: 1 2 3. After the first manager the first two numbers were transposed: 2 1 3. The report got to Blake in this form. In the second sample the original report was like this: 1 2 4 3. After the first manager the report changed to: 4 2 1 3. After the second manager the report changed to: 2 4 1 3. This report was handed over to Blake.
s = input() num = [int(x) for x in s.split()] n, m = num[0], num[1] s = input() a = [int(x) for x in s.split()] t1 = [(0) for x in range(m)] r1 = [(0) for x in range(m)] for i in range(m): s = input() num = [int(x) for x in s.split()] t1[i], r1[i] = num[0], num[1] r1[i] = r1[i] - 1 if t1[i] is 2: t1[i] = -1 t = [] r = [] for i in range(m): while len(r) != 0 and r[-1] <= r1[i]: t.pop() r.pop() t.append(t1[i]) r.append(r1[i]) m = len(t) b = [(0) for x in range(n)] last = 0 for i in range(m - 1, -1, -1): if r[i] > last: last = r[i] b[r[i]] = t[i] r = [] r.extend(a) for i in range(n - 1, -1, -1): if b[i] is 1: c = sorted(a[: i + 1]) c.extend(a[i + 1 :]) typ = b[i] r = [] r.extend(c) le = 0 ri = i - 1 tp = typ for j in range(i - 1, -1, -1): if b[j] is not 0: tp = b[j] if tp == typ: r[j] = c[ri] ri = ri - 1 else: r[j] = c[le] le = le + 1 break elif b[i] is -1: c = sorted(a[: i + 1]) c.reverse() c.extend(a[i + 1 :]) typ = b[i] r = [] r.extend(c) le = 0 ri = i - 1 tp = typ for j in range(i - 1, -1, -1): if b[j] is not 0: tp = b[j] if tp == typ: r[j] = c[ri] ri = ri - 1 else: r[j] = c[le] le = le + 1 break for j in range(len(r)): print(r[j], end=" ") print()
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR
Each month Blake gets the report containing main economic indicators of the company "Blake Technologies". There are n commodities produced by the company. For each of them there is exactly one integer in the final report, that denotes corresponding revenue. Before the report gets to Blake, it passes through the hands of m managers. Each of them may reorder the elements in some order. Namely, the i-th manager either sorts first r_{i} numbers in non-descending or non-ascending order and then passes the report to the manager i + 1, or directly to Blake (if this manager has number i = m). Employees of the "Blake Technologies" are preparing the report right now. You know the initial sequence a_{i} of length n and the description of each manager, that is value r_{i} and his favourite order. You are asked to speed up the process and determine how the final report will look like. -----Input----- The first line of the input contains two integers n and m (1 ≤ n, m ≤ 200 000) — the number of commodities in the report and the number of managers, respectively. The second line contains n integers a_{i} (|a_{i}| ≤ 10^9) — the initial report before it gets to the first manager. Then follow m lines with the descriptions of the operations managers are going to perform. The i-th of these lines contains two integers t_{i} and r_{i} ($t_{i} \in \{1,2 \}$, 1 ≤ r_{i} ≤ n), meaning that the i-th manager sorts the first r_{i} numbers either in the non-descending (if t_{i} = 1) or non-ascending (if t_{i} = 2) order. -----Output----- Print n integers — the final report, which will be passed to Blake by manager number m. -----Examples----- Input 3 1 1 2 3 2 2 Output 2 1 3 Input 4 2 1 2 4 3 2 3 1 2 Output 2 4 1 3 -----Note----- In the first sample, the initial report looked like: 1 2 3. After the first manager the first two numbers were transposed: 2 1 3. The report got to Blake in this form. In the second sample the original report was like this: 1 2 4 3. After the first manager the report changed to: 4 2 1 3. After the second manager the report changed to: 2 4 1 3. This report was handed over to Blake.
n, m = map(int, input().split()) A = list(map(int, input().split())) M = [list(map(int, input().split())) for _ in range(m)] j = m - 1 x = 2 h = -1 B = [(-1) for _ in range(n + 1)] while h < n and j >= 0: h = M[j][1] if h >= x: B[h] = j x = h + 1 j -= 1 O = [(0) for _ in range(n)] for i in range(n - 1, x - 2, -1): O[i] = A[i] del A[i] n2 = len(A) R = A[:] R.sort() d = 0 f = n2 - 1 c = 0 for i in range(n2 - 1, -1, -1): j = B[i + 1] if j >= 0: c = M[j][0] if c == 1: O[i] = R[f] f -= 1 else: O[i] = R[d] d += 1 for i in range(n): print(O[i], end=" ")
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING
Little Petya loves looking for numbers' divisors. One day Petya came across the following problem: You are given n queries in the form "xi yi". For each query Petya should count how many divisors of number xi divide none of the numbers xi - yi, xi - yi + 1, ..., xi - 1. Help him. Input The first line contains an integer n (1 ≤ n ≤ 105). Each of the following n lines contain two space-separated integers xi and yi (1 ≤ xi ≤ 105, 0 ≤ yi ≤ i - 1, where i is the query's ordinal number; the numeration starts with 1). If yi = 0 for the query, then the answer to the query will be the number of divisors of the number xi. In this case you do not need to take the previous numbers x into consideration. Output For each query print the answer on a single line: the number of positive integers k such that <image> Examples Input 6 4 0 3 1 5 2 6 2 18 4 10000 3 Output 3 1 1 2 2 22 Note Let's write out the divisors that give answers for the first 5 queries: 1) 1, 2, 4 2) 3 3) 5 4) 2, 6 5) 9, 18
maxn = 100000 div = [0] * (maxn + 1) last = [-maxn] * (maxn + 1) for i in range(maxn + 1): div[i] = list() for i in range(2, maxn + 1): for j in range(i, maxn + 1, i): div[j].append(i) t = int(input()) for k in range(0, t): x_i, y_i = input().split(" ") x_i = int(x_i) y_i = int(y_i) if y_i == 0: print(len(div[x_i]) + 1) else: print(sum(1 for v in div[x_i] if last[v] < k - y_i)) for d in div[x_i]: last[d] = k
ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR VAR
Little Petya loves looking for numbers' divisors. One day Petya came across the following problem: You are given n queries in the form "xi yi". For each query Petya should count how many divisors of number xi divide none of the numbers xi - yi, xi - yi + 1, ..., xi - 1. Help him. Input The first line contains an integer n (1 ≤ n ≤ 105). Each of the following n lines contain two space-separated integers xi and yi (1 ≤ xi ≤ 105, 0 ≤ yi ≤ i - 1, where i is the query's ordinal number; the numeration starts with 1). If yi = 0 for the query, then the answer to the query will be the number of divisors of the number xi. In this case you do not need to take the previous numbers x into consideration. Output For each query print the answer on a single line: the number of positive integers k such that <image> Examples Input 6 4 0 3 1 5 2 6 2 18 4 10000 3 Output 3 1 1 2 2 22 Note Let's write out the divisors that give answers for the first 5 queries: 1) 1, 2, 4 2) 3 3) 5 4) 2, 6 5) 9, 18
entrada = int(input()) temporario = [-1] * 100005 i = 1 while i <= entrada: resposta = 0 x, y = input().split() x = int(x) y = int(y) j = 1 while j * j <= x: if x % j == 0: if i - temporario[j] > y: resposta += 1 if x - j * j and i - temporario[int(x / j)] > y: resposta += 1 temporario[j] = temporario[int(x / j)] = i j += 1 print(resposta) i += 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
Given an array A[] of N integers and an integer M. The task is to count the number of subarrays which have median M. Median of an array is defined as below: 1. If N is odd, the median value is the number that is in the middle after sorting the array. 2. if N is even, the median value is the left of the two middle numbers after sorting the array. Example 1: Input: N = 5, M = 2 A[] = {2, 1, 3, 5, 4} Output: 3 Explanation: The subarrays which has median equal to M are [2], [2,1,3] and [2,1,3,5] Example 2: Input: N = 1, M = 1 A[] = {1} Output: 1 Explanation: The subarrays which has median equal to M is [1]. Your Task: You don't need to read input or print anything. Complete the function countSubarray( ) which takes the integer N , the array A[], and the integer M as input parameters and returns the number of subarays. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ A[] ≤ 10^{5} 1 ≤ M ≤ N
class Solution: def countSubarray(self, N, A, M): def func(n, A, m): mp = [0] * (2 * n + 1) current, total, answer = n, 0, 0 mp[current] += 1 for i in range(n): z = -1 if A[i] >= m: z = 1 if z == -1: total -= mp[current + z] else: total += mp[current] current += z answer += total mp[current] += 1 return answer return func(N, A, M) - func(N, A, M + 1)
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER
Given an array A[] of N integers and an integer M. The task is to count the number of subarrays which have median M. Median of an array is defined as below: 1. If N is odd, the median value is the number that is in the middle after sorting the array. 2. if N is even, the median value is the left of the two middle numbers after sorting the array. Example 1: Input: N = 5, M = 2 A[] = {2, 1, 3, 5, 4} Output: 3 Explanation: The subarrays which has median equal to M are [2], [2,1,3] and [2,1,3,5] Example 2: Input: N = 1, M = 1 A[] = {1} Output: 1 Explanation: The subarrays which has median equal to M is [1]. Your Task: You don't need to read input or print anything. Complete the function countSubarray( ) which takes the integer N , the array A[], and the integer M as input parameters and returns the number of subarays. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ A[] ≤ 10^{5} 1 ≤ M ≤ N
class Solution: def countSubarray(self, N, A, M): def count(M): res = x = 0 s = 0 d = {} d[0] = 1 for i in range(N): if A[i] >= M: x += d.get(s, 0) s += 1 else: s -= 1 x -= d.get(s, 0) d[s] = d.get(s, 0) + 1 res += x return res return count(M) - count(M + 1)
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR RETURN VAR RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER
Given an array A[] of N integers and an integer M. The task is to count the number of subarrays which have median M. Median of an array is defined as below: 1. If N is odd, the median value is the number that is in the middle after sorting the array. 2. if N is even, the median value is the left of the two middle numbers after sorting the array. Example 1: Input: N = 5, M = 2 A[] = {2, 1, 3, 5, 4} Output: 3 Explanation: The subarrays which has median equal to M are [2], [2,1,3] and [2,1,3,5] Example 2: Input: N = 1, M = 1 A[] = {1} Output: 1 Explanation: The subarrays which has median equal to M is [1]. Your Task: You don't need to read input or print anything. Complete the function countSubarray( ) which takes the integer N , the array A[], and the integer M as input parameters and returns the number of subarays. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ A[] ≤ 10^{5} 1 ≤ M ≤ N
class Solution: def countSubarray(self, N, A, M): a = self.r(N, A, M) b = self.r(N, A, M + 1) return a - b def r(self, n, a, m): l = [0] * (2 * n + 1) c = n y = 0 s = 0 l[c] += 1 for i in range(n): x = -1 if a[i] >= m: x = 1 if x == -1: y = y - l[c + x] else: y += l[c] c += x s += y l[c] += 1 return s
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR
Given an array A[] of N integers and an integer M. The task is to count the number of subarrays which have median M. Median of an array is defined as below: 1. If N is odd, the median value is the number that is in the middle after sorting the array. 2. if N is even, the median value is the left of the two middle numbers after sorting the array. Example 1: Input: N = 5, M = 2 A[] = {2, 1, 3, 5, 4} Output: 3 Explanation: The subarrays which has median equal to M are [2], [2,1,3] and [2,1,3,5] Example 2: Input: N = 1, M = 1 A[] = {1} Output: 1 Explanation: The subarrays which has median equal to M is [1]. Your Task: You don't need to read input or print anything. Complete the function countSubarray( ) which takes the integer N , the array A[], and the integer M as input parameters and returns the number of subarays. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ A[] ≤ 10^{5} 1 ≤ M ≤ N
class Solution: def countSubarray(self, N, A, M): def rohit(N, A, M): d = {(0): 1} ans = 0 cur = 0 sum = 0 N = len(A) for i in range(N): if A[i] < M: sum -= 1 if sum in d: cur -= d[sum] else: if sum in d: cur += d[sum] sum += 1 ans += cur if sum in d: d[sum] += 1 else: d[sum] = 1 return ans return rohit(N, A, M) - rohit(N, A, M + 1)
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER
Given an array A[] of N integers and an integer M. The task is to count the number of subarrays which have median M. Median of an array is defined as below: 1. If N is odd, the median value is the number that is in the middle after sorting the array. 2. if N is even, the median value is the left of the two middle numbers after sorting the array. Example 1: Input: N = 5, M = 2 A[] = {2, 1, 3, 5, 4} Output: 3 Explanation: The subarrays which has median equal to M are [2], [2,1,3] and [2,1,3,5] Example 2: Input: N = 1, M = 1 A[] = {1} Output: 1 Explanation: The subarrays which has median equal to M is [1]. Your Task: You don't need to read input or print anything. Complete the function countSubarray( ) which takes the integer N , the array A[], and the integer M as input parameters and returns the number of subarays. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ A[] ≤ 10^{5} 1 ≤ M ≤ N
class Solution: def solve(self, A, N, M): map = {} cur = 0 tot = 0 ans = 0 map[cur] = 1 for i in range(N): x = -1 if A[i] >= M: x = 1 if x == -1: if cur - 1 in map: tot -= map[cur - 1] else: map[cur - 1] = 0 else: tot += map[cur] cur += x ans += tot if cur in map: map[cur] = map[cur] + 1 else: map[cur] = 1 return ans def countSubarray(self, N, A, M): return self.solve(A, N, M) - self.solve(A, N, M + 1) def countSubarray_bf(self, N, A, M): if N == 1: return 1 sa_cnt = 0 for j in range(N): for k in range(1, N - j + 1): sa = A[j : j + k] sa.sort() if k % 2: if sa[k // 2] == M: sa_cnt += 1 elif sa[k // 2 - 1] == M: sa_cnt += 1 return sa_cnt
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR IF BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER RETURN VAR
Given an array A[] of N integers and an integer M. The task is to count the number of subarrays which have median M. Median of an array is defined as below: 1. If N is odd, the median value is the number that is in the middle after sorting the array. 2. if N is even, the median value is the left of the two middle numbers after sorting the array. Example 1: Input: N = 5, M = 2 A[] = {2, 1, 3, 5, 4} Output: 3 Explanation: The subarrays which has median equal to M are [2], [2,1,3] and [2,1,3,5] Example 2: Input: N = 1, M = 1 A[] = {1} Output: 1 Explanation: The subarrays which has median equal to M is [1]. Your Task: You don't need to read input or print anything. Complete the function countSubarray( ) which takes the integer N , the array A[], and the integer M as input parameters and returns the number of subarays. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ A[] ≤ 10^{5} 1 ≤ M ≤ N
class Solution: def solve(self, A, N, M): map = {} cur = 0 tot = 0 ans = 0 map[cur] = 1 for i in range(N): if A[i] >= M: x = 1 else: x = -1 if x == -1: tot -= map.get(cur - 1, 0) else: tot += map.get(cur, 0) cur += x ans += tot map[cur] = map.get(cur, 0) + 1 return ans def countSubarray(self, N, A, M): v1 = self.solve(A, N, M) v2 = self.solve(A, N, M + 1) return v1 - v2
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR VAR
Given an array A[] of N integers and an integer M. The task is to count the number of subarrays which have median M. Median of an array is defined as below: 1. If N is odd, the median value is the number that is in the middle after sorting the array. 2. if N is even, the median value is the left of the two middle numbers after sorting the array. Example 1: Input: N = 5, M = 2 A[] = {2, 1, 3, 5, 4} Output: 3 Explanation: The subarrays which has median equal to M are [2], [2,1,3] and [2,1,3,5] Example 2: Input: N = 1, M = 1 A[] = {1} Output: 1 Explanation: The subarrays which has median equal to M is [1]. Your Task: You don't need to read input or print anything. Complete the function countSubarray( ) which takes the integer N , the array A[], and the integer M as input parameters and returns the number of subarays. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ A[] ≤ 10^{5} 1 ≤ M ≤ N
class Solution: def countSubarray(self, N, A, M): def greaterMedian(m): tot = 0 ans = 0 pref = [0] * (2 * N + 1) cur = N pref[cur] += 1 for i in A: x = -1 if i >= m: x = 1 if x == 1: tot += pref[cur] else: tot -= pref[cur + x] ans += tot cur += x pref[cur] += 1 return ans return greaterMedian(M) - greaterMedian(M + 1)
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER
Given an array A[] of N integers and an integer M. The task is to count the number of subarrays which have median M. Median of an array is defined as below: 1. If N is odd, the median value is the number that is in the middle after sorting the array. 2. if N is even, the median value is the left of the two middle numbers after sorting the array. Example 1: Input: N = 5, M = 2 A[] = {2, 1, 3, 5, 4} Output: 3 Explanation: The subarrays which has median equal to M are [2], [2,1,3] and [2,1,3,5] Example 2: Input: N = 1, M = 1 A[] = {1} Output: 1 Explanation: The subarrays which has median equal to M is [1]. Your Task: You don't need to read input or print anything. Complete the function countSubarray( ) which takes the integer N , the array A[], and the integer M as input parameters and returns the number of subarays. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ A[] ≤ 10^{5} 1 ≤ M ≤ N
class Solution: def helper(self, N, A, M): deltTracker = dict() deltTracker[0] = 1 out, local, delta = 0, 0, 0 for i in A: j = 1 if i >= M else -1 local = ( local + deltTracker.get(delta, 0) if j > 0 else local - deltTracker.get(delta - 1, 0) ) delta += j deltTracker[delta] = deltTracker.get(delta, 0) + 1 out += local return out def countSubarray(self, N, A, M): return self.helper(N, A, M) - self.helper(N, A, M + 1)
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR RETURN VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER
Given an array A[] of N integers and an integer M. The task is to count the number of subarrays which have median M. Median of an array is defined as below: 1. If N is odd, the median value is the number that is in the middle after sorting the array. 2. if N is even, the median value is the left of the two middle numbers after sorting the array. Example 1: Input: N = 5, M = 2 A[] = {2, 1, 3, 5, 4} Output: 3 Explanation: The subarrays which has median equal to M are [2], [2,1,3] and [2,1,3,5] Example 2: Input: N = 1, M = 1 A[] = {1} Output: 1 Explanation: The subarrays which has median equal to M is [1]. Your Task: You don't need to read input or print anything. Complete the function countSubarray( ) which takes the integer N , the array A[], and the integer M as input parameters and returns the number of subarays. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ A[] ≤ 10^{5} 1 ≤ M ≤ N
class Solution: def countSubarray(self, N, A, M): return self.getsub(N, A, M) - self.getsub(N, A, M + 1) def getsub(self, n, a, m): li = [0] * 2 * (n + 1) ans = 0 cur = n tot = 0 li[cur] += 1 for i in range(n): if a[i] >= m: tot += li[cur] cur += 1 else: tot -= li[cur - 1] cur -= 1 ans += tot li[cur] += 1 return ans
CLASS_DEF FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP BIN_OP LIST NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR VAR VAR NUMBER RETURN VAR
Given an array A[] of N integers and an integer M. The task is to count the number of subarrays which have median M. Median of an array is defined as below: 1. If N is odd, the median value is the number that is in the middle after sorting the array. 2. if N is even, the median value is the left of the two middle numbers after sorting the array. Example 1: Input: N = 5, M = 2 A[] = {2, 1, 3, 5, 4} Output: 3 Explanation: The subarrays which has median equal to M are [2], [2,1,3] and [2,1,3,5] Example 2: Input: N = 1, M = 1 A[] = {1} Output: 1 Explanation: The subarrays which has median equal to M is [1]. Your Task: You don't need to read input or print anything. Complete the function countSubarray( ) which takes the integer N , the array A[], and the integer M as input parameters and returns the number of subarays. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ A[] ≤ 10^{5} 1 ≤ M ≤ N
class Solution: def c(self, N, A, M): mp = {} mp[0] = 1 ans = 0 ind = 0 ele = 0 for i in A: if i >= M: ele = ele + mp[ind] ind = ind + 1 else: ind = ind - 1 if ind in mp: ele = ele - mp[ind] else: mp[ind] = 0 ele = ele - mp[ind] ans = ans + ele if ind in mp: mp[ind] += 1 else: mp[ind] = 1 return ans def countSubarray(self, N, A, M): return self.c(N, A, M) - self.c(N, A, M + 1)
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER
Given an array A[] of N integers and an integer M. The task is to count the number of subarrays which have median M. Median of an array is defined as below: 1. If N is odd, the median value is the number that is in the middle after sorting the array. 2. if N is even, the median value is the left of the two middle numbers after sorting the array. Example 1: Input: N = 5, M = 2 A[] = {2, 1, 3, 5, 4} Output: 3 Explanation: The subarrays which has median equal to M are [2], [2,1,3] and [2,1,3,5] Example 2: Input: N = 1, M = 1 A[] = {1} Output: 1 Explanation: The subarrays which has median equal to M is [1]. Your Task: You don't need to read input or print anything. Complete the function countSubarray( ) which takes the integer N , the array A[], and the integer M as input parameters and returns the number of subarays. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ A[] ≤ 10^{5} 1 ≤ M ≤ N
def count(N, nums, k): d = {(0): 1} ans = 0 cur = 0 sum = 0 for i in range(N): if nums[i] < k: sum -= 1 if sum in d: cur -= d[sum] else: if sum in d: cur += d[sum] sum += 1 ans += cur if sum in d: d[sum] += 1 else: d[sum] = 1 return ans class Solution: def countSubarray(self, N, A, M): a = count(N, A, M) b = count(N, A, M + 1) return a - b
FUNC_DEF ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR VAR
Given an array A[] of N integers and an integer M. The task is to count the number of subarrays which have median M. Median of an array is defined as below: 1. If N is odd, the median value is the number that is in the middle after sorting the array. 2. if N is even, the median value is the left of the two middle numbers after sorting the array. Example 1: Input: N = 5, M = 2 A[] = {2, 1, 3, 5, 4} Output: 3 Explanation: The subarrays which has median equal to M are [2], [2,1,3] and [2,1,3,5] Example 2: Input: N = 1, M = 1 A[] = {1} Output: 1 Explanation: The subarrays which has median equal to M is [1]. Your Task: You don't need to read input or print anything. Complete the function countSubarray( ) which takes the integer N , the array A[], and the integer M as input parameters and returns the number of subarays. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ A[] ≤ 10^{5} 1 ≤ M ≤ N
class Solution: def countSubarray(self, N, A, M): def fn(M): cur = 0 total = 0 res = 0 mem = {(0): 1} for i in range(N): if A[i] >= M: x = 1 else: x = -1 if x == -1: total -= mem.get(cur - 1, 0) else: total += mem.get(cur, 0) cur += x res += total mem[cur] = 1 + mem.get(cur, 0) return res return fn(M) - fn(M + 1)
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER RETURN VAR RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER
Given an array A[] of N integers and an integer M. The task is to count the number of subarrays which have median M. Median of an array is defined as below: 1. If N is odd, the median value is the number that is in the middle after sorting the array. 2. if N is even, the median value is the left of the two middle numbers after sorting the array. Example 1: Input: N = 5, M = 2 A[] = {2, 1, 3, 5, 4} Output: 3 Explanation: The subarrays which has median equal to M are [2], [2,1,3] and [2,1,3,5] Example 2: Input: N = 1, M = 1 A[] = {1} Output: 1 Explanation: The subarrays which has median equal to M is [1]. Your Task: You don't need to read input or print anything. Complete the function countSubarray( ) which takes the integer N , the array A[], and the integer M as input parameters and returns the number of subarays. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 ≤ N ≤ 10^{5} 1 ≤ A[] ≤ 10^{5} 1 ≤ M ≤ N
class Solution: def countSubarray(self, N, A, M): def count(m): ans = 0 s = 0 cnts = {(0): 1} cursum = 0 for i, x in enumerate(A): if x >= m: b = 1 cursum += cnts.get(s, 0) else: b = -1 cursum -= cnts.get(s - 1, 0) s += b ans += cursum if s not in cnts: cnts[s] = 0 cnts[s] += 1 return ans return count(M) - count(M + 1)
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER RETURN VAR RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER
Leonid wants to become a glass carver (the person who creates beautiful artworks by cutting the glass). He already has a rectangular w mm × h mm sheet of glass, a diamond glass cutter and lots of enthusiasm. What he lacks is understanding of what to carve and how. In order not to waste time, he decided to practice the technique of carving. To do this, he makes vertical and horizontal cuts through the entire sheet. This process results in making smaller rectangular fragments of glass. Leonid does not move the newly made glass fragments. In particular, a cut divides each fragment of glass that it goes through into smaller fragments. After each cut Leonid tries to determine what area the largest of the currently available glass fragments has. Since there appear more and more fragments, this question takes him more and more time and distracts him from the fascinating process. Leonid offers to divide the labor — he will cut glass, and you will calculate the area of the maximum fragment after each cut. Do you agree? -----Input----- The first line contains three integers w, h, n (2 ≤ w, h ≤ 200 000, 1 ≤ n ≤ 200 000). Next n lines contain the descriptions of the cuts. Each description has the form H y or V x. In the first case Leonid makes the horizontal cut at the distance y millimeters (1 ≤ y ≤ h - 1) from the lower edge of the original sheet of glass. In the second case Leonid makes a vertical cut at distance x (1 ≤ x ≤ w - 1) millimeters from the left edge of the original sheet of glass. It is guaranteed that Leonid won't make two identical cuts. -----Output----- After each cut print on a single line the area of the maximum available glass fragment in mm^2. -----Examples----- Input 4 3 4 H 2 V 2 V 3 V 1 Output 8 4 4 2 Input 7 6 5 H 4 V 3 V 5 H 2 V 1 Output 28 16 12 6 4 -----Note----- Picture for the first sample test: [Image] Picture for the second sample test: $\square$
w, h, n = map(int, input().split()) x = [0, w] y = [0, h] rev = [] for _ in range(n): s, d = input().split() if s == "H": y.append(int(d)) else: x.append(int(d)) rev.append((s, int(d))) x.sort() y.sort() _max = 0 if len(x) > 1: for idx in range(len(x) - 1): _max = max(_max, x[idx + 1] - x[idx]) else: _max = w max_x = _max _max = 0 if len(y) > 1: for idx in range(len(y) - 1): _max = max(_max, y[idx + 1] - y[idx]) else: _max = w max_y = _max enum_x = {num: idx for idx, num in enumerate(x)} enum_y = {num: idx for idx, num in enumerate(y)} old_x = x old_y = y x = [[0, 0, 0]] * len(old_x) y = [[0, 0, 0]] * len(old_y) for idx in range(1, len(x) - 1): x[idx] = [old_x[idx], idx - 1, idx + 1] for idx in range(1, len(y) - 1): y[idx] = [old_y[idx], idx - 1, idx + 1] x[-1] = [w, 0, 0] y[-1] = [h, 0, 0] rev.reverse() ans = [max_x * max_y] for item in rev: if item[0] == "H": elem = y[enum_y[item[1]]] max_y = max(max_y, y[elem[2]][0] - y[elem[1]][0]) y[elem[1]][2] = elem[2] y[elem[2]][1] = elem[1] else: elem = x[enum_x[item[1]]] max_x = max(max_x, x[elem[2]][0] - x[elem[1]][0]) x[elem[1]][2] = elem[2] x[elem[2]][1] = elem[1] ans.append(max_x * max_y) ans.pop() print("\n".join(map(str, reversed(ans))))
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER VAR ASSIGN VAR LIST NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR STRING EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST LIST NUMBER NUMBER NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST LIST NUMBER NUMBER NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR LIST VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR LIST VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER LIST VAR NUMBER NUMBER ASSIGN VAR NUMBER LIST VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST BIN_OP VAR VAR FOR VAR VAR IF VAR NUMBER STRING ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR FUNC_CALL VAR VAR
Leonid wants to become a glass carver (the person who creates beautiful artworks by cutting the glass). He already has a rectangular w mm × h mm sheet of glass, a diamond glass cutter and lots of enthusiasm. What he lacks is understanding of what to carve and how. In order not to waste time, he decided to practice the technique of carving. To do this, he makes vertical and horizontal cuts through the entire sheet. This process results in making smaller rectangular fragments of glass. Leonid does not move the newly made glass fragments. In particular, a cut divides each fragment of glass that it goes through into smaller fragments. After each cut Leonid tries to determine what area the largest of the currently available glass fragments has. Since there appear more and more fragments, this question takes him more and more time and distracts him from the fascinating process. Leonid offers to divide the labor — he will cut glass, and you will calculate the area of the maximum fragment after each cut. Do you agree? -----Input----- The first line contains three integers w, h, n (2 ≤ w, h ≤ 200 000, 1 ≤ n ≤ 200 000). Next n lines contain the descriptions of the cuts. Each description has the form H y or V x. In the first case Leonid makes the horizontal cut at the distance y millimeters (1 ≤ y ≤ h - 1) from the lower edge of the original sheet of glass. In the second case Leonid makes a vertical cut at distance x (1 ≤ x ≤ w - 1) millimeters from the left edge of the original sheet of glass. It is guaranteed that Leonid won't make two identical cuts. -----Output----- After each cut print on a single line the area of the maximum available glass fragment in mm^2. -----Examples----- Input 4 3 4 H 2 V 2 V 3 V 1 Output 8 4 4 2 Input 7 6 5 H 4 V 3 V 5 H 2 V 1 Output 28 16 12 6 4 -----Note----- Picture for the first sample test: [Image] Picture for the second sample test: $\square$
from sys import stdin def main(): from sys import stdin w, h, n = list(map(int, stdin.readline().split())) res, vrt, hor = [], [], [] vh = vrt, hor for i, s in enumerate(stdin.read().splitlines()): x = int(s[2:]) flag = s[0] == "V" vh[flag].append(i) res.append([x, flag]) dim = [] for tmp, m in zip(vh, (h, w)): tmp.sort(key=lambda e: res[e][0]) u = [None, [0]] dim.append(u) j = z = 0 for i in tmp: x = res[i][0] if z < x - j: z = x - j j = x v = [u, res[i]] u.append(v) u = v res[i].append(u) v = [u, [m], None] u.append(v) dim.append(v) if z < m - j: z = m - j dim.append(z) l, r, wmax, u, d, hmax = dim whmax = [wmax, hmax] for i in range(n - 1, -1, -1): x, flag, link = res[i] u = whmax[flag] res[i] = u * whmax[not flag] link[0][2] = link[2] link[2][0] = link[0] v = link[2][1][0] - link[0][1][0] if u < v: whmax[flag] = v print("\n".join(map(str, res))) def __starting_point(): main() __starting_point()
FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR LIST LIST LIST ASSIGN VAR VAR VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER STRING EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR LIST NONE LIST NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST VAR LIST VAR NONE EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR LIST VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR
Leonid wants to become a glass carver (the person who creates beautiful artworks by cutting the glass). He already has a rectangular w mm × h mm sheet of glass, a diamond glass cutter and lots of enthusiasm. What he lacks is understanding of what to carve and how. In order not to waste time, he decided to practice the technique of carving. To do this, he makes vertical and horizontal cuts through the entire sheet. This process results in making smaller rectangular fragments of glass. Leonid does not move the newly made glass fragments. In particular, a cut divides each fragment of glass that it goes through into smaller fragments. After each cut Leonid tries to determine what area the largest of the currently available glass fragments has. Since there appear more and more fragments, this question takes him more and more time and distracts him from the fascinating process. Leonid offers to divide the labor — he will cut glass, and you will calculate the area of the maximum fragment after each cut. Do you agree? -----Input----- The first line contains three integers w, h, n (2 ≤ w, h ≤ 200 000, 1 ≤ n ≤ 200 000). Next n lines contain the descriptions of the cuts. Each description has the form H y or V x. In the first case Leonid makes the horizontal cut at the distance y millimeters (1 ≤ y ≤ h - 1) from the lower edge of the original sheet of glass. In the second case Leonid makes a vertical cut at distance x (1 ≤ x ≤ w - 1) millimeters from the left edge of the original sheet of glass. It is guaranteed that Leonid won't make two identical cuts. -----Output----- After each cut print on a single line the area of the maximum available glass fragment in mm^2. -----Examples----- Input 4 3 4 H 2 V 2 V 3 V 1 Output 8 4 4 2 Input 7 6 5 H 4 V 3 V 5 H 2 V 1 Output 28 16 12 6 4 -----Note----- Picture for the first sample test: [Image] Picture for the second sample test: $\square$
w, h, n = map(int, input().split()) l = [-1] * (w + 1) r = [-1] * (w + 1) t = [-1] * (h + 1) b = [-1] * (h + 1) l[0] = 0 b[0] = 0 t[h] = h r[w] = w V = [0] * n H = [0] * n for i in range(n): line, index = input().split() index = int(index) if line == "V": r[index] = w V[i] = index else: t[index] = h H[i] = index left = 0 mxw = 0 for i in range(1, w + 1): if r[i] != -1: l[i] = left r[left] = i mxw = max(mxw, i - left) left = i bottom = 0 mxh = 0 for i in range(1, h + 1): if t[i] != -1: b[i] = bottom t[bottom] = i mxh = max(mxh, i - bottom) bottom = i ans = [0] * n ans[n - 1] = mxh * mxw for i in range(n - 1, 0, -1): if V[i] != 0: mxw = max(mxw, r[V[i]] - l[V[i]]) r[l[V[i]]] = r[V[i]] l[r[V[i]]] = l[V[i]] else: mxh = max(mxh, t[H[i]] - b[H[i]]) b[t[H[i]]] = b[H[i]] t[b[H[i]]] = t[H[i]] ans[i - 1] = mxh * mxw for i in range(n): print(ans[i])
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR STRING ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR
Given an array of integers A, consider all non-empty subsequences of A. For any sequence S, let the width of S be the difference between the maximum and minimum element of S. Return the sum of the widths of all subsequences of A.  As the answer may be very large, return the answer modulo 10^9 + 7.   Example 1: Input: [2,1,3] Output: 6 Explanation: Subsequences are [1], [2], [3], [2,1], [2,3], [1,3], [2,1,3]. The corresponding widths are 0, 0, 0, 1, 1, 2, 2. The sum of these widths is 6.   Note: 1 <= A.length <= 20000 1 <= A[i] <= 20000
class Solution: def sumSubseqWidths(self, A: List[int]) -> int: mod = 10**9 + 7 A.sort() n = len(A) return sum(a * ((1 << i) - (1 << n - i - 1)) for i, a in enumerate(A)) % mod
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR
Given an array of integers A, consider all non-empty subsequences of A. For any sequence S, let the width of S be the difference between the maximum and minimum element of S. Return the sum of the widths of all subsequences of A.  As the answer may be very large, return the answer modulo 10^9 + 7.   Example 1: Input: [2,1,3] Output: 6 Explanation: Subsequences are [1], [2], [3], [2,1], [2,3], [1,3], [2,1,3]. The corresponding widths are 0, 0, 0, 1, 1, 2, 2. The sum of these widths is 6.   Note: 1 <= A.length <= 20000 1 <= A[i] <= 20000
class Solution: def sumSubseqWidths(self, A: List[int]) -> int: A.sort() n = len(A) total = 0 for i in range(n): a = A[i] left = i right = n - i - 1 total += ((1 << left) - 1) * a total -= ((1 << right) - 1) * a return total % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
Given an array of integers A, consider all non-empty subsequences of A. For any sequence S, let the width of S be the difference between the maximum and minimum element of S. Return the sum of the widths of all subsequences of A.  As the answer may be very large, return the answer modulo 10^9 + 7.   Example 1: Input: [2,1,3] Output: 6 Explanation: Subsequences are [1], [2], [3], [2,1], [2,3], [1,3], [2,1,3]. The corresponding widths are 0, 0, 0, 1, 1, 2, 2. The sum of these widths is 6.   Note: 1 <= A.length <= 20000 1 <= A[i] <= 20000
class Solution: def sumSubseqWidths(self, A: List[int]) -> int: mod = 10**9 + 7 n = len(A) A.sort() B = [(A[i] - A[n - i - 1]) for i in range(n)] ans = 0 for i, v in enumerate(B): ans = (ans + (v << i)) % mod return ans
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR RETURN VAR VAR
Given an array of integers A, consider all non-empty subsequences of A. For any sequence S, let the width of S be the difference between the maximum and minimum element of S. Return the sum of the widths of all subsequences of A.  As the answer may be very large, return the answer modulo 10^9 + 7.   Example 1: Input: [2,1,3] Output: 6 Explanation: Subsequences are [1], [2], [3], [2,1], [2,3], [1,3], [2,1,3]. The corresponding widths are 0, 0, 0, 1, 1, 2, 2. The sum of these widths is 6.   Note: 1 <= A.length <= 20000 1 <= A[i] <= 20000
class Solution: def sumSubseqWidths(self, A: List[int]) -> int: N = len(A) A = sorted(A) MODS = 10**9 + 7 pow2, res = [1], 0 for ii in range(1, N): pow2.append(2 * pow2[-1] % MODS) for ii, jj in enumerate(A): res = (res + (pow2[ii] - pow2[N - ii - 1]) * jj) % MODS return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR RETURN VAR VAR
Given an array of integers A, consider all non-empty subsequences of A. For any sequence S, let the width of S be the difference between the maximum and minimum element of S. Return the sum of the widths of all subsequences of A.  As the answer may be very large, return the answer modulo 10^9 + 7.   Example 1: Input: [2,1,3] Output: 6 Explanation: Subsequences are [1], [2], [3], [2,1], [2,3], [1,3], [2,1,3]. The corresponding widths are 0, 0, 0, 1, 1, 2, 2. The sum of these widths is 6.   Note: 1 <= A.length <= 20000 1 <= A[i] <= 20000
class Solution: def sumSubseqWidths(self, A: List[int]) -> int: A.sort() S = 0 B = [1] * len(A) for i in range(1, len(B)): B[i] = 2 * B[i - 1] for i in range(len(A)): S += (B[i] - B[-1 - i]) * A[i] return S % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP NUMBER VAR VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
Given an array of integers A, consider all non-empty subsequences of A. For any sequence S, let the width of S be the difference between the maximum and minimum element of S. Return the sum of the widths of all subsequences of A.  As the answer may be very large, return the answer modulo 10^9 + 7.   Example 1: Input: [2,1,3] Output: 6 Explanation: Subsequences are [1], [2], [3], [2,1], [2,3], [1,3], [2,1,3]. The corresponding widths are 0, 0, 0, 1, 1, 2, 2. The sum of these widths is 6.   Note: 1 <= A.length <= 20000 1 <= A[i] <= 20000
class Solution: def sumSubseqWidths(self, A: List[int]) -> int: mod = 10**9 + 7 n = len(A) if n == 1: return 0 pow = [1] for i in range(1, n): pow.append(pow[-1] * 2 % mod) A = sorted(A) s = 0 for i, elem in enumerate(A): n_max, n_min = i, n - i - 1 N1 = pow[i] N2 = pow[n - i - 1] s += elem * (N1 - N2) % (10**9 + 7) s = s % (10**9 + 7) return s
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN VAR VAR
Given an array of integers A, consider all non-empty subsequences of A. For any sequence S, let the width of S be the difference between the maximum and minimum element of S. Return the sum of the widths of all subsequences of A.  As the answer may be very large, return the answer modulo 10^9 + 7.   Example 1: Input: [2,1,3] Output: 6 Explanation: Subsequences are [1], [2], [3], [2,1], [2,3], [1,3], [2,1,3]. The corresponding widths are 0, 0, 0, 1, 1, 2, 2. The sum of these widths is 6.   Note: 1 <= A.length <= 20000 1 <= A[i] <= 20000
class Solution: def sumSubseqWidths(self, A: List[int]) -> int: size = len(A) mod = 10**9 + 7 pow_mod = [1] * size for i in range(1, size): pow_mod[i] = pow_mod[i - 1] * 2 % mod A.sort() ans = 0 for i in range(size): ans += A[i] * ((pow_mod[i] - pow_mod[size - 1 - i]) % mod) % mod ans %= mod return ans
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR VAR RETURN VAR VAR
Given an array of integers A, consider all non-empty subsequences of A. For any sequence S, let the width of S be the difference between the maximum and minimum element of S. Return the sum of the widths of all subsequences of A.  As the answer may be very large, return the answer modulo 10^9 + 7.   Example 1: Input: [2,1,3] Output: 6 Explanation: Subsequences are [1], [2], [3], [2,1], [2,3], [1,3], [2,1,3]. The corresponding widths are 0, 0, 0, 1, 1, 2, 2. The sum of these widths is 6.   Note: 1 <= A.length <= 20000 1 <= A[i] <= 20000
class Solution: def sumSubseqWidths(self, A: List[int]) -> int: A.sort() LIM = 10**9 + 7 res = 0 powTable = [1] for _ in range(len(A)): powTable.append(2 * powTable[-1] % LIM) for i, val in enumerate(A): res = (res + val * powTable[i] - val * powTable[len(A) - i - 1]) % LIM return res
CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR RETURN VAR VAR
Given an array of integers A, consider all non-empty subsequences of A. For any sequence S, let the width of S be the difference between the maximum and minimum element of S. Return the sum of the widths of all subsequences of A.  As the answer may be very large, return the answer modulo 10^9 + 7.   Example 1: Input: [2,1,3] Output: 6 Explanation: Subsequences are [1], [2], [3], [2,1], [2,3], [1,3], [2,1,3]. The corresponding widths are 0, 0, 0, 1, 1, 2, 2. The sum of these widths is 6.   Note: 1 <= A.length <= 20000 1 <= A[i] <= 20000
class Solution: def sumSubseqWidths(self, A: List[int]) -> int: A.sort() res = 0 mod = 10**9 + 7 N = len(A) for i in range(len(A)): res += ((1 << i) - (1 << N - i - 1)) * A[i] % mod return res % mod
CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR RETURN BIN_OP VAR VAR VAR
Given an array of integers A, consider all non-empty subsequences of A. For any sequence S, let the width of S be the difference between the maximum and minimum element of S. Return the sum of the widths of all subsequences of A.  As the answer may be very large, return the answer modulo 10^9 + 7.   Example 1: Input: [2,1,3] Output: 6 Explanation: Subsequences are [1], [2], [3], [2,1], [2,3], [1,3], [2,1,3]. The corresponding widths are 0, 0, 0, 1, 1, 2, 2. The sum of these widths is 6.   Note: 1 <= A.length <= 20000 1 <= A[i] <= 20000
class Solution: def sumSubseqWidths(self, A: List[int]) -> int: A = sorted(A) length = len(A) _sum = 0 for i in range(length): _sum += A[i] * 2**i for i in range(length): _sum -= A[i] * 2 ** (length - i - 1) return _sum % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
Given an array of integers A, consider all non-empty subsequences of A. For any sequence S, let the width of S be the difference between the maximum and minimum element of S. Return the sum of the widths of all subsequences of A.  As the answer may be very large, return the answer modulo 10^9 + 7.   Example 1: Input: [2,1,3] Output: 6 Explanation: Subsequences are [1], [2], [3], [2,1], [2,3], [1,3], [2,1,3]. The corresponding widths are 0, 0, 0, 1, 1, 2, 2. The sum of these widths is 6.   Note: 1 <= A.length <= 20000 1 <= A[i] <= 20000
class Solution: def sumSubseqWidths(self, A: List[int]) -> int: MOD = 10**9 + 7 N = len(A) A.sort() pow2 = [1] for i in range(1, N): pow2.append(pow2[-1] * 2 % MOD) ans = 0 for i, x in enumerate(A): ans = (ans + (pow2[i] - pow2[N - 1 - i]) * x) % MOD return ans
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR VAR
Given an array of integers A, consider all non-empty subsequences of A. For any sequence S, let the width of S be the difference between the maximum and minimum element of S. Return the sum of the widths of all subsequences of A.  As the answer may be very large, return the answer modulo 10^9 + 7.   Example 1: Input: [2,1,3] Output: 6 Explanation: Subsequences are [1], [2], [3], [2,1], [2,3], [1,3], [2,1,3]. The corresponding widths are 0, 0, 0, 1, 1, 2, 2. The sum of these widths is 6.   Note: 1 <= A.length <= 20000 1 <= A[i] <= 20000
class Solution: def sumSubseqWidths(self, A: List[int]) -> int: A = sorted(A) r = 0 m = 10**9 + 7 for i in range(len(A) - 1): r += (A[i + 1] - A[i]) * ( (1 << len(A)) - (1 << i + 1) - (1 << len(A) - i - 1) + 1 ) r %= m return r
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR VAR RETURN VAR VAR
Given an array of integers A, consider all non-empty subsequences of A. For any sequence S, let the width of S be the difference between the maximum and minimum element of S. Return the sum of the widths of all subsequences of A.  As the answer may be very large, return the answer modulo 10^9 + 7.   Example 1: Input: [2,1,3] Output: 6 Explanation: Subsequences are [1], [2], [3], [2,1], [2,3], [1,3], [2,1,3]. The corresponding widths are 0, 0, 0, 1, 1, 2, 2. The sum of these widths is 6.   Note: 1 <= A.length <= 20000 1 <= A[i] <= 20000
mod_ = 10**9 + 7 class Solution: def sumSubseqWidths(self, A: List[int]) -> int: A.sort() n = len(A) p_2 = [1] for i in range(1, n + 2): p_2.append(p_2[-1] * 2 % mod_) l = [0] for i in range(1, n): l.append((2 * l[-1] + (A[i] - A[i - 1]) * (p_2[i] - 1)) % mod_) sol = 0 for elem in l: sol = (sol + elem) % mod_ return sol
ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR VAR
Given an array of integers A, consider all non-empty subsequences of A. For any sequence S, let the width of S be the difference between the maximum and minimum element of S. Return the sum of the widths of all subsequences of A.  As the answer may be very large, return the answer modulo 10^9 + 7.   Example 1: Input: [2,1,3] Output: 6 Explanation: Subsequences are [1], [2], [3], [2,1], [2,3], [1,3], [2,1,3]. The corresponding widths are 0, 0, 0, 1, 1, 2, 2. The sum of these widths is 6.   Note: 1 <= A.length <= 20000 1 <= A[i] <= 20000
class Solution: def sumSubseqWidths(self, A: List[int]) -> int: MOD = 10**9 + 7 A.sort() result = 0 prev = 0 for i in range(1, len(A)): d = A[i] - A[i - 1] prev = (2 * prev + d * (pow(2, i, MOD) - 1)) % MOD result = (result + prev) % MOD return result
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP VAR BIN_OP FUNC_CALL VAR NUMBER VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR VAR
Given an array of integers A, consider all non-empty subsequences of A. For any sequence S, let the width of S be the difference between the maximum and minimum element of S. Return the sum of the widths of all subsequences of A.  As the answer may be very large, return the answer modulo 10^9 + 7.   Example 1: Input: [2,1,3] Output: 6 Explanation: Subsequences are [1], [2], [3], [2,1], [2,3], [1,3], [2,1,3]. The corresponding widths are 0, 0, 0, 1, 1, 2, 2. The sum of these widths is 6.   Note: 1 <= A.length <= 20000 1 <= A[i] <= 20000
class Solution: def sumSubseqWidths(self, A: List[int]) -> int: A = sorted(A) res = 0 MOD = 10**9 + 7 c = 1 for i in range(len(A)): res = (res + A[i] * c % MOD) % MOD c <<= 1 c %= MOD c = 1 for i in range(len(A) - 1, -1, -1): res = (res - A[i] * c % MOD) % MOD c <<= 1 c %= MOD return (res + MOD) % MOD
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR NUMBER VAR VAR RETURN BIN_OP BIN_OP VAR VAR VAR VAR
Given an array of integers A, consider all non-empty subsequences of A. For any sequence S, let the width of S be the difference between the maximum and minimum element of S. Return the sum of the widths of all subsequences of A.  As the answer may be very large, return the answer modulo 10^9 + 7.   Example 1: Input: [2,1,3] Output: 6 Explanation: Subsequences are [1], [2], [3], [2,1], [2,3], [1,3], [2,1,3]. The corresponding widths are 0, 0, 0, 1, 1, 2, 2. The sum of these widths is 6.   Note: 1 <= A.length <= 20000 1 <= A[i] <= 20000
class Solution: def sumSubseqWidths(self, A: List[int]) -> int: return sum( (1 << i) * num - (1 << len(A) - i - 1) * num for i, num in enumerate(sorted(A)) ) % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR RETURN BIN_OP FUNC_CALL VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR BIN_OP BIN_OP NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
Given an array of integers A, consider all non-empty subsequences of A. For any sequence S, let the width of S be the difference between the maximum and minimum element of S. Return the sum of the widths of all subsequences of A.  As the answer may be very large, return the answer modulo 10^9 + 7.   Example 1: Input: [2,1,3] Output: 6 Explanation: Subsequences are [1], [2], [3], [2,1], [2,3], [1,3], [2,1,3]. The corresponding widths are 0, 0, 0, 1, 1, 2, 2. The sum of these widths is 6.   Note: 1 <= A.length <= 20000 1 <= A[i] <= 20000
class Solution: def sumSubseqWidths(self, A: List[int]) -> int: A.sort() l = len(A) p = [1] ans = 0 for i in range(1, l): p.append(p[-1] * 2) for i, j in enumerate(A): ans += (p[i] - p[l - i - 1]) * j return ans % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
Given an array of integers A, consider all non-empty subsequences of A. For any sequence S, let the width of S be the difference between the maximum and minimum element of S. Return the sum of the widths of all subsequences of A.  As the answer may be very large, return the answer modulo 10^9 + 7.   Example 1: Input: [2,1,3] Output: 6 Explanation: Subsequences are [1], [2], [3], [2,1], [2,3], [1,3], [2,1,3]. The corresponding widths are 0, 0, 0, 1, 1, 2, 2. The sum of these widths is 6.   Note: 1 <= A.length <= 20000 1 <= A[i] <= 20000
class Solution: def sumSubseqWidths(self, A: List[int]) -> int: ret = 0 for i, n in enumerate(sorted(A)): ret += n * pow(2, i) ret -= n * pow(2, len(A) - i - 1) ret %= 10**9 + 7 return ret
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN VAR VAR
Given an array of integers A, consider all non-empty subsequences of A. For any sequence S, let the width of S be the difference between the maximum and minimum element of S. Return the sum of the widths of all subsequences of A.  As the answer may be very large, return the answer modulo 10^9 + 7.   Example 1: Input: [2,1,3] Output: 6 Explanation: Subsequences are [1], [2], [3], [2,1], [2,3], [1,3], [2,1,3]. The corresponding widths are 0, 0, 0, 1, 1, 2, 2. The sum of these widths is 6.   Note: 1 <= A.length <= 20000 1 <= A[i] <= 20000
class Solution: def sumSubseqWidths(self, a: List[int]) -> int: a.sort() print(a) count = 0 n = 1 c = a[0] for m, i in enumerate(a[1:]): n *= 2 count += (n - 1) * i - c c = c * 2 + i return count % (pow(10, 9) + 7)
CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR RETURN BIN_OP VAR BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER VAR
Given an array of integers A, consider all non-empty subsequences of A. For any sequence S, let the width of S be the difference between the maximum and minimum element of S. Return the sum of the widths of all subsequences of A.  As the answer may be very large, return the answer modulo 10^9 + 7.   Example 1: Input: [2,1,3] Output: 6 Explanation: Subsequences are [1], [2], [3], [2,1], [2,3], [1,3], [2,1,3]. The corresponding widths are 0, 0, 0, 1, 1, 2, 2. The sum of these widths is 6.   Note: 1 <= A.length <= 20000 1 <= A[i] <= 20000
class Solution: def sumSubseqWidths(self, A: List[int]) -> int: if not A: return 0 ans = 0 A.sort() for i, n in enumerate(A): ans += ((1 << i) - (1 << len(A) - i - 1)) * n return ans % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR IF VAR RETURN NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
Given an array of integers A, consider all non-empty subsequences of A. For any sequence S, let the width of S be the difference between the maximum and minimum element of S. Return the sum of the widths of all subsequences of A.  As the answer may be very large, return the answer modulo 10^9 + 7.   Example 1: Input: [2,1,3] Output: 6 Explanation: Subsequences are [1], [2], [3], [2,1], [2,3], [1,3], [2,1,3]. The corresponding widths are 0, 0, 0, 1, 1, 2, 2. The sum of these widths is 6.   Note: 1 <= A.length <= 20000 1 <= A[i] <= 20000
class Solution: def sumSubseqWidths(self, A: List[int]) -> int: A.sort() total = 0 const = 10**9 + 7 for i in range(len(A)): total += 2**i * A[i] - 2 ** (len(A) - i - 1) * A[i] total = total % const return total
CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR BIN_OP BIN_OP NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN VAR VAR
Given an array of integers A, consider all non-empty subsequences of A. For any sequence S, let the width of S be the difference between the maximum and minimum element of S. Return the sum of the widths of all subsequences of A.  As the answer may be very large, return the answer modulo 10^9 + 7.   Example 1: Input: [2,1,3] Output: 6 Explanation: Subsequences are [1], [2], [3], [2,1], [2,3], [1,3], [2,1,3]. The corresponding widths are 0, 0, 0, 1, 1, 2, 2. The sum of these widths is 6.   Note: 1 <= A.length <= 20000 1 <= A[i] <= 20000
class Solution: def sumSubseqWidths(self, A: List[int]) -> int: A.sort() res = 0 n = len(A) for i in range(n): res += A[i] * 1 << i % 1000000007 res -= A[i] * 1 << n - i - 1 return res % 1000000007
CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER RETURN BIN_OP VAR NUMBER VAR
Given an array of integers A, consider all non-empty subsequences of A. For any sequence S, let the width of S be the difference between the maximum and minimum element of S. Return the sum of the widths of all subsequences of A.  As the answer may be very large, return the answer modulo 10^9 + 7.   Example 1: Input: [2,1,3] Output: 6 Explanation: Subsequences are [1], [2], [3], [2,1], [2,3], [1,3], [2,1,3]. The corresponding widths are 0, 0, 0, 1, 1, 2, 2. The sum of these widths is 6.   Note: 1 <= A.length <= 20000 1 <= A[i] <= 20000
class Solution: def sumSubseqWidths(self, A: List[int]) -> int: A.sort() ret, mod, p = 0, 10**9 + 7, 1 for i in range(len(A)): ret += (A[i] - A[len(A) - i - 1]) * p % mod p = (p << 1) % mod return ret % mod
CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR RETURN BIN_OP VAR VAR VAR
Given an array of integers A, consider all non-empty subsequences of A. For any sequence S, let the width of S be the difference between the maximum and minimum element of S. Return the sum of the widths of all subsequences of A.  As the answer may be very large, return the answer modulo 10^9 + 7.   Example 1: Input: [2,1,3] Output: 6 Explanation: Subsequences are [1], [2], [3], [2,1], [2,3], [1,3], [2,1,3]. The corresponding widths are 0, 0, 0, 1, 1, 2, 2. The sum of these widths is 6.   Note: 1 <= A.length <= 20000 1 <= A[i] <= 20000
class Solution: def sumSubseqWidths(self, A: List[int]) -> int: mod = 10**9 + 7 A.sort() n = len(A) ans = 0 def exp(n): res = 1 x = 2 while n: if n & 1: res = res * x % mod n = n >> 1 x = x * x % mod return res for i in range(n): ans = (ans + exp(i) * A[i] - exp(n - i - 1) * A[i] + mod) % mod return ans
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR RETURN VAR VAR
Given an array of integers A, consider all non-empty subsequences of A. For any sequence S, let the width of S be the difference between the maximum and minimum element of S. Return the sum of the widths of all subsequences of A.  As the answer may be very large, return the answer modulo 10^9 + 7.   Example 1: Input: [2,1,3] Output: 6 Explanation: Subsequences are [1], [2], [3], [2,1], [2,3], [1,3], [2,1,3]. The corresponding widths are 0, 0, 0, 1, 1, 2, 2. The sum of these widths is 6.   Note: 1 <= A.length <= 20000 1 <= A[i] <= 20000
class Solution: def sumSubseqWidths(self, A: List[int]) -> int: n = len(A) widthsum = 0 mod = 10**9 + 7 A.sort() p = s = 0 pre, suf = [], [] for i in range(n): p += A[i] s += A[-i - 1] pre.append(p) suf.append(s) for l in range(n): widthsum += 2**l * (suf[l] - pre[l]) widthsum %= mod return widthsum
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR RETURN VAR VAR
Given an array of integers A, consider all non-empty subsequences of A. For any sequence S, let the width of S be the difference between the maximum and minimum element of S. Return the sum of the widths of all subsequences of A.  As the answer may be very large, return the answer modulo 10^9 + 7.   Example 1: Input: [2,1,3] Output: 6 Explanation: Subsequences are [1], [2], [3], [2,1], [2,3], [1,3], [2,1,3]. The corresponding widths are 0, 0, 0, 1, 1, 2, 2. The sum of these widths is 6.   Note: 1 <= A.length <= 20000 1 <= A[i] <= 20000
class Solution: def sumSubseqWidths(self, A: List[int]) -> int: A.sort() if len(A) == 1: return 0 if len(A) == 0: return 0 ans, prev, n, mod = A[1] - A[0], A[1] - A[0], len(A), 1000000000.0 + 7 twoPow, temp = [1], 2 for i in range(1, n): twoPow.append(temp + twoPow[-1]) temp = temp * 2 % mod for i in range(2, len(A)): diff = A[i] - A[i - 1] prev = (2 * (prev + diff * twoPow[i - 2] % mod) % mod + diff) % mod ans = (ans + prev) % mod return int(ans)
CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER FUNC_CALL VAR VAR BIN_OP NUMBER NUMBER ASSIGN VAR VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER BIN_OP VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR
Given an array of integers A, consider all non-empty subsequences of A. For any sequence S, let the width of S be the difference between the maximum and minimum element of S. Return the sum of the widths of all subsequences of A.  As the answer may be very large, return the answer modulo 10^9 + 7.   Example 1: Input: [2,1,3] Output: 6 Explanation: Subsequences are [1], [2], [3], [2,1], [2,3], [1,3], [2,1,3]. The corresponding widths are 0, 0, 0, 1, 1, 2, 2. The sum of these widths is 6.   Note: 1 <= A.length <= 20000 1 <= A[i] <= 20000
class Solution: def sumSubseqWidths(self, A: List[int]) -> int: B = sorted(A) res = 0 mod = 10**9 + 7 for i in range(len(B)): res += B[i] * ((1 << i) - (1 << len(B) - i - 1)) return res % mod
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN BIN_OP VAR VAR VAR
Given an array of integers A, consider all non-empty subsequences of A. For any sequence S, let the width of S be the difference between the maximum and minimum element of S. Return the sum of the widths of all subsequences of A.  As the answer may be very large, return the answer modulo 10^9 + 7.   Example 1: Input: [2,1,3] Output: 6 Explanation: Subsequences are [1], [2], [3], [2,1], [2,3], [1,3], [2,1,3]. The corresponding widths are 0, 0, 0, 1, 1, 2, 2. The sum of these widths is 6.   Note: 1 <= A.length <= 20000 1 <= A[i] <= 20000
class Solution: def sumSubseqWidths(self, A: List[int]) -> int: MOD = 10**9 + 7 A = sorted(A) if len(A) == 1: return 0 lastaddon = 0 lastv = 0 for i in range(1, len(A)): lastaddon = 2 * lastaddon + (2**i - 1) * (A[i] - A[i - 1]) lastv += lastaddon return lastv % MOD
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN BIN_OP VAR VAR VAR
Given an array of integers A, consider all non-empty subsequences of A. For any sequence S, let the width of S be the difference between the maximum and minimum element of S. Return the sum of the widths of all subsequences of A.  As the answer may be very large, return the answer modulo 10^9 + 7.   Example 1: Input: [2,1,3] Output: 6 Explanation: Subsequences are [1], [2], [3], [2,1], [2,3], [1,3], [2,1,3]. The corresponding widths are 0, 0, 0, 1, 1, 2, 2. The sum of these widths is 6.   Note: 1 <= A.length <= 20000 1 <= A[i] <= 20000
class Solution: def sumSubseqWidths(self, A: List[int]) -> int: N = len(A) if N == 1: return 0 MOD = 10**9 + 7 A.sort() pow2 = [1] widths = 0 for i in range(1, N): pow2.append(pow2[-1] * 2 % MOD) for i in range(N): widths = (widths + (pow2[i] - pow2[N - 1 - i]) * A[i]) % MOD return widths
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN VAR VAR
Given an array of integers A, consider all non-empty subsequences of A. For any sequence S, let the width of S be the difference between the maximum and minimum element of S. Return the sum of the widths of all subsequences of A.  As the answer may be very large, return the answer modulo 10^9 + 7.   Example 1: Input: [2,1,3] Output: 6 Explanation: Subsequences are [1], [2], [3], [2,1], [2,3], [1,3], [2,1,3]. The corresponding widths are 0, 0, 0, 1, 1, 2, 2. The sum of these widths is 6.   Note: 1 <= A.length <= 20000 1 <= A[i] <= 20000
class Solution: def sumSubseqWidths(self, A: List[int]) -> int: Mod = 1000000000.0 + 7 n = len(A) twoPower = [1] while len(twoPower) < n: twoPower.append(twoPower[len(twoPower) - 1] * 2 % Mod) A.sort() ans = 0 for i, a in enumerate(A): left = i right = n - i - 1 ans = (ans + (twoPower[left] - twoPower[right]) * a) % Mod return int((ans + Mod) % Mod)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER WHILE FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR
Given an array of integers A, consider all non-empty subsequences of A. For any sequence S, let the width of S be the difference between the maximum and minimum element of S. Return the sum of the widths of all subsequences of A.  As the answer may be very large, return the answer modulo 10^9 + 7.   Example 1: Input: [2,1,3] Output: 6 Explanation: Subsequences are [1], [2], [3], [2,1], [2,3], [1,3], [2,1,3]. The corresponding widths are 0, 0, 0, 1, 1, 2, 2. The sum of these widths is 6.   Note: 1 <= A.length <= 20000 1 <= A[i] <= 20000
class Solution: def sumSubseqWidths(self, A: List[int]) -> int: aa = sorted(A) n = len(A) res = 0 md = 10**9 + 7 p2 = [1] * n for i in range(1, n): p2[i] = p2[i - 1] * 2 % md for i in range(n): res = (res + aa[i] * (p2[i] - p2[n - i - 1])) % md return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN VAR VAR
Given an array of integers A, consider all non-empty subsequences of A. For any sequence S, let the width of S be the difference between the maximum and minimum element of S. Return the sum of the widths of all subsequences of A.  As the answer may be very large, return the answer modulo 10^9 + 7.   Example 1: Input: [2,1,3] Output: 6 Explanation: Subsequences are [1], [2], [3], [2,1], [2,3], [1,3], [2,1,3]. The corresponding widths are 0, 0, 0, 1, 1, 2, 2. The sum of these widths is 6.   Note: 1 <= A.length <= 20000 1 <= A[i] <= 20000
class Solution: def sumSubseqWidths(self, A: List[int]) -> int: res = 0 n = len(A) M = 10**9 + 7 c = 1 A.sort() for i in range(n): res = (res + A[i] * c - A[n - i - 1] * c) % M c = (c << 1) % M return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR RETURN VAR VAR
Given an array of integers A, consider all non-empty subsequences of A. For any sequence S, let the width of S be the difference between the maximum and minimum element of S. Return the sum of the widths of all subsequences of A.  As the answer may be very large, return the answer modulo 10^9 + 7.   Example 1: Input: [2,1,3] Output: 6 Explanation: Subsequences are [1], [2], [3], [2,1], [2,3], [1,3], [2,1,3]. The corresponding widths are 0, 0, 0, 1, 1, 2, 2. The sum of these widths is 6.   Note: 1 <= A.length <= 20000 1 <= A[i] <= 20000
class Solution: def sumSubseqWidths(self, A: List[int]) -> int: A.sort() total = 0 MOD = 10**9 + 7 powerSum = 2**0 counter = 2 currSum = A[0] for i in range(1, len(A)): total += powerSum * A[i] - currSum currSum *= 2 currSum += A[i] powerSum += counter counter *= 2 return total % MOD
CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER RETURN BIN_OP VAR VAR VAR
Given an array of integers A, consider all non-empty subsequences of A. For any sequence S, let the width of S be the difference between the maximum and minimum element of S. Return the sum of the widths of all subsequences of A.  As the answer may be very large, return the answer modulo 10^9 + 7.   Example 1: Input: [2,1,3] Output: 6 Explanation: Subsequences are [1], [2], [3], [2,1], [2,3], [1,3], [2,1,3]. The corresponding widths are 0, 0, 0, 1, 1, 2, 2. The sum of these widths is 6.   Note: 1 <= A.length <= 20000 1 <= A[i] <= 20000
class Solution: def sumSubseqWidths(self, A: List[int]) -> int: A.sort() N = len(A) return sum(num * (2**i - 2 ** (N - i - 1)) for i, num in enumerate(A)) % ( 10**9 + 7 )
CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP FUNC_CALL VAR BIN_OP VAR BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
Given an array of integers A, consider all non-empty subsequences of A. For any sequence S, let the width of S be the difference between the maximum and minimum element of S. Return the sum of the widths of all subsequences of A.  As the answer may be very large, return the answer modulo 10^9 + 7.   Example 1: Input: [2,1,3] Output: 6 Explanation: Subsequences are [1], [2], [3], [2,1], [2,3], [1,3], [2,1,3]. The corresponding widths are 0, 0, 0, 1, 1, 2, 2. The sum of these widths is 6.   Note: 1 <= A.length <= 20000 1 <= A[i] <= 20000
class Solution: BASE = 10**9 + 7 def sumSubseqWidths(self, A: List[int]) -> int: A.sort() total_cnt = 0 total_prod = 0 ans = 0 for num in A: ans = (ans + total_cnt * num - total_prod) % self.BASE total_cnt = (2 * total_cnt + 1) % self.BASE total_prod = (2 * total_prod + num) % self.BASE return ans
CLASS_DEF ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR RETURN VAR VAR
Given an array of integers A, consider all non-empty subsequences of A. For any sequence S, let the width of S be the difference between the maximum and minimum element of S. Return the sum of the widths of all subsequences of A.  As the answer may be very large, return the answer modulo 10^9 + 7.   Example 1: Input: [2,1,3] Output: 6 Explanation: Subsequences are [1], [2], [3], [2,1], [2,3], [1,3], [2,1,3]. The corresponding widths are 0, 0, 0, 1, 1, 2, 2. The sum of these widths is 6.   Note: 1 <= A.length <= 20000 1 <= A[i] <= 20000
class Solution: def sumSubseqWidths(self, nums): res = 0 for i, num in enumerate(sorted(nums)): res += ((1 << i) - (1 << len(nums) - i - 1)) * num return res % 1000000007
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR BIN_OP NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR RETURN BIN_OP VAR NUMBER
Given an array of integers A, consider all non-empty subsequences of A. For any sequence S, let the width of S be the difference between the maximum and minimum element of S. Return the sum of the widths of all subsequences of A.  As the answer may be very large, return the answer modulo 10^9 + 7.   Example 1: Input: [2,1,3] Output: 6 Explanation: Subsequences are [1], [2], [3], [2,1], [2,3], [1,3], [2,1,3]. The corresponding widths are 0, 0, 0, 1, 1, 2, 2. The sum of these widths is 6.   Note: 1 <= A.length <= 20000 1 <= A[i] <= 20000
class Solution: def sumSubseqWidths(self, A: List[int]) -> int: A.sort() result = 0 for i in range(len(A)): result *= 2 result -= A[i] result += A[~i] return result % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
Given an array of integers A, consider all non-empty subsequences of A. For any sequence S, let the width of S be the difference between the maximum and minimum element of S. Return the sum of the widths of all subsequences of A.  As the answer may be very large, return the answer modulo 10^9 + 7.   Example 1: Input: [2,1,3] Output: 6 Explanation: Subsequences are [1], [2], [3], [2,1], [2,3], [1,3], [2,1,3]. The corresponding widths are 0, 0, 0, 1, 1, 2, 2. The sum of these widths is 6.   Note: 1 <= A.length <= 20000 1 <= A[i] <= 20000
class Solution: def sumSubseqWidths(self, A: List[int]) -> int: POW2 = [(1 << i) for i in range(len(A))] return sum( (POW2[i] - POW2[len(A) - 1 - i]) * n for i, n in enumerate(sorted(A)) ) % (10**9 + 7)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR
Given an array of integers A, consider all non-empty subsequences of A. For any sequence S, let the width of S be the difference between the maximum and minimum element of S. Return the sum of the widths of all subsequences of A.  As the answer may be very large, return the answer modulo 10^9 + 7.   Example 1: Input: [2,1,3] Output: 6 Explanation: Subsequences are [1], [2], [3], [2,1], [2,3], [1,3], [2,1,3]. The corresponding widths are 0, 0, 0, 1, 1, 2, 2. The sum of these widths is 6.   Note: 1 <= A.length <= 20000 1 <= A[i] <= 20000
class Solution: def sumSubseqWidths(self, A: List[int]) -> int: A = sorted(A) total, cur, cnt = 0, 0, 0 MOD = 10**9 + 7 for i in range(1, len(A)): cnt *= 2 cnt += 1 cur *= 2 cur += (A[i] - A[i - 1]) * cnt cur %= MOD total += cur total %= MOD return total
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR
Given a collection of Intervals, the task is to merge all of the overlapping Intervals. Example 1: Input: Intervals = {{1,3},{2,4},{6,8},{9,10}} Output: {{1, 4}, {6, 8}, {9, 10}} Explanation: Given intervals: [1,3],[2,4] [6,8],[9,10], we have only two overlapping intervals here,[1,3] and [2,4]. Therefore we will merge these two and return [1,4], [6,8], [9,10]. Example 2: Input: Intervals = {{6,8},{1,9},{2,4},{4,7}} Output: {{1, 9}} Your Task: Complete the function overlappedInterval() that takes the list N intervals as input parameters and returns sorted list of intervals after merging. Expected Time Complexity: O(N*Log(N)). Expected Auxiliary Space: O(Log(N)) or O(N). Constraints: 1 ≤ N ≤ 100 0 ≤ x ≤ y ≤ 1000
class Solution: def overlappedInterval(self, Intervals): res = [] n = len(Intervals) if n == 0: return res Intervals.sort() curr = Intervals[0] for i in range(1, n): if curr[1] < Intervals[i][0]: res.append(curr) curr = Intervals[i] else: curr[1] = max(curr[1], Intervals[i][1]) res.append(curr) return res
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR
Given a collection of Intervals, the task is to merge all of the overlapping Intervals. Example 1: Input: Intervals = {{1,3},{2,4},{6,8},{9,10}} Output: {{1, 4}, {6, 8}, {9, 10}} Explanation: Given intervals: [1,3],[2,4] [6,8],[9,10], we have only two overlapping intervals here,[1,3] and [2,4]. Therefore we will merge these two and return [1,4], [6,8], [9,10]. Example 2: Input: Intervals = {{6,8},{1,9},{2,4},{4,7}} Output: {{1, 9}} Your Task: Complete the function overlappedInterval() that takes the list N intervals as input parameters and returns sorted list of intervals after merging. Expected Time Complexity: O(N*Log(N)). Expected Auxiliary Space: O(Log(N)) or O(N). Constraints: 1 ≤ N ≤ 100 0 ≤ x ≤ y ≤ 1000
class Solution: def overlappedInterval(self, Intervals): l = [] Intervals.sort() n = len(Intervals) for i in range(n): if len(l) == 0 or Intervals[i][0] > l[-1][1]: l.append(Intervals[i]) else: l[-1][1] = max(l[-1][1], Intervals[i][1]) return l
CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR NUMBER RETURN VAR
Given a collection of Intervals, the task is to merge all of the overlapping Intervals. Example 1: Input: Intervals = {{1,3},{2,4},{6,8},{9,10}} Output: {{1, 4}, {6, 8}, {9, 10}} Explanation: Given intervals: [1,3],[2,4] [6,8],[9,10], we have only two overlapping intervals here,[1,3] and [2,4]. Therefore we will merge these two and return [1,4], [6,8], [9,10]. Example 2: Input: Intervals = {{6,8},{1,9},{2,4},{4,7}} Output: {{1, 9}} Your Task: Complete the function overlappedInterval() that takes the list N intervals as input parameters and returns sorted list of intervals after merging. Expected Time Complexity: O(N*Log(N)). Expected Auxiliary Space: O(Log(N)) or O(N). Constraints: 1 ≤ N ≤ 100 0 ≤ x ≤ y ≤ 1000
class Solution: def overlappedInterval(self, Interval): n = len(Interval) Interval.sort(key=lambda x: x[0]) result = [] start = Interval[0][0] end = Interval[0][1] for i in range(1, n, 1): if Interval[i][0] <= end: end = max(end, Interval[i][1]) else: result.append([start, end]) start = Interval[i][0] end = Interval[i][1] x, y = Interval.pop() if not result or x > result[-1][1]: result.append([start, end]) else: result[-1][1] = end return result
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR IF VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR NUMBER NUMBER VAR RETURN VAR
Given a collection of Intervals, the task is to merge all of the overlapping Intervals. Example 1: Input: Intervals = {{1,3},{2,4},{6,8},{9,10}} Output: {{1, 4}, {6, 8}, {9, 10}} Explanation: Given intervals: [1,3],[2,4] [6,8],[9,10], we have only two overlapping intervals here,[1,3] and [2,4]. Therefore we will merge these two and return [1,4], [6,8], [9,10]. Example 2: Input: Intervals = {{6,8},{1,9},{2,4},{4,7}} Output: {{1, 9}} Your Task: Complete the function overlappedInterval() that takes the list N intervals as input parameters and returns sorted list of intervals after merging. Expected Time Complexity: O(N*Log(N)). Expected Auxiliary Space: O(Log(N)) or O(N). Constraints: 1 ≤ N ≤ 100 0 ≤ x ≤ y ≤ 1000
class Solution: def overlappedInterval(self, Intervals): Intervals.sort(key=lambda x: x[0]) res = [Intervals[0]] n = len(Intervals) for i in range(1, n): if res[-1][-1] >= Intervals[i][0]: res[-1][1] = max(res[-1][-1], Intervals[i][-1]) else: res.append(Intervals[i]) return res
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given a collection of Intervals, the task is to merge all of the overlapping Intervals. Example 1: Input: Intervals = {{1,3},{2,4},{6,8},{9,10}} Output: {{1, 4}, {6, 8}, {9, 10}} Explanation: Given intervals: [1,3],[2,4] [6,8],[9,10], we have only two overlapping intervals here,[1,3] and [2,4]. Therefore we will merge these two and return [1,4], [6,8], [9,10]. Example 2: Input: Intervals = {{6,8},{1,9},{2,4},{4,7}} Output: {{1, 9}} Your Task: Complete the function overlappedInterval() that takes the list N intervals as input parameters and returns sorted list of intervals after merging. Expected Time Complexity: O(N*Log(N)). Expected Auxiliary Space: O(Log(N)) or O(N). Constraints: 1 ≤ N ≤ 100 0 ≤ x ≤ y ≤ 1000
class Solution: def overlappedInterval(self, Intervals): Intervals.sort(key=lambda x: x[0]) answer = [] current = Intervals[0] for i in range(1, len(Intervals)): if Intervals[i][0] <= current[1]: current[1] = max(current[1], Intervals[i][1]) else: answer.append(current) current = Intervals[i] answer.append(current) return answer
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given a collection of Intervals, the task is to merge all of the overlapping Intervals. Example 1: Input: Intervals = {{1,3},{2,4},{6,8},{9,10}} Output: {{1, 4}, {6, 8}, {9, 10}} Explanation: Given intervals: [1,3],[2,4] [6,8],[9,10], we have only two overlapping intervals here,[1,3] and [2,4]. Therefore we will merge these two and return [1,4], [6,8], [9,10]. Example 2: Input: Intervals = {{6,8},{1,9},{2,4},{4,7}} Output: {{1, 9}} Your Task: Complete the function overlappedInterval() that takes the list N intervals as input parameters and returns sorted list of intervals after merging. Expected Time Complexity: O(N*Log(N)). Expected Auxiliary Space: O(Log(N)) or O(N). Constraints: 1 ≤ N ≤ 100 0 ≤ x ≤ y ≤ 1000
class Solution: def overlappedInterval(self, it): i = 0 it.sort() l = [] while i < len(it): y = it[i][0] x = it[i][1] while i < len(it) - 1 and it[i + 1][0] <= x: x = max(x, it[i + 1][1]) i += 1 l.append([y, x]) i += 1 return l
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR NUMBER RETURN VAR
Given a collection of Intervals, the task is to merge all of the overlapping Intervals. Example 1: Input: Intervals = {{1,3},{2,4},{6,8},{9,10}} Output: {{1, 4}, {6, 8}, {9, 10}} Explanation: Given intervals: [1,3],[2,4] [6,8],[9,10], we have only two overlapping intervals here,[1,3] and [2,4]. Therefore we will merge these two and return [1,4], [6,8], [9,10]. Example 2: Input: Intervals = {{6,8},{1,9},{2,4},{4,7}} Output: {{1, 9}} Your Task: Complete the function overlappedInterval() that takes the list N intervals as input parameters and returns sorted list of intervals after merging. Expected Time Complexity: O(N*Log(N)). Expected Auxiliary Space: O(Log(N)) or O(N). Constraints: 1 ≤ N ≤ 100 0 ≤ x ≤ y ≤ 1000
class Solution: def overlappedInterval(self, Intervals): Intervals.sort(key=lambda i: i[0]) output = [Intervals[0]] for start, end in Intervals[1:]: lastend = output[-1][1] if start <= lastend: output[-1][1] = max(end, lastend) else: output.append([start, end]) return output
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER FOR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR RETURN VAR
Given a collection of Intervals, the task is to merge all of the overlapping Intervals. Example 1: Input: Intervals = {{1,3},{2,4},{6,8},{9,10}} Output: {{1, 4}, {6, 8}, {9, 10}} Explanation: Given intervals: [1,3],[2,4] [6,8],[9,10], we have only two overlapping intervals here,[1,3] and [2,4]. Therefore we will merge these two and return [1,4], [6,8], [9,10]. Example 2: Input: Intervals = {{6,8},{1,9},{2,4},{4,7}} Output: {{1, 9}} Your Task: Complete the function overlappedInterval() that takes the list N intervals as input parameters and returns sorted list of intervals after merging. Expected Time Complexity: O(N*Log(N)). Expected Auxiliary Space: O(Log(N)) or O(N). Constraints: 1 ≤ N ≤ 100 0 ≤ x ≤ y ≤ 1000
class Solution: def overlappedInterval(self, Intervals): Intervals.sort(key=lambda x: x[0]) ans = [Intervals[0]] n = len(Intervals) x = ans[0][1] for i in range(1, n): if x < Intervals[i][0]: ans.append(Intervals[i]) x = ans[-1][1] elif x < Intervals[i][1]: ans[-1][1] = Intervals[i][1] x = ans[-1][1] return ans
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER RETURN VAR
Given a collection of Intervals, the task is to merge all of the overlapping Intervals. Example 1: Input: Intervals = {{1,3},{2,4},{6,8},{9,10}} Output: {{1, 4}, {6, 8}, {9, 10}} Explanation: Given intervals: [1,3],[2,4] [6,8],[9,10], we have only two overlapping intervals here,[1,3] and [2,4]. Therefore we will merge these two and return [1,4], [6,8], [9,10]. Example 2: Input: Intervals = {{6,8},{1,9},{2,4},{4,7}} Output: {{1, 9}} Your Task: Complete the function overlappedInterval() that takes the list N intervals as input parameters and returns sorted list of intervals after merging. Expected Time Complexity: O(N*Log(N)). Expected Auxiliary Space: O(Log(N)) or O(N). Constraints: 1 ≤ N ≤ 100 0 ≤ x ≤ y ≤ 1000
class Solution: def overlappedInterval(self, a): a.sort() ans = [a[0]] for r in a: if ans[-1][-1] >= r[0]: ans[-1][0] = min(ans[-1][0], r[0]) ans[-1][1] = max(ans[-1][1], r[1]) else: ans += [r] return ans
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER FOR VAR VAR IF VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER VAR LIST VAR RETURN VAR
Given a collection of Intervals, the task is to merge all of the overlapping Intervals. Example 1: Input: Intervals = {{1,3},{2,4},{6,8},{9,10}} Output: {{1, 4}, {6, 8}, {9, 10}} Explanation: Given intervals: [1,3],[2,4] [6,8],[9,10], we have only two overlapping intervals here,[1,3] and [2,4]. Therefore we will merge these two and return [1,4], [6,8], [9,10]. Example 2: Input: Intervals = {{6,8},{1,9},{2,4},{4,7}} Output: {{1, 9}} Your Task: Complete the function overlappedInterval() that takes the list N intervals as input parameters and returns sorted list of intervals after merging. Expected Time Complexity: O(N*Log(N)). Expected Auxiliary Space: O(Log(N)) or O(N). Constraints: 1 ≤ N ≤ 100 0 ≤ x ≤ y ≤ 1000
class Solution: def overlappedInterval(self, Intervals): Intervals.sort() merged = [] for i in Intervals: if not merged or merged[-1][1] < i[0]: merged.append(i) else: merged[-1][1] = max(merged[-1][1], i[1]) return merged
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR IF VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER RETURN VAR
Given a collection of Intervals, the task is to merge all of the overlapping Intervals. Example 1: Input: Intervals = {{1,3},{2,4},{6,8},{9,10}} Output: {{1, 4}, {6, 8}, {9, 10}} Explanation: Given intervals: [1,3],[2,4] [6,8],[9,10], we have only two overlapping intervals here,[1,3] and [2,4]. Therefore we will merge these two and return [1,4], [6,8], [9,10]. Example 2: Input: Intervals = {{6,8},{1,9},{2,4},{4,7}} Output: {{1, 9}} Your Task: Complete the function overlappedInterval() that takes the list N intervals as input parameters and returns sorted list of intervals after merging. Expected Time Complexity: O(N*Log(N)). Expected Auxiliary Space: O(Log(N)) or O(N). Constraints: 1 ≤ N ≤ 100 0 ≤ x ≤ y ≤ 1000
class Solution: def overlappedInterval(self, Intervals): Intervals.sort() stack = [] for s, e in Intervals: if stack and stack[-1][1] >= s: lastS, lastE = stack.pop() stack.append([lastS, max(lastE, e)]) else: stack.append([s, e]) return stack
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR VAR IF VAR VAR NUMBER NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR RETURN VAR
Given a collection of Intervals, the task is to merge all of the overlapping Intervals. Example 1: Input: Intervals = {{1,3},{2,4},{6,8},{9,10}} Output: {{1, 4}, {6, 8}, {9, 10}} Explanation: Given intervals: [1,3],[2,4] [6,8],[9,10], we have only two overlapping intervals here,[1,3] and [2,4]. Therefore we will merge these two and return [1,4], [6,8], [9,10]. Example 2: Input: Intervals = {{6,8},{1,9},{2,4},{4,7}} Output: {{1, 9}} Your Task: Complete the function overlappedInterval() that takes the list N intervals as input parameters and returns sorted list of intervals after merging. Expected Time Complexity: O(N*Log(N)). Expected Auxiliary Space: O(Log(N)) or O(N). Constraints: 1 ≤ N ≤ 100 0 ≤ x ≤ y ≤ 1000
class Solution: def overlappedInterval(self, Intervals): sol = [] Intervals.sort() s = Intervals[0][0] e = Intervals[0][1] for l, u in Intervals: if l > e: sol.append([s, e]) s, e = l, u else: e = max(e, u) sol.append([s, e]) return sol
CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR RETURN VAR
Given a collection of Intervals, the task is to merge all of the overlapping Intervals. Example 1: Input: Intervals = {{1,3},{2,4},{6,8},{9,10}} Output: {{1, 4}, {6, 8}, {9, 10}} Explanation: Given intervals: [1,3],[2,4] [6,8],[9,10], we have only two overlapping intervals here,[1,3] and [2,4]. Therefore we will merge these two and return [1,4], [6,8], [9,10]. Example 2: Input: Intervals = {{6,8},{1,9},{2,4},{4,7}} Output: {{1, 9}} Your Task: Complete the function overlappedInterval() that takes the list N intervals as input parameters and returns sorted list of intervals after merging. Expected Time Complexity: O(N*Log(N)). Expected Auxiliary Space: O(Log(N)) or O(N). Constraints: 1 ≤ N ≤ 100 0 ≤ x ≤ y ≤ 1000
class Solution: def get_start_interval(self, interval): return interval[0] def overlappedInterval(self, Intervals): Intervals = sorted(Intervals, key=self.get_start_interval) result = [Intervals[0]] for i in range(len(Intervals)): n = len(result) if Intervals[i] not in result: if Intervals[i][0] <= result[n - 1][1]: result[n - 1][1] = max(result[n - 1][1], Intervals[i][1]) else: result.append(Intervals[i]) return result
CLASS_DEF FUNC_DEF RETURN VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given a collection of Intervals, the task is to merge all of the overlapping Intervals. Example 1: Input: Intervals = {{1,3},{2,4},{6,8},{9,10}} Output: {{1, 4}, {6, 8}, {9, 10}} Explanation: Given intervals: [1,3],[2,4] [6,8],[9,10], we have only two overlapping intervals here,[1,3] and [2,4]. Therefore we will merge these two and return [1,4], [6,8], [9,10]. Example 2: Input: Intervals = {{6,8},{1,9},{2,4},{4,7}} Output: {{1, 9}} Your Task: Complete the function overlappedInterval() that takes the list N intervals as input parameters and returns sorted list of intervals after merging. Expected Time Complexity: O(N*Log(N)). Expected Auxiliary Space: O(Log(N)) or O(N). Constraints: 1 ≤ N ≤ 100 0 ≤ x ≤ y ≤ 1000
class Solution: def overlappedInterval(self, intervals): intervals.sort() merged = [intervals[0]] for i in range(1, len(intervals)): if merged[-1][1] < intervals[i][0]: merged.append(intervals[i]) elif merged[-1][1] >= intervals[i][0] and merged[-1][1] < intervals[i][1]: merged[-1][1] = intervals[i][1] return merged
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR NUMBER NUMBER VAR VAR NUMBER VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER VAR VAR NUMBER RETURN VAR
Given a collection of Intervals, the task is to merge all of the overlapping Intervals. Example 1: Input: Intervals = {{1,3},{2,4},{6,8},{9,10}} Output: {{1, 4}, {6, 8}, {9, 10}} Explanation: Given intervals: [1,3],[2,4] [6,8],[9,10], we have only two overlapping intervals here,[1,3] and [2,4]. Therefore we will merge these two and return [1,4], [6,8], [9,10]. Example 2: Input: Intervals = {{6,8},{1,9},{2,4},{4,7}} Output: {{1, 9}} Your Task: Complete the function overlappedInterval() that takes the list N intervals as input parameters and returns sorted list of intervals after merging. Expected Time Complexity: O(N*Log(N)). Expected Auxiliary Space: O(Log(N)) or O(N). Constraints: 1 ≤ N ≤ 100 0 ≤ x ≤ y ≤ 1000
class Solution: def overlappedInterval(self, intervals): intervals.sort(key=lambda x: x[0]) stack = [] stack.append(intervals[0]) for i in range(1, len(intervals)): top = stack[-1] if intervals[i][0] > top[1]: stack.append(intervals[i]) else: top[1] = max(top[1], intervals[i][1]) return stack
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER RETURN VAR
Given a collection of Intervals, the task is to merge all of the overlapping Intervals. Example 1: Input: Intervals = {{1,3},{2,4},{6,8},{9,10}} Output: {{1, 4}, {6, 8}, {9, 10}} Explanation: Given intervals: [1,3],[2,4] [6,8],[9,10], we have only two overlapping intervals here,[1,3] and [2,4]. Therefore we will merge these two and return [1,4], [6,8], [9,10]. Example 2: Input: Intervals = {{6,8},{1,9},{2,4},{4,7}} Output: {{1, 9}} Your Task: Complete the function overlappedInterval() that takes the list N intervals as input parameters and returns sorted list of intervals after merging. Expected Time Complexity: O(N*Log(N)). Expected Auxiliary Space: O(Log(N)) or O(N). Constraints: 1 ≤ N ≤ 100 0 ≤ x ≤ y ≤ 1000
class Solution(object): def overlappedInterval(self, intervals): intervals.sort(key=lambda x: x[0]) stack = [] for interval in intervals: start, end = interval[0], interval[1] if not stack or stack[-1][1] < start: stack.append(interval) else: stack[-1][1] = max(stack[-1][1], end) return stack
CLASS_DEF VAR FUNC_DEF EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER VAR RETURN VAR
Given a collection of Intervals, the task is to merge all of the overlapping Intervals. Example 1: Input: Intervals = {{1,3},{2,4},{6,8},{9,10}} Output: {{1, 4}, {6, 8}, {9, 10}} Explanation: Given intervals: [1,3],[2,4] [6,8],[9,10], we have only two overlapping intervals here,[1,3] and [2,4]. Therefore we will merge these two and return [1,4], [6,8], [9,10]. Example 2: Input: Intervals = {{6,8},{1,9},{2,4},{4,7}} Output: {{1, 9}} Your Task: Complete the function overlappedInterval() that takes the list N intervals as input parameters and returns sorted list of intervals after merging. Expected Time Complexity: O(N*Log(N)). Expected Auxiliary Space: O(Log(N)) or O(N). Constraints: 1 ≤ N ≤ 100 0 ≤ x ≤ y ≤ 1000
class Solution: def overlappedInterval(self, arr): arr.sort(key=lambda x: x[0]) index = 0 for i in range(1, len(arr)): if arr[index][1] >= arr[i][0]: arr[index][1] = max(arr[index][1], arr[i][1]) else: index = index + 1 arr[index] = arr[i] return arr[: index + 1]
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR BIN_OP VAR NUMBER
Given a collection of Intervals, the task is to merge all of the overlapping Intervals. Example 1: Input: Intervals = {{1,3},{2,4},{6,8},{9,10}} Output: {{1, 4}, {6, 8}, {9, 10}} Explanation: Given intervals: [1,3],[2,4] [6,8],[9,10], we have only two overlapping intervals here,[1,3] and [2,4]. Therefore we will merge these two and return [1,4], [6,8], [9,10]. Example 2: Input: Intervals = {{6,8},{1,9},{2,4},{4,7}} Output: {{1, 9}} Your Task: Complete the function overlappedInterval() that takes the list N intervals as input parameters and returns sorted list of intervals after merging. Expected Time Complexity: O(N*Log(N)). Expected Auxiliary Space: O(Log(N)) or O(N). Constraints: 1 ≤ N ≤ 100 0 ≤ x ≤ y ≤ 1000
class Solution: def overlappedInterval(self, Intervals): if not Intervals: return Intervals Intervals.sort() current_interval = Intervals[0] merged_intervals = [] for interval in Intervals: if interval[0] <= current_interval[1]: current_interval[1] = max(current_interval[1], interval[1]) else: merged_intervals.append(current_interval) current_interval = interval merged_intervals.append(current_interval) return merged_intervals
CLASS_DEF FUNC_DEF IF VAR RETURN VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given a collection of Intervals, the task is to merge all of the overlapping Intervals. Example 1: Input: Intervals = {{1,3},{2,4},{6,8},{9,10}} Output: {{1, 4}, {6, 8}, {9, 10}} Explanation: Given intervals: [1,3],[2,4] [6,8],[9,10], we have only two overlapping intervals here,[1,3] and [2,4]. Therefore we will merge these two and return [1,4], [6,8], [9,10]. Example 2: Input: Intervals = {{6,8},{1,9},{2,4},{4,7}} Output: {{1, 9}} Your Task: Complete the function overlappedInterval() that takes the list N intervals as input parameters and returns sorted list of intervals after merging. Expected Time Complexity: O(N*Log(N)). Expected Auxiliary Space: O(Log(N)) or O(N). Constraints: 1 ≤ N ≤ 100 0 ≤ x ≤ y ≤ 1000
class Solution: def overlappedInterval(self, Intervals): p = len(Intervals) if p == 1: return Intervals Intervals.sort() arr = [] maxi = Intervals[0][1] mini = Intervals[0][0] for i in Intervals: if i[0] <= maxi: if maxi < i[1]: maxi = i[1] else: arr.append([mini, maxi]) mini = i[0] maxi = i[1] arr.append([mini, maxi]) return arr
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR IF VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR RETURN VAR
Given a collection of Intervals, the task is to merge all of the overlapping Intervals. Example 1: Input: Intervals = {{1,3},{2,4},{6,8},{9,10}} Output: {{1, 4}, {6, 8}, {9, 10}} Explanation: Given intervals: [1,3],[2,4] [6,8],[9,10], we have only two overlapping intervals here,[1,3] and [2,4]. Therefore we will merge these two and return [1,4], [6,8], [9,10]. Example 2: Input: Intervals = {{6,8},{1,9},{2,4},{4,7}} Output: {{1, 9}} Your Task: Complete the function overlappedInterval() that takes the list N intervals as input parameters and returns sorted list of intervals after merging. Expected Time Complexity: O(N*Log(N)). Expected Auxiliary Space: O(Log(N)) or O(N). Constraints: 1 ≤ N ≤ 100 0 ≤ x ≤ y ≤ 1000
class Solution: def overlappedInterval(self, Intervals): Intervals = sorted(Intervals, key=lambda x: (x[0], x[1])) res = [] current = Intervals[0] for i in range(1, len(Intervals)): if ( Intervals[i][0] <= current[1] <= Intervals[i][1] or Intervals[i][1] <= current[1] ): current[1] = max(current[1], Intervals[i][1]) else: res.append(current) current = Intervals[i] else: res.append(current) return res
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given a collection of Intervals, the task is to merge all of the overlapping Intervals. Example 1: Input: Intervals = {{1,3},{2,4},{6,8},{9,10}} Output: {{1, 4}, {6, 8}, {9, 10}} Explanation: Given intervals: [1,3],[2,4] [6,8],[9,10], we have only two overlapping intervals here,[1,3] and [2,4]. Therefore we will merge these two and return [1,4], [6,8], [9,10]. Example 2: Input: Intervals = {{6,8},{1,9},{2,4},{4,7}} Output: {{1, 9}} Your Task: Complete the function overlappedInterval() that takes the list N intervals as input parameters and returns sorted list of intervals after merging. Expected Time Complexity: O(N*Log(N)). Expected Auxiliary Space: O(Log(N)) or O(N). Constraints: 1 ≤ N ≤ 100 0 ≤ x ≤ y ≤ 1000
class Solution: def overlappedInterval(self, Intervals): Intervals.sort() stack = [] stack.append(Intervals[0]) for i in range(1, len(Intervals)): if stack[-1][1] >= Intervals[i][0]: ele = stack.pop(-1) stack.append([ele[0], max(ele[1], Intervals[i][1])]) else: stack.append(Intervals[i]) return stack
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given a collection of Intervals, the task is to merge all of the overlapping Intervals. Example 1: Input: Intervals = {{1,3},{2,4},{6,8},{9,10}} Output: {{1, 4}, {6, 8}, {9, 10}} Explanation: Given intervals: [1,3],[2,4] [6,8],[9,10], we have only two overlapping intervals here,[1,3] and [2,4]. Therefore we will merge these two and return [1,4], [6,8], [9,10]. Example 2: Input: Intervals = {{6,8},{1,9},{2,4},{4,7}} Output: {{1, 9}} Your Task: Complete the function overlappedInterval() that takes the list N intervals as input parameters and returns sorted list of intervals after merging. Expected Time Complexity: O(N*Log(N)). Expected Auxiliary Space: O(Log(N)) or O(N). Constraints: 1 ≤ N ≤ 100 0 ≤ x ≤ y ≤ 1000
class Solution: def overlappedInterval(self, arr): n = len(arr) arr.sort() ans = [] for i in range(n): start = arr[i][0] end = arr[i][1] if len(ans) != 0: if start <= ans[-1][1]: continue for j in range(i + 1, n): if arr[j][0] <= end: end = max(end, arr[j][1]) end = max(end, arr[i][1]) ans.append([start, end]) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR RETURN VAR
Given a collection of Intervals, the task is to merge all of the overlapping Intervals. Example 1: Input: Intervals = {{1,3},{2,4},{6,8},{9,10}} Output: {{1, 4}, {6, 8}, {9, 10}} Explanation: Given intervals: [1,3],[2,4] [6,8],[9,10], we have only two overlapping intervals here,[1,3] and [2,4]. Therefore we will merge these two and return [1,4], [6,8], [9,10]. Example 2: Input: Intervals = {{6,8},{1,9},{2,4},{4,7}} Output: {{1, 9}} Your Task: Complete the function overlappedInterval() that takes the list N intervals as input parameters and returns sorted list of intervals after merging. Expected Time Complexity: O(N*Log(N)). Expected Auxiliary Space: O(Log(N)) or O(N). Constraints: 1 ≤ N ≤ 100 0 ≤ x ≤ y ≤ 1000
class Solution: def overlappedInterval(self, intervals): intervals.sort(key=lambda x: x[0]) index = 0 for i in range(1, len(intervals)): if intervals[index][1] >= intervals[i][0]: intervals[index][1] = max(intervals[index][1], intervals[i][1]) else: index = index + 1 intervals[index] = intervals[i] ans = [] for i in range(index + 1): ans.append(intervals[i]) return ans
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given a collection of Intervals, the task is to merge all of the overlapping Intervals. Example 1: Input: Intervals = {{1,3},{2,4},{6,8},{9,10}} Output: {{1, 4}, {6, 8}, {9, 10}} Explanation: Given intervals: [1,3],[2,4] [6,8],[9,10], we have only two overlapping intervals here,[1,3] and [2,4]. Therefore we will merge these two and return [1,4], [6,8], [9,10]. Example 2: Input: Intervals = {{6,8},{1,9},{2,4},{4,7}} Output: {{1, 9}} Your Task: Complete the function overlappedInterval() that takes the list N intervals as input parameters and returns sorted list of intervals after merging. Expected Time Complexity: O(N*Log(N)). Expected Auxiliary Space: O(Log(N)) or O(N). Constraints: 1 ≤ N ≤ 100 0 ≤ x ≤ y ≤ 1000
class Solution: def overlappedInterval(self, Intervals): Intervals.sort() i = 0 while len(Intervals) - 1 != i: if Intervals[i][1] >= Intervals[i + 1][0]: if Intervals[i][1] < Intervals[i + 1][1]: Intervals[i][1] = Intervals[i + 1][1] del Intervals[i + 1] else: i += 1 return Intervals
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE BIN_OP FUNC_CALL VAR VAR NUMBER VAR IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR
Given a collection of Intervals, the task is to merge all of the overlapping Intervals. Example 1: Input: Intervals = {{1,3},{2,4},{6,8},{9,10}} Output: {{1, 4}, {6, 8}, {9, 10}} Explanation: Given intervals: [1,3],[2,4] [6,8],[9,10], we have only two overlapping intervals here,[1,3] and [2,4]. Therefore we will merge these two and return [1,4], [6,8], [9,10]. Example 2: Input: Intervals = {{6,8},{1,9},{2,4},{4,7}} Output: {{1, 9}} Your Task: Complete the function overlappedInterval() that takes the list N intervals as input parameters and returns sorted list of intervals after merging. Expected Time Complexity: O(N*Log(N)). Expected Auxiliary Space: O(Log(N)) or O(N). Constraints: 1 ≤ N ≤ 100 0 ≤ x ≤ y ≤ 1000
class Solution: def overlappedInterval(self, intervals): if len(intervals) == 1: return intervals intervals.sort() arr = [] minh = intervals[0][0] maxh = intervals[0][1] for i in range(n - 1): if intervals[i + 1][0] <= maxh: maxh = max(maxh, intervals[i + 1][1]) else: arr.append([minh, maxh]) minh = intervals[i + 1][0] maxh = intervals[i + 1][1] if intervals[n - 1][0] <= maxh: arr.append([minh, max(intervals[n - 1][1], maxh)]) else: arr.append([minh, maxh]) return arr
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR LIST VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR LIST VAR VAR RETURN VAR
Given a collection of Intervals, the task is to merge all of the overlapping Intervals. Example 1: Input: Intervals = {{1,3},{2,4},{6,8},{9,10}} Output: {{1, 4}, {6, 8}, {9, 10}} Explanation: Given intervals: [1,3],[2,4] [6,8],[9,10], we have only two overlapping intervals here,[1,3] and [2,4]. Therefore we will merge these two and return [1,4], [6,8], [9,10]. Example 2: Input: Intervals = {{6,8},{1,9},{2,4},{4,7}} Output: {{1, 9}} Your Task: Complete the function overlappedInterval() that takes the list N intervals as input parameters and returns sorted list of intervals after merging. Expected Time Complexity: O(N*Log(N)). Expected Auxiliary Space: O(Log(N)) or O(N). Constraints: 1 ≤ N ≤ 100 0 ≤ x ≤ y ≤ 1000
class Solution: def overlappedInterval(self, Intervals): idx = 0 Intervals.sort() for i in range(1, n): if Intervals[idx][1] >= Intervals[i][0]: Intervals[idx] = [ Intervals[idx][0], max(Intervals[idx][1], Intervals[i][1]), ] else: idx += 1 Intervals[idx] = Intervals[i] return Intervals[0 : idx + 1]
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR LIST VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR NUMBER BIN_OP VAR NUMBER
Given a collection of Intervals, the task is to merge all of the overlapping Intervals. Example 1: Input: Intervals = {{1,3},{2,4},{6,8},{9,10}} Output: {{1, 4}, {6, 8}, {9, 10}} Explanation: Given intervals: [1,3],[2,4] [6,8],[9,10], we have only two overlapping intervals here,[1,3] and [2,4]. Therefore we will merge these two and return [1,4], [6,8], [9,10]. Example 2: Input: Intervals = {{6,8},{1,9},{2,4},{4,7}} Output: {{1, 9}} Your Task: Complete the function overlappedInterval() that takes the list N intervals as input parameters and returns sorted list of intervals after merging. Expected Time Complexity: O(N*Log(N)). Expected Auxiliary Space: O(Log(N)) or O(N). Constraints: 1 ≤ N ≤ 100 0 ≤ x ≤ y ≤ 1000
class Solution: def overlappedInterval(self, Interval): Interval.sort(key=lambda i: i[0]) st = [] st.append(Interval[0]) for start, end in Interval[1:]: if st[-1][1] >= start: st[-1][1] = max(st[-1][1], end) else: st.append([start, end]) return st
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER FOR VAR VAR VAR NUMBER IF VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR LIST VAR VAR RETURN VAR
Given a collection of Intervals, the task is to merge all of the overlapping Intervals. Example 1: Input: Intervals = {{1,3},{2,4},{6,8},{9,10}} Output: {{1, 4}, {6, 8}, {9, 10}} Explanation: Given intervals: [1,3],[2,4] [6,8],[9,10], we have only two overlapping intervals here,[1,3] and [2,4]. Therefore we will merge these two and return [1,4], [6,8], [9,10]. Example 2: Input: Intervals = {{6,8},{1,9},{2,4},{4,7}} Output: {{1, 9}} Your Task: Complete the function overlappedInterval() that takes the list N intervals as input parameters and returns sorted list of intervals after merging. Expected Time Complexity: O(N*Log(N)). Expected Auxiliary Space: O(Log(N)) or O(N). Constraints: 1 ≤ N ≤ 100 0 ≤ x ≤ y ≤ 1000
class Solution: def overlappedInterval(self, intervals): n = len(intervals) ans = [] if n == 0: return [] else: intervals.sort() left = intervals[0][0] right = intervals[0][1] for i in range(1, n): if intervals[i][0] <= right: right = max(right, intervals[i][1]) else: ans += [[left, right]] left = intervals[i][0] right = intervals[i][1] ans += [[left, right]] return ans
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST IF VAR NUMBER RETURN LIST EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR LIST LIST VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR LIST LIST VAR VAR RETURN VAR
Given a collection of Intervals, the task is to merge all of the overlapping Intervals. Example 1: Input: Intervals = {{1,3},{2,4},{6,8},{9,10}} Output: {{1, 4}, {6, 8}, {9, 10}} Explanation: Given intervals: [1,3],[2,4] [6,8],[9,10], we have only two overlapping intervals here,[1,3] and [2,4]. Therefore we will merge these two and return [1,4], [6,8], [9,10]. Example 2: Input: Intervals = {{6,8},{1,9},{2,4},{4,7}} Output: {{1, 9}} Your Task: Complete the function overlappedInterval() that takes the list N intervals as input parameters and returns sorted list of intervals after merging. Expected Time Complexity: O(N*Log(N)). Expected Auxiliary Space: O(Log(N)) or O(N). Constraints: 1 ≤ N ≤ 100 0 ≤ x ≤ y ≤ 1000
class Solution: def overlappedInterval(self, intervals): intervals.sort() ans = [] temp = intervals[0] for i in range(len(intervals)): if temp[1] < intervals[i][0]: ans.append(temp) temp = intervals[i] elif temp[1] >= intervals[i][0]: temp[1] = max(intervals[i][1], temp[1]) ans.append(temp) return ans
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR
Given a collection of Intervals, the task is to merge all of the overlapping Intervals. Example 1: Input: Intervals = {{1,3},{2,4},{6,8},{9,10}} Output: {{1, 4}, {6, 8}, {9, 10}} Explanation: Given intervals: [1,3],[2,4] [6,8],[9,10], we have only two overlapping intervals here,[1,3] and [2,4]. Therefore we will merge these two and return [1,4], [6,8], [9,10]. Example 2: Input: Intervals = {{6,8},{1,9},{2,4},{4,7}} Output: {{1, 9}} Your Task: Complete the function overlappedInterval() that takes the list N intervals as input parameters and returns sorted list of intervals after merging. Expected Time Complexity: O(N*Log(N)). Expected Auxiliary Space: O(Log(N)) or O(N). Constraints: 1 ≤ N ≤ 100 0 ≤ x ≤ y ≤ 1000
class Solution: def overlappedInterval(self, arr): ans = [] arr.sort(key=lambda x: x[0]) for i in arr: if len(ans) > 0: if ans[-1][1] >= i[0]: ans[-1][1] = max(i[1], ans[-1][1]) else: ans.append(i) else: ans.append(i) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given a collection of Intervals, the task is to merge all of the overlapping Intervals. Example 1: Input: Intervals = {{1,3},{2,4},{6,8},{9,10}} Output: {{1, 4}, {6, 8}, {9, 10}} Explanation: Given intervals: [1,3],[2,4] [6,8],[9,10], we have only two overlapping intervals here,[1,3] and [2,4]. Therefore we will merge these two and return [1,4], [6,8], [9,10]. Example 2: Input: Intervals = {{6,8},{1,9},{2,4},{4,7}} Output: {{1, 9}} Your Task: Complete the function overlappedInterval() that takes the list N intervals as input parameters and returns sorted list of intervals after merging. Expected Time Complexity: O(N*Log(N)). Expected Auxiliary Space: O(Log(N)) or O(N). Constraints: 1 ≤ N ≤ 100 0 ≤ x ≤ y ≤ 1000
class Solution: def overlappedInterval(self, Intervals): res = [] Intervals.sort() temp = Intervals[0] for i in range(0, len(Intervals)): if Intervals[i][0] >= temp[0] and Intervals[i][0] <= temp[1]: lis = [] lis.append(temp[0]) lis.append(max(temp[1], Intervals[i][1])) temp = list(lis) else: res.append(temp) temp = Intervals[i] if temp != []: res.append(temp) temp = [] return res
CLASS_DEF FUNC_DEF ASSIGN VAR LIST EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR LIST EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST RETURN VAR
Given a collection of Intervals, the task is to merge all of the overlapping Intervals. Example 1: Input: Intervals = {{1,3},{2,4},{6,8},{9,10}} Output: {{1, 4}, {6, 8}, {9, 10}} Explanation: Given intervals: [1,3],[2,4] [6,8],[9,10], we have only two overlapping intervals here,[1,3] and [2,4]. Therefore we will merge these two and return [1,4], [6,8], [9,10]. Example 2: Input: Intervals = {{6,8},{1,9},{2,4},{4,7}} Output: {{1, 9}} Your Task: Complete the function overlappedInterval() that takes the list N intervals as input parameters and returns sorted list of intervals after merging. Expected Time Complexity: O(N*Log(N)). Expected Auxiliary Space: O(Log(N)) or O(N). Constraints: 1 ≤ N ≤ 100 0 ≤ x ≤ y ≤ 1000
class Solution: def overlappedInterval(self, intervals): intervals.sort() stack = [] stack.append(intervals[0]) for i in range(1, len(intervals)): cst = intervals[i][0] cet = intervals[i][1] if cst <= stack[-1][1]: stack[-1][1] = max(stack[-1][1], cet) else: stack.append(intervals[i]) return stack
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR VAR VAR RETURN VAR
Given a collection of Intervals, the task is to merge all of the overlapping Intervals. Example 1: Input: Intervals = {{1,3},{2,4},{6,8},{9,10}} Output: {{1, 4}, {6, 8}, {9, 10}} Explanation: Given intervals: [1,3],[2,4] [6,8],[9,10], we have only two overlapping intervals here,[1,3] and [2,4]. Therefore we will merge these two and return [1,4], [6,8], [9,10]. Example 2: Input: Intervals = {{6,8},{1,9},{2,4},{4,7}} Output: {{1, 9}} Your Task: Complete the function overlappedInterval() that takes the list N intervals as input parameters and returns sorted list of intervals after merging. Expected Time Complexity: O(N*Log(N)). Expected Auxiliary Space: O(Log(N)) or O(N). Constraints: 1 ≤ N ≤ 100 0 ≤ x ≤ y ≤ 1000
class Solution: def overlappedInterval(self, intervals): intervals.sort(key=lambda x: x[0]) start = intervals[0][0] end = intervals[0][1] r = [] for i in range(1, len(intervals)): if end >= intervals[i][0]: end = max(end, intervals[i][1]) else: r.append([start, end]) start = intervals[i][0] end = intervals[i][1] r.append([start, end]) return r
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR RETURN VAR
Given a collection of Intervals, the task is to merge all of the overlapping Intervals. Example 1: Input: Intervals = {{1,3},{2,4},{6,8},{9,10}} Output: {{1, 4}, {6, 8}, {9, 10}} Explanation: Given intervals: [1,3],[2,4] [6,8],[9,10], we have only two overlapping intervals here,[1,3] and [2,4]. Therefore we will merge these two and return [1,4], [6,8], [9,10]. Example 2: Input: Intervals = {{6,8},{1,9},{2,4},{4,7}} Output: {{1, 9}} Your Task: Complete the function overlappedInterval() that takes the list N intervals as input parameters and returns sorted list of intervals after merging. Expected Time Complexity: O(N*Log(N)). Expected Auxiliary Space: O(Log(N)) or O(N). Constraints: 1 ≤ N ≤ 100 0 ≤ x ≤ y ≤ 1000
class Solution: def overlappedInterval(self, Intervals): Intervals.sort() if len(Intervals) == 1: return Intervals res = [] res.append(Intervals[0]) for interval in Intervals[1:]: if res[-1][0] <= interval[0] <= res[-1][1]: res[-1][1] = max(res[-1][1], interval[1]) else: res.append(interval) return res
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR NUMBER FOR VAR VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR
Given a collection of Intervals, the task is to merge all of the overlapping Intervals. Example 1: Input: Intervals = {{1,3},{2,4},{6,8},{9,10}} Output: {{1, 4}, {6, 8}, {9, 10}} Explanation: Given intervals: [1,3],[2,4] [6,8],[9,10], we have only two overlapping intervals here,[1,3] and [2,4]. Therefore we will merge these two and return [1,4], [6,8], [9,10]. Example 2: Input: Intervals = {{6,8},{1,9},{2,4},{4,7}} Output: {{1, 9}} Your Task: Complete the function overlappedInterval() that takes the list N intervals as input parameters and returns sorted list of intervals after merging. Expected Time Complexity: O(N*Log(N)). Expected Auxiliary Space: O(Log(N)) or O(N). Constraints: 1 ≤ N ≤ 100 0 ≤ x ≤ y ≤ 1000
class Solution: def overlappedInterval(self, Intervals): Intervals.sort() stk = [Intervals[0][0], Intervals[0][1]] for i in range(1, len(Intervals)): if stk[-1] >= Intervals[i][0]: if stk[-1] < Intervals[i][1]: stk.pop() stk.append(Intervals[i][1]) else: stk.append(Intervals[i][0]) stk.append(Intervals[i][1]) ans = [] for i in range(0, len(stk) - 1, 2): ans.append([stk[i], stk[i + 1]]) return ans
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR NUMBER IF VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR
Given a collection of Intervals, the task is to merge all of the overlapping Intervals. Example 1: Input: Intervals = {{1,3},{2,4},{6,8},{9,10}} Output: {{1, 4}, {6, 8}, {9, 10}} Explanation: Given intervals: [1,3],[2,4] [6,8],[9,10], we have only two overlapping intervals here,[1,3] and [2,4]. Therefore we will merge these two and return [1,4], [6,8], [9,10]. Example 2: Input: Intervals = {{6,8},{1,9},{2,4},{4,7}} Output: {{1, 9}} Your Task: Complete the function overlappedInterval() that takes the list N intervals as input parameters and returns sorted list of intervals after merging. Expected Time Complexity: O(N*Log(N)). Expected Auxiliary Space: O(Log(N)) or O(N). Constraints: 1 ≤ N ≤ 100 0 ≤ x ≤ y ≤ 1000
class Solution: def overlappedInterval(self, arr): arr.sort() i = 0 while i < len(arr) - 1: a = i + 1 if arr[i][1] < arr[a][0]: i += 1 elif arr[i][1] >= arr[a][1]: arr.pop(a) else: arr[i][1] = arr[a][1] arr.pop(a) return arr
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR
Given a collection of Intervals, the task is to merge all of the overlapping Intervals. Example 1: Input: Intervals = {{1,3},{2,4},{6,8},{9,10}} Output: {{1, 4}, {6, 8}, {9, 10}} Explanation: Given intervals: [1,3],[2,4] [6,8],[9,10], we have only two overlapping intervals here,[1,3] and [2,4]. Therefore we will merge these two and return [1,4], [6,8], [9,10]. Example 2: Input: Intervals = {{6,8},{1,9},{2,4},{4,7}} Output: {{1, 9}} Your Task: Complete the function overlappedInterval() that takes the list N intervals as input parameters and returns sorted list of intervals after merging. Expected Time Complexity: O(N*Log(N)). Expected Auxiliary Space: O(Log(N)) or O(N). Constraints: 1 ≤ N ≤ 100 0 ≤ x ≤ y ≤ 1000
class Solution: def overlappedInterval(self, a): a.sort(key=lambda x: x[0]) ans = [] for curr in a: if len(ans) == 0: ans.append(curr) else: prev = ans[-1] if prev[1] >= curr[0]: prev[1] = max(prev[1], curr[1]) else: ans.append(curr) return ans
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR
Given a collection of Intervals, the task is to merge all of the overlapping Intervals. Example 1: Input: Intervals = {{1,3},{2,4},{6,8},{9,10}} Output: {{1, 4}, {6, 8}, {9, 10}} Explanation: Given intervals: [1,3],[2,4] [6,8],[9,10], we have only two overlapping intervals here,[1,3] and [2,4]. Therefore we will merge these two and return [1,4], [6,8], [9,10]. Example 2: Input: Intervals = {{6,8},{1,9},{2,4},{4,7}} Output: {{1, 9}} Your Task: Complete the function overlappedInterval() that takes the list N intervals as input parameters and returns sorted list of intervals after merging. Expected Time Complexity: O(N*Log(N)). Expected Auxiliary Space: O(Log(N)) or O(N). Constraints: 1 ≤ N ≤ 100 0 ≤ x ≤ y ≤ 1000
class Solution: def overlappedInterval(self, Intervals): ans = [] i = 0 for cur in sorted(Intervals, key=lambda item: item[0]): if ans and ans[i - 1][1] >= cur[0]: ans[i - 1][1] = max(ans[i - 1][1], cur[1]) else: ans.append(cur) i += 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR
Given a collection of Intervals, the task is to merge all of the overlapping Intervals. Example 1: Input: Intervals = {{1,3},{2,4},{6,8},{9,10}} Output: {{1, 4}, {6, 8}, {9, 10}} Explanation: Given intervals: [1,3],[2,4] [6,8],[9,10], we have only two overlapping intervals here,[1,3] and [2,4]. Therefore we will merge these two and return [1,4], [6,8], [9,10]. Example 2: Input: Intervals = {{6,8},{1,9},{2,4},{4,7}} Output: {{1, 9}} Your Task: Complete the function overlappedInterval() that takes the list N intervals as input parameters and returns sorted list of intervals after merging. Expected Time Complexity: O(N*Log(N)). Expected Auxiliary Space: O(Log(N)) or O(N). Constraints: 1 ≤ N ≤ 100 0 ≤ x ≤ y ≤ 1000
class Solution: def overlappedInterval(self, intervals): intervals.sort(key=lambda x: x[0]) currentLeft = intervals[0][0] currentRight = intervals[0][1] result = list() for index, interval in enumerate(intervals[1:]): if interval[0] < currentLeft and interval[1] < currentLeft: result.append(interval) elif interval[0] <= currentRight: currentLeft = min(currentLeft, interval[0]) currentRight = max(currentRight, interval[1]) else: result.append([currentLeft, currentRight]) currentLeft = interval[0] currentRight = interval[1] result.append([currentLeft, currentRight]) return result
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR RETURN VAR