description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
You have a garden with n flowers lined up in a row. The height of ith flower is a_{i} units. You will water them for k days. In one day you can water w continuous flowers (you can do this only once in a single day). Whenever you water a flower its height increases by 1 unit. You need to maximize the height of the smallest flower all the time. Example 1: Input: n=6 k=2 w=3 a[]={2,2,2,2,1,1} Output: 2 Explanation: Water last three flowers for first day & first three flowers for second day.The new heights will be {3,3,3,3,2,2} Example 2: Input: n=2 k=5 w=1 a[]={5,8} Output: 9 Explanation: For the first four days water the first flower then water the last flower once. Your Task: You don't need to read input or print anything. Your task is to complete the function maximizeMinHeight() which takes the array a[], its size N, integer K, and an integer W as input parameters and returns the maximum height possible for the smallest flower. Expected Time Complexity: O(NLogN) Expected Space Complexity: O(N) Constraints: 1 <= n<= 10^{5} 1<=w<=n 1<=k<=10^{5} 1 <= a[i] <= 10^{9}
class Solution: def maximizeMinHeight(self, n, k, w, a): i = 1 j = 10**6 ans = 10**9 while i <= j: mid = i + (j - i) // 2 arr = [0] * (n + 1) curr = 0 moves = 0 for ii in range(n): if ii - w >= 0: curr -= arr[ii - w] if a[ii] + curr <= mid: arr[ii] = mid - a[ii] - curr curr += arr[ii] moves += arr[ii] if moves <= k: ans = mid i = mid + 1 else: j = mid - 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
You have a garden with n flowers lined up in a row. The height of ith flower is a_{i} units. You will water them for k days. In one day you can water w continuous flowers (you can do this only once in a single day). Whenever you water a flower its height increases by 1 unit. You need to maximize the height of the smallest flower all the time. Example 1: Input: n=6 k=2 w=3 a[]={2,2,2,2,1,1} Output: 2 Explanation: Water last three flowers for first day & first three flowers for second day.The new heights will be {3,3,3,3,2,2} Example 2: Input: n=2 k=5 w=1 a[]={5,8} Output: 9 Explanation: For the first four days water the first flower then water the last flower once. Your Task: You don't need to read input or print anything. Your task is to complete the function maximizeMinHeight() which takes the array a[], its size N, integer K, and an integer W as input parameters and returns the maximum height possible for the smallest flower. Expected Time Complexity: O(NLogN) Expected Space Complexity: O(N) Constraints: 1 <= n<= 10^{5} 1<=w<=n 1<=k<=10^{5} 1 <= a[i] <= 10^{9}
class Solution: def maximizeMinHeight(self, n, k, w, a): diff = [0] * n mn = a[0] for i in range(1, n): diff[i] = a[i] - a[i - 1] mn = min(mn, a[i]) mx = mn + k ans = 0 def isValid(k, w, tmp, a): for i in range(n): if a[i] < tmp: if tmp - a[i] > k: return False st = i inc = tmp - a[i] k -= inc if st + w >= n: st = max(0, n - w) for j in range(st, min(n, st + w)): a[j] += inc return True while mn <= mx: tmp = (mn + mx) // 2 if isValid(k, w, tmp, a[:]): ans = tmp mn = tmp + 1 else: mx = tmp - 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF BIN_OP VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR RETURN NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
You have a garden with n flowers lined up in a row. The height of ith flower is a_{i} units. You will water them for k days. In one day you can water w continuous flowers (you can do this only once in a single day). Whenever you water a flower its height increases by 1 unit. You need to maximize the height of the smallest flower all the time. Example 1: Input: n=6 k=2 w=3 a[]={2,2,2,2,1,1} Output: 2 Explanation: Water last three flowers for first day & first three flowers for second day.The new heights will be {3,3,3,3,2,2} Example 2: Input: n=2 k=5 w=1 a[]={5,8} Output: 9 Explanation: For the first four days water the first flower then water the last flower once. Your Task: You don't need to read input or print anything. Your task is to complete the function maximizeMinHeight() which takes the array a[], its size N, integer K, and an integer W as input parameters and returns the maximum height possible for the smallest flower. Expected Time Complexity: O(NLogN) Expected Space Complexity: O(N) Constraints: 1 <= n<= 10^{5} 1<=w<=n 1<=k<=10^{5} 1 <= a[i] <= 10^{9}
class Solution: def helper(self, n, k, w, a, req): res, needed = 0, 0 additive = [(0) for _ in range(n)] for i in range(n): if i > 0: additive[i] += additive[i - 1] if a[i] + additive[i] < req: needed = req - a[i] - additive[i] res += needed additive[i] += needed if i < n - w: additive[i + w] -= needed return res <= k def maximizeMinHeight(self, n, k, w, a): left, right = 1, 10000000000 mid = left + (right - left) // 2 while left < mid: if self.helper(n, k, w, a, mid): left = mid else: right = mid mid = left + (right - left) // 2 return mid
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR
You have a garden with n flowers lined up in a row. The height of ith flower is a_{i} units. You will water them for k days. In one day you can water w continuous flowers (you can do this only once in a single day). Whenever you water a flower its height increases by 1 unit. You need to maximize the height of the smallest flower all the time. Example 1: Input: n=6 k=2 w=3 a[]={2,2,2,2,1,1} Output: 2 Explanation: Water last three flowers for first day & first three flowers for second day.The new heights will be {3,3,3,3,2,2} Example 2: Input: n=2 k=5 w=1 a[]={5,8} Output: 9 Explanation: For the first four days water the first flower then water the last flower once. Your Task: You don't need to read input or print anything. Your task is to complete the function maximizeMinHeight() which takes the array a[], its size N, integer K, and an integer W as input parameters and returns the maximum height possible for the smallest flower. Expected Time Complexity: O(NLogN) Expected Space Complexity: O(N) Constraints: 1 <= n<= 10^{5} 1<=w<=n 1<=k<=10^{5} 1 <= a[i] <= 10^{9}
class Solution: def maximizeMinHeight(self, n, k, w, a): def func(arr): dibba = [0] * n ad, nt = 0, k for i in range(n): ad += dibba[i] v = a[i] + ad if v < arr: res = arr - v dibba[i] += res if i + w < n: dibba[i + w] -= res ad += res nt -= res if nt < 0: return False return True L = min(a) R = L + k + 1 while L < R: m = (L + R) // 2 if func(m): L = m + 1 else: R = m return L - 1
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN BIN_OP VAR NUMBER
You have a garden with n flowers lined up in a row. The height of ith flower is a_{i} units. You will water them for k days. In one day you can water w continuous flowers (you can do this only once in a single day). Whenever you water a flower its height increases by 1 unit. You need to maximize the height of the smallest flower all the time. Example 1: Input: n=6 k=2 w=3 a[]={2,2,2,2,1,1} Output: 2 Explanation: Water last three flowers for first day & first three flowers for second day.The new heights will be {3,3,3,3,2,2} Example 2: Input: n=2 k=5 w=1 a[]={5,8} Output: 9 Explanation: For the first four days water the first flower then water the last flower once. Your Task: You don't need to read input or print anything. Your task is to complete the function maximizeMinHeight() which takes the array a[], its size N, integer K, and an integer W as input parameters and returns the maximum height possible for the smallest flower. Expected Time Complexity: O(NLogN) Expected Space Complexity: O(N) Constraints: 1 <= n<= 10^{5} 1<=w<=n 1<=k<=10^{5} 1 <= a[i] <= 10^{9}
class Solution: def maximizeMinHeight(self, n, k, w, a): def check(lvl): days = 0 fls = [0] * (n + 1) diff = max(0, lvl - a[0]) fls[0] = diff fls[w] -= diff days = diff for i in range(1, n): fls[i] += fls[i - 1] cur_ht = a[i] + fls[i] diff = max(0, lvl - cur_ht) days += diff fls[i] += diff if i + w < n: fls[i + w] -= diff return days <= k l = 0 h = 1000000000 res = -1 while l <= h: mid = (l + h) // 2 if check(mid): l = mid + 1 res = mid else: h = mid - 1 return res
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
You have a garden with n flowers lined up in a row. The height of ith flower is a_{i} units. You will water them for k days. In one day you can water w continuous flowers (you can do this only once in a single day). Whenever you water a flower its height increases by 1 unit. You need to maximize the height of the smallest flower all the time. Example 1: Input: n=6 k=2 w=3 a[]={2,2,2,2,1,1} Output: 2 Explanation: Water last three flowers for first day & first three flowers for second day.The new heights will be {3,3,3,3,2,2} Example 2: Input: n=2 k=5 w=1 a[]={5,8} Output: 9 Explanation: For the first four days water the first flower then water the last flower once. Your Task: You don't need to read input or print anything. Your task is to complete the function maximizeMinHeight() which takes the array a[], its size N, integer K, and an integer W as input parameters and returns the maximum height possible for the smallest flower. Expected Time Complexity: O(NLogN) Expected Space Complexity: O(N) Constraints: 1 <= n<= 10^{5} 1<=w<=n 1<=k<=10^{5} 1 <= a[i] <= 10^{9}
class Solution: def maximizeMinHeight(self, n, k, w, a): def check(t, mid): lst = arr[:] for i in range(n): if lst[i] < mid: temp = mid - lst[i] if temp > t: return False t -= temp lst[i] = mid for j in range(i + 1, min(n, i + w)): lst[j] += temp return True lo, hi = min(arr), min(arr) + k res = None while lo <= hi: mid = (lo + hi) // 2 if check(k, mid): res = mid lo = mid + 1 else: hi = mid - 1 return res
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR RETURN NUMBER VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR NONE WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
You have a garden with n flowers lined up in a row. The height of ith flower is a_{i} units. You will water them for k days. In one day you can water w continuous flowers (you can do this only once in a single day). Whenever you water a flower its height increases by 1 unit. You need to maximize the height of the smallest flower all the time. Example 1: Input: n=6 k=2 w=3 a[]={2,2,2,2,1,1} Output: 2 Explanation: Water last three flowers for first day & first three flowers for second day.The new heights will be {3,3,3,3,2,2} Example 2: Input: n=2 k=5 w=1 a[]={5,8} Output: 9 Explanation: For the first four days water the first flower then water the last flower once. Your Task: You don't need to read input or print anything. Your task is to complete the function maximizeMinHeight() which takes the array a[], its size N, integer K, and an integer W as input parameters and returns the maximum height possible for the smallest flower. Expected Time Complexity: O(NLogN) Expected Space Complexity: O(N) Constraints: 1 <= n<= 10^{5} 1<=w<=n 1<=k<=10^{5} 1 <= a[i] <= 10^{9}
class Solution: def maximizeMinHeight(self, n, k, w, a): if n == 1: return a[0] + k minh, maxh = a[0], a[0] da = [0] for i in range(1, n): minh = min(a[i], minh) maxh = max(a[i], maxh) da.append(a[i] - a[i - 1]) ans = minh maxh += k while minh <= maxh: mid = (minh + maxh) // 2 da = [0] for i in range(1, n): da.append(a[i] - a[i - 1]) cumsum = a[0] k_left = k flag = True for i in range(n): cumsum += da[i] if cumsum >= mid: continue if cumsum + k_left < mid: flag = False break step = mid - cumsum k_left -= step cumsum += step if i + w < n: da[i + w] -= step if flag: minh = mid + 1 ans = max(ans, mid) else: maxh = mid - 1 return ans
CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR IF VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
You have a garden with n flowers lined up in a row. The height of ith flower is a_{i} units. You will water them for k days. In one day you can water w continuous flowers (you can do this only once in a single day). Whenever you water a flower its height increases by 1 unit. You need to maximize the height of the smallest flower all the time. Example 1: Input: n=6 k=2 w=3 a[]={2,2,2,2,1,1} Output: 2 Explanation: Water last three flowers for first day & first three flowers for second day.The new heights will be {3,3,3,3,2,2} Example 2: Input: n=2 k=5 w=1 a[]={5,8} Output: 9 Explanation: For the first four days water the first flower then water the last flower once. Your Task: You don't need to read input or print anything. Your task is to complete the function maximizeMinHeight() which takes the array a[], its size N, integer K, and an integer W as input parameters and returns the maximum height possible for the smallest flower. Expected Time Complexity: O(NLogN) Expected Space Complexity: O(N) Constraints: 1 <= n<= 10^{5} 1<=w<=n 1<=k<=10^{5} 1 <= a[i] <= 10^{9}
class Solution: def possible(self, mnheight, k, w, a): days = 0 prefsum = 0 dp = [0] * (n + 1) for i in range(n): prefsum += dp[i] height = a[i] + prefsum if height < mnheight: add = mnheight - height days += add prefsum += add dp[i] += add dp[min(n, i + w)] -= add return days <= k def maximizeMinHeight(self, n, k, w, a): l = min(a) + 1 h = max(a) + k target = l - 1 while l <= h: temp = (l + h) // 2 if self.possible(temp, k, w, a): target = temp l = temp + 1 else: h = temp - 1 return target
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
You have a garden with n flowers lined up in a row. The height of ith flower is a_{i} units. You will water them for k days. In one day you can water w continuous flowers (you can do this only once in a single day). Whenever you water a flower its height increases by 1 unit. You need to maximize the height of the smallest flower all the time. Example 1: Input: n=6 k=2 w=3 a[]={2,2,2,2,1,1} Output: 2 Explanation: Water last three flowers for first day & first three flowers for second day.The new heights will be {3,3,3,3,2,2} Example 2: Input: n=2 k=5 w=1 a[]={5,8} Output: 9 Explanation: For the first four days water the first flower then water the last flower once. Your Task: You don't need to read input or print anything. Your task is to complete the function maximizeMinHeight() which takes the array a[], its size N, integer K, and an integer W as input parameters and returns the maximum height possible for the smallest flower. Expected Time Complexity: O(NLogN) Expected Space Complexity: O(N) Constraints: 1 <= n<= 10^{5} 1<=w<=n 1<=k<=10^{5} 1 <= a[i] <= 10^{9}
class Solution: def maximizeMinHeight(self, n, k, w, a): l, h = min(a), 10**15 def solve(mid): pre_sum = [0] * n cnt = 0 for i in range(n): if i > 0: pre_sum[i] += pre_sum[i - 1] if a[i] + pre_sum[i] < mid: gg = mid - (a[i] + pre_sum[i]) cnt += gg pre_sum[i] += gg if i + w < n: pre_sum[i + w] -= gg return cnt <= k while l < h: mid = l + h + 1 >> 1 if solve(mid): l = mid else: h = mid - 1 return l
CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR RETURN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
You have a garden with n flowers lined up in a row. The height of ith flower is a_{i} units. You will water them for k days. In one day you can water w continuous flowers (you can do this only once in a single day). Whenever you water a flower its height increases by 1 unit. You need to maximize the height of the smallest flower all the time. Example 1: Input: n=6 k=2 w=3 a[]={2,2,2,2,1,1} Output: 2 Explanation: Water last three flowers for first day & first three flowers for second day.The new heights will be {3,3,3,3,2,2} Example 2: Input: n=2 k=5 w=1 a[]={5,8} Output: 9 Explanation: For the first four days water the first flower then water the last flower once. Your Task: You don't need to read input or print anything. Your task is to complete the function maximizeMinHeight() which takes the array a[], its size N, integer K, and an integer W as input parameters and returns the maximum height possible for the smallest flower. Expected Time Complexity: O(NLogN) Expected Space Complexity: O(N) Constraints: 1 <= n<= 10^{5} 1<=w<=n 1<=k<=10^{5} 1 <= a[i] <= 10^{9}
class Solution: def maximizeMinHeight(self, n, k, w, a): def helper(n, k, w, a, mid): for i in range(n): if a[i] < mid: temp = mid - a[i] if temp > k: return False k -= temp j = i while j < n and j < i + w: a[j] += temp j += 1 return True mini = min(a) maxi = mini + k ans = 0 while mini <= maxi: mid = (mini + maxi) // 2 if helper(n, k, w, a[:], mid): mini = mid + 1 ans = mid else: maxi = mid - 1 return ans
CLASS_DEF FUNC_DEF FUNC_DEF FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR RETURN NUMBER VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
You have a garden with n flowers lined up in a row. The height of ith flower is a_{i} units. You will water them for k days. In one day you can water w continuous flowers (you can do this only once in a single day). Whenever you water a flower its height increases by 1 unit. You need to maximize the height of the smallest flower all the time. Example 1: Input: n=6 k=2 w=3 a[]={2,2,2,2,1,1} Output: 2 Explanation: Water last three flowers for first day & first three flowers for second day.The new heights will be {3,3,3,3,2,2} Example 2: Input: n=2 k=5 w=1 a[]={5,8} Output: 9 Explanation: For the first four days water the first flower then water the last flower once. Your Task: You don't need to read input or print anything. Your task is to complete the function maximizeMinHeight() which takes the array a[], its size N, integer K, and an integer W as input parameters and returns the maximum height possible for the smallest flower. Expected Time Complexity: O(NLogN) Expected Space Complexity: O(N) Constraints: 1 <= n<= 10^{5} 1<=w<=n 1<=k<=10^{5} 1 <= a[i] <= 10^{9}
class Solution: def check(self, arr, k, w, mid): ar = list(arr) for i in range(len(arr)): if ar[i] < mid: rem = mid - ar[i] if rem > k: return False k -= rem for j in range(i, min(i + w, len(ar))): ar[j] += rem return True def bin_search(self, start, end, arr, k, w): if start <= end: mid = (end + start) // 2 if self.check(arr, k, w, mid): return self.bin_search(mid + 1, end, arr, k, w) else: return self.bin_search(start, mid - 1, arr, k, w) return start - 1 def maximizeMinHeight(self, n, k, w, a): return self.bin_search(min(a), min(a) + k, a, k, w)
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR RETURN NUMBER VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN NUMBER FUNC_DEF IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR RETURN BIN_OP VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR
You have a garden with n flowers lined up in a row. The height of ith flower is a_{i} units. You will water them for k days. In one day you can water w continuous flowers (you can do this only once in a single day). Whenever you water a flower its height increases by 1 unit. You need to maximize the height of the smallest flower all the time. Example 1: Input: n=6 k=2 w=3 a[]={2,2,2,2,1,1} Output: 2 Explanation: Water last three flowers for first day & first three flowers for second day.The new heights will be {3,3,3,3,2,2} Example 2: Input: n=2 k=5 w=1 a[]={5,8} Output: 9 Explanation: For the first four days water the first flower then water the last flower once. Your Task: You don't need to read input or print anything. Your task is to complete the function maximizeMinHeight() which takes the array a[], its size N, integer K, and an integer W as input parameters and returns the maximum height possible for the smallest flower. Expected Time Complexity: O(NLogN) Expected Space Complexity: O(N) Constraints: 1 <= n<= 10^{5} 1<=w<=n 1<=k<=10^{5} 1 <= a[i] <= 10^{9}
class Solution: def maximizeMinHeight(self, n, k, w, a): mn = min(a) mx = mn + k + 1 def f(ht): days = 0 curr_ht = a[0] flower = [0] * (n + 1) diff = max(0, ht - curr_ht) flower[0] += diff days += diff flower[w] -= diff for i in range(1, n): flower[i] += flower[i - 1] curr_ht = a[i] + flower[i] diff = max(0, ht - curr_ht) flower[i] += diff days += diff if i + w < n: flower[i + w] -= diff return days <= k while mn < mx: ht = (mn + mx) // 2 if not f(ht): mx = ht else: mn = ht + 1 return mn - 1
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR RETURN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER
You have a garden with n flowers lined up in a row. The height of ith flower is a_{i} units. You will water them for k days. In one day you can water w continuous flowers (you can do this only once in a single day). Whenever you water a flower its height increases by 1 unit. You need to maximize the height of the smallest flower all the time. Example 1: Input: n=6 k=2 w=3 a[]={2,2,2,2,1,1} Output: 2 Explanation: Water last three flowers for first day & first three flowers for second day.The new heights will be {3,3,3,3,2,2} Example 2: Input: n=2 k=5 w=1 a[]={5,8} Output: 9 Explanation: For the first four days water the first flower then water the last flower once. Your Task: You don't need to read input or print anything. Your task is to complete the function maximizeMinHeight() which takes the array a[], its size N, integer K, and an integer W as input parameters and returns the maximum height possible for the smallest flower. Expected Time Complexity: O(NLogN) Expected Space Complexity: O(N) Constraints: 1 <= n<= 10^{5} 1<=w<=n 1<=k<=10^{5} 1 <= a[i] <= 10^{9}
class Solution: def height_is_achievable(self, k, w, a, mid): prefix = [0] * len(a) for i in range(len(a)): if a[i] + prefix[i] >= mid: continue diff = mid - a[i] - prefix[i] k -= diff if k < 0: return False for index in range(i, i + w): if index >= len(prefix): break prefix[index] += diff return True def maximizeMinHeight(self, n, k, w, a): max_height_achievable = max(a) + k min_height = min(a) while min_height <= max_height_achievable: mid = (max_height_achievable + min_height) // 2 if self.height_is_achievable(k, w, a, mid): min_height = mid + 1 else: max_height_achievable = mid - 1 return max_height_achievable
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR IF VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN NUMBER FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
You have a garden with n flowers lined up in a row. The height of ith flower is a_{i} units. You will water them for k days. In one day you can water w continuous flowers (you can do this only once in a single day). Whenever you water a flower its height increases by 1 unit. You need to maximize the height of the smallest flower all the time. Example 1: Input: n=6 k=2 w=3 a[]={2,2,2,2,1,1} Output: 2 Explanation: Water last three flowers for first day & first three flowers for second day.The new heights will be {3,3,3,3,2,2} Example 2: Input: n=2 k=5 w=1 a[]={5,8} Output: 9 Explanation: For the first four days water the first flower then water the last flower once. Your Task: You don't need to read input or print anything. Your task is to complete the function maximizeMinHeight() which takes the array a[], its size N, integer K, and an integer W as input parameters and returns the maximum height possible for the smallest flower. Expected Time Complexity: O(NLogN) Expected Space Complexity: O(N) Constraints: 1 <= n<= 10^{5} 1<=w<=n 1<=k<=10^{5} 1 <= a[i] <= 10^{9}
class Solution: def check(self, diff, k2, w, t2): f = 0 for i in range(len(diff)): diff[i] += f f = diff[i] if diff[i] >= t2: continue k2 -= t2 - diff[i] if k2 < 0: return 0 if i + w < len(diff): diff[i + w] -= t2 - diff[i] f = t2 return 1 def maximizeMinHeight(self, n, k, w, a): dif = [0] * n dif[0] = a[0] mi = a[0] for i in range(1, n): dif[i] = a[i] - a[i - 1] mi = min(a[i], mi) ma = mi + k ans = 0 while mi <= ma: t = (mi + ma) // 2 if self.check(dif.copy(), k, w, t): ans = t mi = t + 1 else: ma = t - 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR VAR IF VAR NUMBER RETURN NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR RETURN NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
You have a garden with n flowers lined up in a row. The height of ith flower is a_{i} units. You will water them for k days. In one day you can water w continuous flowers (you can do this only once in a single day). Whenever you water a flower its height increases by 1 unit. You need to maximize the height of the smallest flower all the time. Example 1: Input: n=6 k=2 w=3 a[]={2,2,2,2,1,1} Output: 2 Explanation: Water last three flowers for first day & first three flowers for second day.The new heights will be {3,3,3,3,2,2} Example 2: Input: n=2 k=5 w=1 a[]={5,8} Output: 9 Explanation: For the first four days water the first flower then water the last flower once. Your Task: You don't need to read input or print anything. Your task is to complete the function maximizeMinHeight() which takes the array a[], its size N, integer K, and an integer W as input parameters and returns the maximum height possible for the smallest flower. Expected Time Complexity: O(NLogN) Expected Space Complexity: O(N) Constraints: 1 <= n<= 10^{5} 1<=w<=n 1<=k<=10^{5} 1 <= a[i] <= 10^{9}
class Solution: def isValid(self, n, l, k, w, m): pre = [0] * (n + 1) temp = 0 for i in range(0, n): pre[i] += pre[i - 1] val = pre[i] + l[i] if val < m: pre[i] += m - val pre[min(n, i + w)] -= m - val temp += m - val return temp <= k def maximizeMinHeight(self, n, k, w, a): f = min(a) r = max(a) + k while f <= r: mid = (f + r) // 2 if self.isValid(n, a, k, w, mid): f = mid + 1 else: r = mid - 1 return r
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
You have a garden with n flowers lined up in a row. The height of ith flower is a_{i} units. You will water them for k days. In one day you can water w continuous flowers (you can do this only once in a single day). Whenever you water a flower its height increases by 1 unit. You need to maximize the height of the smallest flower all the time. Example 1: Input: n=6 k=2 w=3 a[]={2,2,2,2,1,1} Output: 2 Explanation: Water last three flowers for first day & first three flowers for second day.The new heights will be {3,3,3,3,2,2} Example 2: Input: n=2 k=5 w=1 a[]={5,8} Output: 9 Explanation: For the first four days water the first flower then water the last flower once. Your Task: You don't need to read input or print anything. Your task is to complete the function maximizeMinHeight() which takes the array a[], its size N, integer K, and an integer W as input parameters and returns the maximum height possible for the smallest flower. Expected Time Complexity: O(NLogN) Expected Space Complexity: O(N) Constraints: 1 <= n<= 10^{5} 1<=w<=n 1<=k<=10^{5} 1 <= a[i] <= 10^{9}
class Solution: def maximizeMinHeight(self, n, k, w, ac): low = 1 high = 10**14 while low < high: mid = (low + high) // 2 l = [0] * n a = ac.copy() prefix_sum = 0 prefix_add = [0] * (n + 1) counter = 0 for i in range(n): if i >= w: prefix_sum -= prefix_add[i - w] if a[i] + prefix_sum < mid: to_be_added = mid - a[i] - prefix_sum start = min(n, i + w) - w prefix_add[start] += to_be_added prefix_sum += to_be_added counter += to_be_added if counter > k: high = mid else: low = mid + 1 return low - 1
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER
You have a garden with n flowers lined up in a row. The height of ith flower is a_{i} units. You will water them for k days. In one day you can water w continuous flowers (you can do this only once in a single day). Whenever you water a flower its height increases by 1 unit. You need to maximize the height of the smallest flower all the time. Example 1: Input: n=6 k=2 w=3 a[]={2,2,2,2,1,1} Output: 2 Explanation: Water last three flowers for first day & first three flowers for second day.The new heights will be {3,3,3,3,2,2} Example 2: Input: n=2 k=5 w=1 a[]={5,8} Output: 9 Explanation: For the first four days water the first flower then water the last flower once. Your Task: You don't need to read input or print anything. Your task is to complete the function maximizeMinHeight() which takes the array a[], its size N, integer K, and an integer W as input parameters and returns the maximum height possible for the smallest flower. Expected Time Complexity: O(NLogN) Expected Space Complexity: O(N) Constraints: 1 <= n<= 10^{5} 1<=w<=n 1<=k<=10^{5} 1 <= a[i] <= 10^{9}
class Solution: def maximizeMinHeight(self, n, k, w, arr): def helper(mid): lis = arr.copy() cnt = 0 for i in range(n): if lis[i] < mid: cnt += mid - lis[i] add = mid - lis[i] for j in range(i, min(n, i + w)): lis[j] += add return cnt <= k l, r = 1, 10**9 ans = min(arr) while l <= r: mid = (l + r) // 2 if helper(mid): ans = mid l = mid + 1 else: r = mid - 1 return ans
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR RETURN VAR VAR ASSIGN VAR VAR NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
You have a garden with n flowers lined up in a row. The height of ith flower is a_{i} units. You will water them for k days. In one day you can water w continuous flowers (you can do this only once in a single day). Whenever you water a flower its height increases by 1 unit. You need to maximize the height of the smallest flower all the time. Example 1: Input: n=6 k=2 w=3 a[]={2,2,2,2,1,1} Output: 2 Explanation: Water last three flowers for first day & first three flowers for second day.The new heights will be {3,3,3,3,2,2} Example 2: Input: n=2 k=5 w=1 a[]={5,8} Output: 9 Explanation: For the first four days water the first flower then water the last flower once. Your Task: You don't need to read input or print anything. Your task is to complete the function maximizeMinHeight() which takes the array a[], its size N, integer K, and an integer W as input parameters and returns the maximum height possible for the smallest flower. Expected Time Complexity: O(NLogN) Expected Space Complexity: O(N) Constraints: 1 <= n<= 10^{5} 1<=w<=n 1<=k<=10^{5} 1 <= a[i] <= 10^{9}
class Solution: def maximizeMinHeight(self, n, k, w, a): high = 10**9 low = 0 ans = -1 while low <= high: mid = (low + high) // 2 if self.isPossible(a, w, mid, k): low = mid + 1 ans = max(mid, ans) else: high = mid - 1 return ans def isPossible(self, arr, l, maxHeight, days): water = [0] * len(arr) for i in range(len(arr)): if i > 0: water[i] = water[i - 1] curHei = water[i] + arr[i] if i >= l: curHei -= water[i - l] if curHei < maxHeight: water[i] += maxHeight - curHei days -= maxHeight - curHei if days < 0: return False return True
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER RETURN NUMBER RETURN NUMBER
You have a garden with n flowers lined up in a row. The height of ith flower is a_{i} units. You will water them for k days. In one day you can water w continuous flowers (you can do this only once in a single day). Whenever you water a flower its height increases by 1 unit. You need to maximize the height of the smallest flower all the time. Example 1: Input: n=6 k=2 w=3 a[]={2,2,2,2,1,1} Output: 2 Explanation: Water last three flowers for first day & first three flowers for second day.The new heights will be {3,3,3,3,2,2} Example 2: Input: n=2 k=5 w=1 a[]={5,8} Output: 9 Explanation: For the first four days water the first flower then water the last flower once. Your Task: You don't need to read input or print anything. Your task is to complete the function maximizeMinHeight() which takes the array a[], its size N, integer K, and an integer W as input parameters and returns the maximum height possible for the smallest flower. Expected Time Complexity: O(NLogN) Expected Space Complexity: O(N) Constraints: 1 <= n<= 10^{5} 1<=w<=n 1<=k<=10^{5} 1 <= a[i] <= 10^{9}
class Solution: def isValid(self, arr, val, w, m): n = len(arr) difSum = [0] * n pref = 0 for i in range(n): pref -= difSum[i] req = max(0, val - arr[i] - pref) m -= req pref += req if i + w < n: difSum[i + w] += req if m < 0: return False return True def maximizeMinHeight(self, n, k, w, a): l = 0 r = 10**9 + 1 while l < r - 1: mid = (l + r) // 2 if self.isValid(a, mid, w, k): l = mid else: r = mid - 1 while r >= l: if self.isValid(a, r, w, k): return r r -= 1
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR IF VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR VAR NUMBER
You have a garden with n flowers lined up in a row. The height of ith flower is a_{i} units. You will water them for k days. In one day you can water w continuous flowers (you can do this only once in a single day). Whenever you water a flower its height increases by 1 unit. You need to maximize the height of the smallest flower all the time. Example 1: Input: n=6 k=2 w=3 a[]={2,2,2,2,1,1} Output: 2 Explanation: Water last three flowers for first day & first three flowers for second day.The new heights will be {3,3,3,3,2,2} Example 2: Input: n=2 k=5 w=1 a[]={5,8} Output: 9 Explanation: For the first four days water the first flower then water the last flower once. Your Task: You don't need to read input or print anything. Your task is to complete the function maximizeMinHeight() which takes the array a[], its size N, integer K, and an integer W as input parameters and returns the maximum height possible for the smallest flower. Expected Time Complexity: O(NLogN) Expected Space Complexity: O(N) Constraints: 1 <= n<= 10^{5} 1<=w<=n 1<=k<=10^{5} 1 <= a[i] <= 10^{9}
class Solution: def maximizeMinHeight(self, n, k, w, a): df = [0] * (n + w) df[0] = a[0] for j in range(1, n): df[j] = a[j] - a[j - 1] def valid(x): dk = k diff = df[:] prev = 0 for l in range(n): cur = prev + diff[l] if cur < x: e = x - cur diff[l] += e diff[l + w] -= e dk -= e cur = x if dk < 0: return False prev = cur return True lo, hi = 0, 10**10 while lo < hi: mid = (lo + hi) // 2 if valid(mid): lo = mid + 1 else: hi = mid return lo - 1
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER BIN_OP NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN BIN_OP VAR NUMBER
You have a garden with n flowers lined up in a row. The height of ith flower is a_{i} units. You will water them for k days. In one day you can water w continuous flowers (you can do this only once in a single day). Whenever you water a flower its height increases by 1 unit. You need to maximize the height of the smallest flower all the time. Example 1: Input: n=6 k=2 w=3 a[]={2,2,2,2,1,1} Output: 2 Explanation: Water last three flowers for first day & first three flowers for second day.The new heights will be {3,3,3,3,2,2} Example 2: Input: n=2 k=5 w=1 a[]={5,8} Output: 9 Explanation: For the first four days water the first flower then water the last flower once. Your Task: You don't need to read input or print anything. Your task is to complete the function maximizeMinHeight() which takes the array a[], its size N, integer K, and an integer W as input parameters and returns the maximum height possible for the smallest flower. Expected Time Complexity: O(NLogN) Expected Space Complexity: O(N) Constraints: 1 <= n<= 10^{5} 1<=w<=n 1<=k<=10^{5} 1 <= a[i] <= 10^{9}
class Solution: def maximizeMinHeight(self, n, k, w, a): def check(m, arr, c): for i in range(len(arr)): if m > arr[i]: x = m - arr[i] c -= x if c < 0: return False for j in range(i, min(i + w, n)): arr[j] += x return True l = min(a) h = min(a) + k ans = min(a) while l <= h: m = (l + h) // 2 if check(m, a[:], k): ans = max(m, ans) l = m + 1 else: h = m - 1 return ans
CLASS_DEF FUNC_DEF FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR IF VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
You are given an array of integers $b_1, b_2, \ldots, b_n$. An array $a_1, a_2, \ldots, a_n$ of integers is hybrid if for each $i$ ($1 \leq i \leq n$) at least one of these conditions is true: $b_i = a_i$, or $b_i = \sum_{j=1}^{i} a_j$. Find the number of hybrid arrays $a_1, a_2, \ldots, a_n$. As the result can be very large, you should print the answer modulo $10^9 + 7$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$). The second line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($-10^9 \le b_i \le 10^9$). It is guaranteed that the sum of $n$ for all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer: the number of hybrid arrays $a_1, a_2, \ldots, a_n$ modulo $10^9 + 7$. -----Examples----- Input 4 3 1 -1 1 4 1 2 3 4 10 2 -1 1 -2 2 3 -5 0 2 -1 4 0 0 0 1 Output 3 8 223 1 -----Note----- In the first test case, the hybrid arrays are $[1, -2, 1]$, $[1, -2, 2]$, $[1, -1, 1]$. In the second test case, the hybrid arrays are $[1, 1, 1, 1]$, $[1, 1, 1, 4]$, $[1, 1, 3, -1]$, $[1, 1, 3, 4]$, $[1, 2, 0, 1]$, $[1, 2, 0, 4]$, $[1, 2, 3, -2]$, $[1, 2, 3, 4]$. In the fourth test case, the only hybrid array is $[0, 0, 0, 1]$.
import sys from sys import stdin mod = 10**9 + 7 class SegTree: def __init__(self, N, first): self.NO = 2 ** (N - 1).bit_length() self.First = first self.data = [first] * (2 * self.NO) def calc(self, l, r): return (l + r) % mod def update(self, ind, x): ind += self.NO - 1 self.data[ind] = x while ind >= 0: ind = (ind - 1) // 2 self.data[ind] = self.calc(self.data[2 * ind + 1], self.data[2 * ind + 2]) def query(self, l, r): L = l + self.NO R = r + self.NO s = self.First while L < R: if R & 1: R -= 1 s = self.calc(s, self.data[R - 1]) if L & 1: s = self.calc(s, self.data[L - 1]) L += 1 L >>= 1 R >>= 1 return s def get(self, ind): ind += self.NO - 1 return self.data[ind] tt = int(stdin.readline()) for loop in range(tt): n = int(stdin.readline()) b = [0] + list(map(int, stdin.readline().split())) c = [0] for i in range(1, n + 1): c.append(c[-1] + b[i]) ctoi = {} tlis = [] for i in range(len(c)): if c[i] not in ctoi: ctoi[c[i]] = 0 tlis.append(c[i]) tlis.sort() for i in range(len(tlis)): ctoi[tlis[i]] = i ST = SegTree(len(tlis), 0) ST.update(ctoi[0], 1) for i in range(1, n + 1): ind = ctoi[c[i - 1]] ST.update(ind, ST.query(0, len(tlis))) print(ST.query(0, len(tlis)))
IMPORT ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP NUMBER FUNC_CALL BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST VAR BIN_OP NUMBER VAR FUNC_DEF RETURN BIN_OP BIN_OP VAR VAR VAR FUNC_DEF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER VAR BIN_OP BIN_OP NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR WHILE VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF VAR BIN_OP VAR NUMBER RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR DICT ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR
You are given an array of integers $b_1, b_2, \ldots, b_n$. An array $a_1, a_2, \ldots, a_n$ of integers is hybrid if for each $i$ ($1 \leq i \leq n$) at least one of these conditions is true: $b_i = a_i$, or $b_i = \sum_{j=1}^{i} a_j$. Find the number of hybrid arrays $a_1, a_2, \ldots, a_n$. As the result can be very large, you should print the answer modulo $10^9 + 7$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$). The second line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($-10^9 \le b_i \le 10^9$). It is guaranteed that the sum of $n$ for all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer: the number of hybrid arrays $a_1, a_2, \ldots, a_n$ modulo $10^9 + 7$. -----Examples----- Input 4 3 1 -1 1 4 1 2 3 4 10 2 -1 1 -2 2 3 -5 0 2 -1 4 0 0 0 1 Output 3 8 223 1 -----Note----- In the first test case, the hybrid arrays are $[1, -2, 1]$, $[1, -2, 2]$, $[1, -1, 1]$. In the second test case, the hybrid arrays are $[1, 1, 1, 1]$, $[1, 1, 1, 4]$, $[1, 1, 3, -1]$, $[1, 1, 3, 4]$, $[1, 2, 0, 1]$, $[1, 2, 0, 4]$, $[1, 2, 3, -2]$, $[1, 2, 3, 4]$. In the fourth test case, the only hybrid array is $[0, 0, 0, 1]$.
for s in [*open(0)][2::2]: C = [0] D = {(0): 1} S = 1 for n in map(int, s.split()): C += (C[-1] + n,) for n in C[1:-1]: D[n], S = S, (2 * S - D.get(n, 0)) % (10**9 + 7) print(S)
FOR VAR LIST FUNC_CALL VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FOR VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR FUNC_CALL VAR VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You are given an array of integers $b_1, b_2, \ldots, b_n$. An array $a_1, a_2, \ldots, a_n$ of integers is hybrid if for each $i$ ($1 \leq i \leq n$) at least one of these conditions is true: $b_i = a_i$, or $b_i = \sum_{j=1}^{i} a_j$. Find the number of hybrid arrays $a_1, a_2, \ldots, a_n$. As the result can be very large, you should print the answer modulo $10^9 + 7$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$). The second line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($-10^9 \le b_i \le 10^9$). It is guaranteed that the sum of $n$ for all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer: the number of hybrid arrays $a_1, a_2, \ldots, a_n$ modulo $10^9 + 7$. -----Examples----- Input 4 3 1 -1 1 4 1 2 3 4 10 2 -1 1 -2 2 3 -5 0 2 -1 4 0 0 0 1 Output 3 8 223 1 -----Note----- In the first test case, the hybrid arrays are $[1, -2, 1]$, $[1, -2, 2]$, $[1, -1, 1]$. In the second test case, the hybrid arrays are $[1, 1, 1, 1]$, $[1, 1, 1, 4]$, $[1, 1, 3, -1]$, $[1, 1, 3, 4]$, $[1, 2, 0, 1]$, $[1, 2, 0, 4]$, $[1, 2, 3, -2]$, $[1, 2, 3, 4]$. In the fourth test case, the only hybrid array is $[0, 0, 0, 1]$.
T = int(input()) for t in range(T): n = int(input()) bb = [int(x) for x in input().split()] sums_bef_offset = {bb[0]: 1} offset = 0 all_sums = 1 base = 1000000007 result = 0 for i in range(1, n): b = bb[i] sums_0 = sums_bef_offset.get(-offset, 0) offset += b prev_all_sums = all_sums sums_bef_offset[b - offset] = prev_all_sums all_sums = (2 * prev_all_sums - sums_0) % base print(all_sums)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array of integers $b_1, b_2, \ldots, b_n$. An array $a_1, a_2, \ldots, a_n$ of integers is hybrid if for each $i$ ($1 \leq i \leq n$) at least one of these conditions is true: $b_i = a_i$, or $b_i = \sum_{j=1}^{i} a_j$. Find the number of hybrid arrays $a_1, a_2, \ldots, a_n$. As the result can be very large, you should print the answer modulo $10^9 + 7$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$). The second line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($-10^9 \le b_i \le 10^9$). It is guaranteed that the sum of $n$ for all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer: the number of hybrid arrays $a_1, a_2, \ldots, a_n$ modulo $10^9 + 7$. -----Examples----- Input 4 3 1 -1 1 4 1 2 3 4 10 2 -1 1 -2 2 3 -5 0 2 -1 4 0 0 0 1 Output 3 8 223 1 -----Note----- In the first test case, the hybrid arrays are $[1, -2, 1]$, $[1, -2, 2]$, $[1, -1, 1]$. In the second test case, the hybrid arrays are $[1, 1, 1, 1]$, $[1, 1, 1, 4]$, $[1, 1, 3, -1]$, $[1, 1, 3, 4]$, $[1, 2, 0, 1]$, $[1, 2, 0, 4]$, $[1, 2, 3, -2]$, $[1, 2, 3, 4]$. In the fourth test case, the only hybrid array is $[0, 0, 0, 1]$.
p = lambda: list(map(int, input().split())) for t in range(p()[0]): N = p()[0] B = p() C = [0] * (N + 1) for i in range(N): C[i + 1] = C[i] + B[i] S = 1 D = dict() D[0] = 1 for i in range(1, N): D[C[i]], S = S, (2 * S - D.get(C[i], 0)) % (10**9 + 7) print(S)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER BIN_OP BIN_OP NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR
You are given an array of integers $b_1, b_2, \ldots, b_n$. An array $a_1, a_2, \ldots, a_n$ of integers is hybrid if for each $i$ ($1 \leq i \leq n$) at least one of these conditions is true: $b_i = a_i$, or $b_i = \sum_{j=1}^{i} a_j$. Find the number of hybrid arrays $a_1, a_2, \ldots, a_n$. As the result can be very large, you should print the answer modulo $10^9 + 7$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$). The second line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($-10^9 \le b_i \le 10^9$). It is guaranteed that the sum of $n$ for all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer: the number of hybrid arrays $a_1, a_2, \ldots, a_n$ modulo $10^9 + 7$. -----Examples----- Input 4 3 1 -1 1 4 1 2 3 4 10 2 -1 1 -2 2 3 -5 0 2 -1 4 0 0 0 1 Output 3 8 223 1 -----Note----- In the first test case, the hybrid arrays are $[1, -2, 1]$, $[1, -2, 2]$, $[1, -1, 1]$. In the second test case, the hybrid arrays are $[1, 1, 1, 1]$, $[1, 1, 1, 4]$, $[1, 1, 3, -1]$, $[1, 1, 3, 4]$, $[1, 2, 0, 1]$, $[1, 2, 0, 4]$, $[1, 2, 3, -2]$, $[1, 2, 3, 4]$. In the fourth test case, the only hybrid array is $[0, 0, 0, 1]$.
import sys mod = 10**9 + 7 for t in range(int(input())): N = int(input()) B = list(map(int, input().split())) C = [0] * (N + 1) for i in range(N): C[i + 1] = C[i] + B[i] S = 1 D = dict() D[0] = 1 for i in range(N - 1): T = (S + S - D.get(C[i + 1], 0)) % mod D[C[i + 1]] = S S = T print(S)
IMPORT ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array of integers $b_1, b_2, \ldots, b_n$. An array $a_1, a_2, \ldots, a_n$ of integers is hybrid if for each $i$ ($1 \leq i \leq n$) at least one of these conditions is true: $b_i = a_i$, or $b_i = \sum_{j=1}^{i} a_j$. Find the number of hybrid arrays $a_1, a_2, \ldots, a_n$. As the result can be very large, you should print the answer modulo $10^9 + 7$. -----Input----- The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The first line of each test case contains a single integer $n$ ($1 \le n \le 2 \cdot 10^5$). The second line of each test case contains $n$ integers $b_1, b_2, \ldots, b_n$ ($-10^9 \le b_i \le 10^9$). It is guaranteed that the sum of $n$ for all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, print a single integer: the number of hybrid arrays $a_1, a_2, \ldots, a_n$ modulo $10^9 + 7$. -----Examples----- Input 4 3 1 -1 1 4 1 2 3 4 10 2 -1 1 -2 2 3 -5 0 2 -1 4 0 0 0 1 Output 3 8 223 1 -----Note----- In the first test case, the hybrid arrays are $[1, -2, 1]$, $[1, -2, 2]$, $[1, -1, 1]$. In the second test case, the hybrid arrays are $[1, 1, 1, 1]$, $[1, 1, 1, 4]$, $[1, 1, 3, -1]$, $[1, 1, 3, 4]$, $[1, 2, 0, 1]$, $[1, 2, 0, 4]$, $[1, 2, 3, -2]$, $[1, 2, 3, 4]$. In the fourth test case, the only hybrid array is $[0, 0, 0, 1]$.
import sys from sys import stdin mod = 10**9 + 7 tt = int(stdin.readline()) for loop in range(tt): n = int(stdin.readline()) b = [0] + list(map(int, stdin.readline().split())) c = [0] for i in range(1, n + 1): c.append(c[-1] + b[i]) dic = {} dic[0] = 1 ds = 1 for i in range(1, n + 1): nc = c[i - 1] if nc not in dic: last = 0 else: last = dic[nc] dic[nc] = ds ds -= last ds += dic[nc] ds %= mod print(ds % mod)
IMPORT ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Iahub recently has learned Bubble Sort, an algorithm that is used to sort a permutation with n elements a_1, a_2, ..., a_{n} in ascending order. He is bored of this so simple algorithm, so he invents his own graph. The graph (let's call it G) initially has n vertices and 0 edges. During Bubble Sort execution, edges appear as described in the following algorithm (pseudocode). procedure bubbleSortGraph() build a graph G with n vertices and 0 edges repeat swapped = false for i = 1 to n - 1 inclusive do: if a[i] > a[i + 1] then add an undirected edge in G between a[i] and a[i + 1] swap( a[i], a[i + 1] ) swapped = true end if end for until not swapped /* repeat the algorithm as long as swapped value is true. */ end procedure For a graph, an independent set is a set of vertices in a graph, no two of which are adjacent (so there are no edges between vertices of an independent set). A maximum independent set is an independent set which has maximum cardinality. Given the permutation, find the size of the maximum independent set of graph G, if we use such permutation as the premutation a in procedure bubbleSortGraph. -----Input----- The first line of the input contains an integer n (2 ≤ n ≤ 10^5). The next line contains n distinct integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ n). -----Output----- Output a single integer — the answer to the problem. -----Examples----- Input 3 3 1 2 Output 2 -----Note----- Consider the first example. Bubble sort swaps elements 3 and 1. We add edge (1, 3). Permutation is now [1, 3, 2]. Then bubble sort swaps elements 3 and 2. We add edge (2, 3). Permutation is now sorted. We have a graph with 3 vertices and 2 edges (1, 3) and (2, 3). Its maximal independent set is [1, 2].
def CeilIndex(A, l, r, key): while r - l > 1: m = l + (r - l) // 2 if A[m] >= key: r = m else: l = m return r def LIS(A, size): tailTable = [(0) for i in range(size + 1)] len = 0 tailTable[0] = A[0] len = 1 for i in range(1, size): if A[i] < tailTable[0]: tailTable[0] = A[i] elif A[i] > tailTable[len - 1]: tailTable[len] = A[i] len += 1 else: tailTable[CeilIndex(tailTable, -1, len - 1, A[i])] = A[i] return len def main(): n = int(input()) arr = list(map(int, input().split())) print(LIS(arr, n)) main()
FUNC_DEF WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR NUMBER ASSIGN VAR NUMBER VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
Iahub recently has learned Bubble Sort, an algorithm that is used to sort a permutation with n elements a_1, a_2, ..., a_{n} in ascending order. He is bored of this so simple algorithm, so he invents his own graph. The graph (let's call it G) initially has n vertices and 0 edges. During Bubble Sort execution, edges appear as described in the following algorithm (pseudocode). procedure bubbleSortGraph() build a graph G with n vertices and 0 edges repeat swapped = false for i = 1 to n - 1 inclusive do: if a[i] > a[i + 1] then add an undirected edge in G between a[i] and a[i + 1] swap( a[i], a[i + 1] ) swapped = true end if end for until not swapped /* repeat the algorithm as long as swapped value is true. */ end procedure For a graph, an independent set is a set of vertices in a graph, no two of which are adjacent (so there are no edges between vertices of an independent set). A maximum independent set is an independent set which has maximum cardinality. Given the permutation, find the size of the maximum independent set of graph G, if we use such permutation as the premutation a in procedure bubbleSortGraph. -----Input----- The first line of the input contains an integer n (2 ≤ n ≤ 10^5). The next line contains n distinct integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ n). -----Output----- Output a single integer — the answer to the problem. -----Examples----- Input 3 3 1 2 Output 2 -----Note----- Consider the first example. Bubble sort swaps elements 3 and 1. We add edge (1, 3). Permutation is now [1, 3, 2]. Then bubble sort swaps elements 3 and 2. We add edge (2, 3). Permutation is now sorted. We have a graph with 3 vertices and 2 edges (1, 3) and (2, 3). Its maximal independent set is [1, 2].
import sys sys.setrecursionlimit(1000000) def solve(): (n,) = rv() (a,) = rl(1) mem = [10000000] * (n + 1) mem[0] = a[0] for i in range(1, n): left, right = 0, n - 1 while left < right: mid = (left + right) // 2 if a[i] < mem[mid]: right = mid else: left = mid + 1 mem[left] = a[i] res = 0 for i in range(1, n): if mem[i] != 10000000: res = i print(res + 1) def rv(): return list(map(int, input().split())) def rl(n): return [list(map(int, input().split())) for _ in range(n)] if sys.hexversion == 50594544: sys.stdin = open("test.txt") solve()
IMPORT EXPR FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR
Iahub recently has learned Bubble Sort, an algorithm that is used to sort a permutation with n elements a_1, a_2, ..., a_{n} in ascending order. He is bored of this so simple algorithm, so he invents his own graph. The graph (let's call it G) initially has n vertices and 0 edges. During Bubble Sort execution, edges appear as described in the following algorithm (pseudocode). procedure bubbleSortGraph() build a graph G with n vertices and 0 edges repeat swapped = false for i = 1 to n - 1 inclusive do: if a[i] > a[i + 1] then add an undirected edge in G between a[i] and a[i + 1] swap( a[i], a[i + 1] ) swapped = true end if end for until not swapped /* repeat the algorithm as long as swapped value is true. */ end procedure For a graph, an independent set is a set of vertices in a graph, no two of which are adjacent (so there are no edges between vertices of an independent set). A maximum independent set is an independent set which has maximum cardinality. Given the permutation, find the size of the maximum independent set of graph G, if we use such permutation as the premutation a in procedure bubbleSortGraph. -----Input----- The first line of the input contains an integer n (2 ≤ n ≤ 10^5). The next line contains n distinct integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ n). -----Output----- Output a single integer — the answer to the problem. -----Examples----- Input 3 3 1 2 Output 2 -----Note----- Consider the first example. Bubble sort swaps elements 3 and 1. We add edge (1, 3). Permutation is now [1, 3, 2]. Then bubble sort swaps elements 3 and 2. We add edge (2, 3). Permutation is now sorted. We have a graph with 3 vertices and 2 edges (1, 3) and (2, 3). Its maximal independent set is [1, 2].
n = int(input()) a = list(map(int, input().split())) bit = [0] * (n + 1) def update(idx, val): while idx <= n: bit[idx] = max(bit[idx], val) idx += idx & -idx def query(idx): res = 0 while idx > 0: res = max(res, bit[idx]) idx -= idx & -idx return res b = [] for i in range(n): b.append([a[i], -(i + 1)]) b.sort() for i in range(n): qe = query(-b[i][1]) update(-b[i][1], qe + 1) print(query(n))
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 BIN_OP VAR NUMBER FUNC_DEF WHILE VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Iahub recently has learned Bubble Sort, an algorithm that is used to sort a permutation with n elements a_1, a_2, ..., a_{n} in ascending order. He is bored of this so simple algorithm, so he invents his own graph. The graph (let's call it G) initially has n vertices and 0 edges. During Bubble Sort execution, edges appear as described in the following algorithm (pseudocode). procedure bubbleSortGraph() build a graph G with n vertices and 0 edges repeat swapped = false for i = 1 to n - 1 inclusive do: if a[i] > a[i + 1] then add an undirected edge in G between a[i] and a[i + 1] swap( a[i], a[i + 1] ) swapped = true end if end for until not swapped /* repeat the algorithm as long as swapped value is true. */ end procedure For a graph, an independent set is a set of vertices in a graph, no two of which are adjacent (so there are no edges between vertices of an independent set). A maximum independent set is an independent set which has maximum cardinality. Given the permutation, find the size of the maximum independent set of graph G, if we use such permutation as the premutation a in procedure bubbleSortGraph. -----Input----- The first line of the input contains an integer n (2 ≤ n ≤ 10^5). The next line contains n distinct integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ n). -----Output----- Output a single integer — the answer to the problem. -----Examples----- Input 3 3 1 2 Output 2 -----Note----- Consider the first example. Bubble sort swaps elements 3 and 1. We add edge (1, 3). Permutation is now [1, 3, 2]. Then bubble sort swaps elements 3 and 2. We add edge (2, 3). Permutation is now sorted. We have a graph with 3 vertices and 2 edges (1, 3) and (2, 3). Its maximal independent set is [1, 2].
MXusl = int MXuso = input MXusL = map MXusr = min MXusI = print n = MXusl(MXuso()) a = [1000000.0] * (n + 1) s = 1 for x in MXusL(MXusl, MXuso().split()): l = 0 r = s while r - l > 1: m = l + r >> 1 if a[m] < x: l = m else: r = m s += r == s a[r] = MXusr(a[r], x) MXusI(s - 1)
ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER
Iahub recently has learned Bubble Sort, an algorithm that is used to sort a permutation with n elements a_1, a_2, ..., a_{n} in ascending order. He is bored of this so simple algorithm, so he invents his own graph. The graph (let's call it G) initially has n vertices and 0 edges. During Bubble Sort execution, edges appear as described in the following algorithm (pseudocode). procedure bubbleSortGraph() build a graph G with n vertices and 0 edges repeat swapped = false for i = 1 to n - 1 inclusive do: if a[i] > a[i + 1] then add an undirected edge in G between a[i] and a[i + 1] swap( a[i], a[i + 1] ) swapped = true end if end for until not swapped /* repeat the algorithm as long as swapped value is true. */ end procedure For a graph, an independent set is a set of vertices in a graph, no two of which are adjacent (so there are no edges between vertices of an independent set). A maximum independent set is an independent set which has maximum cardinality. Given the permutation, find the size of the maximum independent set of graph G, if we use such permutation as the premutation a in procedure bubbleSortGraph. -----Input----- The first line of the input contains an integer n (2 ≤ n ≤ 10^5). The next line contains n distinct integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ n). -----Output----- Output a single integer — the answer to the problem. -----Examples----- Input 3 3 1 2 Output 2 -----Note----- Consider the first example. Bubble sort swaps elements 3 and 1. We add edge (1, 3). Permutation is now [1, 3, 2]. Then bubble sort swaps elements 3 and 2. We add edge (2, 3). Permutation is now sorted. We have a graph with 3 vertices and 2 edges (1, 3) and (2, 3). Its maximal independent set is [1, 2].
from sys import stdin input = stdin.readline class BIT: def __init__(self, n): self.n = n self.tree = [0] * (n + 1) def sum(self, i): ans = 0 i += 1 while i > 0: ans = max(ans, self.tree[i]) i -= i & -i return ans def update(self, i, value): i += 1 while i <= self.n: self.tree[i] = max(value, self.tree[i]) i += i & -i def f(a): newind = 0 maxs = 0 ans = 0 ft = BIT(2 * 10**5 + 5) for i in a: maxs = ft.sum(i - 1) ft.update(i, maxs + 1) ans = max(ans, maxs + 1) return ans a = input() l = list(map(int, input().strip().split())) print(f(l))
ASSIGN VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF ASSIGN VAR NUMBER VAR NUMBER WHILE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER BIN_OP NUMBER NUMBER NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Iahub recently has learned Bubble Sort, an algorithm that is used to sort a permutation with n elements a_1, a_2, ..., a_{n} in ascending order. He is bored of this so simple algorithm, so he invents his own graph. The graph (let's call it G) initially has n vertices and 0 edges. During Bubble Sort execution, edges appear as described in the following algorithm (pseudocode). procedure bubbleSortGraph() build a graph G with n vertices and 0 edges repeat swapped = false for i = 1 to n - 1 inclusive do: if a[i] > a[i + 1] then add an undirected edge in G between a[i] and a[i + 1] swap( a[i], a[i + 1] ) swapped = true end if end for until not swapped /* repeat the algorithm as long as swapped value is true. */ end procedure For a graph, an independent set is a set of vertices in a graph, no two of which are adjacent (so there are no edges between vertices of an independent set). A maximum independent set is an independent set which has maximum cardinality. Given the permutation, find the size of the maximum independent set of graph G, if we use such permutation as the premutation a in procedure bubbleSortGraph. -----Input----- The first line of the input contains an integer n (2 ≤ n ≤ 10^5). The next line contains n distinct integers a_1, a_2, ..., a_{n} (1 ≤ a_{i} ≤ n). -----Output----- Output a single integer — the answer to the problem. -----Examples----- Input 3 3 1 2 Output 2 -----Note----- Consider the first example. Bubble sort swaps elements 3 and 1. We add edge (1, 3). Permutation is now [1, 3, 2]. Then bubble sort swaps elements 3 and 2. We add edge (2, 3). Permutation is now sorted. We have a graph with 3 vertices and 2 edges (1, 3) and (2, 3). Its maximal independent set is [1, 2].
def lis(a): b = [] for c in a: if len(b) == 0 or c > b[-1]: b.append(c) else: l = 0 r = len(b) while l < r - 1: m = l + r >> 1 if b[m] < c: l = m else: r = m if b[l] < c: l += 1 b[l] = c return len(b) n = int(input()) a = list(map(int, input().split())) print(lis(a))
FUNC_DEF ASSIGN VAR LIST FOR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Iahub recently has learned Bubble Sort, an algorithm that is used to sort a permutation with n elements a1, a2, ..., an in ascending order. He is bored of this so simple algorithm, so he invents his own graph. The graph (let's call it G) initially has n vertices and 0 edges. During Bubble Sort execution, edges appear as described in the following algorithm (pseudocode). procedure bubbleSortGraph() build a graph G with n vertices and 0 edges repeat swapped = false for i = 1 to n - 1 inclusive do: if a[i] > a[i + 1] then add an undirected edge in G between a[i] and a[i + 1] swap( a[i], a[i + 1] ) swapped = true end if end for until not swapped /* repeat the algorithm as long as swapped value is true. */ end procedure For a graph, an independent set is a set of vertices in a graph, no two of which are adjacent (so there are no edges between vertices of an independent set). A maximum independent set is an independent set which has maximum cardinality. Given the permutation, find the size of the maximum independent set of graph G, if we use such permutation as the premutation a in procedure bubbleSortGraph. Input The first line of the input contains an integer n (2 ≤ n ≤ 105). The next line contains n distinct integers a1, a2, ..., an (1 ≤ ai ≤ n). Output Output a single integer — the answer to the problem. Examples Input 3 3 1 2 Output 2 Note Consider the first example. Bubble sort swaps elements 3 and 1. We add edge (1, 3). Permutation is now [1, 3, 2]. Then bubble sort swaps elements 3 and 2. We add edge (2, 3). Permutation is now sorted. We have a graph with 3 vertices and 2 edges (1, 3) and (2, 3). Its maximal independent set is [1, 2].
import sys sys.setrecursionlimit(1000000) def solve(): (n,) = rv() (a,) = rl(1) mem = [10000000] * (n + 1) mem[0] = a[0] for i in range(1, n): left, right = 0, n - 1 while left < right: mid = (left + right) // 2 if a[i] < mem[mid]: right = mid else: left = mid + 1 mem[left] = a[i] res = 0 for i in range(1, n): if mem[i] != 10000000: res = i print(res + 1) def rv(): return map(int, input().split()) def rl(n): return [list(map(int, input().split())) for _ in range(n)] if sys.hexversion == 50594544: sys.stdin = open("test.txt") solve()
IMPORT EXPR FUNC_CALL VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR
Limak is a little bear who loves to play. Today he is playing by destroying block towers. He built n towers in a row. The i-th tower is made of hi identical blocks. For clarification see picture for the first sample. Limak will repeat the following operation till everything is destroyed. Block is called internal if it has all four neighbors, i.e. it has each side (top, left, down and right) adjacent to other block or to the floor. Otherwise, block is boundary. In one operation Limak destroys all boundary blocks. His paws are very fast and he destroys all those blocks at the same time. Limak is ready to start. You task is to count how many operations will it take him to destroy all towers. Input The first line contains single integer n (1 ≤ n ≤ 105). The second line contains n space-separated integers h1, h2, ..., hn (1 ≤ hi ≤ 109) — sizes of towers. Output Print the number of operations needed to destroy all towers. Examples Input 6 2 1 4 6 2 2 Output 3 Input 7 3 3 3 1 3 3 3 Output 2 Note The picture below shows all three operations for the first sample test. Each time boundary blocks are marked with red color. <image> After first operation there are four blocks left and only one remains after second operation. This last block is destroyed in third operation.
n = int(input()) h = list(map(int, input().split())) dp = [] dp.append(1) for i in range(1, n - 1, 1): dp.append(min(dp[i - 1] + 1, h[i])) dp.append(1) for i in range(n - 2, 0, -1): dp[i] = min(dp[i], dp[i + 1] + 1) mx = -1 for i in range(n): mx = max(mx, dp[i]) print(mx)
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 BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL 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 ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Limak is a little bear who loves to play. Today he is playing by destroying block towers. He built n towers in a row. The i-th tower is made of hi identical blocks. For clarification see picture for the first sample. Limak will repeat the following operation till everything is destroyed. Block is called internal if it has all four neighbors, i.e. it has each side (top, left, down and right) adjacent to other block or to the floor. Otherwise, block is boundary. In one operation Limak destroys all boundary blocks. His paws are very fast and he destroys all those blocks at the same time. Limak is ready to start. You task is to count how many operations will it take him to destroy all towers. Input The first line contains single integer n (1 ≤ n ≤ 105). The second line contains n space-separated integers h1, h2, ..., hn (1 ≤ hi ≤ 109) — sizes of towers. Output Print the number of operations needed to destroy all towers. Examples Input 6 2 1 4 6 2 2 Output 3 Input 7 3 3 3 1 3 3 3 Output 2 Note The picture below shows all three operations for the first sample test. Each time boundary blocks are marked with red color. <image> After first operation there are four blocks left and only one remains after second operation. This last block is destroyed in third operation.
__author__ = "JohnHook" n = list(map(int, input().split()))[0] + 2 a = [0] + list(map(int, input().split())) + [0] d = [0] for i in range(1, n): if a[i] >= d[i - 1] + 1: d.append(d[i - 1] + 1) continue d.append(a[i]) d.reverse() a.reverse() for i in range(1, n): if a[i] >= d[i - 1] + 1: d[i] = min(d[i], d[i - 1] + 1) continue d[i] = a[i] print(max(d))
ASSIGN VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR LIST NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Limak is a little bear who loves to play. Today he is playing by destroying block towers. He built n towers in a row. The i-th tower is made of hi identical blocks. For clarification see picture for the first sample. Limak will repeat the following operation till everything is destroyed. Block is called internal if it has all four neighbors, i.e. it has each side (top, left, down and right) adjacent to other block or to the floor. Otherwise, block is boundary. In one operation Limak destroys all boundary blocks. His paws are very fast and he destroys all those blocks at the same time. Limak is ready to start. You task is to count how many operations will it take him to destroy all towers. Input The first line contains single integer n (1 ≤ n ≤ 105). The second line contains n space-separated integers h1, h2, ..., hn (1 ≤ hi ≤ 109) — sizes of towers. Output Print the number of operations needed to destroy all towers. Examples Input 6 2 1 4 6 2 2 Output 3 Input 7 3 3 3 1 3 3 3 Output 2 Note The picture below shows all three operations for the first sample test. Each time boundary blocks are marked with red color. <image> After first operation there are four blocks left and only one remains after second operation. This last block is destroyed in third operation.
n = int(input()) h = [0] + list(map(int, input().split())) res = [0] * (n + 1) Min = 0 for i in range(1, n + 1): Min = min(Min, h[i] - i) res[i] = i + Min Min = n + 1 for i in range(n, 0, -1): Min = min(Min, h[i] + i) res[i] = min(res[i], Min - i) ans = max(res) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Limak is a little bear who loves to play. Today he is playing by destroying block towers. He built n towers in a row. The i-th tower is made of hi identical blocks. For clarification see picture for the first sample. Limak will repeat the following operation till everything is destroyed. Block is called internal if it has all four neighbors, i.e. it has each side (top, left, down and right) adjacent to other block or to the floor. Otherwise, block is boundary. In one operation Limak destroys all boundary blocks. His paws are very fast and he destroys all those blocks at the same time. Limak is ready to start. You task is to count how many operations will it take him to destroy all towers. Input The first line contains single integer n (1 ≤ n ≤ 105). The second line contains n space-separated integers h1, h2, ..., hn (1 ≤ hi ≤ 109) — sizes of towers. Output Print the number of operations needed to destroy all towers. Examples Input 6 2 1 4 6 2 2 Output 3 Input 7 3 3 3 1 3 3 3 Output 2 Note The picture below shows all three operations for the first sample test. Each time boundary blocks are marked with red color. <image> After first operation there are four blocks left and only one remains after second operation. This last block is destroyed in third operation.
def main(): n = int(input()) a = [int(i) for i in input().split()] dpl = [1] * n dpr = [1] * n for i in range(1, n): dpl[i] = min(dpl[i - 1] + 1, a[i]) for i in range(n - 2, -1, -1): dpr[i] = min(dpr[i + 1] + 1, a[i]) ans = 0 for i in range(n): ans = max(ans, min(dpl[i], dpr[i])) print(ans) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Limak is a little bear who loves to play. Today he is playing by destroying block towers. He built n towers in a row. The i-th tower is made of hi identical blocks. For clarification see picture for the first sample. Limak will repeat the following operation till everything is destroyed. Block is called internal if it has all four neighbors, i.e. it has each side (top, left, down and right) adjacent to other block or to the floor. Otherwise, block is boundary. In one operation Limak destroys all boundary blocks. His paws are very fast and he destroys all those blocks at the same time. Limak is ready to start. You task is to count how many operations will it take him to destroy all towers. Input The first line contains single integer n (1 ≤ n ≤ 105). The second line contains n space-separated integers h1, h2, ..., hn (1 ≤ hi ≤ 109) — sizes of towers. Output Print the number of operations needed to destroy all towers. Examples Input 6 2 1 4 6 2 2 Output 3 Input 7 3 3 3 1 3 3 3 Output 2 Note The picture below shows all three operations for the first sample test. Each time boundary blocks are marked with red color. <image> After first operation there are four blocks left and only one remains after second operation. This last block is destroyed in third operation.
n = int(input()) h = list(map(int, input().split())) min_h = 1 for i in range(n): if h[i] > min_h: h[i] = min_h else: min_h = h[i] min_h += 1 min_h = 1 for i in range(n - 1, -1, -1): if h[i] > min_h: h[i] = min_h else: min_h = h[i] min_h += 1 print(max(h))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Limak is a little bear who loves to play. Today he is playing by destroying block towers. He built n towers in a row. The i-th tower is made of hi identical blocks. For clarification see picture for the first sample. Limak will repeat the following operation till everything is destroyed. Block is called internal if it has all four neighbors, i.e. it has each side (top, left, down and right) adjacent to other block or to the floor. Otherwise, block is boundary. In one operation Limak destroys all boundary blocks. His paws are very fast and he destroys all those blocks at the same time. Limak is ready to start. You task is to count how many operations will it take him to destroy all towers. Input The first line contains single integer n (1 ≤ n ≤ 105). The second line contains n space-separated integers h1, h2, ..., hn (1 ≤ hi ≤ 109) — sizes of towers. Output Print the number of operations needed to destroy all towers. Examples Input 6 2 1 4 6 2 2 Output 3 Input 7 3 3 3 1 3 3 3 Output 2 Note The picture below shows all three operations for the first sample test. Each time boundary blocks are marked with red color. <image> After first operation there are four blocks left and only one remains after second operation. This last block is destroyed in third operation.
n = int(input()) x = list(map(int, input().split())) pre, suf, ans = [0] * n, [0] * n, 0 for i in range(n): if i == 0: pre[i] = 1 else: pre[i] = min(pre[i - 1] + 1, x[i]) for i in range(n - 1, -1, -1): if i == n - 1: suf[i] = 1 else: suf[i] = min(suf[i + 1] + 1, x[i]) ans = max(ans, min(pre[i], suf[i])) 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 VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Limak is a little bear who loves to play. Today he is playing by destroying block towers. He built n towers in a row. The i-th tower is made of hi identical blocks. For clarification see picture for the first sample. Limak will repeat the following operation till everything is destroyed. Block is called internal if it has all four neighbors, i.e. it has each side (top, left, down and right) adjacent to other block or to the floor. Otherwise, block is boundary. In one operation Limak destroys all boundary blocks. His paws are very fast and he destroys all those blocks at the same time. Limak is ready to start. You task is to count how many operations will it take him to destroy all towers. Input The first line contains single integer n (1 ≤ n ≤ 105). The second line contains n space-separated integers h1, h2, ..., hn (1 ≤ hi ≤ 109) — sizes of towers. Output Print the number of operations needed to destroy all towers. Examples Input 6 2 1 4 6 2 2 Output 3 Input 7 3 3 3 1 3 3 3 Output 2 Note The picture below shows all three operations for the first sample test. Each time boundary blocks are marked with red color. <image> After first operation there are four blocks left and only one remains after second operation. This last block is destroyed in third operation.
x = int(input()) mas = list(map(int, input().split(" "))) mas2 = [0] * x mas2[0] = 1 for i in range(1, x): mas2[i] = min(mas[i], mas2[i - 1] + 1) mas2[-1] = 1 for i in range(2, x + 1): mas2[-i] = min(mas[-i], mas2[-i + 1] + 1, mas2[-i]) print(max(mas2))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Limak is a little bear who loves to play. Today he is playing by destroying block towers. He built n towers in a row. The i-th tower is made of hi identical blocks. For clarification see picture for the first sample. Limak will repeat the following operation till everything is destroyed. Block is called internal if it has all four neighbors, i.e. it has each side (top, left, down and right) adjacent to other block or to the floor. Otherwise, block is boundary. In one operation Limak destroys all boundary blocks. His paws are very fast and he destroys all those blocks at the same time. Limak is ready to start. You task is to count how many operations will it take him to destroy all towers. Input The first line contains single integer n (1 ≤ n ≤ 105). The second line contains n space-separated integers h1, h2, ..., hn (1 ≤ hi ≤ 109) — sizes of towers. Output Print the number of operations needed to destroy all towers. Examples Input 6 2 1 4 6 2 2 Output 3 Input 7 3 3 3 1 3 3 3 Output 2 Note The picture below shows all three operations for the first sample test. Each time boundary blocks are marked with red color. <image> After first operation there are four blocks left and only one remains after second operation. This last block is destroyed in third operation.
n = int(input()) a = [int(x) for x in input().split()] res = [0] * n for i in range(n): res[i] = [0, 0, 0] if n < 3: print(1) else: res[0][0] = 1 res[-1][0] = 1 for i in range(1, n - 1): res[i][0] = min(a[i - 1] + 1, a[i + 1] + 1, a[i]) cur_min = 0 for i in range(0, n): res[i][1] = cur_min + 1 cur_min = min(res[i][0], res[i][1]) cur_min = 0 for i in range(n - 1, -1, -1): res[i][2] = cur_min + 1 cur_min = min(res[i][0], res[i][2]) tres = min(res[0]) for k in res: tres = max(tres, min(k)) print(tres)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST NUMBER NUMBER NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Limak is a little bear who loves to play. Today he is playing by destroying block towers. He built n towers in a row. The i-th tower is made of h_{i} identical blocks. For clarification see picture for the first sample. Limak will repeat the following operation till everything is destroyed. Block is called internal if it has all four neighbors, i.e. it has each side (top, left, down and right) adjacent to other block or to the floor. Otherwise, block is boundary. In one operation Limak destroys all boundary blocks. His paws are very fast and he destroys all those blocks at the same time. Limak is ready to start. You task is to count how many operations will it take him to destroy all towers. -----Input----- The first line contains single integer n (1 ≤ n ≤ 10^5). The second line contains n space-separated integers h_1, h_2, ..., h_{n} (1 ≤ h_{i} ≤ 10^9) — sizes of towers. -----Output----- Print the number of operations needed to destroy all towers. -----Examples----- Input 6 2 1 4 6 2 2 Output 3 Input 7 3 3 3 1 3 3 3 Output 2 -----Note----- The picture below shows all three operations for the first sample test. Each time boundary blocks are marked with red color. [Image] After first operation there are four blocks left and only one remains after second operation. This last block is destroyed in third operation.
def main(): n = int(input()) hts = list(map(int, input().split())) dpleft = [1] for i in range(1, n): dpleft.append(min(hts[i], dpleft[-1] + 1)) hts.reverse() dpright = [1] for i in range(1, n): dpright.append(min(hts[i], dpright[-1] + 1)) dpright.reverse() ans = 0 for i in range(n): ans = max(ans, min(dpleft[i], dpright[i])) 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 NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Limak is a little bear who loves to play. Today he is playing by destroying block towers. He built n towers in a row. The i-th tower is made of h_{i} identical blocks. For clarification see picture for the first sample. Limak will repeat the following operation till everything is destroyed. Block is called internal if it has all four neighbors, i.e. it has each side (top, left, down and right) adjacent to other block or to the floor. Otherwise, block is boundary. In one operation Limak destroys all boundary blocks. His paws are very fast and he destroys all those blocks at the same time. Limak is ready to start. You task is to count how many operations will it take him to destroy all towers. -----Input----- The first line contains single integer n (1 ≤ n ≤ 10^5). The second line contains n space-separated integers h_1, h_2, ..., h_{n} (1 ≤ h_{i} ≤ 10^9) — sizes of towers. -----Output----- Print the number of operations needed to destroy all towers. -----Examples----- Input 6 2 1 4 6 2 2 Output 3 Input 7 3 3 3 1 3 3 3 Output 2 -----Note----- The picture below shows all three operations for the first sample test. Each time boundary blocks are marked with red color. [Image] After first operation there are four blocks left and only one remains after second operation. This last block is destroyed in third operation.
import sys try: while True: n = int(input()) height = list(map(int, input().split(" "))) L = [(0) for i in range(100001)] R = [(0) for i in range(100001)] for i in range(n): L[i + 1] = min(L[i] + 1, height[i]) for i in range(n - 1, -1, -1): R[i] = min(R[i + 1] + 1, height[i]) ans = 0 for i in range(1, n + 1): ans = max(ans, min(R[i - 1], L[i])) print(ans) except EOFError: pass
IMPORT WHILE NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR
Limak is a little bear who loves to play. Today he is playing by destroying block towers. He built n towers in a row. The i-th tower is made of h_{i} identical blocks. For clarification see picture for the first sample. Limak will repeat the following operation till everything is destroyed. Block is called internal if it has all four neighbors, i.e. it has each side (top, left, down and right) adjacent to other block or to the floor. Otherwise, block is boundary. In one operation Limak destroys all boundary blocks. His paws are very fast and he destroys all those blocks at the same time. Limak is ready to start. You task is to count how many operations will it take him to destroy all towers. -----Input----- The first line contains single integer n (1 ≤ n ≤ 10^5). The second line contains n space-separated integers h_1, h_2, ..., h_{n} (1 ≤ h_{i} ≤ 10^9) — sizes of towers. -----Output----- Print the number of operations needed to destroy all towers. -----Examples----- Input 6 2 1 4 6 2 2 Output 3 Input 7 3 3 3 1 3 3 3 Output 2 -----Note----- The picture below shows all three operations for the first sample test. Each time boundary blocks are marked with red color. [Image] After first operation there are four blocks left and only one remains after second operation. This last block is destroyed in third operation.
input() def f(l): c = [0] * len(l) c[0] = c[len(c) - 1] = 9999999 for item in range(1, len(l) - 1): c[item] = min(item, l[item], c[item - 1] + 1) for item in reversed(range(1, len(l) - 1)): c[item] = min(c[item], len(l) - 2 + 1 - item, c[item + 1] + 1) return max(c[1:-1]) print(f([0] + list(map(int, input().split())) + [0]))
EXPR FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR LIST NUMBER
Limak is a little bear who loves to play. Today he is playing by destroying block towers. He built n towers in a row. The i-th tower is made of h_{i} identical blocks. For clarification see picture for the first sample. Limak will repeat the following operation till everything is destroyed. Block is called internal if it has all four neighbors, i.e. it has each side (top, left, down and right) adjacent to other block or to the floor. Otherwise, block is boundary. In one operation Limak destroys all boundary blocks. His paws are very fast and he destroys all those blocks at the same time. Limak is ready to start. You task is to count how many operations will it take him to destroy all towers. -----Input----- The first line contains single integer n (1 ≤ n ≤ 10^5). The second line contains n space-separated integers h_1, h_2, ..., h_{n} (1 ≤ h_{i} ≤ 10^9) — sizes of towers. -----Output----- Print the number of operations needed to destroy all towers. -----Examples----- Input 6 2 1 4 6 2 2 Output 3 Input 7 3 3 3 1 3 3 3 Output 2 -----Note----- The picture below shows all three operations for the first sample test. Each time boundary blocks are marked with red color. [Image] After first operation there are four blocks left and only one remains after second operation. This last block is destroyed in third operation.
n = int(input()) a = [0] + [int(i) for i in input().split()] + [0] l, r = [0] * (n + 2), [0] * (n + 2) for i in range(1, n + 1): l[i] = min(a[i], l[i - 1] + 1) for i in range(n, 0, -1): r[i] = min(a[i], r[i + 1] + 1) ans = 0 for i in range(1, n + 1): ans = max(ans, min(l[i], r[i])) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR LIST NUMBER ASSIGN VAR VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR 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 NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Limak is a little bear who loves to play. Today he is playing by destroying block towers. He built n towers in a row. The i-th tower is made of h_{i} identical blocks. For clarification see picture for the first sample. Limak will repeat the following operation till everything is destroyed. Block is called internal if it has all four neighbors, i.e. it has each side (top, left, down and right) adjacent to other block or to the floor. Otherwise, block is boundary. In one operation Limak destroys all boundary blocks. His paws are very fast and he destroys all those blocks at the same time. Limak is ready to start. You task is to count how many operations will it take him to destroy all towers. -----Input----- The first line contains single integer n (1 ≤ n ≤ 10^5). The second line contains n space-separated integers h_1, h_2, ..., h_{n} (1 ≤ h_{i} ≤ 10^9) — sizes of towers. -----Output----- Print the number of operations needed to destroy all towers. -----Examples----- Input 6 2 1 4 6 2 2 Output 3 Input 7 3 3 3 1 3 3 3 Output 2 -----Note----- The picture below shows all three operations for the first sample test. Each time boundary blocks are marked with red color. [Image] After first operation there are four blocks left and only one remains after second operation. This last block is destroyed in third operation.
n = int(input()) a = [int(x) for x in input().split()] a[0] = 1 a[-1] = 1 for i in range(1, n): a[i] = min(a[i], a[i - 1] + 1) a[-i] = min(a[-i], a[-(i - 1)] + 1) print(max(a))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Limak is a little bear who loves to play. Today he is playing by destroying block towers. He built n towers in a row. The i-th tower is made of h_{i} identical blocks. For clarification see picture for the first sample. Limak will repeat the following operation till everything is destroyed. Block is called internal if it has all four neighbors, i.e. it has each side (top, left, down and right) adjacent to other block or to the floor. Otherwise, block is boundary. In one operation Limak destroys all boundary blocks. His paws are very fast and he destroys all those blocks at the same time. Limak is ready to start. You task is to count how many operations will it take him to destroy all towers. -----Input----- The first line contains single integer n (1 ≤ n ≤ 10^5). The second line contains n space-separated integers h_1, h_2, ..., h_{n} (1 ≤ h_{i} ≤ 10^9) — sizes of towers. -----Output----- Print the number of operations needed to destroy all towers. -----Examples----- Input 6 2 1 4 6 2 2 Output 3 Input 7 3 3 3 1 3 3 3 Output 2 -----Note----- The picture below shows all three operations for the first sample test. Each time boundary blocks are marked with red color. [Image] After first operation there are four blocks left and only one remains after second operation. This last block is destroyed in third operation.
from sys import stdin, stdout nmbr = lambda: int(stdin.readline()) lst = lambda: list(map(int, stdin.readline().split())) PI = float("inf") for _ in range(1): n = nmbr() a = lst() ans = 0 pre = [0] * n suf = [0] * n pre[0] = suf[n - 1] = 1 for i in range(1, n): pre[i] = min(1 + pre[i - 1], a[i]) for i in range(n - 2, -1, -1): suf[i] = min(1 + suf[i + 1], a[i]) for pp, ss in zip(pre, suf): ans = max(ans, min(pp, ss)) 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 FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Limak is a little bear who loves to play. Today he is playing by destroying block towers. He built n towers in a row. The i-th tower is made of h_{i} identical blocks. For clarification see picture for the first sample. Limak will repeat the following operation till everything is destroyed. Block is called internal if it has all four neighbors, i.e. it has each side (top, left, down and right) adjacent to other block or to the floor. Otherwise, block is boundary. In one operation Limak destroys all boundary blocks. His paws are very fast and he destroys all those blocks at the same time. Limak is ready to start. You task is to count how many operations will it take him to destroy all towers. -----Input----- The first line contains single integer n (1 ≤ n ≤ 10^5). The second line contains n space-separated integers h_1, h_2, ..., h_{n} (1 ≤ h_{i} ≤ 10^9) — sizes of towers. -----Output----- Print the number of operations needed to destroy all towers. -----Examples----- Input 6 2 1 4 6 2 2 Output 3 Input 7 3 3 3 1 3 3 3 Output 2 -----Note----- The picture below shows all three operations for the first sample test. Each time boundary blocks are marked with red color. [Image] After first operation there are four blocks left and only one remains after second operation. This last block is destroyed in third operation.
input() l, r = [0], [0] for a, b in (lambda a: list(zip(a, reversed(a))))( [0] + list(map(int, input().split())) + [0] ): l.append(min(a, l[-1] + 1)) r.append(min(b, r[-1] + 1)) print(max(list(map(min, list(zip(l[1:], reversed(r[1:])))))))
EXPR FUNC_CALL VAR ASSIGN VAR VAR LIST NUMBER LIST NUMBER FOR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR LIST NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER
Limak is a little bear who loves to play. Today he is playing by destroying block towers. He built n towers in a row. The i-th tower is made of h_{i} identical blocks. For clarification see picture for the first sample. Limak will repeat the following operation till everything is destroyed. Block is called internal if it has all four neighbors, i.e. it has each side (top, left, down and right) adjacent to other block or to the floor. Otherwise, block is boundary. In one operation Limak destroys all boundary blocks. His paws are very fast and he destroys all those blocks at the same time. Limak is ready to start. You task is to count how many operations will it take him to destroy all towers. -----Input----- The first line contains single integer n (1 ≤ n ≤ 10^5). The second line contains n space-separated integers h_1, h_2, ..., h_{n} (1 ≤ h_{i} ≤ 10^9) — sizes of towers. -----Output----- Print the number of operations needed to destroy all towers. -----Examples----- Input 6 2 1 4 6 2 2 Output 3 Input 7 3 3 3 1 3 3 3 Output 2 -----Note----- The picture below shows all three operations for the first sample test. Each time boundary blocks are marked with red color. [Image] After first operation there are four blocks left and only one remains after second operation. This last block is destroyed in third operation.
n = int(input()) ar = list(map(int, input().split())) r = [] l = [] day = 0 for i in ar: day += 1 day = min(i, day) l.append(day) day = 0 for i in reversed(ar): day += 1 day = min(i, day) r.append(day) ans = 0 x = 0 kk = n - 1 while x < n: ans = max(ans, min(r[kk], l[x])) x += 1 kk -= 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 LIST ASSIGN VAR NUMBER FOR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Limak is a little bear who loves to play. Today he is playing by destroying block towers. He built n towers in a row. The i-th tower is made of h_{i} identical blocks. For clarification see picture for the first sample. Limak will repeat the following operation till everything is destroyed. Block is called internal if it has all four neighbors, i.e. it has each side (top, left, down and right) adjacent to other block or to the floor. Otherwise, block is boundary. In one operation Limak destroys all boundary blocks. His paws are very fast and he destroys all those blocks at the same time. Limak is ready to start. You task is to count how many operations will it take him to destroy all towers. -----Input----- The first line contains single integer n (1 ≤ n ≤ 10^5). The second line contains n space-separated integers h_1, h_2, ..., h_{n} (1 ≤ h_{i} ≤ 10^9) — sizes of towers. -----Output----- Print the number of operations needed to destroy all towers. -----Examples----- Input 6 2 1 4 6 2 2 Output 3 Input 7 3 3 3 1 3 3 3 Output 2 -----Note----- The picture below shows all three operations for the first sample test. Each time boundary blocks are marked with red color. [Image] After first operation there are four blocks left and only one remains after second operation. This last block is destroyed in third operation.
x = int(input()) y = list(map(int, input().split(" "))) y[0] = 1 y[x - 1] = 1 z = y[:] for i in range(1, x): z[i] = min(z[i], z[i - 1] + 1) w = y[:] for i in range(x - 2, -1, -1): w[i] = min(w[i], w[i + 1] + 1) ans = 0 for i in range(x): ans = max(ans, min(z[i], w[i])) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR 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 ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Limak is a little bear who loves to play. Today he is playing by destroying block towers. He built n towers in a row. The i-th tower is made of h_{i} identical blocks. For clarification see picture for the first sample. Limak will repeat the following operation till everything is destroyed. Block is called internal if it has all four neighbors, i.e. it has each side (top, left, down and right) adjacent to other block or to the floor. Otherwise, block is boundary. In one operation Limak destroys all boundary blocks. His paws are very fast and he destroys all those blocks at the same time. Limak is ready to start. You task is to count how many operations will it take him to destroy all towers. -----Input----- The first line contains single integer n (1 ≤ n ≤ 10^5). The second line contains n space-separated integers h_1, h_2, ..., h_{n} (1 ≤ h_{i} ≤ 10^9) — sizes of towers. -----Output----- Print the number of operations needed to destroy all towers. -----Examples----- Input 6 2 1 4 6 2 2 Output 3 Input 7 3 3 3 1 3 3 3 Output 2 -----Note----- The picture below shows all three operations for the first sample test. Each time boundary blocks are marked with red color. [Image] After first operation there are four blocks left and only one remains after second operation. This last block is destroyed in third operation.
def powmod(x, p, m): if p <= 0: return 1 if p <= 1: return x % m return powmod(x * x % m, p // 2, m) * (x % m) ** (p % 2) % m n = int(input()) h = [0] + [int(x) for x in input().split()] + [0] for i in range(1, len(h)): h[i] = min(h[i], h[i - 1] + 1) h[-i - 1] = min(h[-i - 1], h[-i] + 1) ans = max(h) print(ans)
FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN BIN_OP VAR VAR RETURN BIN_OP BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER 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 EXPR FUNC_CALL VAR VAR
Limak is a little bear who loves to play. Today he is playing by destroying block towers. He built n towers in a row. The i-th tower is made of h_{i} identical blocks. For clarification see picture for the first sample. Limak will repeat the following operation till everything is destroyed. Block is called internal if it has all four neighbors, i.e. it has each side (top, left, down and right) adjacent to other block or to the floor. Otherwise, block is boundary. In one operation Limak destroys all boundary blocks. His paws are very fast and he destroys all those blocks at the same time. Limak is ready to start. You task is to count how many operations will it take him to destroy all towers. -----Input----- The first line contains single integer n (1 ≤ n ≤ 10^5). The second line contains n space-separated integers h_1, h_2, ..., h_{n} (1 ≤ h_{i} ≤ 10^9) — sizes of towers. -----Output----- Print the number of operations needed to destroy all towers. -----Examples----- Input 6 2 1 4 6 2 2 Output 3 Input 7 3 3 3 1 3 3 3 Output 2 -----Note----- The picture below shows all three operations for the first sample test. Each time boundary blocks are marked with red color. [Image] After first operation there are four blocks left and only one remains after second operation. This last block is destroyed in third operation.
R = lambda: map(int, input().split()) n = int(input()) arr = [-1] + list(R()) + [-1] dpl, dpr = arr[:], arr[:] for i in range(1, n + 1): dpl[i] = max(0, min(dpl[i - 1] + 1, arr[i] - 1)) for i in range(n, 0, -1): dpr[i] = max(0, min(dpr[i + 1] + 1, arr[i] - 1)) print(1 + max(min(l, r) for l, r in zip(dpl, dpr)))
ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP LIST NUMBER FUNC_CALL VAR FUNC_CALL VAR LIST NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR
Limak is a little bear who loves to play. Today he is playing by destroying block towers. He built n towers in a row. The i-th tower is made of h_{i} identical blocks. For clarification see picture for the first sample. Limak will repeat the following operation till everything is destroyed. Block is called internal if it has all four neighbors, i.e. it has each side (top, left, down and right) adjacent to other block or to the floor. Otherwise, block is boundary. In one operation Limak destroys all boundary blocks. His paws are very fast and he destroys all those blocks at the same time. Limak is ready to start. You task is to count how many operations will it take him to destroy all towers. -----Input----- The first line contains single integer n (1 ≤ n ≤ 10^5). The second line contains n space-separated integers h_1, h_2, ..., h_{n} (1 ≤ h_{i} ≤ 10^9) — sizes of towers. -----Output----- Print the number of operations needed to destroy all towers. -----Examples----- Input 6 2 1 4 6 2 2 Output 3 Input 7 3 3 3 1 3 3 3 Output 2 -----Note----- The picture below shows all three operations for the first sample test. Each time boundary blocks are marked with red color. [Image] After first operation there are four blocks left and only one remains after second operation. This last block is destroyed in third operation.
def main(): input() hh = list(map(int, input().split())) for f in (True, False): m = 1 for i, h in enumerate(hh): if h > m: hh[i] = m else: m = h m += 1 if f: hh.reverse() print(max(hh)) main()
FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
def bfs(arr, i, d, memo): if i in memo: return memo[i] res = 1 for j in range(max(i - 1, 0), max(i - d - 1, -1), -1): if arr[j] >= arr[i]: break res = max(res, bfs(arr, j, d, memo) + 1) for j in range(i + 1, min(i + d + 1, len(arr))): if arr[j] >= arr[i]: break res = max(res, bfs(arr, j, d, memo) + 1) memo[i] = res return res class Solution: def maxJumps(self, arr: List[int], d: int) -> int: memo = dict() return max(bfs(arr, i, d, memo) for i in range(len(arr)))
FUNC_DEF IF VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: memo = {} def solve(i): if i in memo: return memo[i] ret = 1 j = i - 1 while j >= max(0, i - d) and arr[j] < arr[i]: ret = max(ret, 1 + solve(j)) j -= 1 j = i + 1 while j <= min(len(arr) - 1, i + d) and arr[j] < arr[i]: ret = max(ret, 1 + solve(j)) j += 1 memo[i] = ret return ret for i in range(len(arr)): solve(i) return max(memo.values())
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, nums: List[int], d: int) -> int: nums.append(math.inf) dp = [1] * len(nums) stack = [] for i, cur in enumerate(nums): while stack and nums[stack[-1]] < cur: poped_same_val = [stack.pop()] while stack and nums[stack[-1]] == nums[poped_same_val[0]]: poped_same_val.append(stack.pop()) for poped in poped_same_val: if i - poped <= d: dp[i] = max(dp[i], dp[poped] + 1) if stack and poped - stack[-1] <= d: left_first_big_of_poped = stack[-1] dp[left_first_big_of_poped] = max( dp[left_first_big_of_poped], dp[poped] + 1 ) stack.append(i) return max(dp[:-1])
CLASS_DEF FUNC_DEF VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR NUMBER VAR ASSIGN VAR LIST FUNC_CALL VAR WHILE VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR NUMBER VAR
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: book = [1] * len(arr) index = collections.defaultdict(list) for i in range(len(arr)): index[arr[i]].append(i) val = list(index.keys()) val.sort(reverse=True) for v in val: for i in index[v]: height = arr[i] for j in range(i + 1, i + d + 1): if j == len(arr): break if arr[j] > height: book[i] = max(book[i], book[j] + 1) height = arr[j] height = arr[i] for j in range(i - 1, i - d - 1, -1): if j == -1: break if arr[j] > height: book[i] = max(book[i], book[j] + 1) height = arr[j] return max(book)
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER FOR VAR VAR FOR VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def rec(self, v): if self.memo[v] != -1: return self.memo[v] res = 1 for nv in self.G[v]: res = max(res, 1 + self.rec(nv)) self.memo[v] = res return res def maxJumps(self, arr: List[int], d: int) -> int: n = len(arr) self.G = [[] for _ in range(n)] for i in range(n): for j in range(i - 1, i - d - 1, -1): if j < 0 or arr[j] >= arr[i]: break self.G[i].append(j) for j in range(i + 1, i + d + 1): if j >= n or arr[j] >= arr[i]: break self.G[i].append(j) self.memo = [-1] * n ans = 0 for i in range(n): ans = max(ans, self.rec(i)) return ans
CLASS_DEF FUNC_DEF IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR RETURN VAR FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: def helper(arr, index, d, memo): if arr[index] < 0 or not 0 <= index < len(arr): return 0 if index in memo: return memo[index] left_visited = 0 right_visited = 0 arr[index] = -arr[index] for i in range(index + 1, index + d + 1): if i >= len(arr): break if abs(arr[i]) >= abs(arr[index]): break if arr[i] < 0: continue left_visited = max(left_visited, helper(arr, i, d, memo)) for j in range(index - 1, index - d - 1, -1): if j < 0: break if abs(arr[j]) >= abs(arr[index]): break if arr[j] < 0: continue right_visited = max(right_visited, helper(arr, j, d, memo)) arr[index] = -arr[index] memo[index] = 1 + max(left_visited, right_visited) return memo[index] max_visited = 1 p = False memo = {} for index in range(len(arr)): visited = helper(arr, index, d, memo) max_visited = max(visited, max_visited) return max_visited
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF IF VAR VAR NUMBER NUMBER VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR NUMBER IF FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: n = len(arr) i2h = {} for i, h in enumerate(arr): i2h[i] = h, i dp = [1] * n for h, idx in sorted(i2h.values(), key=lambda hi: hi[0]): j = idx + 1 while j <= min(n - 1, idx + d) and arr[idx] > arr[j]: dp[idx] = max(dp[idx], dp[j] + 1) j += 1 j = idx - 1 while j >= max(0, idx - d) and arr[idx] > arr[j]: dp[idx] = max(dp[idx], dp[j] + 1) j -= 1 return max(dp) def maxJumps__(self, arr: List[int], d: int) -> int: n = len(arr) memo = [0] * n def dfs(idx): if memo[idx]: return memo[idx] jump = 1 j = idx + 1 while j <= min(n - 1, idx + d) and arr[idx] > arr[j]: jump = max(jump, dfs(j) + 1) j += 1 j = idx - 1 while j >= max(0, idx - d) and arr[idx] > arr[j]: jump = max(jump, dfs(j) + 1) j -= 1 memo[idx] = jump return jump max_val = 0 for i in range(n): max_val = max(max_val, dfs(i)) return max_val def maxJumps_(self, arr: List[int], d: int) -> int: def backtracking(idx): nonlocal memo, max_val if idx in memo: return memo[idx] lstack = [] rstack = [] lbarrier = False rbarrier = False for i in range(1, d + 1): if not lbarrier and idx - i >= 0 and arr[idx - i] < arr[idx]: lstack.append(idx - i) else: lbarrier = True if not rbarrier and idx + i < len(arr) and arr[idx + i] < arr[idx]: rstack.append(idx + i) else: rbarrier = True ljump = 1 while lstack: lidx = lstack.pop() ljump = max(ljump, 1 + backtracking(lidx)) rjump = 1 while rstack: ridx = rstack.pop() rjump = max(rjump, 1 + backtracking(ridx)) jump = max(ljump, rjump) memo[idx] = jump max_val = max(max_val, jump) return jump memo = {} max_val = 0 for i in range(len(arr)): backtracking(i) return max_val
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR VAR FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF IF VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR FUNC_DEF VAR VAR VAR FUNC_DEF IF VAR VAR RETURN VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: @lru_cache(None) def helper(idx): ans = 1 ub, lb = 1, 1 while ub <= d or lb <= d: if idx + ub < len(arr) and arr[idx + ub] < arr[idx]: ans = max(ans, 1 + helper(idx + ub)) ub += 1 else: ub = sys.maxsize if idx - lb >= 0 and arr[idx - lb] < arr[idx]: ans = max(ans, 1 + helper(idx - lb)) lb += 1 else: lb = sys.maxsize return ans return max([helper(idx) for idx in range(len(arr))])
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR VAR VAR IF BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR FUNC_CALL VAR NONE RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, A: List[int], d: int) -> int: n = len(A) @lru_cache(None) def helper(i): ans = 1 l = max(0, i - d) r = min(n - 1, i + d) for j in reversed(range(l, i)): if A[j] < A[i]: ans = max(ans, 1 + helper(j)) else: break for j in range(i + 1, r + 1): if A[j] < A[i]: ans = max(ans, 1 + helper(j)) else: break return ans ans = max([helper(i) for i in range(n)]) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR RETURN VAR FUNC_CALL VAR NONE ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: maxPath = 0 memory = {i: (0) for i in range(len(arr))} for i in range(len(arr)): maxPath = max(maxPath, self.dfs(i, d, arr, len(arr), memory)) return maxPath def dfs(self, index, d, arr, arrLen, memory): if memory[index] != 0: return memory[index] currentMax = 0 for i in range(index + 1, index + d + 1): if i >= arrLen or arr[index] <= arr[i]: break currentMax = max(currentMax, self.dfs(i, d, arr, arrLen, memory)) for i in range(index - 1, index - d - 1, -1): if i < 0 or arr[index] <= arr[i]: break currentMax = max(currentMax, self.dfs(i, d, arr, arrLen, memory)) memory[index] = currentMax + 1 return memory[index]
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR FUNC_DEF IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: N = len(arr) dp = [None] * N def cdp(i): if dp[i] != None: return dp[i] dp[i] = 1 for j in range(i - 1, max(0, i - d) - 1, -1): if arr[j] >= arr[i]: break dp[i] = max(dp[i], 1 + cdp(j)) for j in range(i + 1, min(N - 1, i + d) + 1): if arr[j] >= arr[i]: break dp[i] = max(dp[i], 1 + cdp(j)) return dp[i] for i in range(N): cdp(i) return max(dp)
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NONE VAR FUNC_DEF IF VAR VAR NONE RETURN VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR RETURN VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: mem = [0] * len(arr) def dfs(i): if mem[i]: return mem[i] num = 1 j = i + 1 while j in range(i + 1, min(len(arr), i + d + 1)) and arr[j] < arr[i]: num = max(num, dfs(j) + 1) j += 1 j = i - 1 while j in range(max(0, i - d), i) and arr[j] < arr[i]: num = max(num, dfs(j) + 1) j -= 1 mem[i] = num return mem[i] res = 0 for i in range(len(arr)): dfs(i) res = max(res, dfs(i) + 1) return max(mem)
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, A: List[int], d: int) -> int: dp = [1] * len(A) steps = [(v, i) for i, v in enumerate(A)] steps.sort() for val, i in steps: maxVal = val for j in range(i + 1, min(i + d + 1, len(steps))): if A[j] > maxVal: maxVal = max(maxVal, A[j]) dp[j] = max(dp[j], dp[i] + 1) maxVal = val for j in reversed(range(max(i - d, 0), i)): if A[j] > maxVal: maxVal = max(maxVal, A[j]) dp[j] = max(dp[j], dp[i] + 1) return max(dp)
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: visited = {} arrLen = len(arr) def check(i0): if i0 in visited: return visited[i0] di = 1 preV = 0 subAns = 1 while di <= d and i0 + di < arrLen and arr[i0 + di] < arr[i0]: if arr[i0 + di] >= preV: subAns = max(subAns, 1 + check(i0 + di)) preV = max(preV, arr[i0 + di]) di += 1 di = 1 preV = 0 while di <= d and i0 - di >= 0 and arr[i0 - di] < arr[i0]: if arr[i0 - di] >= preV: subAns = max(subAns, 1 + check(i0 - di)) preV = max(preV, arr[i0 - di]) di += 1 visited[i0] = subAns return subAns ans = 1 for i in range(arrLen): ans = max(ans, check(i)) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR IF VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR IF VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], dis: int) -> int: res = 1 d = dict() visit = set() if len(arr) == 1: return 1 for i in range(len(arr)): if i == 0: if arr[i] <= arr[i + 1]: d[i] = 1 visit.add(i) elif i == len(arr) - 1: if arr[i] <= arr[i - 1]: d[i] = 1 visit.add(i) elif arr[i] <= arr[i - 1] and arr[i] <= arr[i + 1]: d[i] = 1 visit.add(i) def dfs(index): if index not in visit: visit.add(index) cur = 1 for i in range(1, dis + 1): if index - i >= 0 and arr[index - i] < arr[index]: temp = dfs(index - i) d[index - i] = temp cur = max(cur, 1 + temp) else: break for i in range(1, dis + 1): if index + i < len(arr) and arr[index + i] < arr[index]: temp = dfs(index + i) d[index + i] = temp cur = max(cur, 1 + temp) else: break return cur else: return d[index] for x in range(len(arr)): if x not in visit: cur = dfs(x) d[x] = cur res = max(res, cur) else: res = max(res, d[x]) return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR RETURN VAR RETURN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, A: List[int], d: int) -> int: n = len(A) dp = [1] * n for a, i in sorted([a, i] for i, a in enumerate(A)): j = i - 1 while j >= 0 and A[j] < A[i] and i - j <= d: dp[i] = max(dp[i], dp[j] + 1) j -= 1 j = i + 1 while j < n and A[j] < A[i] and j - i <= d: dp[i] = max(dp[i], dp[j] + 1) j += 1 return max(dp)
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR LIST VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR VAR
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: dp = [(0) for i in range(len(arr))] def findMaxJumps(i): if dp[i] != 0: return dp[i] else: ans = 1 for j in range(i + 1, min(i + d + 1, len(arr))): if arr[j] >= arr[i]: break ans = max(ans, 1 + findMaxJumps(j)) for j in range(i - 1, max(i - d, 0) - 1, -1): if arr[j] >= arr[i]: break ans = max(ans, 1 + findMaxJumps(j)) dp[i] = ans return ans ans = 1 for i in range(len(arr)): ans = max(ans, findMaxJumps(i)) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: ans = [1] * len(arr) def dp(i): nonlocal ans, arr, d if ans[i] != 1: return ans[i] for dr in [-1, 1]: for k in range(i, i + d * dr + dr, dr): if k < 0 or k == i: continue if k > len(arr) - 1 or arr[k] >= arr[i]: break ans[i] = max(ans[i], 1 + dp(k)) return ans[i] for i in range(len(arr)): dp(i) return max(ans)
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR NUMBER RETURN VAR VAR FOR VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR RETURN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution2: def maxJumps(self, arr, d): n = len(arr) dp = [1] * n for a, i in sorted([a, i] for i, a in enumerate(arr)): for di in [-1, 1]: for j in range(i + di, i + di + d * di, di): if not (0 <= j < n and arr[j] < arr[i]): break dp[i] = max(dp[i], dp[j] + 1) return max(dp) class Solution: def maxJumps(self, arr, d): n = len(arr) dp = [1] * (n + 1) stack = [] for i, a in enumerate(arr + [float("inf")]): while stack and arr[stack[-1]] < a: L = [stack.pop()] while stack and arr[stack[-1]] == arr[L[0]]: L.append(stack.pop()) for j in L: if i - j <= d: dp[i] = max(dp[i], dp[j] + 1) if stack and j - stack[-1] <= d: dp[stack[-1]] = max(dp[stack[-1]], dp[j] + 1) stack.append(i) return max(dp[:-1]) class Solution1: def maxJumps(self, arr: List[int], d: int) -> int: n = len(arr) res = [0] * n def dp(i): if res[i]: return res[i] res[i] = 1 for di in [-1, 1]: for j in range(i + di, i + di + d * di, di): if not (0 <= j < n and arr[j] < arr[i]): break res[i] = max(res[i], dp(j) + 1) return res[i] return max(list(map(dp, list(range(n)))))
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR LIST VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR IF NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR BIN_OP VAR LIST FUNC_CALL VAR STRING WHILE VAR VAR VAR NUMBER VAR ASSIGN VAR LIST FUNC_CALL VAR WHILE VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR NUMBER CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF IF VAR VAR RETURN VAR VAR ASSIGN VAR VAR NUMBER FOR VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR IF NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: max_less_than = [] def jump(iter): res = [None for i in range(len(arr))] stack = deque([]) for i in iter: while stack and abs(i - stack[0]) > d: stack.popleft() while stack and arr[i] > arr[stack[-1]]: latest_poped = stack.pop() res[latest_poped] = i stack.append(i) return res max_less_than.append(jump(range(len(arr)))) max_less_than.append(jump(reversed(range(len(arr))))) @lru_cache(None) def dp(i): if i is None: return 0 return ( max(map(dp, [max_less_than[0][i], max_less_than[1][i]]), default=0) + 1 ) return max(map(dp, range(len(arr))))
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST FUNC_DEF ASSIGN VAR NONE VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR LIST FOR VAR VAR WHILE VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR NONE RETURN NUMBER RETURN BIN_OP FUNC_CALL VAR FUNC_CALL VAR VAR LIST VAR NUMBER VAR VAR NUMBER VAR NUMBER NUMBER FUNC_CALL VAR NONE RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: n = len(arr) dp = [(1) for i in range(n)] p = [] for idx, x in enumerate(arr): p.append((x, idx)) p.sort(reverse=True) for i in range(n): idx = p[i][1] for j in range(idx + 1, min(n, d + idx + 1)): if arr[j] >= arr[idx]: break dp[j] = max(dp[j], dp[idx] + 1) for j in range(idx - 1, max(-1, idx - d - 1), -1): if arr[j] >= arr[idx]: break dp[j] = max(dp[j], dp[idx] + 1) return max([dp[i] for i in range(n)])
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: def find_longest_l(pos, d): if pos - 1 < 0 or arr[pos - 1] > arr[pos]: return pos i = pos while i > 0 and abs(pos - i) < d and arr[i - 1] < arr[pos]: i -= 1 return i def find_longest_r(pos, d): if pos + 1 >= len(arr) or arr[pos + 1] > arr[pos]: return pos i = pos while i < len(arr) - 1 and abs(pos - i) < d and arr[i + 1] < arr[pos]: i += 1 return i def jump(pos, number_of_locations, seen): maximum_locations = number_of_locations L = find_longest_l(pos, d) R = find_longest_r(pos, d) for i in range(L, R + 1): if i == pos: continue if i not in seen: jumps_to_i = jump(i, number_of_locations, seen) maximum_locations = max(maximum_locations, jumps_to_i + 1) else: jumps_to_i = seen[i] maximum_locations = max(maximum_locations, jumps_to_i + 1) seen[pos] = maximum_locations return maximum_locations indices = [(i, arr[i]) for i in range(len(arr))] indices.sort(key=lambda x: x[1], reverse=True) seen = {} return max(jump(x[0], 1, seen) for x in indices)
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF IF BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR ASSIGN VAR VAR WHILE VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER RETURN VAR FUNC_DEF IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR RETURN VAR ASSIGN VAR VAR WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR DICT RETURN FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR VAR VAR VAR
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: def backtracking(idx): nonlocal memo, max_val if idx in memo: return memo[idx] lstack = [] rstack = [] lbarrier = False rbarrier = False for i in range(1, d + 1): if not lbarrier and idx - i >= 0 and arr[idx - i] < arr[idx]: lstack.append(idx - i) else: lbarrier = True if not rbarrier and idx + i < len(arr) and arr[idx + i] < arr[idx]: rstack.append(idx + i) else: rbarrier = True ljump = 1 while lstack: lidx = lstack.pop() ljump = max(ljump, 1 + backtracking(lidx)) rjump = 1 while rstack: ridx = rstack.pop() rjump = max(rjump, 1 + backtracking(ridx)) jump = max(ljump, rjump) memo[idx] = jump max_val = max(max_val, jump) return jump memo = {} max_val = 0 for i in range(len(arr)): backtracking(i) return max_val
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF IF VAR VAR RETURN VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: N = len(arr) @lru_cache(None) def rec(idx): if not 0 <= idx < N: return -inf ans = 1 for i in range(idx - 1, idx - d - 1, -1): if i < 0 or arr[i] >= arr[idx]: break ans = max(ans, rec(i) + 1) for i in range(idx + 1, idx + d + 1): if i >= N or arr[i] >= arr[idx]: break ans = max(ans, rec(i) + 1) return ans ans = 0 for i in range(N): ans = max(ans, rec(i)) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF IF NUMBER VAR VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR FUNC_CALL VAR NONE ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: n = len(arr) if n <= 1: return n visited = [(False) for i in range(n)] L_neighbor = {} R_neighbor = {} for i in range(n): L_neighbor[i] = i R_neighbor[i] = i dec = deque() dec.append(0) for i in range(1, n): while len(dec) > 0: if dec[0] < i - d: dec.popleft() else: break while len(dec) > 0: if arr[dec[len(dec) - 1]] < arr[i]: dec.pop() else: break if len(dec) == 0: L_neighbor[i] = max(i - d, 0) dec.append(i) else: L_neighbor[i] = max(dec[len(dec) - 1] + 1, i - d, 0) dec.append(i) dec = deque() dec.append(n - 1) for i in range(n - 2, -1, -1): while len(dec) > 0: if dec[0] > i + d: dec.popleft() else: break while len(dec) > 0: if arr[dec[len(dec) - 1]] < arr[i]: dec.pop() else: break if len(dec) == 0: R_neighbor[i] = min(i + d, n - 1) dec.append(i) else: R_neighbor[i] = min(dec[len(dec) - 1] - 1, i + d, n - 1) dec.append(i) res = 0 limit = [(0) for i in range(n)] for i in range(n): self.maxJumpDFS(i, arr, L_neighbor, R_neighbor, visited, limit) for i in range(n): if res < limit[i]: res = limit[i] return res def maxJumpDFS(self, start, arr, L_neighbor, R_neighbor, visited, limit): if visited[start]: return limit[start] else: currmax = 0 for i in range(L_neighbor[start], R_neighbor[start] + 1): if i == start: continue curr = self.maxJumpDFS(i, arr, L_neighbor, R_neighbor, visited, limit) if currmax < curr: currmax = curr visited[start] = True limit[start] = currmax + 1 return limit[start]
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR WHILE FUNC_CALL VAR VAR NUMBER IF VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR WHILE FUNC_CALL VAR VAR NUMBER IF VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE FUNC_CALL VAR VAR NUMBER IF VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR WHILE FUNC_CALL VAR VAR NUMBER IF VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR VAR FUNC_DEF IF VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: dp = [(-1) for _ in range(len(arr))] def dp_memo(x): if dp[x] != -1: return dp[x] dp[x] = 1 for y in range(x - 1, x - d - 1, -1): if y < 0 or arr[y] >= arr[x]: break dp[x] = max(dp[x], 1 + dp_memo(y)) for y in range(x + 1, x + d + 1, +1): if y >= len(arr) or arr[y] >= arr[x]: break dp[x] = max(dp[x], 1 + dp_memo(y)) return dp[x] for i in range(len(arr)): dp_memo(i) print(dp) return max(dp)
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR RETURN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: n = len(arr) dp = [(1) for i in range(n)] @lru_cache(None) def rec(i): j = 1 while j <= d and i - j >= 0 and arr[i - j] < arr[i]: dp[i] = max(dp[i], 1 + rec(i - j)) j += 1 j = 1 while j <= d and i + j < n and arr[i + j] < arr[i]: dp[i] = max(dp[i], 1 + rec(i + j)) j += 1 return dp[i] for k in range(n): rec(k) return max(dp)
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR NUMBER WHILE VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR VAR FUNC_CALL VAR NONE FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: n = len(arr) dp = [1] * n for a, i in sorted([a, i] for i, a in enumerate(arr)): for di in [-1, 1]: for j in range(i + di, i + d * di + di, di): if not (0 <= j < n and arr[j] < arr[i]): break dp[i] = max(dp[i], dp[j] + 1) return max(dp)
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR LIST VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR IF NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: def helper(i): if cache[i]: return cache[i] numberOfJump = 0 for j in range(i + 1, i + d + 1): if j >= len(arr) or arr[j] >= arr[i]: break numberOfJump = max(helper(j), numberOfJump) for j in range(i - 1, i - d - 1, -1): if j < 0 or arr[j] >= arr[i]: break numberOfJump = max(helper(j), numberOfJump) cache[i] = 1 + numberOfJump return cache[i] cache = [0] * len(arr) ans = 0 for i in range(len(arr)): ans = max(helper(i), ans) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF IF VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR RETURN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: n = len(arr) graph = collections.defaultdict(list) st = [] for i in range(n): while st and arr[st[-1]] < arr[i]: j = st.pop() if i - j <= d: graph[j].append(i) st.append(i) st = [] for i in range(n - 1, -1, -1): while st and arr[st[-1]] < arr[i]: j = st.pop() if j - i <= d: graph[j].append(i) st.append(i) visited = {} def dfs(i): if i in visited: return visited[i] step = 1 for j in graph[i]: step = max(step, 1 + dfs(j)) visited[i] = step return step res = 0 for i in range(n): res = max(res, dfs(i)) return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE VAR VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR IF BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: dp = {} def jump(i): if i in dp: return dp[i] m = 1 step = 1 while i + step < len(arr) and step <= d: if arr[i] <= arr[i + step]: break m = max(m, jump(i + step) + 1) step += 1 step = 1 while i - step >= 0 and step <= d: if arr[i] <= arr[i - step]: break m = max(m, jump(i - step) + 1) step += 1 dp[i] = m return m ans = 0 for i in range(len(arr)): if i not in dp: jump(i) ans = max(ans, dp[i]) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR VAR IF VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: @lru_cache(None) def jump(mid): left = max(mid - d, 0) right = min(mid + d, len(arr) - 1) res = 1 for i in range(mid - 1, left - 1, -1): if arr[i] >= arr[mid]: break res = max(res, jump(i) + 1) for i in range(mid + 1, right + 1): if arr[i] >= arr[mid]: break res = max(res, jump(i) + 1) return res res = [1] * len(arr) for i in range(len(arr)): res[i] = jump(i) return max(res)
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR FUNC_CALL VAR NONE ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: l = len(arr) self.check, self.seen = [[] for _ in range(l)], dict() for i, v in enumerate(arr): for sign in (1, -1): for x in range(sign, (d + 1) * sign, sign): if i + x not in list(range(l)) or v <= arr[i + x]: break self.check[i].append(i + x) if not self.check[i]: self.seen[i] = 1 return max(self.helper(i) for i in range(l)) def helper(self, p): if p not in self.seen: res = 0 for n in self.check[p]: res = max(res, self.seen.get(n, self.helper(n)) + 1) self.seen[p] = res return self.seen[p]
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST VAR FUNC_CALL VAR VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR VAR IF BIN_OP VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR VAR
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: @lru_cache(None) def dfs(i): if i == 0 and i + 1 < len(arr): if arr[i] <= arr[i + 1]: return 1 elif i == len(arr) - 1 and i - 1 >= 0: if arr[i] <= arr[i - 1]: return 1 elif ( i - 1 >= 0 and i + 1 < len(arr) and arr[i] <= arr[i - 1] and arr[i] <= arr[i + 1] ): return 1 left_step = 1 for j in range(i + 1, i + d + 1): if j >= len(arr) or arr[j] >= arr[i]: break left_step = max(left_step, 1 + dfs(j)) right_step = 1 for j in range(i - 1, i - d - 1, -1): if j < 0 or arr[j] >= arr[i]: break right_step = max(right_step, 1 + dfs(j)) return max(left_step, right_step) max_step = 0 for i in range(len(arr)): max_step = max(max_step, dfs(i)) return max_step
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF IF VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR NUMBER VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_CALL VAR NONE ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, A: List[int], d: int) -> int: N = len(A) jumpable = collections.defaultdict(list) def find_jumpable_indices(iter): stack = [] for i in iter: while stack and A[stack[-1]] < A[i]: j = stack.pop() if abs(i - j) <= d: jumpable[i].append(j) stack.append(i) find_jumpable_indices(list(range(N))) find_jumpable_indices(reversed(list(range(N)))) dp = [-1] * N def dfs(idx): if dp[idx] > -1: return dp[idx] res = 1 for j in jumpable[idx]: res = max(res, 1 + dfs(j)) dp[idx] = res return res return max(list(map(dfs, list(range(N)))))
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR LIST FOR VAR VAR WHILE VAR VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: N = len(arr) umap = [list() for i in range(N)] dp = [(-1) for i in range(N)] def dfs(idx: int) -> int: if idx >= N or idx < 0: return 0 if dp[idx] != -1: return dp[idx] curr = 0 for u in umap[idx]: curr = max(curr, dfs(u)) dp[idx] = curr + 1 return dp[idx] for i in range(N): minIdx = max(0, i - d) maxIdx = min(N - 1, i + d) for t in range(i - 1, minIdx - 1, -1): if arr[i] > arr[t]: umap[i].append(t) else: break for t in range(i + 1, maxIdx + 1): if arr[i] > arr[t]: umap[i].append(t) else: break result = 0 for i in range(N): result = max(result, dfs(i)) return result
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_DEF VAR IF VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: arr1 = [[i, v] for i, v in enumerate(arr)] arr1.sort(key=lambda x: x[1]) dp = [0] * len(arr) def dfs(start, arr, dp): if dp[start] != 0: return dp[start] dp[start] = 1 for i in range(start - 1, max(start - d - 1, -1), -1): if arr[start] <= arr[i]: break dp[start] = max(dp[start], dfs(i, arr, dp) + 1) for i in range(start + 1, min(start + d + 1, len(arr))): if arr[start] <= arr[i]: break dp[start] = max(dp[start], dfs(i, arr, dp) + 1) return dp[start] for i in range(len(arr1)): dfs(arr1[i][0], arr, dp) return max(dp)
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST VAR VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR VAR RETURN FUNC_CALL VAR VAR VAR
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: ans = dict() def dfs(idx): if idx in ans: return ans[idx] = 1 i = idx - 1 while i >= 0 and idx - i <= d and arr[i] < arr[idx]: dfs(i) ans[idx] = max(ans[idx], ans[i] + 1) i -= 1 i = idx + 1 while i < len(arr) and i - idx <= d and arr[i] < arr[idx]: dfs(i) ans[idx] = max(ans[idx], ans[i] + 1) i += 1 for i in range(len(arr)): dfs(i) print(ans) return max(ans.values())
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF IF VAR VAR RETURN ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER BIN_OP VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: a = [] jump = [0] * len(arr) result = 0 for i in range(len(arr)): a.append((i, arr[i])) a.sort(key=lambda x: x[1]) for i in range(len(arr)): l = a[i][0] - 1 r = a[i][0] + 1 m = 0 while l >= 0 and arr[l] < a[i][1] and a[i][0] - l <= d: m = max(m, jump[l]) l -= 1 while r < len(arr) and arr[r] < a[i][1] and r - a[i][0] <= d: m = max(m, jump[r]) r += 1 jump[a[i][0]] = m + 1 result = max(result, m + 1) return result
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: memo = [-1] * len(arr) def dfs(i): if i < 0 or i >= len(arr): return 0 if memo[i] > 0: return memo[i] memo[i] = 1 for j in reversed(list(range(max(0, i - d), i))): if arr[j] >= arr[i]: break memo[i] = max(memo[i], dfs(j) + 1) for j in range(i + 1, min(len(arr), i + d + 1)): if arr[j] >= arr[i]: break memo[i] = max(memo[i], dfs(j) + 1) return memo[i] return max(dfs(i) for i in range(len(arr)))
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FUNC_DEF IF VAR NUMBER VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: max_pos = 1 memo = {} def helper(i): if i in list(memo.keys()): return memo[i] res = 0 j = i - 1 while 0 <= j < len(arr) and i - j <= d and arr[j] < arr[i]: res = max(res, helper(j)) j -= 1 j = i + 1 while 0 <= j < len(arr) and j - i <= d and arr[j] < arr[i]: res = max(res, helper(j)) j += 1 res = res + 1 memo[i] = res return res for i in range(len(arr)): max_pos = max(max_pos, helper(i)) return max_pos
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FUNC_DEF IF VAR FUNC_CALL VAR FUNC_CALL VAR RETURN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE NUMBER VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: def dfs(i): if i in cache: return cache[i] cache[i] = 1 for neigh in g[i]: cache[i] = max(cache[i], 1 + dfs(neigh)) return cache[i] g = defaultdict(set) for i in range(len(arr)): for x in range(1, d + 1): if i - x >= 0 and arr[i - x] < arr[i]: g[i].add(i - x) else: break for x in range(1, d + 1): if i + x < len(arr) and arr[i + x] < arr[i]: g[i].add(i + x) else: break cache = {} for i in range(len(arr)): dfs(i) return max(cache.values())
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF IF VAR VAR RETURN VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: self.memo = {} self.result = 0 for i in range(len(arr)): self.dfs(arr, i, d) return self.result def dfs(self, arr, index, d): if index in self.memo: return self.memo[index] result = 0 for dir in [-1, 1]: for i in range(1, d + 1): j = index + i * dir if 0 <= j < len(arr) and arr[index] > arr[j]: result = max(result, self.dfs(arr, j, d)) else: break self.memo[index] = result + 1 self.result = max(self.result, result + 1) return result + 1
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF IF VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR NUMBER
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: n = len(arr) cache = {} def find_max_jumps(i): if i in cache: return cache[i] max_jumps = 1 for dr in [1, -1]: for r in range(1, d + 1): if not (0 <= i + dr * r < n and arr[i + dr * r] < arr[i]): break max_jumps = max(max_jumps, 1 + find_max_jumps(i + dr * r)) cache[i] = max_jumps return max_jumps result = 1 for i in range(n): result = max(result, find_max_jumps(i)) return result class Solution111: def maxJumps(self, arr: List[int], d: int) -> int: n = len(arr) res = [0] * n def dp(i): if res[i]: return res[i] res[i] = 1 for di in [-1, 1]: for j in range(i + di, i + di + d * di, di): if not (0 <= j < n and arr[j] < arr[i]): break res[i] = max(res[i], dp(j) + 1) return res[i] return max(map(dp, range(n)))
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FUNC_DEF IF VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF NUMBER BIN_OP VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR RETURN VAR VAR CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF IF VAR VAR RETURN VAR VAR ASSIGN VAR VAR NUMBER FOR VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR IF NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, lst: List[int], d: int) -> int: def Ab(i, d, X): if i < 0 or i >= len(lst): return -1 if X <= lst[i]: return None if dp[i] != -1: return dp[i] m = 1 for j in range(1, d + 1): a = 0 a = Ab(i - j, d, lst[i]) if a == None: break m = max(m, 1 + a) for j in range(1, d + 1): b = 0 b = Ab(i + j, d, lst[i]) if b == None: break m = max(m, 1 + b) dp[i] = m return dp[i] dp = [(-1) for i in lst] for i in range(len(lst) - 1, -1, -1): Ab(i, d, sys.maxsize) return max(dp)
CLASS_DEF FUNC_DEF VAR VAR VAR FUNC_DEF IF VAR NUMBER VAR FUNC_CALL VAR VAR RETURN NUMBER IF VAR VAR VAR RETURN NONE IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR IF VAR NONE ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR IF VAR NONE ASSIGN VAR FUNC_CALL VAR VAR BIN_OP NUMBER VAR ASSIGN VAR VAR VAR RETURN VAR VAR ASSIGN VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR
Given an array of integers arr and an integer d. In one step you can jump from index i to index: i + x where: i + x < arr.length and 0 < x <= d. i - x where: i - x >= 0 and 0 < x <= d. In addition, you can only jump from index i to index j if arr[i] > arr[j] and arr[i] > arr[k] for all indices k between i and j (More formally min(i, j) < k < max(i, j)). You can choose any index of the array and start jumping. Return the maximum number of indices you can visit. Notice that you can not jump outside of the array at any time.   Example 1: Input: arr = [6,4,14,6,8,13,9,7,10,6,12], d = 2 Output: 4 Explanation: You can start at index 10. You can jump 10 --> 8 --> 6 --> 7 as shown. Note that if you start at index 6 you can only jump to index 7. You cannot jump to index 5 because 13 > 9. You cannot jump to index 4 because index 5 is between index 4 and 6 and 13 > 9. Similarly You cannot jump from index 3 to index 2 or index 1. Example 2: Input: arr = [3,3,3,3,3], d = 3 Output: 1 Explanation: You can start at any index. You always cannot jump to any index. Example 3: Input: arr = [7,6,5,4,3,2,1], d = 1 Output: 7 Explanation: Start at index 0. You can visit all the indicies. Example 4: Input: arr = [7,1,7,1,7,1], d = 2 Output: 2 Example 5: Input: arr = [66], d = 1 Output: 1   Constraints: 1 <= arr.length <= 1000 1 <= arr[i] <= 10^5 1 <= d <= arr.length
class Solution: def maxJumps(self, arr: List[int], d: int) -> int: dp = [0] * len(arr) path = {i: [(i, arr[i])] for i in range(len(arr))} def jump(i): nonlocal dp, path if dp[i] != 0: return dp[i] left, right = 0, 0 nl, nr = i, i for j in range(i + 1, i + d + 1): if j >= len(arr): break if arr[j] < arr[i]: t = jump(j) if right < t: right = t nr = j else: break for j in range(i - 1, i - d - 1, -1): if j < 0: break if arr[j] < arr[i]: t = jump(j) if left < t: left = t nl = j else: break if left < right and right > 0: path[i] += path[nr] elif left >= right and left > 0: path[i] += path[nl] dp[i] = max(left, right) + 1 return dp[i] ans = 0 for i in range(len(arr)): jump(i) ans = max(ans, dp[i]) return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR LIST VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR VAR