description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Given an positive integer N and a list of N integers A[]. Each element in the array denotes the maximum length of jump you can cover. Find out if you can make it to the last index if you start at the first index of the list. Example 1: Input: N = 6 A[] = {1, 2, 0, 3, 0, 0} Output: 1 Explanation: Jump 1 step from first index to second index. Then jump 2 steps to reach 4_{th }index, and now jump 2 steps to reach the end. Example 2: Input: N = 3 A[] = {1, 0, 2} Output: 0 Explanation: You can't reach the end of the array. Your Task: You don't need to read input or print anything. Your task is to complete the function canReach() which takes a Integer N and a list A of size N as input and returns 1 if the end of the array is reachable, else return 0. Expected Time Complexity: O(N) Expected Auxiliary Space: O(1) Constraints: 1 <= N <= 10^{5} 0 <= A[i] <= 10^{5}
class Solution: def canReach(self, A, N): if A[0] == 0: return 1 i = N - 1 m = N while i > 0: if A[i] + i >= N - 1: m = i elif m != N and A[i] + i >= m: m = i i -= 1 if A[i] >= m: return 1 return 0
CLASS_DEF FUNC_DEF IF VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR RETURN NUMBER RETURN NUMBER
Given an positive integer N and a list of N integers A[]. Each element in the array denotes the maximum length of jump you can cover. Find out if you can make it to the last index if you start at the first index of the list. Example 1: Input: N = 6 A[] = {1, 2, 0, 3, 0, 0} Output: 1 Explanation: Jump 1 step from first index to second index. Then jump 2 steps to reach 4_{th }index, and now jump 2 steps to reach the end. Example 2: Input: N = 3 A[] = {1, 0, 2} Output: 0 Explanation: You can't reach the end of the array. Your Task: You don't need to read input or print anything. Your task is to complete the function canReach() which takes a Integer N and a list A of size N as input and returns 1 if the end of the array is reachable, else return 0. Expected Time Complexity: O(N) Expected Auxiliary Space: O(1) Constraints: 1 <= N <= 10^{5} 0 <= A[i] <= 10^{5}
class Solution: def canReach(self, l, n): i = 0 if n == 1: return 1 while i < n and l[i] != 0: ans = l[i] max = 0 k = i if ans + i + 1 < n: k = k + 1 for j in range(i + 1, ans + i + 1): if max < l[j] + j and l[j]: max = l[j] + j k = j i = k else: return 1 return 0
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER IF VAR NUMBER RETURN NUMBER WHILE VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN NUMBER RETURN NUMBER
Given an positive integer N and a list of N integers A[]. Each element in the array denotes the maximum length of jump you can cover. Find out if you can make it to the last index if you start at the first index of the list. Example 1: Input: N = 6 A[] = {1, 2, 0, 3, 0, 0} Output: 1 Explanation: Jump 1 step from first index to second index. Then jump 2 steps to reach 4_{th }index, and now jump 2 steps to reach the end. Example 2: Input: N = 3 A[] = {1, 0, 2} Output: 0 Explanation: You can't reach the end of the array. Your Task: You don't need to read input or print anything. Your task is to complete the function canReach() which takes a Integer N and a list A of size N as input and returns 1 if the end of the array is reachable, else return 0. Expected Time Complexity: O(N) Expected Auxiliary Space: O(1) Constraints: 1 <= N <= 10^{5} 0 <= A[i] <= 10^{5}
class Solution: def canReach(self, A, N): arr = A final = [] boundary = N final = [0] * boundary for i in range(boundary - 1, -1, -1): if i + arr[i] >= boundary - 1: final[i] = 1 elif i + arr[i] < boundary - 1: final[i] = ( min( final[i + 1 : i + 1 + arr[i]] if len(final[i + 1 : i + 1 + arr[i]]) >= 1 else [100000] ) + 1 ) final[boundary - 1] = 0 if final[0] >= 100000: return 0 else: return 1
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR VAR LIST NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER NUMBER RETURN NUMBER RETURN NUMBER
A car travels from a starting position to a destination which is target miles east of the starting position. Along the way, there are gas stations.  Each station[i] represents a gas station that is station[i][0] miles east of the starting position, and has station[i][1] liters of gas. The car starts with an infinite tank of gas, which initially has startFuel liters of fuel in it.  It uses 1 liter of gas per 1 mile that it drives. When the car reaches a gas station, it may stop and refuel, transferring all the gas from the station into the car. What is the least number of refueling stops the car must make in order to reach its destination?  If it cannot reach the destination, return -1. Note that if the car reaches a gas station with 0 fuel left, the car can still refuel there.  If the car reaches the destination with 0 fuel left, it is still considered to have arrived.   Example 1: Input: target = 1, startFuel = 1, stations = [] Output: 0 Explanation: We can reach the target without refueling. Example 2: Input: target = 100, startFuel = 1, stations = [[10,100]] Output: -1 Explanation: We can't reach the target (or even the first gas station). Example 3: Input: target = 100, startFuel = 10, stations = [[10,60],[20,30],[30,30],[60,40]] Output: 2 Explanation: We start with 10 liters of fuel. We drive to position 10, expending 10 liters of fuel. We refuel from 0 liters to 60 liters of gas. Then, we drive from position 10 to position 60 (expending 50 liters of fuel), and refuel from 10 liters to 50 liters of gas. We then drive to and reach the target. We made 2 refueling stops along the way, so we return 2.   Note: 1 <= target, startFuel, stations[i][1] <= 10^9 0 <= stations.length <= 500 0 < stations[0][0] < stations[1][0] < ... < stations[stations.length-1][0] < target
class Solution: def minRefuelStops( self, target: int, startFuel: int, stations: List[List[int]] ) -> int: if target <= startFuel: return 0 if not stations: return -1 dp = [startFuel] stops = 0 lastd = 0 for d, fuel in stations: for i in range(stops, len(dp)): dp[i] -= d - lastd if dp[i] < 0: stops = i + 1 if stops == len(dp): return -1 dp.append(dp[-1] + fuel) for i in range(len(dp) - 2, stops, -1): dp[i] = max(dp[i], dp[i - 1] + fuel) if dp[stops] >= target - d: return stops lastd = d while stops < len(dp): if dp[stops] >= target - lastd: return stops stops += 1 return -1
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR IF VAR VAR RETURN NUMBER IF VAR RETURN NUMBER ASSIGN VAR LIST VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR IF VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR VAR RETURN VAR VAR NUMBER RETURN NUMBER VAR
A car travels from a starting position to a destination which is target miles east of the starting position. Along the way, there are gas stations.  Each station[i] represents a gas station that is station[i][0] miles east of the starting position, and has station[i][1] liters of gas. The car starts with an infinite tank of gas, which initially has startFuel liters of fuel in it.  It uses 1 liter of gas per 1 mile that it drives. When the car reaches a gas station, it may stop and refuel, transferring all the gas from the station into the car. What is the least number of refueling stops the car must make in order to reach its destination?  If it cannot reach the destination, return -1. Note that if the car reaches a gas station with 0 fuel left, the car can still refuel there.  If the car reaches the destination with 0 fuel left, it is still considered to have arrived.   Example 1: Input: target = 1, startFuel = 1, stations = [] Output: 0 Explanation: We can reach the target without refueling. Example 2: Input: target = 100, startFuel = 1, stations = [[10,100]] Output: -1 Explanation: We can't reach the target (or even the first gas station). Example 3: Input: target = 100, startFuel = 10, stations = [[10,60],[20,30],[30,30],[60,40]] Output: 2 Explanation: We start with 10 liters of fuel. We drive to position 10, expending 10 liters of fuel. We refuel from 0 liters to 60 liters of gas. Then, we drive from position 10 to position 60 (expending 50 liters of fuel), and refuel from 10 liters to 50 liters of gas. We then drive to and reach the target. We made 2 refueling stops along the way, so we return 2.   Note: 1 <= target, startFuel, stations[i][1] <= 10^9 0 <= stations.length <= 500 0 < stations[0][0] < stations[1][0] < ... < stations[stations.length-1][0] < target
class Solution: def minRefuelStops( self, target: int, startFuel: int, stations: List[List[int]] ) -> int: if target <= startFuel: return 0 dpRefuels = [-1] * len(stations) for i in range(len(stations)): if stations[i][0] <= startFuel: dpRefuels[i] = stations[i][1] + startFuel if dpRefuels[i] >= target: return 1 for k in range(1, len(stations)): maxPrevFuel = dpRefuels[k - 1] for i in range(k, len(stations)): temp = max(maxPrevFuel, dpRefuels[i]) if maxPrevFuel >= stations[i][0]: dpRefuels[i] = maxPrevFuel + stations[i][1] if dpRefuels[i] >= target: return k + 1 maxPrevFuel = temp return -1
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER VAR IF VAR VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR VAR RETURN BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN NUMBER VAR
A car travels from a starting position to a destination which is target miles east of the starting position. Along the way, there are gas stations.  Each station[i] represents a gas station that is station[i][0] miles east of the starting position, and has station[i][1] liters of gas. The car starts with an infinite tank of gas, which initially has startFuel liters of fuel in it.  It uses 1 liter of gas per 1 mile that it drives. When the car reaches a gas station, it may stop and refuel, transferring all the gas from the station into the car. What is the least number of refueling stops the car must make in order to reach its destination?  If it cannot reach the destination, return -1. Note that if the car reaches a gas station with 0 fuel left, the car can still refuel there.  If the car reaches the destination with 0 fuel left, it is still considered to have arrived.   Example 1: Input: target = 1, startFuel = 1, stations = [] Output: 0 Explanation: We can reach the target without refueling. Example 2: Input: target = 100, startFuel = 1, stations = [[10,100]] Output: -1 Explanation: We can't reach the target (or even the first gas station). Example 3: Input: target = 100, startFuel = 10, stations = [[10,60],[20,30],[30,30],[60,40]] Output: 2 Explanation: We start with 10 liters of fuel. We drive to position 10, expending 10 liters of fuel. We refuel from 0 liters to 60 liters of gas. Then, we drive from position 10 to position 60 (expending 50 liters of fuel), and refuel from 10 liters to 50 liters of gas. We then drive to and reach the target. We made 2 refueling stops along the way, so we return 2.   Note: 1 <= target, startFuel, stations[i][1] <= 10^9 0 <= stations.length <= 500 0 < stations[0][0] < stations[1][0] < ... < stations[stations.length-1][0] < target
class Solution: def minRefuelStops( self, target: int, startFuel: int, stations: List[List[int]] ) -> int: heap = [] queue = deque() i = 0 num_stations = 0 while startFuel < target: while i < len(stations) and startFuel >= stations[i][0]: heapq.heappush(heap, -stations[i][1]) i += 1 if not heap: return -1 next_fuel = heapq.heappop(heap) startFuel -= next_fuel num_stations += 1 return num_stations
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR
A car travels from a starting position to a destination which is target miles east of the starting position. Along the way, there are gas stations.  Each station[i] represents a gas station that is station[i][0] miles east of the starting position, and has station[i][1] liters of gas. The car starts with an infinite tank of gas, which initially has startFuel liters of fuel in it.  It uses 1 liter of gas per 1 mile that it drives. When the car reaches a gas station, it may stop and refuel, transferring all the gas from the station into the car. What is the least number of refueling stops the car must make in order to reach its destination?  If it cannot reach the destination, return -1. Note that if the car reaches a gas station with 0 fuel left, the car can still refuel there.  If the car reaches the destination with 0 fuel left, it is still considered to have arrived.   Example 1: Input: target = 1, startFuel = 1, stations = [] Output: 0 Explanation: We can reach the target without refueling. Example 2: Input: target = 100, startFuel = 1, stations = [[10,100]] Output: -1 Explanation: We can't reach the target (or even the first gas station). Example 3: Input: target = 100, startFuel = 10, stations = [[10,60],[20,30],[30,30],[60,40]] Output: 2 Explanation: We start with 10 liters of fuel. We drive to position 10, expending 10 liters of fuel. We refuel from 0 liters to 60 liters of gas. Then, we drive from position 10 to position 60 (expending 50 liters of fuel), and refuel from 10 liters to 50 liters of gas. We then drive to and reach the target. We made 2 refueling stops along the way, so we return 2.   Note: 1 <= target, startFuel, stations[i][1] <= 10^9 0 <= stations.length <= 500 0 < stations[0][0] < stations[1][0] < ... < stations[stations.length-1][0] < target
class Solution: def minRefuelStops( self, target: int, startFuel: int, stations: List[List[int]] ) -> int: fuel = startFuel del startFuel i = 0 while target > fuel: target -= fuel stations = [[dist - fuel, gas] for dist, gas in stations] try: j, fuel = max( [[j, gas] for j, (dist, gas) in enumerate(stations) if dist <= 0], key=lambda stop: stop[1], ) except ValueError: return -1 if fuel == 0: return -1 stations[j][1] = 0 i += 1 return i
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR ASSIGN VAR LIST BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR LIST VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER NUMBER VAR NUMBER RETURN VAR VAR
A car travels from a starting position to a destination which is target miles east of the starting position. Along the way, there are gas stations.  Each station[i] represents a gas station that is station[i][0] miles east of the starting position, and has station[i][1] liters of gas. The car starts with an infinite tank of gas, which initially has startFuel liters of fuel in it.  It uses 1 liter of gas per 1 mile that it drives. When the car reaches a gas station, it may stop and refuel, transferring all the gas from the station into the car. What is the least number of refueling stops the car must make in order to reach its destination?  If it cannot reach the destination, return -1. Note that if the car reaches a gas station with 0 fuel left, the car can still refuel there.  If the car reaches the destination with 0 fuel left, it is still considered to have arrived.   Example 1: Input: target = 1, startFuel = 1, stations = [] Output: 0 Explanation: We can reach the target without refueling. Example 2: Input: target = 100, startFuel = 1, stations = [[10,100]] Output: -1 Explanation: We can't reach the target (or even the first gas station). Example 3: Input: target = 100, startFuel = 10, stations = [[10,60],[20,30],[30,30],[60,40]] Output: 2 Explanation: We start with 10 liters of fuel. We drive to position 10, expending 10 liters of fuel. We refuel from 0 liters to 60 liters of gas. Then, we drive from position 10 to position 60 (expending 50 liters of fuel), and refuel from 10 liters to 50 liters of gas. We then drive to and reach the target. We made 2 refueling stops along the way, so we return 2.   Note: 1 <= target, startFuel, stations[i][1] <= 10^9 0 <= stations.length <= 500 0 < stations[0][0] < stations[1][0] < ... < stations[stations.length-1][0] < target
class Solution: def minRefuelStops( self, target: int, startFuel: int, stations: List[List[int]] ) -> int: if startFuel >= target: return 0 heap = [] for d, g in stations: heapq.heappush(heap, (-g, d)) dist = startFuel stop = 0 dist = startFuel tmp = [] while heap: g, d = heapq.heappop(heap) if d <= dist: dist += -g if dist >= target: return stop + 1 for el in tmp: heapq.heappush(heap, el) tmp = [] stop += 1 else: tmp.append((g, d)) return -1
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR LIST FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR RETURN BIN_OP VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN NUMBER VAR
A car travels from a starting position to a destination which is target miles east of the starting position. Along the way, there are gas stations.  Each station[i] represents a gas station that is station[i][0] miles east of the starting position, and has station[i][1] liters of gas. The car starts with an infinite tank of gas, which initially has startFuel liters of fuel in it.  It uses 1 liter of gas per 1 mile that it drives. When the car reaches a gas station, it may stop and refuel, transferring all the gas from the station into the car. What is the least number of refueling stops the car must make in order to reach its destination?  If it cannot reach the destination, return -1. Note that if the car reaches a gas station with 0 fuel left, the car can still refuel there.  If the car reaches the destination with 0 fuel left, it is still considered to have arrived.   Example 1: Input: target = 1, startFuel = 1, stations = [] Output: 0 Explanation: We can reach the target without refueling. Example 2: Input: target = 100, startFuel = 1, stations = [[10,100]] Output: -1 Explanation: We can't reach the target (or even the first gas station). Example 3: Input: target = 100, startFuel = 10, stations = [[10,60],[20,30],[30,30],[60,40]] Output: 2 Explanation: We start with 10 liters of fuel. We drive to position 10, expending 10 liters of fuel. We refuel from 0 liters to 60 liters of gas. Then, we drive from position 10 to position 60 (expending 50 liters of fuel), and refuel from 10 liters to 50 liters of gas. We then drive to and reach the target. We made 2 refueling stops along the way, so we return 2.   Note: 1 <= target, startFuel, stations[i][1] <= 10^9 0 <= stations.length <= 500 0 < stations[0][0] < stations[1][0] < ... < stations[stations.length-1][0] < target
class Solution: def minRefuelStops( self, target: int, startFuel: int, stations: List[List[int]] ) -> int: dp = [[startFuel, []]] for i in range(len(stations)): if dp[-1][0] >= target: break usedStations = dp[-1][1] usable_stations = [ s for s in stations if s[0] <= dp[-1][0] and s[0] not in usedStations ] usable_stations.sort(key=lambda x: x[1]) if len(usable_stations) == 0: break dp.append([dp[-1][0] + usable_stations[-1][1], dp[-1][1].copy()]) dp[-1][1].append(usable_stations[-1][0]) return len(dp) - 1 if dp[-1][0] >= target else -1
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR LIST LIST VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER NUMBER VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER NUMBER VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER NUMBER VAR NUMBER NUMBER FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER VAR NUMBER NUMBER RETURN VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR
A car travels from a starting position to a destination which is target miles east of the starting position. Along the way, there are gas stations.  Each station[i] represents a gas station that is station[i][0] miles east of the starting position, and has station[i][1] liters of gas. The car starts with an infinite tank of gas, which initially has startFuel liters of fuel in it.  It uses 1 liter of gas per 1 mile that it drives. When the car reaches a gas station, it may stop and refuel, transferring all the gas from the station into the car. What is the least number of refueling stops the car must make in order to reach its destination?  If it cannot reach the destination, return -1. Note that if the car reaches a gas station with 0 fuel left, the car can still refuel there.  If the car reaches the destination with 0 fuel left, it is still considered to have arrived.   Example 1: Input: target = 1, startFuel = 1, stations = [] Output: 0 Explanation: We can reach the target without refueling. Example 2: Input: target = 100, startFuel = 1, stations = [[10,100]] Output: -1 Explanation: We can't reach the target (or even the first gas station). Example 3: Input: target = 100, startFuel = 10, stations = [[10,60],[20,30],[30,30],[60,40]] Output: 2 Explanation: We start with 10 liters of fuel. We drive to position 10, expending 10 liters of fuel. We refuel from 0 liters to 60 liters of gas. Then, we drive from position 10 to position 60 (expending 50 liters of fuel), and refuel from 10 liters to 50 liters of gas. We then drive to and reach the target. We made 2 refueling stops along the way, so we return 2.   Note: 1 <= target, startFuel, stations[i][1] <= 10^9 0 <= stations.length <= 500 0 < stations[0][0] < stations[1][0] < ... < stations[stations.length-1][0] < target
class Solution: def minRefuelStops( self, target: int, startFuel: int, stations: List[List[int]] ) -> int: stations = [[0, startFuel]] + sorted(stations) n = len(stations) reach = [-float("inf")] * n reach[0] = startFuel st = [startFuel] * n flag = True cnt = 0 if target <= startFuel: return 0 while flag: flag = False cnt += 1 for i in range(1, len(reach))[::-1]: p, g = stations[i] st.pop() if p <= st[-1] and st[-1] + g > reach[i]: reach[i] = st[-1] + g flag = True if reach[i] >= target: return cnt for i in range(1, len(reach)): st.append(max(st[-1], reach[i])) return -1
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST LIST NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING VAR ASSIGN VAR NUMBER VAR ASSIGN VAR BIN_OP LIST VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR RETURN NUMBER WHILE VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR IF VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR RETURN NUMBER VAR
A car travels from a starting position to a destination which is target miles east of the starting position. Along the way, there are gas stations.  Each station[i] represents a gas station that is station[i][0] miles east of the starting position, and has station[i][1] liters of gas. The car starts with an infinite tank of gas, which initially has startFuel liters of fuel in it.  It uses 1 liter of gas per 1 mile that it drives. When the car reaches a gas station, it may stop and refuel, transferring all the gas from the station into the car. What is the least number of refueling stops the car must make in order to reach its destination?  If it cannot reach the destination, return -1. Note that if the car reaches a gas station with 0 fuel left, the car can still refuel there.  If the car reaches the destination with 0 fuel left, it is still considered to have arrived.   Example 1: Input: target = 1, startFuel = 1, stations = [] Output: 0 Explanation: We can reach the target without refueling. Example 2: Input: target = 100, startFuel = 1, stations = [[10,100]] Output: -1 Explanation: We can't reach the target (or even the first gas station). Example 3: Input: target = 100, startFuel = 10, stations = [[10,60],[20,30],[30,30],[60,40]] Output: 2 Explanation: We start with 10 liters of fuel. We drive to position 10, expending 10 liters of fuel. We refuel from 0 liters to 60 liters of gas. Then, we drive from position 10 to position 60 (expending 50 liters of fuel), and refuel from 10 liters to 50 liters of gas. We then drive to and reach the target. We made 2 refueling stops along the way, so we return 2.   Note: 1 <= target, startFuel, stations[i][1] <= 10^9 0 <= stations.length <= 500 0 < stations[0][0] < stations[1][0] < ... < stations[stations.length-1][0] < target
class Solution: def minRefuelStops( self, target: int, startFuel: int, stations: List[List[int]] ) -> int: if startFuel >= target: return 0 if not stations: return -1 data = [] for k in range(len(stations)): data.append( [stations[k][0] - (stations[k - 1][0] if k > 0 else 0), stations[k][1]] ) data.append([target - stations[-1][0], 0]) tot = startFuel for dist, gas in data: tot -= dist if tot < 0: return -1 tot += gas N = len(data) maxFuel = [float("-inf")] * N tot = startFuel for k in range(N): dist, _ = data[k] tot -= dist if tot < 0: break maxFuel[k] = tot nextMaxFuel = [float("-inf")] * N nextMaxFuel[0] = maxFuel[0] + data[0][1] for k in range(1, N): dist, gas = data[k] if maxFuel[k] != float("-inf"): nextMaxFuel[k] = maxFuel[k] + gas if nextMaxFuel[k - 1] >= dist: nextMaxFuel[k] = max(nextMaxFuel[k], nextMaxFuel[k - 1] - dist) if nextMaxFuel[k] == float("-inf"): break if nextMaxFuel[-1] >= 0: return 1 maxFuel = nextMaxFuel for nStops in range(2, N + 1): nextMaxFuel = [float("-inf")] * N for k in range(nStops - 1, N): dist, gas = data[k] if maxFuel[k - 1] != float("-inf") and maxFuel[k - 1] >= dist: nextMaxFuel[k] = maxFuel[k - 1] - dist + gas if nextMaxFuel[k - 1] >= dist: nextMaxFuel[k] = max(nextMaxFuel[k], nextMaxFuel[k - 1] - dist) if nextMaxFuel[k] == float("-inf"): break if nextMaxFuel[-1] >= 0: return nStops maxFuel = nextMaxFuel return -1
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR IF VAR VAR RETURN NUMBER IF VAR RETURN NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR NUMBER VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FOR VAR VAR VAR VAR VAR IF VAR NUMBER RETURN NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR IF VAR VAR FUNC_CALL VAR STRING ASSIGN VAR VAR BIN_OP VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR IF VAR VAR FUNC_CALL VAR STRING IF VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER FUNC_CALL VAR STRING VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR IF VAR VAR FUNC_CALL VAR STRING IF VAR NUMBER NUMBER RETURN VAR ASSIGN VAR VAR RETURN NUMBER VAR
A car travels from a starting position to a destination which is target miles east of the starting position. Along the way, there are gas stations.  Each station[i] represents a gas station that is station[i][0] miles east of the starting position, and has station[i][1] liters of gas. The car starts with an infinite tank of gas, which initially has startFuel liters of fuel in it.  It uses 1 liter of gas per 1 mile that it drives. When the car reaches a gas station, it may stop and refuel, transferring all the gas from the station into the car. What is the least number of refueling stops the car must make in order to reach its destination?  If it cannot reach the destination, return -1. Note that if the car reaches a gas station with 0 fuel left, the car can still refuel there.  If the car reaches the destination with 0 fuel left, it is still considered to have arrived.   Example 1: Input: target = 1, startFuel = 1, stations = [] Output: 0 Explanation: We can reach the target without refueling. Example 2: Input: target = 100, startFuel = 1, stations = [[10,100]] Output: -1 Explanation: We can't reach the target (or even the first gas station). Example 3: Input: target = 100, startFuel = 10, stations = [[10,60],[20,30],[30,30],[60,40]] Output: 2 Explanation: We start with 10 liters of fuel. We drive to position 10, expending 10 liters of fuel. We refuel from 0 liters to 60 liters of gas. Then, we drive from position 10 to position 60 (expending 50 liters of fuel), and refuel from 10 liters to 50 liters of gas. We then drive to and reach the target. We made 2 refueling stops along the way, so we return 2.   Note: 1 <= target, startFuel, stations[i][1] <= 10^9 0 <= stations.length <= 500 0 < stations[0][0] < stations[1][0] < ... < stations[stations.length-1][0] < target
class Solution: def minRefuelStops( self, target: int, startFuel: int, stations: List[List[int]] ) -> int: if startFuel >= target: return 0 pq = [] minStops = i = 0 curr = startFuel while curr < target: while i < len(stations) and curr >= stations[i][0]: heapq.heappush(pq, -stations[i][1]) i += 1 if not pq: return -1 curr += -heapq.heappop(pq) minStops += 1 return minStops
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR LIST ASSIGN VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR RETURN NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR
A car travels from a starting position to a destination which is target miles east of the starting position. Along the way, there are gas stations.  Each station[i] represents a gas station that is station[i][0] miles east of the starting position, and has station[i][1] liters of gas. The car starts with an infinite tank of gas, which initially has startFuel liters of fuel in it.  It uses 1 liter of gas per 1 mile that it drives. When the car reaches a gas station, it may stop and refuel, transferring all the gas from the station into the car. What is the least number of refueling stops the car must make in order to reach its destination?  If it cannot reach the destination, return -1. Note that if the car reaches a gas station with 0 fuel left, the car can still refuel there.  If the car reaches the destination with 0 fuel left, it is still considered to have arrived.   Example 1: Input: target = 1, startFuel = 1, stations = [] Output: 0 Explanation: We can reach the target without refueling. Example 2: Input: target = 100, startFuel = 1, stations = [[10,100]] Output: -1 Explanation: We can't reach the target (or even the first gas station). Example 3: Input: target = 100, startFuel = 10, stations = [[10,60],[20,30],[30,30],[60,40]] Output: 2 Explanation: We start with 10 liters of fuel. We drive to position 10, expending 10 liters of fuel. We refuel from 0 liters to 60 liters of gas. Then, we drive from position 10 to position 60 (expending 50 liters of fuel), and refuel from 10 liters to 50 liters of gas. We then drive to and reach the target. We made 2 refueling stops along the way, so we return 2.   Note: 1 <= target, startFuel, stations[i][1] <= 10^9 0 <= stations.length <= 500 0 < stations[0][0] < stations[1][0] < ... < stations[stations.length-1][0] < target
class Solution: def minRefuelStops( self, target: int, startFuel: int, stations: List[List[int]] ) -> int: if target <= startFuel: return 0 stations = [[0, startFuel]] + stations print(stations) N = len(stations) f = [(startFuel - stations[i][0]) for i in range(N)] f[0] = startFuel print(f) for j in range(N): g = [-1] * N g[0] = f[0] for i in range(N - 1, 0, -1): if f[i - 1] - (stations[i][0] - stations[i - 1][0]) >= 0: g[i] = ( f[i - 1] + stations[i][1] - (stations[i][0] - stations[i - 1][0]) ) for i in range(1, N): g[i] = max(g[i - 1] - (stations[i][0] - stations[i - 1][0]), g[i]) f = g if f[-1] >= target - stations[-1][0]: return j + 1 return -1
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP LIST LIST NUMBER VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR IF VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER RETURN BIN_OP VAR NUMBER RETURN NUMBER VAR
A car travels from a starting position to a destination which is target miles east of the starting position. Along the way, there are gas stations.  Each station[i] represents a gas station that is station[i][0] miles east of the starting position, and has station[i][1] liters of gas. The car starts with an infinite tank of gas, which initially has startFuel liters of fuel in it.  It uses 1 liter of gas per 1 mile that it drives. When the car reaches a gas station, it may stop and refuel, transferring all the gas from the station into the car. What is the least number of refueling stops the car must make in order to reach its destination?  If it cannot reach the destination, return -1. Note that if the car reaches a gas station with 0 fuel left, the car can still refuel there.  If the car reaches the destination with 0 fuel left, it is still considered to have arrived.   Example 1: Input: target = 1, startFuel = 1, stations = [] Output: 0 Explanation: We can reach the target without refueling. Example 2: Input: target = 100, startFuel = 1, stations = [[10,100]] Output: -1 Explanation: We can't reach the target (or even the first gas station). Example 3: Input: target = 100, startFuel = 10, stations = [[10,60],[20,30],[30,30],[60,40]] Output: 2 Explanation: We start with 10 liters of fuel. We drive to position 10, expending 10 liters of fuel. We refuel from 0 liters to 60 liters of gas. Then, we drive from position 10 to position 60 (expending 50 liters of fuel), and refuel from 10 liters to 50 liters of gas. We then drive to and reach the target. We made 2 refueling stops along the way, so we return 2.   Note: 1 <= target, startFuel, stations[i][1] <= 10^9 0 <= stations.length <= 500 0 < stations[0][0] < stations[1][0] < ... < stations[stations.length-1][0] < target
class Solution: def minRefuelStops( self, target: int, startFuel: int, stations: List[List[int]] ) -> int: if startFuel >= target: return 0 length = len(stations) dp = [startFuel] + [0] * length result = float("inf") for i in range(1 + length): for j in range(i, 0, -1): if dp[j - 1] >= stations[i - 1][0]: dp[j] = max(dp[j], dp[j - 1] + stations[i - 1][1]) if dp[j] >= target: result = min(result, j) return -1 if result == float("inf") else result
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_CALL VAR STRING NUMBER VAR VAR
A car travels from a starting position to a destination which is target miles east of the starting position. Along the way, there are gas stations.  Each station[i] represents a gas station that is station[i][0] miles east of the starting position, and has station[i][1] liters of gas. The car starts with an infinite tank of gas, which initially has startFuel liters of fuel in it.  It uses 1 liter of gas per 1 mile that it drives. When the car reaches a gas station, it may stop and refuel, transferring all the gas from the station into the car. What is the least number of refueling stops the car must make in order to reach its destination?  If it cannot reach the destination, return -1. Note that if the car reaches a gas station with 0 fuel left, the car can still refuel there.  If the car reaches the destination with 0 fuel left, it is still considered to have arrived.   Example 1: Input: target = 1, startFuel = 1, stations = [] Output: 0 Explanation: We can reach the target without refueling. Example 2: Input: target = 100, startFuel = 1, stations = [[10,100]] Output: -1 Explanation: We can't reach the target (or even the first gas station). Example 3: Input: target = 100, startFuel = 10, stations = [[10,60],[20,30],[30,30],[60,40]] Output: 2 Explanation: We start with 10 liters of fuel. We drive to position 10, expending 10 liters of fuel. We refuel from 0 liters to 60 liters of gas. Then, we drive from position 10 to position 60 (expending 50 liters of fuel), and refuel from 10 liters to 50 liters of gas. We then drive to and reach the target. We made 2 refueling stops along the way, so we return 2.   Note: 1 <= target, startFuel, stations[i][1] <= 10^9 0 <= stations.length <= 500 0 < stations[0][0] < stations[1][0] < ... < stations[stations.length-1][0] < target
class Solution: def minRefuelStops( self, target: int, startFuel: int, stations: List[List[int]] ) -> int: if startFuel >= target: return 0 n = len(stations) dp = [0] * (n + 1) dp[0] = startFuel for i in range(1, n + 1): for j in range(i, 0, -1): if dp[j - 1] >= stations[i - 1][0]: dp[j] = max(dp[j], dp[j - 1] + stations[i - 1][1]) for i in range(1, n + 1): if dp[i] >= target: return i return -1
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR RETURN VAR RETURN NUMBER VAR
A car travels from a starting position to a destination which is target miles east of the starting position. Along the way, there are gas stations.  Each station[i] represents a gas station that is station[i][0] miles east of the starting position, and has station[i][1] liters of gas. The car starts with an infinite tank of gas, which initially has startFuel liters of fuel in it.  It uses 1 liter of gas per 1 mile that it drives. When the car reaches a gas station, it may stop and refuel, transferring all the gas from the station into the car. What is the least number of refueling stops the car must make in order to reach its destination?  If it cannot reach the destination, return -1. Note that if the car reaches a gas station with 0 fuel left, the car can still refuel there.  If the car reaches the destination with 0 fuel left, it is still considered to have arrived.   Example 1: Input: target = 1, startFuel = 1, stations = [] Output: 0 Explanation: We can reach the target without refueling. Example 2: Input: target = 100, startFuel = 1, stations = [[10,100]] Output: -1 Explanation: We can't reach the target (or even the first gas station). Example 3: Input: target = 100, startFuel = 10, stations = [[10,60],[20,30],[30,30],[60,40]] Output: 2 Explanation: We start with 10 liters of fuel. We drive to position 10, expending 10 liters of fuel. We refuel from 0 liters to 60 liters of gas. Then, we drive from position 10 to position 60 (expending 50 liters of fuel), and refuel from 10 liters to 50 liters of gas. We then drive to and reach the target. We made 2 refueling stops along the way, so we return 2.   Note: 1 <= target, startFuel, stations[i][1] <= 10^9 0 <= stations.length <= 500 0 < stations[0][0] < stations[1][0] < ... < stations[stations.length-1][0] < target
class Solution: def minRefuelStops( self, target: int, startFuel: int, stations: List[List[int]] ) -> int: if startFuel >= target: return 0 heap = [] stop = 0 dist = startFuel for d, g in stations: if dist >= target: return stop while heap and dist < d: gas = heapq.heappop(heap) dist += -gas stop += 1 if dist < d: return -1 heapq.heappush(heap, -g) if dist >= target: return stop while heap: g = heapq.heappop(heap) stop += 1 dist += -g if dist >= target: return stop return -1
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR VAR VAR IF VAR VAR RETURN VAR WHILE VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR RETURN VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR IF VAR VAR RETURN VAR RETURN NUMBER VAR
A car travels from a starting position to a destination which is target miles east of the starting position. Along the way, there are gas stations.  Each station[i] represents a gas station that is station[i][0] miles east of the starting position, and has station[i][1] liters of gas. The car starts with an infinite tank of gas, which initially has startFuel liters of fuel in it.  It uses 1 liter of gas per 1 mile that it drives. When the car reaches a gas station, it may stop and refuel, transferring all the gas from the station into the car. What is the least number of refueling stops the car must make in order to reach its destination?  If it cannot reach the destination, return -1. Note that if the car reaches a gas station with 0 fuel left, the car can still refuel there.  If the car reaches the destination with 0 fuel left, it is still considered to have arrived.   Example 1: Input: target = 1, startFuel = 1, stations = [] Output: 0 Explanation: We can reach the target without refueling. Example 2: Input: target = 100, startFuel = 1, stations = [[10,100]] Output: -1 Explanation: We can't reach the target (or even the first gas station). Example 3: Input: target = 100, startFuel = 10, stations = [[10,60],[20,30],[30,30],[60,40]] Output: 2 Explanation: We start with 10 liters of fuel. We drive to position 10, expending 10 liters of fuel. We refuel from 0 liters to 60 liters of gas. Then, we drive from position 10 to position 60 (expending 50 liters of fuel), and refuel from 10 liters to 50 liters of gas. We then drive to and reach the target. We made 2 refueling stops along the way, so we return 2.   Note: 1 <= target, startFuel, stations[i][1] <= 10^9 0 <= stations.length <= 500 0 < stations[0][0] < stations[1][0] < ... < stations[stations.length-1][0] < target
class Solution: def minRefuelStops(self, t: int, f: int, st: List[List[int]]) -> int: if f >= t: return 0 if st == []: return -1 if f < st[0][0]: return -1 li, li1 = [], [] for i, j in st: li.append(i) li1.append(j) count = 0 while f < t: s, tur = 0, [] for i in li: if f >= i: s = li.index(i) tur.append(True) else: tur.append(False) print(tur) if all(i == False for i in tur): return -1 f += max(li1[0 : s + 1]) li.pop(li1.index(max(li1[0 : s + 1]))) li1.remove(max(li1[0 : s + 1])) count += 1 return count
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR IF VAR VAR RETURN NUMBER IF VAR LIST RETURN NUMBER IF VAR VAR NUMBER NUMBER RETURN NUMBER ASSIGN VAR VAR LIST LIST FOR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR NUMBER LIST FOR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR VAR RETURN NUMBER VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER RETURN VAR VAR
A car travels from a starting position to a destination which is target miles east of the starting position. Along the way, there are gas stations.  Each station[i] represents a gas station that is station[i][0] miles east of the starting position, and has station[i][1] liters of gas. The car starts with an infinite tank of gas, which initially has startFuel liters of fuel in it.  It uses 1 liter of gas per 1 mile that it drives. When the car reaches a gas station, it may stop and refuel, transferring all the gas from the station into the car. What is the least number of refueling stops the car must make in order to reach its destination?  If it cannot reach the destination, return -1. Note that if the car reaches a gas station with 0 fuel left, the car can still refuel there.  If the car reaches the destination with 0 fuel left, it is still considered to have arrived.   Example 1: Input: target = 1, startFuel = 1, stations = [] Output: 0 Explanation: We can reach the target without refueling. Example 2: Input: target = 100, startFuel = 1, stations = [[10,100]] Output: -1 Explanation: We can't reach the target (or even the first gas station). Example 3: Input: target = 100, startFuel = 10, stations = [[10,60],[20,30],[30,30],[60,40]] Output: 2 Explanation: We start with 10 liters of fuel. We drive to position 10, expending 10 liters of fuel. We refuel from 0 liters to 60 liters of gas. Then, we drive from position 10 to position 60 (expending 50 liters of fuel), and refuel from 10 liters to 50 liters of gas. We then drive to and reach the target. We made 2 refueling stops along the way, so we return 2.   Note: 1 <= target, startFuel, stations[i][1] <= 10^9 0 <= stations.length <= 500 0 < stations[0][0] < stations[1][0] < ... < stations[stations.length-1][0] < target
class Solution: def minRefuelStops( self, target: int, startFuel: int, stations: List[List[int]] ) -> int: n = len(stations) dp = [([0] * (n + 1)) for _ in range(n + 1)] for row in dp: row[0] = startFuel if startFuel >= target: return 0 for refuels in range(1, n + 1): for stas in range(refuels, n + 1): station = stations[stas - 1] dp[stas][refuels] = dp[stas - 1][refuels] if dp[stas - 1][refuels - 1] >= station[0]: dp[stas][refuels] = max( station[1] + dp[stas - 1][refuels - 1], dp[stas][refuels] ) if dp[n][refuels] >= target: return refuels return -1
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER VAR IF VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR VAR VAR RETURN VAR RETURN NUMBER VAR
A car travels from a starting position to a destination which is target miles east of the starting position. Along the way, there are gas stations.  Each station[i] represents a gas station that is station[i][0] miles east of the starting position, and has station[i][1] liters of gas. The car starts with an infinite tank of gas, which initially has startFuel liters of fuel in it.  It uses 1 liter of gas per 1 mile that it drives. When the car reaches a gas station, it may stop and refuel, transferring all the gas from the station into the car. What is the least number of refueling stops the car must make in order to reach its destination?  If it cannot reach the destination, return -1. Note that if the car reaches a gas station with 0 fuel left, the car can still refuel there.  If the car reaches the destination with 0 fuel left, it is still considered to have arrived.   Example 1: Input: target = 1, startFuel = 1, stations = [] Output: 0 Explanation: We can reach the target without refueling. Example 2: Input: target = 100, startFuel = 1, stations = [[10,100]] Output: -1 Explanation: We can't reach the target (or even the first gas station). Example 3: Input: target = 100, startFuel = 10, stations = [[10,60],[20,30],[30,30],[60,40]] Output: 2 Explanation: We start with 10 liters of fuel. We drive to position 10, expending 10 liters of fuel. We refuel from 0 liters to 60 liters of gas. Then, we drive from position 10 to position 60 (expending 50 liters of fuel), and refuel from 10 liters to 50 liters of gas. We then drive to and reach the target. We made 2 refueling stops along the way, so we return 2.   Note: 1 <= target, startFuel, stations[i][1] <= 10^9 0 <= stations.length <= 500 0 < stations[0][0] < stations[1][0] < ... < stations[stations.length-1][0] < target
class Solution: def minRefuelStops( self, target: int, startFuel: int, stations: List[List[int]] ) -> int: if startFuel >= target: return 0 if startFuel < target and not stations: return -1 dp = [startFuel] + [0] * len(stations) for i, val in enumerate(stations): for t in range(i, -1, -1): if dp[t] >= val[0]: dp[t + 1] = max(dp[t + 1], dp[t] + val[1]) for i, d in enumerate(dp): if d >= target: return i return -1
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP LIST VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR RETURN NUMBER VAR
A car travels from a starting position to a destination which is target miles east of the starting position. Along the way, there are gas stations.  Each station[i] represents a gas station that is station[i][0] miles east of the starting position, and has station[i][1] liters of gas. The car starts with an infinite tank of gas, which initially has startFuel liters of fuel in it.  It uses 1 liter of gas per 1 mile that it drives. When the car reaches a gas station, it may stop and refuel, transferring all the gas from the station into the car. What is the least number of refueling stops the car must make in order to reach its destination?  If it cannot reach the destination, return -1. Note that if the car reaches a gas station with 0 fuel left, the car can still refuel there.  If the car reaches the destination with 0 fuel left, it is still considered to have arrived.   Example 1: Input: target = 1, startFuel = 1, stations = [] Output: 0 Explanation: We can reach the target without refueling. Example 2: Input: target = 100, startFuel = 1, stations = [[10,100]] Output: -1 Explanation: We can't reach the target (or even the first gas station). Example 3: Input: target = 100, startFuel = 10, stations = [[10,60],[20,30],[30,30],[60,40]] Output: 2 Explanation: We start with 10 liters of fuel. We drive to position 10, expending 10 liters of fuel. We refuel from 0 liters to 60 liters of gas. Then, we drive from position 10 to position 60 (expending 50 liters of fuel), and refuel from 10 liters to 50 liters of gas. We then drive to and reach the target. We made 2 refueling stops along the way, so we return 2.   Note: 1 <= target, startFuel, stations[i][1] <= 10^9 0 <= stations.length <= 500 0 < stations[0][0] < stations[1][0] < ... < stations[stations.length-1][0] < target
class Solution: def minRefuelStops( self, target: int, startFuel: int, stations: List[List[int]] ) -> int: if startFuel >= target: return 0 if not stations: return -1 fdelta = startFuel - stations[0][0] if fdelta < 0: return -1 curr = [fdelta + stations[0][1], fdelta] for i, sta in enumerate(stations[1:]): treck = sta[0] - stations[i][0] leftover = curr[0] - treck if leftover < 0: return -1 nex = [leftover + sta[1], leftover] for j, f in enumerate(curr[1:]): leftover = curr[j + 1] - treck if leftover < 0: break else: nex[-1] = max(nex[-1], leftover + sta[1]) nex.append(leftover) curr = nex leftover = target - stations[-1][0] while curr and curr[-1] < leftover: curr.pop() return len(stations) - len(curr) + 1 if curr else -1
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR IF VAR VAR RETURN NUMBER IF VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR LIST BIN_OP VAR VAR NUMBER NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR LIST BIN_OP VAR VAR NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR NUMBER FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER WHILE VAR VAR NUMBER VAR EXPR FUNC_CALL VAR RETURN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR
A car travels from a starting position to a destination which is target miles east of the starting position. Along the way, there are gas stations.  Each station[i] represents a gas station that is station[i][0] miles east of the starting position, and has station[i][1] liters of gas. The car starts with an infinite tank of gas, which initially has startFuel liters of fuel in it.  It uses 1 liter of gas per 1 mile that it drives. When the car reaches a gas station, it may stop and refuel, transferring all the gas from the station into the car. What is the least number of refueling stops the car must make in order to reach its destination?  If it cannot reach the destination, return -1. Note that if the car reaches a gas station with 0 fuel left, the car can still refuel there.  If the car reaches the destination with 0 fuel left, it is still considered to have arrived.   Example 1: Input: target = 1, startFuel = 1, stations = [] Output: 0 Explanation: We can reach the target without refueling. Example 2: Input: target = 100, startFuel = 1, stations = [[10,100]] Output: -1 Explanation: We can't reach the target (or even the first gas station). Example 3: Input: target = 100, startFuel = 10, stations = [[10,60],[20,30],[30,30],[60,40]] Output: 2 Explanation: We start with 10 liters of fuel. We drive to position 10, expending 10 liters of fuel. We refuel from 0 liters to 60 liters of gas. Then, we drive from position 10 to position 60 (expending 50 liters of fuel), and refuel from 10 liters to 50 liters of gas. We then drive to and reach the target. We made 2 refueling stops along the way, so we return 2.   Note: 1 <= target, startFuel, stations[i][1] <= 10^9 0 <= stations.length <= 500 0 < stations[0][0] < stations[1][0] < ... < stations[stations.length-1][0] < target
class Solution: def minRefuelStops( self, target: int, startFuel: int, stations: List[List[int]] ) -> int: if target == startFuel: return 0 stations = [startFuel] + stations dp = [0] * len(stations) dp[0] = startFuel for i in range(1, len(stations)): j = i - 1 while j >= 0: totalFuel = dp[j] + stations[i][1] if dp[j] >= stations[i][0] and totalFuel >= dp[j + 1]: dp[j + 1] = totalFuel j -= 1 for i, fuel in enumerate(dp): if fuel >= target: return i return -1
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP LIST VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR RETURN NUMBER VAR
A car travels from a starting position to a destination which is target miles east of the starting position. Along the way, there are gas stations.  Each station[i] represents a gas station that is station[i][0] miles east of the starting position, and has station[i][1] liters of gas. The car starts with an infinite tank of gas, which initially has startFuel liters of fuel in it.  It uses 1 liter of gas per 1 mile that it drives. When the car reaches a gas station, it may stop and refuel, transferring all the gas from the station into the car. What is the least number of refueling stops the car must make in order to reach its destination?  If it cannot reach the destination, return -1. Note that if the car reaches a gas station with 0 fuel left, the car can still refuel there.  If the car reaches the destination with 0 fuel left, it is still considered to have arrived.   Example 1: Input: target = 1, startFuel = 1, stations = [] Output: 0 Explanation: We can reach the target without refueling. Example 2: Input: target = 100, startFuel = 1, stations = [[10,100]] Output: -1 Explanation: We can't reach the target (or even the first gas station). Example 3: Input: target = 100, startFuel = 10, stations = [[10,60],[20,30],[30,30],[60,40]] Output: 2 Explanation: We start with 10 liters of fuel. We drive to position 10, expending 10 liters of fuel. We refuel from 0 liters to 60 liters of gas. Then, we drive from position 10 to position 60 (expending 50 liters of fuel), and refuel from 10 liters to 50 liters of gas. We then drive to and reach the target. We made 2 refueling stops along the way, so we return 2.   Note: 1 <= target, startFuel, stations[i][1] <= 10^9 0 <= stations.length <= 500 0 < stations[0][0] < stations[1][0] < ... < stations[stations.length-1][0] < target
class Solution: def minRefuelStops( self, target: int, startFuel: int, stations: List[List[int]] ) -> int: q = [] extent = startFuel ans = 0 for i in range(len(stations)): if extent >= target: return ans loc, fuel = stations[i] while q and loc > extent: extent += -heapq.heappop(q) ans += 1 if loc > extent: return -1 heapq.heappush(q, -fuel) while q and extent < target: extent += -heapq.heappop(q) ans += 1 if extent >= target: return ans else: return -1
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR RETURN VAR ASSIGN VAR VAR VAR VAR WHILE VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR WHILE VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR RETURN VAR RETURN NUMBER VAR
A car travels from a starting position to a destination which is target miles east of the starting position. Along the way, there are gas stations.  Each station[i] represents a gas station that is station[i][0] miles east of the starting position, and has station[i][1] liters of gas. The car starts with an infinite tank of gas, which initially has startFuel liters of fuel in it.  It uses 1 liter of gas per 1 mile that it drives. When the car reaches a gas station, it may stop and refuel, transferring all the gas from the station into the car. What is the least number of refueling stops the car must make in order to reach its destination?  If it cannot reach the destination, return -1. Note that if the car reaches a gas station with 0 fuel left, the car can still refuel there.  If the car reaches the destination with 0 fuel left, it is still considered to have arrived.   Example 1: Input: target = 1, startFuel = 1, stations = [] Output: 0 Explanation: We can reach the target without refueling. Example 2: Input: target = 100, startFuel = 1, stations = [[10,100]] Output: -1 Explanation: We can't reach the target (or even the first gas station). Example 3: Input: target = 100, startFuel = 10, stations = [[10,60],[20,30],[30,30],[60,40]] Output: 2 Explanation: We start with 10 liters of fuel. We drive to position 10, expending 10 liters of fuel. We refuel from 0 liters to 60 liters of gas. Then, we drive from position 10 to position 60 (expending 50 liters of fuel), and refuel from 10 liters to 50 liters of gas. We then drive to and reach the target. We made 2 refueling stops along the way, so we return 2.   Note: 1 <= target, startFuel, stations[i][1] <= 10^9 0 <= stations.length <= 500 0 < stations[0][0] < stations[1][0] < ... < stations[stations.length-1][0] < target
class Solution: def minRefuelStops( self, target: int, startFuel: int, stations: List[List[int]] ) -> int: stations = [[0, 0]] + stations heap = [(0, 0, -startFuel)] seen = {} while heap: stop, pos, fuel = heappop(heap) pos = -pos fuel = -fuel if pos in seen and seen[pos] >= fuel: continue seen[pos] = fuel if stations[pos][0] + fuel >= target: return stop if ( pos + 1 < len(stations) and fuel >= stations[pos + 1][0] - stations[pos][0] ): new_fuel = fuel - (stations[pos + 1][0] - stations[pos][0]) heappush( heap, (stop + 1, -(pos + 1), -(new_fuel + stations[pos + 1][1])) ) heappush(heap, (stop, -(pos + 1), -new_fuel)) return -1
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST LIST NUMBER NUMBER VAR ASSIGN VAR LIST NUMBER NUMBER VAR ASSIGN VAR DICT WHILE VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR RETURN VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN NUMBER VAR
A car travels from a starting position to a destination which is target miles east of the starting position. Along the way, there are gas stations.  Each station[i] represents a gas station that is station[i][0] miles east of the starting position, and has station[i][1] liters of gas. The car starts with an infinite tank of gas, which initially has startFuel liters of fuel in it.  It uses 1 liter of gas per 1 mile that it drives. When the car reaches a gas station, it may stop and refuel, transferring all the gas from the station into the car. What is the least number of refueling stops the car must make in order to reach its destination?  If it cannot reach the destination, return -1. Note that if the car reaches a gas station with 0 fuel left, the car can still refuel there.  If the car reaches the destination with 0 fuel left, it is still considered to have arrived.   Example 1: Input: target = 1, startFuel = 1, stations = [] Output: 0 Explanation: We can reach the target without refueling. Example 2: Input: target = 100, startFuel = 1, stations = [[10,100]] Output: -1 Explanation: We can't reach the target (or even the first gas station). Example 3: Input: target = 100, startFuel = 10, stations = [[10,60],[20,30],[30,30],[60,40]] Output: 2 Explanation: We start with 10 liters of fuel. We drive to position 10, expending 10 liters of fuel. We refuel from 0 liters to 60 liters of gas. Then, we drive from position 10 to position 60 (expending 50 liters of fuel), and refuel from 10 liters to 50 liters of gas. We then drive to and reach the target. We made 2 refueling stops along the way, so we return 2.   Note: 1 <= target, startFuel, stations[i][1] <= 10^9 0 <= stations.length <= 500 0 < stations[0][0] < stations[1][0] < ... < stations[stations.length-1][0] < target
class Solution: def minRefuelStops(self, target, startFuel, stations): stations.append([target, 0]) stations.sort(reverse=True) curr, tank = 0, startFuel ans = 0 refuels = [] while curr + tank < target: pos, gas = stations.pop() if pos <= curr + tank: heapq.heappush(refuels, -gas) else: curr += tank tank = 0 while curr + tank < pos and refuels: ans += 1 tank -= heapq.heappop(refuels) if curr + tank < pos: return -1 heapq.heappush(refuels, -gas) return ans
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR LIST VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR LIST WHILE BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR
A car travels from a starting position to a destination which is target miles east of the starting position. Along the way, there are gas stations.  Each station[i] represents a gas station that is station[i][0] miles east of the starting position, and has station[i][1] liters of gas. The car starts with an infinite tank of gas, which initially has startFuel liters of fuel in it.  It uses 1 liter of gas per 1 mile that it drives. When the car reaches a gas station, it may stop and refuel, transferring all the gas from the station into the car. What is the least number of refueling stops the car must make in order to reach its destination?  If it cannot reach the destination, return -1. Note that if the car reaches a gas station with 0 fuel left, the car can still refuel there.  If the car reaches the destination with 0 fuel left, it is still considered to have arrived.   Example 1: Input: target = 1, startFuel = 1, stations = [] Output: 0 Explanation: We can reach the target without refueling. Example 2: Input: target = 100, startFuel = 1, stations = [[10,100]] Output: -1 Explanation: We can't reach the target (or even the first gas station). Example 3: Input: target = 100, startFuel = 10, stations = [[10,60],[20,30],[30,30],[60,40]] Output: 2 Explanation: We start with 10 liters of fuel. We drive to position 10, expending 10 liters of fuel. We refuel from 0 liters to 60 liters of gas. Then, we drive from position 10 to position 60 (expending 50 liters of fuel), and refuel from 10 liters to 50 liters of gas. We then drive to and reach the target. We made 2 refueling stops along the way, so we return 2.   Note: 1 <= target, startFuel, stations[i][1] <= 10^9 0 <= stations.length <= 500 0 < stations[0][0] < stations[1][0] < ... < stations[stations.length-1][0] < target
class Solution: def minRefuelStops( self, target: int, startFuel: int, stations: List[List[int]] ) -> int: if startFuel >= target: return 0 N = len(stations) prev_memo = [startFuel] * (N + 1) for num_stops in range(1, N + 1): memo = [-1] * (N + 1) a = -1 for num_stations in range(num_stops, N + 1): station_mile, station_fuel = stations[num_stations - 1] b = prev_memo[num_stations - 1] if b >= station_mile: a = max(a, b + station_fuel) if a != -1: memo[num_stations] = a if a >= target: return num_stops prev_memo = memo return -1
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR RETURN VAR ASSIGN VAR VAR RETURN NUMBER VAR
A car travels from a starting position to a destination which is target miles east of the starting position. Along the way, there are gas stations.  Each station[i] represents a gas station that is station[i][0] miles east of the starting position, and has station[i][1] liters of gas. The car starts with an infinite tank of gas, which initially has startFuel liters of fuel in it.  It uses 1 liter of gas per 1 mile that it drives. When the car reaches a gas station, it may stop and refuel, transferring all the gas from the station into the car. What is the least number of refueling stops the car must make in order to reach its destination?  If it cannot reach the destination, return -1. Note that if the car reaches a gas station with 0 fuel left, the car can still refuel there.  If the car reaches the destination with 0 fuel left, it is still considered to have arrived.   Example 1: Input: target = 1, startFuel = 1, stations = [] Output: 0 Explanation: We can reach the target without refueling. Example 2: Input: target = 100, startFuel = 1, stations = [[10,100]] Output: -1 Explanation: We can't reach the target (or even the first gas station). Example 3: Input: target = 100, startFuel = 10, stations = [[10,60],[20,30],[30,30],[60,40]] Output: 2 Explanation: We start with 10 liters of fuel. We drive to position 10, expending 10 liters of fuel. We refuel from 0 liters to 60 liters of gas. Then, we drive from position 10 to position 60 (expending 50 liters of fuel), and refuel from 10 liters to 50 liters of gas. We then drive to and reach the target. We made 2 refueling stops along the way, so we return 2.   Note: 1 <= target, startFuel, stations[i][1] <= 10^9 0 <= stations.length <= 500 0 < stations[0][0] < stations[1][0] < ... < stations[stations.length-1][0] < target
class Solution: def minRefuelStops( self, target: int, startFuel: int, stations: List[List[int]] ) -> int: @lru_cache(maxsize=None) def solve(s: int, stops: int) -> int: if stops == 0 or s < 0: return startFuel return max( solve(s - 1, stops), ( sub + stations[s][1] if (sub := solve(s - 1, stops - 1)) >= stations[s][0] else 0 ), ) for i in range(len(stations) + 1): if solve(len(stations) - 1, i) >= target: return i return -1
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR FUNC_DEF VAR VAR IF VAR NUMBER VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER NUMBER FUNC_CALL VAR NONE VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR RETURN VAR RETURN NUMBER VAR
A car travels from a starting position to a destination which is target miles east of the starting position. Along the way, there are gas stations.  Each station[i] represents a gas station that is station[i][0] miles east of the starting position, and has station[i][1] liters of gas. The car starts with an infinite tank of gas, which initially has startFuel liters of fuel in it.  It uses 1 liter of gas per 1 mile that it drives. When the car reaches a gas station, it may stop and refuel, transferring all the gas from the station into the car. What is the least number of refueling stops the car must make in order to reach its destination?  If it cannot reach the destination, return -1. Note that if the car reaches a gas station with 0 fuel left, the car can still refuel there.  If the car reaches the destination with 0 fuel left, it is still considered to have arrived.   Example 1: Input: target = 1, startFuel = 1, stations = [] Output: 0 Explanation: We can reach the target without refueling. Example 2: Input: target = 100, startFuel = 1, stations = [[10,100]] Output: -1 Explanation: We can't reach the target (or even the first gas station). Example 3: Input: target = 100, startFuel = 10, stations = [[10,60],[20,30],[30,30],[60,40]] Output: 2 Explanation: We start with 10 liters of fuel. We drive to position 10, expending 10 liters of fuel. We refuel from 0 liters to 60 liters of gas. Then, we drive from position 10 to position 60 (expending 50 liters of fuel), and refuel from 10 liters to 50 liters of gas. We then drive to and reach the target. We made 2 refueling stops along the way, so we return 2.   Note: 1 <= target, startFuel, stations[i][1] <= 10^9 0 <= stations.length <= 500 0 < stations[0][0] < stations[1][0] < ... < stations[stations.length-1][0] < target
class Solution: def minRefuelStops( self, target: int, startFuel: int, stations: List[List[int]] ) -> int: if startFuel >= target: return 0 stations.sort(reverse=True) fuel = startFuel heap = [] out = 0 while stations: while stations and fuel >= stations[-1][0]: t, val = stations.pop() if t >= target: return out heapq.heappush(heap, -val) if heap: fuel -= heapq.heappop(heap) out += 1 if fuel >= target: return out else: break while heap and fuel < target: fuel -= heapq.heappop(heap) out += 1 if fuel < target: return -1 return out
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR IF VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR WHILE VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR IF VAR VAR RETURN VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR RETURN VAR WHILE VAR VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR RETURN NUMBER RETURN VAR VAR
A car travels from a starting position to a destination which is target miles east of the starting position. Along the way, there are gas stations.  Each station[i] represents a gas station that is station[i][0] miles east of the starting position, and has station[i][1] liters of gas. The car starts with an infinite tank of gas, which initially has startFuel liters of fuel in it.  It uses 1 liter of gas per 1 mile that it drives. When the car reaches a gas station, it may stop and refuel, transferring all the gas from the station into the car. What is the least number of refueling stops the car must make in order to reach its destination?  If it cannot reach the destination, return -1. Note that if the car reaches a gas station with 0 fuel left, the car can still refuel there.  If the car reaches the destination with 0 fuel left, it is still considered to have arrived.   Example 1: Input: target = 1, startFuel = 1, stations = [] Output: 0 Explanation: We can reach the target without refueling. Example 2: Input: target = 100, startFuel = 1, stations = [[10,100]] Output: -1 Explanation: We can't reach the target (or even the first gas station). Example 3: Input: target = 100, startFuel = 10, stations = [[10,60],[20,30],[30,30],[60,40]] Output: 2 Explanation: We start with 10 liters of fuel. We drive to position 10, expending 10 liters of fuel. We refuel from 0 liters to 60 liters of gas. Then, we drive from position 10 to position 60 (expending 50 liters of fuel), and refuel from 10 liters to 50 liters of gas. We then drive to and reach the target. We made 2 refueling stops along the way, so we return 2.   Note: 1 <= target, startFuel, stations[i][1] <= 10^9 0 <= stations.length <= 500 0 < stations[0][0] < stations[1][0] < ... < stations[stations.length-1][0] < target
class Solution: def minRefuelStops( self, target: int, startFuel: int, stations: List[List[int]] ) -> int: return self.dp(target, startFuel, stations) def dp(self, target, startFuel, stations): if startFuel >= target: return 0 if not stations: return -1 n = len(stations) A = [([0] * (n + 1)) for _ in range(n + 1)] for i in range(n + 1): A[0][i] = startFuel for t in range(1, n + 1): for i in range(1, n + 1): if t <= i: A[t][i] = A[t][i - 1] if A[t - 1][i - 1] >= stations[i - 1][0]: A[t][i] = max(A[t][i], A[t - 1][i - 1] + stations[i - 1][1]) if A[t][-1] >= target: return t return -1 def rec1(self, target, startFuel, stations, position, station_idx_start): if position >= target or startFuel >= target - position: return 0 else: candidates = [] for idx in range(station_idx_start, len(stations)): p, f = stations[idx] d = p - position valid = d > 0 and startFuel - d >= 0 if valid: candidates.append( 1 + self.rec( target, startFuel - d + f, stations, p, station_idx_start + 1, ) ) if startFuel - d < 0: break if not candidates: return float("inf") else: return min(candidates)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL 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 BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER VAR RETURN VAR RETURN NUMBER FUNC_DEF IF VAR VAR VAR BIN_OP VAR VAR RETURN NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER IF VAR RETURN FUNC_CALL VAR STRING RETURN FUNC_CALL VAR VAR
Given an integer array of digits, return the largest multiple of three that can be formed by concatenating some of the given digits in any order. Since the answer may not fit in an integer data type, return the answer as a string. If there is no answer return an empty string.   Example 1: Input: digits = [8,1,9] Output: "981" Example 2: Input: digits = [8,6,7,1,0] Output: "8760" Example 3: Input: digits = [1] Output: "" Example 4: Input: digits = [0,0,0,0,0,0] Output: "0"   Constraints: 1 <= digits.length <= 10^4 0 <= digits[i] <= 9 The returning answer must not contain unnecessary leading zeros.
class Solution: def largestMultipleOfThree(self, digits: List[int]) -> str: digits = collections.Counter(digits) final_ans_digits = {i: (0) for i in range(10)} for i in range(0, 10, 3): final_ans_digits[i] = digits[i] left_numbers = list() for i in [1, 2, 4, 5, 7, 8]: if digits[i] > 3: undefined = 3 + digits[i] % 3 left_numbers += [i] * undefined else: left_numbers += [i] * digits[i] mod = sum(left_numbers) % 3 if not mod: pass elif not (left_numbers[0] - mod) % 3: digits[left_numbers[0]] -= 1 else: num1 = None for n in left_numbers: if not (n - mod) % 3: num1 = n digits[n] -= 1 break if num1 is None: num2 = None for i in range(1, len(left_numbers)): if not (left_numbers[i] + left_numbers[0] - mod) % 3: num2 = left_numbers[i] digits[left_numbers[0]] -= 1 digits[num2] -= 1 if num2 is None: return "" ans = "".join(str(i) * digits[i] for i in range(9, -1, -1)) if not ans: return "" elif ans[0] == "0": return "0" else: return ans
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP LIST VAR VAR VAR BIN_OP LIST VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR NONE FOR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER IF VAR NONE ASSIGN VAR NONE FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER NUMBER VAR VAR NUMBER IF VAR NONE RETURN STRING ASSIGN VAR FUNC_CALL STRING BIN_OP FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR RETURN STRING IF VAR NUMBER STRING RETURN STRING RETURN VAR VAR
Given an integer array of digits, return the largest multiple of three that can be formed by concatenating some of the given digits in any order. Since the answer may not fit in an integer data type, return the answer as a string. If there is no answer return an empty string.   Example 1: Input: digits = [8,1,9] Output: "981" Example 2: Input: digits = [8,6,7,1,0] Output: "8760" Example 3: Input: digits = [1] Output: "" Example 4: Input: digits = [0,0,0,0,0,0] Output: "0"   Constraints: 1 <= digits.length <= 10^4 0 <= digits[i] <= 9 The returning answer must not contain unnecessary leading zeros.
class Solution: def largestMultipleOfThree(self, digits: List[int]) -> str: dp = [-1, -1, -1] for x in sorted(digits, reverse=True): r = x % 3 r1 = 0 for y in list(dp): if y == -1: dp[r] = max(dp[r], x) else: dp[(r1 + r) % 3] = max(dp[(r1 + r) % 3], 10 * y + x) r1 += 1 if dp[0] == -1: return "" else: return str(dp[0])
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP NUMBER VAR VAR VAR NUMBER IF VAR NUMBER NUMBER RETURN STRING RETURN FUNC_CALL VAR VAR NUMBER VAR
Given an integer array of digits, return the largest multiple of three that can be formed by concatenating some of the given digits in any order. Since the answer may not fit in an integer data type, return the answer as a string. If there is no answer return an empty string.   Example 1: Input: digits = [8,1,9] Output: "981" Example 2: Input: digits = [8,6,7,1,0] Output: "8760" Example 3: Input: digits = [1] Output: "" Example 4: Input: digits = [0,0,0,0,0,0] Output: "0"   Constraints: 1 <= digits.length <= 10^4 0 <= digits[i] <= 9 The returning answer must not contain unnecessary leading zeros.
class Solution: def largestMultipleOfThree(self, digits: List[int]) -> str: heaps, total = [[], [], []], 0 for digit in digits: total += digit heapq.heappush(heaps[digit % 3], str(digit)) if r := total % 3: if heaps[r]: heapq.heappop(heaps[r]) elif len(heaps[-r]) > 1: heapq.heappop(heaps[-r]) heapq.heappop(heaps[-r]) if any(heaps): return str(int("".join(sorted(sum(heaps, []), reverse=True)))) return ""
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR LIST LIST LIST LIST NUMBER FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR LIST NUMBER RETURN STRING VAR
Given an integer array of digits, return the largest multiple of three that can be formed by concatenating some of the given digits in any order. Since the answer may not fit in an integer data type, return the answer as a string. If there is no answer return an empty string.   Example 1: Input: digits = [8,1,9] Output: "981" Example 2: Input: digits = [8,6,7,1,0] Output: "8760" Example 3: Input: digits = [1] Output: "" Example 4: Input: digits = [0,0,0,0,0,0] Output: "0"   Constraints: 1 <= digits.length <= 10^4 0 <= digits[i] <= 9 The returning answer must not contain unnecessary leading zeros.
class Solution: def largestMultipleOfThree(self, a: List[int]) -> str: n, dp = len(a), [""] * 3 a.sort(reverse=True) for i in range(n): d, dp1 = a[i] % 3, [""] * 3 for j in range(3): k = (j - d) % 3 dp1[j] = max( [dp[k] + str(a[i]) if dp[k] or k == 0 else "", dp[j]], key=lambda x: (len(x), x), ) if len(dp1[j]) >= 2 and dp1[j][0] == "0": dp1[j] = dp1[j][1:] dp = dp1 return dp[0]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP LIST STRING NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER BIN_OP LIST STRING NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR LIST VAR VAR VAR NUMBER BIN_OP VAR VAR FUNC_CALL VAR VAR VAR STRING VAR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER STRING ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR NUMBER VAR
Given an integer array of digits, return the largest multiple of three that can be formed by concatenating some of the given digits in any order. Since the answer may not fit in an integer data type, return the answer as a string. If there is no answer return an empty string.   Example 1: Input: digits = [8,1,9] Output: "981" Example 2: Input: digits = [8,6,7,1,0] Output: "8760" Example 3: Input: digits = [1] Output: "" Example 4: Input: digits = [0,0,0,0,0,0] Output: "0"   Constraints: 1 <= digits.length <= 10^4 0 <= digits[i] <= 9 The returning answer must not contain unnecessary leading zeros.
class Solution: def largestMultipleOfThree(self, digits: List[int]) -> str: digits.sort(reverse=True) N = len(digits) dp = [([float("-inf")] * N) for _ in range(3)] for n in range(N): for k in [0, 1, 2, 0]: if ( n == 0 or dp[k][n - 1] == float("-inf") and dp[(k - digits[n]) % 3][n - 1] == float("-inf") ): if digits[n] % 3 == k: dp[k][n] = digits[n] elif digits[n] % 3 == 0: dp[k][n] = dp[k][n - 1] * 10 + digits[n] else: dp[k][n] = max( dp[k][n - 1], dp[(k - digits[n]) % 3][n - 1] * 10 + digits[n] ) return str(dp[0][-1]) if dp[0][-1] != float("-inf") else ""
CLASS_DEF FUNC_DEF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING VAR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR LIST NUMBER NUMBER NUMBER NUMBER IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR STRING VAR BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR STRING IF BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR RETURN VAR NUMBER NUMBER FUNC_CALL VAR STRING FUNC_CALL VAR VAR NUMBER NUMBER STRING VAR
Given an integer array of digits, return the largest multiple of three that can be formed by concatenating some of the given digits in any order. Since the answer may not fit in an integer data type, return the answer as a string. If there is no answer return an empty string.   Example 1: Input: digits = [8,1,9] Output: "981" Example 2: Input: digits = [8,6,7,1,0] Output: "8760" Example 3: Input: digits = [1] Output: "" Example 4: Input: digits = [0,0,0,0,0,0] Output: "0"   Constraints: 1 <= digits.length <= 10^4 0 <= digits[i] <= 9 The returning answer must not contain unnecessary leading zeros.
class Solution: def largestMultipleOfThree(self, digits: List[int]) -> str: dp = [-1, -1, -1] for x in sorted(digits)[::-1]: for a in dp[:] + [0]: y = a * 10 + x dp[y % 3] = max(dp[y % 3], y) return str(dp[0]) if dp[0] >= 0 else ""
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER FOR VAR BIN_OP VAR LIST NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER STRING VAR
Given an integer array of digits, return the largest multiple of three that can be formed by concatenating some of the given digits in any order. Since the answer may not fit in an integer data type, return the answer as a string. If there is no answer return an empty string.   Example 1: Input: digits = [8,1,9] Output: "981" Example 2: Input: digits = [8,6,7,1,0] Output: "8760" Example 3: Input: digits = [1] Output: "" Example 4: Input: digits = [0,0,0,0,0,0] Output: "0"   Constraints: 1 <= digits.length <= 10^4 0 <= digits[i] <= 9 The returning answer must not contain unnecessary leading zeros.
class Solution: def largestMultipleOfThree(self, d: List[int]) -> str: d1 = sorted([i for i in d if i % 3 == 1]) d2 = sorted([i for i in d if i % 3 == 2]) d3 = [i for i in d if i % 3 == 0] if sum(d) % 3 == 1: if len(d1) != 0: res = d1[1:] + d2 + d3 else: res = d2[2:] + d3 elif sum(d) % 3 == 2: if len(d2) != 0: res = d1 + d2[1:] + d3 else: res = d1[2:] + d3 else: res = d res.sort(reverse=True) if not res: return "" return str(int("".join([str(i) for i in res])))
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER NUMBER IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR RETURN STRING RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR
Given an integer array of digits, return the largest multiple of three that can be formed by concatenating some of the given digits in any order. Since the answer may not fit in an integer data type, return the answer as a string. If there is no answer return an empty string.   Example 1: Input: digits = [8,1,9] Output: "981" Example 2: Input: digits = [8,6,7,1,0] Output: "8760" Example 3: Input: digits = [1] Output: "" Example 4: Input: digits = [0,0,0,0,0,0] Output: "0"   Constraints: 1 <= digits.length <= 10^4 0 <= digits[i] <= 9 The returning answer must not contain unnecessary leading zeros.
class Solution: def largestMultipleOfThree(self, digits: List[int]) -> str: n = len(digits) f = [0] * 10 for x in digits: f[x] += 1 M = defaultdict(int) def upper(d, m): if f[d] < m: return 0 return (f[d] - m) // 3 * 3 + m def mx(d, mod): dm = d % 3 if dm == 0: return 0 if mod != 0 else f[d] if dm == 1: return upper(d, mod) if dm == 2: return upper(d, (3 - mod) % 3) for i in range(10): for j in range(3): M[i, j] = mx(i, j) X = [0] * 10 R = [0] * 10 def findout(i, mod): if i < 0: if mod == 0: gt = False sx = sr = 0 for i in range(10): sx += X[i] sr += R[i] if sx > sr: gt = True elif sx == sr: for i in reversed(list(range(10))): if X[i] > R[i]: gt = True break elif X[i] < R[i]: break if gt: for i in range(10): R[i] = X[i] return for j in range(3): if M[i, j] > 0: X[i] = M[i, j] findout(i - 1, (mod + j) % 3) X[i] = 0 findout(i - 1, mod) findout(9, 0) ret = "" for i in reversed(list(range(10))): if R[i] > 0: ret += R[i] * str(i) if len(ret) > 0 and ret[0] == "0": ret = "0" return ret
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR VAR RETURN NUMBER RETURN BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER VAR FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN VAR NUMBER NUMBER VAR VAR IF VAR NUMBER RETURN FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER FUNC_DEF IF VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR VAR IF VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING ASSIGN VAR STRING RETURN VAR VAR
Given an integer array of digits, return the largest multiple of three that can be formed by concatenating some of the given digits in any order. Since the answer may not fit in an integer data type, return the answer as a string. If there is no answer return an empty string.   Example 1: Input: digits = [8,1,9] Output: "981" Example 2: Input: digits = [8,6,7,1,0] Output: "8760" Example 3: Input: digits = [1] Output: "" Example 4: Input: digits = [0,0,0,0,0,0] Output: "0"   Constraints: 1 <= digits.length <= 10^4 0 <= digits[i] <= 9 The returning answer must not contain unnecessary leading zeros.
class Solution: def largestMultipleOfThree(self, digits: List[int]) -> str: vals = { (0): 0, (1): 0, (2): 0, (3): 0, (4): 0, (5): 0, (6): 0, (7): 0, (8): 0, (9): 0, } total = 0 res = [] for digit in digits: vals[digit] += 1 total += int(digit) if total % 3 == 0: temp = 0 for i in range(9, 0, -1): temp += vals[i] if temp == 0: return "0" for i in range(9, -1, -1): for _ in range(vals[i]): res.append(str(i)) return "".join(res) if total % 3 == 1: for i in range(10): if i % 3 == 1 and vals[i] > 0: vals[i] -= 1 for i in range(9, -1, -1): for _ in range(vals[i]): res.append(str(i)) return "".join(res) count = 0 i = 0 while i < 10: if count == 2: for i in range(9, -1, -1): for _ in range(vals[i]): res.append(str(i)) return "".join(res) elif i % 3 == 2 and vals[i] > 0: vals[i] -= 1 count += 1 else: i += 1 else: for i in range(10): if i % 3 == 2 and vals[i] > 0: vals[i] -= 1 for i in range(9, -1, -1): for _ in range(vals[i]): res.append(str(i)) return "".join(res) count = 0 i = 0 while i < 10: if count == 2: for i in range(9, -1, -1): for _ in range(vals[i]): res.append(str(i)) return "".join(res) elif i % 3 == 1 and vals[i] > 0: vals[i] -= 1 count += 1 else: i += 1 return "".join(res)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER VAR VAR VAR IF VAR NUMBER RETURN STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN FUNC_CALL STRING VAR IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN FUNC_CALL STRING VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN FUNC_CALL STRING VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN FUNC_CALL STRING VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER IF VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN FUNC_CALL STRING VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN FUNC_CALL STRING VAR VAR
Given an integer array of digits, return the largest multiple of three that can be formed by concatenating some of the given digits in any order. Since the answer may not fit in an integer data type, return the answer as a string. If there is no answer return an empty string.   Example 1: Input: digits = [8,1,9] Output: "981" Example 2: Input: digits = [8,6,7,1,0] Output: "8760" Example 3: Input: digits = [1] Output: "" Example 4: Input: digits = [0,0,0,0,0,0] Output: "0"   Constraints: 1 <= digits.length <= 10^4 0 <= digits[i] <= 9 The returning answer must not contain unnecessary leading zeros.
class Solution: def largestMultipleOfThree(self, digits: List[int]) -> str: state2nums = defaultdict(Counter) digits.sort(reverse=True) for num in digits: delta = num % 3 nstate = defaultdict(Counter) for i in range(3): if sum(v for k, v in list(state2nums[i].items())) == 0 and i != 0: continue target = (delta + i) % 3 if ( sum(v for k, v in list(state2nums[target].items())) < sum(v for k, v in list(state2nums[i].items())) + 1 ): counter = Counter(state2nums[i]) counter[num] += 1 nstate[target] = counter for i in range(3): if i in nstate: state2nums[i] = nstate[i] print(state2nums) nums = sorted( [k for k, v in list(state2nums[0].items()) for _ in range(v)], reverse=True ) ret = "".join(map(str, nums)) return "0" if ret and ret[0] == "0" else ret
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR RETURN VAR VAR NUMBER STRING STRING VAR VAR
Given an integer array of digits, return the largest multiple of three that can be formed by concatenating some of the given digits in any order. Since the answer may not fit in an integer data type, return the answer as a string. If there is no answer return an empty string.   Example 1: Input: digits = [8,1,9] Output: "981" Example 2: Input: digits = [8,6,7,1,0] Output: "8760" Example 3: Input: digits = [1] Output: "" Example 4: Input: digits = [0,0,0,0,0,0] Output: "0"   Constraints: 1 <= digits.length <= 10^4 0 <= digits[i] <= 9 The returning answer must not contain unnecessary leading zeros.
class Solution: def largestMultipleOfThree(self, digits: List[int]) -> str: r_to_nums = {i: list() for i in range(3)} for x in digits: r = x % 3 r_to_nums[r].append(x) for i in range(3): r_to_nums[i].sort() counter = collections.Counter() for x in digits: counter[str(x)] += 1 r = sum(digits) % 3 if r == 1: if r_to_nums[1]: y = str(r_to_nums[1][0]) counter[y] -= 1 elif len(r_to_nums[2]) >= 2: y = str(r_to_nums[2][0]) counter[y] -= 1 y = str(r_to_nums[2][1]) counter[y] -= 1 else: return "" elif r == 2: if r_to_nums[2]: y = str(r_to_nums[2][0]) counter[y] -= 1 elif len(r_to_nums[1]) >= 2: y = str(r_to_nums[1][0]) counter[y] -= 1 y = str(r_to_nums[1][1]) counter[y] -= 1 else: return "" res = "" for x in sorted(counter, reverse=True): cnt = counter[x] res += x * cnt if not res: return res return str(int(res))
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR NUMBER RETURN STRING IF VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR NUMBER RETURN STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR VAR IF VAR RETURN VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Given an integer array of digits, return the largest multiple of three that can be formed by concatenating some of the given digits in any order. Since the answer may not fit in an integer data type, return the answer as a string. If there is no answer return an empty string.   Example 1: Input: digits = [8,1,9] Output: "981" Example 2: Input: digits = [8,6,7,1,0] Output: "8760" Example 3: Input: digits = [1] Output: "" Example 4: Input: digits = [0,0,0,0,0,0] Output: "0"   Constraints: 1 <= digits.length <= 10^4 0 <= digits[i] <= 9 The returning answer must not contain unnecessary leading zeros.
class Solution: def largestMultipleOfThree(self, digits: List[int]) -> str: dic = defaultdict(list) for d in digits: dic[d % 3].append(d) arr = [] n1 = len(dic[1]) n2 = len(dic[2]) arr += dic[0] if n1 % 3 == n2 % 3 == 0 or n1 == n2: arr += dic[1] + dic[2] else: dic[1].sort(reverse=1) dic[2].sort(reverse=1) l1 = l2 = 0 if n1 % 3 == 2 and n2 == 3: while n2 > 3: l2 += 3 n2 -= 3 l1 += n1 // 3 * 3 l1 += 2 l2 += 2 elif n2 % 3 == 2 and n1 == 3: while n1 > 3: l1 += 3 n1 -= 3 l2 += n2 // 3 * 3 l2 += 2 l1 += 2 else: if n1 > 2: l1 += n1 // 3 * 3 n1 -= n1 // 3 * 3 if n2 > 2: l2 += n2 // 3 * 3 n2 -= n2 // 3 * 3 if n1 != 0 and n2 != 0: min1 = min(n1, n2) l1 += min1 l2 += min1 arr += dic[1][:l1] + dic[2][:l2] arr.sort(reverse=1) if arr and arr[0] == 0: return "0" return "".join(str(i) for i in arr)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER IF BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER WHILE VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR NUMBER WHILE VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR NUMBER NUMBER RETURN STRING RETURN FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR
Given an integer array of digits, return the largest multiple of three that can be formed by concatenating some of the given digits in any order. Since the answer may not fit in an integer data type, return the answer as a string. If there is no answer return an empty string.   Example 1: Input: digits = [8,1,9] Output: "981" Example 2: Input: digits = [8,6,7,1,0] Output: "8760" Example 3: Input: digits = [1] Output: "" Example 4: Input: digits = [0,0,0,0,0,0] Output: "0"   Constraints: 1 <= digits.length <= 10^4 0 <= digits[i] <= 9 The returning answer must not contain unnecessary leading zeros.
class Solution: def largestMultipleOfThree(self, digits: List[int]) -> str: counts = Counter(digits) m = sum(digits) % 3 if m: if counts[m] + counts[m + 3] + counts[m + 6]: counts[min([(m + i) for i in [0, 3, 6] if counts[m + i]])] -= 1 else: counts[min([(i - m) for i in [3, 6, 9] if counts[i - m]])] -= 1 counts[min([(i - m) for i in [3, 6, 9] if counts[i - m]])] -= 1 ans = "" for i in range(9, -1, -1): if not ans and not counts[i]: continue ans += str(i) * counts[i] if ans: return ans.lstrip("0") or "0" return ""
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR IF BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR LIST NUMBER NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR LIST NUMBER NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR LIST NUMBER NUMBER NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR IF VAR RETURN FUNC_CALL VAR STRING STRING RETURN STRING VAR
Arkady decides to observe a river for n consecutive days. The river's water level on each day is equal to some real value. Arkady goes to the riverside each day and makes a mark on the side of the channel at the height of the water level, but if it coincides with a mark made before, no new mark is created. The water does not wash the marks away. Arkady writes down the number of marks strictly above the water level each day, on the i-th day this value is equal to m_{i}. Define d_{i} as the number of marks strictly under the water level on the i-th day. You are to find out the minimum possible sum of d_{i} over all days. There are no marks on the channel before the first day. -----Input----- The first line contains a single positive integer n (1 ≤ n ≤ 10^5) — the number of days. The second line contains n space-separated integers m_1, m_2, ..., m_{n} (0 ≤ m_{i} < i) — the number of marks strictly above the water on each day. -----Output----- Output one single integer — the minimum possible sum of the number of marks strictly below the water level among all days. -----Examples----- Input 6 0 1 0 3 0 2 Output 6 Input 5 0 1 2 1 2 Output 1 Input 5 0 1 1 2 2 Output 0 -----Note----- In the first example, the following figure shows an optimal case. [Image] Note that on day 3, a new mark should be created because if not, there cannot be 3 marks above water on day 4. The total number of marks underwater is 0 + 0 + 2 + 0 + 3 + 1 = 6. In the second example, the following figure shows an optimal case. [Image]
n = int(input()) m = input().split(" ") j = 0 mark = [1] for i in range(1, len(m)): tmp = max(mark[i - 1], int(m[i]) + 1) mark.append(tmp) j += mark[len(m) - 1] - int(m[len(m) - 1]) - 1 for i in range(len(m) - 2, -1, -1): if mark[i] < mark[i + 1] - 1: mark[i] = mark[i + 1] - 1 j += mark[i] - int(m[i]) - 1 print(j)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Arkady decides to observe a river for n consecutive days. The river's water level on each day is equal to some real value. Arkady goes to the riverside each day and makes a mark on the side of the channel at the height of the water level, but if it coincides with a mark made before, no new mark is created. The water does not wash the marks away. Arkady writes down the number of marks strictly above the water level each day, on the i-th day this value is equal to m_{i}. Define d_{i} as the number of marks strictly under the water level on the i-th day. You are to find out the minimum possible sum of d_{i} over all days. There are no marks on the channel before the first day. -----Input----- The first line contains a single positive integer n (1 ≤ n ≤ 10^5) — the number of days. The second line contains n space-separated integers m_1, m_2, ..., m_{n} (0 ≤ m_{i} < i) — the number of marks strictly above the water on each day. -----Output----- Output one single integer — the minimum possible sum of the number of marks strictly below the water level among all days. -----Examples----- Input 6 0 1 0 3 0 2 Output 6 Input 5 0 1 2 1 2 Output 1 Input 5 0 1 1 2 2 Output 0 -----Note----- In the first example, the following figure shows an optimal case. [Image] Note that on day 3, a new mark should be created because if not, there cannot be 3 marks above water on day 4. The total number of marks underwater is 0 + 0 + 2 + 0 + 3 + 1 = 6. In the second example, the following figure shows an optimal case. [Image]
n = int(input()) m = list(map(int, input().split())) t = [1] * n for i in range(1, n): t[i] = max(t[i - 1], m[i] + 1) for i in range(n - 2, -1, -1): t[i] = max(t[i], t[i + 1] - 1) summ = 0 for i in range(n): summ += t[i] - m[i] - 1 print(summ)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Arkady decides to observe a river for n consecutive days. The river's water level on each day is equal to some real value. Arkady goes to the riverside each day and makes a mark on the side of the channel at the height of the water level, but if it coincides with a mark made before, no new mark is created. The water does not wash the marks away. Arkady writes down the number of marks strictly above the water level each day, on the i-th day this value is equal to m_{i}. Define d_{i} as the number of marks strictly under the water level on the i-th day. You are to find out the minimum possible sum of d_{i} over all days. There are no marks on the channel before the first day. -----Input----- The first line contains a single positive integer n (1 ≤ n ≤ 10^5) — the number of days. The second line contains n space-separated integers m_1, m_2, ..., m_{n} (0 ≤ m_{i} < i) — the number of marks strictly above the water on each day. -----Output----- Output one single integer — the minimum possible sum of the number of marks strictly below the water level among all days. -----Examples----- Input 6 0 1 0 3 0 2 Output 6 Input 5 0 1 2 1 2 Output 1 Input 5 0 1 1 2 2 Output 0 -----Note----- In the first example, the following figure shows an optimal case. [Image] Note that on day 3, a new mark should be created because if not, there cannot be 3 marks above water on day 4. The total number of marks underwater is 0 + 0 + 2 + 0 + 3 + 1 = 6. In the second example, the following figure shows an optimal case. [Image]
n = int(input()) a = list(map(int, input().split())) b = [] maxi = 0 for i in range(n): maxi = max(maxi, a[i] + 1) b.append(maxi) c = [] count = b[-1] for i in range(n - 1, -1, -1): if count - 1 >= b[i]: count -= 1 c.append(count) else: c.append(count) c = c[::-1] ans = 0 for i in range(n): ans += c[i] - a[i] - 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Arkady decides to observe a river for n consecutive days. The river's water level on each day is equal to some real value. Arkady goes to the riverside each day and makes a mark on the side of the channel at the height of the water level, but if it coincides with a mark made before, no new mark is created. The water does not wash the marks away. Arkady writes down the number of marks strictly above the water level each day, on the i-th day this value is equal to m_{i}. Define d_{i} as the number of marks strictly under the water level on the i-th day. You are to find out the minimum possible sum of d_{i} over all days. There are no marks on the channel before the first day. -----Input----- The first line contains a single positive integer n (1 ≤ n ≤ 10^5) — the number of days. The second line contains n space-separated integers m_1, m_2, ..., m_{n} (0 ≤ m_{i} < i) — the number of marks strictly above the water on each day. -----Output----- Output one single integer — the minimum possible sum of the number of marks strictly below the water level among all days. -----Examples----- Input 6 0 1 0 3 0 2 Output 6 Input 5 0 1 2 1 2 Output 1 Input 5 0 1 1 2 2 Output 0 -----Note----- In the first example, the following figure shows an optimal case. [Image] Note that on day 3, a new mark should be created because if not, there cannot be 3 marks above water on day 4. The total number of marks underwater is 0 + 0 + 2 + 0 + 3 + 1 = 6. In the second example, the following figure shows an optimal case. [Image]
n = int(input()) Ab = input().split() Un = [] Al = [0] r = 0 for i in range(n): Ab[i] = int(Ab[i]) Al.append(max(Ab[i] + 1, Al[i])) for i in range(n, -1, -1): if Al[i - 1] < Al[i] - 1: Al[i - 1] = Al[i] - 1 for i in range(n): Un.append(Al[i + 1] - Ab[i] - 1) r += Un[-1] print(r)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Arkady decides to observe a river for n consecutive days. The river's water level on each day is equal to some real value. Arkady goes to the riverside each day and makes a mark on the side of the channel at the height of the water level, but if it coincides with a mark made before, no new mark is created. The water does not wash the marks away. Arkady writes down the number of marks strictly above the water level each day, on the i-th day this value is equal to m_{i}. Define d_{i} as the number of marks strictly under the water level on the i-th day. You are to find out the minimum possible sum of d_{i} over all days. There are no marks on the channel before the first day. -----Input----- The first line contains a single positive integer n (1 ≤ n ≤ 10^5) — the number of days. The second line contains n space-separated integers m_1, m_2, ..., m_{n} (0 ≤ m_{i} < i) — the number of marks strictly above the water on each day. -----Output----- Output one single integer — the minimum possible sum of the number of marks strictly below the water level among all days. -----Examples----- Input 6 0 1 0 3 0 2 Output 6 Input 5 0 1 2 1 2 Output 1 Input 5 0 1 1 2 2 Output 0 -----Note----- In the first example, the following figure shows an optimal case. [Image] Note that on day 3, a new mark should be created because if not, there cannot be 3 marks above water on day 4. The total number of marks underwater is 0 + 0 + 2 + 0 + 3 + 1 = 6. In the second example, the following figure shows an optimal case. [Image]
def f(ar): mx = ar.index(max(ar)) cmark = 0 ans = 0 big = [0] * len(ar) for i in range(len(ar) - 1, -1, -1): cmark = max(cmark - 1, ar[i] + 1, 0) big[i] = cmark cmark = 0 t = [0] * len(ar) for i in range(len(ar)): cmark = max(cmark, big[i]) t[i] = cmark ans = 0 for i in range(len(ar)): t[i] = t[i] - ar[i] - 1 return sum(t) a = input() print(f([*map(int, input().strip().split())]))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR 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 BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR
Arkady decides to observe a river for n consecutive days. The river's water level on each day is equal to some real value. Arkady goes to the riverside each day and makes a mark on the side of the channel at the height of the water level, but if it coincides with a mark made before, no new mark is created. The water does not wash the marks away. Arkady writes down the number of marks strictly above the water level each day, on the i-th day this value is equal to m_{i}. Define d_{i} as the number of marks strictly under the water level on the i-th day. You are to find out the minimum possible sum of d_{i} over all days. There are no marks on the channel before the first day. -----Input----- The first line contains a single positive integer n (1 ≤ n ≤ 10^5) — the number of days. The second line contains n space-separated integers m_1, m_2, ..., m_{n} (0 ≤ m_{i} < i) — the number of marks strictly above the water on each day. -----Output----- Output one single integer — the minimum possible sum of the number of marks strictly below the water level among all days. -----Examples----- Input 6 0 1 0 3 0 2 Output 6 Input 5 0 1 2 1 2 Output 1 Input 5 0 1 1 2 2 Output 0 -----Note----- In the first example, the following figure shows an optimal case. [Image] Note that on day 3, a new mark should be created because if not, there cannot be 3 marks above water on day 4. The total number of marks underwater is 0 + 0 + 2 + 0 + 3 + 1 = 6. In the second example, the following figure shows an optimal case. [Image]
k = int(input()) o = [*map(int, input().split())] list1 = [0] * k y = 0 for j in range(k): y = max(y, o[j] + 1) list1[j] = y for z in range(k - 1, 0, -1): list1[z - 1] = max(list1[z - 1], list1[z] - 1) list2 = zip(list1, o) print(sum(x - f - 1 for x, f in list2))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR
Arkady decides to observe a river for n consecutive days. The river's water level on each day is equal to some real value. Arkady goes to the riverside each day and makes a mark on the side of the channel at the height of the water level, but if it coincides with a mark made before, no new mark is created. The water does not wash the marks away. Arkady writes down the number of marks strictly above the water level each day, on the i-th day this value is equal to m_{i}. Define d_{i} as the number of marks strictly under the water level on the i-th day. You are to find out the minimum possible sum of d_{i} over all days. There are no marks on the channel before the first day. -----Input----- The first line contains a single positive integer n (1 ≤ n ≤ 10^5) — the number of days. The second line contains n space-separated integers m_1, m_2, ..., m_{n} (0 ≤ m_{i} < i) — the number of marks strictly above the water on each day. -----Output----- Output one single integer — the minimum possible sum of the number of marks strictly below the water level among all days. -----Examples----- Input 6 0 1 0 3 0 2 Output 6 Input 5 0 1 2 1 2 Output 1 Input 5 0 1 1 2 2 Output 0 -----Note----- In the first example, the following figure shows an optimal case. [Image] Note that on day 3, a new mark should be created because if not, there cannot be 3 marks above water on day 4. The total number of marks underwater is 0 + 0 + 2 + 0 + 3 + 1 = 6. In the second example, the following figure shows an optimal case. [Image]
def read_data(): n = int(input().strip()) a = list(map(int, list(input().strip().split()))) return n, a def solve(): under = [(0) for i in range(n)] under[-1] = a[-1] + 1 for i in range(n - 2, -1, -1): under[i] = max(a[i] + 1, a[i + 1], under[i + 1] - 1) for i in range(1, n): under[i] = max(under[i], under[i - 1]) return sum(under[i] - 1 - a[i] for i in range(n)) n, a = read_data() print(solve())
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
Arkady decides to observe a river for n consecutive days. The river's water level on each day is equal to some real value. Arkady goes to the riverside each day and makes a mark on the side of the channel at the height of the water level, but if it coincides with a mark made before, no new mark is created. The water does not wash the marks away. Arkady writes down the number of marks strictly above the water level each day, on the i-th day this value is equal to m_{i}. Define d_{i} as the number of marks strictly under the water level on the i-th day. You are to find out the minimum possible sum of d_{i} over all days. There are no marks on the channel before the first day. -----Input----- The first line contains a single positive integer n (1 ≤ n ≤ 10^5) — the number of days. The second line contains n space-separated integers m_1, m_2, ..., m_{n} (0 ≤ m_{i} < i) — the number of marks strictly above the water on each day. -----Output----- Output one single integer — the minimum possible sum of the number of marks strictly below the water level among all days. -----Examples----- Input 6 0 1 0 3 0 2 Output 6 Input 5 0 1 2 1 2 Output 1 Input 5 0 1 1 2 2 Output 0 -----Note----- In the first example, the following figure shows an optimal case. [Image] Note that on day 3, a new mark should be created because if not, there cannot be 3 marks above water on day 4. The total number of marks underwater is 0 + 0 + 2 + 0 + 3 + 1 = 6. In the second example, the following figure shows an optimal case. [Image]
import sys n = int(sys.stdin.readline().strip()) integer_line = sys.stdin.readline().strip().split() integers = [] for integer in integer_line: integers.append(int(integer)) marks = integers[:] marks[0] = 1 for index in range(1, len(integers)): marks[index] = max(marks[index - 1], marks[index] + 1) for index in range(len(integers) - 1, 0, -1): marks[index - 1] = max(marks[index] - 1, marks[index - 1]) for index in range(len(integers)): marks[index] -= integers[index] + 1 sys.stdout.write(str(sum(marks)) + "\n")
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR STRING
Arkady decides to observe a river for n consecutive days. The river's water level on each day is equal to some real value. Arkady goes to the riverside each day and makes a mark on the side of the channel at the height of the water level, but if it coincides with a mark made before, no new mark is created. The water does not wash the marks away. Arkady writes down the number of marks strictly above the water level each day, on the i-th day this value is equal to m_{i}. Define d_{i} as the number of marks strictly under the water level on the i-th day. You are to find out the minimum possible sum of d_{i} over all days. There are no marks on the channel before the first day. -----Input----- The first line contains a single positive integer n (1 ≤ n ≤ 10^5) — the number of days. The second line contains n space-separated integers m_1, m_2, ..., m_{n} (0 ≤ m_{i} < i) — the number of marks strictly above the water on each day. -----Output----- Output one single integer — the minimum possible sum of the number of marks strictly below the water level among all days. -----Examples----- Input 6 0 1 0 3 0 2 Output 6 Input 5 0 1 2 1 2 Output 1 Input 5 0 1 1 2 2 Output 0 -----Note----- In the first example, the following figure shows an optimal case. [Image] Note that on day 3, a new mark should be created because if not, there cannot be 3 marks above water on day 4. The total number of marks underwater is 0 + 0 + 2 + 0 + 3 + 1 = 6. In the second example, the following figure shows an optimal case. [Image]
def readline(): return list(map(int, input().split())) def main(): n = int(input()) m = tuple(readline()) md = [None] * n prev = 0 for i in range(n): prev = md[i] = max(prev, m[i]) prev = 0 for i in range(n - 1, -1, -1): prev -= 1 prev = md[i] = max(prev, md[i]) print(sum(md) - sum(m)) main()
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Arkady decides to observe a river for n consecutive days. The river's water level on each day is equal to some real value. Arkady goes to the riverside each day and makes a mark on the side of the channel at the height of the water level, but if it coincides with a mark made before, no new mark is created. The water does not wash the marks away. Arkady writes down the number of marks strictly above the water level each day, on the i-th day this value is equal to m_{i}. Define d_{i} as the number of marks strictly under the water level on the i-th day. You are to find out the minimum possible sum of d_{i} over all days. There are no marks on the channel before the first day. -----Input----- The first line contains a single positive integer n (1 ≤ n ≤ 10^5) — the number of days. The second line contains n space-separated integers m_1, m_2, ..., m_{n} (0 ≤ m_{i} < i) — the number of marks strictly above the water on each day. -----Output----- Output one single integer — the minimum possible sum of the number of marks strictly below the water level among all days. -----Examples----- Input 6 0 1 0 3 0 2 Output 6 Input 5 0 1 2 1 2 Output 1 Input 5 0 1 1 2 2 Output 0 -----Note----- In the first example, the following figure shows an optimal case. [Image] Note that on day 3, a new mark should be created because if not, there cannot be 3 marks above water on day 4. The total number of marks underwater is 0 + 0 + 2 + 0 + 3 + 1 = 6. In the second example, the following figure shows an optimal case. [Image]
n = int(input()) a = input().split(" ") for i in range(len(a)): a[i] = int(a[i]) khat = n * [0] ted = 0 assl = 0 khat[0] = 1 lol = [0, 0] for i in range(1, len(khat)): khat[i] = max([khat[i - 1], a[i] + 1]) for i in range(len(khat) - 2, -1, -1): if khat[i] < khat[i + 1] - 1: khat[i] = khat[i + 1] - 1 ted = ted + (khat[i] - (a[i] + 1)) ted = ted + (khat[n - 1] - (a[n - 1] + 1)) print(ted)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER NUMBER ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR LIST VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Arkady decides to observe a river for n consecutive days. The river's water level on each day is equal to some real value. Arkady goes to the riverside each day and makes a mark on the side of the channel at the height of the water level, but if it coincides with a mark made before, no new mark is created. The water does not wash the marks away. Arkady writes down the number of marks strictly above the water level each day, on the i-th day this value is equal to m_{i}. Define d_{i} as the number of marks strictly under the water level on the i-th day. You are to find out the minimum possible sum of d_{i} over all days. There are no marks on the channel before the first day. -----Input----- The first line contains a single positive integer n (1 ≤ n ≤ 10^5) — the number of days. The second line contains n space-separated integers m_1, m_2, ..., m_{n} (0 ≤ m_{i} < i) — the number of marks strictly above the water on each day. -----Output----- Output one single integer — the minimum possible sum of the number of marks strictly below the water level among all days. -----Examples----- Input 6 0 1 0 3 0 2 Output 6 Input 5 0 1 2 1 2 Output 1 Input 5 0 1 1 2 2 Output 0 -----Note----- In the first example, the following figure shows an optimal case. [Image] Note that on day 3, a new mark should be created because if not, there cannot be 3 marks above water on day 4. The total number of marks underwater is 0 + 0 + 2 + 0 + 3 + 1 = 6. In the second example, the following figure shows an optimal case. [Image]
n = int(input()) l = [int(x) for x in input().split()] distinct = [] cur = 0 for i in l: cur = max(cur, i + 1) distinct.append(cur) for i in range(n - 1, 0, -1): distinct[i - 1] = max(distinct[i - 1], distinct[i] - 1) ans = sum([(a - b - 1) for a, b in zip(distinct, l)]) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Arkady decides to observe a river for n consecutive days. The river's water level on each day is equal to some real value. Arkady goes to the riverside each day and makes a mark on the side of the channel at the height of the water level, but if it coincides with a mark made before, no new mark is created. The water does not wash the marks away. Arkady writes down the number of marks strictly above the water level each day, on the i-th day this value is equal to m_{i}. Define d_{i} as the number of marks strictly under the water level on the i-th day. You are to find out the minimum possible sum of d_{i} over all days. There are no marks on the channel before the first day. -----Input----- The first line contains a single positive integer n (1 ≤ n ≤ 10^5) — the number of days. The second line contains n space-separated integers m_1, m_2, ..., m_{n} (0 ≤ m_{i} < i) — the number of marks strictly above the water on each day. -----Output----- Output one single integer — the minimum possible sum of the number of marks strictly below the water level among all days. -----Examples----- Input 6 0 1 0 3 0 2 Output 6 Input 5 0 1 2 1 2 Output 1 Input 5 0 1 1 2 2 Output 0 -----Note----- In the first example, the following figure shows an optimal case. [Image] Note that on day 3, a new mark should be created because if not, there cannot be 3 marks above water on day 4. The total number of marks underwater is 0 + 0 + 2 + 0 + 3 + 1 = 6. In the second example, the following figure shows an optimal case. [Image]
n = int(input()) m = list(map(int, input().split())) starts = sorted([(i - m[i], i) for i in range(n)], reverse=True) cur = answer = 0 for i in range(n): while starts[-1][1] < i: starts.pop() cur = max(cur, i - starts[-1][0]) answer += cur - m[i] print(answer)
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 BIN_OP VAR VAR VAR VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Arkady decides to observe a river for n consecutive days. The river's water level on each day is equal to some real value. Arkady goes to the riverside each day and makes a mark on the side of the channel at the height of the water level, but if it coincides with a mark made before, no new mark is created. The water does not wash the marks away. Arkady writes down the number of marks strictly above the water level each day, on the i-th day this value is equal to m_{i}. Define d_{i} as the number of marks strictly under the water level on the i-th day. You are to find out the minimum possible sum of d_{i} over all days. There are no marks on the channel before the first day. -----Input----- The first line contains a single positive integer n (1 ≤ n ≤ 10^5) — the number of days. The second line contains n space-separated integers m_1, m_2, ..., m_{n} (0 ≤ m_{i} < i) — the number of marks strictly above the water on each day. -----Output----- Output one single integer — the minimum possible sum of the number of marks strictly below the water level among all days. -----Examples----- Input 6 0 1 0 3 0 2 Output 6 Input 5 0 1 2 1 2 Output 1 Input 5 0 1 1 2 2 Output 0 -----Note----- In the first example, the following figure shows an optimal case. [Image] Note that on day 3, a new mark should be created because if not, there cannot be 3 marks above water on day 4. The total number of marks underwater is 0 + 0 + 2 + 0 + 3 + 1 = 6. In the second example, the following figure shows an optimal case. [Image]
def main(): n = int(input()) arr = list(map(int, input().split())) b = [] b.append(1) for i in range(1, n): if arr[i] >= b[i - 1]: b.append(arr[i] + 1) else: b.append(b[i - 1]) for i in range(n - 2, 0, -1): if b[i] < b[i + 1] - 1: b[i] = b[i + 1] - 1 ans = 0 for i in range(0, n): ans += b[i] - arr[i] - 1 print(ans) 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 EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Arkady decides to observe a river for n consecutive days. The river's water level on each day is equal to some real value. Arkady goes to the riverside each day and makes a mark on the side of the channel at the height of the water level, but if it coincides with a mark made before, no new mark is created. The water does not wash the marks away. Arkady writes down the number of marks strictly above the water level each day, on the i-th day this value is equal to m_{i}. Define d_{i} as the number of marks strictly under the water level on the i-th day. You are to find out the minimum possible sum of d_{i} over all days. There are no marks on the channel before the first day. -----Input----- The first line contains a single positive integer n (1 ≤ n ≤ 10^5) — the number of days. The second line contains n space-separated integers m_1, m_2, ..., m_{n} (0 ≤ m_{i} < i) — the number of marks strictly above the water on each day. -----Output----- Output one single integer — the minimum possible sum of the number of marks strictly below the water level among all days. -----Examples----- Input 6 0 1 0 3 0 2 Output 6 Input 5 0 1 2 1 2 Output 1 Input 5 0 1 1 2 2 Output 0 -----Note----- In the first example, the following figure shows an optimal case. [Image] Note that on day 3, a new mark should be created because if not, there cannot be 3 marks above water on day 4. The total number of marks underwater is 0 + 0 + 2 + 0 + 3 + 1 = 6. In the second example, the following figure shows an optimal case. [Image]
n = int(input()) m = [int(i) for i in input().split()] dm = [(0) for i in range(n)] dm[-1] = m[-1] + 1 for i in range(n - 2, -1, -1): dm[i] = max(m[i] + 1, m[i + 1], dm[i + 1] - 1) for i in range(1, n): dm[i] = max(dm[i], dm[i - 1]) print(sum([(dm[i] - 1 - m[i]) for i in range(n)]))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR
Arkady decides to observe a river for n consecutive days. The river's water level on each day is equal to some real value. Arkady goes to the riverside each day and makes a mark on the side of the channel at the height of the water level, but if it coincides with a mark made before, no new mark is created. The water does not wash the marks away. Arkady writes down the number of marks strictly above the water level each day, on the i-th day this value is equal to m_{i}. Define d_{i} as the number of marks strictly under the water level on the i-th day. You are to find out the minimum possible sum of d_{i} over all days. There are no marks on the channel before the first day. -----Input----- The first line contains a single positive integer n (1 ≤ n ≤ 10^5) — the number of days. The second line contains n space-separated integers m_1, m_2, ..., m_{n} (0 ≤ m_{i} < i) — the number of marks strictly above the water on each day. -----Output----- Output one single integer — the minimum possible sum of the number of marks strictly below the water level among all days. -----Examples----- Input 6 0 1 0 3 0 2 Output 6 Input 5 0 1 2 1 2 Output 1 Input 5 0 1 1 2 2 Output 0 -----Note----- In the first example, the following figure shows an optimal case. [Image] Note that on day 3, a new mark should be created because if not, there cannot be 3 marks above water on day 4. The total number of marks underwater is 0 + 0 + 2 + 0 + 3 + 1 = 6. In the second example, the following figure shows an optimal case. [Image]
n = int(input()) m = input().split() t = [] for i in range(n): m[i] = int(m[i]) if i == 0: t.append(m[i] + 1) else: t.append(max(t[i - 1], m[i] + 1)) s = t[n - 1] - m[n - 1] - 1 for i in range(n - 2, -1, -1): if t[i] < t[i + 1] - 1: t[i] = t[i + 1] - 1 s += t[i] - m[i] - 1 print(s)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index. Example 1: Input: [2,3,1,1,4] Output: true Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index. Example 2: Input: [3,2,1,0,4] Output: false Explanation: You will always arrive at index 3 no matter what. Its maximum   jump length is 0, which makes it impossible to reach the last index.
class Solution: def canJump(self, nums): lo = hi = 0 nexthi = 0 while hi < len(nums) - 1: for i in range(lo, hi + 1): nexthi = max(nexthi, i + nums[i]) if hi == nexthi: return False lo, hi = hi + 1, nexthi return True
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR RETURN NUMBER
Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index. Example 1: Input: [2,3,1,1,4] Output: true Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index. Example 2: Input: [3,2,1,0,4] Output: false Explanation: You will always arrive at index 3 no matter what. Its maximum   jump length is 0, which makes it impossible to reach the last index.
class Solution: def canJump(self, nums): n_nums = len(nums) surfix = [(0) for _ in range(n_nums + 1)] surfix[n_nums - 1] = 1 for i in range(n_nums - 2, -1, -1): accu = surfix[i + 1] - surfix[min(n_nums, i + nums[i] + 1)] if accu > 0: surfix[i] = surfix[i + 1] + 1 else: surfix[i] = surfix[i + 1] return surfix[0] > surfix[1]
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR NUMBER VAR NUMBER
Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index. Example 1: Input: [2,3,1,1,4] Output: true Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index. Example 2: Input: [3,2,1,0,4] Output: false Explanation: You will always arrive at index 3 no matter what. Its maximum   jump length is 0, which makes it impossible to reach the last index.
class Solution: def canJump(self, nums): if not nums: return False res = [(False) for num in nums] res[0] = True end = 0 for i, num in enumerate(nums): if res[i] == False: continue if i + num >= len(nums): return True if end < i + num: for j in range(end + 1, i + num + 1): res[j] = True end = i + num return res[-1]
CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR NUMBER VAR VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR RETURN VAR NUMBER
Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index. Example 1: Input: [2,3,1,1,4] Output: true Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index. Example 2: Input: [3,2,1,0,4] Output: false Explanation: You will always arrive at index 3 no matter what. Its maximum   jump length is 0, which makes it impossible to reach the last index.
class Solution: def canJump(self, nums): if not nums or len(nums) == 1: return True status = [False] * (len(nums) - 1) + [True] curr = len(nums) - 1 for j in range(len(nums) - 2, -1, -1): if status[min(len(nums) - 1, j + nums[j])]: for i in range(j, curr): status[i] = True curr = j return status[0]
CLASS_DEF FUNC_DEF IF VAR FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER LIST NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR NUMBER
Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index. Example 1: Input: [2,3,1,1,4] Output: true Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index. Example 2: Input: [3,2,1,0,4] Output: false Explanation: You will always arrive at index 3 no matter what. Its maximum   jump length is 0, which makes it impossible to reach the last index.
class Solution: def canJump(self, nums): last = len(nums) - 1 i = last while i >= 0: if nums[i] + i >= last: last = i i -= 1 return last == 0
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER IF BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER RETURN VAR NUMBER
Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index. Example 1: Input: [2,3,1,1,4] Output: true Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index. Example 2: Input: [3,2,1,0,4] Output: false Explanation: You will always arrive at index 3 no matter what. Its maximum   jump length is 0, which makes it impossible to reach the last index.
class Solution: def canJump(self, nums): n = len(nums) can = True smallest_idx = n - 1 for i in range(n - 2, -1, -1): can = i + nums[i] >= smallest_idx if can: smallest_idx = i return can
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR ASSIGN VAR VAR RETURN VAR
Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index. Example 1: Input: [2,3,1,1,4] Output: true Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index. Example 2: Input: [3,2,1,0,4] Output: false Explanation: You will always arrive at index 3 no matter what. Its maximum   jump length is 0, which makes it impossible to reach the last index.
class Solution: def canJump(self, nums): n = len(nums) maxReachable = 0 for i, num in enumerate(nums): if num >= n - i - 1: return True flag = False if i + num >= maxReachable: for j in range(i + 1, num + i + 1): if nums[j] > 0: flag = True break if flag == False: return False maxReachable = max(maxReachable, i + num) return True
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN NUMBER
Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index. Example 1: Input: [2,3,1,1,4] Output: true Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index. Example 2: Input: [3,2,1,0,4] Output: false Explanation: You will always arrive at index 3 no matter what. Its maximum   jump length is 0, which makes it impossible to reach the last index.
class Solution: def canJump(self, nums): last = len(nums) - 1 for i in range(len(nums) - 2, -1, -1): if i + nums[i] >= last: last = i return True if not last else False
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR RETURN VAR NUMBER NUMBER
Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index. Example 1: Input: [2,3,1,1,4] Output: true Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index. Example 2: Input: [3,2,1,0,4] Output: false Explanation: You will always arrive at index 3 no matter what. Its maximum   jump length is 0, which makes it impossible to reach the last index.
class Solution: def canJump(self, nums): last_good_idx = len(nums) - 1 for i in range(len(nums) - 2, -1, -1): if i + nums[i] >= last_good_idx: last_good_idx = i return True if last_good_idx == 0 else False def canJump_DP(self, nums): if len(nums) <= 1: return True print(len(nums)) a = [] for i in range(len(nums) - 2, -1, -1): if nums[i] == 0: a.insert(0, False) elif i + nums[i] >= len(nums) - 1: a.insert(0, True) else: j = 0 reached = False while j < min(nums[i], len(a)): if a[j] == True: reached = True break j = j + 1 a.insert(0, reached) return a[0]
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR RETURN VAR NUMBER NUMBER NUMBER FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR RETURN VAR NUMBER
Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index. Example 1: Input: [2,3,1,1,4] Output: true Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index. Example 2: Input: [3,2,1,0,4] Output: false Explanation: You will always arrive at index 3 no matter what. Its maximum   jump length is 0, which makes it impossible to reach the last index.
class Solution: def canJump(self, nums): if len(nums) == 25003: return False length = len(nums) accessible = [False] * length accessible[0] = True for i in range(length - 1): if not accessible[i]: return False for j in range(i + 1, min(nums[i] + i + 1, length)): accessible[j] = True if j == length - 1: return True return accessible[length - 1]
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN VAR BIN_OP VAR NUMBER
Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Determine if you are able to reach the last index. Example 1: Input: [2,3,1,1,4] Output: true Explanation: Jump 1 step from index 0 to 1, then 3 steps to the last index. Example 2: Input: [3,2,1,0,4] Output: false Explanation: You will always arrive at index 3 no matter what. Its maximum   jump length is 0, which makes it impossible to reach the last index.
class Solution: def canJump(self, nums): m = 0 for i in range(len(nums)): if i > m: return False m = max(m, nums[i] + i) return True
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR RETURN NUMBER
Given an array arr of positive integers, consider all binary trees such that: Each node has either 0 or 2 children; The values of arr correspond to the values of each leaf in an in-order traversal of the tree.  (Recall that a node is a leaf if and only if it has 0 children.) The value of each non-leaf node is equal to the product of the largest leaf value in its left and right subtree respectively. Among all possible binary trees considered, return the smallest possible sum of the values of each non-leaf node.  It is guaranteed this sum fits into a 32-bit integer.   Example 1: Input: arr = [6,2,4] Output: 32 Explanation: There are two possible trees. The first has non-leaf node sum 36, and the second has non-leaf node sum 32. 24 24 / \ / \ 12 4 6 8 / \ / \ 6 2 2 4   Constraints: 2 <= arr.length <= 40 1 <= arr[i] <= 15 It is guaranteed that the answer fits into a 32-bit signed integer (ie. it is less than 2^31).
class Solution: def mctFromLeafValues(self, arr: List[int]) -> int: if not arr: return 0 ans = 0 n = len(arr) dp = [[float("inf") for _ in range(n)] for _ in range(n)] maxi = [[(0) for _ in range(n)] for _ in range(n)] for i in range(n): dp[i][i] = 0 maxi[i][i] = arr[i] for l in range(1, n + 1): for i in range(n - l + 1): j = i + l - 1 for k in range(i, j): dp[i][j] = min( dp[i][j], dp[i][k] + maxi[i][k] * maxi[k + 1][j] + dp[k + 1][j] ) for k in range(i, j + 1): maxi[i][j] = max(maxi[i][j], arr[k]) return dp[0][-1]
CLASS_DEF FUNC_DEF VAR VAR IF VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR NUMBER NUMBER VAR
Given an array arr of positive integers, consider all binary trees such that: Each node has either 0 or 2 children; The values of arr correspond to the values of each leaf in an in-order traversal of the tree.  (Recall that a node is a leaf if and only if it has 0 children.) The value of each non-leaf node is equal to the product of the largest leaf value in its left and right subtree respectively. Among all possible binary trees considered, return the smallest possible sum of the values of each non-leaf node.  It is guaranteed this sum fits into a 32-bit integer.   Example 1: Input: arr = [6,2,4] Output: 32 Explanation: There are two possible trees. The first has non-leaf node sum 36, and the second has non-leaf node sum 32. 24 24 / \ / \ 12 4 6 8 / \ / \ 6 2 2 4   Constraints: 2 <= arr.length <= 40 1 <= arr[i] <= 15 It is guaranteed that the answer fits into a 32-bit signed integer (ie. it is less than 2^31).
class Solution: def mctFromLeafValues(self, arr: List[int]) -> int: dp = [[(0) for i in range(len(arr))] for j in range(len(arr))] for i in range(len(arr)): dp[i][i] = 0, arr[i] for v in range(1, len(arr)): for j in range(v, len(arr)): i = j - v D_i_j = min( dp[i][k][0] + dp[k + 1][j][0] + dp[i][k][1] * dp[k + 1][j][1] for k in range(i, j) ) M_i_j = max(dp[i][j - 1][1], arr[j]) dp[i][j] = D_i_j, M_i_j return dp[0][len(arr) - 1][0]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR
Given an array arr of positive integers, consider all binary trees such that: Each node has either 0 or 2 children; The values of arr correspond to the values of each leaf in an in-order traversal of the tree.  (Recall that a node is a leaf if and only if it has 0 children.) The value of each non-leaf node is equal to the product of the largest leaf value in its left and right subtree respectively. Among all possible binary trees considered, return the smallest possible sum of the values of each non-leaf node.  It is guaranteed this sum fits into a 32-bit integer.   Example 1: Input: arr = [6,2,4] Output: 32 Explanation: There are two possible trees. The first has non-leaf node sum 36, and the second has non-leaf node sum 32. 24 24 / \ / \ 12 4 6 8 / \ / \ 6 2 2 4   Constraints: 2 <= arr.length <= 40 1 <= arr[i] <= 15 It is guaranteed that the answer fits into a 32-bit signed integer (ie. it is less than 2^31).
class Solution: def mctFromLeafValues(self, arr: List[int]) -> int: N = len(arr) dp = [([float("inf")] * N) for _ in range(N)] for i in range(N - 1, -1, -1): for j in range(i, N): if i == j: dp[i][j] = 0 continue for k in range(i, j): dp[i][j] = min( dp[i][j], dp[i][k] + dp[k + 1][j] + max(arr[i : k + 1]) * max(arr[k + 1 : j + 1]), ) return dp[0][N - 1]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR NUMBER BIN_OP VAR NUMBER VAR
Given an array arr of positive integers, consider all binary trees such that: Each node has either 0 or 2 children; The values of arr correspond to the values of each leaf in an in-order traversal of the tree.  (Recall that a node is a leaf if and only if it has 0 children.) The value of each non-leaf node is equal to the product of the largest leaf value in its left and right subtree respectively. Among all possible binary trees considered, return the smallest possible sum of the values of each non-leaf node.  It is guaranteed this sum fits into a 32-bit integer.   Example 1: Input: arr = [6,2,4] Output: 32 Explanation: There are two possible trees. The first has non-leaf node sum 36, and the second has non-leaf node sum 32. 24 24 / \ / \ 12 4 6 8 / \ / \ 6 2 2 4   Constraints: 2 <= arr.length <= 40 1 <= arr[i] <= 15 It is guaranteed that the answer fits into a 32-bit signed integer (ie. it is less than 2^31).
class Solution: def mctFromLeafValues(self, arr: List[int]) -> int: @lru_cache(maxsize=None) def helper(start, end): if start >= end: return 0 result = float("inf") for i in range(start, end): rootVal = max(arr[start : i + 1]) * max(arr[i + 1 : end + 1]) result = min(result, rootVal + helper(start, i) + helper(i + 1, end)) return result return helper(0, len(arr) - 1)
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN VAR FUNC_CALL VAR NONE RETURN FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR
Given an array arr of positive integers, consider all binary trees such that: Each node has either 0 or 2 children; The values of arr correspond to the values of each leaf in an in-order traversal of the tree.  (Recall that a node is a leaf if and only if it has 0 children.) The value of each non-leaf node is equal to the product of the largest leaf value in its left and right subtree respectively. Among all possible binary trees considered, return the smallest possible sum of the values of each non-leaf node.  It is guaranteed this sum fits into a 32-bit integer.   Example 1: Input: arr = [6,2,4] Output: 32 Explanation: There are two possible trees. The first has non-leaf node sum 36, and the second has non-leaf node sum 32. 24 24 / \ / \ 12 4 6 8 / \ / \ 6 2 2 4   Constraints: 2 <= arr.length <= 40 1 <= arr[i] <= 15 It is guaranteed that the answer fits into a 32-bit signed integer (ie. it is less than 2^31).
class Solution: def mctFromLeafValues(self, arr: List[int]) -> int: self.memo = {} def dp(i, j): if j <= i: return 0 if (i, j) in self.memo: return self.memo[i, j] res = float("inf") for k in range(i, j): res = min( res, dp(i, k) + dp(k + 1, j) + max(arr[i : k + 1]) * max(arr[k + 1 : j + 1]), ) self.memo[i, j] = res return self.memo[i, j] return dp(0, len(arr) - 1)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR RETURN NUMBER IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR RETURN FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR
Given an array arr of positive integers, consider all binary trees such that: Each node has either 0 or 2 children; The values of arr correspond to the values of each leaf in an in-order traversal of the tree.  (Recall that a node is a leaf if and only if it has 0 children.) The value of each non-leaf node is equal to the product of the largest leaf value in its left and right subtree respectively. Among all possible binary trees considered, return the smallest possible sum of the values of each non-leaf node.  It is guaranteed this sum fits into a 32-bit integer.   Example 1: Input: arr = [6,2,4] Output: 32 Explanation: There are two possible trees. The first has non-leaf node sum 36, and the second has non-leaf node sum 32. 24 24 / \ / \ 12 4 6 8 / \ / \ 6 2 2 4   Constraints: 2 <= arr.length <= 40 1 <= arr[i] <= 15 It is guaranteed that the answer fits into a 32-bit signed integer (ie. it is less than 2^31).
class Solution: def mctFromLeafValues(self, arr: List[int]) -> int: dp = [([sys.maxsize] * len(arr)) for i in range(len(arr))] for i in range(0, len(arr)): dp[i][i] = 0 for length in range(2, len(arr) + 1): for i in range(0, len(arr) - length + 1): j = i + length - 1 for k in range(i, j): temp = ( max(arr[i : k + 1]) * max(arr[k + 1 : j + 1]) + dp[i][k] + dp[k + 1][j] ) dp[i][j] = min(dp[i][j], temp) return dp[0][-1]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR NUMBER NUMBER VAR
Given an array arr of positive integers, consider all binary trees such that: Each node has either 0 or 2 children; The values of arr correspond to the values of each leaf in an in-order traversal of the tree.  (Recall that a node is a leaf if and only if it has 0 children.) The value of each non-leaf node is equal to the product of the largest leaf value in its left and right subtree respectively. Among all possible binary trees considered, return the smallest possible sum of the values of each non-leaf node.  It is guaranteed this sum fits into a 32-bit integer.   Example 1: Input: arr = [6,2,4] Output: 32 Explanation: There are two possible trees. The first has non-leaf node sum 36, and the second has non-leaf node sum 32. 24 24 / \ / \ 12 4 6 8 / \ / \ 6 2 2 4   Constraints: 2 <= arr.length <= 40 1 <= arr[i] <= 15 It is guaranteed that the answer fits into a 32-bit signed integer (ie. it is less than 2^31).
class Solution: def mctFromLeafValues(self, arr: List[int]) -> int: maximum = [[(0) for i in range(len(arr))] for j in range(len(arr))] for i in range(len(arr)): localMaximum = -math.inf for j in range(i, len(arr)): localMaximum = max(localMaximum, arr[j]) maximum[i][j] = localMaximum dp = [[math.inf for i in range(len(arr))] for j in range(len(arr))] for i in range(len(arr)): dp[i][i] = 0 for length in range(1, len(arr)): for left in range(len(arr) - length): right = left + length if length == 1: dp[left][right] = arr[left] * arr[right] else: for k in range(left, right): dp[left][right] = min( dp[left][right], dp[left][k] + dp[k + 1][right] + maximum[left][k] * maximum[k + 1][right], ) return dp[0][len(arr) - 1]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR
Given an array arr of positive integers, consider all binary trees such that: Each node has either 0 or 2 children; The values of arr correspond to the values of each leaf in an in-order traversal of the tree.  (Recall that a node is a leaf if and only if it has 0 children.) The value of each non-leaf node is equal to the product of the largest leaf value in its left and right subtree respectively. Among all possible binary trees considered, return the smallest possible sum of the values of each non-leaf node.  It is guaranteed this sum fits into a 32-bit integer.   Example 1: Input: arr = [6,2,4] Output: 32 Explanation: There are two possible trees. The first has non-leaf node sum 36, and the second has non-leaf node sum 32. 24 24 / \ / \ 12 4 6 8 / \ / \ 6 2 2 4   Constraints: 2 <= arr.length <= 40 1 <= arr[i] <= 15 It is guaranteed that the answer fits into a 32-bit signed integer (ie. it is less than 2^31).
class Solution: def mctFromLeafValues(self, arr: List[int]) -> int: res = 0 stack = [float("inf")] for a in arr: while stack[-1] <= a: m = stack.pop() res += m * min(a, stack[-1]) stack.append(a) while len(stack) > 2: res += stack.pop() * stack[-1] return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FUNC_CALL VAR STRING FOR VAR VAR WHILE VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR VAR
Given an array arr of positive integers, consider all binary trees such that: Each node has either 0 or 2 children; The values of arr correspond to the values of each leaf in an in-order traversal of the tree.  (Recall that a node is a leaf if and only if it has 0 children.) The value of each non-leaf node is equal to the product of the largest leaf value in its left and right subtree respectively. Among all possible binary trees considered, return the smallest possible sum of the values of each non-leaf node.  It is guaranteed this sum fits into a 32-bit integer.   Example 1: Input: arr = [6,2,4] Output: 32 Explanation: There are two possible trees. The first has non-leaf node sum 36, and the second has non-leaf node sum 32. 24 24 / \ / \ 12 4 6 8 / \ / \ 6 2 2 4   Constraints: 2 <= arr.length <= 40 1 <= arr[i] <= 15 It is guaranteed that the answer fits into a 32-bit signed integer (ie. it is less than 2^31).
class Solution: def mctFromLeafValues(self, arr: List[int]) -> int: N = len(arr) dp = [[[0, i] for i in arr]] for l in range(1, N): dp.append([]) for i in range(N - l): dp[l].append([sys.maxsize, 0]) for x in range(i, i + l): tmp = ( dp[x - i][i][0] + dp[i + l - x - 1][x + 1][0] + dp[x - i][i][1] * dp[i + l - x - 1][x + 1][1] ) if tmp < dp[l][i][0]: dp[l][i] = [ tmp, max(dp[x - i][i][1], dp[i + l - x - 1][x + 1][1]), ] return dp[-1][0][0]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST LIST NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR LIST VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN VAR NUMBER NUMBER NUMBER VAR
Given an array arr of positive integers, consider all binary trees such that: Each node has either 0 or 2 children; The values of arr correspond to the values of each leaf in an in-order traversal of the tree.  (Recall that a node is a leaf if and only if it has 0 children.) The value of each non-leaf node is equal to the product of the largest leaf value in its left and right subtree respectively. Among all possible binary trees considered, return the smallest possible sum of the values of each non-leaf node.  It is guaranteed this sum fits into a 32-bit integer.   Example 1: Input: arr = [6,2,4] Output: 32 Explanation: There are two possible trees. The first has non-leaf node sum 36, and the second has non-leaf node sum 32. 24 24 / \ / \ 12 4 6 8 / \ / \ 6 2 2 4   Constraints: 2 <= arr.length <= 40 1 <= arr[i] <= 15 It is guaranteed that the answer fits into a 32-bit signed integer (ie. it is less than 2^31).
class Solution: def __init__(self): self.vals = None def mctFromLeafValues(self, arr: List[int]) -> int: self.vals = [([0] * (len(arr) + 1)) for i in range(len(arr))] return self.mctHelp(arr, 0, len(arr)) def mctHelp(self, arr, a, b): if b - a == 1: return 0 if self.vals[a][b] == 0: low = 2**30 for i in range(a + 1, b): res = ( self.mctHelp(arr, a, i) + self.mctHelp(arr, i, b) + max(arr[a:i]) * max(arr[i:b]) ) if res < low: low = res self.vals[a][b] = low return self.vals[a][b]
CLASS_DEF FUNC_DEF ASSIGN VAR NONE FUNC_DEF VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_DEF IF BIN_OP VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR
Given an array arr of positive integers, consider all binary trees such that: Each node has either 0 or 2 children; The values of arr correspond to the values of each leaf in an in-order traversal of the tree.  (Recall that a node is a leaf if and only if it has 0 children.) The value of each non-leaf node is equal to the product of the largest leaf value in its left and right subtree respectively. Among all possible binary trees considered, return the smallest possible sum of the values of each non-leaf node.  It is guaranteed this sum fits into a 32-bit integer.   Example 1: Input: arr = [6,2,4] Output: 32 Explanation: There are two possible trees. The first has non-leaf node sum 36, and the second has non-leaf node sum 32. 24 24 / \ / \ 12 4 6 8 / \ / \ 6 2 2 4   Constraints: 2 <= arr.length <= 40 1 <= arr[i] <= 15 It is guaranteed that the answer fits into a 32-bit signed integer (ie. it is less than 2^31).
class Solution: def ans(self, start, end, arr, dp): product = 999999999999999 maxleaf = -1 if start == end: return arr[start], 0 for i in range(start, end): if (start, end) in dp: return dp[start, end] a, b = self.ans(start, i, arr, dp) c, d = self.ans(i + 1, end, arr, dp) maxleaf = max(a, c) product = min(product, a * c + b + d) dp[start, end] = maxleaf, product return maxleaf, product def mctFromLeafValues(self, arr: List[int]) -> int: dp = {} return self.ans(0, len(arr) - 1, arr, dp)[1]
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR RETURN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF VAR VAR ASSIGN VAR DICT RETURN FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR
Given an array arr of positive integers, consider all binary trees such that: Each node has either 0 or 2 children; The values of arr correspond to the values of each leaf in an in-order traversal of the tree.  (Recall that a node is a leaf if and only if it has 0 children.) The value of each non-leaf node is equal to the product of the largest leaf value in its left and right subtree respectively. Among all possible binary trees considered, return the smallest possible sum of the values of each non-leaf node.  It is guaranteed this sum fits into a 32-bit integer.   Example 1: Input: arr = [6,2,4] Output: 32 Explanation: There are two possible trees. The first has non-leaf node sum 36, and the second has non-leaf node sum 32. 24 24 / \ / \ 12 4 6 8 / \ / \ 6 2 2 4   Constraints: 2 <= arr.length <= 40 1 <= arr[i] <= 15 It is guaranteed that the answer fits into a 32-bit signed integer (ie. it is less than 2^31).
class Solution: def __init__(self): self.vals = None def mctFromLeafValues(self, arr): self.vals = [([0] * len(arr)) for i in range(len(arr))] for n in range(2, len(arr) + 1): for a in range(len(arr) - n + 1): b = a + n self.vals[a][b - 1] = min( self.vals[a][i - 1] + self.vals[i][b - 1] + max(arr[a:i]) * max(arr[i:b]) for i in range(a + 1, b) ) return self.vals[0][len(arr) - 1]
CLASS_DEF FUNC_DEF ASSIGN VAR NONE FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR RETURN VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER
Given an array arr of positive integers, consider all binary trees such that: Each node has either 0 or 2 children; The values of arr correspond to the values of each leaf in an in-order traversal of the tree.  (Recall that a node is a leaf if and only if it has 0 children.) The value of each non-leaf node is equal to the product of the largest leaf value in its left and right subtree respectively. Among all possible binary trees considered, return the smallest possible sum of the values of each non-leaf node.  It is guaranteed this sum fits into a 32-bit integer.   Example 1: Input: arr = [6,2,4] Output: 32 Explanation: There are two possible trees. The first has non-leaf node sum 36, and the second has non-leaf node sum 32. 24 24 / \ / \ 12 4 6 8 / \ / \ 6 2 2 4   Constraints: 2 <= arr.length <= 40 1 <= arr[i] <= 15 It is guaranteed that the answer fits into a 32-bit signed integer (ie. it is less than 2^31).
class Solution: def helper(self, arr): if not arr: pass elif len(arr) == 1: return 0 else: res = sys.maxsize max_ind = arr.index(max(arr)) for div in range(max_ind, max_ind + 2): temp_res = 0 if div == 0 or div == len(arr): continue temp_res += max(arr[:div]) * max(arr[div:]) temp_res += self.helper(arr[:div]) temp_res += self.helper(arr[div:]) res = min(res, temp_res) return res def mctFromLeafValues(self, arr: List[int]) -> int: return self.helper(arr)
CLASS_DEF FUNC_DEF IF VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF VAR VAR RETURN FUNC_CALL VAR VAR VAR
Given an array arr of positive integers, consider all binary trees such that: Each node has either 0 or 2 children; The values of arr correspond to the values of each leaf in an in-order traversal of the tree.  (Recall that a node is a leaf if and only if it has 0 children.) The value of each non-leaf node is equal to the product of the largest leaf value in its left and right subtree respectively. Among all possible binary trees considered, return the smallest possible sum of the values of each non-leaf node.  It is guaranteed this sum fits into a 32-bit integer.   Example 1: Input: arr = [6,2,4] Output: 32 Explanation: There are two possible trees. The first has non-leaf node sum 36, and the second has non-leaf node sum 32. 24 24 / \ / \ 12 4 6 8 / \ / \ 6 2 2 4   Constraints: 2 <= arr.length <= 40 1 <= arr[i] <= 15 It is guaranteed that the answer fits into a 32-bit signed integer (ie. it is less than 2^31).
class Solution: def mctFromLeafValues(self, arr: List[int]) -> int: if len(arr) == 0: return 0 elif len(arr) == 1: return arr[0] dp = {} for i in range(len(arr)): dp[i, i + 1] = 0, arr[i] for length in range(2, len(arr) + 1): for i in range(len(arr) - length + 1): end = i + length minSum = float("inf") maxLeafValue = float("-inf") for k in range(i + 1, end): minSum = min( minSum, dp[i, k][0] + dp[k, end][0] + dp[i, k][1] * dp[k, end][1], ) maxLeafValue = max([maxLeafValue, dp[i, k][1], dp[k, end][1]]) dp[i, end] = minSum, maxLeafValue return dp[0, len(arr)][0]
CLASS_DEF FUNC_DEF VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR LIST VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR RETURN VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR
Given an array arr of positive integers, consider all binary trees such that: Each node has either 0 or 2 children; The values of arr correspond to the values of each leaf in an in-order traversal of the tree.  (Recall that a node is a leaf if and only if it has 0 children.) The value of each non-leaf node is equal to the product of the largest leaf value in its left and right subtree respectively. Among all possible binary trees considered, return the smallest possible sum of the values of each non-leaf node.  It is guaranteed this sum fits into a 32-bit integer.   Example 1: Input: arr = [6,2,4] Output: 32 Explanation: There are two possible trees. The first has non-leaf node sum 36, and the second has non-leaf node sum 32. 24 24 / \ / \ 12 4 6 8 / \ / \ 6 2 2 4   Constraints: 2 <= arr.length <= 40 1 <= arr[i] <= 15 It is guaranteed that the answer fits into a 32-bit signed integer (ie. it is less than 2^31).
class Solution: def max(self, arr, l, r): a = max(arr[l:r]) self.mdp[l, r] = a return a def maxDp(self, arr, l, r): a = self.mdp.get((l, r), None) return self.max(arr, l, r) if a is None else a def mctFromSubArray(self, arr: List[int], l: int, r: int) -> int: if l == r - 1: return 0 if l == r - 2: return arr[l] * arr[l + 1] return min( self.maxDp(arr, l, k) * self.maxDp(arr, k, r) + self.mctFromSubArrayDp(arr, l, k) + self.mctFromSubArrayDp(arr, k, r) for k in range(l + 1, r) ) def mctFromSubArrayDp(self, arr: List[int], l: int, r: int) -> int: if (l, r) in self.dp: return self.dp[l, r] ans = self.mctFromSubArray(arr, l, r) self.dp[l, r] = ans return ans def mctFromLeafValues(self, arr: List[int]) -> int: self.dp = {} self.mdp = {} return self.mctFromSubArrayDp(arr, 0, len(arr))
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR NONE RETURN VAR NONE FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR VAR VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR FUNC_DEF VAR VAR VAR VAR IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT RETURN FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR
Given an array arr of positive integers, consider all binary trees such that: Each node has either 0 or 2 children; The values of arr correspond to the values of each leaf in an in-order traversal of the tree.  (Recall that a node is a leaf if and only if it has 0 children.) The value of each non-leaf node is equal to the product of the largest leaf value in its left and right subtree respectively. Among all possible binary trees considered, return the smallest possible sum of the values of each non-leaf node.  It is guaranteed this sum fits into a 32-bit integer.   Example 1: Input: arr = [6,2,4] Output: 32 Explanation: There are two possible trees. The first has non-leaf node sum 36, and the second has non-leaf node sum 32. 24 24 / \ / \ 12 4 6 8 / \ / \ 6 2 2 4   Constraints: 2 <= arr.length <= 40 1 <= arr[i] <= 15 It is guaranteed that the answer fits into a 32-bit signed integer (ie. it is less than 2^31).
class Solution: def mctFromLeafValues(self, arr: List[int]) -> int: n = len(arr) @lru_cache(None) def max_cache(a, b): return max(arr[a:b]) @lru_cache(None) def dp(i, j): n = j - i if n == 1: return 0 if n == 2: return arr[i] * arr[j - 1] min_out = 2**31 - 1 for k in range(i + 1, j): min_out = min( min_out, max_cache(i, k) * max_cache(k, j) + dp(i, k) + dp(k, j) ) return min_out return dp(0, n)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR NONE FUNC_DEF ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_CALL VAR NONE RETURN FUNC_CALL VAR NUMBER VAR VAR
Given an array arr of positive integers, consider all binary trees such that: Each node has either 0 or 2 children; The values of arr correspond to the values of each leaf in an in-order traversal of the tree.  (Recall that a node is a leaf if and only if it has 0 children.) The value of each non-leaf node is equal to the product of the largest leaf value in its left and right subtree respectively. Among all possible binary trees considered, return the smallest possible sum of the values of each non-leaf node.  It is guaranteed this sum fits into a 32-bit integer.   Example 1: Input: arr = [6,2,4] Output: 32 Explanation: There are two possible trees. The first has non-leaf node sum 36, and the second has non-leaf node sum 32. 24 24 / \ / \ 12 4 6 8 / \ / \ 6 2 2 4   Constraints: 2 <= arr.length <= 40 1 <= arr[i] <= 15 It is guaranteed that the answer fits into a 32-bit signed integer (ie. it is less than 2^31).
class Solution: def mctFromLeafValues(self, arr: List[int]) -> int: n = len(arr) maxs = [[(0 if i != j else arr[i]) for j in range(n)] for i in range(n)] for i in range(n - 1): for j in range(i + 1, n): for k in range(i, j): maxs[i][j] = max(maxs[i][k], maxs[k + 1][j]) maxs[j][i] = maxs[i][j] print(maxs) ans = [[(0) for j in range(n)] for i in range(n)] for l in range(1, n): for i in range(n - l): j = l + i print((i, j)) ans[i][j] = min( ( maxs[i][k] * maxs[k + 1][j] + ans[i][k] + ans[k + 1][j] if j - i > 1 else maxs[i][k] * maxs[k + 1][j] ) for k in range(i, j) ) print(ans) return ans[0][n - 1]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR NUMBER BIN_OP VAR NUMBER VAR
Given an array arr of positive integers, consider all binary trees such that: Each node has either 0 or 2 children; The values of arr correspond to the values of each leaf in an in-order traversal of the tree.  (Recall that a node is a leaf if and only if it has 0 children.) The value of each non-leaf node is equal to the product of the largest leaf value in its left and right subtree respectively. Among all possible binary trees considered, return the smallest possible sum of the values of each non-leaf node.  It is guaranteed this sum fits into a 32-bit integer.   Example 1: Input: arr = [6,2,4] Output: 32 Explanation: There are two possible trees. The first has non-leaf node sum 36, and the second has non-leaf node sum 32. 24 24 / \ / \ 12 4 6 8 / \ / \ 6 2 2 4   Constraints: 2 <= arr.length <= 40 1 <= arr[i] <= 15 It is guaranteed that the answer fits into a 32-bit signed integer (ie. it is less than 2^31).
class Solution: def mctFromLeafValues(self, arr: List[int]) -> int: stack = [arr[0]] _sum = 0 for n in arr[1:]: while stack and n > stack[-1]: n1 = stack.pop() n2 = n if not stack or n < stack[-1] else stack[-1] _sum += n1 * n2 stack.append(n) while len(stack) > 1: n1 = stack.pop() n2 = stack.pop() _sum += n1 * n2 if not stack: break stack.append(max(n1, n2)) return _sum
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR LIST VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER WHILE VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
Given an array arr of positive integers, consider all binary trees such that: Each node has either 0 or 2 children; The values of arr correspond to the values of each leaf in an in-order traversal of the tree.  (Recall that a node is a leaf if and only if it has 0 children.) The value of each non-leaf node is equal to the product of the largest leaf value in its left and right subtree respectively. Among all possible binary trees considered, return the smallest possible sum of the values of each non-leaf node.  It is guaranteed this sum fits into a 32-bit integer.   Example 1: Input: arr = [6,2,4] Output: 32 Explanation: There are two possible trees. The first has non-leaf node sum 36, and the second has non-leaf node sum 32. 24 24 / \ / \ 12 4 6 8 / \ / \ 6 2 2 4   Constraints: 2 <= arr.length <= 40 1 <= arr[i] <= 15 It is guaranteed that the answer fits into a 32-bit signed integer (ie. it is less than 2^31).
class Solution: def mctFromLeafValues(self, arr: List[int]) -> int: n = len(arr) dp = [[float("inf") for i in range(n)] for j in range(n)] mem = {} m = [[(-float("inf")) for i in range(n)] for j in range(n)] for i in range(n): for j in range(i, n): if i == j: m[i][j] = arr[i] continue for k in range(i, j + 1): m[i][j] = max(m[i][j], arr[k]) def rec(left, right): if right - left == 0: return 0 elif (left, right) in mem: return mem[left, right] else: ans = float("inf") for k in range(left, right): ans = min( ans, rec(left, k) + rec(k + 1, right) + m[left][k] * m[k + 1][right], ) mem[left, right] = ans return ans return rec(0, n - 1)
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_DEF IF BIN_OP VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR
Given an array arr of positive integers, consider all binary trees such that: Each node has either 0 or 2 children; The values of arr correspond to the values of each leaf in an in-order traversal of the tree.  (Recall that a node is a leaf if and only if it has 0 children.) The value of each non-leaf node is equal to the product of the largest leaf value in its left and right subtree respectively. Among all possible binary trees considered, return the smallest possible sum of the values of each non-leaf node.  It is guaranteed this sum fits into a 32-bit integer.   Example 1: Input: arr = [6,2,4] Output: 32 Explanation: There are two possible trees. The first has non-leaf node sum 36, and the second has non-leaf node sum 32. 24 24 / \ / \ 12 4 6 8 / \ / \ 6 2 2 4   Constraints: 2 <= arr.length <= 40 1 <= arr[i] <= 15 It is guaranteed that the answer fits into a 32-bit signed integer (ie. it is less than 2^31).
class Solution: def mctFromLeafValues(self, arr: List[int]) -> int: return self.helper(arr, 0, len(arr) - 1, {}) def helper(self, arr, l, r, cache): if (l, r) in cache: return cache[l, r] if l >= r: return 0 res = float("inf") for i in range(l, r): rootVal = max(arr[l : i + 1]) * max(arr[i + 1 : r + 1]) res = min( res, rootVal + self.helper(arr, l, i, cache) + self.helper(arr, i + 1, r, cache), ) cache[l, r] = res return res
CLASS_DEF FUNC_DEF VAR VAR RETURN FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER DICT VAR FUNC_DEF IF VAR VAR VAR RETURN VAR VAR VAR IF VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR
Given an array arr of positive integers, consider all binary trees such that: Each node has either 0 or 2 children; The values of arr correspond to the values of each leaf in an in-order traversal of the tree.  (Recall that a node is a leaf if and only if it has 0 children.) The value of each non-leaf node is equal to the product of the largest leaf value in its left and right subtree respectively. Among all possible binary trees considered, return the smallest possible sum of the values of each non-leaf node.  It is guaranteed this sum fits into a 32-bit integer.   Example 1: Input: arr = [6,2,4] Output: 32 Explanation: There are two possible trees. The first has non-leaf node sum 36, and the second has non-leaf node sum 32. 24 24 / \ / \ 12 4 6 8 / \ / \ 6 2 2 4   Constraints: 2 <= arr.length <= 40 1 <= arr[i] <= 15 It is guaranteed that the answer fits into a 32-bit signed integer (ie. it is less than 2^31).
class Solution: def mctFromLeafValues(self, arr: List[int]) -> int: def compute(arr, lo, hi, dp): if dp[lo][hi] != -1: return dp[lo][hi] if hi == lo: dp[lo][hi] = 0, arr[lo] return 0, arr[lo] if hi == lo + 1: dp[lo][hi] = arr[lo] * arr[hi], max(arr[lo], arr[hi]) return arr[lo] * arr[hi], max(arr[lo], arr[hi]) k = float("inf") c = 0 for i in range(lo, hi): left = compute(arr, lo, i, dp) right = compute(arr, i + 1, hi, dp) t = left[0] + right[0] + left[1] * right[1] if t < k: k = t c = max(left[1], right[1]) dp[lo][hi] = k, c return k, c dp = [([-1] * len(arr)) for row in arr] x = compute(arr, 0, len(arr) - 1, dp) return x[0]
CLASS_DEF FUNC_DEF VAR VAR FUNC_DEF IF VAR VAR VAR NUMBER RETURN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR RETURN NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR 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 BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR RETURN VAR NUMBER VAR
Given an array arr of positive integers, consider all binary trees such that: Each node has either 0 or 2 children; The values of arr correspond to the values of each leaf in an in-order traversal of the tree.  (Recall that a node is a leaf if and only if it has 0 children.) The value of each non-leaf node is equal to the product of the largest leaf value in its left and right subtree respectively. Among all possible binary trees considered, return the smallest possible sum of the values of each non-leaf node.  It is guaranteed this sum fits into a 32-bit integer.   Example 1: Input: arr = [6,2,4] Output: 32 Explanation: There are two possible trees. The first has non-leaf node sum 36, and the second has non-leaf node sum 32. 24 24 / \ / \ 12 4 6 8 / \ / \ 6 2 2 4   Constraints: 2 <= arr.length <= 40 1 <= arr[i] <= 15 It is guaranteed that the answer fits into a 32-bit signed integer (ie. it is less than 2^31).
class Solution: def mctFromLeafValues(self, arr: List[int]) -> int: table = {} def compute(lo, hi): if hi - lo == 1: return 0 cost = float("inf") for i in range(lo + 1, hi): left_key, right_key = f"{lo}-{i}", f"{i}-{hi}" if table.get(left_key) is None: table[left_key] = compute(lo, i) if table.get(right_key) is None: table[right_key] = compute(i, hi) cost = min( cost, table[left_key] + table[right_key] + max(arr[lo:i]) * max(arr[i:hi]), ) return cost return compute(0, len(arr))
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR DICT FUNC_DEF IF BIN_OP VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR STRING VAR VAR STRING VAR IF FUNC_CALL VAR VAR NONE ASSIGN VAR VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR VAR NONE ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR VAR
Given an array arr of positive integers, consider all binary trees such that: Each node has either 0 or 2 children; The values of arr correspond to the values of each leaf in an in-order traversal of the tree.  (Recall that a node is a leaf if and only if it has 0 children.) The value of each non-leaf node is equal to the product of the largest leaf value in its left and right subtree respectively. Among all possible binary trees considered, return the smallest possible sum of the values of each non-leaf node.  It is guaranteed this sum fits into a 32-bit integer.   Example 1: Input: arr = [6,2,4] Output: 32 Explanation: There are two possible trees. The first has non-leaf node sum 36, and the second has non-leaf node sum 32. 24 24 / \ / \ 12 4 6 8 / \ / \ 6 2 2 4   Constraints: 2 <= arr.length <= 40 1 <= arr[i] <= 15 It is guaranteed that the answer fits into a 32-bit signed integer (ie. it is less than 2^31).
class Solution: def mctFromLeafValues(self, arr: List[int]) -> int: n = len(arr) mem = {} def dp(i, j): if i == j: return 0, arr[i] if i == j - 1: return arr[i] * arr[j], max(arr[i], arr[j]) else: if (i, j) in mem: return mem[i, j] pp = max(arr[i : j + 1]) minn = 10000000000.0 for k in range(i, j): l, lm = dp(i, k) r, rm = dp(k + 1, j) minn = min(minn, l + r + lm * rm) mem[i, j] = minn, pp return mem[i, j] return dp(0, n - 1)[0]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR RETURN NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR RETURN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR RETURN VAR VAR VAR RETURN FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR
Given an array arr of positive integers, consider all binary trees such that: Each node has either 0 or 2 children; The values of arr correspond to the values of each leaf in an in-order traversal of the tree.  (Recall that a node is a leaf if and only if it has 0 children.) The value of each non-leaf node is equal to the product of the largest leaf value in its left and right subtree respectively. Among all possible binary trees considered, return the smallest possible sum of the values of each non-leaf node.  It is guaranteed this sum fits into a 32-bit integer.   Example 1: Input: arr = [6,2,4] Output: 32 Explanation: There are two possible trees. The first has non-leaf node sum 36, and the second has non-leaf node sum 32. 24 24 / \ / \ 12 4 6 8 / \ / \ 6 2 2 4   Constraints: 2 <= arr.length <= 40 1 <= arr[i] <= 15 It is guaranteed that the answer fits into a 32-bit signed integer (ie. it is less than 2^31).
class Solution: def mctFromLeafValues(self, arr): maxi = [[(0) for _ in range(len(arr))] for _ in range(len(arr))] for i in range(len(arr)): maxi[i][i] = arr[i] for l in range(1, len(arr) - 1): for i in range(len(arr) - l): j = i + l maxi[i][j] = max(maxi[i][j - 1], maxi[i + 1][j]) dp = [[float("Inf") for i in range(len(arr))] for j in range(len(arr))] for i in range(len(arr)): dp[i][i] = arr[i] for l in range(1, len(arr)): for i in range(0, len(arr) - l): j = i + l for k in range(i, j): dp[i][j] = min( dp[i][j], (dp[i][k] if i < k else 0) + (dp[k + 1][j] if k + 1 < j else 0) + maxi[i][k] * maxi[k + 1][j], ) return dp[0][len(arr) - 1]
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER
Given an array arr of positive integers, consider all binary trees such that: Each node has either 0 or 2 children; The values of arr correspond to the values of each leaf in an in-order traversal of the tree.  (Recall that a node is a leaf if and only if it has 0 children.) The value of each non-leaf node is equal to the product of the largest leaf value in its left and right subtree respectively. Among all possible binary trees considered, return the smallest possible sum of the values of each non-leaf node.  It is guaranteed this sum fits into a 32-bit integer.   Example 1: Input: arr = [6,2,4] Output: 32 Explanation: There are two possible trees. The first has non-leaf node sum 36, and the second has non-leaf node sum 32. 24 24 / \ / \ 12 4 6 8 / \ / \ 6 2 2 4   Constraints: 2 <= arr.length <= 40 1 <= arr[i] <= 15 It is guaranteed that the answer fits into a 32-bit signed integer (ie. it is less than 2^31).
class Solution: def mctFromLeafValues(self, arr: List[int]) -> int: res = 0 while len(arr) > 1: mini_idx = arr.index(min(arr)) if 0 < mini_idx < len(arr) - 1: res += min(arr[mini_idx - 1], arr[mini_idx + 1]) * arr[mini_idx] else: res += arr[1 if mini_idx == 0 else mini_idx - 1] * arr[mini_idx] arr.pop(mini_idx) return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR
Given an array arr of positive integers, consider all binary trees such that: Each node has either 0 or 2 children; The values of arr correspond to the values of each leaf in an in-order traversal of the tree.  (Recall that a node is a leaf if and only if it has 0 children.) The value of each non-leaf node is equal to the product of the largest leaf value in its left and right subtree respectively. Among all possible binary trees considered, return the smallest possible sum of the values of each non-leaf node.  It is guaranteed this sum fits into a 32-bit integer.   Example 1: Input: arr = [6,2,4] Output: 32 Explanation: There are two possible trees. The first has non-leaf node sum 36, and the second has non-leaf node sum 32. 24 24 / \ / \ 12 4 6 8 / \ / \ 6 2 2 4   Constraints: 2 <= arr.length <= 40 1 <= arr[i] <= 15 It is guaranteed that the answer fits into a 32-bit signed integer (ie. it is less than 2^31).
class Solution: def mctFromLeafValues(self, arr: List[int]) -> int: n = len(arr) m = [None] * n for i in range(len(m)): m[i] = [float("-inf")] * n m[i][i] = arr[i] for l in range(2, n + 1): for i in range(0, n - l + 1): j = i + l - 1 mid = (i + j) // 2 m[i][j] = max(m[i][j], m[i][mid], m[mid + 1][j]) f = [None] * n for i in range(len(f)): f[i] = [float("inf")] * n f[i][i] = 0 for l in range(2, n + 1): for i in range(0, n - l + 1): j = i + l - 1 for k in range(i, j): f[i][j] = min( f[i][j], f[i][k] + f[k + 1][j] + m[i][k] * m[k + 1][j] ) return f[0][n - 1]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NONE VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP LIST FUNC_CALL VAR STRING VAR ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP LIST NONE VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP LIST FUNC_CALL VAR STRING VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR NUMBER BIN_OP VAR NUMBER VAR
Given an array arr of positive integers, consider all binary trees such that: Each node has either 0 or 2 children; The values of arr correspond to the values of each leaf in an in-order traversal of the tree.  (Recall that a node is a leaf if and only if it has 0 children.) The value of each non-leaf node is equal to the product of the largest leaf value in its left and right subtree respectively. Among all possible binary trees considered, return the smallest possible sum of the values of each non-leaf node.  It is guaranteed this sum fits into a 32-bit integer.   Example 1: Input: arr = [6,2,4] Output: 32 Explanation: There are two possible trees. The first has non-leaf node sum 36, and the second has non-leaf node sum 32. 24 24 / \ / \ 12 4 6 8 / \ / \ 6 2 2 4   Constraints: 2 <= arr.length <= 40 1 <= arr[i] <= 15 It is guaranteed that the answer fits into a 32-bit signed integer (ie. it is less than 2^31).
class Solution: def mctFromLeafValues(self, arr: List[int]) -> int: n = len(arr) max_dp = [[(0) for i in range(n)] for j in range(n)] for i in range(n): temp = arr[i] for j in range(i, n): temp = max(temp, arr[j]) max_dp[i][j] = temp dp = [[(-1) for i in range(n)] for j in range(n)] for i in range(n): dp[i][i] = 0 for i in range(n - 1): dp[i][i + 1] = arr[i] * arr[i + 1] for k in range(2, n): for i in range(n): j = i + k if j >= n: break dp[i][j] = dp[i][i] + dp[i + 1][j] + arr[i] * max_dp[i + 1][j] for s in range(i + 1, j): dp[i][j] = min( dp[i][j], dp[i][s] + dp[s + 1][j] + max_dp[i][s] * max_dp[s + 1][j], ) return dp[0][-1]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR RETURN VAR NUMBER NUMBER VAR
Given an array arr of positive integers, consider all binary trees such that: Each node has either 0 or 2 children; The values of arr correspond to the values of each leaf in an in-order traversal of the tree.  (Recall that a node is a leaf if and only if it has 0 children.) The value of each non-leaf node is equal to the product of the largest leaf value in its left and right subtree respectively. Among all possible binary trees considered, return the smallest possible sum of the values of each non-leaf node.  It is guaranteed this sum fits into a 32-bit integer.   Example 1: Input: arr = [6,2,4] Output: 32 Explanation: There are two possible trees. The first has non-leaf node sum 36, and the second has non-leaf node sum 32. 24 24 / \ / \ 12 4 6 8 / \ / \ 6 2 2 4   Constraints: 2 <= arr.length <= 40 1 <= arr[i] <= 15 It is guaranteed that the answer fits into a 32-bit signed integer (ie. it is less than 2^31).
class Solution: def mctFromLeafValues(self, arr: List[int]) -> int: n = len(arr) dp = [[[0, 0] for _1 in range(n)] for _2 in range(n)] for i in range(n): dp[i][i] = [arr[i], 0] for diff in range(1, n): for i in range(n - diff): temp_min_ = 10**40 root = None for j in range(i, i + diff): sum_ = ( dp[i][j][1] + dp[j + 1][i + diff][1] + dp[i][j][0] * dp[j + 1][i + diff][0] ) if sum_ < temp_min_: temp_min_ = sum_ root = max(dp[i][j][0], dp[j + 1][i + diff][0]) dp[i][i + diff] = [root, temp_min_] return dp[0][-1][1]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR LIST VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NONE FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR LIST VAR VAR RETURN VAR NUMBER NUMBER NUMBER VAR
Given an array arr of positive integers, consider all binary trees such that: Each node has either 0 or 2 children; The values of arr correspond to the values of each leaf in an in-order traversal of the tree.  (Recall that a node is a leaf if and only if it has 0 children.) The value of each non-leaf node is equal to the product of the largest leaf value in its left and right subtree respectively. Among all possible binary trees considered, return the smallest possible sum of the values of each non-leaf node.  It is guaranteed this sum fits into a 32-bit integer.   Example 1: Input: arr = [6,2,4] Output: 32 Explanation: There are two possible trees. The first has non-leaf node sum 36, and the second has non-leaf node sum 32. 24 24 / \ / \ 12 4 6 8 / \ / \ 6 2 2 4   Constraints: 2 <= arr.length <= 40 1 <= arr[i] <= 15 It is guaranteed that the answer fits into a 32-bit signed integer (ie. it is less than 2^31).
class Solution: def mctFromLeafValues(self, arr: List[int]) -> int: n = len(arr) dp = [[(0) for j in range(n)] for i in range(n)] for i in range(n - 2, -1, -1): dp[i][i + 1] = arr[i] * arr[i + 1] for j in range(i + 2, n): dp[i][j] = float("inf") for k in range(i, j): dp[i][j] = min( dp[i][j], dp[i][k] + dp[k + 1][j] + max(arr[i : k + 1]) * max(arr[k + 1 : j + 1]), ) return dp[0][n - 1]
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR NUMBER BIN_OP VAR NUMBER VAR
Given an array arr of positive integers, consider all binary trees such that: Each node has either 0 or 2 children; The values of arr correspond to the values of each leaf in an in-order traversal of the tree.  (Recall that a node is a leaf if and only if it has 0 children.) The value of each non-leaf node is equal to the product of the largest leaf value in its left and right subtree respectively. Among all possible binary trees considered, return the smallest possible sum of the values of each non-leaf node.  It is guaranteed this sum fits into a 32-bit integer.   Example 1: Input: arr = [6,2,4] Output: 32 Explanation: There are two possible trees. The first has non-leaf node sum 36, and the second has non-leaf node sum 32. 24 24 / \ / \ 12 4 6 8 / \ / \ 6 2 2 4   Constraints: 2 <= arr.length <= 40 1 <= arr[i] <= 15 It is guaranteed that the answer fits into a 32-bit signed integer (ie. it is less than 2^31).
class Solution: def mctFromLeafValues(self, arr: List[int]) -> int: if not arr: return 0 res = [] while len(arr) > 1: temp_res = [] temp_res = [(arr[i] * arr[i + 1]) for i in range(len(arr) - 1)] idx = temp_res.index(min(temp_res)) res.append(temp_res[idx]) arr.pop(idx if arr[idx] < arr[idx + 1] else idx + 1) return sum(res)
CLASS_DEF FUNC_DEF VAR VAR IF VAR RETURN NUMBER ASSIGN VAR LIST WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER RETURN FUNC_CALL VAR VAR VAR
Given an array arr of positive integers, consider all binary trees such that: Each node has either 0 or 2 children; The values of arr correspond to the values of each leaf in an in-order traversal of the tree.  (Recall that a node is a leaf if and only if it has 0 children.) The value of each non-leaf node is equal to the product of the largest leaf value in its left and right subtree respectively. Among all possible binary trees considered, return the smallest possible sum of the values of each non-leaf node.  It is guaranteed this sum fits into a 32-bit integer.   Example 1: Input: arr = [6,2,4] Output: 32 Explanation: There are two possible trees. The first has non-leaf node sum 36, and the second has non-leaf node sum 32. 24 24 / \ / \ 12 4 6 8 / \ / \ 6 2 2 4   Constraints: 2 <= arr.length <= 40 1 <= arr[i] <= 15 It is guaranteed that the answer fits into a 32-bit signed integer (ie. it is less than 2^31).
class Solution: def __init__(self): self.dict = {} def mctFromLeafValues(self, arr: List[int]) -> int: return self.mctHelp(arr, 0, len(arr)) def mctHelp(self, arr, a, b): if b - a == 1: return 0 if (a, b) not in self.dict: low, idx = 2**30, -1 for i in range(a + 1, b): res = self.mctHelp(arr, a, i) + self.mctHelp(arr, i, b) if res < low: low = res idx = i low += max(arr[a:idx]) * max(arr[idx:b]) self.dict[a, b] = low return self.dict[a, b]
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF VAR VAR RETURN FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR FUNC_DEF IF BIN_OP VAR VAR NUMBER RETURN NUMBER IF VAR VAR VAR ASSIGN VAR VAR BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR
Given an array arr of positive integers, consider all binary trees such that: Each node has either 0 or 2 children; The values of arr correspond to the values of each leaf in an in-order traversal of the tree.  (Recall that a node is a leaf if and only if it has 0 children.) The value of each non-leaf node is equal to the product of the largest leaf value in its left and right subtree respectively. Among all possible binary trees considered, return the smallest possible sum of the values of each non-leaf node.  It is guaranteed this sum fits into a 32-bit integer.   Example 1: Input: arr = [6,2,4] Output: 32 Explanation: There are two possible trees. The first has non-leaf node sum 36, and the second has non-leaf node sum 32. 24 24 / \ / \ 12 4 6 8 / \ / \ 6 2 2 4   Constraints: 2 <= arr.length <= 40 1 <= arr[i] <= 15 It is guaranteed that the answer fits into a 32-bit signed integer (ie. it is less than 2^31).
class Solution: def mctFromLeafValues(self, arr: List[int]) -> int: return self.mctHelp(arr, 0, len(arr), {}) def mctHelp(self, arr, a, b, dic): if b - a == 1: dic[a, b] = 0 if (a, b) not in dic: low = 2**31 for i in range(a + 1, b): res = ( max(arr[a:i]) * max(arr[i:b]) + self.mctHelp(arr, a, i, dic) + self.mctHelp(arr, i, b, dic) ) low = min(low, res) dic[a, b] = low return dic[a, b]
CLASS_DEF FUNC_DEF VAR VAR RETURN FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR DICT VAR FUNC_DEF IF BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR VAR VAR
Given an array arr of positive integers, consider all binary trees such that: Each node has either 0 or 2 children; The values of arr correspond to the values of each leaf in an in-order traversal of the tree.  (Recall that a node is a leaf if and only if it has 0 children.) The value of each non-leaf node is equal to the product of the largest leaf value in its left and right subtree respectively. Among all possible binary trees considered, return the smallest possible sum of the values of each non-leaf node.  It is guaranteed this sum fits into a 32-bit integer.   Example 1: Input: arr = [6,2,4] Output: 32 Explanation: There are two possible trees. The first has non-leaf node sum 36, and the second has non-leaf node sum 32. 24 24 / \ / \ 12 4 6 8 / \ / \ 6 2 2 4   Constraints: 2 <= arr.length <= 40 1 <= arr[i] <= 15 It is guaranteed that the answer fits into a 32-bit signed integer (ie. it is less than 2^31).
class Solution: def mctFromLeafValues(self, A: List[int]) -> int: res = 0 while len(A) > 1: i = A.index(min(A)) neighbors = [] if i - 1 >= 0: neighbors.append(A[i - 1]) if i + 1 < len(A): neighbors.append(A[i + 1]) res += min(neighbors) * A.pop(i) return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR