description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
In a country popular for train travel, you have planned some train travelling one year in advance. The days of the year that you will travel is given as an array days. Each day is an integer from 1 to 365.
Train tickets are sold in 3 different ways:
a 1-day pass is sold for costs[0] dollars;
a 7-day pass is sold for costs[1] dollars;
a 30-day pass is sold for costs[2] dollars.
The passes allow that many days of consecutive travel. For example, if we get a 7-day pass on day 2, then we can travel for 7 days: day 2, 3, 4, 5, 6, 7, and 8.
Return the minimum number of dollars you need to travel every day in the given list of days.
Example 1:
Input: days = [1,4,6,7,8,20], costs = [2,7,15]
Output: 11
Explanation:
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 1-day pass for costs[0] = $2, which covered day 1.
On day 3, you bought a 7-day pass for costs[1] = $7, which covered days 3, 4, ..., 9.
On day 20, you bought a 1-day pass for costs[0] = $2, which covered day 20.
In total you spent $11 and covered all the days of your travel.
Example 2:
Input: days = [1,2,3,4,5,6,7,8,9,10,30,31], costs = [2,7,15]
Output: 17
Explanation:
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 30-day pass for costs[2] = $15 which covered days 1, 2, ..., 30.
On day 31, you bought a 1-day pass for costs[0] = $2 which covered day 31.
In total you spent $17 and covered all the days of your travel.
Note:
1 <= days.length <= 365
1 <= days[i] <= 365
days is in strictly increasing order.
costs.length == 3
1 <= costs[i] <= 1000 | class Solution:
def mincostTickets(self, days: List[int], costs: List[int]) -> int:
n = len(days)
m = len(costs)
ranges = [1, 7, 30]
dp = [float("inf")] * (n + 1)
dp[0] = 0
for j in range(1, n + 1):
for i in range(1, m + 1):
for k in range(j, n + 1):
if days[k - 1] - days[j - 1] >= ranges[i - 1]:
break
dp[k] = min(dp[k], costs[i - 1] + dp[j - 1])
return dp[n] | CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR |
In a country popular for train travel, you have planned some train travelling one year in advance. The days of the year that you will travel is given as an array days. Each day is an integer from 1 to 365.
Train tickets are sold in 3 different ways:
a 1-day pass is sold for costs[0] dollars;
a 7-day pass is sold for costs[1] dollars;
a 30-day pass is sold for costs[2] dollars.
The passes allow that many days of consecutive travel. For example, if we get a 7-day pass on day 2, then we can travel for 7 days: day 2, 3, 4, 5, 6, 7, and 8.
Return the minimum number of dollars you need to travel every day in the given list of days.
Example 1:
Input: days = [1,4,6,7,8,20], costs = [2,7,15]
Output: 11
Explanation:
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 1-day pass for costs[0] = $2, which covered day 1.
On day 3, you bought a 7-day pass for costs[1] = $7, which covered days 3, 4, ..., 9.
On day 20, you bought a 1-day pass for costs[0] = $2, which covered day 20.
In total you spent $11 and covered all the days of your travel.
Example 2:
Input: days = [1,2,3,4,5,6,7,8,9,10,30,31], costs = [2,7,15]
Output: 17
Explanation:
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 30-day pass for costs[2] = $15 which covered days 1, 2, ..., 30.
On day 31, you bought a 1-day pass for costs[0] = $2 which covered day 31.
In total you spent $17 and covered all the days of your travel.
Note:
1 <= days.length <= 365
1 <= days[i] <= 365
days is in strictly increasing order.
costs.length == 3
1 <= costs[i] <= 1000 | class Solution:
def mincostTickets(self, days: List[int], costs: List[int]) -> int:
given = {}
for day in days:
given[day] = True
dp = [(0) for _ in range(days[-1] + 1)]
dp[1] = costs[0]
for day in range(days[-1] + 1):
if day not in given:
dp[day] = dp[day - 1]
else:
one_day_pass = dp[max(0, day - 1)] + costs[0]
one_week_pass = dp[max(0, day - 7)] + costs[1]
one_month_pass = dp[max(0, day - 30)] + costs[2]
dp[day] = min(one_day_pass, one_week_pass, one_month_pass)
print(dp)
return dp[-1] | CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR NUMBER VAR |
In a country popular for train travel, you have planned some train travelling one year in advance. The days of the year that you will travel is given as an array days. Each day is an integer from 1 to 365.
Train tickets are sold in 3 different ways:
a 1-day pass is sold for costs[0] dollars;
a 7-day pass is sold for costs[1] dollars;
a 30-day pass is sold for costs[2] dollars.
The passes allow that many days of consecutive travel. For example, if we get a 7-day pass on day 2, then we can travel for 7 days: day 2, 3, 4, 5, 6, 7, and 8.
Return the minimum number of dollars you need to travel every day in the given list of days.
Example 1:
Input: days = [1,4,6,7,8,20], costs = [2,7,15]
Output: 11
Explanation:
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 1-day pass for costs[0] = $2, which covered day 1.
On day 3, you bought a 7-day pass for costs[1] = $7, which covered days 3, 4, ..., 9.
On day 20, you bought a 1-day pass for costs[0] = $2, which covered day 20.
In total you spent $11 and covered all the days of your travel.
Example 2:
Input: days = [1,2,3,4,5,6,7,8,9,10,30,31], costs = [2,7,15]
Output: 17
Explanation:
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 30-day pass for costs[2] = $15 which covered days 1, 2, ..., 30.
On day 31, you bought a 1-day pass for costs[0] = $2 which covered day 31.
In total you spent $17 and covered all the days of your travel.
Note:
1 <= days.length <= 365
1 <= days[i] <= 365
days is in strictly increasing order.
costs.length == 3
1 <= costs[i] <= 1000 | class Solution:
def mincostTickets(self, days: List[int], costs: List[int]) -> int:
if not days:
return 0
lastday = days[-1]
dp = [0] * (lastday + 1)
for i in range(1, lastday + 1):
if i not in days:
dp[i] = dp[i - 1]
continue
prev_1 = dp[i - 1] if i >= 1 else 0
prev_7 = dp[i - 7] if i >= 7 else 0
prev_30 = dp[i - 30] if i >= 30 else 0
dp[i] = min(prev_1 + costs[0], prev_7 + costs[1], prev_30 + costs[2])
return dp[-1] | CLASS_DEF FUNC_DEF VAR VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER RETURN VAR NUMBER VAR |
In a country popular for train travel, you have planned some train travelling one year in advance. The days of the year that you will travel is given as an array days. Each day is an integer from 1 to 365.
Train tickets are sold in 3 different ways:
a 1-day pass is sold for costs[0] dollars;
a 7-day pass is sold for costs[1] dollars;
a 30-day pass is sold for costs[2] dollars.
The passes allow that many days of consecutive travel. For example, if we get a 7-day pass on day 2, then we can travel for 7 days: day 2, 3, 4, 5, 6, 7, and 8.
Return the minimum number of dollars you need to travel every day in the given list of days.
Example 1:
Input: days = [1,4,6,7,8,20], costs = [2,7,15]
Output: 11
Explanation:
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 1-day pass for costs[0] = $2, which covered day 1.
On day 3, you bought a 7-day pass for costs[1] = $7, which covered days 3, 4, ..., 9.
On day 20, you bought a 1-day pass for costs[0] = $2, which covered day 20.
In total you spent $11 and covered all the days of your travel.
Example 2:
Input: days = [1,2,3,4,5,6,7,8,9,10,30,31], costs = [2,7,15]
Output: 17
Explanation:
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 30-day pass for costs[2] = $15 which covered days 1, 2, ..., 30.
On day 31, you bought a 1-day pass for costs[0] = $2 which covered day 31.
In total you spent $17 and covered all the days of your travel.
Note:
1 <= days.length <= 365
1 <= days[i] <= 365
days is in strictly increasing order.
costs.length == 3
1 <= costs[i] <= 1000 | class Solution:
def mincostTickets(self, days: List[int], costs: List[int]) -> int:
dp = [0] * (days[-1] + 1)
pos = 0
for i in range(1, days[-1] + 1):
if i == days[pos]:
d1 = i - 1
d2 = i - 7 if i - 7 > 0 else 0
d3 = i - 30 if i - 30 > 0 else 0
dp[i] = min(dp[d1] + costs[0], dp[d2] + costs[1], dp[d3] + costs[2])
pos += 1
else:
dp[i] = dp[i - 1]
return dp[-1] | CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR NUMBER VAR |
In a country popular for train travel, you have planned some train travelling one year in advance. The days of the year that you will travel is given as an array days. Each day is an integer from 1 to 365.
Train tickets are sold in 3 different ways:
a 1-day pass is sold for costs[0] dollars;
a 7-day pass is sold for costs[1] dollars;
a 30-day pass is sold for costs[2] dollars.
The passes allow that many days of consecutive travel. For example, if we get a 7-day pass on day 2, then we can travel for 7 days: day 2, 3, 4, 5, 6, 7, and 8.
Return the minimum number of dollars you need to travel every day in the given list of days.
Example 1:
Input: days = [1,4,6,7,8,20], costs = [2,7,15]
Output: 11
Explanation:
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 1-day pass for costs[0] = $2, which covered day 1.
On day 3, you bought a 7-day pass for costs[1] = $7, which covered days 3, 4, ..., 9.
On day 20, you bought a 1-day pass for costs[0] = $2, which covered day 20.
In total you spent $11 and covered all the days of your travel.
Example 2:
Input: days = [1,2,3,4,5,6,7,8,9,10,30,31], costs = [2,7,15]
Output: 17
Explanation:
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 30-day pass for costs[2] = $15 which covered days 1, 2, ..., 30.
On day 31, you bought a 1-day pass for costs[0] = $2 which covered day 31.
In total you spent $17 and covered all the days of your travel.
Note:
1 <= days.length <= 365
1 <= days[i] <= 365
days is in strictly increasing order.
costs.length == 3
1 <= costs[i] <= 1000 | class Solution:
def mincostTickets(self, days: List[int], costs: List[int]) -> int:
dp = [float("inf")] * (len(days) + 1)
dp[0] = 0
for i in range(1, len(days) + 1):
j = 0
while i + j < len(dp):
if days[i + j - 1] <= days[i - 1]:
dp[i + j] = min(dp[i + j], dp[i - 1] + costs[0])
if days[i + j - 1] <= days[i - 1] + 6:
dp[i + j] = min(dp[i + j], dp[i - 1] + costs[1])
if days[i + j - 1] <= days[i - 1] + 29:
dp[i + j] = min(dp[i + j], dp[i - 1] + costs[2])
else:
break
j += 1
return dp[-1] | CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR NUMBER VAR |
In a country popular for train travel, you have planned some train travelling one year in advance. The days of the year that you will travel is given as an array days. Each day is an integer from 1 to 365.
Train tickets are sold in 3 different ways:
a 1-day pass is sold for costs[0] dollars;
a 7-day pass is sold for costs[1] dollars;
a 30-day pass is sold for costs[2] dollars.
The passes allow that many days of consecutive travel. For example, if we get a 7-day pass on day 2, then we can travel for 7 days: day 2, 3, 4, 5, 6, 7, and 8.
Return the minimum number of dollars you need to travel every day in the given list of days.
Example 1:
Input: days = [1,4,6,7,8,20], costs = [2,7,15]
Output: 11
Explanation:
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 1-day pass for costs[0] = $2, which covered day 1.
On day 3, you bought a 7-day pass for costs[1] = $7, which covered days 3, 4, ..., 9.
On day 20, you bought a 1-day pass for costs[0] = $2, which covered day 20.
In total you spent $11 and covered all the days of your travel.
Example 2:
Input: days = [1,2,3,4,5,6,7,8,9,10,30,31], costs = [2,7,15]
Output: 17
Explanation:
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 30-day pass for costs[2] = $15 which covered days 1, 2, ..., 30.
On day 31, you bought a 1-day pass for costs[0] = $2 which covered day 31.
In total you spent $17 and covered all the days of your travel.
Note:
1 <= days.length <= 365
1 <= days[i] <= 365
days is in strictly increasing order.
costs.length == 3
1 <= costs[i] <= 1000 | class Solution:
def mincostTickets(self, days: List[int], costs: List[int]) -> int:
@lru_cache(None)
def recur(day):
if day > 365:
return 0
elif day in days:
ret = math.inf
for c, d in zip(costs, [1, 7, 30]):
ret = min(ret, c + recur(day + d))
return ret
else:
return recur(day + 1)
return recur(0) | CLASS_DEF FUNC_DEF VAR VAR VAR VAR FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR ASSIGN VAR VAR FOR VAR VAR FUNC_CALL VAR VAR LIST NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR NONE RETURN FUNC_CALL VAR NUMBER VAR |
In a country popular for train travel, you have planned some train travelling one year in advance. The days of the year that you will travel is given as an array days. Each day is an integer from 1 to 365.
Train tickets are sold in 3 different ways:
a 1-day pass is sold for costs[0] dollars;
a 7-day pass is sold for costs[1] dollars;
a 30-day pass is sold for costs[2] dollars.
The passes allow that many days of consecutive travel. For example, if we get a 7-day pass on day 2, then we can travel for 7 days: day 2, 3, 4, 5, 6, 7, and 8.
Return the minimum number of dollars you need to travel every day in the given list of days.
Example 1:
Input: days = [1,4,6,7,8,20], costs = [2,7,15]
Output: 11
Explanation:
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 1-day pass for costs[0] = $2, which covered day 1.
On day 3, you bought a 7-day pass for costs[1] = $7, which covered days 3, 4, ..., 9.
On day 20, you bought a 1-day pass for costs[0] = $2, which covered day 20.
In total you spent $11 and covered all the days of your travel.
Example 2:
Input: days = [1,2,3,4,5,6,7,8,9,10,30,31], costs = [2,7,15]
Output: 17
Explanation:
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 30-day pass for costs[2] = $15 which covered days 1, 2, ..., 30.
On day 31, you bought a 1-day pass for costs[0] = $2 which covered day 31.
In total you spent $17 and covered all the days of your travel.
Note:
1 <= days.length <= 365
1 <= days[i] <= 365
days is in strictly increasing order.
costs.length == 3
1 <= costs[i] <= 1000 | class Solution:
def mincostTickets(self, days: List[int], costs: List[int]) -> int:
if not days:
return 0
dp = [0] * len(days)
def search(start: int, end: int, day: int) -> int:
if start == end - 1:
if day >= days[start]:
return start
else:
return -1
m = int((end - start) / 2) + start
if day >= days[m]:
return search(m, end, day)
else:
return search(start, m, day)
m = {day: i for i, day in enumerate(days)}
for i, day in enumerate(days):
prev_1 = day - 1
prev_7 = day - 7
prev_30 = day - 30
c_1 = costs[0]
c_7 = costs[1]
c_30 = costs[2]
if prev_1 in m:
c_1 += dp[m[prev_1]]
elif prev_1 >= 0 and prev_1 >= days[0]:
c_1 += dp[search(0, i, prev_1)]
if prev_7 in m:
c_7 += dp[m[prev_7]]
elif prev_7 >= 0 and prev_7 >= days[0]:
c_7 += dp[search(0, i, prev_7)]
if prev_30 in m:
c_30 += dp[m[prev_30]]
elif prev_30 >= 0 and prev_30 >= days[0]:
c_30 += dp[search(0, i, prev_30)]
dp[i] = min(c_1, c_7, c_30)
return dp[-1] | CLASS_DEF FUNC_DEF VAR VAR VAR VAR IF VAR RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FUNC_DEF VAR VAR VAR IF VAR BIN_OP VAR NUMBER IF VAR VAR VAR RETURN VAR RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER VAR VAR IF VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER VAR VAR IF VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR NUMBER VAR |
In a country popular for train travel, you have planned some train travelling one year in advance. The days of the year that you will travel is given as an array days. Each day is an integer from 1 to 365.
Train tickets are sold in 3 different ways:
a 1-day pass is sold for costs[0] dollars;
a 7-day pass is sold for costs[1] dollars;
a 30-day pass is sold for costs[2] dollars.
The passes allow that many days of consecutive travel. For example, if we get a 7-day pass on day 2, then we can travel for 7 days: day 2, 3, 4, 5, 6, 7, and 8.
Return the minimum number of dollars you need to travel every day in the given list of days.
Example 1:
Input: days = [1,4,6,7,8,20], costs = [2,7,15]
Output: 11
Explanation:
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 1-day pass for costs[0] = $2, which covered day 1.
On day 3, you bought a 7-day pass for costs[1] = $7, which covered days 3, 4, ..., 9.
On day 20, you bought a 1-day pass for costs[0] = $2, which covered day 20.
In total you spent $11 and covered all the days of your travel.
Example 2:
Input: days = [1,2,3,4,5,6,7,8,9,10,30,31], costs = [2,7,15]
Output: 17
Explanation:
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 30-day pass for costs[2] = $15 which covered days 1, 2, ..., 30.
On day 31, you bought a 1-day pass for costs[0] = $2 which covered day 31.
In total you spent $17 and covered all the days of your travel.
Note:
1 <= days.length <= 365
1 <= days[i] <= 365
days is in strictly increasing order.
costs.length == 3
1 <= costs[i] <= 1000 | class Solution:
def mincostTickets(self, days: List[int], costs: List[int]) -> int:
n = len(days)
if not days:
return 0
total = [float("inf") for _ in range(n)]
total[0] = min(costs)
for i in range(1, n):
curday = days[i]
total[i] = total[i - 1] + min(costs)
for j in range(i - 1, -1, -1):
diff = curday - days[j]
if diff < 7:
if j > 0:
total[i] = min(total[i], total[j - 1] + costs[1])
else:
total[i] = min(total[i], costs[1])
if diff < 30:
if j > 0:
total[i] = min(total[i], total[j - 1] + costs[2])
else:
total[i] = min(total[i], costs[2])
else:
break
return total[-1] | CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR NUMBER VAR |
In a country popular for train travel, you have planned some train travelling one year in advance. The days of the year that you will travel is given as an array days. Each day is an integer from 1 to 365.
Train tickets are sold in 3 different ways:
a 1-day pass is sold for costs[0] dollars;
a 7-day pass is sold for costs[1] dollars;
a 30-day pass is sold for costs[2] dollars.
The passes allow that many days of consecutive travel. For example, if we get a 7-day pass on day 2, then we can travel for 7 days: day 2, 3, 4, 5, 6, 7, and 8.
Return the minimum number of dollars you need to travel every day in the given list of days.
Example 1:
Input: days = [1,4,6,7,8,20], costs = [2,7,15]
Output: 11
Explanation:
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 1-day pass for costs[0] = $2, which covered day 1.
On day 3, you bought a 7-day pass for costs[1] = $7, which covered days 3, 4, ..., 9.
On day 20, you bought a 1-day pass for costs[0] = $2, which covered day 20.
In total you spent $11 and covered all the days of your travel.
Example 2:
Input: days = [1,2,3,4,5,6,7,8,9,10,30,31], costs = [2,7,15]
Output: 17
Explanation:
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 30-day pass for costs[2] = $15 which covered days 1, 2, ..., 30.
On day 31, you bought a 1-day pass for costs[0] = $2 which covered day 31.
In total you spent $17 and covered all the days of your travel.
Note:
1 <= days.length <= 365
1 <= days[i] <= 365
days is in strictly increasing order.
costs.length == 3
1 <= costs[i] <= 1000 | class Solution:
def mincostTickets(self, days: List[int], costs: List[int]) -> int:
dp = [0] + [(-1) for i in range(days[-1])]
for day in days:
dp[day] = 0
for i in range(1, len(dp)):
if dp[i] == -1:
dp[i] = dp[i - 1]
else:
dp[i] = min(
dp[i - 1] + costs[0],
dp[max(i - 7, 0)] + costs[1],
dp[max(i - 30, 0)] + costs[2],
)
return dp[-1] | CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER RETURN VAR NUMBER VAR |
In a country popular for train travel, you have planned some train travelling one year in advance. The days of the year that you will travel is given as an array days. Each day is an integer from 1 to 365.
Train tickets are sold in 3 different ways:
a 1-day pass is sold for costs[0] dollars;
a 7-day pass is sold for costs[1] dollars;
a 30-day pass is sold for costs[2] dollars.
The passes allow that many days of consecutive travel. For example, if we get a 7-day pass on day 2, then we can travel for 7 days: day 2, 3, 4, 5, 6, 7, and 8.
Return the minimum number of dollars you need to travel every day in the given list of days.
Example 1:
Input: days = [1,4,6,7,8,20], costs = [2,7,15]
Output: 11
Explanation:
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 1-day pass for costs[0] = $2, which covered day 1.
On day 3, you bought a 7-day pass for costs[1] = $7, which covered days 3, 4, ..., 9.
On day 20, you bought a 1-day pass for costs[0] = $2, which covered day 20.
In total you spent $11 and covered all the days of your travel.
Example 2:
Input: days = [1,2,3,4,5,6,7,8,9,10,30,31], costs = [2,7,15]
Output: 17
Explanation:
For example, here is one way to buy passes that lets you travel your travel plan:
On day 1, you bought a 30-day pass for costs[2] = $15 which covered days 1, 2, ..., 30.
On day 31, you bought a 1-day pass for costs[0] = $2 which covered day 31.
In total you spent $17 and covered all the days of your travel.
Note:
1 <= days.length <= 365
1 <= days[i] <= 365
days is in strictly increasing order.
costs.length == 3
1 <= costs[i] <= 1000 | class Solution:
def mincostTickets(self, days: List[int], costs: List[int]) -> int:
dp = [float("inf") for d in range(days[-1] + 1)]
dp[0] = 0
pass_num_days = [1, 7, 30]
days_set = set(days)
for day in range(days[-1] + 1):
if day not in days_set:
dp[day] = dp[max(0, day - 1)]
else:
for num_days, cost in zip(pass_num_days, costs):
dp[day] = min(dp[day], dp[max(0, day - num_days)] + cost)
print(dp)
return dp[-1] | CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR NUMBER VAR |
Although Inzane successfully found his beloved bone, Zane, his owner, has yet to return. To search for Zane, he would need a lot of money, of which he sadly has none. To deal with the problem, he has decided to hack the banks. [Image]
There are n banks, numbered from 1 to n. There are also n - 1 wires connecting the banks. All banks are initially online. Each bank also has its initial strength: bank i has initial strength a_{i}.
Let us define some keywords before we proceed. Bank i and bank j are neighboring if and only if there exists a wire directly connecting them. Bank i and bank j are semi-neighboring if and only if there exists an online bank k such that bank i and bank k are neighboring and bank k and bank j are neighboring.
When a bank is hacked, it becomes offline (and no longer online), and other banks that are neighboring or semi-neighboring to it have their strengths increased by 1.
To start his plan, Inzane will choose a bank to hack first. Indeed, the strength of such bank must not exceed the strength of his computer. After this, he will repeatedly choose some bank to hack next until all the banks are hacked, but he can continue to hack bank x if and only if all these conditions are met: Bank x is online. That is, bank x is not hacked yet. Bank x is neighboring to some offline bank. The strength of bank x is less than or equal to the strength of Inzane's computer.
Determine the minimum strength of the computer Inzane needs to hack all the banks.
-----Input-----
The first line contains one integer n (1 ≤ n ≤ 3·10^5) — the total number of banks.
The second line contains n integers a_1, a_2, ..., a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9) — the strengths of the banks.
Each of the next n - 1 lines contains two integers u_{i} and v_{i} (1 ≤ u_{i}, v_{i} ≤ n, u_{i} ≠ v_{i}) — meaning that there is a wire directly connecting banks u_{i} and v_{i}.
It is guaranteed that the wires connect the banks in such a way that Inzane can somehow hack all the banks using a computer with appropriate strength.
-----Output-----
Print one integer — the minimum strength of the computer Inzane needs to accomplish the goal.
-----Examples-----
Input
5
1 2 3 4 5
1 2
2 3
3 4
4 5
Output
5
Input
7
38 -29 87 93 39 28 -55
1 2
2 5
3 2
2 4
1 7
7 6
Output
93
Input
5
1 2 7 6 7
1 5
5 3
3 4
2 4
Output
8
-----Note-----
In the first sample, Inzane can hack all banks using a computer with strength 5. Here is how: Initially, strengths of the banks are [1, 2, 3, 4, 5]. He hacks bank 5, then strengths of the banks become [1, 2, 4, 5, - ]. He hacks bank 4, then strengths of the banks become [1, 3, 5, - , - ]. He hacks bank 3, then strengths of the banks become [2, 4, - , - , - ]. He hacks bank 2, then strengths of the banks become [3, - , - , - , - ]. He completes his goal by hacking bank 1.
In the second sample, Inzane can hack banks 4, 2, 3, 1, 5, 7, and 6, in this order. This way, he can hack all banks using a computer with strength 93. | def main():
n = int(input())
a = list(map(int, input().split()))
graph = [[] for _ in range(n)]
for i in range(n - 1):
u, v = map(lambda x: int(x) - 1, input().split())
graph[u].append(v)
graph[v].append(u)
a_max = max(a)
max_count_0 = sum(ai == a_max for ai in a)
prev_count_0 = sum(ai == a_max - 1 for ai in a)
result = a_max + 2
for u in range(n):
max_count = max_count_0
prev_count = prev_count_0
if a[u] == a_max:
max_count -= 1
else:
prev_count -= 1
for v in graph[u]:
if a[v] == a_max:
max_count -= 1
prev_count += 1
elif a[v] == a_max - 1:
prev_count -= 1
if max_count == 0:
if prev_count == 0:
result = a_max
break
else:
result = a_max + 1
print(result)
main() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Although Inzane successfully found his beloved bone, Zane, his owner, has yet to return. To search for Zane, he would need a lot of money, of which he sadly has none. To deal with the problem, he has decided to hack the banks. [Image]
There are n banks, numbered from 1 to n. There are also n - 1 wires connecting the banks. All banks are initially online. Each bank also has its initial strength: bank i has initial strength a_{i}.
Let us define some keywords before we proceed. Bank i and bank j are neighboring if and only if there exists a wire directly connecting them. Bank i and bank j are semi-neighboring if and only if there exists an online bank k such that bank i and bank k are neighboring and bank k and bank j are neighboring.
When a bank is hacked, it becomes offline (and no longer online), and other banks that are neighboring or semi-neighboring to it have their strengths increased by 1.
To start his plan, Inzane will choose a bank to hack first. Indeed, the strength of such bank must not exceed the strength of his computer. After this, he will repeatedly choose some bank to hack next until all the banks are hacked, but he can continue to hack bank x if and only if all these conditions are met: Bank x is online. That is, bank x is not hacked yet. Bank x is neighboring to some offline bank. The strength of bank x is less than or equal to the strength of Inzane's computer.
Determine the minimum strength of the computer Inzane needs to hack all the banks.
-----Input-----
The first line contains one integer n (1 ≤ n ≤ 3·10^5) — the total number of banks.
The second line contains n integers a_1, a_2, ..., a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9) — the strengths of the banks.
Each of the next n - 1 lines contains two integers u_{i} and v_{i} (1 ≤ u_{i}, v_{i} ≤ n, u_{i} ≠ v_{i}) — meaning that there is a wire directly connecting banks u_{i} and v_{i}.
It is guaranteed that the wires connect the banks in such a way that Inzane can somehow hack all the banks using a computer with appropriate strength.
-----Output-----
Print one integer — the minimum strength of the computer Inzane needs to accomplish the goal.
-----Examples-----
Input
5
1 2 3 4 5
1 2
2 3
3 4
4 5
Output
5
Input
7
38 -29 87 93 39 28 -55
1 2
2 5
3 2
2 4
1 7
7 6
Output
93
Input
5
1 2 7 6 7
1 5
5 3
3 4
2 4
Output
8
-----Note-----
In the first sample, Inzane can hack all banks using a computer with strength 5. Here is how: Initially, strengths of the banks are [1, 2, 3, 4, 5]. He hacks bank 5, then strengths of the banks become [1, 2, 4, 5, - ]. He hacks bank 4, then strengths of the banks become [1, 3, 5, - , - ]. He hacks bank 3, then strengths of the banks become [2, 4, - , - , - ]. He hacks bank 2, then strengths of the banks become [3, - , - , - , - ]. He completes his goal by hacking bank 1.
In the second sample, Inzane can hack banks 4, 2, 3, 1, 5, 7, and 6, in this order. This way, he can hack all banks using a computer with strength 93. | import sys
def solve():
n = int(sys.stdin.readline().rstrip())
a = [int(i) for i in sys.stdin.readline().split()]
Adj = [[] for i in range(n)]
for i in range(n - 1):
u, v = map(int, sys.stdin.readline().split())
u, v = u - 1, v - 1
Adj[u].append(v)
Adj[v].append(u)
max_v = max(a)
max_n = a.index(max_v)
num_m = sum(ai == max_v for ai in a)
if num_m == 1:
ans = max_v
rinsetu = [False] * n
for u in Adj[max_n]:
rinsetu[u] = True
rinsetu[max_n] = True
for u in range(n):
if rinsetu[u]:
continue
if a[u] == max_v - 1:
ans = max_v + 1
break
else:
for u in range(n):
cnt = 0
if a[u] == max_v:
cnt += 1
for v in Adj[u]:
if a[v] == max_v:
cnt += 1
if cnt == num_m:
ans = max_v + 1
break
else:
ans = max_v + 2
print(ans)
def debug(x, table):
for name, val in table.items():
if x is val:
print("DEBUG:{} -> {}".format(name, val), file=sys.stderr)
return None
def __starting_point():
solve()
__starting_point() | IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR NUMBER FOR VAR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF FOR VAR VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR VAR RETURN NONE FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR |
Although Inzane successfully found his beloved bone, Zane, his owner, has yet to return. To search for Zane, he would need a lot of money, of which he sadly has none. To deal with the problem, he has decided to hack the banks. [Image]
There are n banks, numbered from 1 to n. There are also n - 1 wires connecting the banks. All banks are initially online. Each bank also has its initial strength: bank i has initial strength a_{i}.
Let us define some keywords before we proceed. Bank i and bank j are neighboring if and only if there exists a wire directly connecting them. Bank i and bank j are semi-neighboring if and only if there exists an online bank k such that bank i and bank k are neighboring and bank k and bank j are neighboring.
When a bank is hacked, it becomes offline (and no longer online), and other banks that are neighboring or semi-neighboring to it have their strengths increased by 1.
To start his plan, Inzane will choose a bank to hack first. Indeed, the strength of such bank must not exceed the strength of his computer. After this, he will repeatedly choose some bank to hack next until all the banks are hacked, but he can continue to hack bank x if and only if all these conditions are met: Bank x is online. That is, bank x is not hacked yet. Bank x is neighboring to some offline bank. The strength of bank x is less than or equal to the strength of Inzane's computer.
Determine the minimum strength of the computer Inzane needs to hack all the banks.
-----Input-----
The first line contains one integer n (1 ≤ n ≤ 3·10^5) — the total number of banks.
The second line contains n integers a_1, a_2, ..., a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9) — the strengths of the banks.
Each of the next n - 1 lines contains two integers u_{i} and v_{i} (1 ≤ u_{i}, v_{i} ≤ n, u_{i} ≠ v_{i}) — meaning that there is a wire directly connecting banks u_{i} and v_{i}.
It is guaranteed that the wires connect the banks in such a way that Inzane can somehow hack all the banks using a computer with appropriate strength.
-----Output-----
Print one integer — the minimum strength of the computer Inzane needs to accomplish the goal.
-----Examples-----
Input
5
1 2 3 4 5
1 2
2 3
3 4
4 5
Output
5
Input
7
38 -29 87 93 39 28 -55
1 2
2 5
3 2
2 4
1 7
7 6
Output
93
Input
5
1 2 7 6 7
1 5
5 3
3 4
2 4
Output
8
-----Note-----
In the first sample, Inzane can hack all banks using a computer with strength 5. Here is how: Initially, strengths of the banks are [1, 2, 3, 4, 5]. He hacks bank 5, then strengths of the banks become [1, 2, 4, 5, - ]. He hacks bank 4, then strengths of the banks become [1, 3, 5, - , - ]. He hacks bank 3, then strengths of the banks become [2, 4, - , - , - ]. He hacks bank 2, then strengths of the banks become [3, - , - , - , - ]. He completes his goal by hacking bank 1.
In the second sample, Inzane can hack banks 4, 2, 3, 1, 5, 7, and 6, in this order. This way, he can hack all banks using a computer with strength 93. | import sys
def solve():
n = int(sys.stdin.readline())
d = list(map(int, sys.stdin.readline().split()))
s = [[] for g in d]
mx_tmp = max(d)
mx_tmp2 = max(g for g in d + [-2000000000.0] if g < mx_tmp)
mxpt = [mx_tmp, mx_tmp2]
mxcnt = [d.count(mx_tmp), d.count(mx_tmp2)]
for i in range(1, n):
a, b = map(int, sys.stdin.readline().split())
a -= 1
b -= 1
s[a] += [b]
s[b] += [a]
mx = int(2000000000.0)
for i in range(n):
nmx = [] + mxcnt
tmpmax = d[i]
for k in s[i]:
if d[k] == mxpt[0]:
nmx[0] -= 1
elif d[k] == mxpt[1]:
nmx[1] -= 1
if nmx[0] != mxcnt[0]:
tmpmax = mxpt[0] + 1
elif nmx[1] != mxcnt[1]:
tmpmax = max(tmpmax, mxpt[1] + 1)
if d[i] == mxpt[0]:
nmx[0] -= 1
elif d[i] == mxpt[1]:
nmx[1] -= 1
if nmx[0]:
tmpmax = mxpt[0] + 2
elif nmx[1]:
tmpmax = max(mxpt[1] + 2, tmpmax)
mx = min(mx, tmpmax)
print(mx)
def __starting_point():
solve()
__starting_point() | IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR LIST NUMBER VAR VAR ASSIGN VAR LIST VAR VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR LIST VAR VAR VAR LIST VAR ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST VAR ASSIGN VAR VAR VAR FOR VAR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER NUMBER IF VAR VAR VAR NUMBER VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR NUMBER VAR NUMBER NUMBER IF VAR VAR VAR NUMBER VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR |
Although Inzane successfully found his beloved bone, Zane, his owner, has yet to return. To search for Zane, he would need a lot of money, of which he sadly has none. To deal with the problem, he has decided to hack the banks. [Image]
There are n banks, numbered from 1 to n. There are also n - 1 wires connecting the banks. All banks are initially online. Each bank also has its initial strength: bank i has initial strength a_{i}.
Let us define some keywords before we proceed. Bank i and bank j are neighboring if and only if there exists a wire directly connecting them. Bank i and bank j are semi-neighboring if and only if there exists an online bank k such that bank i and bank k are neighboring and bank k and bank j are neighboring.
When a bank is hacked, it becomes offline (and no longer online), and other banks that are neighboring or semi-neighboring to it have their strengths increased by 1.
To start his plan, Inzane will choose a bank to hack first. Indeed, the strength of such bank must not exceed the strength of his computer. After this, he will repeatedly choose some bank to hack next until all the banks are hacked, but he can continue to hack bank x if and only if all these conditions are met: Bank x is online. That is, bank x is not hacked yet. Bank x is neighboring to some offline bank. The strength of bank x is less than or equal to the strength of Inzane's computer.
Determine the minimum strength of the computer Inzane needs to hack all the banks.
-----Input-----
The first line contains one integer n (1 ≤ n ≤ 3·10^5) — the total number of banks.
The second line contains n integers a_1, a_2, ..., a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9) — the strengths of the banks.
Each of the next n - 1 lines contains two integers u_{i} and v_{i} (1 ≤ u_{i}, v_{i} ≤ n, u_{i} ≠ v_{i}) — meaning that there is a wire directly connecting banks u_{i} and v_{i}.
It is guaranteed that the wires connect the banks in such a way that Inzane can somehow hack all the banks using a computer with appropriate strength.
-----Output-----
Print one integer — the minimum strength of the computer Inzane needs to accomplish the goal.
-----Examples-----
Input
5
1 2 3 4 5
1 2
2 3
3 4
4 5
Output
5
Input
7
38 -29 87 93 39 28 -55
1 2
2 5
3 2
2 4
1 7
7 6
Output
93
Input
5
1 2 7 6 7
1 5
5 3
3 4
2 4
Output
8
-----Note-----
In the first sample, Inzane can hack all banks using a computer with strength 5. Here is how: Initially, strengths of the banks are [1, 2, 3, 4, 5]. He hacks bank 5, then strengths of the banks become [1, 2, 4, 5, - ]. He hacks bank 4, then strengths of the banks become [1, 3, 5, - , - ]. He hacks bank 3, then strengths of the banks become [2, 4, - , - , - ]. He hacks bank 2, then strengths of the banks become [3, - , - , - , - ]. He completes his goal by hacking bank 1.
In the second sample, Inzane can hack banks 4, 2, 3, 1, 5, 7, and 6, in this order. This way, he can hack all banks using a computer with strength 93. | def sol():
n = int(input())
st = list(map(int, input().split(" ")))
d = {}
for x in range(n):
d[x] = []
st = [(st[i], i) for i in range(len(st))]
st = sorted(st)
for a0 in range(n - 1):
u, v = map(int, input().split(" "))
u, v = u - 1, v - 1
d[u].append(v)
d[v].append(u)
hardest = []
almost = []
single_hardest = st[-1][0]
for x in st[::-1]:
if x[0] == single_hardest:
hardest.append(x[1])
elif x[0] == single_hardest - 1:
almost.append(x[1])
else:
break
def inter(a, b):
c = []
for x in a:
if x in b:
c.append(x)
return c
lower_bound = single_hardest
inte = d[hardest[0]] + [hardest[0]]
for h in hardest[1:]:
inte = inter(inte, d[h] + [h])
if not inte:
return single_hardest + 2
if len(hardest) > 1:
return single_hardest + 1
if not almost:
return single_hardest
cand = st[-1][1]
for h in almost:
if h not in d[cand]:
return single_hardest + 1
return single_hardest
print(sol()) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR NUMBER IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR LIST FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER LIST VAR NUMBER FOR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR LIST VAR IF VAR RETURN BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN BIN_OP VAR NUMBER IF VAR RETURN VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR IF VAR VAR VAR RETURN BIN_OP VAR NUMBER RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR |
Although Inzane successfully found his beloved bone, Zane, his owner, has yet to return. To search for Zane, he would need a lot of money, of which he sadly has none. To deal with the problem, he has decided to hack the banks. [Image]
There are n banks, numbered from 1 to n. There are also n - 1 wires connecting the banks. All banks are initially online. Each bank also has its initial strength: bank i has initial strength a_{i}.
Let us define some keywords before we proceed. Bank i and bank j are neighboring if and only if there exists a wire directly connecting them. Bank i and bank j are semi-neighboring if and only if there exists an online bank k such that bank i and bank k are neighboring and bank k and bank j are neighboring.
When a bank is hacked, it becomes offline (and no longer online), and other banks that are neighboring or semi-neighboring to it have their strengths increased by 1.
To start his plan, Inzane will choose a bank to hack first. Indeed, the strength of such bank must not exceed the strength of his computer. After this, he will repeatedly choose some bank to hack next until all the banks are hacked, but he can continue to hack bank x if and only if all these conditions are met: Bank x is online. That is, bank x is not hacked yet. Bank x is neighboring to some offline bank. The strength of bank x is less than or equal to the strength of Inzane's computer.
Determine the minimum strength of the computer Inzane needs to hack all the banks.
-----Input-----
The first line contains one integer n (1 ≤ n ≤ 3·10^5) — the total number of banks.
The second line contains n integers a_1, a_2, ..., a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9) — the strengths of the banks.
Each of the next n - 1 lines contains two integers u_{i} and v_{i} (1 ≤ u_{i}, v_{i} ≤ n, u_{i} ≠ v_{i}) — meaning that there is a wire directly connecting banks u_{i} and v_{i}.
It is guaranteed that the wires connect the banks in such a way that Inzane can somehow hack all the banks using a computer with appropriate strength.
-----Output-----
Print one integer — the minimum strength of the computer Inzane needs to accomplish the goal.
-----Examples-----
Input
5
1 2 3 4 5
1 2
2 3
3 4
4 5
Output
5
Input
7
38 -29 87 93 39 28 -55
1 2
2 5
3 2
2 4
1 7
7 6
Output
93
Input
5
1 2 7 6 7
1 5
5 3
3 4
2 4
Output
8
-----Note-----
In the first sample, Inzane can hack all banks using a computer with strength 5. Here is how: Initially, strengths of the banks are [1, 2, 3, 4, 5]. He hacks bank 5, then strengths of the banks become [1, 2, 4, 5, - ]. He hacks bank 4, then strengths of the banks become [1, 3, 5, - , - ]. He hacks bank 3, then strengths of the banks become [2, 4, - , - , - ]. He hacks bank 2, then strengths of the banks become [3, - , - , - , - ]. He completes his goal by hacking bank 1.
In the second sample, Inzane can hack banks 4, 2, 3, 1, 5, 7, and 6, in this order. This way, he can hack all banks using a computer with strength 93. | import sys
n = int(sys.stdin.readline())
d = list(map(int, sys.stdin.readline().split()))
s = [[] for g in d]
mxpt = [-2000000000.0, -2000000000.0]
mxcnt = [0, 0]
for i in d:
if i > mxpt[0]:
mxpt[1] = mxpt[0]
mxcnt[1] = mxcnt[0]
mxpt[0] = i
mxcnt[0] = 1
elif i == mxpt[0]:
mxcnt[0] += 1
elif i > mxpt[1]:
mxpt[1] = i
mxcnt[1] = 1
else:
mxcnt[1] += i == mxpt[1]
for i in range(1, n):
a, b = map(int, sys.stdin.readline().split())
a -= 1
b -= 1
s[a] += [b]
s[b] += [a]
mx = int(2000000000.0)
for i in range(n):
nmx = [] + mxcnt
tmpmax = d[i]
for k in s[i]:
if d[k] == mxpt[0]:
nmx[0] -= 1
elif d[k] == mxpt[1]:
nmx[1] -= 1
if nmx[0] != mxcnt[0]:
tmpmax = mxpt[0] + 1
elif nmx[1] != mxcnt[1]:
tmpmax = max(tmpmax, mxpt[1] + 1)
if d[i] == mxpt[0]:
nmx[0] -= 1
elif d[i] == mxpt[1]:
nmx[1] -= 1
if nmx[0]:
tmpmax = mxpt[0] + 2
elif nmx[1]:
tmpmax = max(mxpt[1] + 2, tmpmax)
mx = min(mx, tmpmax)
print(mx) | IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST VAR VAR ASSIGN VAR LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER FOR VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER NUMBER IF VAR VAR NUMBER VAR NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR VAR LIST VAR VAR VAR LIST VAR ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST VAR ASSIGN VAR VAR VAR FOR VAR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER NUMBER IF VAR VAR VAR NUMBER VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR NUMBER VAR NUMBER NUMBER IF VAR VAR VAR NUMBER VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Although Inzane successfully found his beloved bone, Zane, his owner, has yet to return. To search for Zane, he would need a lot of money, of which he sadly has none. To deal with the problem, he has decided to hack the banks. [Image]
There are n banks, numbered from 1 to n. There are also n - 1 wires connecting the banks. All banks are initially online. Each bank also has its initial strength: bank i has initial strength a_{i}.
Let us define some keywords before we proceed. Bank i and bank j are neighboring if and only if there exists a wire directly connecting them. Bank i and bank j are semi-neighboring if and only if there exists an online bank k such that bank i and bank k are neighboring and bank k and bank j are neighboring.
When a bank is hacked, it becomes offline (and no longer online), and other banks that are neighboring or semi-neighboring to it have their strengths increased by 1.
To start his plan, Inzane will choose a bank to hack first. Indeed, the strength of such bank must not exceed the strength of his computer. After this, he will repeatedly choose some bank to hack next until all the banks are hacked, but he can continue to hack bank x if and only if all these conditions are met: Bank x is online. That is, bank x is not hacked yet. Bank x is neighboring to some offline bank. The strength of bank x is less than or equal to the strength of Inzane's computer.
Determine the minimum strength of the computer Inzane needs to hack all the banks.
-----Input-----
The first line contains one integer n (1 ≤ n ≤ 3·10^5) — the total number of banks.
The second line contains n integers a_1, a_2, ..., a_{n} ( - 10^9 ≤ a_{i} ≤ 10^9) — the strengths of the banks.
Each of the next n - 1 lines contains two integers u_{i} and v_{i} (1 ≤ u_{i}, v_{i} ≤ n, u_{i} ≠ v_{i}) — meaning that there is a wire directly connecting banks u_{i} and v_{i}.
It is guaranteed that the wires connect the banks in such a way that Inzane can somehow hack all the banks using a computer with appropriate strength.
-----Output-----
Print one integer — the minimum strength of the computer Inzane needs to accomplish the goal.
-----Examples-----
Input
5
1 2 3 4 5
1 2
2 3
3 4
4 5
Output
5
Input
7
38 -29 87 93 39 28 -55
1 2
2 5
3 2
2 4
1 7
7 6
Output
93
Input
5
1 2 7 6 7
1 5
5 3
3 4
2 4
Output
8
-----Note-----
In the first sample, Inzane can hack all banks using a computer with strength 5. Here is how: Initially, strengths of the banks are [1, 2, 3, 4, 5]. He hacks bank 5, then strengths of the banks become [1, 2, 4, 5, - ]. He hacks bank 4, then strengths of the banks become [1, 3, 5, - , - ]. He hacks bank 3, then strengths of the banks become [2, 4, - , - , - ]. He hacks bank 2, then strengths of the banks become [3, - , - , - , - ]. He completes his goal by hacking bank 1.
In the second sample, Inzane can hack banks 4, 2, 3, 1, 5, 7, and 6, in this order. This way, he can hack all banks using a computer with strength 93. | n = int(input())
strengths = list(map(int, input().split()))
max_strength = max(strengths)
count_max = strengths.count(max_strength)
count_second_place = strengths.count(max_strength - 1)
maxes = [(0) for i in range(n)]
second_places = [(0) for i in range(n)]
for i in range(n - 1):
a, b = list(map(int, input().split()))
a -= 1
b -= 1
if strengths[a] == max_strength:
maxes[b] += 1
elif strengths[a] == max_strength - 1:
second_places[b] += 1
if strengths[b] == max_strength:
maxes[a] += 1
elif strengths[b] == max_strength - 1:
second_places[a] += 1
total_max = 1000000009
for i in range(n):
here = 0
if strengths[i] < max_strength:
if maxes[i] == count_max:
here = max_strength + 1
else:
here = max_strength + 2
elif count_max == 1:
if second_places[i] == count_second_place:
here = max_strength
else:
here = max_strength + 1
elif maxes[i] == count_max - 1:
here = max_strength + 1
else:
here = max_strength + 2
total_max = min(total_max, here)
print(total_max) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR |
Teddy and Tracy like to play a game based on strings. The game is as follows. Initially, Tracy writes a long random string on a whiteboard. Then, each player starting with Teddy makes turn alternately. Each turn, the player must erase a contiguous substring that exists in the dictionary. The dictionary consists of N words.
Of course, the player that can't erase any substring in his turn loses the game, and the other player is declared the winner.
Note that after a substring R is erased, the remaining substring becomes separated, i.e. they cannot erase a word that occurs partially to the left of R and partially to the right of R.
Determine the winner of the game, assuming that both players play optimally.
------ Input ------
The first line contains a single integer T, the number of test cases. T test cases follow. The first line of each testcase contains a string S, the string Tracy writes on the whiteboard. The next line contains a single integer N. N lines follow. The i-th line contains a single string w_{i}, the i-th word in the dictionary.
------ Output ------
For each test case, output a single line containing the name of the winner of the game.
------ Constraints ------
$1 ≤ T ≤ 5$
$1 ≤ N ≤ 30$
$1 ≤ |S| ≤ 30$
$1 ≤ |w_{i}| ≤ 30$
$S and w_{i} contain only characters 'a'-'z'$
----- Sample Input 1 ------
3
codechef
2
code
chef
foo
1
bar
mississippi
4
ssissi
mippi
mi
ppi
----- Sample Output 1 ------
Tracy
Tracy
Teddy | def grundy(g):
if g in gdic:
return gdic[g]
lg = len(g)
subg = set()
for ln, w in words:
pos = g.find(w)
while pos >= 0:
gpre = grundy(g[:pos])
gpost = grundy(g[pos + ln :])
subg.add(gpre ^ gpost)
pos = g.find(w, pos + 1)
if len(subg) == 0:
gdic[g] = 0
else:
mex = 0
while mex in subg:
mex += 1
gdic[g] = mex
return gdic[g]
for _ in range(int(input())):
game = input().strip()
n = int(input())
wordset = set(input().strip() for _ in range(n))
words = [(len(w), w) for w in wordset]
gdic = {"": 0}
print("Teddy" if grundy(game) > 0 else "Tracy") | FUNC_DEF IF VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR DICT STRING NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER STRING STRING |
Let's define a function countUniqueChars(s) that returns the number of unique characters on s, for example if s = "LEETCODE" then "L", "T","C","O","D" are the unique characters since they appear only once in s, therefore countUniqueChars(s) = 5.
On this problem given a string s we need to return the sum of countUniqueChars(t) where t is a substring of s. Notice that some substrings can be repeated so on this case you have to count the repeated ones too.
Since the answer can be very large, return the answer modulo 10 ^ 9 + 7.
Example 1:
Input: s = "ABC"
Output: 10
Explanation: All possible substrings are: "A","B","C","AB","BC" and "ABC".
Evey substring is composed with only unique letters.
Sum of lengths of all substring is 1 + 1 + 1 + 2 + 2 + 3 = 10
Example 2:
Input: s = "ABA"
Output: 8
Explanation: The same as example 1, except countUniqueChars("ABA") = 1.
Example 3:
Input: s = "LEETCODE"
Output: 92
Constraints:
0 <= s.length <= 10^4
s contain upper-case English letters only. | class Solution:
def uniqueLetterString(self, s: str) -> int:
chrLoc = defaultdict(list)
ct = 0
md = 1000000007
l = len(s)
for i, c in enumerate(s):
chrLoc[c].append(i)
for c in chrLoc:
locs = [-1] + chrLoc[c] + [l]
loc_ct = len(locs)
for i in range(1, loc_ct - 1):
leftWingSpan = locs[i] - locs[i - 1]
rightWingSpan = locs[i + 1] - locs[i]
ct += leftWingSpan % md * (rightWingSpan % md) % md
ct %= md
return ct | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER VAR VAR LIST VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR RETURN VAR VAR |
Let's define a function countUniqueChars(s) that returns the number of unique characters on s, for example if s = "LEETCODE" then "L", "T","C","O","D" are the unique characters since they appear only once in s, therefore countUniqueChars(s) = 5.
On this problem given a string s we need to return the sum of countUniqueChars(t) where t is a substring of s. Notice that some substrings can be repeated so on this case you have to count the repeated ones too.
Since the answer can be very large, return the answer modulo 10 ^ 9 + 7.
Example 1:
Input: s = "ABC"
Output: 10
Explanation: All possible substrings are: "A","B","C","AB","BC" and "ABC".
Evey substring is composed with only unique letters.
Sum of lengths of all substring is 1 + 1 + 1 + 2 + 2 + 3 = 10
Example 2:
Input: s = "ABA"
Output: 8
Explanation: The same as example 1, except countUniqueChars("ABA") = 1.
Example 3:
Input: s = "LEETCODE"
Output: 92
Constraints:
0 <= s.length <= 10^4
s contain upper-case English letters only. | class Solution:
def uniqueLetterString(self, s: str) -> int:
if not s:
return 0
dp = collections.defaultdict(list)
for index, val in enumerate(s):
dp[val].append(index)
ans = 0
for A in list(dp.values()):
A = [-1] + A + [len(s)]
for i in range(1, len(A) - 1):
ans += (A[i] - A[i - 1]) * (A[i + 1] - A[i])
return ans % (10**9 + 7) | CLASS_DEF FUNC_DEF VAR IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER VAR LIST FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR |
Let's define a function countUniqueChars(s) that returns the number of unique characters on s, for example if s = "LEETCODE" then "L", "T","C","O","D" are the unique characters since they appear only once in s, therefore countUniqueChars(s) = 5.
On this problem given a string s we need to return the sum of countUniqueChars(t) where t is a substring of s. Notice that some substrings can be repeated so on this case you have to count the repeated ones too.
Since the answer can be very large, return the answer modulo 10 ^ 9 + 7.
Example 1:
Input: s = "ABC"
Output: 10
Explanation: All possible substrings are: "A","B","C","AB","BC" and "ABC".
Evey substring is composed with only unique letters.
Sum of lengths of all substring is 1 + 1 + 1 + 2 + 2 + 3 = 10
Example 2:
Input: s = "ABA"
Output: 8
Explanation: The same as example 1, except countUniqueChars("ABA") = 1.
Example 3:
Input: s = "LEETCODE"
Output: 92
Constraints:
0 <= s.length <= 10^4
s contain upper-case English letters only. | class Solution:
def uniqueLetterString(self, s: str) -> int:
last = collections.defaultdict(int)
count = 0
for i, char in enumerate(s):
for comp in last:
if comp != char:
count += last[comp][1]
else:
count += i - last[comp][0]
last[char] = i, i - last[comp][0]
if char not in last:
count += i + 1
last[char] = i, i + 1
return count | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR |
Let's define a function countUniqueChars(s) that returns the number of unique characters on s, for example if s = "LEETCODE" then "L", "T","C","O","D" are the unique characters since they appear only once in s, therefore countUniqueChars(s) = 5.
On this problem given a string s we need to return the sum of countUniqueChars(t) where t is a substring of s. Notice that some substrings can be repeated so on this case you have to count the repeated ones too.
Since the answer can be very large, return the answer modulo 10 ^ 9 + 7.
Example 1:
Input: s = "ABC"
Output: 10
Explanation: All possible substrings are: "A","B","C","AB","BC" and "ABC".
Evey substring is composed with only unique letters.
Sum of lengths of all substring is 1 + 1 + 1 + 2 + 2 + 3 = 10
Example 2:
Input: s = "ABA"
Output: 8
Explanation: The same as example 1, except countUniqueChars("ABA") = 1.
Example 3:
Input: s = "LEETCODE"
Output: 92
Constraints:
0 <= s.length <= 10^4
s contain upper-case English letters only. | class Solution:
def uniqueLetterString(self, s: str) -> int:
def count(c, s):
n = len(s)
idxes = []
for i in range(n):
if s[i] == c:
idxes.append(i)
total = 0
for i, idx in enumerate(idxes):
left = idx - 0 + 1
right = n - 1 - idx + 1
if i:
left = idx - idxes[i - 1]
if i < len(idxes) - 1:
right = idxes[i + 1] - idx
total += left * right
return total
chars = set(s)
ans = 0
for c in chars:
ans += count(c, s)
ans = ans % (10**9 + 7)
return ans | CLASS_DEF FUNC_DEF VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN VAR VAR |
Let's define a function countUniqueChars(s) that returns the number of unique characters on s, for example if s = "LEETCODE" then "L", "T","C","O","D" are the unique characters since they appear only once in s, therefore countUniqueChars(s) = 5.
On this problem given a string s we need to return the sum of countUniqueChars(t) where t is a substring of s. Notice that some substrings can be repeated so on this case you have to count the repeated ones too.
Since the answer can be very large, return the answer modulo 10 ^ 9 + 7.
Example 1:
Input: s = "ABC"
Output: 10
Explanation: All possible substrings are: "A","B","C","AB","BC" and "ABC".
Evey substring is composed with only unique letters.
Sum of lengths of all substring is 1 + 1 + 1 + 2 + 2 + 3 = 10
Example 2:
Input: s = "ABA"
Output: 8
Explanation: The same as example 1, except countUniqueChars("ABA") = 1.
Example 3:
Input: s = "LEETCODE"
Output: 92
Constraints:
0 <= s.length <= 10^4
s contain upper-case English letters only. | class Solution:
def uniqueLetterString(self, s: str) -> int:
ind = collections.defaultdict(list)
for i, v in enumerate(s):
ind[v].append(i)
ans, n = 0, len(s)
for i, v in ind.items():
temp = 0
for k, index in enumerate(v):
if k == 0 and k == len(v) - 1:
temp += (index + 1) * (n - index)
elif k == 0:
temp += (index + 1) * (v[k + 1] - index)
elif k > 0 and k < len(v) - 1:
temp += (index - v[k - 1]) * (v[k + 1] - index)
elif k == len(v) - 1:
temp += (index - v[k - 1]) * (n - index)
ans += temp
return ans | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR IF VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR RETURN VAR VAR |
Let's define a function countUniqueChars(s) that returns the number of unique characters on s, for example if s = "LEETCODE" then "L", "T","C","O","D" are the unique characters since they appear only once in s, therefore countUniqueChars(s) = 5.
On this problem given a string s we need to return the sum of countUniqueChars(t) where t is a substring of s. Notice that some substrings can be repeated so on this case you have to count the repeated ones too.
Since the answer can be very large, return the answer modulo 10 ^ 9 + 7.
Example 1:
Input: s = "ABC"
Output: 10
Explanation: All possible substrings are: "A","B","C","AB","BC" and "ABC".
Evey substring is composed with only unique letters.
Sum of lengths of all substring is 1 + 1 + 1 + 2 + 2 + 3 = 10
Example 2:
Input: s = "ABA"
Output: 8
Explanation: The same as example 1, except countUniqueChars("ABA") = 1.
Example 3:
Input: s = "LEETCODE"
Output: 92
Constraints:
0 <= s.length <= 10^4
s contain upper-case English letters only. | class Solution:
def uniqueLetterString(self, s: str) -> int:
if not s:
return 0
ind = defaultdict(list)
for i, ch in enumerate(s):
ind[ch].append(i)
res = 0
for seq in ind.values():
for i, index in enumerate(seq):
len_left, len_right = 0, 0
if i > 0:
len_left = index - seq[i - 1] - 1
else:
len_left = index
if i + 1 < len(seq):
len_right = seq[i + 1] - index - 1
else:
len_right = len(s) - index - 1
res += (len_left + 1) * (len_right + 1)
return res | CLASS_DEF FUNC_DEF VAR IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR VAR |
Let's define a function countUniqueChars(s) that returns the number of unique characters on s, for example if s = "LEETCODE" then "L", "T","C","O","D" are the unique characters since they appear only once in s, therefore countUniqueChars(s) = 5.
On this problem given a string s we need to return the sum of countUniqueChars(t) where t is a substring of s. Notice that some substrings can be repeated so on this case you have to count the repeated ones too.
Since the answer can be very large, return the answer modulo 10 ^ 9 + 7.
Example 1:
Input: s = "ABC"
Output: 10
Explanation: All possible substrings are: "A","B","C","AB","BC" and "ABC".
Evey substring is composed with only unique letters.
Sum of lengths of all substring is 1 + 1 + 1 + 2 + 2 + 3 = 10
Example 2:
Input: s = "ABA"
Output: 8
Explanation: The same as example 1, except countUniqueChars("ABA") = 1.
Example 3:
Input: s = "LEETCODE"
Output: 92
Constraints:
0 <= s.length <= 10^4
s contain upper-case English letters only. | class Solution:
def uniqueLetterString(self, s: str) -> int:
mod = 1000000007
n = len(s)
char_locs = defaultdict(list)
for i in range(n):
c = s[i]
if not char_locs[c]:
char_locs[c].append(-1)
char_locs[c].append(i)
ans = 0
for c in char_locs:
char_locs[c].append(n)
indices = char_locs[c]
for i in range(1, len(indices) - 1):
before = indices[i] - indices[i - 1]
after = indices[i + 1] - indices[i]
ans = (ans + before * after) % mod
return ans | CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR RETURN VAR VAR |
Let's define a function countUniqueChars(s) that returns the number of unique characters on s, for example if s = "LEETCODE" then "L", "T","C","O","D" are the unique characters since they appear only once in s, therefore countUniqueChars(s) = 5.
On this problem given a string s we need to return the sum of countUniqueChars(t) where t is a substring of s. Notice that some substrings can be repeated so on this case you have to count the repeated ones too.
Since the answer can be very large, return the answer modulo 10 ^ 9 + 7.
Example 1:
Input: s = "ABC"
Output: 10
Explanation: All possible substrings are: "A","B","C","AB","BC" and "ABC".
Evey substring is composed with only unique letters.
Sum of lengths of all substring is 1 + 1 + 1 + 2 + 2 + 3 = 10
Example 2:
Input: s = "ABA"
Output: 8
Explanation: The same as example 1, except countUniqueChars("ABA") = 1.
Example 3:
Input: s = "LEETCODE"
Output: 92
Constraints:
0 <= s.length <= 10^4
s contain upper-case English letters only. | class Solution:
def uniqueLetterString(self, s: str) -> int:
index = {
char: (
[-1] + [ic for ic, char0 in enumerate(s) if char == char0] + [len(s)]
)
for char in set(s)
}
return sum(
[
(
(index[char][jj] - index[char][jj - 1])
* (index[char][jj + 1] - index[char][jj])
)
for char in index
for jj in range(1, len(index[char]) - 1)
]
) | CLASS_DEF FUNC_DEF VAR ASSIGN VAR VAR BIN_OP BIN_OP LIST NUMBER VAR VAR VAR FUNC_CALL VAR VAR VAR VAR LIST FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR |
Let's define a function countUniqueChars(s) that returns the number of unique characters on s, for example if s = "LEETCODE" then "L", "T","C","O","D" are the unique characters since they appear only once in s, therefore countUniqueChars(s) = 5.
On this problem given a string s we need to return the sum of countUniqueChars(t) where t is a substring of s. Notice that some substrings can be repeated so on this case you have to count the repeated ones too.
Since the answer can be very large, return the answer modulo 10 ^ 9 + 7.
Example 1:
Input: s = "ABC"
Output: 10
Explanation: All possible substrings are: "A","B","C","AB","BC" and "ABC".
Evey substring is composed with only unique letters.
Sum of lengths of all substring is 1 + 1 + 1 + 2 + 2 + 3 = 10
Example 2:
Input: s = "ABA"
Output: 8
Explanation: The same as example 1, except countUniqueChars("ABA") = 1.
Example 3:
Input: s = "LEETCODE"
Output: 92
Constraints:
0 <= s.length <= 10^4
s contain upper-case English letters only. | class Solution:
def uniqueLetterString(self, s: str) -> int:
result = 0
mat = collections.defaultdict(list)
for i, ch in enumerate(s):
if mat[ch]:
mat[ch].append(i)
else:
mat[ch] = [i]
def helperF(ranges, n):
res = 0
i = 0
while i < len(ranges):
elem = ranges[i]
lef = None
right = None
if i > 0:
left = ranges[i] - ranges[i - 1]
else:
left = ranges[i] + 1
if i != len(ranges) - 1:
right = ranges[i + 1] - ranges[i]
else:
right = n - ranges[i]
res += left * right
i += 1
return res
for ch in mat:
result += helperF(mat[ch], len(s))
return result | CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR LIST VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR FOR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR |
Let's define a function countUniqueChars(s) that returns the number of unique characters on s, for example if s = "LEETCODE" then "L", "T","C","O","D" are the unique characters since they appear only once in s, therefore countUniqueChars(s) = 5.
On this problem given a string s we need to return the sum of countUniqueChars(t) where t is a substring of s. Notice that some substrings can be repeated so on this case you have to count the repeated ones too.
Since the answer can be very large, return the answer modulo 10 ^ 9 + 7.
Example 1:
Input: s = "ABC"
Output: 10
Explanation: All possible substrings are: "A","B","C","AB","BC" and "ABC".
Evey substring is composed with only unique letters.
Sum of lengths of all substring is 1 + 1 + 1 + 2 + 2 + 3 = 10
Example 2:
Input: s = "ABA"
Output: 8
Explanation: The same as example 1, except countUniqueChars("ABA") = 1.
Example 3:
Input: s = "LEETCODE"
Output: 92
Constraints:
0 <= s.length <= 10^4
s contain upper-case English letters only. | class Solution:
def uniqueLetterString(self, s: str) -> int:
dic = {}
for i in range(len(s)):
l = dic.get(s[i], [-1])
l.append(i)
dic[s[i]] = l
res = 0
for j in dic:
dic[j].append(len(s))
for i in range(1, len(dic[j]) - 1):
res += (dic[j][i] - dic[j][i - 1]) * (dic[j][i + 1] - dic[j][i])
return res % (10**9 + 7) | CLASS_DEF FUNC_DEF VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR LIST NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN BIN_OP VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER VAR |
Let's define a function countUniqueChars(s) that returns the number of unique characters on s, for example if s = "LEETCODE" then "L", "T","C","O","D" are the unique characters since they appear only once in s, therefore countUniqueChars(s) = 5.
On this problem given a string s we need to return the sum of countUniqueChars(t) where t is a substring of s. Notice that some substrings can be repeated so on this case you have to count the repeated ones too.
Since the answer can be very large, return the answer modulo 10 ^ 9 + 7.
Example 1:
Input: s = "ABC"
Output: 10
Explanation: All possible substrings are: "A","B","C","AB","BC" and "ABC".
Evey substring is composed with only unique letters.
Sum of lengths of all substring is 1 + 1 + 1 + 2 + 2 + 3 = 10
Example 2:
Input: s = "ABA"
Output: 8
Explanation: The same as example 1, except countUniqueChars("ABA") = 1.
Example 3:
Input: s = "LEETCODE"
Output: 92
Constraints:
0 <= s.length <= 10^4
s contain upper-case English letters only. | class Solution:
def uniqueLetterString(self, s: str) -> int:
dic = collections.defaultdict(list)
for i, e in enumerate(s):
dic[e].append(i)
for k in dic.keys():
dic[k].append(len(s))
dic[k].insert(0, -1)
res = 0
for v in dic.values():
for i in range(1, len(v) - 1):
res += (v[i] - v[i - 1]) * (v[i + 1] - v[i]) % (10**9 + 7)
return res | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN VAR VAR |
Let's define a function countUniqueChars(s) that returns the number of unique characters on s, for example if s = "LEETCODE" then "L", "T","C","O","D" are the unique characters since they appear only once in s, therefore countUniqueChars(s) = 5.
On this problem given a string s we need to return the sum of countUniqueChars(t) where t is a substring of s. Notice that some substrings can be repeated so on this case you have to count the repeated ones too.
Since the answer can be very large, return the answer modulo 10 ^ 9 + 7.
Example 1:
Input: s = "ABC"
Output: 10
Explanation: All possible substrings are: "A","B","C","AB","BC" and "ABC".
Evey substring is composed with only unique letters.
Sum of lengths of all substring is 1 + 1 + 1 + 2 + 2 + 3 = 10
Example 2:
Input: s = "ABA"
Output: 8
Explanation: The same as example 1, except countUniqueChars("ABA") = 1.
Example 3:
Input: s = "LEETCODE"
Output: 92
Constraints:
0 <= s.length <= 10^4
s contain upper-case English letters only. | class Solution:
def uniqueLetterString(self, s: str) -> int:
ans, d = 0, defaultdict(lambda: (-1, -1))
for i, c in enumerate(s):
ans += (d[c][1] - d[c][0]) * (i - d[c][1])
d[c] = d[c][1], i
for c, pre in d.items():
ans += (d[c][1] - d[c][0]) * (len(s) - d[c][1])
return ans | CLASS_DEF FUNC_DEF VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR VAR |
Let's define a function countUniqueChars(s) that returns the number of unique characters on s, for example if s = "LEETCODE" then "L", "T","C","O","D" are the unique characters since they appear only once in s, therefore countUniqueChars(s) = 5.
On this problem given a string s we need to return the sum of countUniqueChars(t) where t is a substring of s. Notice that some substrings can be repeated so on this case you have to count the repeated ones too.
Since the answer can be very large, return the answer modulo 10 ^ 9 + 7.
Example 1:
Input: s = "ABC"
Output: 10
Explanation: All possible substrings are: "A","B","C","AB","BC" and "ABC".
Evey substring is composed with only unique letters.
Sum of lengths of all substring is 1 + 1 + 1 + 2 + 2 + 3 = 10
Example 2:
Input: s = "ABA"
Output: 8
Explanation: The same as example 1, except countUniqueChars("ABA") = 1.
Example 3:
Input: s = "LEETCODE"
Output: 92
Constraints:
0 <= s.length <= 10^4
s contain upper-case English letters only. | class Solution:
def uniqueLetterString(self, s: str) -> int:
n = len(s)
mod = 10**9 + 7
index = defaultdict(deque)
for i, c in enumerate(s):
index[c].append(i)
for c in index:
index[c].append(n)
index[c].appendleft(-1)
ans = 0
for c in list(index.keys()):
for i in range(1, len(index[c]) - 1):
ans = (
ans
+ (index[c][i] - index[c][i - 1]) * (index[c][i + 1] - index[c][i])
) % mod
return ans | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN VAR VAR |
Let's define a function countUniqueChars(s) that returns the number of unique characters on s, for example if s = "LEETCODE" then "L", "T","C","O","D" are the unique characters since they appear only once in s, therefore countUniqueChars(s) = 5.
On this problem given a string s we need to return the sum of countUniqueChars(t) where t is a substring of s. Notice that some substrings can be repeated so on this case you have to count the repeated ones too.
Since the answer can be very large, return the answer modulo 10 ^ 9 + 7.
Example 1:
Input: s = "ABC"
Output: 10
Explanation: All possible substrings are: "A","B","C","AB","BC" and "ABC".
Evey substring is composed with only unique letters.
Sum of lengths of all substring is 1 + 1 + 1 + 2 + 2 + 3 = 10
Example 2:
Input: s = "ABA"
Output: 8
Explanation: The same as example 1, except countUniqueChars("ABA") = 1.
Example 3:
Input: s = "LEETCODE"
Output: 92
Constraints:
0 <= s.length <= 10^4
s contain upper-case English letters only. | class Solution:
def uniqueLetterString(self, s: str) -> int:
n = len(s)
dbefore = {}
dafter = {}
before = [-1] * n
after = [n] * n
for i in range(n):
before[i] = dbefore.get(s[i], -1)
after[n - 1 - i] = dafter.get(s[n - 1 - i], n)
dbefore[s[i]] = i
dafter[s[n - 1 - i]] = n - 1 - i
c = 10**9 + 7
res = 0
for i in range(n):
res += (i - before[i]) * (after[i] - i)
res %= c
return res | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR RETURN VAR VAR |
Given two strings X and Y of lengths m and n respectively, find the length of the smallest string which has both, X and Y as its sub-sequences.
Note: X and Y can have both uppercase and lowercase letters.
Example 1
Input:
X = abcd, Y = xycd
Output: 6
Explanation: Shortest Common Supersequence
would be abxycd which is of length 6 and
has both the strings as its subsequences.
Example 2
Input:
X = efgh, Y = jghi
Output: 6
Explanation: Shortest Common Supersequence
would be ejfghi which is of length 6 and
has both the strings as its subsequences.
Your Task:
Complete shortestCommonSupersequence() function that takes X, Y, m, and n as arguments and returns the length of the required string.
Expected Time Complexity: O(Length(X) * Length(Y)).
Expected Auxiliary Space: O(Length(X) * Length(Y)).
Constraints:
1<= |X|, |Y| <= 100 | class Solution:
def shortestCommonSupersequence(self, X, Y, m, n):
a = X
b = Y
n = len(a)
m = len(b)
S = [[(0) for j in range(m)] for i in range(n)]
S[0][0] = 1 if a[0] == b[0] else 0
for i in range(1, n):
if a[i] == b[0]:
S[i][0] = 1
else:
S[i][0] = S[i - 1][0]
for j in range(1, m):
if a[0] == b[j]:
S[0][j] = 1
else:
S[0][j] = S[0][j - 1]
for i in range(1, n):
for j in range(1, m):
if a[i] == b[j]:
S[i][j] = S[i - 1][j - 1] + 1
else:
S[i][j] = max(S[i - 1][j], S[i][j - 1])
return n + m - S[n - 1][m - 1] | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER VAR VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER |
Given two strings X and Y of lengths m and n respectively, find the length of the smallest string which has both, X and Y as its sub-sequences.
Note: X and Y can have both uppercase and lowercase letters.
Example 1
Input:
X = abcd, Y = xycd
Output: 6
Explanation: Shortest Common Supersequence
would be abxycd which is of length 6 and
has both the strings as its subsequences.
Example 2
Input:
X = efgh, Y = jghi
Output: 6
Explanation: Shortest Common Supersequence
would be ejfghi which is of length 6 and
has both the strings as its subsequences.
Your Task:
Complete shortestCommonSupersequence() function that takes X, Y, m, and n as arguments and returns the length of the required string.
Expected Time Complexity: O(Length(X) * Length(Y)).
Expected Auxiliary Space: O(Length(X) * Length(Y)).
Constraints:
1<= |X|, |Y| <= 100 | class Solution:
def func(self, X, Y, m, n, t):
for i in range(m + 1):
for j in range(n + 1):
if i == 0 or j == 0:
t[i][j] = 0
for i in range(1, m + 1):
for j in range(1, n + 1):
if X[i - 1] == Y[j - 1]:
t[i][j] = 1 + t[i - 1][j - 1]
else:
t[i][j] = max(t[i - 1][j], t[i][j - 1])
return t[m][n]
def LCS(self, X, Y, m, n):
t = [[(0) for i in range(n + 1)] for i in range(m + 1)]
c = self.func(X, Y, m, n, t)
return c
def shortestCommonSupersequence(self, X, Y, m, n):
s = m + n - self.LCS(X, Y, m, n)
return s | CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR |
Given two strings X and Y of lengths m and n respectively, find the length of the smallest string which has both, X and Y as its sub-sequences.
Note: X and Y can have both uppercase and lowercase letters.
Example 1
Input:
X = abcd, Y = xycd
Output: 6
Explanation: Shortest Common Supersequence
would be abxycd which is of length 6 and
has both the strings as its subsequences.
Example 2
Input:
X = efgh, Y = jghi
Output: 6
Explanation: Shortest Common Supersequence
would be ejfghi which is of length 6 and
has both the strings as its subsequences.
Your Task:
Complete shortestCommonSupersequence() function that takes X, Y, m, and n as arguments and returns the length of the required string.
Expected Time Complexity: O(Length(X) * Length(Y)).
Expected Auxiliary Space: O(Length(X) * Length(Y)).
Constraints:
1<= |X|, |Y| <= 100 | class Solution:
def shortestCommonSupersequence(self, X, Y, m, n):
def LCS(x, y, m, n):
t = [([-1] * (n + 1)) for i in range(m + 1)]
for i in range(m + 1):
for j in range(n + 1):
if i == 0 or j == 0:
t[i][j] == 0
for i in range(1, m + 1):
for j in range(1, n + 1):
if x[i - 1] == y[j - 1]:
t[i][j] = 1 + t[i - 1][j - 1]
else:
t[i][j] = max(t[i - 1][j], t[i][j - 1])
return t[m][n]
t = LCS(X, Y, m, n)
return m + n - t - 1 | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER |
Given two strings X and Y of lengths m and n respectively, find the length of the smallest string which has both, X and Y as its sub-sequences.
Note: X and Y can have both uppercase and lowercase letters.
Example 1
Input:
X = abcd, Y = xycd
Output: 6
Explanation: Shortest Common Supersequence
would be abxycd which is of length 6 and
has both the strings as its subsequences.
Example 2
Input:
X = efgh, Y = jghi
Output: 6
Explanation: Shortest Common Supersequence
would be ejfghi which is of length 6 and
has both the strings as its subsequences.
Your Task:
Complete shortestCommonSupersequence() function that takes X, Y, m, and n as arguments and returns the length of the required string.
Expected Time Complexity: O(Length(X) * Length(Y)).
Expected Auxiliary Space: O(Length(X) * Length(Y)).
Constraints:
1<= |X|, |Y| <= 100 | class Solution:
def shortestCommonSupersequence(self, X, Y, m, n):
dp = [([-1] * (n + 1)) for i in range(m + 1)]
for i in range(m + 1):
dp[i][0] = 0
for j in range(n + 1):
dp[0][j] = 0
for i in range(1, m + 1):
for j in range(1, n + 1):
if X[i - 1] == Y[j - 1]:
dp[i][j] = 1 + dp[i - 1][j - 1]
else:
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
i = m
j = n
charstr = ""
while i > 0 and j > 0:
if X[i - 1] == Y[j - 1]:
charstr += X[i - 1]
i -= 1
j -= 1
elif dp[i - 1][j] > dp[i][j - 1]:
charstr += X[i - 1]
i -= 1
else:
charstr += Y[j - 1]
j -= 1
while i > 0:
charstr += X[i - 1]
i -= 1
while j > 0:
charstr += Y[j - 1]
j -= 1
return m + n - dp[m][n] | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR STRING WHILE VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR VAR VAR VAR |
Given two strings X and Y of lengths m and n respectively, find the length of the smallest string which has both, X and Y as its sub-sequences.
Note: X and Y can have both uppercase and lowercase letters.
Example 1
Input:
X = abcd, Y = xycd
Output: 6
Explanation: Shortest Common Supersequence
would be abxycd which is of length 6 and
has both the strings as its subsequences.
Example 2
Input:
X = efgh, Y = jghi
Output: 6
Explanation: Shortest Common Supersequence
would be ejfghi which is of length 6 and
has both the strings as its subsequences.
Your Task:
Complete shortestCommonSupersequence() function that takes X, Y, m, and n as arguments and returns the length of the required string.
Expected Time Complexity: O(Length(X) * Length(Y)).
Expected Auxiliary Space: O(Length(X) * Length(Y)).
Constraints:
1<= |X|, |Y| <= 100 | class Solution:
def shortestCommonSupersequence(self, s1, s2, n, m):
ans = ""
dp = [[(0) for j in range(m + 1)] for i in range(n + 1)]
for ind1 in range(1, n + 1):
for ind2 in range(1, m + 1):
if s1[ind1 - 1] == s2[ind2 - 1]:
dp[ind1][ind2] = 1 + dp[ind1 - 1][ind2 - 1]
else:
dp[ind1][ind2] = max(dp[ind1 - 1][ind2], dp[ind1][ind2 - 1])
return n + m - dp[n][m] | CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR VAR VAR VAR |
Given two strings X and Y of lengths m and n respectively, find the length of the smallest string which has both, X and Y as its sub-sequences.
Note: X and Y can have both uppercase and lowercase letters.
Example 1
Input:
X = abcd, Y = xycd
Output: 6
Explanation: Shortest Common Supersequence
would be abxycd which is of length 6 and
has both the strings as its subsequences.
Example 2
Input:
X = efgh, Y = jghi
Output: 6
Explanation: Shortest Common Supersequence
would be ejfghi which is of length 6 and
has both the strings as its subsequences.
Your Task:
Complete shortestCommonSupersequence() function that takes X, Y, m, and n as arguments and returns the length of the required string.
Expected Time Complexity: O(Length(X) * Length(Y)).
Expected Auxiliary Space: O(Length(X) * Length(Y)).
Constraints:
1<= |X|, |Y| <= 100 | class Solution:
def shortestCommonSupersequence(self, s1, s2, x, y):
def find(x, y, s1, s2, dp):
if x >= len(s1):
return 0
if y >= len(s2):
return 0
if dp[x][y] != -1:
return dp[x][y]
ans = 0
if s1[x] == s2[y]:
ans += 1 + find(x + 1, y + 1, s1, s2, dp)
else:
ans += max(find(x + 1, y, s1, s2, dp), find(x, y + 1, s1, s2, dp))
dp[x][y] = ans
return dp[x][y]
dp = [[(-1) for _ in range(y)] for i in range(x)]
return x + y - find(0, 0, s1, s2, dp) | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR NUMBER NUMBER VAR VAR VAR |
Given two strings X and Y of lengths m and n respectively, find the length of the smallest string which has both, X and Y as its sub-sequences.
Note: X and Y can have both uppercase and lowercase letters.
Example 1
Input:
X = abcd, Y = xycd
Output: 6
Explanation: Shortest Common Supersequence
would be abxycd which is of length 6 and
has both the strings as its subsequences.
Example 2
Input:
X = efgh, Y = jghi
Output: 6
Explanation: Shortest Common Supersequence
would be ejfghi which is of length 6 and
has both the strings as its subsequences.
Your Task:
Complete shortestCommonSupersequence() function that takes X, Y, m, and n as arguments and returns the length of the required string.
Expected Time Complexity: O(Length(X) * Length(Y)).
Expected Auxiliary Space: O(Length(X) * Length(Y)).
Constraints:
1<= |X|, |Y| <= 100 | class Solution:
def shortestCommonSupersequence(self, X, Y, m, n):
dp = [[(-1) for i in range(n + 1)] for j in range(m + 1)]
def solve1(a, b, s1, s2, dp):
if a == len(s1) or b == len(s2):
return 0
if dp[a][b] != -1:
return dp[a][b]
if s1[a] == s2[b]:
dp[a][b] = 1 + solve1(a + 1, b + 1, s1, s2, dp)
return dp[a][b]
else:
dp[a][b] = max(
solve1(a + 1, b, s1, s2, dp), solve1(a, b + 1, s1, s2, dp)
)
return dp[a][b]
return m + n - solve1(0, 0, X, Y, dp)
def solve(X, Y, m, n, dp):
if m == 0 or n == 0:
return m + n
if dp[m][n] != -1:
return dp[m][n]
if X[m - 1] == Y[n - 1]:
dp[m][n] = 1 + solve(X, Y, m - 1, n - 1, dp)
else:
dp[m][n] = 1 + min(solve(X, Y, m - 1, n, dp), solve(X, Y, m, n - 1, dp))
return dp[m][n] | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR VAR VAR RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR NUMBER NUMBER VAR VAR VAR FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN BIN_OP VAR VAR IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR |
Given two strings X and Y of lengths m and n respectively, find the length of the smallest string which has both, X and Y as its sub-sequences.
Note: X and Y can have both uppercase and lowercase letters.
Example 1
Input:
X = abcd, Y = xycd
Output: 6
Explanation: Shortest Common Supersequence
would be abxycd which is of length 6 and
has both the strings as its subsequences.
Example 2
Input:
X = efgh, Y = jghi
Output: 6
Explanation: Shortest Common Supersequence
would be ejfghi which is of length 6 and
has both the strings as its subsequences.
Your Task:
Complete shortestCommonSupersequence() function that takes X, Y, m, and n as arguments and returns the length of the required string.
Expected Time Complexity: O(Length(X) * Length(Y)).
Expected Auxiliary Space: O(Length(X) * Length(Y)).
Constraints:
1<= |X|, |Y| <= 100 | class Solution:
def shortestCommonSupersequence(self, X, Y, m, n):
n = len(X)
m = len(Y)
dp = [[(0) for i in range(m + 1)] for i in range(n + 1)]
for i in range(n + 1):
dp[i][0] = 0
for j in range(m + 1):
dp[0][j] = 0
for ind1 in range(1, n + 1):
for ind2 in range(1, m + 1):
if X[ind1 - 1] == Y[ind2 - 1]:
dp[ind1][ind2] = 1 + dp[ind1 - 1][ind2 - 1]
else:
dp[ind1][ind2] = max(dp[ind1 - 1][ind2], dp[ind1][ind2 - 1])
return n + m - dp[n][m] | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR VAR VAR VAR |
Given two strings X and Y of lengths m and n respectively, find the length of the smallest string which has both, X and Y as its sub-sequences.
Note: X and Y can have both uppercase and lowercase letters.
Example 1
Input:
X = abcd, Y = xycd
Output: 6
Explanation: Shortest Common Supersequence
would be abxycd which is of length 6 and
has both the strings as its subsequences.
Example 2
Input:
X = efgh, Y = jghi
Output: 6
Explanation: Shortest Common Supersequence
would be ejfghi which is of length 6 and
has both the strings as its subsequences.
Your Task:
Complete shortestCommonSupersequence() function that takes X, Y, m, and n as arguments and returns the length of the required string.
Expected Time Complexity: O(Length(X) * Length(Y)).
Expected Auxiliary Space: O(Length(X) * Length(Y)).
Constraints:
1<= |X|, |Y| <= 100 | class Solution:
def find_lcs(self, dp, txt1, txt2, m, n):
if m == 0 or n == 0:
return 0
if dp[m][n] != -1:
return dp[m][n]
if txt1[m - 1] == txt2[n - 1]:
dp[m][n] = 1 + self.find_lcs(dp, txt1, txt2, m - 1, n - 1)
return dp[m][n]
else:
dp[m][n] = max(
self.find_lcs(dp, txt1, txt2, m - 1, n),
self.find_lcs(dp, txt1, txt2, m, n - 1),
)
return dp[m][n]
def shortestCommonSupersequence(self, X, Y, m, n):
dp = [[(-1) for i in range(n + 1)] for j in range(m + 1)]
lcs = self.find_lcs(dp, X, Y, m, n)
return m + n - lcs | CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN BIN_OP BIN_OP VAR VAR VAR |
Given two strings X and Y of lengths m and n respectively, find the length of the smallest string which has both, X and Y as its sub-sequences.
Note: X and Y can have both uppercase and lowercase letters.
Example 1
Input:
X = abcd, Y = xycd
Output: 6
Explanation: Shortest Common Supersequence
would be abxycd which is of length 6 and
has both the strings as its subsequences.
Example 2
Input:
X = efgh, Y = jghi
Output: 6
Explanation: Shortest Common Supersequence
would be ejfghi which is of length 6 and
has both the strings as its subsequences.
Your Task:
Complete shortestCommonSupersequence() function that takes X, Y, m, and n as arguments and returns the length of the required string.
Expected Time Complexity: O(Length(X) * Length(Y)).
Expected Auxiliary Space: O(Length(X) * Length(Y)).
Constraints:
1<= |X|, |Y| <= 100 | class Solution:
def common_sequence(self, X, Y, m, n):
t = [[(0) for _ in range(m + 1)] for _ in range(n + 1)]
for i in range(n + 1):
for j in range(m + 1):
if i == 0 or j == 0:
t[i][j] = 0
elif X[j - 1] == Y[i - 1]:
t[i][j] = t[i - 1][j - 1] + 1
else:
t[i][j] = max(t[i][j - 1], t[i - 1][j])
return t[n][m]
def shortestCommonSupersequence(self, X, Y, m, n):
k = self.common_sequence(X, Y, m, n)
return m + n - k | 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 BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN BIN_OP BIN_OP VAR VAR VAR |
Given two strings X and Y of lengths m and n respectively, find the length of the smallest string which has both, X and Y as its sub-sequences.
Note: X and Y can have both uppercase and lowercase letters.
Example 1
Input:
X = abcd, Y = xycd
Output: 6
Explanation: Shortest Common Supersequence
would be abxycd which is of length 6 and
has both the strings as its subsequences.
Example 2
Input:
X = efgh, Y = jghi
Output: 6
Explanation: Shortest Common Supersequence
would be ejfghi which is of length 6 and
has both the strings as its subsequences.
Your Task:
Complete shortestCommonSupersequence() function that takes X, Y, m, and n as arguments and returns the length of the required string.
Expected Time Complexity: O(Length(X) * Length(Y)).
Expected Auxiliary Space: O(Length(X) * Length(Y)).
Constraints:
1<= |X|, |Y| <= 100 | class Solution:
def shortestCommonSupersequence(self, X, Y, m, n):
str1 = X + Y
str = len(str1)
n = len(X)
m = len(Y)
str2 = self.lcs(n, m, X, Y)
return str - str2
def lcs(self, x, y, s1, s2):
t = [[(-1) for i in range(y + 1)] for j in range(x + 1)]
for i in range(y + 1):
t[0][i] = 0
for j in range(x + 1):
t[j][0] = 0
for i in range(1, x + 1):
for j in range(1, y + 1):
if s1[i - 1] == s2[j - 1]:
t[i][j] = 1 + t[i - 1][j - 1]
else:
t[i][j] = max(t[i - 1][j], t[i][j - 1])
return t[x][y] | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN BIN_OP VAR VAR 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 BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR |
Given two strings X and Y of lengths m and n respectively, find the length of the smallest string which has both, X and Y as its sub-sequences.
Note: X and Y can have both uppercase and lowercase letters.
Example 1
Input:
X = abcd, Y = xycd
Output: 6
Explanation: Shortest Common Supersequence
would be abxycd which is of length 6 and
has both the strings as its subsequences.
Example 2
Input:
X = efgh, Y = jghi
Output: 6
Explanation: Shortest Common Supersequence
would be ejfghi which is of length 6 and
has both the strings as its subsequences.
Your Task:
Complete shortestCommonSupersequence() function that takes X, Y, m, and n as arguments and returns the length of the required string.
Expected Time Complexity: O(Length(X) * Length(Y)).
Expected Auxiliary Space: O(Length(X) * Length(Y)).
Constraints:
1<= |X|, |Y| <= 100 | class Solution:
def shortestCommonSupersequence(self, X, Y, m, n):
return m + n - self.lcs(X, Y, m, n)
def lcs(self, X, Y, m, n):
memory = [[(0) for i in range(n + 1)] for j in range(m + 1)]
for i in range(m + 1):
for j in range(n + 1):
if i == 0 or j == 0:
memory[i][j] = 0
elif X[i - 1] == Y[j - 1]:
memory[i][j] = 1 + memory[i - 1][j - 1]
else:
memory[i][j] = max(memory[i - 1][j], memory[i][j - 1])
return memory[m][n] | CLASS_DEF FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR VAR 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 BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR |
Given two strings X and Y of lengths m and n respectively, find the length of the smallest string which has both, X and Y as its sub-sequences.
Note: X and Y can have both uppercase and lowercase letters.
Example 1
Input:
X = abcd, Y = xycd
Output: 6
Explanation: Shortest Common Supersequence
would be abxycd which is of length 6 and
has both the strings as its subsequences.
Example 2
Input:
X = efgh, Y = jghi
Output: 6
Explanation: Shortest Common Supersequence
would be ejfghi which is of length 6 and
has both the strings as its subsequences.
Your Task:
Complete shortestCommonSupersequence() function that takes X, Y, m, and n as arguments and returns the length of the required string.
Expected Time Complexity: O(Length(X) * Length(Y)).
Expected Auxiliary Space: O(Length(X) * Length(Y)).
Constraints:
1<= |X|, |Y| <= 100 | class Solution:
def shortestCommonSupersequence(self, X, Y, m, n):
x = X
y = Y
dp = [([0] * (n + 1)) for i in range(m + 1)]
for i in range(m):
dp[i][n] = m - i
for j in range(n):
dp[m][j] = n - j
for i in range(m - 1, -1, -1):
for j in range(n - 1, -1, -1):
dp[i][j] = (
dp[i + 1][j + 1] + 1
if x[i] == y[j]
else min(dp[i + 1][j], dp[i][j + 1]) + 1
)
return dp[0][0] | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER RETURN VAR NUMBER NUMBER |
Given two strings X and Y of lengths m and n respectively, find the length of the smallest string which has both, X and Y as its sub-sequences.
Note: X and Y can have both uppercase and lowercase letters.
Example 1
Input:
X = abcd, Y = xycd
Output: 6
Explanation: Shortest Common Supersequence
would be abxycd which is of length 6 and
has both the strings as its subsequences.
Example 2
Input:
X = efgh, Y = jghi
Output: 6
Explanation: Shortest Common Supersequence
would be ejfghi which is of length 6 and
has both the strings as its subsequences.
Your Task:
Complete shortestCommonSupersequence() function that takes X, Y, m, and n as arguments and returns the length of the required string.
Expected Time Complexity: O(Length(X) * Length(Y)).
Expected Auxiliary Space: O(Length(X) * Length(Y)).
Constraints:
1<= |X|, |Y| <= 100 | class Solution:
def shortestCommonSupersequence(self, X, Y, m, n):
dp = [[(0) for k in range(len(Y) + 1)] for j in range(len(X) + 1)]
str1 = ""
for i in range(1, len(X) + 1):
for j in range(1, len(Y) + 1):
if X[i - 1] == Y[j - 1]:
dp[i][j] = 1 + dp[i - 1][j - 1]
elif i == 0 and j == 0:
dp[i][j] = 0
else:
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
return len(Y) + (len(X) - dp[-1][-1]) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER |
Given two strings X and Y of lengths m and n respectively, find the length of the smallest string which has both, X and Y as its sub-sequences.
Note: X and Y can have both uppercase and lowercase letters.
Example 1
Input:
X = abcd, Y = xycd
Output: 6
Explanation: Shortest Common Supersequence
would be abxycd which is of length 6 and
has both the strings as its subsequences.
Example 2
Input:
X = efgh, Y = jghi
Output: 6
Explanation: Shortest Common Supersequence
would be ejfghi which is of length 6 and
has both the strings as its subsequences.
Your Task:
Complete shortestCommonSupersequence() function that takes X, Y, m, and n as arguments and returns the length of the required string.
Expected Time Complexity: O(Length(X) * Length(Y)).
Expected Auxiliary Space: O(Length(X) * Length(Y)).
Constraints:
1<= |X|, |Y| <= 100 | class Solution:
def shortestCommonSupersequence(self, X, Y, m, n):
dp = [([0] * (n + 1)) for _ in range(m + 1)]
for i in range(1, m + 1):
for j in range(1, n + 1):
c1, c2 = X[i - 1], Y[j - 1]
if c1 == c2:
dp[i][j] = dp[i - 1][j - 1] + 1
else:
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
l = dp[m][n]
return m + n - l | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN BIN_OP BIN_OP VAR VAR VAR |
Given two strings X and Y of lengths m and n respectively, find the length of the smallest string which has both, X and Y as its sub-sequences.
Note: X and Y can have both uppercase and lowercase letters.
Example 1
Input:
X = abcd, Y = xycd
Output: 6
Explanation: Shortest Common Supersequence
would be abxycd which is of length 6 and
has both the strings as its subsequences.
Example 2
Input:
X = efgh, Y = jghi
Output: 6
Explanation: Shortest Common Supersequence
would be ejfghi which is of length 6 and
has both the strings as its subsequences.
Your Task:
Complete shortestCommonSupersequence() function that takes X, Y, m, and n as arguments and returns the length of the required string.
Expected Time Complexity: O(Length(X) * Length(Y)).
Expected Auxiliary Space: O(Length(X) * Length(Y)).
Constraints:
1<= |X|, |Y| <= 100 | class Solution:
def lcs(self, str1, str2, dp, m, n):
for i in range(m + 1):
for j in range(n + 1):
if i == 0 or j == 0:
dp[i][j] = 0
for i in range(1, m + 1):
for j in range(1, n + 1):
if str1[i - 1] == str2[j - 1]:
dp[i][j] = 1 + dp[i - 1][j - 1]
else:
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
return dp[m][n]
def shortestCommonSupersequence(self, X, Y, m, n):
str1 = X
str2 = Y
m = len(str1)
n = len(str2)
dp = []
for i in range(m + 1):
dp.append([])
for j in range(n + 1):
dp[i].append(-1)
common = self.lcs(str1, str2, dp, m, n)
return len(str1) + len(str2) - common | CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR 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 EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR |
Given two strings X and Y of lengths m and n respectively, find the length of the smallest string which has both, X and Y as its sub-sequences.
Note: X and Y can have both uppercase and lowercase letters.
Example 1
Input:
X = abcd, Y = xycd
Output: 6
Explanation: Shortest Common Supersequence
would be abxycd which is of length 6 and
has both the strings as its subsequences.
Example 2
Input:
X = efgh, Y = jghi
Output: 6
Explanation: Shortest Common Supersequence
would be ejfghi which is of length 6 and
has both the strings as its subsequences.
Your Task:
Complete shortestCommonSupersequence() function that takes X, Y, m, and n as arguments and returns the length of the required string.
Expected Time Complexity: O(Length(X) * Length(Y)).
Expected Auxiliary Space: O(Length(X) * Length(Y)).
Constraints:
1<= |X|, |Y| <= 100 | class Solution:
def shortestCommonSupersequence(self, X, Y, m, n):
arr = [[(0) for j in range(n + 1)] for i in range(m + 1)]
for j in range(n + 1):
arr[0][j] = j
for i in range(m + 1):
arr[i][0] = i
for i in range(1, m + 1):
for j in range(1, n + 1):
if X[i - 1] == Y[j - 1]:
arr[i][j] = arr[i - 1][j - 1] + 1
else:
arr[i][j] = min(arr[i - 1][j], arr[i][j - 1]) + 1
return arr[m][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 BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER RETURN VAR VAR VAR |
Given two strings X and Y of lengths m and n respectively, find the length of the smallest string which has both, X and Y as its sub-sequences.
Note: X and Y can have both uppercase and lowercase letters.
Example 1
Input:
X = abcd, Y = xycd
Output: 6
Explanation: Shortest Common Supersequence
would be abxycd which is of length 6 and
has both the strings as its subsequences.
Example 2
Input:
X = efgh, Y = jghi
Output: 6
Explanation: Shortest Common Supersequence
would be ejfghi which is of length 6 and
has both the strings as its subsequences.
Your Task:
Complete shortestCommonSupersequence() function that takes X, Y, m, and n as arguments and returns the length of the required string.
Expected Time Complexity: O(Length(X) * Length(Y)).
Expected Auxiliary Space: O(Length(X) * Length(Y)).
Constraints:
1<= |X|, |Y| <= 100 | class Solution:
def shortestCommonSupersequence(self, s1, s2, x, y):
dp = [[(0) for j in range(y + 1)] for i in range(x + 1)]
for i in range(1, x + 1):
for j in range(1, y + 1):
if s1[i - 1] == s2[j - 1]:
dp[i][j] = 1 + dp[i - 1][j - 1]
else:
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
p = len(s1) - dp[x][y]
q = len(s2) - dp[x][y]
return dp[x][y] + p + q | 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 NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR RETURN BIN_OP BIN_OP VAR VAR VAR VAR VAR |
Given two strings X and Y of lengths m and n respectively, find the length of the smallest string which has both, X and Y as its sub-sequences.
Note: X and Y can have both uppercase and lowercase letters.
Example 1
Input:
X = abcd, Y = xycd
Output: 6
Explanation: Shortest Common Supersequence
would be abxycd which is of length 6 and
has both the strings as its subsequences.
Example 2
Input:
X = efgh, Y = jghi
Output: 6
Explanation: Shortest Common Supersequence
would be ejfghi which is of length 6 and
has both the strings as its subsequences.
Your Task:
Complete shortestCommonSupersequence() function that takes X, Y, m, and n as arguments and returns the length of the required string.
Expected Time Complexity: O(Length(X) * Length(Y)).
Expected Auxiliary Space: O(Length(X) * Length(Y)).
Constraints:
1<= |X|, |Y| <= 100 | class Solution:
def solve(self, X, Y, m, n, dp):
ct = 0
for a in range(1, m + 1):
for b in range(1, n + 1):
if X[a - 1] == Y[b - 1]:
dp[a][b] = 1 + dp[a - 1][b - 1]
ct = max(ct, dp[a][b])
else:
dp[a][b] = max(dp[a - 1][b], dp[a][b - 1])
ct = max(ct, dp[a][b])
return ct
def shortestCommonSupersequence(self, X, Y, m, n):
dp = [[(0) for i in range(n + 1)] for j in range(m + 1)]
a = self.solve(X, Y, m, n, dp)
return m + n - a | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN BIN_OP BIN_OP VAR VAR VAR |
Given two strings X and Y of lengths m and n respectively, find the length of the smallest string which has both, X and Y as its sub-sequences.
Note: X and Y can have both uppercase and lowercase letters.
Example 1
Input:
X = abcd, Y = xycd
Output: 6
Explanation: Shortest Common Supersequence
would be abxycd which is of length 6 and
has both the strings as its subsequences.
Example 2
Input:
X = efgh, Y = jghi
Output: 6
Explanation: Shortest Common Supersequence
would be ejfghi which is of length 6 and
has both the strings as its subsequences.
Your Task:
Complete shortestCommonSupersequence() function that takes X, Y, m, and n as arguments and returns the length of the required string.
Expected Time Complexity: O(Length(X) * Length(Y)).
Expected Auxiliary Space: O(Length(X) * Length(Y)).
Constraints:
1<= |X|, |Y| <= 100 | class Solution:
def shortestCommonSupersequence(self, X, Y, m, n):
res = [[(0) for y in range(n + 1)] for x in range(m + 1)]
for i in range(m):
for j in range(n):
if X[i] == Y[j]:
res[i + 1][j + 1] = 1 + res[i][j]
else:
res[i + 1][j + 1] = max(res[i + 1][j], res[i][j + 1])
return m + n - res[-1][-1] | 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 FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER |
Given two strings X and Y of lengths m and n respectively, find the length of the smallest string which has both, X and Y as its sub-sequences.
Note: X and Y can have both uppercase and lowercase letters.
Example 1
Input:
X = abcd, Y = xycd
Output: 6
Explanation: Shortest Common Supersequence
would be abxycd which is of length 6 and
has both the strings as its subsequences.
Example 2
Input:
X = efgh, Y = jghi
Output: 6
Explanation: Shortest Common Supersequence
would be ejfghi which is of length 6 and
has both the strings as its subsequences.
Your Task:
Complete shortestCommonSupersequence() function that takes X, Y, m, and n as arguments and returns the length of the required string.
Expected Time Complexity: O(Length(X) * Length(Y)).
Expected Auxiliary Space: O(Length(X) * Length(Y)).
Constraints:
1<= |X|, |Y| <= 100 | class Solution:
def ls(self, x, y, s1, s2, dp):
if x == 0 or y == 0:
return 0
if dp[x][y] != -1:
return dp[x][y]
if s1[x - 1] == s2[y - 1]:
dp[x][y] = 1 + self.ls(x - 1, y - 1, s1, s2, dp)
return dp[x][y]
else:
dp[x][y] = max(self.ls(x - 1, y, s1, s2, dp), self.ls(x, y - 1, s1, s2, dp))
return dp[x][y]
def shortestCommonSupersequence(self, X, Y, m, n):
dp = [[(-1) for _ in range(n + 1)] for _ in range(m + 1)]
return m + n - self.ls(m, n, X, Y, dp) | CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR |
Given two strings X and Y of lengths m and n respectively, find the length of the smallest string which has both, X and Y as its sub-sequences.
Note: X and Y can have both uppercase and lowercase letters.
Example 1
Input:
X = abcd, Y = xycd
Output: 6
Explanation: Shortest Common Supersequence
would be abxycd which is of length 6 and
has both the strings as its subsequences.
Example 2
Input:
X = efgh, Y = jghi
Output: 6
Explanation: Shortest Common Supersequence
would be ejfghi which is of length 6 and
has both the strings as its subsequences.
Your Task:
Complete shortestCommonSupersequence() function that takes X, Y, m, and n as arguments and returns the length of the required string.
Expected Time Complexity: O(Length(X) * Length(Y)).
Expected Auxiliary Space: O(Length(X) * Length(Y)).
Constraints:
1<= |X|, |Y| <= 100 | class Solution:
def shortestCommonSupersequence(self, s1, s2, x, y):
dp = [([0] * (y + 1)) for i in range(x + 1)]
for i in range(1, x + 1):
for j in range(1, y + 1):
if s1[i - 1] == s2[j - 1]:
dp[i][j] = 1 + dp[i - 1][j - 1]
else:
dp[i][j] = max(dp[i][j - 1], dp[i - 1][j])
lcs = dp[x][y]
result = lcs + (x - lcs) + (y - lcs)
return result | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR RETURN VAR |
Given two strings X and Y of lengths m and n respectively, find the length of the smallest string which has both, X and Y as its sub-sequences.
Note: X and Y can have both uppercase and lowercase letters.
Example 1
Input:
X = abcd, Y = xycd
Output: 6
Explanation: Shortest Common Supersequence
would be abxycd which is of length 6 and
has both the strings as its subsequences.
Example 2
Input:
X = efgh, Y = jghi
Output: 6
Explanation: Shortest Common Supersequence
would be ejfghi which is of length 6 and
has both the strings as its subsequences.
Your Task:
Complete shortestCommonSupersequence() function that takes X, Y, m, and n as arguments and returns the length of the required string.
Expected Time Complexity: O(Length(X) * Length(Y)).
Expected Auxiliary Space: O(Length(X) * Length(Y)).
Constraints:
1<= |X|, |Y| <= 100 | class Solution:
def shortestCommonSupersequence(self, str1, str2, m, n):
m = len(str1)
n = len(str2)
dp = {}
def f(i, j):
if (i, j) in dp:
return dp[i, j]
if i < 0 and j < 0:
return 0
if i >= 0 and j < 0:
return i + 1
if j >= 0 and i < 0:
return j + 1
if str1[i] == str2[j]:
dp[i, j] = 1 + f(i - 1, j - 1)
return dp[i, j]
else:
dp[i, j] = 1 + min(f(i - 1, j), f(i, j - 1))
return dp[i, j]
return f(m - 1, n - 1) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR VAR RETURN VAR VAR VAR IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER RETURN BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER |
Given two strings X and Y of lengths m and n respectively, find the length of the smallest string which has both, X and Y as its sub-sequences.
Note: X and Y can have both uppercase and lowercase letters.
Example 1
Input:
X = abcd, Y = xycd
Output: 6
Explanation: Shortest Common Supersequence
would be abxycd which is of length 6 and
has both the strings as its subsequences.
Example 2
Input:
X = efgh, Y = jghi
Output: 6
Explanation: Shortest Common Supersequence
would be ejfghi which is of length 6 and
has both the strings as its subsequences.
Your Task:
Complete shortestCommonSupersequence() function that takes X, Y, m, and n as arguments and returns the length of the required string.
Expected Time Complexity: O(Length(X) * Length(Y)).
Expected Auxiliary Space: O(Length(X) * Length(Y)).
Constraints:
1<= |X|, |Y| <= 100 | class Solution:
def shortestCommonSupersequence(self, X, Y, m, n):
dp = [[(-1) for i in range(n + 1)] for j in range(m + 1)]
def solve(X, Y, m, n, dp):
if m == 0 or n == 0:
return m + n
if dp[m][n] != -1:
return dp[m][n]
if X[m - 1] == Y[n - 1]:
dp[m][n] = 1 + solve(X, Y, m - 1, n - 1, dp)
else:
dp[m][n] = 1 + min(solve(X, Y, m - 1, n, dp), solve(X, Y, m, n - 1, dp))
return dp[m][n]
return solve(X, Y, m, n, dp) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN BIN_OP VAR VAR IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR VAR |
Given two strings X and Y of lengths m and n respectively, find the length of the smallest string which has both, X and Y as its sub-sequences.
Note: X and Y can have both uppercase and lowercase letters.
Example 1
Input:
X = abcd, Y = xycd
Output: 6
Explanation: Shortest Common Supersequence
would be abxycd which is of length 6 and
has both the strings as its subsequences.
Example 2
Input:
X = efgh, Y = jghi
Output: 6
Explanation: Shortest Common Supersequence
would be ejfghi which is of length 6 and
has both the strings as its subsequences.
Your Task:
Complete shortestCommonSupersequence() function that takes X, Y, m, and n as arguments and returns the length of the required string.
Expected Time Complexity: O(Length(X) * Length(Y)).
Expected Auxiliary Space: O(Length(X) * Length(Y)).
Constraints:
1<= |X|, |Y| <= 100 | class Solution:
def shortestCommonSupersequence(self, X, Y, m, n):
dp = [[(0) for i in range(n + 1)] for j in range(m + 1)]
res = 0
ans = self.f(X, Y, m, n, dp)
if n > m:
res = m - ans
return n + res
else:
res = n - ans
return m + res
def f(self, X, Y, m, n, dp):
for i in range(1, m + 1):
for j in range(1, n + 1):
if X[i - 1] == Y[j - 1]:
dp[i][j] = 1 + dp[i - 1][j - 1]
else:
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
return dp[m][n] | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN BIN_OP VAR VAR FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR |
Given two strings X and Y of lengths m and n respectively, find the length of the smallest string which has both, X and Y as its sub-sequences.
Note: X and Y can have both uppercase and lowercase letters.
Example 1
Input:
X = abcd, Y = xycd
Output: 6
Explanation: Shortest Common Supersequence
would be abxycd which is of length 6 and
has both the strings as its subsequences.
Example 2
Input:
X = efgh, Y = jghi
Output: 6
Explanation: Shortest Common Supersequence
would be ejfghi which is of length 6 and
has both the strings as its subsequences.
Your Task:
Complete shortestCommonSupersequence() function that takes X, Y, m, and n as arguments and returns the length of the required string.
Expected Time Complexity: O(Length(X) * Length(Y)).
Expected Auxiliary Space: O(Length(X) * Length(Y)).
Constraints:
1<= |X|, |Y| <= 100 | class Solution:
def subs(self, X, Y, n, m):
prev = [0] * (m + 1)
curr = [0] * (m + 1)
for i in range(1, n + 1):
for j in range(1, m + 1):
if X[i - 1] == Y[j - 1]:
curr[j] = 1 + prev[j - 1]
else:
curr[j] = max(prev[j], curr[j - 1])
prev = curr[:]
return prev[-1]
def shortestCommonSupersequence(self, X, Y, n, m):
return m + n - self.subs(X, Y, n, m) | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR NUMBER FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR VAR |
Given two strings X and Y of lengths m and n respectively, find the length of the smallest string which has both, X and Y as its sub-sequences.
Note: X and Y can have both uppercase and lowercase letters.
Example 1
Input:
X = abcd, Y = xycd
Output: 6
Explanation: Shortest Common Supersequence
would be abxycd which is of length 6 and
has both the strings as its subsequences.
Example 2
Input:
X = efgh, Y = jghi
Output: 6
Explanation: Shortest Common Supersequence
would be ejfghi which is of length 6 and
has both the strings as its subsequences.
Your Task:
Complete shortestCommonSupersequence() function that takes X, Y, m, and n as arguments and returns the length of the required string.
Expected Time Complexity: O(Length(X) * Length(Y)).
Expected Auxiliary Space: O(Length(X) * Length(Y)).
Constraints:
1<= |X|, |Y| <= 100 | class Solution:
def shortestCommonSupersequence(self, s1, s2, x, y):
x = len(s1)
y = len(s2)
dp = [[(-1) for i in range(y)] for j in range(x)]
def answer(i, j, s1, s2):
if i == -1 or j == -1:
return 0
if dp[i][j] != -1:
return dp[i][j]
if s1[i] == s2[j]:
dp[i][j] = 1 + answer(i - 1, j - 1, s1, s2)
return dp[i][j]
else:
dp[i][j] = max(answer(i, j - 1, s1, s2), answer(i - 1, j, s1, s2))
return dp[i][j]
return x - answer(x - 1, y - 1, s1, s2) + y | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR RETURN VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN VAR VAR VAR RETURN BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR |
Given two strings X and Y of lengths m and n respectively, find the length of the smallest string which has both, X and Y as its sub-sequences.
Note: X and Y can have both uppercase and lowercase letters.
Example 1
Input:
X = abcd, Y = xycd
Output: 6
Explanation: Shortest Common Supersequence
would be abxycd which is of length 6 and
has both the strings as its subsequences.
Example 2
Input:
X = efgh, Y = jghi
Output: 6
Explanation: Shortest Common Supersequence
would be ejfghi which is of length 6 and
has both the strings as its subsequences.
Your Task:
Complete shortestCommonSupersequence() function that takes X, Y, m, and n as arguments and returns the length of the required string.
Expected Time Complexity: O(Length(X) * Length(Y)).
Expected Auxiliary Space: O(Length(X) * Length(Y)).
Constraints:
1<= |X|, |Y| <= 100 | class Solution:
def shortestCommonSupersequence(self, s1, s2, m, n):
dp = [([-1] * (n + 1)) for i in range(m + 1)]
def solve(i, j, s1, s2):
if i < 0 or j < 0:
return 0
ans = 0
if dp[i][j] != -1:
return dp[i][j]
if s1[i] == s2[j]:
ans = 1 + solve(i - 1, j - 1, s1, s2)
else:
ans = max(solve(i - 1, j, s1, s2), solve(i, j - 1, s1, s2))
dp[i][j] = ans
return ans
lcs = solve(m - 1, n - 1, s1, s2)
return m - lcs + n | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR RETURN BIN_OP BIN_OP VAR VAR VAR |
Given two strings X and Y of lengths m and n respectively, find the length of the smallest string which has both, X and Y as its sub-sequences.
Note: X and Y can have both uppercase and lowercase letters.
Example 1
Input:
X = abcd, Y = xycd
Output: 6
Explanation: Shortest Common Supersequence
would be abxycd which is of length 6 and
has both the strings as its subsequences.
Example 2
Input:
X = efgh, Y = jghi
Output: 6
Explanation: Shortest Common Supersequence
would be ejfghi which is of length 6 and
has both the strings as its subsequences.
Your Task:
Complete shortestCommonSupersequence() function that takes X, Y, m, and n as arguments and returns the length of the required string.
Expected Time Complexity: O(Length(X) * Length(Y)).
Expected Auxiliary Space: O(Length(X) * Length(Y)).
Constraints:
1<= |X|, |Y| <= 100 | class Solution:
dp = []
def gen(self, x, y, m, n):
if m < 0 or n < 0:
return abs(m - n)
p = 999999
if self.dp[m][n] != -1:
return self.dp[m][n]
if x[m] == y[n]:
p = self.gen(x, y, m - 1, n - 1) + 1
q = self.gen(x, y, m - 1, n) + 1
r = self.gen(x, y, m, n - 1) + 1
self.dp[m][n] = min(p, q, r)
return min(p, q, r)
def shortestCommonSupersequence(self, X, Y, m, n):
self.dp = []
for i in range(m):
b = []
for j in range(n):
b.append(-1)
self.dp.append(b)
return self.gen(X, Y, m - 1, n - 1) | CLASS_DEF ASSIGN VAR LIST FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER |
Given two strings X and Y of lengths m and n respectively, find the length of the smallest string which has both, X and Y as its sub-sequences.
Note: X and Y can have both uppercase and lowercase letters.
Example 1
Input:
X = abcd, Y = xycd
Output: 6
Explanation: Shortest Common Supersequence
would be abxycd which is of length 6 and
has both the strings as its subsequences.
Example 2
Input:
X = efgh, Y = jghi
Output: 6
Explanation: Shortest Common Supersequence
would be ejfghi which is of length 6 and
has both the strings as its subsequences.
Your Task:
Complete shortestCommonSupersequence() function that takes X, Y, m, and n as arguments and returns the length of the required string.
Expected Time Complexity: O(Length(X) * Length(Y)).
Expected Auxiliary Space: O(Length(X) * Length(Y)).
Constraints:
1<= |X|, |Y| <= 100 | class Solution:
def shortestCommonSupersequence(self, s1, s2, x, y):
dp = [([0] * (y + 1)) for _ in range(x + 1)]
for i in range(x + 1):
for j in range(y + 1):
m, n = i, j
if m == 0 or n == 0:
pass
elif s1[m - 1] == s2[n - 1]:
dp[i][j] = 1 + dp[i - 1][j - 1]
else:
c1 = dp[i - 1][j]
c2 = dp[i][j - 1]
dp[i][j] = max(c1, c2)
c = dp[x][y]
return x + y - c | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN BIN_OP BIN_OP VAR VAR VAR |
Given two strings X and Y of lengths m and n respectively, find the length of the smallest string which has both, X and Y as its sub-sequences.
Note: X and Y can have both uppercase and lowercase letters.
Example 1
Input:
X = abcd, Y = xycd
Output: 6
Explanation: Shortest Common Supersequence
would be abxycd which is of length 6 and
has both the strings as its subsequences.
Example 2
Input:
X = efgh, Y = jghi
Output: 6
Explanation: Shortest Common Supersequence
would be ejfghi which is of length 6 and
has both the strings as its subsequences.
Your Task:
Complete shortestCommonSupersequence() function that takes X, Y, m, and n as arguments and returns the length of the required string.
Expected Time Complexity: O(Length(X) * Length(Y)).
Expected Auxiliary Space: O(Length(X) * Length(Y)).
Constraints:
1<= |X|, |Y| <= 100 | class Solution:
def shortestCommonSupersequence(self, X, Y, m, n):
dp = [([-1] * (n + 1)) for i in range(m + 1)]
def lcs(x, y, n, m):
if n == 0 or m == 0:
return 0
if dp[n][m] != -1:
return dp[n][m]
if x[n - 1] == y[m - 1]:
dp[n][m] = 1 + lcs(x, y, n - 1, m - 1)
else:
dp[n][m] = max(lcs(x, y, n - 1, m), lcs(x, y, n, m - 1))
return dp[n][m]
ans = lcs(X, Y, m, n)
return m + n - ans | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN BIN_OP BIN_OP VAR VAR VAR |
Given two strings X and Y of lengths m and n respectively, find the length of the smallest string which has both, X and Y as its sub-sequences.
Note: X and Y can have both uppercase and lowercase letters.
Example 1
Input:
X = abcd, Y = xycd
Output: 6
Explanation: Shortest Common Supersequence
would be abxycd which is of length 6 and
has both the strings as its subsequences.
Example 2
Input:
X = efgh, Y = jghi
Output: 6
Explanation: Shortest Common Supersequence
would be ejfghi which is of length 6 and
has both the strings as its subsequences.
Your Task:
Complete shortestCommonSupersequence() function that takes X, Y, m, and n as arguments and returns the length of the required string.
Expected Time Complexity: O(Length(X) * Length(Y)).
Expected Auxiliary Space: O(Length(X) * Length(Y)).
Constraints:
1<= |X|, |Y| <= 100 | class Solution:
def lcs(self, s1: str, s2: str) -> int:
dp = [([0] * (len(s2) + 1)) for j in range(len(s1) + 1)]
for i in range(1, len(s1) + 1):
for j in range(1, len(s2) + 1):
if s1[i - 1] == s2[j - 1]:
dp[i][j] = 1 + dp[i - 1][j - 1]
else:
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])
return dp[-1][-1]
def shortestCommonSupersequence(self, X, Y, m, n):
return m + n - self.lcs(X, Y) | CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR NUMBER NUMBER VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR |
Everybody loves magic, especially magicians who compete for glory on the Byteland Magic Tournament. Magician Cyael is one such magician.
Cyael has been having some issues with her last performances and today she’ll have to perform for an audience of some judges, who will change her tournament ranking, possibly increasing it. As she is a great magician she managed to gather a description of the fixed judges’ disposition on the room (which is represented as an N × N square matrix), such that she knows in advance the fixed points each judge will provide. She also knows that the room is divided into several parallel corridors, such that we will denote the j-th cell on corridor i, as [i][j]. Note that some judges can award Cyael, zero points or negative points, as they are never pleased with her performance. There is just one judge at each cell of the matrix, except the cells [1][1] and [N][N].
To complete her evaluation, she must start on the top leftmost corner of the room (cell [1][1]), and finish on the bottom right corner (cell [N][N]), moving either to the cell directly in front of her on the same corridor (that is, moving from cell [r][c] to cell [r][c+1], where c+1 ≤ N) or to the cell in the next corridor directly in front of where she is (that is, moving from cell [r][c] to cell [r+1][c], where r+1 ≤ N). She will keep doing this until she reaches the end point of the room, i.e. last cell [N][N] on the last corridor. Cyael will be judged at all visited cells with a judge.
Cyael wants to maximize her average score at end of her performance. More specifically, if she passes K judges, each being on cell [i_{1}][j_{1}], cell [i_{2}][j_{2}], ..., cell [i_{K}][j_{K}] respectively, then she wants to maximize (S[i_{1}][j_{1}] + S[i_{2}][j_{2}] + ... + S[i_{K}][j_{K}]) / K, where S[i][j] denotes the points that the judge will give her on the cell [i][j].
Help her determine the best path she has to follow in order to maximize her average points.
------ Input ------
The first line contains a single integer T denoting the number of test cases. The description for T test cases follows. For each test case, the first line contains a single integer N. Each of the next N lines contains N space-separated integers.
The j-th integer S[i][j] in i-th line denotes the points awarded by the judge at cell [i][j].
Note that the cells [1][1] and [N][N] have no judges, so S[1][1] and S[N][N] will be 0.
------ Output ------
For each test case, if the maximum possible average points Cyael can obtain is negative, output a single line containing "Bad Judges" (quotes for clarity). Otherwise, output the maximum possible average points. The answer will be considered correct if it has an absolute error no more than 10^{-6}.
------ Constraints ------
1 ≤ T ≤ 20
2 ≤ N ≤ 100
-2500 ≤ S[i][j] ≤ 2500
S[1][1] = S[N][N] = 0
Your code will be judged against several input files.
------ Example ------
----- Sample Input 1 ------
2
2
0 -4
8 0
2
0 -45
-3 0
----- Sample Output 1 ------
8.000000
Bad Judges
----- explanation 1 ------
Test case $1$: An optimal path for Cyael would be $(1,1)\rightarrow (2,1)\rightarrow (2,2)$. This way Cyael faces $1$ judge and gets a total score of $8$. Thus, the average score is $\frac{8}{1} = 8$.
Test case $2$: No matter what path Cyael chooses, the final score would be less than $0$. | t = int(input())
for _ in range(t):
n = int(input())
a = []
for i in range(n):
a.append(list(map(int, input().split())))
ans = [([0] * n) for i in range(n)]
for i in range(1, n):
ans[i][0] = ans[i - 1][0] + a[i][0]
ans[0][i] = ans[0][i - 1] + a[0][i]
for i in range(1, n):
for j in range(1, n):
ans[i][j] = a[i][j] + max(ans[i - 1][j], ans[i][j - 1])
m = ans[n - 1][n - 1]
tot = 2 * (n - 1) - 1
print("{:.6f}".format(m / tot) if m >= 0 else "Bad Judges") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR NUMBER FUNC_CALL STRING BIN_OP VAR VAR STRING |
Everybody loves magic, especially magicians who compete for glory on the Byteland Magic Tournament. Magician Cyael is one such magician.
Cyael has been having some issues with her last performances and today she’ll have to perform for an audience of some judges, who will change her tournament ranking, possibly increasing it. As she is a great magician she managed to gather a description of the fixed judges’ disposition on the room (which is represented as an N × N square matrix), such that she knows in advance the fixed points each judge will provide. She also knows that the room is divided into several parallel corridors, such that we will denote the j-th cell on corridor i, as [i][j]. Note that some judges can award Cyael, zero points or negative points, as they are never pleased with her performance. There is just one judge at each cell of the matrix, except the cells [1][1] and [N][N].
To complete her evaluation, she must start on the top leftmost corner of the room (cell [1][1]), and finish on the bottom right corner (cell [N][N]), moving either to the cell directly in front of her on the same corridor (that is, moving from cell [r][c] to cell [r][c+1], where c+1 ≤ N) or to the cell in the next corridor directly in front of where she is (that is, moving from cell [r][c] to cell [r+1][c], where r+1 ≤ N). She will keep doing this until she reaches the end point of the room, i.e. last cell [N][N] on the last corridor. Cyael will be judged at all visited cells with a judge.
Cyael wants to maximize her average score at end of her performance. More specifically, if she passes K judges, each being on cell [i_{1}][j_{1}], cell [i_{2}][j_{2}], ..., cell [i_{K}][j_{K}] respectively, then she wants to maximize (S[i_{1}][j_{1}] + S[i_{2}][j_{2}] + ... + S[i_{K}][j_{K}]) / K, where S[i][j] denotes the points that the judge will give her on the cell [i][j].
Help her determine the best path she has to follow in order to maximize her average points.
------ Input ------
The first line contains a single integer T denoting the number of test cases. The description for T test cases follows. For each test case, the first line contains a single integer N. Each of the next N lines contains N space-separated integers.
The j-th integer S[i][j] in i-th line denotes the points awarded by the judge at cell [i][j].
Note that the cells [1][1] and [N][N] have no judges, so S[1][1] and S[N][N] will be 0.
------ Output ------
For each test case, if the maximum possible average points Cyael can obtain is negative, output a single line containing "Bad Judges" (quotes for clarity). Otherwise, output the maximum possible average points. The answer will be considered correct if it has an absolute error no more than 10^{-6}.
------ Constraints ------
1 ≤ T ≤ 20
2 ≤ N ≤ 100
-2500 ≤ S[i][j] ≤ 2500
S[1][1] = S[N][N] = 0
Your code will be judged against several input files.
------ Example ------
----- Sample Input 1 ------
2
2
0 -4
8 0
2
0 -45
-3 0
----- Sample Output 1 ------
8.000000
Bad Judges
----- explanation 1 ------
Test case $1$: An optimal path for Cyael would be $(1,1)\rightarrow (2,1)\rightarrow (2,2)$. This way Cyael faces $1$ judge and gets a total score of $8$. Thus, the average score is $\frac{8}{1} = 8$.
Test case $2$: No matter what path Cyael chooses, the final score would be less than $0$. | from itertools import islice
def calculate_rankings(judges):
judges = [[-(10**6)] * len(judges[0])] + judges
judges = [([-(10**6)] + row) for row in judges]
judges[0][1] = 0
for row_i, row in islice(enumerate(judges), 1, None):
for cell_i, cell in islice(enumerate(row), 1, None):
judges[row_i][cell_i] += max(row[cell_i - 1], judges[row_i - 1][cell_i])
return judges[-1][-1] / (2 * len(judges) - 5)
for _ in range(int(input())):
n = int(input())
judges = [[int(j) for j in input().split()] for _ in range(n)]
ranking = calculate_rankings(judges)
print("{:.6f}".format(ranking) if ranking >= 0 else "Bad Judges") | FUNC_DEF ASSIGN VAR BIN_OP LIST BIN_OP LIST BIN_OP NUMBER NUMBER FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR BIN_OP LIST BIN_OP NUMBER NUMBER VAR VAR VAR ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NONE FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NONE VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR RETURN BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FUNC_CALL STRING VAR STRING |
Everybody loves magic, especially magicians who compete for glory on the Byteland Magic Tournament. Magician Cyael is one such magician.
Cyael has been having some issues with her last performances and today she’ll have to perform for an audience of some judges, who will change her tournament ranking, possibly increasing it. As she is a great magician she managed to gather a description of the fixed judges’ disposition on the room (which is represented as an N × N square matrix), such that she knows in advance the fixed points each judge will provide. She also knows that the room is divided into several parallel corridors, such that we will denote the j-th cell on corridor i, as [i][j]. Note that some judges can award Cyael, zero points or negative points, as they are never pleased with her performance. There is just one judge at each cell of the matrix, except the cells [1][1] and [N][N].
To complete her evaluation, she must start on the top leftmost corner of the room (cell [1][1]), and finish on the bottom right corner (cell [N][N]), moving either to the cell directly in front of her on the same corridor (that is, moving from cell [r][c] to cell [r][c+1], where c+1 ≤ N) or to the cell in the next corridor directly in front of where she is (that is, moving from cell [r][c] to cell [r+1][c], where r+1 ≤ N). She will keep doing this until she reaches the end point of the room, i.e. last cell [N][N] on the last corridor. Cyael will be judged at all visited cells with a judge.
Cyael wants to maximize her average score at end of her performance. More specifically, if she passes K judges, each being on cell [i_{1}][j_{1}], cell [i_{2}][j_{2}], ..., cell [i_{K}][j_{K}] respectively, then she wants to maximize (S[i_{1}][j_{1}] + S[i_{2}][j_{2}] + ... + S[i_{K}][j_{K}]) / K, where S[i][j] denotes the points that the judge will give her on the cell [i][j].
Help her determine the best path she has to follow in order to maximize her average points.
------ Input ------
The first line contains a single integer T denoting the number of test cases. The description for T test cases follows. For each test case, the first line contains a single integer N. Each of the next N lines contains N space-separated integers.
The j-th integer S[i][j] in i-th line denotes the points awarded by the judge at cell [i][j].
Note that the cells [1][1] and [N][N] have no judges, so S[1][1] and S[N][N] will be 0.
------ Output ------
For each test case, if the maximum possible average points Cyael can obtain is negative, output a single line containing "Bad Judges" (quotes for clarity). Otherwise, output the maximum possible average points. The answer will be considered correct if it has an absolute error no more than 10^{-6}.
------ Constraints ------
1 ≤ T ≤ 20
2 ≤ N ≤ 100
-2500 ≤ S[i][j] ≤ 2500
S[1][1] = S[N][N] = 0
Your code will be judged against several input files.
------ Example ------
----- Sample Input 1 ------
2
2
0 -4
8 0
2
0 -45
-3 0
----- Sample Output 1 ------
8.000000
Bad Judges
----- explanation 1 ------
Test case $1$: An optimal path for Cyael would be $(1,1)\rightarrow (2,1)\rightarrow (2,2)$. This way Cyael faces $1$ judge and gets a total score of $8$. Thus, the average score is $\frac{8}{1} = 8$.
Test case $2$: No matter what path Cyael chooses, the final score would be less than $0$. | import sys
def trunc(f, n):
return ("%.*f" % (n + 1, f))[:-1]
T = int(input())
while T > 0:
N = int(input())
arr = []
for i in range(N):
row = input().split()
arr.append([])
for inp in row:
arr[i].append(int(inp))
greed = [[0]]
for i in range(1, N):
greed.append([])
greed[0].append(greed[0][i - 1] + arr[0][i])
greed[i].append(greed[i - 1][0] + arr[i][0])
for row in range(1, N):
for col in range(1, N):
possibility1 = greed[row - 1][col] + arr[row][col]
possibility2 = greed[row][col - 1] + arr[row][col]
finalResult = possibility1 if possibility1 > possibility2 else possibility2
greed[row].append(finalResult)
finalAnswer = float(greed[N - 1][N - 1])
if finalAnswer >= 0:
print("%s" % trunc(float(greed[N - 1][N - 1]) / (N + N - 3), 6))
else:
print("Bad Judges")
T -= 1 | IMPORT FUNC_DEF RETURN BIN_OP STRING BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR LIST EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING VAR NUMBER |
Everybody loves magic, especially magicians who compete for glory on the Byteland Magic Tournament. Magician Cyael is one such magician.
Cyael has been having some issues with her last performances and today she’ll have to perform for an audience of some judges, who will change her tournament ranking, possibly increasing it. As she is a great magician she managed to gather a description of the fixed judges’ disposition on the room (which is represented as an N × N square matrix), such that she knows in advance the fixed points each judge will provide. She also knows that the room is divided into several parallel corridors, such that we will denote the j-th cell on corridor i, as [i][j]. Note that some judges can award Cyael, zero points or negative points, as they are never pleased with her performance. There is just one judge at each cell of the matrix, except the cells [1][1] and [N][N].
To complete her evaluation, she must start on the top leftmost corner of the room (cell [1][1]), and finish on the bottom right corner (cell [N][N]), moving either to the cell directly in front of her on the same corridor (that is, moving from cell [r][c] to cell [r][c+1], where c+1 ≤ N) or to the cell in the next corridor directly in front of where she is (that is, moving from cell [r][c] to cell [r+1][c], where r+1 ≤ N). She will keep doing this until she reaches the end point of the room, i.e. last cell [N][N] on the last corridor. Cyael will be judged at all visited cells with a judge.
Cyael wants to maximize her average score at end of her performance. More specifically, if she passes K judges, each being on cell [i_{1}][j_{1}], cell [i_{2}][j_{2}], ..., cell [i_{K}][j_{K}] respectively, then she wants to maximize (S[i_{1}][j_{1}] + S[i_{2}][j_{2}] + ... + S[i_{K}][j_{K}]) / K, where S[i][j] denotes the points that the judge will give her on the cell [i][j].
Help her determine the best path she has to follow in order to maximize her average points.
------ Input ------
The first line contains a single integer T denoting the number of test cases. The description for T test cases follows. For each test case, the first line contains a single integer N. Each of the next N lines contains N space-separated integers.
The j-th integer S[i][j] in i-th line denotes the points awarded by the judge at cell [i][j].
Note that the cells [1][1] and [N][N] have no judges, so S[1][1] and S[N][N] will be 0.
------ Output ------
For each test case, if the maximum possible average points Cyael can obtain is negative, output a single line containing "Bad Judges" (quotes for clarity). Otherwise, output the maximum possible average points. The answer will be considered correct if it has an absolute error no more than 10^{-6}.
------ Constraints ------
1 ≤ T ≤ 20
2 ≤ N ≤ 100
-2500 ≤ S[i][j] ≤ 2500
S[1][1] = S[N][N] = 0
Your code will be judged against several input files.
------ Example ------
----- Sample Input 1 ------
2
2
0 -4
8 0
2
0 -45
-3 0
----- Sample Output 1 ------
8.000000
Bad Judges
----- explanation 1 ------
Test case $1$: An optimal path for Cyael would be $(1,1)\rightarrow (2,1)\rightarrow (2,2)$. This way Cyael faces $1$ judge and gets a total score of $8$. Thus, the average score is $\frac{8}{1} = 8$.
Test case $2$: No matter what path Cyael chooses, the final score would be less than $0$. | t = int(input())
for _ in range(t):
n = int(input())
arr = []
for _ in range(n):
arr.append(list(map(int, input().split())))
total_judge = 2 * n - 3
for i in range(n):
for j in range(n):
if i == 0 and j == 0:
pass
elif i == 0:
arr[i][j] += arr[i][j - 1]
elif j == 0:
arr[i][j] += arr[i - 1][j]
else:
arr[i][j] += max(arr[i - 1][j], arr[i][j - 1])
if arr[-1][-1] < 0:
print("Bad Judges")
else:
ans = arr[-1][-1] / total_judge
print("{:.6f}".format(ans)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
Everybody loves magic, especially magicians who compete for glory on the Byteland Magic Tournament. Magician Cyael is one such magician.
Cyael has been having some issues with her last performances and today she’ll have to perform for an audience of some judges, who will change her tournament ranking, possibly increasing it. As she is a great magician she managed to gather a description of the fixed judges’ disposition on the room (which is represented as an N × N square matrix), such that she knows in advance the fixed points each judge will provide. She also knows that the room is divided into several parallel corridors, such that we will denote the j-th cell on corridor i, as [i][j]. Note that some judges can award Cyael, zero points or negative points, as they are never pleased with her performance. There is just one judge at each cell of the matrix, except the cells [1][1] and [N][N].
To complete her evaluation, she must start on the top leftmost corner of the room (cell [1][1]), and finish on the bottom right corner (cell [N][N]), moving either to the cell directly in front of her on the same corridor (that is, moving from cell [r][c] to cell [r][c+1], where c+1 ≤ N) or to the cell in the next corridor directly in front of where she is (that is, moving from cell [r][c] to cell [r+1][c], where r+1 ≤ N). She will keep doing this until she reaches the end point of the room, i.e. last cell [N][N] on the last corridor. Cyael will be judged at all visited cells with a judge.
Cyael wants to maximize her average score at end of her performance. More specifically, if she passes K judges, each being on cell [i_{1}][j_{1}], cell [i_{2}][j_{2}], ..., cell [i_{K}][j_{K}] respectively, then she wants to maximize (S[i_{1}][j_{1}] + S[i_{2}][j_{2}] + ... + S[i_{K}][j_{K}]) / K, where S[i][j] denotes the points that the judge will give her on the cell [i][j].
Help her determine the best path she has to follow in order to maximize her average points.
------ Input ------
The first line contains a single integer T denoting the number of test cases. The description for T test cases follows. For each test case, the first line contains a single integer N. Each of the next N lines contains N space-separated integers.
The j-th integer S[i][j] in i-th line denotes the points awarded by the judge at cell [i][j].
Note that the cells [1][1] and [N][N] have no judges, so S[1][1] and S[N][N] will be 0.
------ Output ------
For each test case, if the maximum possible average points Cyael can obtain is negative, output a single line containing "Bad Judges" (quotes for clarity). Otherwise, output the maximum possible average points. The answer will be considered correct if it has an absolute error no more than 10^{-6}.
------ Constraints ------
1 ≤ T ≤ 20
2 ≤ N ≤ 100
-2500 ≤ S[i][j] ≤ 2500
S[1][1] = S[N][N] = 0
Your code will be judged against several input files.
------ Example ------
----- Sample Input 1 ------
2
2
0 -4
8 0
2
0 -45
-3 0
----- Sample Output 1 ------
8.000000
Bad Judges
----- explanation 1 ------
Test case $1$: An optimal path for Cyael would be $(1,1)\rightarrow (2,1)\rightarrow (2,2)$. This way Cyael faces $1$ judge and gets a total score of $8$. Thus, the average score is $\frac{8}{1} = 8$.
Test case $2$: No matter what path Cyael chooses, the final score would be less than $0$. | for t in range(int(input())):
n = int(input())
a = []
for i in range(n):
a.append(list(map(int, input().split())))
dp = []
for i in range(n):
dp.append([])
for i in range(n):
for j in range(n):
dp[i].append(0)
dp[0][0] = 0
for j in range(1, n):
dp[0][j] = dp[0][j - 1] + a[0][j]
for i in range(1, n):
dp[i][0] = dp[i - 1][0] + a[i][0]
for i in range(1, n):
for j in range(1, n):
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]) + a[i][j]
if dp[n - 1][n - 1] < 0:
print("Bad Judges")
else:
x = n - 2
x *= 2
x += 1
print("%.6f" % (dp[n - 1][n - 1] / x)) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP STRING BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR |
Everybody loves magic, especially magicians who compete for glory on the Byteland Magic Tournament. Magician Cyael is one such magician.
Cyael has been having some issues with her last performances and today she’ll have to perform for an audience of some judges, who will change her tournament ranking, possibly increasing it. As she is a great magician she managed to gather a description of the fixed judges’ disposition on the room (which is represented as an N × N square matrix), such that she knows in advance the fixed points each judge will provide. She also knows that the room is divided into several parallel corridors, such that we will denote the j-th cell on corridor i, as [i][j]. Note that some judges can award Cyael, zero points or negative points, as they are never pleased with her performance. There is just one judge at each cell of the matrix, except the cells [1][1] and [N][N].
To complete her evaluation, she must start on the top leftmost corner of the room (cell [1][1]), and finish on the bottom right corner (cell [N][N]), moving either to the cell directly in front of her on the same corridor (that is, moving from cell [r][c] to cell [r][c+1], where c+1 ≤ N) or to the cell in the next corridor directly in front of where she is (that is, moving from cell [r][c] to cell [r+1][c], where r+1 ≤ N). She will keep doing this until she reaches the end point of the room, i.e. last cell [N][N] on the last corridor. Cyael will be judged at all visited cells with a judge.
Cyael wants to maximize her average score at end of her performance. More specifically, if she passes K judges, each being on cell [i_{1}][j_{1}], cell [i_{2}][j_{2}], ..., cell [i_{K}][j_{K}] respectively, then she wants to maximize (S[i_{1}][j_{1}] + S[i_{2}][j_{2}] + ... + S[i_{K}][j_{K}]) / K, where S[i][j] denotes the points that the judge will give her on the cell [i][j].
Help her determine the best path she has to follow in order to maximize her average points.
------ Input ------
The first line contains a single integer T denoting the number of test cases. The description for T test cases follows. For each test case, the first line contains a single integer N. Each of the next N lines contains N space-separated integers.
The j-th integer S[i][j] in i-th line denotes the points awarded by the judge at cell [i][j].
Note that the cells [1][1] and [N][N] have no judges, so S[1][1] and S[N][N] will be 0.
------ Output ------
For each test case, if the maximum possible average points Cyael can obtain is negative, output a single line containing "Bad Judges" (quotes for clarity). Otherwise, output the maximum possible average points. The answer will be considered correct if it has an absolute error no more than 10^{-6}.
------ Constraints ------
1 ≤ T ≤ 20
2 ≤ N ≤ 100
-2500 ≤ S[i][j] ≤ 2500
S[1][1] = S[N][N] = 0
Your code will be judged against several input files.
------ Example ------
----- Sample Input 1 ------
2
2
0 -4
8 0
2
0 -45
-3 0
----- Sample Output 1 ------
8.000000
Bad Judges
----- explanation 1 ------
Test case $1$: An optimal path for Cyael would be $(1,1)\rightarrow (2,1)\rightarrow (2,2)$. This way Cyael faces $1$ judge and gets a total score of $8$. Thus, the average score is $\frac{8}{1} = 8$.
Test case $2$: No matter what path Cyael chooses, the final score would be less than $0$. | t = int(input())
for z in range(t):
size = int(input())
array = []
for i in range(size):
array.append(list(map(int, input().split())))
for i in range(1, size):
array[i][0] = int(array[i - 1][0]) + int(array[i][0])
for i in range(1, size):
array[0][i] = int(array[0][i - 1]) + int(array[0][i])
for i in range(1, size):
for j in range(1, size):
array[i][j] = max(array[i - 1][j], array[i][j - 1]) + array[i][j]
value = array[size - 1][size - 1] / (2 * size - 3)
if value < 0:
print("Bad Judges")
else:
print("%.6f" % value) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP STRING VAR |
Everybody loves magic, especially magicians who compete for glory on the Byteland Magic Tournament. Magician Cyael is one such magician.
Cyael has been having some issues with her last performances and today she’ll have to perform for an audience of some judges, who will change her tournament ranking, possibly increasing it. As she is a great magician she managed to gather a description of the fixed judges’ disposition on the room (which is represented as an N × N square matrix), such that she knows in advance the fixed points each judge will provide. She also knows that the room is divided into several parallel corridors, such that we will denote the j-th cell on corridor i, as [i][j]. Note that some judges can award Cyael, zero points or negative points, as they are never pleased with her performance. There is just one judge at each cell of the matrix, except the cells [1][1] and [N][N].
To complete her evaluation, she must start on the top leftmost corner of the room (cell [1][1]), and finish on the bottom right corner (cell [N][N]), moving either to the cell directly in front of her on the same corridor (that is, moving from cell [r][c] to cell [r][c+1], where c+1 ≤ N) or to the cell in the next corridor directly in front of where she is (that is, moving from cell [r][c] to cell [r+1][c], where r+1 ≤ N). She will keep doing this until she reaches the end point of the room, i.e. last cell [N][N] on the last corridor. Cyael will be judged at all visited cells with a judge.
Cyael wants to maximize her average score at end of her performance. More specifically, if she passes K judges, each being on cell [i_{1}][j_{1}], cell [i_{2}][j_{2}], ..., cell [i_{K}][j_{K}] respectively, then she wants to maximize (S[i_{1}][j_{1}] + S[i_{2}][j_{2}] + ... + S[i_{K}][j_{K}]) / K, where S[i][j] denotes the points that the judge will give her on the cell [i][j].
Help her determine the best path she has to follow in order to maximize her average points.
------ Input ------
The first line contains a single integer T denoting the number of test cases. The description for T test cases follows. For each test case, the first line contains a single integer N. Each of the next N lines contains N space-separated integers.
The j-th integer S[i][j] in i-th line denotes the points awarded by the judge at cell [i][j].
Note that the cells [1][1] and [N][N] have no judges, so S[1][1] and S[N][N] will be 0.
------ Output ------
For each test case, if the maximum possible average points Cyael can obtain is negative, output a single line containing "Bad Judges" (quotes for clarity). Otherwise, output the maximum possible average points. The answer will be considered correct if it has an absolute error no more than 10^{-6}.
------ Constraints ------
1 ≤ T ≤ 20
2 ≤ N ≤ 100
-2500 ≤ S[i][j] ≤ 2500
S[1][1] = S[N][N] = 0
Your code will be judged against several input files.
------ Example ------
----- Sample Input 1 ------
2
2
0 -4
8 0
2
0 -45
-3 0
----- Sample Output 1 ------
8.000000
Bad Judges
----- explanation 1 ------
Test case $1$: An optimal path for Cyael would be $(1,1)\rightarrow (2,1)\rightarrow (2,2)$. This way Cyael faces $1$ judge and gets a total score of $8$. Thus, the average score is $\frac{8}{1} = 8$.
Test case $2$: No matter what path Cyael chooses, the final score would be less than $0$. | tn = int(input())
for _ in range(tn):
n = int(input())
ar = [list(map(int, input().split())) for _ in range(n)]
dp = [[(0) for _ in range(n + 1)] for _ in range(n + 1)]
for i in range(1, n + 1):
dp[0][i] = dp[i][0] = -1000000000
for i in range(n):
for j in range(n):
if i == 0 and j == 0:
dp[i + 1][j + 1] = ar[i][j]
continue
dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]) + ar[i][j]
sm = dp[-1][-1] / (2 * n - 3)
if sm >= 0:
print("{:.6f}".format(sm))
else:
print("Bad Judges") | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR EXPR FUNC_CALL VAR STRING |
Recently Lynyrd and Skynyrd went to a shop where Lynyrd bought a permutation $p$ of length $n$, and Skynyrd bought an array $a$ of length $m$, consisting of integers from $1$ to $n$.
Lynyrd and Skynyrd became bored, so they asked you $q$ queries, each of which has the following form: "does the subsegment of $a$ from the $l$-th to the $r$-th positions, inclusive, have a subsequence that is a cyclic shift of $p$?" Please answer the queries.
A permutation of length $n$ is a sequence of $n$ integers such that each integer from $1$ to $n$ appears exactly once in it.
A cyclic shift of a permutation $(p_1, p_2, \ldots, p_n)$ is a permutation $(p_i, p_{i + 1}, \ldots, p_{n}, p_1, p_2, \ldots, p_{i - 1})$ for some $i$ from $1$ to $n$. For example, a permutation $(2, 1, 3)$ has three distinct cyclic shifts: $(2, 1, 3)$, $(1, 3, 2)$, $(3, 2, 1)$.
A subsequence of a subsegment of array $a$ from the $l$-th to the $r$-th positions, inclusive, is a sequence $a_{i_1}, a_{i_2}, \ldots, a_{i_k}$ for some $i_1, i_2, \ldots, i_k$ such that $l \leq i_1 < i_2 < \ldots < i_k \leq r$.
-----Input-----
The first line contains three integers $n$, $m$, $q$ ($1 \le n, m, q \le 2 \cdot 10^5$) — the length of the permutation $p$, the length of the array $a$ and the number of queries.
The next line contains $n$ integers from $1$ to $n$, where the $i$-th of them is the $i$-th element of the permutation. Each integer from $1$ to $n$ appears exactly once.
The next line contains $m$ integers from $1$ to $n$, the $i$-th of them is the $i$-th element of the array $a$.
The next $q$ lines describe queries. The $i$-th of these lines contains two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le m$), meaning that the $i$-th query is about the subsegment of the array from the $l_i$-th to the $r_i$-th positions, inclusive.
-----Output-----
Print a single string of length $q$, consisting of $0$ and $1$, the digit on the $i$-th positions should be $1$, if the subsegment of array $a$ from the $l_i$-th to the $r_i$-th positions, inclusive, contains a subsequence that is a cyclic shift of $p$, and $0$ otherwise.
-----Examples-----
Input
3 6 3
2 1 3
1 2 3 1 2 3
1 5
2 6
3 5
Output
110
Input
2 4 3
2 1
1 1 2 2
1 2
2 3
3 4
Output
010
-----Note-----
In the first example the segment from the $1$-st to the $5$-th positions is $1, 2, 3, 1, 2$. There is a subsequence $1, 3, 2$ that is a cyclic shift of the permutation. The subsegment from the $2$-nd to the $6$-th positions also contains a subsequence $2, 1, 3$ that is equal to the permutation. The subsegment from the $3$-rd to the $5$-th positions is $3, 1, 2$, there is only one subsequence of length $3$ ($3, 1, 2$), but it is not a cyclic shift of the permutation.
In the second example the possible cyclic shifts are $1, 2$ and $2, 1$. The subsegment from the $1$-st to the $2$-nd positions is $1, 1$, its subsequences are not cyclic shifts of the permutation. The subsegment from the $2$-nd to the $3$-rd positions is $1, 2$, it coincides with the permutation. The subsegment from the $3$ to the $4$ positions is $2, 2$, its subsequences are not cyclic shifts of the permutation. | import sys
inp = [int(x) for x in sys.stdin.read().split()]
n, m, q = inp[0], inp[1], inp[2]
p = [inp[idx] for idx in range(3, n + 3)]
index_arr = [0] * (n + 1)
for i in range(n):
index_arr[p[i]] = i
a = [inp[idx] for idx in range(n + 3, n + 3 + m)]
leftmost_pos = [m] * (n + 1)
next = [-1] * m
for i in range(m - 1, -1, -1):
index = index_arr[a[i]]
right_index = 0 if index == n - 1 else index + 1
right = p[right_index]
next[i] = leftmost_pos[right]
leftmost_pos[a[i]] = i
log = 0
while 1 << log <= n:
log += 1
log += 1
dp = [[m for _ in range(m + 1)] for _ in range(log)]
for i in range(m):
dp[0][i] = next[i]
for j in range(1, log):
for i in range(m):
dp[j][i] = dp[j - 1][dp[j - 1][i]]
last = [0] * m
for i in range(m):
p = i
len = n - 1
for j in range(log - 1, -1, -1):
if 1 << j <= len:
p = dp[j][p]
len -= 1 << j
last[i] = p
for i in range(m - 2, -1, -1):
last[i] = min(last[i], last[i + 1])
inp_idx = n + m + 3
ans = []
for i in range(q):
l, r = inp[inp_idx] - 1, inp[inp_idx + 1] - 1
inp_idx += 2
if last[l] <= r:
ans.append("1")
else:
ans.append("0")
print("".join(ans)) | IMPORT ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP NUMBER VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
Recently Lynyrd and Skynyrd went to a shop where Lynyrd bought a permutation $p$ of length $n$, and Skynyrd bought an array $a$ of length $m$, consisting of integers from $1$ to $n$.
Lynyrd and Skynyrd became bored, so they asked you $q$ queries, each of which has the following form: "does the subsegment of $a$ from the $l$-th to the $r$-th positions, inclusive, have a subsequence that is a cyclic shift of $p$?" Please answer the queries.
A permutation of length $n$ is a sequence of $n$ integers such that each integer from $1$ to $n$ appears exactly once in it.
A cyclic shift of a permutation $(p_1, p_2, \ldots, p_n)$ is a permutation $(p_i, p_{i + 1}, \ldots, p_{n}, p_1, p_2, \ldots, p_{i - 1})$ for some $i$ from $1$ to $n$. For example, a permutation $(2, 1, 3)$ has three distinct cyclic shifts: $(2, 1, 3)$, $(1, 3, 2)$, $(3, 2, 1)$.
A subsequence of a subsegment of array $a$ from the $l$-th to the $r$-th positions, inclusive, is a sequence $a_{i_1}, a_{i_2}, \ldots, a_{i_k}$ for some $i_1, i_2, \ldots, i_k$ such that $l \leq i_1 < i_2 < \ldots < i_k \leq r$.
-----Input-----
The first line contains three integers $n$, $m$, $q$ ($1 \le n, m, q \le 2 \cdot 10^5$) — the length of the permutation $p$, the length of the array $a$ and the number of queries.
The next line contains $n$ integers from $1$ to $n$, where the $i$-th of them is the $i$-th element of the permutation. Each integer from $1$ to $n$ appears exactly once.
The next line contains $m$ integers from $1$ to $n$, the $i$-th of them is the $i$-th element of the array $a$.
The next $q$ lines describe queries. The $i$-th of these lines contains two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le m$), meaning that the $i$-th query is about the subsegment of the array from the $l_i$-th to the $r_i$-th positions, inclusive.
-----Output-----
Print a single string of length $q$, consisting of $0$ and $1$, the digit on the $i$-th positions should be $1$, if the subsegment of array $a$ from the $l_i$-th to the $r_i$-th positions, inclusive, contains a subsequence that is a cyclic shift of $p$, and $0$ otherwise.
-----Examples-----
Input
3 6 3
2 1 3
1 2 3 1 2 3
1 5
2 6
3 5
Output
110
Input
2 4 3
2 1
1 1 2 2
1 2
2 3
3 4
Output
010
-----Note-----
In the first example the segment from the $1$-st to the $5$-th positions is $1, 2, 3, 1, 2$. There is a subsequence $1, 3, 2$ that is a cyclic shift of the permutation. The subsegment from the $2$-nd to the $6$-th positions also contains a subsequence $2, 1, 3$ that is equal to the permutation. The subsegment from the $3$-rd to the $5$-th positions is $3, 1, 2$, there is only one subsequence of length $3$ ($3, 1, 2$), but it is not a cyclic shift of the permutation.
In the second example the possible cyclic shifts are $1, 2$ and $2, 1$. The subsegment from the $1$-st to the $2$-nd positions is $1, 1$, its subsequences are not cyclic shifts of the permutation. The subsegment from the $2$-nd to the $3$-rd positions is $1, 2$, it coincides with the permutation. The subsegment from the $3$ to the $4$ positions is $2, 2$, its subsequences are not cyclic shifts of the permutation. | import sys
N, M, Q = map(int, sys.stdin.readline().split())
P = list(map(int, sys.stdin.readline().split()))
A = list(map(int, sys.stdin.readline().split()))
k = N.bit_length()
bef = [-2] * (N + 1)
for p, q in zip(P[1:] + [P[0]], P):
bef[p] = q
recentapp = [-1] * (N + 1)
prepoints = [([-1] * (M + 2)) for _ in range(k)]
for i, a in enumerate(A, 1):
prepoints[0][i] = recentapp[bef[a]]
recentapp[a] = i
for dim in range(1, k):
for i in range(1, M + 1):
prepoints[dim][i] = prepoints[dim - 1][prepoints[dim - 1][i]]
use = [i for i in range(k) if 1 & N - 1 >> i]
ran = [-1] * (M + 2)
maxpre = -1
for i in range(1, M + 1):
t = i
for dim in use:
t = prepoints[dim][t]
maxpre = max(maxpre, t)
ran[i] = maxpre
Ans = [None] * Q
for q in range(Q):
l, r = map(int, sys.stdin.readline().split())
Ans[q] = str(int(l <= ran[r]))
print("".join(Ans)) | IMPORT 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 VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER LIST VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST NONE VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
Recently Lynyrd and Skynyrd went to a shop where Lynyrd bought a permutation $p$ of length $n$, and Skynyrd bought an array $a$ of length $m$, consisting of integers from $1$ to $n$.
Lynyrd and Skynyrd became bored, so they asked you $q$ queries, each of which has the following form: "does the subsegment of $a$ from the $l$-th to the $r$-th positions, inclusive, have a subsequence that is a cyclic shift of $p$?" Please answer the queries.
A permutation of length $n$ is a sequence of $n$ integers such that each integer from $1$ to $n$ appears exactly once in it.
A cyclic shift of a permutation $(p_1, p_2, \ldots, p_n)$ is a permutation $(p_i, p_{i + 1}, \ldots, p_{n}, p_1, p_2, \ldots, p_{i - 1})$ for some $i$ from $1$ to $n$. For example, a permutation $(2, 1, 3)$ has three distinct cyclic shifts: $(2, 1, 3)$, $(1, 3, 2)$, $(3, 2, 1)$.
A subsequence of a subsegment of array $a$ from the $l$-th to the $r$-th positions, inclusive, is a sequence $a_{i_1}, a_{i_2}, \ldots, a_{i_k}$ for some $i_1, i_2, \ldots, i_k$ such that $l \leq i_1 < i_2 < \ldots < i_k \leq r$.
-----Input-----
The first line contains three integers $n$, $m$, $q$ ($1 \le n, m, q \le 2 \cdot 10^5$) — the length of the permutation $p$, the length of the array $a$ and the number of queries.
The next line contains $n$ integers from $1$ to $n$, where the $i$-th of them is the $i$-th element of the permutation. Each integer from $1$ to $n$ appears exactly once.
The next line contains $m$ integers from $1$ to $n$, the $i$-th of them is the $i$-th element of the array $a$.
The next $q$ lines describe queries. The $i$-th of these lines contains two integers $l_i$ and $r_i$ ($1 \le l_i \le r_i \le m$), meaning that the $i$-th query is about the subsegment of the array from the $l_i$-th to the $r_i$-th positions, inclusive.
-----Output-----
Print a single string of length $q$, consisting of $0$ and $1$, the digit on the $i$-th positions should be $1$, if the subsegment of array $a$ from the $l_i$-th to the $r_i$-th positions, inclusive, contains a subsequence that is a cyclic shift of $p$, and $0$ otherwise.
-----Examples-----
Input
3 6 3
2 1 3
1 2 3 1 2 3
1 5
2 6
3 5
Output
110
Input
2 4 3
2 1
1 1 2 2
1 2
2 3
3 4
Output
010
-----Note-----
In the first example the segment from the $1$-st to the $5$-th positions is $1, 2, 3, 1, 2$. There is a subsequence $1, 3, 2$ that is a cyclic shift of the permutation. The subsegment from the $2$-nd to the $6$-th positions also contains a subsequence $2, 1, 3$ that is equal to the permutation. The subsegment from the $3$-rd to the $5$-th positions is $3, 1, 2$, there is only one subsequence of length $3$ ($3, 1, 2$), but it is not a cyclic shift of the permutation.
In the second example the possible cyclic shifts are $1, 2$ and $2, 1$. The subsegment from the $1$-st to the $2$-nd positions is $1, 1$, its subsequences are not cyclic shifts of the permutation. The subsegment from the $2$-nd to the $3$-rd positions is $1, 2$, it coincides with the permutation. The subsegment from the $3$ to the $4$ positions is $2, 2$, its subsequences are not cyclic shifts of the permutation. | import sys
class segmentTree:
def __init__(self, n):
self.n = n
self.seg = [self.n + 1] * (self.n << 1)
def update(self, p, value):
p += self.n
self.seg[p] = value
while p > 1:
p >>= 1
self.seg[p] = min(self.seg[p * 2], self.seg[p * 2 + 1])
def query(self, l, r):
res = self.n
l += self.n
r += self.n
while l < r:
if l & 1:
res = min(res, self.seg[l])
l += 1
if r & 1:
res = min(res, self.seg[r - 1])
r -= 1
l >>= 1
r >>= 1
return res
inp = [int(x) for x in sys.stdin.read().split()]
n, m, q = inp[0], inp[1], inp[2]
p = [inp[idx] for idx in range(3, n + 3)]
index_arr = [0] * (n + 1)
for i in range(n):
index_arr[p[i]] = i
a = [inp[idx] for idx in range(n + 3, n + 3 + m)]
leftmost_pos = [m] * (n + 1)
next = [-1] * m
for i in range(m - 1, -1, -1):
index = index_arr[a[i]]
right_index = 0 if index == n - 1 else index + 1
right = p[right_index]
next[i] = leftmost_pos[right]
leftmost_pos[a[i]] = i
log = 0
while 1 << log <= n:
log += 1
log += 1
dp = [[m for _ in range(m + 1)] for _ in range(log)]
for i in range(m):
dp[0][i] = next[i]
for j in range(1, log):
for i in range(m):
dp[j][i] = dp[j - 1][dp[j - 1][i]]
tree = segmentTree(m)
for i in range(m):
p = i
len = n - 1
for j in range(log - 1, -1, -1):
if 1 << j <= len:
p = dp[j][p]
len -= 1 << j
tree.update(i, p)
inp_idx = n + m + 3
ans = []
for i in range(q):
l, r = inp[inp_idx] - 1, inp[inp_idx + 1] - 1
inp_idx += 2
if tree.query(l, r + 1) <= r:
ans.append("1")
else:
ans.append("0")
print("".join(ans)) | IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_DEF VAR VAR ASSIGN VAR VAR VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR VAR VAR VAR VAR WHILE VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP NUMBER VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR FUNC_CALL STRING VAR |
The director of your college is planning to send 2 people to the ICPC regionals. He wants them to be from different branches. You will be given a list of pairs of student ids. Each pair is made of students from the same branch. Determine how many pairs of students from different branches they can choose from.
Example 1:
Input:
N=5
P=3
pairs[]={{0,1},
{2,3},
{0,4}}
Output:
6
Explanation:
Their are total five studets 0,1,2,3,4.
Students [0,1,4] are from same bracnh while
[2,3] are from a different one.So we can choose
different pairs like: [0,2],[0,3],[1,2],[1,3],
[4,2],[4,3]
Example 2:
Input:
N=4
P=1
pairs[]={{0,2}}
Output:
5
Explanation:
[0,1],[0,3],[2,1],[2,3] and [1,3] are all possible
pairs because [0,2],[1] and [3] all belongs to
different branches.
Your Task:
You don't need to read input or print anything. Your task is to complete the function numberOfPairs() which takes the 2d array pairs[], its size P and an integer N representing total number of students as input parameters and returns the total number of pairs(as explianed in the question)..
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraint:
1<=N<=10^{5}
1<=P<=10^{4}
0<=P[i][0],P[i][1] | class Solution:
def __init__(self):
self.num = 0
def numberOfPairs(self, a, pairs) -> int:
graph = {i: [] for i in range(a[0])}
out = []
for p1, p2 in pairs:
graph[p1].append(p2)
graph[p2].append(p1)
visit = [(False) for _ in range(a[0])]
for i in range(a[0]):
if visit[i] is False:
self.num = 0
self.dfs(i, graph, visit)
out.append(self.num)
ans = 0
temp = 0
a1 = [0] * len(out)
for i in range(len(out)):
a1[i] = temp
temp += out[i]
for i in range(len(out)):
ans += a1[i] * out[i]
return ans
def dfs(self, i, graph, visit):
visit[i] = True
self.num += 1
for ele in graph[i]:
if visit[ele] is False:
self.dfs(ele, graph, visit) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR VAR LIST VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER VAR NUMBER FOR VAR VAR VAR IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR |
Given an array called A[] of sorted integers having no duplicates, find the length of the Longest Arithmetic Progression (LLAP) in it.
Example 1:
Input:
N = 6
set[] = {1, 7, 10, 13, 14, 19}
Output: 4
Explanation: The longest arithmetic
progression is {1, 7, 13, 19}.
Example 2:
Input:
N = 5
A[] = {2, 4, 6, 8, 10}
Output: 5
Explanation: The whole set is in AP.
Your Task:
You don't need to read input or print anything. Your task is to complete the function lenghtOfLongestAP() which takes the array of integers called set[] and n as input parameters and returns the length of LLAP.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ N ≤ 1000
1 ≤ set[i] ≤ 10^{4} | class Solution:
def lengthOfLongestAP(self, A, n):
diff = A[-1] - A[0]
dp = [[(0) for i in range(n)] for i in range(diff + 1)]
ans = 0
for i in range(1, n):
for j in range(i):
d = A[i] - A[j]
dp[d][i] = dp[d][j] + 1
ans = max(ans, dp[d][i])
return ans + 1 | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN BIN_OP VAR NUMBER |
Given an array called A[] of sorted integers having no duplicates, find the length of the Longest Arithmetic Progression (LLAP) in it.
Example 1:
Input:
N = 6
set[] = {1, 7, 10, 13, 14, 19}
Output: 4
Explanation: The longest arithmetic
progression is {1, 7, 13, 19}.
Example 2:
Input:
N = 5
A[] = {2, 4, 6, 8, 10}
Output: 5
Explanation: The whole set is in AP.
Your Task:
You don't need to read input or print anything. Your task is to complete the function lenghtOfLongestAP() which takes the array of integers called set[] and n as input parameters and returns the length of LLAP.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ N ≤ 1000
1 ≤ set[i] ≤ 10^{4} | class Solution:
def lengthOfLongestAP(self, set, n):
if n <= 2:
return n
L = [[(2) for x in range(n)] for y in range(n)]
llap = 2
for i in range(n):
L[i][n - 1] = 2
for j in range(n - 2, 0, -1):
i = j - 1
k = j + 1
while i >= 0 and k <= n - 1:
if set[i] + set[k] < 2 * set[j]:
k += 1
elif set[i] + set[k] > 2 * set[j]:
L[i][j] = 2
i -= 1
else:
L[i][j] = L[j][k] + 1
llap = max(llap, L[i][j])
i -= 1
k += 1
return llap | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP NUMBER VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP NUMBER VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR |
Given an array called A[] of sorted integers having no duplicates, find the length of the Longest Arithmetic Progression (LLAP) in it.
Example 1:
Input:
N = 6
set[] = {1, 7, 10, 13, 14, 19}
Output: 4
Explanation: The longest arithmetic
progression is {1, 7, 13, 19}.
Example 2:
Input:
N = 5
A[] = {2, 4, 6, 8, 10}
Output: 5
Explanation: The whole set is in AP.
Your Task:
You don't need to read input or print anything. Your task is to complete the function lenghtOfLongestAP() which takes the array of integers called set[] and n as input parameters and returns the length of LLAP.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ N ≤ 1000
1 ≤ set[i] ≤ 10^{4} | class Solution:
def lengthOfLongestAP(self, A, n):
a = A
if n <= 2:
return n
ans = 0
dp = [{} for i in range(n + 1)]
for i in range(1, n):
for j in range(0, i):
diff = a[i] - a[j]
cnt = 1
if diff in dp[j]:
cnt = dp[j][diff]
dp[i][diff] = 1 + cnt
ans = max(ans, dp[i][diff])
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR VAR IF VAR NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR DICT VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR |
Given an array called A[] of sorted integers having no duplicates, find the length of the Longest Arithmetic Progression (LLAP) in it.
Example 1:
Input:
N = 6
set[] = {1, 7, 10, 13, 14, 19}
Output: 4
Explanation: The longest arithmetic
progression is {1, 7, 13, 19}.
Example 2:
Input:
N = 5
A[] = {2, 4, 6, 8, 10}
Output: 5
Explanation: The whole set is in AP.
Your Task:
You don't need to read input or print anything. Your task is to complete the function lenghtOfLongestAP() which takes the array of integers called set[] and n as input parameters and returns the length of LLAP.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ N ≤ 1000
1 ≤ set[i] ≤ 10^{4} | class Solution:
def Solve(self, A, n, index, diff, memory):
if index < 0:
return 0
if memory[index][diff] == -1:
ans = 0
for back in range(index - 1, -1, -1):
if A[index] - A[back] == diff:
ans = max(ans, 1 + self.Solve(A, n, back, diff, memory))
memory[index][diff] = ans
return memory[index][diff]
def Bottom(self, a, n):
diff = a[n - 1] - a[0]
memory = [[(0) for i in range(diff + 1)] for j in range(n)]
if n <= 2:
return n
ans = 0
for i in range(n):
for j in range(0, i):
current_count = 1
current_diff = a[i] - a[j]
if memory[j][current_diff]:
current_count = memory[j][current_diff]
memory[i][current_diff] = 1 + current_count
ans = max(ans, 1 + current_count)
return ans
def lengthOfLongestAP(self, A, n):
return self.Bottom(A, n) | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR |
Given an array called A[] of sorted integers having no duplicates, find the length of the Longest Arithmetic Progression (LLAP) in it.
Example 1:
Input:
N = 6
set[] = {1, 7, 10, 13, 14, 19}
Output: 4
Explanation: The longest arithmetic
progression is {1, 7, 13, 19}.
Example 2:
Input:
N = 5
A[] = {2, 4, 6, 8, 10}
Output: 5
Explanation: The whole set is in AP.
Your Task:
You don't need to read input or print anything. Your task is to complete the function lenghtOfLongestAP() which takes the array of integers called set[] and n as input parameters and returns the length of LLAP.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ N ≤ 1000
1 ≤ set[i] ≤ 10^{4} | class Solution:
def lengthOfLongestAP(self, A, n):
if len(A) == 1:
return 1
dp = [{} for _ in range(len(A))]
maxi = 0
for i in range(1, len(A)):
for j in range(i):
diff = A[i] - A[j]
if diff in dp[j]:
dp[i][diff] = dp[j][diff] + 1
else:
dp[i][diff] = 2
maxi = max(dp[i][diff], maxi)
return maxi | CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR DICT VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR |
Given an array called A[] of sorted integers having no duplicates, find the length of the Longest Arithmetic Progression (LLAP) in it.
Example 1:
Input:
N = 6
set[] = {1, 7, 10, 13, 14, 19}
Output: 4
Explanation: The longest arithmetic
progression is {1, 7, 13, 19}.
Example 2:
Input:
N = 5
A[] = {2, 4, 6, 8, 10}
Output: 5
Explanation: The whole set is in AP.
Your Task:
You don't need to read input or print anything. Your task is to complete the function lenghtOfLongestAP() which takes the array of integers called set[] and n as input parameters and returns the length of LLAP.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ N ≤ 1000
1 ≤ set[i] ≤ 10^{4} | class Solution:
def lengthOfLongestAP(self, nums, n):
n = len(nums)
if n <= 2:
return n
ans = 0
dp = [{} for i in range(n)]
for i in range(1, n):
for j in range(0, i):
diff = nums[i] - nums[j]
if diff in dp[j]:
dp[i][diff] = max(2, 1 + dp[j][diff])
else:
dp[i][diff] = 2
ans = max(ans, dp[i][diff])
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR DICT VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP NUMBER VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR |
Given an array called A[] of sorted integers having no duplicates, find the length of the Longest Arithmetic Progression (LLAP) in it.
Example 1:
Input:
N = 6
set[] = {1, 7, 10, 13, 14, 19}
Output: 4
Explanation: The longest arithmetic
progression is {1, 7, 13, 19}.
Example 2:
Input:
N = 5
A[] = {2, 4, 6, 8, 10}
Output: 5
Explanation: The whole set is in AP.
Your Task:
You don't need to read input or print anything. Your task is to complete the function lenghtOfLongestAP() which takes the array of integers called set[] and n as input parameters and returns the length of LLAP.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ N ≤ 1000
1 ≤ set[i] ≤ 10^{4} | class Solution:
def lengthOfLongestAP(self, A, n):
if n <= 2:
return n
arr = A
dp = {}
max_length = 0
dp[0] = {}
for i in range(1, n):
dp[i] = {}
for j in range(0, i):
diff = arr[i] - arr[j]
if diff in dp[j]:
dp[i][diff] = dp[j][diff] + 1
else:
dp[i][diff] = 2
max_length = max(max_length, dp[i][diff])
return max_length | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN VAR ASSIGN VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER DICT FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR DICT FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR |
Given an array called A[] of sorted integers having no duplicates, find the length of the Longest Arithmetic Progression (LLAP) in it.
Example 1:
Input:
N = 6
set[] = {1, 7, 10, 13, 14, 19}
Output: 4
Explanation: The longest arithmetic
progression is {1, 7, 13, 19}.
Example 2:
Input:
N = 5
A[] = {2, 4, 6, 8, 10}
Output: 5
Explanation: The whole set is in AP.
Your Task:
You don't need to read input or print anything. Your task is to complete the function lenghtOfLongestAP() which takes the array of integers called set[] and n as input parameters and returns the length of LLAP.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ N ≤ 1000
1 ≤ set[i] ≤ 10^{4} | class Solution:
def lengthOfLongestAP(self, A, n):
if not A:
return 0
if n <= 0:
return 0
if n == 1:
return 1
if n == 2:
return 2
dp = {}
for i in range(1, len(A)):
for j in range(i):
dp[i, A[i] - A[j]] = dp.get((j, A[i] - A[j]), 1) + 1
return max(dp.values()) | CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR NUMBER NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR |
Given an array called A[] of sorted integers having no duplicates, find the length of the Longest Arithmetic Progression (LLAP) in it.
Example 1:
Input:
N = 6
set[] = {1, 7, 10, 13, 14, 19}
Output: 4
Explanation: The longest arithmetic
progression is {1, 7, 13, 19}.
Example 2:
Input:
N = 5
A[] = {2, 4, 6, 8, 10}
Output: 5
Explanation: The whole set is in AP.
Your Task:
You don't need to read input or print anything. Your task is to complete the function lenghtOfLongestAP() which takes the array of integers called set[] and n as input parameters and returns the length of LLAP.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ N ≤ 1000
1 ≤ set[i] ≤ 10^{4} | class Solution:
def lengthOfLongestAP(self, a, n):
if n <= 2:
return n
dp = [dict() for i in range(n)]
ans = 0
for i in range(1, n):
for j in range(i):
diff = A[i] - A[j]
count = 1
if dp[j].get(diff):
count = dp[j][diff]
dp[i][diff] = count + 1
ans = max(ans, dp[i][diff])
return ans
return max(dp) | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR |
Given an array called A[] of sorted integers having no duplicates, find the length of the Longest Arithmetic Progression (LLAP) in it.
Example 1:
Input:
N = 6
set[] = {1, 7, 10, 13, 14, 19}
Output: 4
Explanation: The longest arithmetic
progression is {1, 7, 13, 19}.
Example 2:
Input:
N = 5
A[] = {2, 4, 6, 8, 10}
Output: 5
Explanation: The whole set is in AP.
Your Task:
You don't need to read input or print anything. Your task is to complete the function lenghtOfLongestAP() which takes the array of integers called set[] and n as input parameters and returns the length of LLAP.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ N ≤ 1000
1 ≤ set[i] ≤ 10^{4} | class Solution:
def lengthOfLongestAP(self, A, n):
dp, ans = {}, 1
for i in range(n):
vi = A[i]
for j in range(i):
vj = A[j]
dif = vi - vj
tmp = dp[vi, dif] = 1 + dp.get((vj, dif), 1)
ans = max(ans, tmp)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR VAR DICT NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given an array called A[] of sorted integers having no duplicates, find the length of the Longest Arithmetic Progression (LLAP) in it.
Example 1:
Input:
N = 6
set[] = {1, 7, 10, 13, 14, 19}
Output: 4
Explanation: The longest arithmetic
progression is {1, 7, 13, 19}.
Example 2:
Input:
N = 5
A[] = {2, 4, 6, 8, 10}
Output: 5
Explanation: The whole set is in AP.
Your Task:
You don't need to read input or print anything. Your task is to complete the function lenghtOfLongestAP() which takes the array of integers called set[] and n as input parameters and returns the length of LLAP.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ N ≤ 1000
1 ≤ set[i] ≤ 10^{4} | class Solution:
def lengthOfLongestAP(self, A, n):
if n <= 2:
return n
result_map = {i: {} for i in range(n + 1)}
ans = 0
dp = {}
for i in range(len(A)):
for j in range(i + 1, len(A)):
diff = A[j] - A[i]
dp[j, diff] = dp.get((i, diff), 1) + 1
ans = max(ans, dp[j, diff])
return ans | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN VAR ASSIGN VAR VAR DICT VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR |
Given an array called A[] of sorted integers having no duplicates, find the length of the Longest Arithmetic Progression (LLAP) in it.
Example 1:
Input:
N = 6
set[] = {1, 7, 10, 13, 14, 19}
Output: 4
Explanation: The longest arithmetic
progression is {1, 7, 13, 19}.
Example 2:
Input:
N = 5
A[] = {2, 4, 6, 8, 10}
Output: 5
Explanation: The whole set is in AP.
Your Task:
You don't need to read input or print anything. Your task is to complete the function lenghtOfLongestAP() which takes the array of integers called set[] and n as input parameters and returns the length of LLAP.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ N ≤ 1000
1 ≤ set[i] ≤ 10^{4} | class Solution:
def lengthOfLongestAP(self, A, n):
if n == 1 or n == 2:
return n
d = {}
for h in A:
if h not in d:
d[h] = 1
m = 0
dif = 0
for g in range(n - 1):
for s in range(g + 1, n - 1):
c = 0
i = A[s] - A[g]
o = s + 1
b = A[s]
c += 2
while i + b in d:
c += 1
b += i
m = max(m, c)
return m | CLASS_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given an array called A[] of sorted integers having no duplicates, find the length of the Longest Arithmetic Progression (LLAP) in it.
Example 1:
Input:
N = 6
set[] = {1, 7, 10, 13, 14, 19}
Output: 4
Explanation: The longest arithmetic
progression is {1, 7, 13, 19}.
Example 2:
Input:
N = 5
A[] = {2, 4, 6, 8, 10}
Output: 5
Explanation: The whole set is in AP.
Your Task:
You don't need to read input or print anything. Your task is to complete the function lenghtOfLongestAP() which takes the array of integers called set[] and n as input parameters and returns the length of LLAP.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ N ≤ 1000
1 ≤ set[i] ≤ 10^{4} | class Solution:
def lengthOfLongestAP(self, A, n):
n = len(A)
dp = [{} for i in range(n)]
maxx = 0
for i in range(n):
for j in range(i):
val = A[i] - A[j]
if val in dp[j]:
dp[i][val] = dp[j][val] + 1
else:
dp[i][val] = 1
maxx = max(maxx, dp[i][val])
return maxx + 1 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN BIN_OP VAR NUMBER |
Given an array called A[] of sorted integers having no duplicates, find the length of the Longest Arithmetic Progression (LLAP) in it.
Example 1:
Input:
N = 6
set[] = {1, 7, 10, 13, 14, 19}
Output: 4
Explanation: The longest arithmetic
progression is {1, 7, 13, 19}.
Example 2:
Input:
N = 5
A[] = {2, 4, 6, 8, 10}
Output: 5
Explanation: The whole set is in AP.
Your Task:
You don't need to read input or print anything. Your task is to complete the function lenghtOfLongestAP() which takes the array of integers called set[] and n as input parameters and returns the length of LLAP.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ N ≤ 1000
1 ≤ set[i] ≤ 10^{4} | def LLAP(arr, n):
arr.sort()
if n <= 2:
return n
dp = [[(0) for i in range(n)] for j in range(n)]
res = 2
for i in range(n - 2, -1, -1):
dp[i][n - 1] = 2
for j in range(n - 2, -1, -1):
i = j - 1
k = j + 1
while i >= 0 and k <= n - 1:
if arr[i] + arr[k] > 2 * arr[j]:
dp[i][j] = 2
i = i - 1
elif arr[i] + arr[k] < 2 * arr[j]:
k = k + 1
else:
dp[i][j] = dp[j][k] + 1
res = max(res, dp[i][j])
i = i - 1
k = k + 1
while i >= 0:
dp[i][j] = 2
i = i - 1
return res
class Solution:
def lengthOfLongestAP(self, A, n):
return LLAP(A, n) | FUNC_DEF EXPR FUNC_CALL VAR IF VAR NUMBER RETURN VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP NUMBER VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR VAR |
Given an array called A[] of sorted integers having no duplicates, find the length of the Longest Arithmetic Progression (LLAP) in it.
Example 1:
Input:
N = 6
set[] = {1, 7, 10, 13, 14, 19}
Output: 4
Explanation: The longest arithmetic
progression is {1, 7, 13, 19}.
Example 2:
Input:
N = 5
A[] = {2, 4, 6, 8, 10}
Output: 5
Explanation: The whole set is in AP.
Your Task:
You don't need to read input or print anything. Your task is to complete the function lenghtOfLongestAP() which takes the array of integers called set[] and n as input parameters and returns the length of LLAP.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ N ≤ 1000
1 ≤ set[i] ≤ 10^{4} | class Solution:
def countNumOfElemWithGivenDiff(self, setA, starting_elem, diff):
cnt = 0
next_elem_to_search = starting_elem + diff
while True:
if next_elem_to_search in setA:
cnt += 1
else:
break
next_elem_to_search = next_elem_to_search + diff
return cnt + 1
def lengthOfLongestAP(self, A, n):
setA = set(A)
best_cnt = 1
for i in range(n):
for j in range(i + 1, n):
diff = A[j] - A[i]
cnt = self.countNumOfElemWithGivenDiff(setA, A[i], diff)
best_cnt = max(best_cnt, cnt)
return best_cnt | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR WHILE NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR RETURN BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given an array called A[] of sorted integers having no duplicates, find the length of the Longest Arithmetic Progression (LLAP) in it.
Example 1:
Input:
N = 6
set[] = {1, 7, 10, 13, 14, 19}
Output: 4
Explanation: The longest arithmetic
progression is {1, 7, 13, 19}.
Example 2:
Input:
N = 5
A[] = {2, 4, 6, 8, 10}
Output: 5
Explanation: The whole set is in AP.
Your Task:
You don't need to read input or print anything. Your task is to complete the function lenghtOfLongestAP() which takes the array of integers called set[] and n as input parameters and returns the length of LLAP.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ N ≤ 1000
1 ≤ set[i] ≤ 10^{4} | class Solution:
def lengthOfLongestAP(self, A, n):
dp = []
for x in range(n):
a = dict()
dp.append(a)
ma = 1
for x in range(n):
if x > 0:
for y in range(x):
diff = A[x] - A[y]
if dp[y].get(diff) is not None:
dp[x][diff] = dp[y][diff] + 1
else:
dp[x][diff] = 2
ma = max(ma, dp[x][diff])
return ma | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR NONE ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR |
Given an array called A[] of sorted integers having no duplicates, find the length of the Longest Arithmetic Progression (LLAP) in it.
Example 1:
Input:
N = 6
set[] = {1, 7, 10, 13, 14, 19}
Output: 4
Explanation: The longest arithmetic
progression is {1, 7, 13, 19}.
Example 2:
Input:
N = 5
A[] = {2, 4, 6, 8, 10}
Output: 5
Explanation: The whole set is in AP.
Your Task:
You don't need to read input or print anything. Your task is to complete the function lenghtOfLongestAP() which takes the array of integers called set[] and n as input parameters and returns the length of LLAP.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ N ≤ 1000
1 ≤ set[i] ≤ 10^{4} | class Solution:
def lengthOfLongestAP(self, A, n):
dp = {}
for i in range(n):
for j in range(i + 1, n):
dp[j, A[j] - A[i]] = dp.get((i, A[j] - A[i]), 1) + 1
return max(dp.values()) if dp else 1 | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR NUMBER NUMBER RETURN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER |
Given an array called A[] of sorted integers having no duplicates, find the length of the Longest Arithmetic Progression (LLAP) in it.
Example 1:
Input:
N = 6
set[] = {1, 7, 10, 13, 14, 19}
Output: 4
Explanation: The longest arithmetic
progression is {1, 7, 13, 19}.
Example 2:
Input:
N = 5
A[] = {2, 4, 6, 8, 10}
Output: 5
Explanation: The whole set is in AP.
Your Task:
You don't need to read input or print anything. Your task is to complete the function lenghtOfLongestAP() which takes the array of integers called set[] and n as input parameters and returns the length of LLAP.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ N ≤ 1000
1 ≤ set[i] ≤ 10^{4} | def tabu(array):
n = len(array)
if n <= 2:
return n
ans = 0
dp = [{} for _ in range(n + 1)]
for i in range(1, n):
for j in range(i):
diff = array[i] - array[j]
count = 1
try:
if dp[j][diff] != None:
count = dp[j][diff]
except:
dp[j][diff] = None
dp[i][diff] = 1 + count
ans = max(ans, dp[i][diff])
return ans
class Solution:
def lengthOfLongestAP(self, array, n):
return tabu(array) | FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR DICT VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NONE ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NONE ASSIGN VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR |
Given an array called A[] of sorted integers having no duplicates, find the length of the Longest Arithmetic Progression (LLAP) in it.
Example 1:
Input:
N = 6
set[] = {1, 7, 10, 13, 14, 19}
Output: 4
Explanation: The longest arithmetic
progression is {1, 7, 13, 19}.
Example 2:
Input:
N = 5
A[] = {2, 4, 6, 8, 10}
Output: 5
Explanation: The whole set is in AP.
Your Task:
You don't need to read input or print anything. Your task is to complete the function lenghtOfLongestAP() which takes the array of integers called set[] and n as input parameters and returns the length of LLAP.
Expected Time Complexity: O(N^{2})
Expected Auxiliary Space: O(N^{2})
Constraints:
1 ≤ N ≤ 1000
1 ≤ set[i] ≤ 10^{4} | class Solution:
def lengthOfLongestAP(self, A, n):
t = []
for i in range(n):
t.append({})
ans = 1
for i in range(1, n):
for j in range(i):
diff = A[i] - A[j]
t[i][diff] = max(t[j].get(diff, 0) + 1, t[i].get(diff, 0))
ans = max(ans, 1 + t[i][diff])
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR VAR VAR RETURN VAR |
You are given a matrix grid of n x m size consisting of values 0 and 1. A value of 1 means that you can enter that cell and 0 implies that entry to that cell is not allowed.
You start at the upper-left corner of the grid (1, 1) and you have to reach the bottom-right corner (n, m) such that you can only move in the right or down direction from every cell.
Your task is to calculate the total number of ways of reaching the target modulo (10^{9}+7).
Note: The first (1, 1) and last cell (n, m) of the grid can also be 0
Example 1:
Input:
n = 3, m = 3
grid[][] = {{1, 1, 1};
{1, 0, 1};
{1, 1, 1}}
Output:
2
Explanation:
1 1 1
1 0 1
1 1 1
This is one possible path.
1 1 1
1 0 1
1 1 1
This is another possible path.
Example 2:
Input:
n = 1, m = 3
grid = {{1, 0, 1}}
Output :
0
Explanation:
There is no possible path to reach the end.
Your Task:
You don't need to read input or print anything. Your task is to complete the function uniquePaths() which takes 2 integers n, and m, and a matrix of size n*m as input and returns the number of unique paths from cell (1,1) to (n,m) modulo (10^{9}+7)
Expected Time Complexity: O(n*m)
Expected Auxiliary Space: O(n*m)
Constraints:
1 ≤ n*m ≤ 10^{6} | class Solution:
def uniquePaths(self, n, m, grid):
dp = grid
for i in range(n):
for j in range(m):
if i == 0 and j == 0:
continue
if grid[i][j] == 0:
continue
else:
up = 0
down = 0
if i > 0:
up = dp[i - 1][j]
if j > 0:
down = dp[i][j - 1]
dp[i][j] = up + down
return dp[-1][-1] % (10**9 + 7) | CLASS_DEF FUNC_DEF ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR RETURN BIN_OP VAR NUMBER NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER |
You are given a matrix grid of n x m size consisting of values 0 and 1. A value of 1 means that you can enter that cell and 0 implies that entry to that cell is not allowed.
You start at the upper-left corner of the grid (1, 1) and you have to reach the bottom-right corner (n, m) such that you can only move in the right or down direction from every cell.
Your task is to calculate the total number of ways of reaching the target modulo (10^{9}+7).
Note: The first (1, 1) and last cell (n, m) of the grid can also be 0
Example 1:
Input:
n = 3, m = 3
grid[][] = {{1, 1, 1};
{1, 0, 1};
{1, 1, 1}}
Output:
2
Explanation:
1 1 1
1 0 1
1 1 1
This is one possible path.
1 1 1
1 0 1
1 1 1
This is another possible path.
Example 2:
Input:
n = 1, m = 3
grid = {{1, 0, 1}}
Output :
0
Explanation:
There is no possible path to reach the end.
Your Task:
You don't need to read input or print anything. Your task is to complete the function uniquePaths() which takes 2 integers n, and m, and a matrix of size n*m as input and returns the number of unique paths from cell (1,1) to (n,m) modulo (10^{9}+7)
Expected Time Complexity: O(n*m)
Expected Auxiliary Space: O(n*m)
Constraints:
1 ≤ n*m ≤ 10^{6} | class Solution:
def uniquePaths(self, r, c, mat):
mod = 1000000007
ans = [[0] * c] * r
for i in range(r):
for j in range(c):
if mat[i][j] == 0:
ans[i][j] = 0
elif i == 0 and j == 0:
ans[i][j] = 1
elif i == 0:
ans[i][j] = ans[i][j - 1]
elif j == 0:
ans[i][j] = ans[i - 1][j]
else:
ans[i][j] = ans[i - 1][j] % mod + ans[i][j - 1] % mod
return ans[-1][-1] % mod
return mat | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST BIN_OP LIST NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR RETURN BIN_OP VAR NUMBER NUMBER VAR RETURN VAR |
You are given a matrix grid of n x m size consisting of values 0 and 1. A value of 1 means that you can enter that cell and 0 implies that entry to that cell is not allowed.
You start at the upper-left corner of the grid (1, 1) and you have to reach the bottom-right corner (n, m) such that you can only move in the right or down direction from every cell.
Your task is to calculate the total number of ways of reaching the target modulo (10^{9}+7).
Note: The first (1, 1) and last cell (n, m) of the grid can also be 0
Example 1:
Input:
n = 3, m = 3
grid[][] = {{1, 1, 1};
{1, 0, 1};
{1, 1, 1}}
Output:
2
Explanation:
1 1 1
1 0 1
1 1 1
This is one possible path.
1 1 1
1 0 1
1 1 1
This is another possible path.
Example 2:
Input:
n = 1, m = 3
grid = {{1, 0, 1}}
Output :
0
Explanation:
There is no possible path to reach the end.
Your Task:
You don't need to read input or print anything. Your task is to complete the function uniquePaths() which takes 2 integers n, and m, and a matrix of size n*m as input and returns the number of unique paths from cell (1,1) to (n,m) modulo (10^{9}+7)
Expected Time Complexity: O(n*m)
Expected Auxiliary Space: O(n*m)
Constraints:
1 ≤ n*m ≤ 10^{6} | class Solution:
def uniquePaths(self, n, m, grid):
for i in range(1, n):
if grid[i][0] == 1:
grid[i][0] = grid[i - 1][0]
for i in range(1, m):
if grid[0][i] == 1:
grid[0][i] = grid[0][i - 1]
for i in range(1, n):
for j in range(1, m):
if grid[i][j] == 1:
a = b = 0
if i - 1 > -1:
a = grid[i - 1][j]
if j - 1 > -1:
b = grid[i][j - 1]
grid[i][j] = a + b
grid[i][j] %= 10**9 + 7
return grid[n - 1][m - 1] | CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER RETURN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER |
You are given a matrix grid of n x m size consisting of values 0 and 1. A value of 1 means that you can enter that cell and 0 implies that entry to that cell is not allowed.
You start at the upper-left corner of the grid (1, 1) and you have to reach the bottom-right corner (n, m) such that you can only move in the right or down direction from every cell.
Your task is to calculate the total number of ways of reaching the target modulo (10^{9}+7).
Note: The first (1, 1) and last cell (n, m) of the grid can also be 0
Example 1:
Input:
n = 3, m = 3
grid[][] = {{1, 1, 1};
{1, 0, 1};
{1, 1, 1}}
Output:
2
Explanation:
1 1 1
1 0 1
1 1 1
This is one possible path.
1 1 1
1 0 1
1 1 1
This is another possible path.
Example 2:
Input:
n = 1, m = 3
grid = {{1, 0, 1}}
Output :
0
Explanation:
There is no possible path to reach the end.
Your Task:
You don't need to read input or print anything. Your task is to complete the function uniquePaths() which takes 2 integers n, and m, and a matrix of size n*m as input and returns the number of unique paths from cell (1,1) to (n,m) modulo (10^{9}+7)
Expected Time Complexity: O(n*m)
Expected Auxiliary Space: O(n*m)
Constraints:
1 ≤ n*m ≤ 10^{6} | class Solution:
def uniquePaths(self, n, m, grid):
dp = [([0] * m) for i in range(n)]
dp[0][0] = 1 if grid[0][0] else 0
for i in range(1, n):
dp[i][0] = dp[i - 1][0] if grid[i][0] else 0
for j in range(1, m):
dp[0][j] = dp[0][j - 1] if grid[0][j] else 0
for i in range(1, n):
for j in range(1, m):
dp[i][j] = (
(dp[i - 1][j] + dp[i][j - 1]) % 1000000007 if grid[i][j] else 0
)
return dp[n - 1][m - 1] | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER RETURN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER |
You are given a matrix grid of n x m size consisting of values 0 and 1. A value of 1 means that you can enter that cell and 0 implies that entry to that cell is not allowed.
You start at the upper-left corner of the grid (1, 1) and you have to reach the bottom-right corner (n, m) such that you can only move in the right or down direction from every cell.
Your task is to calculate the total number of ways of reaching the target modulo (10^{9}+7).
Note: The first (1, 1) and last cell (n, m) of the grid can also be 0
Example 1:
Input:
n = 3, m = 3
grid[][] = {{1, 1, 1};
{1, 0, 1};
{1, 1, 1}}
Output:
2
Explanation:
1 1 1
1 0 1
1 1 1
This is one possible path.
1 1 1
1 0 1
1 1 1
This is another possible path.
Example 2:
Input:
n = 1, m = 3
grid = {{1, 0, 1}}
Output :
0
Explanation:
There is no possible path to reach the end.
Your Task:
You don't need to read input or print anything. Your task is to complete the function uniquePaths() which takes 2 integers n, and m, and a matrix of size n*m as input and returns the number of unique paths from cell (1,1) to (n,m) modulo (10^{9}+7)
Expected Time Complexity: O(n*m)
Expected Auxiliary Space: O(n*m)
Constraints:
1 ≤ n*m ≤ 10^{6} | class Solution:
def uniquePaths(self, n, m, grid):
dp = [0] * n
for i in range(m):
cur = [0] * n
for j in range(n):
if grid[j][i] == 0:
cur[j] = 0
elif i == 0 and j == 0:
cur[j] = 1
else:
cur[j] = (dp[j] + cur[j - 1]) % (10**9 + 7)
dp = cur
return dp[n - 1] | CLASS_DEF FUNC_DEF 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 IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR VAR RETURN VAR BIN_OP VAR NUMBER |
You are given a matrix grid of n x m size consisting of values 0 and 1. A value of 1 means that you can enter that cell and 0 implies that entry to that cell is not allowed.
You start at the upper-left corner of the grid (1, 1) and you have to reach the bottom-right corner (n, m) such that you can only move in the right or down direction from every cell.
Your task is to calculate the total number of ways of reaching the target modulo (10^{9}+7).
Note: The first (1, 1) and last cell (n, m) of the grid can also be 0
Example 1:
Input:
n = 3, m = 3
grid[][] = {{1, 1, 1};
{1, 0, 1};
{1, 1, 1}}
Output:
2
Explanation:
1 1 1
1 0 1
1 1 1
This is one possible path.
1 1 1
1 0 1
1 1 1
This is another possible path.
Example 2:
Input:
n = 1, m = 3
grid = {{1, 0, 1}}
Output :
0
Explanation:
There is no possible path to reach the end.
Your Task:
You don't need to read input or print anything. Your task is to complete the function uniquePaths() which takes 2 integers n, and m, and a matrix of size n*m as input and returns the number of unique paths from cell (1,1) to (n,m) modulo (10^{9}+7)
Expected Time Complexity: O(n*m)
Expected Auxiliary Space: O(n*m)
Constraints:
1 ≤ n*m ≤ 10^{6} | class Solution:
def uniquePaths(self, n, m, grid):
if not grid[0][0] or not grid[n - 1][m - 1]:
return 0
temp = [([0] * m) for _ in range(n)]
temp[n - 1][m - 1] = 1
for i in range(n - 2, -1, -1):
if grid[i][m - 1]:
temp[i][m - 1] = temp[i + 1][m - 1]
for i in range(m - 2, -1, -1):
if grid[n - 1][i]:
temp[n - 1][i] = temp[n - 1][i + 1]
for i in range(n - 2, -1, -1):
for j in range(m - 2, -1, -1):
if grid[i][j]:
temp[i][j] = temp[i + 1][j] + temp[i][j + 1]
return temp[0][0] % int(1000000000.0 + 7) | CLASS_DEF FUNC_DEF IF VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP NUMBER NUMBER |
You are given a matrix grid of n x m size consisting of values 0 and 1. A value of 1 means that you can enter that cell and 0 implies that entry to that cell is not allowed.
You start at the upper-left corner of the grid (1, 1) and you have to reach the bottom-right corner (n, m) such that you can only move in the right or down direction from every cell.
Your task is to calculate the total number of ways of reaching the target modulo (10^{9}+7).
Note: The first (1, 1) and last cell (n, m) of the grid can also be 0
Example 1:
Input:
n = 3, m = 3
grid[][] = {{1, 1, 1};
{1, 0, 1};
{1, 1, 1}}
Output:
2
Explanation:
1 1 1
1 0 1
1 1 1
This is one possible path.
1 1 1
1 0 1
1 1 1
This is another possible path.
Example 2:
Input:
n = 1, m = 3
grid = {{1, 0, 1}}
Output :
0
Explanation:
There is no possible path to reach the end.
Your Task:
You don't need to read input or print anything. Your task is to complete the function uniquePaths() which takes 2 integers n, and m, and a matrix of size n*m as input and returns the number of unique paths from cell (1,1) to (n,m) modulo (10^{9}+7)
Expected Time Complexity: O(n*m)
Expected Auxiliary Space: O(n*m)
Constraints:
1 ≤ n*m ≤ 10^{6} | class Solution:
def uniquePaths(self, n, m, grid):
ss = False
for i in range(n):
if grid[i][0] == 0:
ss = True
if ss:
grid[i][0] = 0
ss = False
for i in range(m):
if grid[0][i] == 0:
ss = True
if ss:
grid[0][i] = 0
for i in range(1, n):
for j in range(1, m):
if grid[i][j]:
grid[i][j] = grid[i - 1][j] + grid[i][j - 1]
return grid[-1][-1] % (pow(10, 9) + 7) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER NUMBER BIN_OP FUNC_CALL VAR NUMBER NUMBER NUMBER |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.