description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
There are n soldiers standing in a line. Each soldier is assigned a unique rating value.
You have to form a team of 3 soldiers amongst them under the following rules:
Choose 3 soldiers with index (i, j, k) with rating (rating[i], rating[j], rating[k]).
A team is valid if: (rating[i] < rating[j] < rating[k]) or (rating[i] > rating[j] > rating[k]) where (0 <= i < j < k < n).
Return the number of teams you can form given the conditions. (soldiers can be part of multiple teams).
Example 1:
Input: rating = [2,5,3,4,1]
Output: 3
Explanation: We can form three teams given the conditions. (2,3,4), (5,4,1), (5,3,1).
Example 2:
Input: rating = [2,1,3]
Output: 0
Explanation: We can't form any team given the conditions.
Example 3:
Input: rating = [1,2,3,4]
Output: 4
Constraints:
n == rating.length
1 <= n <= 200
1 <= rating[i] <= 10^5 | class Solution:
def solve_inc(self, rating, count, idx, last):
if count == 3:
return 1
if idx >= len(rating):
return 0
if rating[idx] > last:
return self.solve_inc(
rating, count + 1, idx + 1, rating[idx]
) + self.solve_inc(rating, count, idx + 1, last)
else:
return self.solve_inc(rating, count, idx + 1, last)
def solve_dec(self, rating, count, idx, last):
if count == 3:
return 1
if idx >= len(rating):
return 0
if rating[idx] < last:
return self.solve_dec(
rating, count + 1, idx + 1, rating[idx]
) + self.solve_dec(rating, count, idx + 1, last)
else:
return self.solve_dec(rating, count, idx + 1, last)
def numTeams(self, rating: List[int]) -> int:
idx = 0
res = 0
for i in range(len(rating)):
res += self.solve_inc(rating, 1, i + 1, rating[i])
res += self.solve_dec(rating, 1, i + 1, rating[i])
return res | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR RETURN VAR VAR |
There are n soldiers standing in a line. Each soldier is assigned a unique rating value.
You have to form a team of 3 soldiers amongst them under the following rules:
Choose 3 soldiers with index (i, j, k) with rating (rating[i], rating[j], rating[k]).
A team is valid if: (rating[i] < rating[j] < rating[k]) or (rating[i] > rating[j] > rating[k]) where (0 <= i < j < k < n).
Return the number of teams you can form given the conditions. (soldiers can be part of multiple teams).
Example 1:
Input: rating = [2,5,3,4,1]
Output: 3
Explanation: We can form three teams given the conditions. (2,3,4), (5,4,1), (5,3,1).
Example 2:
Input: rating = [2,1,3]
Output: 0
Explanation: We can't form any team given the conditions.
Example 3:
Input: rating = [1,2,3,4]
Output: 4
Constraints:
n == rating.length
1 <= n <= 200
1 <= rating[i] <= 10^5 | class Solution:
def numTeams(self, rating: List[int]) -> int:
cnt = 0
for i, r in enumerate(rating):
if i >= len(rating) - 2:
break
li = [val for val in rating[i + 1 :] if val > r]
for j, r2 in enumerate(li):
li2 = [val for val in li[j + 1 :] if val > r2]
cnt += len(li2)
li = [val for val in rating[i + 1 :] if val < r]
for j, r2 in enumerate(li):
li2 = [val for val in li[j + 1 :] if val < r2]
cnt += len(li2)
return cnt
def count_comb(self, li, num, asc):
cnt = 0
for i, r in enumerate(li):
if i >= len(li) - 1:
break
if asc:
for j in range(i + 1, len(li)):
if num < li[j]:
cnt += 1
else:
for j in range(i + 1, len(li)):
if num > li[j]:
cnt += 1
return cnt | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER RETURN VAR |
There are n soldiers standing in a line. Each soldier is assigned a unique rating value.
You have to form a team of 3 soldiers amongst them under the following rules:
Choose 3 soldiers with index (i, j, k) with rating (rating[i], rating[j], rating[k]).
A team is valid if: (rating[i] < rating[j] < rating[k]) or (rating[i] > rating[j] > rating[k]) where (0 <= i < j < k < n).
Return the number of teams you can form given the conditions. (soldiers can be part of multiple teams).
Example 1:
Input: rating = [2,5,3,4,1]
Output: 3
Explanation: We can form three teams given the conditions. (2,3,4), (5,4,1), (5,3,1).
Example 2:
Input: rating = [2,1,3]
Output: 0
Explanation: We can't form any team given the conditions.
Example 3:
Input: rating = [1,2,3,4]
Output: 4
Constraints:
n == rating.length
1 <= n <= 200
1 <= rating[i] <= 10^5 | def findmyTeams(
myrating: List[int], member_list: List[int], current_member: int
) -> int:
my_new_member_list = member_list.copy()
if len(member_list) == 3:
return 1
elif len(myrating) == 0:
return 0
score = 0
for a in range(len(myrating)):
if myrating[a] > current_member:
my_new_member_list.append(myrating[a])
score += findmyTeams(myrating[a:], my_new_member_list, myrating[a])
my_new_member_list.pop()
return score
class Solution:
def numTeams(self, rating: List[int]) -> int:
answer = 0
for i in range(len(rating)):
answer += findmyTeams(rating[i:], [rating[i]], rating[i])
rating.reverse()
for i in range(len(rating)):
answer += findmyTeams(rating[i:], [rating[i]], rating[i])
return answer | FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR RETURN VAR VAR CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR LIST VAR VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR LIST VAR VAR VAR VAR RETURN VAR VAR |
There are n soldiers standing in a line. Each soldier is assigned a unique rating value.
You have to form a team of 3 soldiers amongst them under the following rules:
Choose 3 soldiers with index (i, j, k) with rating (rating[i], rating[j], rating[k]).
A team is valid if: (rating[i] < rating[j] < rating[k]) or (rating[i] > rating[j] > rating[k]) where (0 <= i < j < k < n).
Return the number of teams you can form given the conditions. (soldiers can be part of multiple teams).
Example 1:
Input: rating = [2,5,3,4,1]
Output: 3
Explanation: We can form three teams given the conditions. (2,3,4), (5,4,1), (5,3,1).
Example 2:
Input: rating = [2,1,3]
Output: 0
Explanation: We can't form any team given the conditions.
Example 3:
Input: rating = [1,2,3,4]
Output: 4
Constraints:
n == rating.length
1 <= n <= 200
1 <= rating[i] <= 10^5 | class Solution:
def numTeams(self, rating: List[int]) -> int:
combos = 0
for index, numba in enumerate(rating[:-2]):
for numbay in rating[index + 1 : -1]:
for numbaz in rating[rating.index(numbay) + 1 :]:
if numba < numbay and numbay < numbaz:
combos += 1
elif numba > numbay and numbay > numbaz:
combos += 1
return combos | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR |
There are n soldiers standing in a line. Each soldier is assigned a unique rating value.
You have to form a team of 3 soldiers amongst them under the following rules:
Choose 3 soldiers with index (i, j, k) with rating (rating[i], rating[j], rating[k]).
A team is valid if: (rating[i] < rating[j] < rating[k]) or (rating[i] > rating[j] > rating[k]) where (0 <= i < j < k < n).
Return the number of teams you can form given the conditions. (soldiers can be part of multiple teams).
Example 1:
Input: rating = [2,5,3,4,1]
Output: 3
Explanation: We can form three teams given the conditions. (2,3,4), (5,4,1), (5,3,1).
Example 2:
Input: rating = [2,1,3]
Output: 0
Explanation: We can't form any team given the conditions.
Example 3:
Input: rating = [1,2,3,4]
Output: 4
Constraints:
n == rating.length
1 <= n <= 200
1 <= rating[i] <= 10^5 | class Solution:
def numTeams(self, rating: List[int]) -> int:
res = 0
res += self.increse_dfs(rating, [], 0)
res += self.decrese_dfs(rating, [], 0)
return res
def increse_dfs(self, rating: List[int], tmp: List[int], res: int) -> int:
if len(tmp) == 3:
res += 1
return res
if len(rating) == 0:
return res
for idx, n in enumerate(rating):
if len(tmp) == 0:
tmp.append(n)
res = self.increse_dfs(rating[idx + 1 :], tmp, res)
tmp.pop()
elif n > tmp[-1]:
tmp.append(n)
res = self.increse_dfs(rating[idx + 1 :], tmp, res)
tmp.pop()
return res
def decrese_dfs(self, rating: List[int], tmp: List[int], res: int) -> int:
if len(tmp) == 3:
res += 1
return res
if len(rating) == 0:
return res
for idx, n in enumerate(rating):
if len(tmp) == 0:
tmp.append(n)
res = self.decrese_dfs(rating[idx + 1 :], tmp, res)
tmp.pop()
elif n < tmp[-1]:
tmp.append(n)
res = self.decrese_dfs(rating[idx + 1 :], tmp, res)
tmp.pop()
return res | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR LIST NUMBER VAR FUNC_CALL VAR VAR LIST NUMBER RETURN VAR VAR FUNC_DEF VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR FOR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR RETURN VAR VAR FUNC_DEF VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER RETURN VAR IF FUNC_CALL VAR VAR NUMBER RETURN VAR FOR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR RETURN VAR VAR |
There are n soldiers standing in a line. Each soldier is assigned a unique rating value.
You have to form a team of 3 soldiers amongst them under the following rules:
Choose 3 soldiers with index (i, j, k) with rating (rating[i], rating[j], rating[k]).
A team is valid if: (rating[i] < rating[j] < rating[k]) or (rating[i] > rating[j] > rating[k]) where (0 <= i < j < k < n).
Return the number of teams you can form given the conditions. (soldiers can be part of multiple teams).
Example 1:
Input: rating = [2,5,3,4,1]
Output: 3
Explanation: We can form three teams given the conditions. (2,3,4), (5,4,1), (5,3,1).
Example 2:
Input: rating = [2,1,3]
Output: 0
Explanation: We can't form any team given the conditions.
Example 3:
Input: rating = [1,2,3,4]
Output: 4
Constraints:
n == rating.length
1 <= n <= 200
1 <= rating[i] <= 10^5 | class Solution:
def numTeams(self, rating: List[int]) -> int:
greater = defaultdict(int)
less = defaultdict(int)
ans = 0
for i in range(len(rating) - 1):
for j in range(i + 1, len(rating)):
if rating[j] > rating[i]:
greater[i] += 1
else:
less[i] += 1
for i in range(len(rating) - 2):
for j in range(i + 1, len(rating)):
if rating[i] < rating[j]:
ans += greater[j]
else:
ans += less[j]
return ans | 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 BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR |
There are n soldiers standing in a line. Each soldier is assigned a unique rating value.
You have to form a team of 3 soldiers amongst them under the following rules:
Choose 3 soldiers with index (i, j, k) with rating (rating[i], rating[j], rating[k]).
A team is valid if: (rating[i] < rating[j] < rating[k]) or (rating[i] > rating[j] > rating[k]) where (0 <= i < j < k < n).
Return the number of teams you can form given the conditions. (soldiers can be part of multiple teams).
Example 1:
Input: rating = [2,5,3,4,1]
Output: 3
Explanation: We can form three teams given the conditions. (2,3,4), (5,4,1), (5,3,1).
Example 2:
Input: rating = [2,1,3]
Output: 0
Explanation: We can't form any team given the conditions.
Example 3:
Input: rating = [1,2,3,4]
Output: 4
Constraints:
n == rating.length
1 <= n <= 200
1 <= rating[i] <= 10^5 | class Solution:
def numTeams(self, rating: List[int]) -> int:
output = 0
for i in range(len(rating)):
for j in range(len(rating)):
if i >= j:
continue
for k in range(len(rating)):
if j >= k:
continue
if (
rating[i] > rating[j] > rating[k]
or rating[i] < rating[j] < rating[k]
):
output += 1
return output | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR |
There are n soldiers standing in a line. Each soldier is assigned a unique rating value.
You have to form a team of 3 soldiers amongst them under the following rules:
Choose 3 soldiers with index (i, j, k) with rating (rating[i], rating[j], rating[k]).
A team is valid if: (rating[i] < rating[j] < rating[k]) or (rating[i] > rating[j] > rating[k]) where (0 <= i < j < k < n).
Return the number of teams you can form given the conditions. (soldiers can be part of multiple teams).
Example 1:
Input: rating = [2,5,3,4,1]
Output: 3
Explanation: We can form three teams given the conditions. (2,3,4), (5,4,1), (5,3,1).
Example 2:
Input: rating = [2,1,3]
Output: 0
Explanation: We can't form any team given the conditions.
Example 3:
Input: rating = [1,2,3,4]
Output: 4
Constraints:
n == rating.length
1 <= n <= 200
1 <= rating[i] <= 10^5 | from itertools import combinations
class Solution:
def numTeams(self, rating: List[int]) -> int:
from itertools import combinations
dic = {}
for i, x in enumerate(rating):
dic[x] = i
rating.sort()
cnt = 0
combinations = list(combinations(rating, 3))
for comb in combinations:
if (
dic[comb[0]] < dic[comb[1]]
and dic[comb[1]] < dic[comb[2]]
or dic[comb[0]] > dic[comb[1]]
and dic[comb[1]] > dic[comb[2]]
):
cnt += 1
return cnt
return cnt | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER RETURN VAR RETURN VAR VAR |
There are n soldiers standing in a line. Each soldier is assigned a unique rating value.
You have to form a team of 3 soldiers amongst them under the following rules:
Choose 3 soldiers with index (i, j, k) with rating (rating[i], rating[j], rating[k]).
A team is valid if: (rating[i] < rating[j] < rating[k]) or (rating[i] > rating[j] > rating[k]) where (0 <= i < j < k < n).
Return the number of teams you can form given the conditions. (soldiers can be part of multiple teams).
Example 1:
Input: rating = [2,5,3,4,1]
Output: 3
Explanation: We can form three teams given the conditions. (2,3,4), (5,4,1), (5,3,1).
Example 2:
Input: rating = [2,1,3]
Output: 0
Explanation: We can't form any team given the conditions.
Example 3:
Input: rating = [1,2,3,4]
Output: 4
Constraints:
n == rating.length
1 <= n <= 200
1 <= rating[i] <= 10^5 | class Solution:
def numTeams(self, rating: List[int]) -> int:
i = 0
final = []
add = 0
while i < len(rating):
j = len(rating) - 1
while j > i + 1:
k = j - 1
while k > i:
if rating[i] < rating[k] and rating[k] < rating[j]:
add += 1
if rating[i] > rating[k] and rating[k] > rating[j]:
add += 1
k -= 1
j -= 1
i += 1
return add | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR |
There are n soldiers standing in a line. Each soldier is assigned a unique rating value.
You have to form a team of 3 soldiers amongst them under the following rules:
Choose 3 soldiers with index (i, j, k) with rating (rating[i], rating[j], rating[k]).
A team is valid if: (rating[i] < rating[j] < rating[k]) or (rating[i] > rating[j] > rating[k]) where (0 <= i < j < k < n).
Return the number of teams you can form given the conditions. (soldiers can be part of multiple teams).
Example 1:
Input: rating = [2,5,3,4,1]
Output: 3
Explanation: We can form three teams given the conditions. (2,3,4), (5,4,1), (5,3,1).
Example 2:
Input: rating = [2,1,3]
Output: 0
Explanation: We can't form any team given the conditions.
Example 3:
Input: rating = [1,2,3,4]
Output: 4
Constraints:
n == rating.length
1 <= n <= 200
1 <= rating[i] <= 10^5 | class Solution:
def numTeams(self, rating: List[int]) -> int:
def helper(rating):
resDict = {}
res = 0
for i in range(len(rating)):
for j in range(i, len(rating)):
if rating[i] > rating[j]:
resDict[i] = resDict.get(i, 0) + 1
for i in range(len(rating)):
for j in range(i, len(rating)):
if rating[i] > rating[j]:
res += resDict.get(j, 0)
return res
return helper(rating) + helper(rating[::-1]) | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER RETURN VAR RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR |
There are n soldiers standing in a line. Each soldier is assigned a unique rating value.
You have to form a team of 3 soldiers amongst them under the following rules:
Choose 3 soldiers with index (i, j, k) with rating (rating[i], rating[j], rating[k]).
A team is valid if: (rating[i] < rating[j] < rating[k]) or (rating[i] > rating[j] > rating[k]) where (0 <= i < j < k < n).
Return the number of teams you can form given the conditions. (soldiers can be part of multiple teams).
Example 1:
Input: rating = [2,5,3,4,1]
Output: 3
Explanation: We can form three teams given the conditions. (2,3,4), (5,4,1), (5,3,1).
Example 2:
Input: rating = [2,1,3]
Output: 0
Explanation: We can't form any team given the conditions.
Example 3:
Input: rating = [1,2,3,4]
Output: 4
Constraints:
n == rating.length
1 <= n <= 200
1 <= rating[i] <= 10^5 | class Solution:
def numTeams(self, rating: List[int]) -> int:
res = 0
@lru_cache(maxsize=None)
def check(a, b):
if rating[a] > rating[b]:
return 1
elif rating[a] < rating[b]:
return 2
else:
return 0
for ind, val in enumerate(rating[2:]):
ind += 2
for a in range(ind - 1):
for b in range(a + 1, ind):
c = check(a, b)
if c == 1 and val < rating[b] or c == 2 and val > rating[b]:
res += 1
return res | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FUNC_DEF IF VAR VAR VAR VAR RETURN NUMBER IF VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_CALL VAR NONE FOR VAR VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER RETURN VAR VAR |
There are n soldiers standing in a line. Each soldier is assigned a unique rating value.
You have to form a team of 3 soldiers amongst them under the following rules:
Choose 3 soldiers with index (i, j, k) with rating (rating[i], rating[j], rating[k]).
A team is valid if: (rating[i] < rating[j] < rating[k]) or (rating[i] > rating[j] > rating[k]) where (0 <= i < j < k < n).
Return the number of teams you can form given the conditions. (soldiers can be part of multiple teams).
Example 1:
Input: rating = [2,5,3,4,1]
Output: 3
Explanation: We can form three teams given the conditions. (2,3,4), (5,4,1), (5,3,1).
Example 2:
Input: rating = [2,1,3]
Output: 0
Explanation: We can't form any team given the conditions.
Example 3:
Input: rating = [1,2,3,4]
Output: 4
Constraints:
n == rating.length
1 <= n <= 200
1 <= rating[i] <= 10^5 | class node:
def __init__(self, val):
self.val = val
self.right = None
self.left = None
class Solution:
def update(self, i, j, ind, root):
if i == j:
root.val += 1
else:
mid = (i + j) // 2
if ind <= mid:
self.update(i, mid, ind, root.left)
elif ind > mid:
self.update(mid + 1, j, ind, root.right)
root.val = 0
if root.left:
root.val += root.left.val
if root.right:
root.val += root.right.val
def findSum(self, i, j, left, right, root):
if left <= i and right >= j:
return root.val
elif right < i or left > j:
return 0
mid = (i + j) // 2
if right <= mid:
return self.findSum(i, mid, left, right, root.left)
elif left > mid:
return self.findSum(mid + 1, j, left, right, root.right)
else:
x = self.findSum(i, mid, left, mid, root.left)
y = self.findSum(mid + 1, j, mid + 1, right, root.right)
return x + y
def build(self, i, j):
if i == j:
root = node(0)
else:
mid = (i + j) // 2
root = node(0)
root.left = self.build(i, mid)
root.right = self.build(mid + 1, j)
if root.left:
root.val += root.left.val
if root.right:
root.val += root.right.val
return root
def solve(self, rating):
n = max(rating) + 1
self.arr = [0] * n
length = len(rating)
left = [0] * length
right = [0] * length
rootLeft = self.build(0, n)
rootRight = self.build(0, n)
for i in range(length):
left[i] = self.findSum(0, n, 0, rating[i] - 1, rootLeft)
self.update(0, n, rating[i], rootLeft)
for i in range(length - 1, -1, -1):
right[i] = self.findSum(0, n, rating[i] + 1, n, rootRight)
self.update(0, n, rating[i], rootRight)
ans = 0
for i in range(1, length - 1):
ans = ans + left[i] * right[i]
return ans
def numTeams(self, rating: List[int]) -> int:
if len(rating) < 3:
return 0
temp = rating[:]
temp.sort()
value = 1
self.mp = {}
for x in temp:
if x not in self.mp:
self.mp[x] = value
value += 1
for i in range(len(rating)):
rating[i] = self.mp[rating[i]]
ans = self.solve(rating) + self.solve(rating[::-1])
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE CLASS_DEF FUNC_DEF IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR IF VAR VAR VAR FUNC_DEF IF VAR VAR VAR VAR RETURN VAR IF VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR RETURN BIN_OP VAR VAR FUNC_DEF IF VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR IF VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER VAR NUMBER BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER VAR BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR RETURN VAR FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER RETURN VAR VAR |
There are n soldiers standing in a line. Each soldier is assigned a unique rating value.
You have to form a team of 3 soldiers amongst them under the following rules:
Choose 3 soldiers with index (i, j, k) with rating (rating[i], rating[j], rating[k]).
A team is valid if: (rating[i] < rating[j] < rating[k]) or (rating[i] > rating[j] > rating[k]) where (0 <= i < j < k < n).
Return the number of teams you can form given the conditions. (soldiers can be part of multiple teams).
Example 1:
Input: rating = [2,5,3,4,1]
Output: 3
Explanation: We can form three teams given the conditions. (2,3,4), (5,4,1), (5,3,1).
Example 2:
Input: rating = [2,1,3]
Output: 0
Explanation: We can't form any team given the conditions.
Example 3:
Input: rating = [1,2,3,4]
Output: 4
Constraints:
n == rating.length
1 <= n <= 200
1 <= rating[i] <= 10^5 | class Solution:
def numTeams(self, rating: List[int]) -> int:
n = len(rating)
g, s = [0] * n, [0] * n
for i in range(n):
for j in range(i + 1, n):
print(rating[j])
if rating[j] > rating[i]:
g[i] += 1
else:
s[i] += 1
print(g)
print(s)
ans = 0
for i in range(n):
for j in range(i + 1, n):
if rating[j] > rating[i]:
ans += g[j]
else:
ans += s[j]
return ans | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR |
There are n soldiers standing in a line. Each soldier is assigned a unique rating value.
You have to form a team of 3 soldiers amongst them under the following rules:
Choose 3 soldiers with index (i, j, k) with rating (rating[i], rating[j], rating[k]).
A team is valid if: (rating[i] < rating[j] < rating[k]) or (rating[i] > rating[j] > rating[k]) where (0 <= i < j < k < n).
Return the number of teams you can form given the conditions. (soldiers can be part of multiple teams).
Example 1:
Input: rating = [2,5,3,4,1]
Output: 3
Explanation: We can form three teams given the conditions. (2,3,4), (5,4,1), (5,3,1).
Example 2:
Input: rating = [2,1,3]
Output: 0
Explanation: We can't form any team given the conditions.
Example 3:
Input: rating = [1,2,3,4]
Output: 4
Constraints:
n == rating.length
1 <= n <= 200
1 <= rating[i] <= 10^5 | class Solution:
def numTeams(self, a: List[int]) -> int:
def rec(a, start, path, res):
for i in range(start, len(a)):
if len(path) == 0:
path.append(a[i])
rec(a, i + 1, path, res)
path.pop()
elif len(path) == 1:
if a[i] != path[-1]:
path.append(a[i])
rec(a, i + 1, path, res)
path.pop()
elif len(path) == 2:
if path[0] < path[1] and path[1] < a[i]:
path.append(a[i])
res[0] += 1
path.pop()
elif path[0] > path[1] and path[1] > a[i]:
path.append(a[i])
res[0] += 1
path.pop()
return
res = [0]
rec(a, 0, [], res)
return res[0] | CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR RETURN ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR VAR NUMBER LIST VAR RETURN VAR NUMBER VAR |
There are n soldiers standing in a line. Each soldier is assigned a unique rating value.
You have to form a team of 3 soldiers amongst them under the following rules:
Choose 3 soldiers with index (i, j, k) with rating (rating[i], rating[j], rating[k]).
A team is valid if: (rating[i] < rating[j] < rating[k]) or (rating[i] > rating[j] > rating[k]) where (0 <= i < j < k < n).
Return the number of teams you can form given the conditions. (soldiers can be part of multiple teams).
Example 1:
Input: rating = [2,5,3,4,1]
Output: 3
Explanation: We can form three teams given the conditions. (2,3,4), (5,4,1), (5,3,1).
Example 2:
Input: rating = [2,1,3]
Output: 0
Explanation: We can't form any team given the conditions.
Example 3:
Input: rating = [1,2,3,4]
Output: 4
Constraints:
n == rating.length
1 <= n <= 200
1 <= rating[i] <= 10^5 | class Solution:
def numTeams(self, rating: List[int]) -> int:
numT = 0
if len(rating) < 3:
return 0
blah = list(range(len(rating)))
for first_idx in blah[:-2]:
first = rating[first_idx]
for second_idx in blah[first_idx:]:
second = rating[second_idx]
for third_idx in blah[second_idx:]:
third = rating[third_idx]
if first < second and second < third:
numT += 1
if first > second and second > third:
numT += 1
return numT | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR |
There are n soldiers standing in a line. Each soldier is assigned a unique rating value.
You have to form a team of 3 soldiers amongst them under the following rules:
Choose 3 soldiers with index (i, j, k) with rating (rating[i], rating[j], rating[k]).
A team is valid if: (rating[i] < rating[j] < rating[k]) or (rating[i] > rating[j] > rating[k]) where (0 <= i < j < k < n).
Return the number of teams you can form given the conditions. (soldiers can be part of multiple teams).
Example 1:
Input: rating = [2,5,3,4,1]
Output: 3
Explanation: We can form three teams given the conditions. (2,3,4), (5,4,1), (5,3,1).
Example 2:
Input: rating = [2,1,3]
Output: 0
Explanation: We can't form any team given the conditions.
Example 3:
Input: rating = [1,2,3,4]
Output: 4
Constraints:
n == rating.length
1 <= n <= 200
1 <= rating[i] <= 10^5 | class Solution:
def numTeams(self, rating: List[int]) -> int:
teams = 0
for base in range(len(rating)):
second = base + 1
while second < len(rating):
if rating[second] > rating[base]:
third = second + 1
while third < len(rating):
if rating[third] > rating[second]:
teams += 1
third += 1
second += 1
for base in range(len(rating)):
second = base + 1
while second < len(rating):
if rating[second] < rating[base]:
third = second + 1
while third < len(rating):
if rating[third] < rating[second]:
teams += 1
third += 1
second += 1
return teams | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR |
There are n soldiers standing in a line. Each soldier is assigned a unique rating value.
You have to form a team of 3 soldiers amongst them under the following rules:
Choose 3 soldiers with index (i, j, k) with rating (rating[i], rating[j], rating[k]).
A team is valid if: (rating[i] < rating[j] < rating[k]) or (rating[i] > rating[j] > rating[k]) where (0 <= i < j < k < n).
Return the number of teams you can form given the conditions. (soldiers can be part of multiple teams).
Example 1:
Input: rating = [2,5,3,4,1]
Output: 3
Explanation: We can form three teams given the conditions. (2,3,4), (5,4,1), (5,3,1).
Example 2:
Input: rating = [2,1,3]
Output: 0
Explanation: We can't form any team given the conditions.
Example 3:
Input: rating = [1,2,3,4]
Output: 4
Constraints:
n == rating.length
1 <= n <= 200
1 <= rating[i] <= 10^5 | class Solution:
def numTeams(self, rating: List[int]) -> int:
res = 0
l = len(rating)
for i in range(0, l - 2):
for j in range(i + 1, l - 1):
res += len(
[
x
for x in rating[j + 1 :]
if (rating[j] < x) == (rating[i] < rating[j])
]
)
return res | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR |
There are n soldiers standing in a line. Each soldier is assigned a unique rating value.
You have to form a team of 3 soldiers amongst them under the following rules:
Choose 3 soldiers with index (i, j, k) with rating (rating[i], rating[j], rating[k]).
A team is valid if: (rating[i] < rating[j] < rating[k]) or (rating[i] > rating[j] > rating[k]) where (0 <= i < j < k < n).
Return the number of teams you can form given the conditions. (soldiers can be part of multiple teams).
Example 1:
Input: rating = [2,5,3,4,1]
Output: 3
Explanation: We can form three teams given the conditions. (2,3,4), (5,4,1), (5,3,1).
Example 2:
Input: rating = [2,1,3]
Output: 0
Explanation: We can't form any team given the conditions.
Example 3:
Input: rating = [1,2,3,4]
Output: 4
Constraints:
n == rating.length
1 <= n <= 200
1 <= rating[i] <= 10^5 | class Solution:
def numTeams(self, rating: List[int]) -> int:
count = 0
new = sorted(rating)
if new == rating:
return len(rating)
for i in range(len(rating) - 2):
for j in range(i + 1, len(rating) - 1):
for k in range(j + 1, len(rating)):
if (
rating[i] < rating[j]
and rating[j] < rating[k]
or rating[i] > rating[j]
and rating[j] > rating[k]
):
count = count + 1
return count | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR |
There are n soldiers standing in a line. Each soldier is assigned a unique rating value.
You have to form a team of 3 soldiers amongst them under the following rules:
Choose 3 soldiers with index (i, j, k) with rating (rating[i], rating[j], rating[k]).
A team is valid if: (rating[i] < rating[j] < rating[k]) or (rating[i] > rating[j] > rating[k]) where (0 <= i < j < k < n).
Return the number of teams you can form given the conditions. (soldiers can be part of multiple teams).
Example 1:
Input: rating = [2,5,3,4,1]
Output: 3
Explanation: We can form three teams given the conditions. (2,3,4), (5,4,1), (5,3,1).
Example 2:
Input: rating = [2,1,3]
Output: 0
Explanation: We can't form any team given the conditions.
Example 3:
Input: rating = [1,2,3,4]
Output: 4
Constraints:
n == rating.length
1 <= n <= 200
1 <= rating[i] <= 10^5 | class Solution:
def numTeams(self, rating: List[int]) -> int:
length = len(rating)
ans = []
for i in range(length - 2):
temp = rating[i]
for j in range(i + 1, length):
if rating[j] > temp:
for k in range(j + 1, length):
if rating[k] > rating[j]:
ans.append([rating[i], rating[j], rating[k]])
if rating[j] < temp:
for k in range(j + 1, length):
if rating[k] < rating[j]:
ans.append([rating[i], rating[j], rating[k]])
return len(ans) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR VAR VAR IF VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR |
There are n soldiers standing in a line. Each soldier is assigned a unique rating value.
You have to form a team of 3 soldiers amongst them under the following rules:
Choose 3 soldiers with index (i, j, k) with rating (rating[i], rating[j], rating[k]).
A team is valid if: (rating[i] < rating[j] < rating[k]) or (rating[i] > rating[j] > rating[k]) where (0 <= i < j < k < n).
Return the number of teams you can form given the conditions. (soldiers can be part of multiple teams).
Example 1:
Input: rating = [2,5,3,4,1]
Output: 3
Explanation: We can form three teams given the conditions. (2,3,4), (5,4,1), (5,3,1).
Example 2:
Input: rating = [2,1,3]
Output: 0
Explanation: We can't form any team given the conditions.
Example 3:
Input: rating = [1,2,3,4]
Output: 4
Constraints:
n == rating.length
1 <= n <= 200
1 <= rating[i] <= 10^5 | class Solution:
def numTeams(self, rating: List[int]) -> int:
z = len(rating)
final = []
if z < 3:
return 0
for i in range(0, z):
for j in range(i + 1, z):
for k in range(j + 1, z):
if (
rating[i] < rating[j]
and rating[j] < rating[k]
or rating[i] > rating[j]
and rating[j] > rating[k]
):
final.append((i, j, k))
final = set(final)
return len(final) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST IF VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR |
There are n soldiers standing in a line. Each soldier is assigned a unique rating value.
You have to form a team of 3 soldiers amongst them under the following rules:
Choose 3 soldiers with index (i, j, k) with rating (rating[i], rating[j], rating[k]).
A team is valid if: (rating[i] < rating[j] < rating[k]) or (rating[i] > rating[j] > rating[k]) where (0 <= i < j < k < n).
Return the number of teams you can form given the conditions. (soldiers can be part of multiple teams).
Example 1:
Input: rating = [2,5,3,4,1]
Output: 3
Explanation: We can form three teams given the conditions. (2,3,4), (5,4,1), (5,3,1).
Example 2:
Input: rating = [2,1,3]
Output: 0
Explanation: We can't form any team given the conditions.
Example 3:
Input: rating = [1,2,3,4]
Output: 4
Constraints:
n == rating.length
1 <= n <= 200
1 <= rating[i] <= 10^5 | def increasecheck(List, second_index):
count = 0
for i in List[second_index + 1 :]:
if i > List[second_index]:
count += 1
return count
def decreasecheck(List, second_index):
count = 0
for i in List[second_index + 1 :]:
if i < List[second_index]:
count += 1
return count
class Solution:
def numTeams(self, rating: List[int]) -> int:
n = len(rating)
output = 0
for i in range(n):
for j in range(n - 1):
if i > j:
continue
if rating[i] > rating[j]:
output += decreasecheck(rating, j)
if rating[i] < rating[j]:
output += increasecheck(rating, j)
return output | FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR NUMBER RETURN VAR CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR IF VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR |
There are n soldiers standing in a line. Each soldier is assigned a unique rating value.
You have to form a team of 3 soldiers amongst them under the following rules:
Choose 3 soldiers with index (i, j, k) with rating (rating[i], rating[j], rating[k]).
A team is valid if: (rating[i] < rating[j] < rating[k]) or (rating[i] > rating[j] > rating[k]) where (0 <= i < j < k < n).
Return the number of teams you can form given the conditions. (soldiers can be part of multiple teams).
Example 1:
Input: rating = [2,5,3,4,1]
Output: 3
Explanation: We can form three teams given the conditions. (2,3,4), (5,4,1), (5,3,1).
Example 2:
Input: rating = [2,1,3]
Output: 0
Explanation: We can't form any team given the conditions.
Example 3:
Input: rating = [1,2,3,4]
Output: 4
Constraints:
n == rating.length
1 <= n <= 200
1 <= rating[i] <= 10^5 | class Solution:
def numTeams(self, rating: List[int]) -> int:
n = len(rating)
count = [0]
def generate(team, pos, method):
if len(team) == 3:
count[0] += 1
return
else:
for i in range(pos, n):
if team != []:
if method == "i":
if rating[i] > team[-1]:
generate(team + [rating[i]], i + 1, method)
else:
continue
elif method == "d":
if rating[i] < team[-1]:
generate(team + [rating[i]], i + 1, method)
else:
continue
if team == []:
generate(team + [rating[i]], i + 1, method)
generate([], 0, "i")
generate([], 0, "d")
return count[0] | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER FUNC_DEF IF FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER RETURN FOR VAR FUNC_CALL VAR VAR VAR IF VAR LIST IF VAR STRING IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR LIST VAR VAR BIN_OP VAR NUMBER VAR IF VAR STRING IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR LIST VAR VAR BIN_OP VAR NUMBER VAR IF VAR LIST EXPR FUNC_CALL VAR BIN_OP VAR LIST VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR LIST NUMBER STRING EXPR FUNC_CALL VAR LIST NUMBER STRING RETURN VAR NUMBER VAR |
There are n soldiers standing in a line. Each soldier is assigned a unique rating value.
You have to form a team of 3 soldiers amongst them under the following rules:
Choose 3 soldiers with index (i, j, k) with rating (rating[i], rating[j], rating[k]).
A team is valid if: (rating[i] < rating[j] < rating[k]) or (rating[i] > rating[j] > rating[k]) where (0 <= i < j < k < n).
Return the number of teams you can form given the conditions. (soldiers can be part of multiple teams).
Example 1:
Input: rating = [2,5,3,4,1]
Output: 3
Explanation: We can form three teams given the conditions. (2,3,4), (5,4,1), (5,3,1).
Example 2:
Input: rating = [2,1,3]
Output: 0
Explanation: We can't form any team given the conditions.
Example 3:
Input: rating = [1,2,3,4]
Output: 4
Constraints:
n == rating.length
1 <= n <= 200
1 <= rating[i] <= 10^5 | class Solution:
def numTeams(self, rating: List[int]) -> int:
n = len(rating)
less_r = [0] * n
bigger_r = [0] * n
less_l = [0] * n
bigger_l = [0] * n
for i in range(n):
for j in range(i):
less_l[i] += rating[i] > rating[j]
bigger_l[i] += rating[i] < rating[j]
for j in range(i + 1, n):
less_r[i] += rating[i] > rating[j]
bigger_r[i] += rating[i] < rating[j]
ans = 0
for i in range(n):
ans += less_l[i] * bigger_r[i] + less_r[i] * bigger_l[i]
return ans | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR RETURN VAR VAR |
There are n soldiers standing in a line. Each soldier is assigned a unique rating value.
You have to form a team of 3 soldiers amongst them under the following rules:
Choose 3 soldiers with index (i, j, k) with rating (rating[i], rating[j], rating[k]).
A team is valid if: (rating[i] < rating[j] < rating[k]) or (rating[i] > rating[j] > rating[k]) where (0 <= i < j < k < n).
Return the number of teams you can form given the conditions. (soldiers can be part of multiple teams).
Example 1:
Input: rating = [2,5,3,4,1]
Output: 3
Explanation: We can form three teams given the conditions. (2,3,4), (5,4,1), (5,3,1).
Example 2:
Input: rating = [2,1,3]
Output: 0
Explanation: We can't form any team given the conditions.
Example 3:
Input: rating = [1,2,3,4]
Output: 4
Constraints:
n == rating.length
1 <= n <= 200
1 <= rating[i] <= 10^5 | class Solution:
def numTeams(self, rating: List[int]) -> int:
less = {}
more = {}
count = []
n = len(rating)
for i in range(n):
l = []
m = []
for j in range(i + 1, n):
if rating[i] < rating[j]:
l.append(rating[j])
if rating[i] > rating[j]:
m.append(rating[j])
if len(l) > 0:
less[rating[i]] = l
if len(m) > 0:
more[rating[i]] = m
print((less, more))
for i in list(less.keys()):
t = less[i]
for j in t:
if j in list(less.keys()):
for k in less[j]:
count.append([i, j, k])
for i in list(more.keys()):
t = more[i]
for j in t:
if j in list(more.keys()):
for k in more[j]:
count.append([i, j, k])
return len(count) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FOR VAR VAR IF VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FOR VAR VAR IF VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR |
There are n soldiers standing in a line. Each soldier is assigned a unique rating value.
You have to form a team of 3 soldiers amongst them under the following rules:
Choose 3 soldiers with index (i, j, k) with rating (rating[i], rating[j], rating[k]).
A team is valid if: (rating[i] < rating[j] < rating[k]) or (rating[i] > rating[j] > rating[k]) where (0 <= i < j < k < n).
Return the number of teams you can form given the conditions. (soldiers can be part of multiple teams).
Example 1:
Input: rating = [2,5,3,4,1]
Output: 3
Explanation: We can form three teams given the conditions. (2,3,4), (5,4,1), (5,3,1).
Example 2:
Input: rating = [2,1,3]
Output: 0
Explanation: We can't form any team given the conditions.
Example 3:
Input: rating = [1,2,3,4]
Output: 4
Constraints:
n == rating.length
1 <= n <= 200
1 <= rating[i] <= 10^5 | class Solution:
def my_function(self, start: int, end: int) -> int:
if end - start <= 1:
return 0
s = sorted(range(end - start), key=lambda x: self.rating[start + x])
return abs(s.index(0) - s.index(end - start - 1)) - 1
def numTeams(self, rating: List[int]) -> int:
self.rating = rating
total = 0
l = len(rating)
for i in range(l):
for j in range(l - i - 1):
a = self.my_function(i, l - j)
total += a
return total | CLASS_DEF FUNC_DEF VAR VAR IF BIN_OP VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR RETURN BIN_OP FUNC_CALL VAR BIN_OP FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR FUNC_DEF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR RETURN VAR VAR |
There are n soldiers standing in a line. Each soldier is assigned a unique rating value.
You have to form a team of 3 soldiers amongst them under the following rules:
Choose 3 soldiers with index (i, j, k) with rating (rating[i], rating[j], rating[k]).
A team is valid if: (rating[i] < rating[j] < rating[k]) or (rating[i] > rating[j] > rating[k]) where (0 <= i < j < k < n).
Return the number of teams you can form given the conditions. (soldiers can be part of multiple teams).
Example 1:
Input: rating = [2,5,3,4,1]
Output: 3
Explanation: We can form three teams given the conditions. (2,3,4), (5,4,1), (5,3,1).
Example 2:
Input: rating = [2,1,3]
Output: 0
Explanation: We can't form any team given the conditions.
Example 3:
Input: rating = [1,2,3,4]
Output: 4
Constraints:
n == rating.length
1 <= n <= 200
1 <= rating[i] <= 10^5 | class Solution:
def numTeams(self, rating: List[int]) -> int:
temp_list = []
temp1 = 0
temp2 = 0
for i in range(0, len(rating)):
temp1 = rating[i]
for j in range(i + 1, len(rating)):
if rating[j] > temp1:
temp2 = rating[j]
for k in range(j + 1, len(rating)):
if rating[k] > temp2:
temp_list.append([temp1, temp2, rating[k]])
for j in range(i + 1, len(rating)):
if rating[j] < temp1:
temp2 = rating[j]
for k in range(j + 1, len(rating)):
if rating[k] < temp2:
temp_list.append([temp1, temp2, rating[k]])
return len(temp_list) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR |
Given a 2D matrix M of dimensions RxC. Find the maximum sum submatrix in it.
Example 1:
Input:
R=4
C=5
M=[[1,2,-1,-4,-20],
[-8,-3,4,2,1],
[3,8,10,1,3],
[-4,-1,1,7,-6]]
Output:
29
Explanation:
The matrix is as follows and the
blue rectangle denotes the maximum sum
rectangle.
Example 2:
Input:
R=2
C=2
M=[[-1,-2],[-3,-4]]
Output:
-1
Explanation:
Taking only the first cell is the
optimal choice.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maximumSumRectangle() which takes the number R, C, and the 2D matrix M as input parameters and returns the maximum sum submatrix.
Expected Time Complexity:O(R*R*C)
Expected Auxillary Space:O(R*C)
Constraints:
1<=R,C<=500
-1000<=M[i][j]<=1000 | class Solution:
def maximumSumRectangle(self, m, n, mat):
for i in range(m):
for j in range(1, n):
mat[i][j] += mat[i][j - 1]
maxsum = float("-inf")
for fc in range(n):
for sc in range(fc, n):
rowSum = [0] * m
for r in range(m):
rowSum[r] = mat[r][sc] - (mat[r][fc - 1] if fc > 0 else 0)
cmax = 0
for r in range(m):
cmax = max(cmax + rowSum[r], rowSum[r])
maxsum = max(maxsum, cmax)
return maxsum | CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given a 2D matrix M of dimensions RxC. Find the maximum sum submatrix in it.
Example 1:
Input:
R=4
C=5
M=[[1,2,-1,-4,-20],
[-8,-3,4,2,1],
[3,8,10,1,3],
[-4,-1,1,7,-6]]
Output:
29
Explanation:
The matrix is as follows and the
blue rectangle denotes the maximum sum
rectangle.
Example 2:
Input:
R=2
C=2
M=[[-1,-2],[-3,-4]]
Output:
-1
Explanation:
Taking only the first cell is the
optimal choice.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maximumSumRectangle() which takes the number R, C, and the 2D matrix M as input parameters and returns the maximum sum submatrix.
Expected Time Complexity:O(R*R*C)
Expected Auxillary Space:O(R*C)
Constraints:
1<=R,C<=500
-1000<=M[i][j]<=1000 | import sys
class Solution:
def kadane(self, arr, n):
maxa = -(sys.maxsize - 1)
temp = 0
for el in arr:
temp = temp + el
if temp > maxa:
maxa = temp
if temp < 0:
temp = 0
return maxa
def dp_sol(self, R, C, M):
temp = [0] * R
Cc = 0
maxi = -(sys.maxsize - 1)
for i in range(C):
for j in range(i, C):
for k in range(R):
temp[k] = temp[k] + M[k][j]
maxi = max(maxi, self.kadane(temp, R))
temp = [0] * R
return maxi
def maximumSumRectangle(self, R, C, M):
return self.dp_sol(R, C, M) | IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR VAR |
Given a 2D matrix M of dimensions RxC. Find the maximum sum submatrix in it.
Example 1:
Input:
R=4
C=5
M=[[1,2,-1,-4,-20],
[-8,-3,4,2,1],
[3,8,10,1,3],
[-4,-1,1,7,-6]]
Output:
29
Explanation:
The matrix is as follows and the
blue rectangle denotes the maximum sum
rectangle.
Example 2:
Input:
R=2
C=2
M=[[-1,-2],[-3,-4]]
Output:
-1
Explanation:
Taking only the first cell is the
optimal choice.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maximumSumRectangle() which takes the number R, C, and the 2D matrix M as input parameters and returns the maximum sum submatrix.
Expected Time Complexity:O(R*R*C)
Expected Auxillary Space:O(R*C)
Constraints:
1<=R,C<=500
-1000<=M[i][j]<=1000 | import sys
class Solution:
def kadens(self, arr):
cur_sum = arr[0]
max_sum = arr[0]
for i in range(1, len(arr)):
cur_sum = max(arr[i], cur_sum + arr[i])
max_sum = max(cur_sum, max_sum)
return max_sum
def maximumSumRectangle(self, R, C, M):
mxans = -99999
for i in range(C):
arr = [0] * R
for j in range(i, C):
for k in range(R):
arr[k] += M[k][j]
currsu = self.kadens(arr)
mxans = max(currsu, mxans)
return mxans | IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given a 2D matrix M of dimensions RxC. Find the maximum sum submatrix in it.
Example 1:
Input:
R=4
C=5
M=[[1,2,-1,-4,-20],
[-8,-3,4,2,1],
[3,8,10,1,3],
[-4,-1,1,7,-6]]
Output:
29
Explanation:
The matrix is as follows and the
blue rectangle denotes the maximum sum
rectangle.
Example 2:
Input:
R=2
C=2
M=[[-1,-2],[-3,-4]]
Output:
-1
Explanation:
Taking only the first cell is the
optimal choice.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maximumSumRectangle() which takes the number R, C, and the 2D matrix M as input parameters and returns the maximum sum submatrix.
Expected Time Complexity:O(R*R*C)
Expected Auxillary Space:O(R*C)
Constraints:
1<=R,C<=500
-1000<=M[i][j]<=1000 | class Solution:
def maximumSumRectangle(self, R, C, a):
ans = a[0][0]
for i in range(C):
temp = [0] * R
for j in range(i, C):
for k in range(R):
temp[k] += a[k][j]
currSum = 0
for r in range(R):
currSum += temp[r]
if ans < currSum:
ans = currSum
if currSum < 0:
currSum = 0
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER RETURN VAR |
Given a 2D matrix M of dimensions RxC. Find the maximum sum submatrix in it.
Example 1:
Input:
R=4
C=5
M=[[1,2,-1,-4,-20],
[-8,-3,4,2,1],
[3,8,10,1,3],
[-4,-1,1,7,-6]]
Output:
29
Explanation:
The matrix is as follows and the
blue rectangle denotes the maximum sum
rectangle.
Example 2:
Input:
R=2
C=2
M=[[-1,-2],[-3,-4]]
Output:
-1
Explanation:
Taking only the first cell is the
optimal choice.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maximumSumRectangle() which takes the number R, C, and the 2D matrix M as input parameters and returns the maximum sum submatrix.
Expected Time Complexity:O(R*R*C)
Expected Auxillary Space:O(R*C)
Constraints:
1<=R,C<=500
-1000<=M[i][j]<=1000 | class Solution:
def maximumSumRectangle(self, R, C, M):
max_sum = float("-inf")
for left in range(C):
temp = [0] * R
for right in range(left, C):
for i in range(R):
temp[i] += M[i][right]
current_sum = max_subarray_sum(temp)
max_sum = max(max_sum, current_sum)
return max_sum
def max_subarray_sum(temp):
max_sum = float("-inf")
current_sum = 0
for i in range(len(temp)):
current_sum += temp[i]
max_sum = max(max_sum, current_sum)
if current_sum < 0:
current_sum = 0
return max_sum | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER RETURN VAR |
Given a 2D matrix M of dimensions RxC. Find the maximum sum submatrix in it.
Example 1:
Input:
R=4
C=5
M=[[1,2,-1,-4,-20],
[-8,-3,4,2,1],
[3,8,10,1,3],
[-4,-1,1,7,-6]]
Output:
29
Explanation:
The matrix is as follows and the
blue rectangle denotes the maximum sum
rectangle.
Example 2:
Input:
R=2
C=2
M=[[-1,-2],[-3,-4]]
Output:
-1
Explanation:
Taking only the first cell is the
optimal choice.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maximumSumRectangle() which takes the number R, C, and the 2D matrix M as input parameters and returns the maximum sum submatrix.
Expected Time Complexity:O(R*R*C)
Expected Auxillary Space:O(R*C)
Constraints:
1<=R,C<=500
-1000<=M[i][j]<=1000 | class Solution:
def kadane(self, l, n):
s = 0
ans = l[0]
for i in range(n):
s += l[i]
ans = max(ans, s)
if s < 0:
s = 0
return ans
def maximumSumRectangle(self, R, C, M):
ans = -100000
s = 0
for i in range(C):
temp = [0] * R
for j in range(i, C):
for k in range(R):
temp[k] += M[k][j]
s = self.kadane(temp, R)
ans = max(s, ans)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given a 2D matrix M of dimensions RxC. Find the maximum sum submatrix in it.
Example 1:
Input:
R=4
C=5
M=[[1,2,-1,-4,-20],
[-8,-3,4,2,1],
[3,8,10,1,3],
[-4,-1,1,7,-6]]
Output:
29
Explanation:
The matrix is as follows and the
blue rectangle denotes the maximum sum
rectangle.
Example 2:
Input:
R=2
C=2
M=[[-1,-2],[-3,-4]]
Output:
-1
Explanation:
Taking only the first cell is the
optimal choice.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maximumSumRectangle() which takes the number R, C, and the 2D matrix M as input parameters and returns the maximum sum submatrix.
Expected Time Complexity:O(R*R*C)
Expected Auxillary Space:O(R*C)
Constraints:
1<=R,C<=500
-1000<=M[i][j]<=1000 | class Solution:
def maximumSumRectangle(self, R, C, M):
def max_sub_arr(arr):
s = ans = arr[0]
for i in range(1, C):
s += arr[i]
s = max(s, arr[i])
ans = max(ans, s)
return ans
ans = M[0][0]
for k in range(R):
a = [0] * C
for i in range(k, R):
for j in range(C):
a[j] += M[i][j]
ans = max(ans, max_sub_arr(a))
return ans | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR |
Given a 2D matrix M of dimensions RxC. Find the maximum sum submatrix in it.
Example 1:
Input:
R=4
C=5
M=[[1,2,-1,-4,-20],
[-8,-3,4,2,1],
[3,8,10,1,3],
[-4,-1,1,7,-6]]
Output:
29
Explanation:
The matrix is as follows and the
blue rectangle denotes the maximum sum
rectangle.
Example 2:
Input:
R=2
C=2
M=[[-1,-2],[-3,-4]]
Output:
-1
Explanation:
Taking only the first cell is the
optimal choice.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maximumSumRectangle() which takes the number R, C, and the 2D matrix M as input parameters and returns the maximum sum submatrix.
Expected Time Complexity:O(R*R*C)
Expected Auxillary Space:O(R*C)
Constraints:
1<=R,C<=500
-1000<=M[i][j]<=1000 | class Solution:
def maximumSumRectangle(self, R, C, M):
def kaden(temp):
curr = 0
maxx = max(temp)
for i in temp:
curr += i
maxx = max(maxx, curr)
if curr <= 0:
curr = 0
return maxx
maxx = float("-inf")
for c in range(C):
dp = [(0) for i in range(R)]
for i in range(c, C):
for r in range(R):
dp[r] = dp[r] + M[r][i]
maxx = max(maxx, kaden(dp))
return maxx | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR |
Given a 2D matrix M of dimensions RxC. Find the maximum sum submatrix in it.
Example 1:
Input:
R=4
C=5
M=[[1,2,-1,-4,-20],
[-8,-3,4,2,1],
[3,8,10,1,3],
[-4,-1,1,7,-6]]
Output:
29
Explanation:
The matrix is as follows and the
blue rectangle denotes the maximum sum
rectangle.
Example 2:
Input:
R=2
C=2
M=[[-1,-2],[-3,-4]]
Output:
-1
Explanation:
Taking only the first cell is the
optimal choice.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maximumSumRectangle() which takes the number R, C, and the 2D matrix M as input parameters and returns the maximum sum submatrix.
Expected Time Complexity:O(R*R*C)
Expected Auxillary Space:O(R*C)
Constraints:
1<=R,C<=500
-1000<=M[i][j]<=1000 | class Solution:
def maximumSumRectangle(self, R, C, M):
colSumMatrix = [([0] * C) for _ in range(R)]
for c in range(C):
curr = 0
for r in range(R):
curr += M[r][c]
colSumMatrix[r][c] = curr
ans = -float("inf")
for r1 in range(R):
for r2 in range(r1, R):
curr = 0
for c in range(C):
curr += colSumMatrix[r2][c] - (
colSumMatrix[r1 - 1][c] if r1 > 0 else 0
)
ans = max(ans, curr)
if curr < 0:
curr = 0
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER RETURN VAR |
Given a 2D matrix M of dimensions RxC. Find the maximum sum submatrix in it.
Example 1:
Input:
R=4
C=5
M=[[1,2,-1,-4,-20],
[-8,-3,4,2,1],
[3,8,10,1,3],
[-4,-1,1,7,-6]]
Output:
29
Explanation:
The matrix is as follows and the
blue rectangle denotes the maximum sum
rectangle.
Example 2:
Input:
R=2
C=2
M=[[-1,-2],[-3,-4]]
Output:
-1
Explanation:
Taking only the first cell is the
optimal choice.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maximumSumRectangle() which takes the number R, C, and the 2D matrix M as input parameters and returns the maximum sum submatrix.
Expected Time Complexity:O(R*R*C)
Expected Auxillary Space:O(R*C)
Constraints:
1<=R,C<=500
-1000<=M[i][j]<=1000 | class Solution:
def maximumSumRectangle(self, R, C, M):
def kadanes(arr):
currMax = float("-inf")
currMaxHere = 0
for num in arr:
currMaxHere += num
if currMax < currMaxHere:
currMax = currMaxHere
if currMaxHere < 0:
currMaxHere = 0
return currMax
sums = []
maxi = float("-inf")
for sCol in range(0, C):
sums = [0] * R
for eCol in range(sCol, C):
for row in range(R):
sums[row] += M[row][eCol]
res = kadanes(sums)
maxi = max(maxi, res)
return maxi | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER RETURN VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given a 2D matrix M of dimensions RxC. Find the maximum sum submatrix in it.
Example 1:
Input:
R=4
C=5
M=[[1,2,-1,-4,-20],
[-8,-3,4,2,1],
[3,8,10,1,3],
[-4,-1,1,7,-6]]
Output:
29
Explanation:
The matrix is as follows and the
blue rectangle denotes the maximum sum
rectangle.
Example 2:
Input:
R=2
C=2
M=[[-1,-2],[-3,-4]]
Output:
-1
Explanation:
Taking only the first cell is the
optimal choice.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maximumSumRectangle() which takes the number R, C, and the 2D matrix M as input parameters and returns the maximum sum submatrix.
Expected Time Complexity:O(R*R*C)
Expected Auxillary Space:O(R*C)
Constraints:
1<=R,C<=500
-1000<=M[i][j]<=1000 | class Solution:
def kadane(self, ans):
m, s = float("-inf"), 0
for i in range(len(ans)):
s += ans[i]
if s > m:
m = s
if s < 0:
s = 0
return m
def maximumSumRectangle(self, R, C, M):
m = float("-inf")
for i in range(R):
ans = [0] * C
for j in range(i, R):
for k in range(C):
ans[k] += M[j][k]
m = max(m, self.kadane(ans))
return m | CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR STRING NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR |
Given a 2D matrix M of dimensions RxC. Find the maximum sum submatrix in it.
Example 1:
Input:
R=4
C=5
M=[[1,2,-1,-4,-20],
[-8,-3,4,2,1],
[3,8,10,1,3],
[-4,-1,1,7,-6]]
Output:
29
Explanation:
The matrix is as follows and the
blue rectangle denotes the maximum sum
rectangle.
Example 2:
Input:
R=2
C=2
M=[[-1,-2],[-3,-4]]
Output:
-1
Explanation:
Taking only the first cell is the
optimal choice.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maximumSumRectangle() which takes the number R, C, and the 2D matrix M as input parameters and returns the maximum sum submatrix.
Expected Time Complexity:O(R*R*C)
Expected Auxillary Space:O(R*C)
Constraints:
1<=R,C<=500
-1000<=M[i][j]<=1000 | class Solution:
def kadane(self, ans):
cur_sum = 0
best_sum = float("-inf")
for val in ans:
if cur_sum <= 0:
cur_sum = val
else:
cur_sum += val
best_sum = max(best_sum, cur_sum)
return best_sum
def maximumSumRectangle(self, R, C, M):
maxVal = float("-inf")
for i in range(R):
ans = [0] * C
for j in range(i, R):
for elem in range(C):
ans[elem] += M[j][elem]
maxVal = max(maxVal, self.kadane(ans))
return maxVal | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR |
Given a 2D matrix M of dimensions RxC. Find the maximum sum submatrix in it.
Example 1:
Input:
R=4
C=5
M=[[1,2,-1,-4,-20],
[-8,-3,4,2,1],
[3,8,10,1,3],
[-4,-1,1,7,-6]]
Output:
29
Explanation:
The matrix is as follows and the
blue rectangle denotes the maximum sum
rectangle.
Example 2:
Input:
R=2
C=2
M=[[-1,-2],[-3,-4]]
Output:
-1
Explanation:
Taking only the first cell is the
optimal choice.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maximumSumRectangle() which takes the number R, C, and the 2D matrix M as input parameters and returns the maximum sum submatrix.
Expected Time Complexity:O(R*R*C)
Expected Auxillary Space:O(R*C)
Constraints:
1<=R,C<=500
-1000<=M[i][j]<=1000 | class Solution:
def rowWiseKedan(self, mat):
summ = 0
maxSum = float("-inf")
for i in range(len(mat)):
summ += mat[i]
maxSum = max(maxSum, summ)
if summ < 0:
summ = 0
return maxSum
def maximumSumRectangle(self, R, C, A):
n = len(A)
m = len(A[0])
maxSum = float("-inf")
for i in range(m):
temp = [0] * n
for j in range(i, m):
for k in range(n):
temp[k] += A[k][j]
maxSum = max(maxSum, self.rowWiseKedan(temp))
return maxSum | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR |
Given a 2D matrix M of dimensions RxC. Find the maximum sum submatrix in it.
Example 1:
Input:
R=4
C=5
M=[[1,2,-1,-4,-20],
[-8,-3,4,2,1],
[3,8,10,1,3],
[-4,-1,1,7,-6]]
Output:
29
Explanation:
The matrix is as follows and the
blue rectangle denotes the maximum sum
rectangle.
Example 2:
Input:
R=2
C=2
M=[[-1,-2],[-3,-4]]
Output:
-1
Explanation:
Taking only the first cell is the
optimal choice.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maximumSumRectangle() which takes the number R, C, and the 2D matrix M as input parameters and returns the maximum sum submatrix.
Expected Time Complexity:O(R*R*C)
Expected Auxillary Space:O(R*C)
Constraints:
1<=R,C<=500
-1000<=M[i][j]<=1000 | class Solution:
def kadane(self, nums, n):
sum_ = 0
max_ = -1000000000000
for i in range(n):
sum_ = max(sum_ + nums[i], nums[i])
max_ = max(sum_, max_)
return max_
def find_max(self, M, m, n, x):
temp = [(0) for _ in range(n)]
for i in range(x, m):
for j in range(n):
temp[j] += M[i][j]
temp_max = self.kadane(temp, n)
self.max_ = max(self.max_, temp_max)
return
def maximumSumRectangle(self, R, C, M):
self.max_ = -100000000000
for i in range(R):
self.find_max(M, R, C, i)
return self.max_ | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR |
Given a 2D matrix M of dimensions RxC. Find the maximum sum submatrix in it.
Example 1:
Input:
R=4
C=5
M=[[1,2,-1,-4,-20],
[-8,-3,4,2,1],
[3,8,10,1,3],
[-4,-1,1,7,-6]]
Output:
29
Explanation:
The matrix is as follows and the
blue rectangle denotes the maximum sum
rectangle.
Example 2:
Input:
R=2
C=2
M=[[-1,-2],[-3,-4]]
Output:
-1
Explanation:
Taking only the first cell is the
optimal choice.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maximumSumRectangle() which takes the number R, C, and the 2D matrix M as input parameters and returns the maximum sum submatrix.
Expected Time Complexity:O(R*R*C)
Expected Auxillary Space:O(R*C)
Constraints:
1<=R,C<=500
-1000<=M[i][j]<=1000 | class Solution:
def maximumSumRectangle(self, R, C, M):
arr = [float("-inf")] * R
maxSum = float("-inf")
for cstart in range(0, C):
arr = [0] * R
for cend in range(cstart, C):
for r in range(0, R):
arr[r] += M[r][cend]
cursum = self.kadane(arr)
maxSum = max(cursum, maxSum)
return maxSum
def kadane(self, arr):
curSum = 0
maxSum = float("-inf")
for i in range(0, len(arr)):
curSum += arr[i]
if curSum > maxSum:
maxSum = curSum
if curSum < 0:
curSum = 0
return maxSum | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER RETURN VAR |
Given a 2D matrix M of dimensions RxC. Find the maximum sum submatrix in it.
Example 1:
Input:
R=4
C=5
M=[[1,2,-1,-4,-20],
[-8,-3,4,2,1],
[3,8,10,1,3],
[-4,-1,1,7,-6]]
Output:
29
Explanation:
The matrix is as follows and the
blue rectangle denotes the maximum sum
rectangle.
Example 2:
Input:
R=2
C=2
M=[[-1,-2],[-3,-4]]
Output:
-1
Explanation:
Taking only the first cell is the
optimal choice.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maximumSumRectangle() which takes the number R, C, and the 2D matrix M as input parameters and returns the maximum sum submatrix.
Expected Time Complexity:O(R*R*C)
Expected Auxillary Space:O(R*C)
Constraints:
1<=R,C<=500
-1000<=M[i][j]<=1000 | class Solution:
def maximumSumRectangle(self, R, C, M):
pre = [([0] * (C + 1)) for i in range(0, R)]
ans = -1000000000000000
for i in range(0, R):
for j in range(0, C):
pre[i][j + 1] += pre[i][j] + M[i][j]
ans = max(ans, M[i][j])
for i in range(0, C):
for j in range(i, C):
sy, ey = i, j + 1
ca = -1000000000000000
mtt = -1000000000000000
for k in range(0, R):
cv = pre[k][ey] - pre[k][sy]
mtt = max(cv, cv + mtt)
ca = max(ca, mtt)
ans = max(ans, ca)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given a 2D matrix M of dimensions RxC. Find the maximum sum submatrix in it.
Example 1:
Input:
R=4
C=5
M=[[1,2,-1,-4,-20],
[-8,-3,4,2,1],
[3,8,10,1,3],
[-4,-1,1,7,-6]]
Output:
29
Explanation:
The matrix is as follows and the
blue rectangle denotes the maximum sum
rectangle.
Example 2:
Input:
R=2
C=2
M=[[-1,-2],[-3,-4]]
Output:
-1
Explanation:
Taking only the first cell is the
optimal choice.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maximumSumRectangle() which takes the number R, C, and the 2D matrix M as input parameters and returns the maximum sum submatrix.
Expected Time Complexity:O(R*R*C)
Expected Auxillary Space:O(R*C)
Constraints:
1<=R,C<=500
-1000<=M[i][j]<=1000 | class Solution:
def kadane(self, q):
dp = [-float("inf")] * len(q)
dp[0] = q[0]
for i in range(1, len(q)):
dp[i] = max(q[i], dp[i - 1] + q[i])
return max(dp)
def maximumSumRectangle(self, r, c, mat):
ans = -float("inf")
for i in range(r):
q = [0] * c
for j in range(i, r):
q = [sum(x) for x in zip(q, mat[j])]
ans = max(ans, self.kadane(q))
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR RETURN FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR |
Given a 2D matrix M of dimensions RxC. Find the maximum sum submatrix in it.
Example 1:
Input:
R=4
C=5
M=[[1,2,-1,-4,-20],
[-8,-3,4,2,1],
[3,8,10,1,3],
[-4,-1,1,7,-6]]
Output:
29
Explanation:
The matrix is as follows and the
blue rectangle denotes the maximum sum
rectangle.
Example 2:
Input:
R=2
C=2
M=[[-1,-2],[-3,-4]]
Output:
-1
Explanation:
Taking only the first cell is the
optimal choice.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maximumSumRectangle() which takes the number R, C, and the 2D matrix M as input parameters and returns the maximum sum submatrix.
Expected Time Complexity:O(R*R*C)
Expected Auxillary Space:O(R*C)
Constraints:
1<=R,C<=500
-1000<=M[i][j]<=1000 | class Solution:
def maximumSumRectangle(self, R, C, M):
max_so_far = -1000000
l = 0
r = 0
for l in range(0, C):
sub = [0] * R
for r in range(l, C):
for i in range(0, len(sub)):
sub[i] = sub[i] + M[i][r]
currsum = 0
for i in range(0, len(sub)):
currsum = currsum + sub[i]
if currsum > max_so_far:
max_so_far = currsum
if currsum < 0:
currsum = 0
return max_so_far | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER RETURN VAR |
Given a 2D matrix M of dimensions RxC. Find the maximum sum submatrix in it.
Example 1:
Input:
R=4
C=5
M=[[1,2,-1,-4,-20],
[-8,-3,4,2,1],
[3,8,10,1,3],
[-4,-1,1,7,-6]]
Output:
29
Explanation:
The matrix is as follows and the
blue rectangle denotes the maximum sum
rectangle.
Example 2:
Input:
R=2
C=2
M=[[-1,-2],[-3,-4]]
Output:
-1
Explanation:
Taking only the first cell is the
optimal choice.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maximumSumRectangle() which takes the number R, C, and the 2D matrix M as input parameters and returns the maximum sum submatrix.
Expected Time Complexity:O(R*R*C)
Expected Auxillary Space:O(R*C)
Constraints:
1<=R,C<=500
-1000<=M[i][j]<=1000 | class Solution:
def maximumSumRectangle(self, R, C, M):
def kadane(cols, start_finish, n):
max_sum = -math.inf
local_start = -1
start_finish = [-1, -1]
cur_sum = 0
for i in range(n):
cur_sum += cols[i]
if cur_sum < 0:
cur_sum = 0
local_start = i + 1
elif cur_sum > max_sum:
start_finish = [local_start, i]
max_sum = cur_sum
if start_finish[-1] != -1:
return max_sum
max_sum = cols[0]
local_start = 0
start_finish = [0][0]
for i in range(1, n):
if max_sum < cols[i]:
max_sum = cols[i]
start_finish = [i, i]
return max_sum
final_top, final_left, final_bottom, final_right = (None, None, None, None)
max_sum = -math.inf
start_finish = [-1, -1]
for left in range(C):
cols = [0] * R
for right in range(left, C):
for i in range(R):
cols[i] += M[i][right]
cur_sum = kadane(cols, start_finish, R)
if cur_sum > max_sum:
max_sum = cur_sum
final_top, final_bottom = start_finish
final_left = left
final_right = right
return max_sum | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR LIST VAR VAR ASSIGN VAR VAR IF VAR NUMBER NUMBER RETURN VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR LIST VAR VAR RETURN VAR ASSIGN VAR VAR VAR VAR NONE NONE NONE NONE ASSIGN VAR VAR ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR |
Given a 2D matrix M of dimensions RxC. Find the maximum sum submatrix in it.
Example 1:
Input:
R=4
C=5
M=[[1,2,-1,-4,-20],
[-8,-3,4,2,1],
[3,8,10,1,3],
[-4,-1,1,7,-6]]
Output:
29
Explanation:
The matrix is as follows and the
blue rectangle denotes the maximum sum
rectangle.
Example 2:
Input:
R=2
C=2
M=[[-1,-2],[-3,-4]]
Output:
-1
Explanation:
Taking only the first cell is the
optimal choice.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maximumSumRectangle() which takes the number R, C, and the 2D matrix M as input parameters and returns the maximum sum submatrix.
Expected Time Complexity:O(R*R*C)
Expected Auxillary Space:O(R*C)
Constraints:
1<=R,C<=500
-1000<=M[i][j]<=1000 | class Solution:
def maximumSumRectangle(self, R, C, M):
maxVal = -float("inf")
for cStart in range(C):
temp = [(0) for _ in range(R)]
for cEnd in range(cStart, C):
for r in range(R):
temp[r] += M[r][cEnd]
currSum = 0
for elem in temp:
currSum += elem
maxVal = max(maxVal, currSum)
if currSum < 0:
currSum = 0
return maxVal | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER RETURN VAR |
Given a 2D matrix M of dimensions RxC. Find the maximum sum submatrix in it.
Example 1:
Input:
R=4
C=5
M=[[1,2,-1,-4,-20],
[-8,-3,4,2,1],
[3,8,10,1,3],
[-4,-1,1,7,-6]]
Output:
29
Explanation:
The matrix is as follows and the
blue rectangle denotes the maximum sum
rectangle.
Example 2:
Input:
R=2
C=2
M=[[-1,-2],[-3,-4]]
Output:
-1
Explanation:
Taking only the first cell is the
optimal choice.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maximumSumRectangle() which takes the number R, C, and the 2D matrix M as input parameters and returns the maximum sum submatrix.
Expected Time Complexity:O(R*R*C)
Expected Auxillary Space:O(R*C)
Constraints:
1<=R,C<=500
-1000<=M[i][j]<=1000 | class Solution:
def maximumSumRectangle(self, R, C, matrix):
rows, cols = R, C
maxsum, left, right, top, bottom = -float("inf"), 0, 0, 0, 0
temp = [0] * rows
for left in range(cols):
temp = [0] * rows
for right in range(left, cols):
for i in range(rows):
temp[i] += matrix[i][right]
cursum = 0
for i in range(R):
cursum += temp[i]
maxsum = max(maxsum, cursum)
cursum = max(cursum, 0)
return maxsum | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR STRING NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER RETURN VAR |
Given a 2D matrix M of dimensions RxC. Find the maximum sum submatrix in it.
Example 1:
Input:
R=4
C=5
M=[[1,2,-1,-4,-20],
[-8,-3,4,2,1],
[3,8,10,1,3],
[-4,-1,1,7,-6]]
Output:
29
Explanation:
The matrix is as follows and the
blue rectangle denotes the maximum sum
rectangle.
Example 2:
Input:
R=2
C=2
M=[[-1,-2],[-3,-4]]
Output:
-1
Explanation:
Taking only the first cell is the
optimal choice.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maximumSumRectangle() which takes the number R, C, and the 2D matrix M as input parameters and returns the maximum sum submatrix.
Expected Time Complexity:O(R*R*C)
Expected Auxillary Space:O(R*C)
Constraints:
1<=R,C<=500
-1000<=M[i][j]<=1000 | class Solution:
def maximumSumRectangle(self, R, C, M):
if M == 1 and C == 1:
return M[0][0]
for i in range(1, R):
for j in range(C):
M[i][j] += M[i - 1][j]
arr = [0] * C
max_area = float("-inf")
for l in range(1, R + 1):
for i in range(R - l + 1):
for j in range(C):
arr[j] = (
M[i + l - 1][j] - M[i - 1][j] if i - 1 >= 0 else M[i + l - 1][j]
)
currsum = 0
maxsum = float("-inf")
for k in range(C):
currsum += arr[k]
if currsum > maxsum:
maxsum = currsum
if currsum < 0:
currsum = 0
if maxsum > max_area:
max_area = maxsum
return max_area | CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR RETURN VAR |
Given a 2D matrix M of dimensions RxC. Find the maximum sum submatrix in it.
Example 1:
Input:
R=4
C=5
M=[[1,2,-1,-4,-20],
[-8,-3,4,2,1],
[3,8,10,1,3],
[-4,-1,1,7,-6]]
Output:
29
Explanation:
The matrix is as follows and the
blue rectangle denotes the maximum sum
rectangle.
Example 2:
Input:
R=2
C=2
M=[[-1,-2],[-3,-4]]
Output:
-1
Explanation:
Taking only the first cell is the
optimal choice.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maximumSumRectangle() which takes the number R, C, and the 2D matrix M as input parameters and returns the maximum sum submatrix.
Expected Time Complexity:O(R*R*C)
Expected Auxillary Space:O(R*C)
Constraints:
1<=R,C<=500
-1000<=M[i][j]<=1000 | class Solution:
def maximumSumRectangle(self, R, C, M):
def kadane(a):
max_sum = a[0]
cur_sum = a[0]
for i in range(1, len(a)):
cur_sum = max(a[i], cur_sum + a[i])
max_sum = max(max_sum, cur_sum)
return max_sum
gsum = float("-inf")
for left in range(len(M[0])):
temp = [0] * len(M)
for right in range(left, len(M[0])):
for i in range(len(M)):
temp[i] += M[i][right]
Sum = kadane(temp)
if Sum > gsum:
gsum = Sum
return gsum | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR |
Given a 2D matrix M of dimensions RxC. Find the maximum sum submatrix in it.
Example 1:
Input:
R=4
C=5
M=[[1,2,-1,-4,-20],
[-8,-3,4,2,1],
[3,8,10,1,3],
[-4,-1,1,7,-6]]
Output:
29
Explanation:
The matrix is as follows and the
blue rectangle denotes the maximum sum
rectangle.
Example 2:
Input:
R=2
C=2
M=[[-1,-2],[-3,-4]]
Output:
-1
Explanation:
Taking only the first cell is the
optimal choice.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maximumSumRectangle() which takes the number R, C, and the 2D matrix M as input parameters and returns the maximum sum submatrix.
Expected Time Complexity:O(R*R*C)
Expected Auxillary Space:O(R*C)
Constraints:
1<=R,C<=500
-1000<=M[i][j]<=1000 | class Solution:
def kadane(self, arr, start, finish, n):
Sum = 0
maxSum = -999999999999
i = None
finish[0] = -1
local_start = 0
for i in range(n):
Sum += arr[i]
if Sum < 0:
Sum = 0
local_start = i + 1
elif Sum > maxSum:
maxSum = Sum
start[0] = local_start
finish[0] = i
if finish[0] != -1:
return maxSum
maxSum = arr[0]
start[0] = finish[0] = 0
for i in range(1, n):
if arr[i] > maxSum:
maxSum = arr[i]
start[0] = finish[0] = i
return maxSum
def maximumSumRectangle(self, ROW, COL, M):
maxSum, finalLeft = -999999999999, None
finalRight, finalTop, finalBottom = None, None, None
left, right, i = None, None, None
temp = [None] * ROW
Sum = 0
start = [0]
finish = [0]
for left in range(COL):
temp = [0] * ROW
for right in range(left, COL):
for i in range(ROW):
temp[i] += M[i][right]
Sum = self.kadane(temp, start, finish, ROW)
if Sum > maxSum:
maxSum = Sum
finalLeft = left
finalRight = right
finalTop = start[0]
finalBottom = finish[0]
return maxSum | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NONE ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER VAR IF VAR NUMBER NUMBER RETURN VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR NUMBER NONE ASSIGN VAR VAR VAR NONE NONE NONE ASSIGN VAR VAR VAR NONE NONE NONE ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER RETURN VAR |
Given a 2D matrix M of dimensions RxC. Find the maximum sum submatrix in it.
Example 1:
Input:
R=4
C=5
M=[[1,2,-1,-4,-20],
[-8,-3,4,2,1],
[3,8,10,1,3],
[-4,-1,1,7,-6]]
Output:
29
Explanation:
The matrix is as follows and the
blue rectangle denotes the maximum sum
rectangle.
Example 2:
Input:
R=2
C=2
M=[[-1,-2],[-3,-4]]
Output:
-1
Explanation:
Taking only the first cell is the
optimal choice.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maximumSumRectangle() which takes the number R, C, and the 2D matrix M as input parameters and returns the maximum sum submatrix.
Expected Time Complexity:O(R*R*C)
Expected Auxillary Space:O(R*C)
Constraints:
1<=R,C<=500
-1000<=M[i][j]<=1000 | class Solution:
def kadane(self, arr, n):
dp = [x for x in arr]
maxi = dp[0]
for i in range(1, n):
if dp[i] < dp[i - 1] + arr[i]:
dp[i] = dp[i - 1] + arr[i]
maxi = max(maxi, dp[i])
return maxi
def maximumSumRectangle(self, R, C, M):
arr = [(0) for x in range(R)]
answer = -float("inf")
for j in range(C):
for i in range(R):
arr[i] = M[i][j]
answer = max(answer, self.kadane(arr, R))
for k in range(j + 1, C):
for i in range(R):
arr[i] += M[i][k]
answer = max(answer, self.kadane(arr, R))
return answer | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given a 2D matrix M of dimensions RxC. Find the maximum sum submatrix in it.
Example 1:
Input:
R=4
C=5
M=[[1,2,-1,-4,-20],
[-8,-3,4,2,1],
[3,8,10,1,3],
[-4,-1,1,7,-6]]
Output:
29
Explanation:
The matrix is as follows and the
blue rectangle denotes the maximum sum
rectangle.
Example 2:
Input:
R=2
C=2
M=[[-1,-2],[-3,-4]]
Output:
-1
Explanation:
Taking only the first cell is the
optimal choice.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maximumSumRectangle() which takes the number R, C, and the 2D matrix M as input parameters and returns the maximum sum submatrix.
Expected Time Complexity:O(R*R*C)
Expected Auxillary Space:O(R*C)
Constraints:
1<=R,C<=500
-1000<=M[i][j]<=1000 | class Solution:
def maximumSumRectangle(self, R, C, M):
sum = []
for i in range(R):
sum.append([])
for j in range(C):
if i == 0:
sum[i].append(M[0][j])
else:
sum[i].append(sum[i - 1][j] + M[i][j])
max_sum = None
for i in range(R):
for j in range(R):
if i <= j:
temp = []
if i != 0:
for c_pos in range(C):
temp.append(sum[j][c_pos] - sum[i - 1][c_pos])
else:
temp = sum[j]
local_max = None
current_sum = 0
for pos in range(C):
current_sum += temp[pos]
if not local_max:
local_max = current_sum
local_max = max(current_sum, local_max)
if current_sum <= 0:
current_sum = 0
if not max_sum:
max_sum = local_max
max_sum = max(max_sum, local_max)
return max_sum | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR NONE FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR LIST IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR NONE ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given a 2D matrix M of dimensions RxC. Find the maximum sum submatrix in it.
Example 1:
Input:
R=4
C=5
M=[[1,2,-1,-4,-20],
[-8,-3,4,2,1],
[3,8,10,1,3],
[-4,-1,1,7,-6]]
Output:
29
Explanation:
The matrix is as follows and the
blue rectangle denotes the maximum sum
rectangle.
Example 2:
Input:
R=2
C=2
M=[[-1,-2],[-3,-4]]
Output:
-1
Explanation:
Taking only the first cell is the
optimal choice.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maximumSumRectangle() which takes the number R, C, and the 2D matrix M as input parameters and returns the maximum sum submatrix.
Expected Time Complexity:O(R*R*C)
Expected Auxillary Space:O(R*C)
Constraints:
1<=R,C<=500
-1000<=M[i][j]<=1000 | class Solution:
def maximumSumRectangle(self, R, C, M):
long = [[(0) for i in range(C)] for j in range(R + 1)]
long[1] = M[0]
for i in range(1, R):
for j in range(C):
long[i + 1][j] += long[i][j] + M[i][j]
m = -(10**9)
for i in range(1, R + 1):
for x in range(i, R + 1):
t = 0
for y in range(C):
t += long[x][y] - long[i - 1][y]
m = max(m, t)
if t < 0:
t = 0
return m | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER RETURN VAR |
Given a 2D matrix M of dimensions RxC. Find the maximum sum submatrix in it.
Example 1:
Input:
R=4
C=5
M=[[1,2,-1,-4,-20],
[-8,-3,4,2,1],
[3,8,10,1,3],
[-4,-1,1,7,-6]]
Output:
29
Explanation:
The matrix is as follows and the
blue rectangle denotes the maximum sum
rectangle.
Example 2:
Input:
R=2
C=2
M=[[-1,-2],[-3,-4]]
Output:
-1
Explanation:
Taking only the first cell is the
optimal choice.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maximumSumRectangle() which takes the number R, C, and the 2D matrix M as input parameters and returns the maximum sum submatrix.
Expected Time Complexity:O(R*R*C)
Expected Auxillary Space:O(R*C)
Constraints:
1<=R,C<=500
-1000<=M[i][j]<=1000 | class Solution:
def maximumSumRectangle(self, R, C, M):
result = float("-inf")
for left in range(C):
row_sums = [0] * R
for right in range(left, C):
for row in range(R):
row_sums[row] += M[row][right]
curr_sum = row_sums[0]
rectangle_sum = row_sums[0]
for num in row_sums[1:]:
curr_sum = max(curr_sum + num, num)
rectangle_sum = max(rectangle_sum, curr_sum)
result = max(result, rectangle_sum)
return result | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given a 2D matrix M of dimensions RxC. Find the maximum sum submatrix in it.
Example 1:
Input:
R=4
C=5
M=[[1,2,-1,-4,-20],
[-8,-3,4,2,1],
[3,8,10,1,3],
[-4,-1,1,7,-6]]
Output:
29
Explanation:
The matrix is as follows and the
blue rectangle denotes the maximum sum
rectangle.
Example 2:
Input:
R=2
C=2
M=[[-1,-2],[-3,-4]]
Output:
-1
Explanation:
Taking only the first cell is the
optimal choice.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maximumSumRectangle() which takes the number R, C, and the 2D matrix M as input parameters and returns the maximum sum submatrix.
Expected Time Complexity:O(R*R*C)
Expected Auxillary Space:O(R*C)
Constraints:
1<=R,C<=500
-1000<=M[i][j]<=1000 | class Solution:
def maximumSumRectangle(self, R, C, M):
ans = -99999
for i in range(C):
arr = [0] * R
for j in range(i, C):
for k in range(R):
arr[k] = arr[k] + M[k][j]
curr_sum = self.submatrix(arr)
ans = max(curr_sum, ans)
return ans
def submatrix(self, arr):
mx = -999
sum = 0
for i in arr:
sum = sum + i
mx = max(sum, mx)
if sum < 0:
sum = 0
return mx | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER RETURN VAR |
Given a 2D matrix M of dimensions RxC. Find the maximum sum submatrix in it.
Example 1:
Input:
R=4
C=5
M=[[1,2,-1,-4,-20],
[-8,-3,4,2,1],
[3,8,10,1,3],
[-4,-1,1,7,-6]]
Output:
29
Explanation:
The matrix is as follows and the
blue rectangle denotes the maximum sum
rectangle.
Example 2:
Input:
R=2
C=2
M=[[-1,-2],[-3,-4]]
Output:
-1
Explanation:
Taking only the first cell is the
optimal choice.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maximumSumRectangle() which takes the number R, C, and the 2D matrix M as input parameters and returns the maximum sum submatrix.
Expected Time Complexity:O(R*R*C)
Expected Auxillary Space:O(R*C)
Constraints:
1<=R,C<=500
-1000<=M[i][j]<=1000 | from sys import maxsize
def kadanealgo(arr, n):
Sum = 0
maxSum = -maxsize
for i in range(n):
Sum += arr[i]
if maxSum < Sum:
maxSum = Sum
if Sum < 0:
Sum = 0
return maxSum
class Solution:
def maximumSumRectangle(self, n, m, arr):
maxSum = -maxsize
temp = [0] * n
for left in range(m):
for i in range(n):
temp[i] = 0
for right in range(left, m):
for i in range(n):
temp[i] += arr[i][right]
Sum = kadanealgo(temp, n)
if maxSum < Sum:
maxSum = Sum
return maxSum | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR |
Given a 2D matrix M of dimensions RxC. Find the maximum sum submatrix in it.
Example 1:
Input:
R=4
C=5
M=[[1,2,-1,-4,-20],
[-8,-3,4,2,1],
[3,8,10,1,3],
[-4,-1,1,7,-6]]
Output:
29
Explanation:
The matrix is as follows and the
blue rectangle denotes the maximum sum
rectangle.
Example 2:
Input:
R=2
C=2
M=[[-1,-2],[-3,-4]]
Output:
-1
Explanation:
Taking only the first cell is the
optimal choice.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maximumSumRectangle() which takes the number R, C, and the 2D matrix M as input parameters and returns the maximum sum submatrix.
Expected Time Complexity:O(R*R*C)
Expected Auxillary Space:O(R*C)
Constraints:
1<=R,C<=500
-1000<=M[i][j]<=1000 | class Solution:
def maximumSumRectangle(self, R, C, M):
def kadaneAlgo(nums):
cur_sum = 0
max_sum = -99999999
for num in nums:
cur_sum += num
max_sum = max(max_sum, cur_sum)
if cur_sum < 0:
cur_sum = 0
return max_sum
m = len(M)
n = len(M[0])
nums = [(0) for _ in range(n)]
res = -9999999999
for i in range(m):
for j in range(i, m):
if i == j:
nums = M[i]
res = max(res, kadaneAlgo(M[i]))
else:
for k in range(n):
nums[k] = nums[k] + M[j][k]
res = max(res, kadaneAlgo(nums))
return res | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR |
Given a 2D matrix M of dimensions RxC. Find the maximum sum submatrix in it.
Example 1:
Input:
R=4
C=5
M=[[1,2,-1,-4,-20],
[-8,-3,4,2,1],
[3,8,10,1,3],
[-4,-1,1,7,-6]]
Output:
29
Explanation:
The matrix is as follows and the
blue rectangle denotes the maximum sum
rectangle.
Example 2:
Input:
R=2
C=2
M=[[-1,-2],[-3,-4]]
Output:
-1
Explanation:
Taking only the first cell is the
optimal choice.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maximumSumRectangle() which takes the number R, C, and the 2D matrix M as input parameters and returns the maximum sum submatrix.
Expected Time Complexity:O(R*R*C)
Expected Auxillary Space:O(R*C)
Constraints:
1<=R,C<=500
-1000<=M[i][j]<=1000 | class Solution:
def maximumSumRectangle(self, R, C, M):
cs = [([0] * (C + 1)) for _ in range(R)]
for r in range(R):
for c in range(1, C + 1):
cs[r][c] = M[r][c - 1] + cs[r][c - 1]
max_sum = -float("inf")
for right in range(C):
for left in range(right + 1):
cur_sum = 0
max_sum_right_left = -float("inf")
for row in range(R):
cur_sum += cs[row][right + 1] - cs[row][left]
max_sum_right_left = max(max_sum_right_left, cur_sum)
if cur_sum < 0:
cur_sum = 0
max_sum = max(max_sum, max_sum_right_left)
return max_sum | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given a 2D matrix M of dimensions RxC. Find the maximum sum submatrix in it.
Example 1:
Input:
R=4
C=5
M=[[1,2,-1,-4,-20],
[-8,-3,4,2,1],
[3,8,10,1,3],
[-4,-1,1,7,-6]]
Output:
29
Explanation:
The matrix is as follows and the
blue rectangle denotes the maximum sum
rectangle.
Example 2:
Input:
R=2
C=2
M=[[-1,-2],[-3,-4]]
Output:
-1
Explanation:
Taking only the first cell is the
optimal choice.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maximumSumRectangle() which takes the number R, C, and the 2D matrix M as input parameters and returns the maximum sum submatrix.
Expected Time Complexity:O(R*R*C)
Expected Auxillary Space:O(R*C)
Constraints:
1<=R,C<=500
-1000<=M[i][j]<=1000 | class Solution:
def maxSubArray(self, nums):
n = len(nums)
chain = nums[0]
ans = nums[0]
for i in range(1, n):
if nums[i] + chain > nums[i]:
chain = nums[i] + chain
else:
chain = nums[i]
ans = max(ans, chain)
return ans
def maximumSumRectangle(self, R, C, M):
ans = -999999999999
for start in range(R):
temp = [0] * C
for end in range(start, R):
for j in range(C):
temp[j] = temp[j] + M[end][j]
res = self.maxSubArray(temp)
ans = max(ans, res)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given a 2D matrix M of dimensions RxC. Find the maximum sum submatrix in it.
Example 1:
Input:
R=4
C=5
M=[[1,2,-1,-4,-20],
[-8,-3,4,2,1],
[3,8,10,1,3],
[-4,-1,1,7,-6]]
Output:
29
Explanation:
The matrix is as follows and the
blue rectangle denotes the maximum sum
rectangle.
Example 2:
Input:
R=2
C=2
M=[[-1,-2],[-3,-4]]
Output:
-1
Explanation:
Taking only the first cell is the
optimal choice.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maximumSumRectangle() which takes the number R, C, and the 2D matrix M as input parameters and returns the maximum sum submatrix.
Expected Time Complexity:O(R*R*C)
Expected Auxillary Space:O(R*C)
Constraints:
1<=R,C<=500
-1000<=M[i][j]<=1000 | class Solution:
def maximumSumRectangle(self, R, C, M):
maxx = M[0][0]
for start in range(R):
list_l = [0] * C
for end in range(start, R):
for i in range(C):
list_l[i] = list_l[i] + M[end][i]
cur_max = 0
maxx_loc = M[0][0]
for i in range(C):
cur_max += list_l[i]
maxx_loc = max(maxx_loc, cur_max)
if cur_max < 0:
cur_max = 0
maxx = max(maxx_loc, maxx)
return maxx | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given a 2D matrix M of dimensions RxC. Find the maximum sum submatrix in it.
Example 1:
Input:
R=4
C=5
M=[[1,2,-1,-4,-20],
[-8,-3,4,2,1],
[3,8,10,1,3],
[-4,-1,1,7,-6]]
Output:
29
Explanation:
The matrix is as follows and the
blue rectangle denotes the maximum sum
rectangle.
Example 2:
Input:
R=2
C=2
M=[[-1,-2],[-3,-4]]
Output:
-1
Explanation:
Taking only the first cell is the
optimal choice.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maximumSumRectangle() which takes the number R, C, and the 2D matrix M as input parameters and returns the maximum sum submatrix.
Expected Time Complexity:O(R*R*C)
Expected Auxillary Space:O(R*C)
Constraints:
1<=R,C<=500
-1000<=M[i][j]<=1000 | class Solution:
def maximumSumRectangle(self, R, C, A):
def kadane(v):
currSum = 0
maxSum = -sys.maxsize - 1
for i in range(len(v)):
currSum = max(currSum + v[i], v[i])
maxSum = max(maxSum, currSum)
return maxSum
r = len(A)
c = len(A[0])
prefix = [[(0) for i in range(c)] for j in range(r)]
for i in range(r):
for j in range(c):
if j == 0:
prefix[i][j] = A[i][j]
else:
prefix[i][j] = A[i][j] + prefix[i][j - 1]
maxSum = -sys.maxsize - 1
for i in range(c):
for j in range(i, c):
v = []
for k in range(r):
el = 0
if i == 0:
el = prefix[k][j]
else:
el = prefix[k][j] - prefix[k][i - 1]
v.append(el)
maxSum = max(maxSum, kadane(v))
return maxSum | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR |
Given a 2D matrix M of dimensions RxC. Find the maximum sum submatrix in it.
Example 1:
Input:
R=4
C=5
M=[[1,2,-1,-4,-20],
[-8,-3,4,2,1],
[3,8,10,1,3],
[-4,-1,1,7,-6]]
Output:
29
Explanation:
The matrix is as follows and the
blue rectangle denotes the maximum sum
rectangle.
Example 2:
Input:
R=2
C=2
M=[[-1,-2],[-3,-4]]
Output:
-1
Explanation:
Taking only the first cell is the
optimal choice.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maximumSumRectangle() which takes the number R, C, and the 2D matrix M as input parameters and returns the maximum sum submatrix.
Expected Time Complexity:O(R*R*C)
Expected Auxillary Space:O(R*C)
Constraints:
1<=R,C<=500
-1000<=M[i][j]<=1000 | class Solution:
def maximumSumRectangle(self, R, C, M):
for r in M:
for i in range(1, C):
r[i] += r[i - 1]
ans = float("-inf")
for l in range(C):
for r in range(l, C):
running = 0
for i in range(R):
v = M[i][r]
if l > 0:
v -= M[i][l - 1]
running += v
ans = max(ans, running)
if running < 0:
running = 0
return ans | CLASS_DEF FUNC_DEF FOR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER RETURN VAR |
Given a 2D matrix M of dimensions RxC. Find the maximum sum submatrix in it.
Example 1:
Input:
R=4
C=5
M=[[1,2,-1,-4,-20],
[-8,-3,4,2,1],
[3,8,10,1,3],
[-4,-1,1,7,-6]]
Output:
29
Explanation:
The matrix is as follows and the
blue rectangle denotes the maximum sum
rectangle.
Example 2:
Input:
R=2
C=2
M=[[-1,-2],[-3,-4]]
Output:
-1
Explanation:
Taking only the first cell is the
optimal choice.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maximumSumRectangle() which takes the number R, C, and the 2D matrix M as input parameters and returns the maximum sum submatrix.
Expected Time Complexity:O(R*R*C)
Expected Auxillary Space:O(R*C)
Constraints:
1<=R,C<=500
-1000<=M[i][j]<=1000 | class Solution:
def maximumSumRectangle(self, r, c, a):
n = r
m = c
maxval = -999999999
r1 = -1
r2 = -1
c11 = -1
c12 = -1
for c1 in range(m):
column = []
for c2 in range(c1, m):
for i in range(n):
if c1 == c2:
column.append(a[i][c2])
else:
column[i] += a[i][c2]
currsum = 0
ind = 0
for i in range(n):
currsum += column[i]
if maxval < currsum:
maxval = max(maxval, currsum)
r1 = ind
c11 = c1
r2 = i
c12 = c2
if currsum < 0:
currsum = 0
ind = i + 1
return maxval | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR |
Given a 2D matrix M of dimensions RxC. Find the maximum sum submatrix in it.
Example 1:
Input:
R=4
C=5
M=[[1,2,-1,-4,-20],
[-8,-3,4,2,1],
[3,8,10,1,3],
[-4,-1,1,7,-6]]
Output:
29
Explanation:
The matrix is as follows and the
blue rectangle denotes the maximum sum
rectangle.
Example 2:
Input:
R=2
C=2
M=[[-1,-2],[-3,-4]]
Output:
-1
Explanation:
Taking only the first cell is the
optimal choice.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maximumSumRectangle() which takes the number R, C, and the 2D matrix M as input parameters and returns the maximum sum submatrix.
Expected Time Complexity:O(R*R*C)
Expected Auxillary Space:O(R*C)
Constraints:
1<=R,C<=500
-1000<=M[i][j]<=1000 | class Solution:
def maximumSumRectangle(self, R, C, M):
_mxsum = -10000000000
for cbegin in range(C):
cend = cbegin
sm = [0] * R
while cend < C:
for r in range(R):
sm[r] += M[r][cend]
_mxsum = max(_mxsum, self.maxSubArray(sm))
cend += 1
return _mxsum
def maxSubArray(self, A):
ans = A[0]
sm = 0
for i in range(0, len(A)):
sm = max(sm + A[i], A[i])
ans = max(ans, sm)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR WHILE VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given a 2D matrix M of dimensions RxC. Find the maximum sum submatrix in it.
Example 1:
Input:
R=4
C=5
M=[[1,2,-1,-4,-20],
[-8,-3,4,2,1],
[3,8,10,1,3],
[-4,-1,1,7,-6]]
Output:
29
Explanation:
The matrix is as follows and the
blue rectangle denotes the maximum sum
rectangle.
Example 2:
Input:
R=2
C=2
M=[[-1,-2],[-3,-4]]
Output:
-1
Explanation:
Taking only the first cell is the
optimal choice.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maximumSumRectangle() which takes the number R, C, and the 2D matrix M as input parameters and returns the maximum sum submatrix.
Expected Time Complexity:O(R*R*C)
Expected Auxillary Space:O(R*C)
Constraints:
1<=R,C<=500
-1000<=M[i][j]<=1000 | class Solution:
def maximumSumRectangle(self, R, C, M):
def kadane(arr):
n = len(arr)
a = [(0) for i in range(n)]
a[0] = arr[0]
for i in range(1, n):
if arr[i] < arr[i] + a[i - 1]:
a[i] = arr[i] + a[i - 1]
else:
a[i] = arr[i]
return max(a)
ans = float("-inf")
for i in range(R):
l = [0] * C
for j in range(i, R):
for k in range(C):
l[k] = l[k] + M[j][k]
ans = max(ans, kadane(l.copy()))
return ans | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR RETURN VAR |
Given a 2D matrix M of dimensions RxC. Find the maximum sum submatrix in it.
Example 1:
Input:
R=4
C=5
M=[[1,2,-1,-4,-20],
[-8,-3,4,2,1],
[3,8,10,1,3],
[-4,-1,1,7,-6]]
Output:
29
Explanation:
The matrix is as follows and the
blue rectangle denotes the maximum sum
rectangle.
Example 2:
Input:
R=2
C=2
M=[[-1,-2],[-3,-4]]
Output:
-1
Explanation:
Taking only the first cell is the
optimal choice.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maximumSumRectangle() which takes the number R, C, and the 2D matrix M as input parameters and returns the maximum sum submatrix.
Expected Time Complexity:O(R*R*C)
Expected Auxillary Space:O(R*C)
Constraints:
1<=R,C<=500
-1000<=M[i][j]<=1000 | class Solution:
def maximumSumRectangle(self, R, C, M):
def kadanes(arr):
ans = -1000000000.0
summ = 0
for i in arr:
summ = summ + i
ans = max(ans, summ)
if summ < 0:
summ = 0
return ans
maxi = -1000000000.0
for i in range(R):
arr = [0] * C
for j in range(i, R):
for k in range(C):
arr[k] += M[j][k]
maxi = max(maxi, kadanes(arr))
return maxi | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR |
Given a 2D matrix M of dimensions RxC. Find the maximum sum submatrix in it.
Example 1:
Input:
R=4
C=5
M=[[1,2,-1,-4,-20],
[-8,-3,4,2,1],
[3,8,10,1,3],
[-4,-1,1,7,-6]]
Output:
29
Explanation:
The matrix is as follows and the
blue rectangle denotes the maximum sum
rectangle.
Example 2:
Input:
R=2
C=2
M=[[-1,-2],[-3,-4]]
Output:
-1
Explanation:
Taking only the first cell is the
optimal choice.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maximumSumRectangle() which takes the number R, C, and the 2D matrix M as input parameters and returns the maximum sum submatrix.
Expected Time Complexity:O(R*R*C)
Expected Auxillary Space:O(R*C)
Constraints:
1<=R,C<=500
-1000<=M[i][j]<=1000 | class Solution:
def maximumSumRectangle(self, R, C, M):
def kadane(a):
temp = float("-inf")
res = float("-inf")
for i in a:
temp = max(i, temp + i)
res = max(res, temp)
return res
maxSum = float("-inf")
for cStart in range(C):
arr = [(0) for i in range(R)]
for cEnd in range(cStart, C):
for row in range(R):
arr[row] += M[row][cEnd]
currMaxSum = kadane(arr)
maxSum = max(maxSum, currMaxSum)
return maxSum | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given a 2D matrix M of dimensions RxC. Find the maximum sum submatrix in it.
Example 1:
Input:
R=4
C=5
M=[[1,2,-1,-4,-20],
[-8,-3,4,2,1],
[3,8,10,1,3],
[-4,-1,1,7,-6]]
Output:
29
Explanation:
The matrix is as follows and the
blue rectangle denotes the maximum sum
rectangle.
Example 2:
Input:
R=2
C=2
M=[[-1,-2],[-3,-4]]
Output:
-1
Explanation:
Taking only the first cell is the
optimal choice.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maximumSumRectangle() which takes the number R, C, and the 2D matrix M as input parameters and returns the maximum sum submatrix.
Expected Time Complexity:O(R*R*C)
Expected Auxillary Space:O(R*C)
Constraints:
1<=R,C<=500
-1000<=M[i][j]<=1000 | class Solution:
def maximumSumRectangle(self, R, C, M):
def kadaneAlgo(arr):
n = len(arr)
if n == 0:
return 0
maxVal = -sys.maxsize
Tsum = 0
startingIndex = 0
s = 0
endingIndex = 0
for i in range(n):
Tsum += arr[i]
if maxVal < Tsum:
startingIndex = s
endingIndex = i
maxVal = Tsum
if Tsum < 0:
Tsum = 0
s = i + 1
return maxVal, startingIndex, endingIndex
maxSum = -sys.maxsize
finalLeftCol = -1
finalRIghtCol = -1
finalTop = -1
finaBottom = -1
for leftCol in range(C):
temp = [0] * R
for rightCol in range(leftCol, C):
for i in range(R):
temp[i] += M[i][rightCol]
summ, start, end = kadaneAlgo(temp)
if maxSum < summ:
finalLeftCol = leftCol
finalRightCol = rightCol
finalTop = start
finalBottom = end
maxSum = summ
return maxSum | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR |
Given a 2D matrix M of dimensions RxC. Find the maximum sum submatrix in it.
Example 1:
Input:
R=4
C=5
M=[[1,2,-1,-4,-20],
[-8,-3,4,2,1],
[3,8,10,1,3],
[-4,-1,1,7,-6]]
Output:
29
Explanation:
The matrix is as follows and the
blue rectangle denotes the maximum sum
rectangle.
Example 2:
Input:
R=2
C=2
M=[[-1,-2],[-3,-4]]
Output:
-1
Explanation:
Taking only the first cell is the
optimal choice.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maximumSumRectangle() which takes the number R, C, and the 2D matrix M as input parameters and returns the maximum sum submatrix.
Expected Time Complexity:O(R*R*C)
Expected Auxillary Space:O(R*C)
Constraints:
1<=R,C<=500
-1000<=M[i][j]<=1000 | class Solution:
def maximumSumRectangle(self, R, C, M):
maxSum = -99999999
for start in range(R):
sum = [(0) for i in range(C)]
for end in range(start, R):
for c in range(C):
sum[c] += M[end][c]
currSum = self.Kedane(sum)
maxSum = max(currSum, maxSum)
return maxSum
def Kedane(self, arr):
maxSum, currSum = -99999999, 0
for i in range(len(arr)):
currSum = max(arr[i], currSum + arr[i])
maxSum = max(currSum, maxSum)
return maxSum | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given a 2D matrix M of dimensions RxC. Find the maximum sum submatrix in it.
Example 1:
Input:
R=4
C=5
M=[[1,2,-1,-4,-20],
[-8,-3,4,2,1],
[3,8,10,1,3],
[-4,-1,1,7,-6]]
Output:
29
Explanation:
The matrix is as follows and the
blue rectangle denotes the maximum sum
rectangle.
Example 2:
Input:
R=2
C=2
M=[[-1,-2],[-3,-4]]
Output:
-1
Explanation:
Taking only the first cell is the
optimal choice.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maximumSumRectangle() which takes the number R, C, and the 2D matrix M as input parameters and returns the maximum sum submatrix.
Expected Time Complexity:O(R*R*C)
Expected Auxillary Space:O(R*C)
Constraints:
1<=R,C<=500
-1000<=M[i][j]<=1000 | class Solution:
def maximumSumRectangle(self, row, col, arr):
def kadane(arr):
if max(arr) < 0:
return max(arr)
maxsum = -1000000000.0
curr = 0
for i in arr:
curr += i
maxsum = max(maxsum, curr)
if curr < 0:
curr = 0
return maxsum
maxi = -1000000000.0
for i in range(col):
temp = [0] * row
for j in range(i, col):
for k in range(row):
temp[k] += arr[k][j]
Sum = kadane(temp)
maxi = max(maxi, Sum)
return maxi | CLASS_DEF FUNC_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Chef is given a rooted tree with N nodes numbered 1 to N where 1 is the root node.
Each node is assigned a lowercase english alphabet between a to z (both included).
Given two nodes A and B:
Let LCA(A, B) denote the [lowest common ancestor] of nodes A and B.
Let DIS(A,B) denote the string containing all the characters associated with the nodes in the same order as the shortest path from node A to node B (excluding the character associated to node B). Note that DIS(A,A) is an empty string.
Chef needs to answer Q queries. Each query is of the type:
U V: Given two integers U and V (1≤ U,V ≤ N), determine if there exists a concatenation of a non-empty [subsequence] and a non-empty subsequence of DIS(V,LCA(U,V)) such that it is a [palindrome].
------ Input Format ------
- The first line of each test case contains T, the number of test cases.
- The second line of each test case contains N, the number of nodes in the tree.
- The third line of each test case contains a string S consisting of N characters. Here, S_{i} corresponds to the character associated with node i.
- The next (N-1) lines each contain two space-separated integers x and y, denoting that an edge exists between nodes x and y.
- The next line contains Q, the number of queries.
- The next Q lines each contain two space-separated integers U and V, denoting the query.
------ Output Format ------
For each test case, output Q lines. Print YES in the i^{th} line if there exist a concatenation of a non-empty [subsequence] and a non-empty subsequence of DIS(V,LCA(U,V)) such that it is a palindrome for the i^{th} query. Otherwise, print NO.
You may print each character of the string in uppercase or lowercase (for example, the strings YES, yEs, yes, and yeS will all be treated as identical).
------ Constraints ------
$1 ≤T ≤2 \cdot 10^{5}$
$2 ≤N ≤2 \cdot 10^{5}$
$1 ≤Q ≤2 \cdot 10^{5}$
$1≤x, y ≤N$ and $x\ne y$.
$1 ≤U,V ≤N$ and $U \ne V$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
- Sum of $Q$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
1
7
ydbxcbd
1 2
2 3
1 4
2 5
6 4
7 6
2
3 5
3 6
----- Sample Output 1 ------
NO
YES
----- explanation 1 ------
The tree looks like:
Query $1$: $LCA(3, 5) = 2$. $DIS(3, LCA(3,5)) = DIS(3,2) = $ b. Similarly, $DIS(5, LCA(3,5)) = DIS(5,2) = $ c. It is not possible to concatenate non-empty subsequences from $DIS(3,2)$ and $DIS(5,2)$ such that we get a palindrome.
Query $2$: $LCA(3, 6) = 1$. $DIS(3, LCA(3,6)) = DIS(3,1) = $ bd. Similarly, $DIS(6, LCA(3,6)) = DIS(6,1) = $ bx. We can pick subsequence b from both $DIS(3,1)$ and $DIS(6,1)$. After concatenation, we get bb which is a palindrome. | import sys
sys.setrecursionlimit(1000000)
def mi():
return map(int, input().split())
def li():
return list(mi())
def si():
return str(input())
def ni():
return int(input())
def printyes():
print("YES")
def printno():
print("NO")
class RangeQuery:
def __init__(self, data, func=min):
self.func = func
self._data = _data = [list(data)]
i, n = 1, len(_data[0])
while 2 * i <= n:
prev = _data[-1]
_data.append([func(prev[j], prev[j + i]) for j in range(n - 2 * i + 1)])
i <<= 1
def query(self, begin, end):
depth = (end - begin).bit_length() - 1
return self.func(
self._data[depth][begin], self._data[depth][end - (1 << depth)]
)
class LCA:
def __init__(self, root, graph):
self.time = [-1] * len(graph)
self.path = [-1] * len(graph)
P = [-1] * len(graph)
t = -1
dfs = [root]
while dfs:
node = dfs.pop()
self.path[t] = P[node]
self.time[node] = t = t + 1
for nei in graph[node]:
if self.time[nei] == -1:
P[nei] = node
dfs.append(nei)
self.rmq = RangeQuery(self.time[node] for node in self.path)
def __call__(self, a, b):
if a == b:
return a
a = self.time[a]
b = self.time[b]
if a > b:
a, b = b, a
return self.path[self.rmq.query(a, b)]
def find(g, s, ct, visited, node, n):
ct[node][ord(s[node - 1]) - 97] += 1
for i in g[node]:
if i not in visited:
visited.add(i)
for k in range(26):
ct[i][k] += ct[node][k]
find(g, s, ct, visited, i, n)
for t in range(int(input())):
n = ni()
s = si()
g = []
for i in range(n + 1):
g.append([])
for i in range(n - 1):
a, b = mi()
g[a].append(b)
g[b].append(a)
L = LCA(1, g)
visited = set()
visited.add(1)
ct = []
for i in range(n + 5):
ct.append([0] * 26)
find(g, s, ct, visited, 1, n)
for q in range(int(input())):
a, b = mi()
if a < 1 or b < 1 or a > n or b > n:
printno()
continue
curr = L(a, b)
if curr < 1 or curr > n:
printno()
continue
flag = False
for i in range(26):
if ct[a][i] - ct[curr][i] > 0 and ct[b][i] - ct[curr][i] > 0:
flag = True
break
if flag:
printyes()
else:
printno() | IMPORT EXPR FUNC_CALL VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR STRING CLASS_DEF FUNC_DEF VAR ASSIGN VAR VAR ASSIGN VAR VAR LIST FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER WHILE BIN_OP NUMBER VAR VAR ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP NUMBER VAR CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF IF VAR VAR RETURN VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_CALL VAR VAR VAR FUNC_DEF VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP LIST NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR |
Chef is given a rooted tree with N nodes numbered 1 to N where 1 is the root node.
Each node is assigned a lowercase english alphabet between a to z (both included).
Given two nodes A and B:
Let LCA(A, B) denote the [lowest common ancestor] of nodes A and B.
Let DIS(A,B) denote the string containing all the characters associated with the nodes in the same order as the shortest path from node A to node B (excluding the character associated to node B). Note that DIS(A,A) is an empty string.
Chef needs to answer Q queries. Each query is of the type:
U V: Given two integers U and V (1≤ U,V ≤ N), determine if there exists a concatenation of a non-empty [subsequence] and a non-empty subsequence of DIS(V,LCA(U,V)) such that it is a [palindrome].
------ Input Format ------
- The first line of each test case contains T, the number of test cases.
- The second line of each test case contains N, the number of nodes in the tree.
- The third line of each test case contains a string S consisting of N characters. Here, S_{i} corresponds to the character associated with node i.
- The next (N-1) lines each contain two space-separated integers x and y, denoting that an edge exists between nodes x and y.
- The next line contains Q, the number of queries.
- The next Q lines each contain two space-separated integers U and V, denoting the query.
------ Output Format ------
For each test case, output Q lines. Print YES in the i^{th} line if there exist a concatenation of a non-empty [subsequence] and a non-empty subsequence of DIS(V,LCA(U,V)) such that it is a palindrome for the i^{th} query. Otherwise, print NO.
You may print each character of the string in uppercase or lowercase (for example, the strings YES, yEs, yes, and yeS will all be treated as identical).
------ Constraints ------
$1 ≤T ≤2 \cdot 10^{5}$
$2 ≤N ≤2 \cdot 10^{5}$
$1 ≤Q ≤2 \cdot 10^{5}$
$1≤x, y ≤N$ and $x\ne y$.
$1 ≤U,V ≤N$ and $U \ne V$
- Sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$.
- Sum of $Q$ over all test cases does not exceed $2 \cdot 10^{5}$.
----- Sample Input 1 ------
1
7
ydbxcbd
1 2
2 3
1 4
2 5
6 4
7 6
2
3 5
3 6
----- Sample Output 1 ------
NO
YES
----- explanation 1 ------
The tree looks like:
Query $1$: $LCA(3, 5) = 2$. $DIS(3, LCA(3,5)) = DIS(3,2) = $ b. Similarly, $DIS(5, LCA(3,5)) = DIS(5,2) = $ c. It is not possible to concatenate non-empty subsequences from $DIS(3,2)$ and $DIS(5,2)$ such that we get a palindrome.
Query $2$: $LCA(3, 6) = 1$. $DIS(3, LCA(3,6)) = DIS(3,1) = $ bd. Similarly, $DIS(6, LCA(3,6)) = DIS(6,1) = $ bx. We can pick subsequence b from both $DIS(3,1)$ and $DIS(6,1)$. After concatenation, we get bb which is a palindrome. | from sys import setrecursionlimit, stdin
input = stdin.readline
setrecursionlimit(5 * 10**5)
def dfs(p, prev, this, lvl):
level[p] = lvl
parent[p][0] = prev
for i in range(1, 21):
if parent[p][i - 1] != -1:
parent[p][i] = parent[parent[p][i - 1]][i - 1]
this[ord(a[p - 1]) - 97] += 1
count[p] = this[:]
for i in child[p]:
if i == prev:
continue
dfs(i, p, this[:], lvl + 1)
def lca(u, v):
if level[u] > level[v]:
u, v = v, u
dist = level[v] - level[u]
for i in range(20, -1, -1):
if dist >> i & 1:
v = parent[v][i]
if u == v:
return u
for i in range(20, -1, -1):
if parent[u][i] != parent[v][i]:
u = parent[u][i]
v = parent[v][i]
return parent[u][0]
def answer():
for q in range(int(input())):
u, v = map(int, input().split())
Lca, yes = lca(u, v), False
for i in range(26):
left = count[u][i] - count[Lca][i]
right = count[v][i] - count[Lca][i]
if left >= 1 and right >= 1:
yes = True
break
if yes:
print("YES")
else:
print("NO")
for T in range(int(input())):
n = int(input())
a = input().strip()
child = [[] for i in range(n + 1)]
for i in range(n - 1):
u, v = map(int, input().split())
child[u].append(v)
child[v].append(u)
parent = [[(-1) for i in range(21)] for j in range(n + 1)]
level = [(0) for i in range(n + 1)]
count = [None for i in range(n + 1)]
this = [(0) for i in range(26)]
dfs(1, -1, this, 0)
answer() | ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER IF VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR FOR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NONE VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR |
Given an array of N integers, you have to find if it is possible to partition the array with following rules:
Each element should belong to exactly one partition.
Each partition should have atleast K elements.
Absolute difference between any pair of elements in the same partition should not exceed M.
Example 1:
Input:
N = 5
K = 2
M = 3
A[] = {8, 3, 9, 1, 2}
Output:
YES
Explanation:
We can partition the array into two
partitions: {8, 9} and {3, 1, 2} such that
all rules are satisfied.
Your Task:
You don't need to read input or print anything. Your task is to complete the function partitionArray() which takes the number of elements N, integer K, integer M and array A[ ] as input parameters and returns true if we can partition the array such that all rules are satisfied, else returns false.
Expected Time Complexity: O(N * Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 2*10^{5}
1 ≤ K ≤ N
1 ≤ M ≤ 10^{9}
1 ≤ A[i] ≤ 10^{9} | class Solution:
def partitionArray(self, N, K, M, arr):
arr.sort()
dp = [False] * N
for i, e in enumerate(arr[K - 1 :], start=K - 1):
n = i + 1 - k + 1
lo, hi = 0, n
while lo < hi:
mi = lo + (hi - lo) // 2
if arr[i] - arr[mi] <= M:
hi = mi
else:
lo = mi + 1
for j in range(lo, n):
if j == 0 or dp[j - 1]:
dp[i] = True
break
return dp[-1] | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER RETURN VAR NUMBER |
Given an array of N integers, you have to find if it is possible to partition the array with following rules:
Each element should belong to exactly one partition.
Each partition should have atleast K elements.
Absolute difference between any pair of elements in the same partition should not exceed M.
Example 1:
Input:
N = 5
K = 2
M = 3
A[] = {8, 3, 9, 1, 2}
Output:
YES
Explanation:
We can partition the array into two
partitions: {8, 9} and {3, 1, 2} such that
all rules are satisfied.
Your Task:
You don't need to read input or print anything. Your task is to complete the function partitionArray() which takes the number of elements N, integer K, integer M and array A[ ] as input parameters and returns true if we can partition the array such that all rules are satisfied, else returns false.
Expected Time Complexity: O(N * Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 2*10^{5}
1 ≤ K ≤ N
1 ≤ M ≤ 10^{9}
1 ≤ A[i] ≤ 10^{9} | class Solution:
def partitionArray(self, n, k, m, arr):
arr = arr[:n]
arr.sort()
q = [0]
for i in range(k - 1, n):
while i - q[0] + 1 >= k:
if arr[i] - arr[q[0]] <= m:
q.append(i + 1)
break
q.pop(0)
if not q:
return 0
return q[-1] == n | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR WHILE BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR IF BIN_OP VAR VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR RETURN NUMBER RETURN VAR NUMBER VAR |
Given an array of N integers, you have to find if it is possible to partition the array with following rules:
Each element should belong to exactly one partition.
Each partition should have atleast K elements.
Absolute difference between any pair of elements in the same partition should not exceed M.
Example 1:
Input:
N = 5
K = 2
M = 3
A[] = {8, 3, 9, 1, 2}
Output:
YES
Explanation:
We can partition the array into two
partitions: {8, 9} and {3, 1, 2} such that
all rules are satisfied.
Your Task:
You don't need to read input or print anything. Your task is to complete the function partitionArray() which takes the number of elements N, integer K, integer M and array A[ ] as input parameters and returns true if we can partition the array such that all rules are satisfied, else returns false.
Expected Time Complexity: O(N * Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 2*10^{5}
1 ≤ K ≤ N
1 ≤ M ≤ 10^{9}
1 ≤ A[i] ≤ 10^{9} | class Solution:
def partitionArray(self, N, K, M, arr):
arr.sort()
def dfs(start):
if start == N:
return True
nexti = start + k - 1
while nexti < N:
if arr[nexti] - arr[start] > M:
return False
if dfs(nexti + 1):
return True
nexti += 1
return False
return dfs(0) | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR VAR IF BIN_OP VAR VAR VAR VAR VAR RETURN NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER RETURN NUMBER VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR NUMBER |
Given an array of N integers, you have to find if it is possible to partition the array with following rules:
Each element should belong to exactly one partition.
Each partition should have atleast K elements.
Absolute difference between any pair of elements in the same partition should not exceed M.
Example 1:
Input:
N = 5
K = 2
M = 3
A[] = {8, 3, 9, 1, 2}
Output:
YES
Explanation:
We can partition the array into two
partitions: {8, 9} and {3, 1, 2} such that
all rules are satisfied.
Your Task:
You don't need to read input or print anything. Your task is to complete the function partitionArray() which takes the number of elements N, integer K, integer M and array A[ ] as input parameters and returns true if we can partition the array such that all rules are satisfied, else returns false.
Expected Time Complexity: O(N * Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 2*10^{5}
1 ≤ K ≤ N
1 ≤ M ≤ 10^{9}
1 ≤ A[i] ≤ 10^{9} | class Solution:
def partitionArray(self, N, K, M, arr):
arr.sort()
def helper(indx):
n = len(arr)
if indx >= n:
return True
if n - indx < k:
return False
for i in range(indx + k - 1, n):
if arr[i] - arr[indx] > M:
return False
if helper(i + 1):
return True
return False
return helper(0) | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER IF BIN_OP VAR VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR VAR RETURN NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER RETURN FUNC_CALL VAR NUMBER |
Given an array of N integers, you have to find if it is possible to partition the array with following rules:
Each element should belong to exactly one partition.
Each partition should have atleast K elements.
Absolute difference between any pair of elements in the same partition should not exceed M.
Example 1:
Input:
N = 5
K = 2
M = 3
A[] = {8, 3, 9, 1, 2}
Output:
YES
Explanation:
We can partition the array into two
partitions: {8, 9} and {3, 1, 2} such that
all rules are satisfied.
Your Task:
You don't need to read input or print anything. Your task is to complete the function partitionArray() which takes the number of elements N, integer K, integer M and array A[ ] as input parameters and returns true if we can partition the array such that all rules are satisfied, else returns false.
Expected Time Complexity: O(N * Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 2*10^{5}
1 ≤ K ≤ N
1 ≤ M ≤ 10^{9}
1 ≤ A[i] ≤ 10^{9} | class Solution:
def partitionArray(self, N, K, M, arr):
arr.sort()
dp = [(-1) for i in range(N)]
def fun(arr, i):
if i >= len(arr):
return True
elif dp[i] != -1:
return dp[i]
else:
dp[i] = False
for l in range(i + K - 1, len(arr)):
if arr[l] - arr[i] <= M:
dp[i] = dp[i] or fun(arr, l + 1)
return dp[i]
return fun(arr, 0) | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR RETURN FUNC_CALL VAR VAR NUMBER |
Given an array of N integers, you have to find if it is possible to partition the array with following rules:
Each element should belong to exactly one partition.
Each partition should have atleast K elements.
Absolute difference between any pair of elements in the same partition should not exceed M.
Example 1:
Input:
N = 5
K = 2
M = 3
A[] = {8, 3, 9, 1, 2}
Output:
YES
Explanation:
We can partition the array into two
partitions: {8, 9} and {3, 1, 2} such that
all rules are satisfied.
Your Task:
You don't need to read input or print anything. Your task is to complete the function partitionArray() which takes the number of elements N, integer K, integer M and array A[ ] as input parameters and returns true if we can partition the array such that all rules are satisfied, else returns false.
Expected Time Complexity: O(N * Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 2*10^{5}
1 ≤ K ≤ N
1 ≤ M ≤ 10^{9}
1 ≤ A[i] ≤ 10^{9} | class Solution:
def partitionArray(self, N, K, M, arr):
arr.sort()
def rec(start, arr, k, m, n):
if start >= n:
return True
for i in range(start + k - 1, n):
if arr[i] - arr[start] > m:
return False
if rec(i + 1, arr, k, m, n):
return True
return False
return rec(0, arr, K, M, N) | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR FUNC_DEF IF VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR VAR RETURN NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER RETURN FUNC_CALL VAR NUMBER VAR VAR VAR VAR |
Given an array of N integers, you have to find if it is possible to partition the array with following rules:
Each element should belong to exactly one partition.
Each partition should have atleast K elements.
Absolute difference between any pair of elements in the same partition should not exceed M.
Example 1:
Input:
N = 5
K = 2
M = 3
A[] = {8, 3, 9, 1, 2}
Output:
YES
Explanation:
We can partition the array into two
partitions: {8, 9} and {3, 1, 2} such that
all rules are satisfied.
Your Task:
You don't need to read input or print anything. Your task is to complete the function partitionArray() which takes the number of elements N, integer K, integer M and array A[ ] as input parameters and returns true if we can partition the array such that all rules are satisfied, else returns false.
Expected Time Complexity: O(N * Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 2*10^{5}
1 ≤ K ≤ N
1 ≤ M ≤ 10^{9}
1 ≤ A[i] ≤ 10^{9} | class Solution:
def partitionArray(self, N, K, M, arr):
arr.sort()
cache = {}
def dp(i):
if i in cache:
return cache[i]
if N - i < K:
return False
if N - i == K:
return arr[-1] - arr[i] <= M
if N - i > K and arr[-1] - arr[i] <= M:
return True
cache[i] = False
for k in range(i + K - 1, N):
val = True
if arr[k] - arr[i] <= M:
val &= dp(k + 1)
if val:
cache[i] = True
return cache[i]
else:
break
return cache[i]
return dp(0) | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR RETURN VAR VAR IF BIN_OP VAR VAR VAR RETURN NUMBER IF BIN_OP VAR VAR VAR RETURN BIN_OP VAR NUMBER VAR VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR ASSIGN VAR VAR NUMBER RETURN VAR VAR RETURN VAR VAR RETURN FUNC_CALL VAR NUMBER |
Given an array of N integers, you have to find if it is possible to partition the array with following rules:
Each element should belong to exactly one partition.
Each partition should have atleast K elements.
Absolute difference between any pair of elements in the same partition should not exceed M.
Example 1:
Input:
N = 5
K = 2
M = 3
A[] = {8, 3, 9, 1, 2}
Output:
YES
Explanation:
We can partition the array into two
partitions: {8, 9} and {3, 1, 2} such that
all rules are satisfied.
Your Task:
You don't need to read input or print anything. Your task is to complete the function partitionArray() which takes the number of elements N, integer K, integer M and array A[ ] as input parameters and returns true if we can partition the array such that all rules are satisfied, else returns false.
Expected Time Complexity: O(N * Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 2*10^{5}
1 ≤ K ≤ N
1 ≤ M ≤ 10^{9}
1 ≤ A[i] ≤ 10^{9} | class Solution:
def partitionArray(self, N, K, M, arr):
arr.sort()
memo = {}
def recur(strt, end):
nonlocal N, K, M, arr, memo
if strt >= N:
return True
if end - strt + 1 < K:
return False
if (strt, end) in memo:
return memo[strt, end]
j = strt + K - 1
while j < N and arr[j] - arr[strt] <= M:
res = recur(j + 1, end)
if res:
memo[strt, end] = True
return res
j += 1
memo[strt, end] = False
return False
return recur(0, N - 1) | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR RETURN NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN NUMBER IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER RETURN FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER |
Given an array of N integers, you have to find if it is possible to partition the array with following rules:
Each element should belong to exactly one partition.
Each partition should have atleast K elements.
Absolute difference between any pair of elements in the same partition should not exceed M.
Example 1:
Input:
N = 5
K = 2
M = 3
A[] = {8, 3, 9, 1, 2}
Output:
YES
Explanation:
We can partition the array into two
partitions: {8, 9} and {3, 1, 2} such that
all rules are satisfied.
Your Task:
You don't need to read input or print anything. Your task is to complete the function partitionArray() which takes the number of elements N, integer K, integer M and array A[ ] as input parameters and returns true if we can partition the array such that all rules are satisfied, else returns false.
Expected Time Complexity: O(N * Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 2*10^{5}
1 ≤ K ≤ N
1 ≤ M ≤ 10^{9}
1 ≤ A[i] ≤ 10^{9} | class Solution:
def partitionArray(self, N, K, M, arr):
arr.sort()
dp = [False] * N
for i, e in enumerate(arr[K - 1 :], start=K - 1):
n = i + 1 - k + 1
lo, hi = 0, n
while lo < hi:
mi = lo + (hi - lo) // 2
if arr[i] - arr[mi] <= M:
hi = mi
else:
lo = mi + 1
for j in range(lo, n):
if j == 0 or dp[j - 1]:
dp[i] = True
break
return dp[-1]
if __name__ == "__main__":
for _ in range(int(input())):
n, k, m = map(int, input().split())
arr = list(map(int, input().strip().split()))
obj = Solution()
ans = obj.partitionArray(n, k, m, arr)
if ans:
print("YES")
else:
print("NO") | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER RETURN VAR NUMBER IF VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING |
Given an array of N integers, you have to find if it is possible to partition the array with following rules:
Each element should belong to exactly one partition.
Each partition should have atleast K elements.
Absolute difference between any pair of elements in the same partition should not exceed M.
Example 1:
Input:
N = 5
K = 2
M = 3
A[] = {8, 3, 9, 1, 2}
Output:
YES
Explanation:
We can partition the array into two
partitions: {8, 9} and {3, 1, 2} such that
all rules are satisfied.
Your Task:
You don't need to read input or print anything. Your task is to complete the function partitionArray() which takes the number of elements N, integer K, integer M and array A[ ] as input parameters and returns true if we can partition the array such that all rules are satisfied, else returns false.
Expected Time Complexity: O(N * Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 2*10^{5}
1 ≤ K ≤ N
1 ≤ M ≤ 10^{9}
1 ≤ A[i] ≤ 10^{9} | class Solution:
def partitionArray(self, N, K, M, arr):
arr.sort()
def dfs(i):
if i >= N:
return True
for j in range(i + K - 1, N):
if abs(arr[j] - arr[i]) > M:
break
if dfs(j + 1):
return True
return False
dp = [-1] * N
return dfs(0) | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR FUNC_DEF IF VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR RETURN FUNC_CALL VAR NUMBER |
Given an array of N integers, you have to find if it is possible to partition the array with following rules:
Each element should belong to exactly one partition.
Each partition should have atleast K elements.
Absolute difference between any pair of elements in the same partition should not exceed M.
Example 1:
Input:
N = 5
K = 2
M = 3
A[] = {8, 3, 9, 1, 2}
Output:
YES
Explanation:
We can partition the array into two
partitions: {8, 9} and {3, 1, 2} such that
all rules are satisfied.
Your Task:
You don't need to read input or print anything. Your task is to complete the function partitionArray() which takes the number of elements N, integer K, integer M and array A[ ] as input parameters and returns true if we can partition the array such that all rules are satisfied, else returns false.
Expected Time Complexity: O(N * Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 2*10^{5}
1 ≤ K ≤ N
1 ≤ M ≤ 10^{9}
1 ≤ A[i] ≤ 10^{9} | class Solution:
def partitionArray(self, N, K, M, arr):
arr.sort()
dp = [-1] * N
can_do = self.solve(0, arr, K, M, dp)
return can_do
def solve(self, start, arr, K, M, dp):
n = len(arr)
if start >= n:
return True
if dp[start] != -1:
return dp[start]
for i in range(start + K - 1, n):
diff = abs(arr[i] - arr[start])
if diff > M:
break
next_solvable = self.solve(i + 1, arr, K, M, dp)
if next_solvable:
dp[start] = True
return dp[start]
dp[start] = False
return dp[start] | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR ASSIGN VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR VAR NUMBER RETURN VAR VAR |
Given a sorted array keys[0.. n-1] of search keys and an array freq[0.. n-1] of frequency counts, where freq[i] is the number of searches to keys[i]. Construct a binary search tree of all keys such that the total cost of all the searches is as small as possible.
Let us first define the cost of a BST. The cost of a BST node is level of that node multiplied by its frequency. Level of root is 1.
Example 1:
Input:
n = 2
keys = {10, 12}
freq = {34, 50}
Output: 118
Explaination:
There can be following two possible BSTs
10 12
\ /
12 10
The cost of tree I is 34*1 + 50*2 = 134
The cost of tree II is 50*1 + 34*2 = 118
Example 2:
Input:
N = 3
keys = {10, 12, 20}
freq = {34, 8, 50}
Output: 142
Explaination: There can be many possible BSTs
20
/
10
\
12
Among all possible BSTs,
cost of this BST is minimum.
Cost of this BST is 1*50 + 2*34 + 3*8 = 142
Your Task:
You don't need to read input or print anything. Your task is to complete the function optimalSearchTree() which takes the array keys[], freq[] and their size n as input parameters and returns the total cost of all the searches is as small as possible.
Expected Time Complexity: O(n^{3})
Expected Auxiliary Space: O(n^{2})
Constraints:
1 ≤ N ≤ 100 | class Solution:
def optimalSearchTree(self, A, F, n):
dp = [([-1] * (1 + n)) for i in range(n + 1)]
def find(i, j):
if i > j:
return 0
if dp[i][j] != -1:
return dp[i][j]
if j == i:
dp[i][j] = F[i]
else:
ans = float("inf")
for k in range(i, j + 1):
left = find(i, k - 1)
right = find(k + 1, j)
ans = min(ans, sum(F[i : j + 1]) + left + right)
dp[i][j] = ans
return dp[i][j]
return find(0, len(A) - 1) | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR RETURN FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER |
Given a sorted array keys[0.. n-1] of search keys and an array freq[0.. n-1] of frequency counts, where freq[i] is the number of searches to keys[i]. Construct a binary search tree of all keys such that the total cost of all the searches is as small as possible.
Let us first define the cost of a BST. The cost of a BST node is level of that node multiplied by its frequency. Level of root is 1.
Example 1:
Input:
n = 2
keys = {10, 12}
freq = {34, 50}
Output: 118
Explaination:
There can be following two possible BSTs
10 12
\ /
12 10
The cost of tree I is 34*1 + 50*2 = 134
The cost of tree II is 50*1 + 34*2 = 118
Example 2:
Input:
N = 3
keys = {10, 12, 20}
freq = {34, 8, 50}
Output: 142
Explaination: There can be many possible BSTs
20
/
10
\
12
Among all possible BSTs,
cost of this BST is minimum.
Cost of this BST is 1*50 + 2*34 + 3*8 = 142
Your Task:
You don't need to read input or print anything. Your task is to complete the function optimalSearchTree() which takes the array keys[], freq[] and their size n as input parameters and returns the total cost of all the searches is as small as possible.
Expected Time Complexity: O(n^{3})
Expected Auxiliary Space: O(n^{2})
Constraints:
1 ≤ N ≤ 100 | def util(keys, freq, i, j, dp):
if i < 0 or i >= n or j < 0 or j >= n:
return 0
if dp[i][j] != -1:
return dp[i][j]
if i > j:
dp[i][j] = 0
return 0
elif i == j:
dp[i][j] = freq[i]
return freq[i]
res = float("inf")
c = 0
for k in range(i, j + 1):
c += freq[k]
cur = util(keys, freq, i, k - 1, dp) + util(keys, freq, k + 1, j, dp)
res = min(res, cur)
dp[i][j] = res + c
return res + c
class Solution:
def optimalSearchTree(self, keys, freq, n):
dp = [[(-1) for j in range(n)] for i in range(n)]
return util(keys, freq, 0, n - 1, dp) | FUNC_DEF IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN BIN_OP VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR |
Given a sorted array keys[0.. n-1] of search keys and an array freq[0.. n-1] of frequency counts, where freq[i] is the number of searches to keys[i]. Construct a binary search tree of all keys such that the total cost of all the searches is as small as possible.
Let us first define the cost of a BST. The cost of a BST node is level of that node multiplied by its frequency. Level of root is 1.
Example 1:
Input:
n = 2
keys = {10, 12}
freq = {34, 50}
Output: 118
Explaination:
There can be following two possible BSTs
10 12
\ /
12 10
The cost of tree I is 34*1 + 50*2 = 134
The cost of tree II is 50*1 + 34*2 = 118
Example 2:
Input:
N = 3
keys = {10, 12, 20}
freq = {34, 8, 50}
Output: 142
Explaination: There can be many possible BSTs
20
/
10
\
12
Among all possible BSTs,
cost of this BST is minimum.
Cost of this BST is 1*50 + 2*34 + 3*8 = 142
Your Task:
You don't need to read input or print anything. Your task is to complete the function optimalSearchTree() which takes the array keys[], freq[] and their size n as input parameters and returns the total cost of all the searches is as small as possible.
Expected Time Complexity: O(n^{3})
Expected Auxiliary Space: O(n^{2})
Constraints:
1 ≤ N ≤ 100 | class Solution:
def optimalSearchTree(self, keys, freq, n):
sum_ = [0] * n
sum_[0] = freq[0]
for i in range(1, n):
sum_[i] = sum_[i - 1] + freq[i]
dp = [([0] * n) for i in range(n)]
for i in range(n):
dp[i][i] = freq[i]
for size in range(1, n):
for i in range(n - size):
dp[i][i + size] = float("inf")
for j in range(i, i + size + 1):
exp1 = (
(dp[i][j - 1] if j > 0 else 0)
+ freq[j]
+ (dp[j + 1][i + size] if j < n - 1 else 0)
)
exp2 = (
(sum_[j - 1] if j > 0 else 0)
- (sum_[i - 1] if i > 0 else 0)
+ (sum_[i + size] - sum_[j])
)
dp[i][i + size] = min(dp[i][i + size], exp1 + exp2)
return dp[0][n - 1] | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR RETURN VAR NUMBER BIN_OP VAR NUMBER |
Given a sorted array keys[0.. n-1] of search keys and an array freq[0.. n-1] of frequency counts, where freq[i] is the number of searches to keys[i]. Construct a binary search tree of all keys such that the total cost of all the searches is as small as possible.
Let us first define the cost of a BST. The cost of a BST node is level of that node multiplied by its frequency. Level of root is 1.
Example 1:
Input:
n = 2
keys = {10, 12}
freq = {34, 50}
Output: 118
Explaination:
There can be following two possible BSTs
10 12
\ /
12 10
The cost of tree I is 34*1 + 50*2 = 134
The cost of tree II is 50*1 + 34*2 = 118
Example 2:
Input:
N = 3
keys = {10, 12, 20}
freq = {34, 8, 50}
Output: 142
Explaination: There can be many possible BSTs
20
/
10
\
12
Among all possible BSTs,
cost of this BST is minimum.
Cost of this BST is 1*50 + 2*34 + 3*8 = 142
Your Task:
You don't need to read input or print anything. Your task is to complete the function optimalSearchTree() which takes the array keys[], freq[] and their size n as input parameters and returns the total cost of all the searches is as small as possible.
Expected Time Complexity: O(n^{3})
Expected Auxiliary Space: O(n^{2})
Constraints:
1 ≤ N ≤ 100 | import sys
class Solution:
def level_sum(self, i, j, freq):
return sum(freq[i : j + 1])
def util(self, i, j, freq, dp):
if i > j:
return 0
if i == j:
return freq[i]
if dp[i][j] != -1:
return dp[i][j]
level_sum = self.level_sum(i, j, freq)
temp = sys.maxsize
for k in range(i, j + 1):
curr = self.util(i, k - 1, freq, dp) + self.util(k + 1, j, freq, dp)
temp = min(temp, curr)
dp[i][j] = temp + level_sum
return dp[i][j]
def optimalSearchTree(self, keys, freq, n):
dp = [([-1] * n) for i in range(n)]
return self.util(0, n - 1, freq, dp)
if __name__ == "__main__":
t = int(input())
for _ in range(t):
n = int(input())
keys = input().split()
for itr in range(n):
keys[itr] = int(keys[itr])
freq = input().split()
for itr in range(n):
freq[itr] = int(freq[itr])
ob = Solution()
print(ob.optimalSearchTree(keys, freq, n)) | IMPORT CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR RETURN VAR VAR IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR |
Given a sorted array keys[0.. n-1] of search keys and an array freq[0.. n-1] of frequency counts, where freq[i] is the number of searches to keys[i]. Construct a binary search tree of all keys such that the total cost of all the searches is as small as possible.
Let us first define the cost of a BST. The cost of a BST node is level of that node multiplied by its frequency. Level of root is 1.
Example 1:
Input:
n = 2
keys = {10, 12}
freq = {34, 50}
Output: 118
Explaination:
There can be following two possible BSTs
10 12
\ /
12 10
The cost of tree I is 34*1 + 50*2 = 134
The cost of tree II is 50*1 + 34*2 = 118
Example 2:
Input:
N = 3
keys = {10, 12, 20}
freq = {34, 8, 50}
Output: 142
Explaination: There can be many possible BSTs
20
/
10
\
12
Among all possible BSTs,
cost of this BST is minimum.
Cost of this BST is 1*50 + 2*34 + 3*8 = 142
Your Task:
You don't need to read input or print anything. Your task is to complete the function optimalSearchTree() which takes the array keys[], freq[] and their size n as input parameters and returns the total cost of all the searches is as small as possible.
Expected Time Complexity: O(n^{3})
Expected Auxiliary Space: O(n^{2})
Constraints:
1 ≤ N ≤ 100 | class Solution:
def optimalSearchTree(self, keys, freq, n):
dp = [[(-1) for _ in range(n)] for _ in range(n)]
def minCost(i, j):
if i > j:
return 0
if i == j:
return freq[i]
if dp[i][j] != -1:
return dp[i][j]
mini = float("inf")
s = 0
for k in range(i, j + 1):
s += freq[k]
for k in range(i, j + 1):
mini = min(mini, minCost(i, k - 1) + minCost(k + 1, j) + s)
dp[i][j] = mini
return mini
return minCost(0, n - 1) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR RETURN VAR VAR IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER |
Given a sorted array keys[0.. n-1] of search keys and an array freq[0.. n-1] of frequency counts, where freq[i] is the number of searches to keys[i]. Construct a binary search tree of all keys such that the total cost of all the searches is as small as possible.
Let us first define the cost of a BST. The cost of a BST node is level of that node multiplied by its frequency. Level of root is 1.
Example 1:
Input:
n = 2
keys = {10, 12}
freq = {34, 50}
Output: 118
Explaination:
There can be following two possible BSTs
10 12
\ /
12 10
The cost of tree I is 34*1 + 50*2 = 134
The cost of tree II is 50*1 + 34*2 = 118
Example 2:
Input:
N = 3
keys = {10, 12, 20}
freq = {34, 8, 50}
Output: 142
Explaination: There can be many possible BSTs
20
/
10
\
12
Among all possible BSTs,
cost of this BST is minimum.
Cost of this BST is 1*50 + 2*34 + 3*8 = 142
Your Task:
You don't need to read input or print anything. Your task is to complete the function optimalSearchTree() which takes the array keys[], freq[] and their size n as input parameters and returns the total cost of all the searches is as small as possible.
Expected Time Complexity: O(n^{3})
Expected Auxiliary Space: O(n^{2})
Constraints:
1 ≤ N ≤ 100 | class Solution:
def optimalSearchTree(self, keys, freq, n):
dp = [[(0) for _ in keys] for _ in keys]
for g in range(n):
for i, j in zip(range(n - g), range(g, n)):
mini = -1
sumi = 0
for k in range(i, j + 1):
sumi += freq[k]
for k in range(i, j + 1):
left = dp[i][k - 1] if k > i else 0
right = dp[k + 1][j] if k < j else 0
z = left + right + sumi
mini = z if mini < 0 else min(mini, z)
dp[i][j] = mini
return dp[0][n - 1] | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR NUMBER BIN_OP VAR NUMBER |
Given a sorted array keys[0.. n-1] of search keys and an array freq[0.. n-1] of frequency counts, where freq[i] is the number of searches to keys[i]. Construct a binary search tree of all keys such that the total cost of all the searches is as small as possible.
Let us first define the cost of a BST. The cost of a BST node is level of that node multiplied by its frequency. Level of root is 1.
Example 1:
Input:
n = 2
keys = {10, 12}
freq = {34, 50}
Output: 118
Explaination:
There can be following two possible BSTs
10 12
\ /
12 10
The cost of tree I is 34*1 + 50*2 = 134
The cost of tree II is 50*1 + 34*2 = 118
Example 2:
Input:
N = 3
keys = {10, 12, 20}
freq = {34, 8, 50}
Output: 142
Explaination: There can be many possible BSTs
20
/
10
\
12
Among all possible BSTs,
cost of this BST is minimum.
Cost of this BST is 1*50 + 2*34 + 3*8 = 142
Your Task:
You don't need to read input or print anything. Your task is to complete the function optimalSearchTree() which takes the array keys[], freq[] and their size n as input parameters and returns the total cost of all the searches is as small as possible.
Expected Time Complexity: O(n^{3})
Expected Auxiliary Space: O(n^{2})
Constraints:
1 ≤ N ≤ 100 | class Solution:
def optimalSearchTree(self, keys, freq, n):
dp = [[(-1) for _ in range(n)] for _ in range(n)]
return self.solve(keys, freq, dp, 0, n - 1)
def solve(self, keys, freqs, dp, i, j):
if i > j:
return 0
if dp[i][j] != -1:
return dp[i][j]
totalFreqs = 0
for k in range(i, j + 1):
totalFreqs += freqs[k]
MIN = 10**10
for k in range(i, j + 1):
left = self.solve(keys, freqs, dp, i, k - 1)
right = self.solve(keys, freqs, dp, k + 1, j)
MIN = min(MIN, left + right)
cost = totalFreqs + MIN
dp[i][j] = cost
return cost
print(totalFreqs)
return | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR NUMBER BIN_OP VAR NUMBER FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR EXPR FUNC_CALL VAR VAR RETURN |
Given a sorted array keys[0.. n-1] of search keys and an array freq[0.. n-1] of frequency counts, where freq[i] is the number of searches to keys[i]. Construct a binary search tree of all keys such that the total cost of all the searches is as small as possible.
Let us first define the cost of a BST. The cost of a BST node is level of that node multiplied by its frequency. Level of root is 1.
Example 1:
Input:
n = 2
keys = {10, 12}
freq = {34, 50}
Output: 118
Explaination:
There can be following two possible BSTs
10 12
\ /
12 10
The cost of tree I is 34*1 + 50*2 = 134
The cost of tree II is 50*1 + 34*2 = 118
Example 2:
Input:
N = 3
keys = {10, 12, 20}
freq = {34, 8, 50}
Output: 142
Explaination: There can be many possible BSTs
20
/
10
\
12
Among all possible BSTs,
cost of this BST is minimum.
Cost of this BST is 1*50 + 2*34 + 3*8 = 142
Your Task:
You don't need to read input or print anything. Your task is to complete the function optimalSearchTree() which takes the array keys[], freq[] and their size n as input parameters and returns the total cost of all the searches is as small as possible.
Expected Time Complexity: O(n^{3})
Expected Auxiliary Space: O(n^{2})
Constraints:
1 ≤ N ≤ 100 | def util(keys, freq, i, j, dp):
if i < 0 or i >= n or j < 0 or j >= n:
return 0
if dp[i][j] != -1:
return dp[i][j]
if i > j:
dp[i][j] = 0
return 0
elif i == j:
dp[i][j] = freq[i]
return freq[i]
res = float("inf")
c = 0
for k in range(i, j + 1):
c += freq[k]
cur = util(keys, freq, i, k - 1, dp) + util(keys, freq, k + 1, j, dp)
res = min(res, cur)
dp[i][j] = res + c
return res + c
class Solution:
def optimalSearchTree(self, keys, freq, n):
dp = [[(0) for j in range(n)] for i in range(n + 1)]
for i in range(n - 1, -1, -1):
for j in range(n):
if i > j:
dp[i][j] = 0
elif i == j:
dp[i][j] = freq[i]
else:
c = 0
res = float("inf")
for k in range(i, j + 1):
c += freq[k]
try:
cur = dp[i][k - 1] + dp[k + 1][j]
except IndexError as e:
print(i, k - 1, k + 1, j, e)
res = min(res, cur)
dp[i][j] = res + c
return dp[0][n - 1]
dp = [[(-1) for j in range(n)] for i in range(n)]
return util(keys, freq, 0, n - 1, dp) | FUNC_DEF IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN BIN_OP VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR |
Given a sorted array keys[0.. n-1] of search keys and an array freq[0.. n-1] of frequency counts, where freq[i] is the number of searches to keys[i]. Construct a binary search tree of all keys such that the total cost of all the searches is as small as possible.
Let us first define the cost of a BST. The cost of a BST node is level of that node multiplied by its frequency. Level of root is 1.
Example 1:
Input:
n = 2
keys = {10, 12}
freq = {34, 50}
Output: 118
Explaination:
There can be following two possible BSTs
10 12
\ /
12 10
The cost of tree I is 34*1 + 50*2 = 134
The cost of tree II is 50*1 + 34*2 = 118
Example 2:
Input:
N = 3
keys = {10, 12, 20}
freq = {34, 8, 50}
Output: 142
Explaination: There can be many possible BSTs
20
/
10
\
12
Among all possible BSTs,
cost of this BST is minimum.
Cost of this BST is 1*50 + 2*34 + 3*8 = 142
Your Task:
You don't need to read input or print anything. Your task is to complete the function optimalSearchTree() which takes the array keys[], freq[] and their size n as input parameters and returns the total cost of all the searches is as small as possible.
Expected Time Complexity: O(n^{3})
Expected Auxiliary Space: O(n^{2})
Constraints:
1 ≤ N ≤ 100 | class Solution:
def optimalSearchTree(self, keys, freq, n):
dp = [[(0) for i in range(n)] for j in range(n)]
for g in range(n):
for i, j in zip(range(n - g), range(g, n)):
if g == 0:
dp[i][j] = freq[i]
if g == 1:
dp[i][j] = min(freq[i] + 2 * freq[j], freq[j] + 2 * freq[i])
else:
mini = float("inf")
freq_sum = 0
for x in range(i, j + 1):
freq_sum += freq[x]
for k in range(i, j + 1):
left = 0 if k == i else dp[i][k - 1]
right = 0 if k == j else dp[k + 1][j]
if left + right + freq_sum < mini:
mini = left + right + freq_sum
dp[i][j] = mini
return dp[0][n - 1] | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP NUMBER VAR VAR BIN_OP VAR VAR BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR IF BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR NUMBER BIN_OP VAR NUMBER |
Given a sorted array keys[0.. n-1] of search keys and an array freq[0.. n-1] of frequency counts, where freq[i] is the number of searches to keys[i]. Construct a binary search tree of all keys such that the total cost of all the searches is as small as possible.
Let us first define the cost of a BST. The cost of a BST node is level of that node multiplied by its frequency. Level of root is 1.
Example 1:
Input:
n = 2
keys = {10, 12}
freq = {34, 50}
Output: 118
Explaination:
There can be following two possible BSTs
10 12
\ /
12 10
The cost of tree I is 34*1 + 50*2 = 134
The cost of tree II is 50*1 + 34*2 = 118
Example 2:
Input:
N = 3
keys = {10, 12, 20}
freq = {34, 8, 50}
Output: 142
Explaination: There can be many possible BSTs
20
/
10
\
12
Among all possible BSTs,
cost of this BST is minimum.
Cost of this BST is 1*50 + 2*34 + 3*8 = 142
Your Task:
You don't need to read input or print anything. Your task is to complete the function optimalSearchTree() which takes the array keys[], freq[] and their size n as input parameters and returns the total cost of all the searches is as small as possible.
Expected Time Complexity: O(n^{3})
Expected Auxiliary Space: O(n^{2})
Constraints:
1 ≤ N ≤ 100 | class Solution:
def optimalSearchTree(self, keys, freq, n):
dp = [[(0) for i in range(n)] for j in range(n)]
for g in range(n):
i = 0
j = g
while j < n:
if g == 0:
dp[i][j] = freq[i]
elif g == 1:
dp[i][j] = min(2 * freq[j] + freq[i], freq[j] + 2 * freq[i])
else:
mini = float("inf")
fSum = 0
for m in range(i, j + 1):
fSum += freq[m]
for k in range(i, j + 1):
left = dp[i][k - 1] if k != 0 else 0
right = dp[k + 1][j] if k != n - 1 else 0
mini = min(mini, left + right + fSum)
dp[i][j] = mini
j += 1
i += 1
return dp[0][n - 1] | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR NUMBER BIN_OP VAR NUMBER |
Given a sorted array keys[0.. n-1] of search keys and an array freq[0.. n-1] of frequency counts, where freq[i] is the number of searches to keys[i]. Construct a binary search tree of all keys such that the total cost of all the searches is as small as possible.
Let us first define the cost of a BST. The cost of a BST node is level of that node multiplied by its frequency. Level of root is 1.
Example 1:
Input:
n = 2
keys = {10, 12}
freq = {34, 50}
Output: 118
Explaination:
There can be following two possible BSTs
10 12
\ /
12 10
The cost of tree I is 34*1 + 50*2 = 134
The cost of tree II is 50*1 + 34*2 = 118
Example 2:
Input:
N = 3
keys = {10, 12, 20}
freq = {34, 8, 50}
Output: 142
Explaination: There can be many possible BSTs
20
/
10
\
12
Among all possible BSTs,
cost of this BST is minimum.
Cost of this BST is 1*50 + 2*34 + 3*8 = 142
Your Task:
You don't need to read input or print anything. Your task is to complete the function optimalSearchTree() which takes the array keys[], freq[] and their size n as input parameters and returns the total cost of all the searches is as small as possible.
Expected Time Complexity: O(n^{3})
Expected Auxiliary Space: O(n^{2})
Constraints:
1 ≤ N ≤ 100 | class Solution:
def __init__(self):
self.d = {}
def optimalSearchTree(self, keys, freq, n):
def f(fr, i, j):
if i > j:
return 0
if i == j:
return fr[i]
if (i, j) in self.d.keys():
return self.d[i, j]
s = sum(fr[i : j + 1])
minn = float("inf")
for r in range(i, j + 1):
cost = f(fr, i, r - 1) + f(fr, r + 1, j)
minn = min(cost, minn)
self.d[i, j] = minn + s
return minn + s
return f(freq, 0, n - 1) | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR RETURN VAR VAR IF VAR VAR FUNC_CALL VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN BIN_OP VAR VAR RETURN FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER |
Given a sorted array keys[0.. n-1] of search keys and an array freq[0.. n-1] of frequency counts, where freq[i] is the number of searches to keys[i]. Construct a binary search tree of all keys such that the total cost of all the searches is as small as possible.
Let us first define the cost of a BST. The cost of a BST node is level of that node multiplied by its frequency. Level of root is 1.
Example 1:
Input:
n = 2
keys = {10, 12}
freq = {34, 50}
Output: 118
Explaination:
There can be following two possible BSTs
10 12
\ /
12 10
The cost of tree I is 34*1 + 50*2 = 134
The cost of tree II is 50*1 + 34*2 = 118
Example 2:
Input:
N = 3
keys = {10, 12, 20}
freq = {34, 8, 50}
Output: 142
Explaination: There can be many possible BSTs
20
/
10
\
12
Among all possible BSTs,
cost of this BST is minimum.
Cost of this BST is 1*50 + 2*34 + 3*8 = 142
Your Task:
You don't need to read input or print anything. Your task is to complete the function optimalSearchTree() which takes the array keys[], freq[] and their size n as input parameters and returns the total cost of all the searches is as small as possible.
Expected Time Complexity: O(n^{3})
Expected Auxiliary Space: O(n^{2})
Constraints:
1 ≤ N ≤ 100 | class Solution:
def optimalSearchTree(self, keys, freq, n):
cost = [[(0) for x in range(n)] for y in range(n)]
for i in range(n):
cost[i][i] = freq[i]
for L in range(2, n + 1):
for i in range(n - L + 2):
j = i + L - 1
if i >= n or j >= n:
break
off_set_sum = self.sum(freq, i, j)
cost[i][j] = 10**10
for r in range(i, j + 1):
c = 0
if r > i:
c += cost[i][r - 1]
if r < j:
c += cost[r + 1][j]
c += off_set_sum
if c < cost[i][j]:
cost[i][j] = c
return cost[0][n - 1]
def sum(self, freq, i, j):
s = 0
for k in range(i, j + 1):
s += freq[k]
return s | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR |
Given a sorted array keys[0.. n-1] of search keys and an array freq[0.. n-1] of frequency counts, where freq[i] is the number of searches to keys[i]. Construct a binary search tree of all keys such that the total cost of all the searches is as small as possible.
Let us first define the cost of a BST. The cost of a BST node is level of that node multiplied by its frequency. Level of root is 1.
Example 1:
Input:
n = 2
keys = {10, 12}
freq = {34, 50}
Output: 118
Explaination:
There can be following two possible BSTs
10 12
\ /
12 10
The cost of tree I is 34*1 + 50*2 = 134
The cost of tree II is 50*1 + 34*2 = 118
Example 2:
Input:
N = 3
keys = {10, 12, 20}
freq = {34, 8, 50}
Output: 142
Explaination: There can be many possible BSTs
20
/
10
\
12
Among all possible BSTs,
cost of this BST is minimum.
Cost of this BST is 1*50 + 2*34 + 3*8 = 142
Your Task:
You don't need to read input or print anything. Your task is to complete the function optimalSearchTree() which takes the array keys[], freq[] and their size n as input parameters and returns the total cost of all the searches is as small as possible.
Expected Time Complexity: O(n^{3})
Expected Auxiliary Space: O(n^{2})
Constraints:
1 ≤ N ≤ 100 | class Solution:
def optimalSearchTree(self, keys, freq, n):
dp = [[(100000000) for _ in range(n)] for _ in range(n)]
prefix_sum = [freq[i] for i in range(n)]
dp[0][0] = freq[0]
for i in range(1, n):
prefix_sum[i] += prefix_sum[i - 1]
dp[i][i] = freq[i]
def sum_in_range(a, b):
if a > b:
return 0
if a == 0:
return prefix_sum[b]
return prefix_sum[b] - prefix_sum[a - 1]
i = 0
for gap in range(1, n):
for j in range(gap, n):
for k in range(i, j + 1):
left_tree_cnt = (
sum_in_range(i, k - 1) + dp[i][k - 1] if k > i else 0
)
right_tree_cnt = (
sum_in_range(k + 1, j) + dp[k + 1][j] if k < j else 0
)
dp[i][j] = min(dp[i][j], left_tree_cnt + freq[k] + right_tree_cnt)
i += 1
i = 0
return dp[0][n - 1] | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR NUMBER RETURN VAR VAR RETURN BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER RETURN VAR NUMBER BIN_OP VAR NUMBER |
Given a sorted array keys[0.. n-1] of search keys and an array freq[0.. n-1] of frequency counts, where freq[i] is the number of searches to keys[i]. Construct a binary search tree of all keys such that the total cost of all the searches is as small as possible.
Let us first define the cost of a BST. The cost of a BST node is level of that node multiplied by its frequency. Level of root is 1.
Example 1:
Input:
n = 2
keys = {10, 12}
freq = {34, 50}
Output: 118
Explaination:
There can be following two possible BSTs
10 12
\ /
12 10
The cost of tree I is 34*1 + 50*2 = 134
The cost of tree II is 50*1 + 34*2 = 118
Example 2:
Input:
N = 3
keys = {10, 12, 20}
freq = {34, 8, 50}
Output: 142
Explaination: There can be many possible BSTs
20
/
10
\
12
Among all possible BSTs,
cost of this BST is minimum.
Cost of this BST is 1*50 + 2*34 + 3*8 = 142
Your Task:
You don't need to read input or print anything. Your task is to complete the function optimalSearchTree() which takes the array keys[], freq[] and their size n as input parameters and returns the total cost of all the searches is as small as possible.
Expected Time Complexity: O(n^{3})
Expected Auxiliary Space: O(n^{2})
Constraints:
1 ≤ N ≤ 100 | class Solution:
def optimalSearchTree(self, keys, freq, n):
n += 1
p = [0]
p.extend(freq)
q = [0] * n
e = [([float("inf")] * n) for _ in range(n + 1)]
w = [([0] * n) for _ in range(n + 1)]
for i in range(1, n + 1):
w[i][i - 1] = q[i - 1]
e[i][i - 1] = q[i - 1]
for i in range(n - 1, 0, -1):
for j in range(i, n):
w[i][j] = w[i][j - 1] + p[j] + q[j]
for k in range(i, j + 1):
e[i][j] = min(e[i][j], e[i][k - 1] + e[k + 1][j] + w[i][j])
return e[1][n - 1] | CLASS_DEF FUNC_DEF VAR NUMBER ASSIGN VAR LIST NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN VAR NUMBER BIN_OP VAR NUMBER |
Given a sorted array keys[0.. n-1] of search keys and an array freq[0.. n-1] of frequency counts, where freq[i] is the number of searches to keys[i]. Construct a binary search tree of all keys such that the total cost of all the searches is as small as possible.
Let us first define the cost of a BST. The cost of a BST node is level of that node multiplied by its frequency. Level of root is 1.
Example 1:
Input:
n = 2
keys = {10, 12}
freq = {34, 50}
Output: 118
Explaination:
There can be following two possible BSTs
10 12
\ /
12 10
The cost of tree I is 34*1 + 50*2 = 134
The cost of tree II is 50*1 + 34*2 = 118
Example 2:
Input:
N = 3
keys = {10, 12, 20}
freq = {34, 8, 50}
Output: 142
Explaination: There can be many possible BSTs
20
/
10
\
12
Among all possible BSTs,
cost of this BST is minimum.
Cost of this BST is 1*50 + 2*34 + 3*8 = 142
Your Task:
You don't need to read input or print anything. Your task is to complete the function optimalSearchTree() which takes the array keys[], freq[] and their size n as input parameters and returns the total cost of all the searches is as small as possible.
Expected Time Complexity: O(n^{3})
Expected Auxiliary Space: O(n^{2})
Constraints:
1 ≤ N ≤ 100 | class Solution:
def optimalSearchTree(self, keys, freq, n):
dp = [[(-1) for i in range(1001)] for j in range(1001)]
def sumfreq(i, j):
summ = 0
for k in range(i, j + 1):
summ += freq[k]
return summ
def solve(i, j):
if i > j:
return 0
if i == j:
return freq[i]
if dp[i][j] != -1:
return dp[i][j]
weight = sumfreq(i, j)
ans = float("inf")
for k in range(i, j + 1):
temp = solve(i, k - 1) + solve(k + 1, j)
ans = min(ans, temp)
dp[i][j] = ans + weight
return dp[i][j]
return solve(0, n - 1) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR RETURN VAR VAR IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN VAR VAR VAR RETURN FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER |
Given a sorted array keys[0.. n-1] of search keys and an array freq[0.. n-1] of frequency counts, where freq[i] is the number of searches to keys[i]. Construct a binary search tree of all keys such that the total cost of all the searches is as small as possible.
Let us first define the cost of a BST. The cost of a BST node is level of that node multiplied by its frequency. Level of root is 1.
Example 1:
Input:
n = 2
keys = {10, 12}
freq = {34, 50}
Output: 118
Explaination:
There can be following two possible BSTs
10 12
\ /
12 10
The cost of tree I is 34*1 + 50*2 = 134
The cost of tree II is 50*1 + 34*2 = 118
Example 2:
Input:
N = 3
keys = {10, 12, 20}
freq = {34, 8, 50}
Output: 142
Explaination: There can be many possible BSTs
20
/
10
\
12
Among all possible BSTs,
cost of this BST is minimum.
Cost of this BST is 1*50 + 2*34 + 3*8 = 142
Your Task:
You don't need to read input or print anything. Your task is to complete the function optimalSearchTree() which takes the array keys[], freq[] and their size n as input parameters and returns the total cost of all the searches is as small as possible.
Expected Time Complexity: O(n^{3})
Expected Auxiliary Space: O(n^{2})
Constraints:
1 ≤ N ≤ 100 | class Solution:
def optimalSearchTree(self, keys, freq, n):
dp = [[(-1) for i in range(n + 1)] for j in range(n + 1)]
for i in range(n):
dp[i][i] = 0
dp[i][i + 1] = freq[i]
dp[n][n] = 0
for i in range(2, n + 1):
for j in range(n - i + 1):
mn = float("inf")
for k in range(j + 1, j + i + 1):
mn = min(mn, dp[j][k - 1] + dp[k][j + i])
dp[j][j + i] = mn + sum(freq[j : j + i])
return dp[0][n]
if __name__ == "__main__":
t = int(input())
for _ in range(t):
n = int(input())
keys = input().split()
for itr in range(n):
keys[itr] = int(keys[itr])
freq = input().split()
for itr in range(n):
freq[itr] = int(freq[itr])
ob = Solution()
print(ob.optimalSearchTree(keys, freq, n)) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR RETURN VAR NUMBER VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR |
Given a sorted array keys[0.. n-1] of search keys and an array freq[0.. n-1] of frequency counts, where freq[i] is the number of searches to keys[i]. Construct a binary search tree of all keys such that the total cost of all the searches is as small as possible.
Let us first define the cost of a BST. The cost of a BST node is level of that node multiplied by its frequency. Level of root is 1.
Example 1:
Input:
n = 2
keys = {10, 12}
freq = {34, 50}
Output: 118
Explaination:
There can be following two possible BSTs
10 12
\ /
12 10
The cost of tree I is 34*1 + 50*2 = 134
The cost of tree II is 50*1 + 34*2 = 118
Example 2:
Input:
N = 3
keys = {10, 12, 20}
freq = {34, 8, 50}
Output: 142
Explaination: There can be many possible BSTs
20
/
10
\
12
Among all possible BSTs,
cost of this BST is minimum.
Cost of this BST is 1*50 + 2*34 + 3*8 = 142
Your Task:
You don't need to read input or print anything. Your task is to complete the function optimalSearchTree() which takes the array keys[], freq[] and their size n as input parameters and returns the total cost of all the searches is as small as possible.
Expected Time Complexity: O(n^{3})
Expected Auxiliary Space: O(n^{2})
Constraints:
1 ≤ N ≤ 100 | class Solution:
def optimalSearchTree(self, keys, freq, n):
cost = [[(0) for i in range(n)] for j in range(n)]
for i in range(n):
cost[i][i] = freq[i]
for L in range(2, n + 1):
for i in range(n - L + 1):
j = i + L - 1
offsetSum = sum(freq[i : j + 1])
cost[i][j] = float("inf")
for r in range(i, j + 1):
c = 0
if r > i:
c += cost[i][r - 1]
if r < j:
c += cost[r + 1][j]
c += offsetSum
if c < cost[i][j]:
cost[i][j] = c
return cost[0][n - 1] | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR NUMBER BIN_OP VAR NUMBER |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.