inputs
stringlengths 50
14k
| targets
stringlengths 4
655k
|
|---|---|
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cut and similarly, verticalCuts[j] is the distance from the left of the rectangular cake to the jth vertical cut.
Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts. Since the answer can be a huge number, return this modulo 10^9 + 7.
Example 1:
Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
Output: 4
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area.
Example 2:
Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]
Output: 6
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area.
Example 3:
Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
Output: 9
Constraints:
2 <= h, w <= 10^9
1 <= horizontalCuts.length < min(h, 10^5)
1 <= verticalCuts.length < min(w, 10^5)
1 <= horizontalCuts[i] < h
1 <= verticalCuts[i] < w
It is guaranteed that all elements in horizontalCuts are distinct.
It is guaranteed that all elements in verticalCuts are distinct.
|
class Solution:
def maxArea(self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int]) -> int:
n=len(horizontalCuts)
m=len(verticalCuts)
A=horizontalCuts[:]
B=verticalCuts[:]
A.sort()
B.sort()
A.insert(0,0)
B.insert(0,0)
A.append(h)
B.append(w)
maxw=0
maxh=0
for i in range(1,n+2):
maxh=max(maxh,A[i]-A[i-1])
for i in range(1,m+2):
maxw=max(maxw,B[i]-B[i-1])
m=10**9+7
return ((maxw%m)*(maxh%m))%m
|
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cut and similarly, verticalCuts[j] is the distance from the left of the rectangular cake to the jth vertical cut.
Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts. Since the answer can be a huge number, return this modulo 10^9 + 7.
Example 1:
Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
Output: 4
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area.
Example 2:
Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]
Output: 6
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area.
Example 3:
Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
Output: 9
Constraints:
2 <= h, w <= 10^9
1 <= horizontalCuts.length < min(h, 10^5)
1 <= verticalCuts.length < min(w, 10^5)
1 <= horizontalCuts[i] < h
1 <= verticalCuts[i] < w
It is guaranteed that all elements in horizontalCuts are distinct.
It is guaranteed that all elements in verticalCuts are distinct.
|
class Solution:
def maxArea(self, h: int, w: int, hc: List[int], v: List[int]) -> int:
hc.append(0)
hc.append(h)
v.append(0)
v.append(w)
v.sort()
hc.sort()
max_v = max_h = 0
for i in range(1,len(hc)):
max_h = max(max_h,hc[i]-hc[i-1])
for i in range(1,len(v)):
max_v = max(max_v,v[i]-v[i-1])
return max_h*max_v% 1000000007
|
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cut and similarly, verticalCuts[j] is the distance from the left of the rectangular cake to the jth vertical cut.
Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts. Since the answer can be a huge number, return this modulo 10^9 + 7.
Example 1:
Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
Output: 4
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area.
Example 2:
Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]
Output: 6
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area.
Example 3:
Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
Output: 9
Constraints:
2 <= h, w <= 10^9
1 <= horizontalCuts.length < min(h, 10^5)
1 <= verticalCuts.length < min(w, 10^5)
1 <= horizontalCuts[i] < h
1 <= verticalCuts[i] < w
It is guaranteed that all elements in horizontalCuts are distinct.
It is guaranteed that all elements in verticalCuts are distinct.
|
class Solution:
def maxArea(self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int]) -> int:
horizontalCuts = sorted(horizontalCuts)
verticalCuts = sorted(verticalCuts)
horizontalCuts.insert(0, 0)
horizontalCuts.append(h)
verticalCuts.insert(0,0)
verticalCuts.append(w)
print(horizontalCuts)
print(verticalCuts)
maxx = 0
for x in range(1, len(horizontalCuts)):
maxx = max(maxx, horizontalCuts[x] - horizontalCuts[x-1])
maxy = 0
for y in range(1, len(verticalCuts)):
maxy = max(maxy, verticalCuts[y] - verticalCuts[y-1])
return (maxx * maxy)%1000000007
|
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cut and similarly, verticalCuts[j] is the distance from the left of the rectangular cake to the jth vertical cut.
Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts. Since the answer can be a huge number, return this modulo 10^9 + 7.
Example 1:
Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
Output: 4
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area.
Example 2:
Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]
Output: 6
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area.
Example 3:
Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
Output: 9
Constraints:
2 <= h, w <= 10^9
1 <= horizontalCuts.length < min(h, 10^5)
1 <= verticalCuts.length < min(w, 10^5)
1 <= horizontalCuts[i] < h
1 <= verticalCuts[i] < w
It is guaranteed that all elements in horizontalCuts are distinct.
It is guaranteed that all elements in verticalCuts are distinct.
|
class Solution:
def maxArea(self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int]) -> int:
# def get_max_length(length: int, cuts: List[int]) -> int:
# cuts.sort()
# max_length = max(cuts[0], length - cuts[-1])
# for i in range(1, len(cuts)):
# max_length = max(max_length, cuts[i] - cuts[i-1])
# return max_length
return self.get_max_length(h, horizontalCuts) * self.get_max_length(w, verticalCuts) % 1000000007
def get_max_length(self, length: int, cuts: List[int]) -> int:
if len(cuts) == 1:
return max(cuts[0], length - cuts[0])
cuts.sort()
max_length = max(cuts[0], length - cuts[-1])
for i in range(1, len(cuts)):
max_length = max(max_length, cuts[i] - cuts[i - 1])
return max_length
|
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cut and similarly, verticalCuts[j] is the distance from the left of the rectangular cake to the jth vertical cut.
Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts. Since the answer can be a huge number, return this modulo 10^9 + 7.
Example 1:
Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
Output: 4
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area.
Example 2:
Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]
Output: 6
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area.
Example 3:
Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
Output: 9
Constraints:
2 <= h, w <= 10^9
1 <= horizontalCuts.length < min(h, 10^5)
1 <= verticalCuts.length < min(w, 10^5)
1 <= horizontalCuts[i] < h
1 <= verticalCuts[i] < w
It is guaranteed that all elements in horizontalCuts are distinct.
It is guaranteed that all elements in verticalCuts are distinct.
|
class Solution:
def maxArea(self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int]) -> int:
if 0 not in horizontalCuts:
horizontalCuts.append(0)
if h not in horizontalCuts:
horizontalCuts.append(h)
if 0 not in verticalCuts:
verticalCuts.append(0)
if w not in verticalCuts:
verticalCuts.append(w)
horizontalCuts.sort()
verticalCuts.sort()
i = 0
max_diff = -float('inf')
while i in range(len(horizontalCuts)-1):
if horizontalCuts[i+1]-horizontalCuts[i] > max_diff:
max_diff = max(max_diff, horizontalCuts[i+1]-horizontalCuts[i])
i += 1
j = 0
max_diff1 = -float('inf')
while j in range(len(verticalCuts)-1):
if verticalCuts[j+1] - verticalCuts[j] > max_diff1:
max_diff1 = max(max_diff1, verticalCuts[j + 1] - verticalCuts[j])
j += 1
return (max_diff*max_diff1)%((10**9)+7)
|
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cut and similarly, verticalCuts[j] is the distance from the left of the rectangular cake to the jth vertical cut.
Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts. Since the answer can be a huge number, return this modulo 10^9 + 7.
Example 1:
Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
Output: 4
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area.
Example 2:
Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]
Output: 6
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area.
Example 3:
Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
Output: 9
Constraints:
2 <= h, w <= 10^9
1 <= horizontalCuts.length < min(h, 10^5)
1 <= verticalCuts.length < min(w, 10^5)
1 <= horizontalCuts[i] < h
1 <= verticalCuts[i] < w
It is guaranteed that all elements in horizontalCuts are distinct.
It is guaranteed that all elements in verticalCuts are distinct.
|
class Solution:
def maxArea(self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int]) -> int:
# def get_max_length(length: int, cuts: List[int]) -> int:
# cuts.sort()
# max_length = max(cuts[0], length - cuts[-1])
# for i in range(1, len(cuts)):
# max_length = max(max_length, cuts[i] - cuts[i-1])
# return max_length
mod = int(1e9) + 7
return (self.get_max_length(h, horizontalCuts) % mod) * (self.get_max_length(w, verticalCuts) % mod) % mod
def get_max_length(self, length: int, cuts: List[int]) -> int:
if len(cuts) == 1:
return max(cuts[0], length - cuts[0])
cuts.sort()
max_length = max(cuts[0], length - cuts[-1])
for i in range(1, len(cuts)):
# max_length = max(max_length, cuts[i] - cuts[i - 1])
if cuts[i] - cuts[i - 1] > max_length:
max_length = cuts[i] - cuts[i - 1]
return max_length
|
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cut and similarly, verticalCuts[j] is the distance from the left of the rectangular cake to the jth vertical cut.
Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts. Since the answer can be a huge number, return this modulo 10^9 + 7.
Example 1:
Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
Output: 4
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area.
Example 2:
Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]
Output: 6
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area.
Example 3:
Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
Output: 9
Constraints:
2 <= h, w <= 10^9
1 <= horizontalCuts.length < min(h, 10^5)
1 <= verticalCuts.length < min(w, 10^5)
1 <= horizontalCuts[i] < h
1 <= verticalCuts[i] < w
It is guaranteed that all elements in horizontalCuts are distinct.
It is guaranteed that all elements in verticalCuts are distinct.
|
class Solution:
def maxArea(self, h: int, w: int, hh: List[int], vv: List[int]) -> int:
hh = sorted(hh) + [h]
vv = sorted(vv) + [w]
prev = 0
hm = 0
for i in hh:
hm = max(hm,i-prev)
prev = i
prev = 0
vm = 0
for i in vv:
vm = max(vm,i-prev)
prev = i
return (vm * hm) % (10**9 + 7)
|
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cut and similarly, verticalCuts[j] is the distance from the left of the rectangular cake to the jth vertical cut.
Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts. Since the answer can be a huge number, return this modulo 10^9 + 7.
Example 1:
Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
Output: 4
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area.
Example 2:
Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]
Output: 6
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area.
Example 3:
Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
Output: 9
Constraints:
2 <= h, w <= 10^9
1 <= horizontalCuts.length < min(h, 10^5)
1 <= verticalCuts.length < min(w, 10^5)
1 <= horizontalCuts[i] < h
1 <= verticalCuts[i] < w
It is guaranteed that all elements in horizontalCuts are distinct.
It is guaranteed that all elements in verticalCuts are distinct.
|
class Solution:
def maxArea(self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int]) -> int:
# def get_max_length(length: int, cuts: List[int]) -> int:
# cuts.sort()
# max_length = max(cuts[0], length - cuts[-1])
# for i in range(1, len(cuts)):
# max_length = max(max_length, cuts[i] - cuts[i-1])
# return max_length
mod = int(1e9) + 7
return (self.get_max_length(h, horizontalCuts) % mod) * (self.get_max_length(w, verticalCuts) % mod) % mod
def get_max_length(self, length: int, cuts: List[int]) -> int:
if len(cuts) == 1:
return max(cuts[0], length - cuts[0])
cuts.sort()
max_length = max(cuts[0], length - cuts[-1])
for i in range(1, len(cuts)):
max_length = max(max_length, cuts[i] - cuts[i - 1])
return max_length
|
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cut and similarly, verticalCuts[j] is the distance from the left of the rectangular cake to the jth vertical cut.
Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts. Since the answer can be a huge number, return this modulo 10^9 + 7.
Example 1:
Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
Output: 4
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area.
Example 2:
Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]
Output: 6
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area.
Example 3:
Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
Output: 9
Constraints:
2 <= h, w <= 10^9
1 <= horizontalCuts.length < min(h, 10^5)
1 <= verticalCuts.length < min(w, 10^5)
1 <= horizontalCuts[i] < h
1 <= verticalCuts[i] < w
It is guaranteed that all elements in horizontalCuts are distinct.
It is guaranteed that all elements in verticalCuts are distinct.
|
class Solution:
def maxArea(self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int]) -> int:
# def get_max_length(length: int, cuts: List[int]) -> int:
# cuts.sort()
# max_length = max(cuts[0], length - cuts[-1])
# for i in range(1, len(cuts)):
# max_length = max(max_length, cuts[i] - cuts[i-1])
# return max_length
return self.get_max_length(h, horizontalCuts) * self.get_max_length(w, verticalCuts) % 1000000007
def get_max_length(self, length: int, cuts: List[int]) -> int:
cuts.sort()
max_length = max(cuts[0], length - cuts[-1])
for i in range(1, len(cuts)):
max_length = max(max_length, cuts[i] - cuts[i - 1])
return max_length
|
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cut and similarly, verticalCuts[j] is the distance from the left of the rectangular cake to the jth vertical cut.
Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts. Since the answer can be a huge number, return this modulo 10^9 + 7.
Example 1:
Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
Output: 4
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area.
Example 2:
Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]
Output: 6
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area.
Example 3:
Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
Output: 9
Constraints:
2 <= h, w <= 10^9
1 <= horizontalCuts.length < min(h, 10^5)
1 <= verticalCuts.length < min(w, 10^5)
1 <= horizontalCuts[i] < h
1 <= verticalCuts[i] < w
It is guaranteed that all elements in horizontalCuts are distinct.
It is guaranteed that all elements in verticalCuts are distinct.
|
class Solution:
def maxArea(self, h: int, w: int, hc: List[int], vc: List[int]) -> int:
hc, vc = [0] + sorted(hc) + [h], [0] + sorted(vc) + [w]
return max(hc[i] - hc[i-1] for i in range(1, len(hc))) * max(vc[i] - vc[i-1] for i in range(1, len(vc))) % 1000000007
|
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cut and similarly, verticalCuts[j] is the distance from the left of the rectangular cake to the jth vertical cut.
Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts. Since the answer can be a huge number, return this modulo 10^9 + 7.
Example 1:
Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
Output: 4
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area.
Example 2:
Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]
Output: 6
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area.
Example 3:
Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
Output: 9
Constraints:
2 <= h, w <= 10^9
1 <= horizontalCuts.length < min(h, 10^5)
1 <= verticalCuts.length < min(w, 10^5)
1 <= horizontalCuts[i] < h
1 <= verticalCuts[i] < w
It is guaranteed that all elements in horizontalCuts are distinct.
It is guaranteed that all elements in verticalCuts are distinct.
|
class Solution:
def maxArea(self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int]) -> int:
mod = 10**9 + 7
horizontalCuts.extend([0, h])
horizontalCuts.sort()
verticalCuts.extend([0, w])
verticalCuts.sort()
hor_max = 0
for i in range(len(horizontalCuts)-1):
hor_max = max(hor_max, horizontalCuts[i+1] - horizontalCuts[i])
ver_max = 0
for i in range(len(verticalCuts)-1):
ver_max = max(ver_max, verticalCuts[i+1] - verticalCuts[i])
return (hor_max*ver_max)%mod
|
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cut and similarly, verticalCuts[j] is the distance from the left of the rectangular cake to the jth vertical cut.
Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts. Since the answer can be a huge number, return this modulo 10^9 + 7.
Example 1:
Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
Output: 4
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area.
Example 2:
Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]
Output: 6
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area.
Example 3:
Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
Output: 9
Constraints:
2 <= h, w <= 10^9
1 <= horizontalCuts.length < min(h, 10^5)
1 <= verticalCuts.length < min(w, 10^5)
1 <= horizontalCuts[i] < h
1 <= verticalCuts[i] < w
It is guaranteed that all elements in horizontalCuts are distinct.
It is guaranteed that all elements in verticalCuts are distinct.
|
class Solution:
def maxArea(self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int]) -> int:
new_horizontal_cuts = [0] + sorted(horizontalCuts) +[h]
new_vertical_cuts = [0] + sorted(verticalCuts) +[w]
mod = int(1e9)+7
max_height = 0
max_width = 0
for j in range(len(new_vertical_cuts)-1):
if max_width < new_vertical_cuts[j+1]-new_vertical_cuts[j]:
max_width = new_vertical_cuts[j+1]-new_vertical_cuts[j]
for i in range(len(new_horizontal_cuts)-1):
if max_height< new_horizontal_cuts[i+1]-new_horizontal_cuts[i]:
max_height = new_horizontal_cuts[i+1]-new_horizontal_cuts[i]
return (max_width*max_height)%mod
|
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cut and similarly, verticalCuts[j] is the distance from the left of the rectangular cake to the jth vertical cut.
Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts. Since the answer can be a huge number, return this modulo 10^9 + 7.
Example 1:
Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
Output: 4
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area.
Example 2:
Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]
Output: 6
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area.
Example 3:
Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
Output: 9
Constraints:
2 <= h, w <= 10^9
1 <= horizontalCuts.length < min(h, 10^5)
1 <= verticalCuts.length < min(w, 10^5)
1 <= horizontalCuts[i] < h
1 <= verticalCuts[i] < w
It is guaranteed that all elements in horizontalCuts are distinct.
It is guaranteed that all elements in verticalCuts are distinct.
|
class Solution:
def maxArea(self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int]) -> int:
mod = 10**9 + 7
horizontalCuts += [0, h]
horizontalCuts.sort()
verticalCuts += [0, w]
verticalCuts.sort()
hor_max = 0
for i in range(len(horizontalCuts)-1):
hor_max = max(hor_max, abs(horizontalCuts[i] - horizontalCuts[i+1]))
ver_max = 0
for i in range(len(verticalCuts)-1):
ver_max = max(ver_max, abs(verticalCuts[i] - verticalCuts[i+1]))
return (hor_max*ver_max)%mod
|
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cut and similarly, verticalCuts[j] is the distance from the left of the rectangular cake to the jth vertical cut.
Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts. Since the answer can be a huge number, return this modulo 10^9 + 7.
Example 1:
Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
Output: 4
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area.
Example 2:
Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]
Output: 6
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area.
Example 3:
Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
Output: 9
Constraints:
2 <= h, w <= 10^9
1 <= horizontalCuts.length < min(h, 10^5)
1 <= verticalCuts.length < min(w, 10^5)
1 <= horizontalCuts[i] < h
1 <= verticalCuts[i] < w
It is guaranteed that all elements in horizontalCuts are distinct.
It is guaranteed that all elements in verticalCuts are distinct.
|
class Solution:
def maxArea(self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int]) -> int:
def get_max_length(length: int, cuts: List[int]) -> int:
if len(cuts) == 1:
return max(cuts[0], length - cuts[0])
cuts.sort()
max_length = max(cuts[0], length - cuts[-1])
for i in range(1, len(cuts)):
# max_length = max(max_length, cuts[i] - cuts[i - 1])
if cuts[i] - cuts[i - 1] > max_length:
max_length = cuts[i] - cuts[i - 1]
return max_length
return get_max_length(h, horizontalCuts) * get_max_length(w, verticalCuts) % 1000000007
|
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cut and similarly, verticalCuts[j] is the distance from the left of the rectangular cake to the jth vertical cut.
Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts. Since the answer can be a huge number, return this modulo 10^9 + 7.
Example 1:
Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
Output: 4
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area.
Example 2:
Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]
Output: 6
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area.
Example 3:
Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
Output: 9
Constraints:
2 <= h, w <= 10^9
1 <= horizontalCuts.length < min(h, 10^5)
1 <= verticalCuts.length < min(w, 10^5)
1 <= horizontalCuts[i] < h
1 <= verticalCuts[i] < w
It is guaranteed that all elements in horizontalCuts are distinct.
It is guaranteed that all elements in verticalCuts are distinct.
|
class Solution:
def maxArea(self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int]) -> int:
# def get_max_length(length: int, cuts: List[int]) -> int:
# if len(cuts) == 1:
# return max(cuts[0], length - cuts[0])
# cuts.sort()
# max_length = max(cuts[0], length - cuts[-1])
# for i in range(1, len(cuts)):
# # max_length = max(max_length, cuts[i] - cuts[i - 1])
# if cuts[i] - cuts[i - 1] > max_length:
# max_length = cuts[i] - cuts[i - 1]
# return max_length
return self.get_max_length(h, horizontalCuts) * self.get_max_length(w, verticalCuts) % 1000000007
def get_max_length(self, length: int, cuts: List[int]) -> int:
if len(cuts) == 1:
return max(cuts[0], length - cuts[0])
cuts.sort()
max_length = max(cuts[0], length - cuts[-1])
for i in range(1, len(cuts)):
# max_length = max(max_length, cuts[i] - cuts[i - 1])
if cuts[i] - cuts[i - 1] > max_length:
max_length = cuts[i] - cuts[i - 1]
return max_length
|
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cut and similarly, verticalCuts[j] is the distance from the left of the rectangular cake to the jth vertical cut.
Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts. Since the answer can be a huge number, return this modulo 10^9 + 7.
Example 1:
Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
Output: 4
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area.
Example 2:
Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]
Output: 6
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area.
Example 3:
Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
Output: 9
Constraints:
2 <= h, w <= 10^9
1 <= horizontalCuts.length < min(h, 10^5)
1 <= verticalCuts.length < min(w, 10^5)
1 <= horizontalCuts[i] < h
1 <= verticalCuts[i] < w
It is guaranteed that all elements in horizontalCuts are distinct.
It is guaranteed that all elements in verticalCuts are distinct.
|
class Solution:
def maxArea(self, h: int, w: int, hori: List[int], verti: List[int]) -> int:
hori.sort()
verti.sort()
x = hori[0]
for i in range(1, len(hori)):
x = max(x, hori[i]-hori[i-1])
x = max(x, h - hori[-1])
y = verti[0]
for i in range(1, len(verti)):
y = max(y, verti[i]-verti[i-1])
y = max(y, w - verti[-1])
return (x*y)%1000000007
|
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cut and similarly, verticalCuts[j] is the distance from the left of the rectangular cake to the jth vertical cut.
Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts. Since the answer can be a huge number, return this modulo 10^9 + 7.
Example 1:
Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
Output: 4
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area.
Example 2:
Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]
Output: 6
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area.
Example 3:
Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
Output: 9
Constraints:
2 <= h, w <= 10^9
1 <= horizontalCuts.length < min(h, 10^5)
1 <= verticalCuts.length < min(w, 10^5)
1 <= horizontalCuts[i] < h
1 <= verticalCuts[i] < w
It is guaranteed that all elements in horizontalCuts are distinct.
It is guaranteed that all elements in verticalCuts are distinct.
|
class Solution:
def maxArea(self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int]) -> int:
horizontalStrips = [0] + sorted(horizontalCuts) + [h]
verticalStrips = [0] + sorted(verticalCuts) + [w]
maxW = max([horizontalStrips[i + 1] - horizontalStrips[i] for i in range(len(horizontalStrips) - 1)])
maxH = max([verticalStrips[i + 1] - verticalStrips[i] for i in range(len(verticalStrips) - 1)])
return (maxW * maxH) % ((10 ** 9) + 7)
|
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cut and similarly, verticalCuts[j] is the distance from the left of the rectangular cake to the jth vertical cut.
Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts. Since the answer can be a huge number, return this modulo 10^9 + 7.
Example 1:
Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
Output: 4
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area.
Example 2:
Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]
Output: 6
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area.
Example 3:
Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
Output: 9
Constraints:
2 <= h, w <= 10^9
1 <= horizontalCuts.length < min(h, 10^5)
1 <= verticalCuts.length < min(w, 10^5)
1 <= horizontalCuts[i] < h
1 <= verticalCuts[i] < w
It is guaranteed that all elements in horizontalCuts are distinct.
It is guaranteed that all elements in verticalCuts are distinct.
|
class Solution:
def maxArea(self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int]) -> int:
new_horizontal_cuts = [0] + sorted(horizontalCuts) +[h]
new_vertical_cuts = [0] + sorted(verticalCuts) +[w]
max_area = 0
mod = int(1e9)+7
max_height = 0
max_width = 0
for j in range(len(new_vertical_cuts)-1):
if max_width < new_vertical_cuts[j+1]-new_vertical_cuts[j]:
max_width = new_vertical_cuts[j+1]-new_vertical_cuts[j]
for i in range(len(new_horizontal_cuts)-1):
if max_height< new_horizontal_cuts[i+1]-new_horizontal_cuts[i]:
max_height = new_horizontal_cuts[i+1]-new_horizontal_cuts[i]
return (max_width*max_height)%mod
|
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cut and similarly, verticalCuts[j] is the distance from the left of the rectangular cake to the jth vertical cut.
Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts. Since the answer can be a huge number, return this modulo 10^9 + 7.
Example 1:
Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
Output: 4
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area.
Example 2:
Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]
Output: 6
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area.
Example 3:
Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
Output: 9
Constraints:
2 <= h, w <= 10^9
1 <= horizontalCuts.length < min(h, 10^5)
1 <= verticalCuts.length < min(w, 10^5)
1 <= horizontalCuts[i] < h
1 <= verticalCuts[i] < w
It is guaranteed that all elements in horizontalCuts are distinct.
It is guaranteed that all elements in verticalCuts are distinct.
|
class Solution:
def maxArea(self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int]) -> int:
# Return ans % (1E9 + 7)
# A = ((vi+1) - vi)*(hi+1 - hi)
def getMaxSpace(cuts, upperLimit):
cuts = sorted(cuts)
if not any(cuts):
return upperLimit
ans = cuts[0] # first piece is 0 to first cut
for i in range(len(cuts)-1):
ans = max(ans, cuts[i+1] - cuts[i])
return max(ans, upperLimit - cuts[-1])
maxHorSpace = getMaxSpace(horizontalCuts, h)
maxVertSpace = getMaxSpace(verticalCuts, w)
mod = 1E9+7
return int((maxHorSpace * maxVertSpace) % mod)
|
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cut and similarly, verticalCuts[j] is the distance from the left of the rectangular cake to the jth vertical cut.
Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts. Since the answer can be a huge number, return this modulo 10^9 + 7.
Example 1:
Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
Output: 4
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area.
Example 2:
Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]
Output: 6
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area.
Example 3:
Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
Output: 9
Constraints:
2 <= h, w <= 10^9
1 <= horizontalCuts.length < min(h, 10^5)
1 <= verticalCuts.length < min(w, 10^5)
1 <= horizontalCuts[i] < h
1 <= verticalCuts[i] < w
It is guaranteed that all elements in horizontalCuts are distinct.
It is guaranteed that all elements in verticalCuts are distinct.
|
class Solution:
def getMax(self, max: int, cuts: List[int]) -> int:
last = 0
for x in cuts:
cur = x - last
if cur > max:
max = cur
last = x
return max
def maxArea(self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int]) -> int:
mod = 1000000007
horizontalCuts.sort()
verticalCuts.sort()
maxh = self.getMax(h - horizontalCuts[-1], horizontalCuts)
maxv = self.getMax(w - verticalCuts[-1], verticalCuts)
return maxh%mod * maxv%mod
|
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cut and similarly, verticalCuts[j] is the distance from the left of the rectangular cake to the jth vertical cut.
Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts. Since the answer can be a huge number, return this modulo 10^9 + 7.
Example 1:
Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
Output: 4
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area.
Example 2:
Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]
Output: 6
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area.
Example 3:
Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
Output: 9
Constraints:
2 <= h, w <= 10^9
1 <= horizontalCuts.length < min(h, 10^5)
1 <= verticalCuts.length < min(w, 10^5)
1 <= horizontalCuts[i] < h
1 <= verticalCuts[i] < w
It is guaranteed that all elements in horizontalCuts are distinct.
It is guaranteed that all elements in verticalCuts are distinct.
|
class Solution:
def maxArea(self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int]) -> int:
horizontalCuts.append(0)
horizontalCuts.append(h)
verticalCuts.append(0)
verticalCuts.append(w)
horizontalCuts.sort()
verticalCuts.sort()
mw = 0
mh = 0
for x, y in zip(horizontalCuts, horizontalCuts[1:]):
mw = max(mw, y-x)
for x, y in zip(verticalCuts, verticalCuts[1:]):
mh = max(mh, y-x)
mod = 10**9 + 7
return (mw * mh) % mod
|
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cut and similarly, verticalCuts[j] is the distance from the left of the rectangular cake to the jth vertical cut.
Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts. Since the answer can be a huge number, return this modulo 10^9 + 7.
Example 1:
Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
Output: 4
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area.
Example 2:
Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]
Output: 6
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area.
Example 3:
Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
Output: 9
Constraints:
2 <= h, w <= 10^9
1 <= horizontalCuts.length < min(h, 10^5)
1 <= verticalCuts.length < min(w, 10^5)
1 <= horizontalCuts[i] < h
1 <= verticalCuts[i] < w
It is guaranteed that all elements in horizontalCuts are distinct.
It is guaranteed that all elements in verticalCuts are distinct.
|
class Solution:
def maxArea(self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int]) -> int:
# m = 0
# horizontalCuts.sort()
# verticalCuts.sort()
# numHoriz = len(horizontalCuts)
# numVert = len(verticalCuts)
# prevH = 0
# for horiz in range(numHoriz + 1):
# prevV = 0
# for vert in range(numVert + 1):
# if horiz == numHoriz:
# currH = h
# else:
# currH = horizontalCuts[horiz]
# if vert == numVert:
# currV = w
# else:
# currV = verticalCuts[vert]
# m = max(m, (currH - prevH) * (currV - prevV))
# prevV = currV
# prevH = currH
# return m % (10**9 + 7)
horizontalCuts.sort()
verticalCuts.sort()
prev = 0
hDiff = 0
for horiz in horizontalCuts:
hDiff = max(hDiff, horiz - prev)
prev = horiz
hDiff = max(h - prev, hDiff)
prev = 0
vDiff = 0
for vert in verticalCuts:
vDiff = max(vDiff, vert - prev)
prev = vert
vDiff = max(w - prev, vDiff)
return (vDiff * hDiff) % (10**9 + 7)
|
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cut and similarly, verticalCuts[j] is the distance from the left of the rectangular cake to the jth vertical cut.
Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts. Since the answer can be a huge number, return this modulo 10^9 + 7.
Example 1:
Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
Output: 4
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area.
Example 2:
Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]
Output: 6
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area.
Example 3:
Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
Output: 9
Constraints:
2 <= h, w <= 10^9
1 <= horizontalCuts.length < min(h, 10^5)
1 <= verticalCuts.length < min(w, 10^5)
1 <= horizontalCuts[i] < h
1 <= verticalCuts[i] < w
It is guaranteed that all elements in horizontalCuts are distinct.
It is guaranteed that all elements in verticalCuts are distinct.
|
class Solution:
def maxArea(self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int]) -> int:
horizontalCuts.append(h)
verticalCuts.append(w)
def get_max_gap(array):
array.sort()
max_hor_width = array[0]
for previous_cut, next_cut in zip(array, array[1:]):
max_hor_width = max(max_hor_width, next_cut - previous_cut)
return max_hor_width
return (get_max_gap(horizontalCuts) * get_max_gap(verticalCuts)) % 1000000007
# time complexity - nlogn - mainly sorting
# sub1: corner case for 0, n index
# sub2: result should be module
# does insertion take extra time? - maybe just O(n) - could be avoided with smart comparison
|
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cut and similarly, verticalCuts[j] is the distance from the left of the rectangular cake to the jth vertical cut.
Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts. Since the answer can be a huge number, return this modulo 10^9 + 7.
Example 1:
Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
Output: 4
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area.
Example 2:
Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]
Output: 6
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area.
Example 3:
Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
Output: 9
Constraints:
2 <= h, w <= 10^9
1 <= horizontalCuts.length < min(h, 10^5)
1 <= verticalCuts.length < min(w, 10^5)
1 <= horizontalCuts[i] < h
1 <= verticalCuts[i] < w
It is guaranteed that all elements in horizontalCuts are distinct.
It is guaranteed that all elements in verticalCuts are distinct.
|
class Solution:
def maxArea(self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int]) -> int:
horizontalCuts.sort()
verticalCuts.sort()
max_h,max_w=max(horizontalCuts[0],h-horizontalCuts[-1]),max(verticalCuts[0],w-verticalCuts[-1])
for i in range(1,len(horizontalCuts)):
max_h=max(max_h,horizontalCuts[i]-horizontalCuts[i-1])
for j in range(1,len(verticalCuts)):
max_w=max(max_w,verticalCuts[j]-verticalCuts[j-1])
return (max_w*max_h)%(10**9+7)
|
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cut and similarly, verticalCuts[j] is the distance from the left of the rectangular cake to the jth vertical cut.
Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts. Since the answer can be a huge number, return this modulo 10^9 + 7.
Example 1:
Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
Output: 4
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area.
Example 2:
Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]
Output: 6
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area.
Example 3:
Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
Output: 9
Constraints:
2 <= h, w <= 10^9
1 <= horizontalCuts.length < min(h, 10^5)
1 <= verticalCuts.length < min(w, 10^5)
1 <= horizontalCuts[i] < h
1 <= verticalCuts[i] < w
It is guaranteed that all elements in horizontalCuts are distinct.
It is guaranteed that all elements in verticalCuts are distinct.
|
class Solution:
def maxArea(self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int]) -> int:
horizontalCuts.append(h)
verticalCuts.append(w)
def get_max_gap(array):
array.sort()
max_hor_width = array[0]
for previous_cut, next_cut in zip(array, array[1:]):
max_hor_width = max(max_hor_width, next_cut - previous_cut)
return max_hor_width
return (get_max_gap(horizontalCuts) * get_max_gap(verticalCuts)) % 1000000007
# time complexity - nlogn - mainly sorting
# sub1: corner case for 0, n index
# sub2: result should be module
# does insertion take extra time? - maybe just O(n) - could be avoided with smart comparison
|
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cut and similarly, verticalCuts[j] is the distance from the left of the rectangular cake to the jth vertical cut.
Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts. Since the answer can be a huge number, return this modulo 10^9 + 7.
Example 1:
Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
Output: 4
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area.
Example 2:
Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]
Output: 6
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area.
Example 3:
Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
Output: 9
Constraints:
2 <= h, w <= 10^9
1 <= horizontalCuts.length < min(h, 10^5)
1 <= verticalCuts.length < min(w, 10^5)
1 <= horizontalCuts[i] < h
1 <= verticalCuts[i] < w
It is guaranteed that all elements in horizontalCuts are distinct.
It is guaranteed that all elements in verticalCuts are distinct.
|
from typing import List
import numpy
import sys
# Definition for a binary tree node.
# Definition for a binary tree node.
class Solution:
def maxArea(self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int]) -> int:
horizontalCuts.insert(0, 0)
horizontalCuts.append(h)
horizontalCuts.sort()
verticalCuts.insert(0, 0)
verticalCuts.append(w)
verticalCuts.sort()
maxh = -1
maxv = -1
for index, value in enumerate(horizontalCuts):
if index == 0:
continue
maxh = max(maxh, value - horizontalCuts[index-1])
for index, value in enumerate(verticalCuts):
if index == 0:
continue
maxv = max(maxv, value - verticalCuts[index-1])
module = int(pow(10, 9) + 7)
result = maxv * maxh % module
return result
|
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cut and similarly, verticalCuts[j] is the distance from the left of the rectangular cake to the jth vertical cut.
Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts. Since the answer can be a huge number, return this modulo 10^9 + 7.
Example 1:
Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
Output: 4
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area.
Example 2:
Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]
Output: 6
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area.
Example 3:
Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
Output: 9
Constraints:
2 <= h, w <= 10^9
1 <= horizontalCuts.length < min(h, 10^5)
1 <= verticalCuts.length < min(w, 10^5)
1 <= horizontalCuts[i] < h
1 <= verticalCuts[i] < w
It is guaranteed that all elements in horizontalCuts are distinct.
It is guaranteed that all elements in verticalCuts are distinct.
|
class Solution:
def maxArea(self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int]) -> int:
horizontalCuts.sort()
verticalCuts.sort()
mx= max(verticalCuts[0], w - verticalCuts[-1])
my = max(horizontalCuts[0], h - horizontalCuts[-1])
for i in range(1,len(verticalCuts)):
mx=max(mx,verticalCuts[i] - verticalCuts[i - 1])
for i in range(1,len(horizontalCuts)):
my = max(my, horizontalCuts[i] - horizontalCuts[i - 1])
return mx*my%(10**9+7)
|
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cut and similarly, verticalCuts[j] is the distance from the left of the rectangular cake to the jth vertical cut.
Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts. Since the answer can be a huge number, return this modulo 10^9 + 7.
Example 1:
Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
Output: 4
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area.
Example 2:
Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]
Output: 6
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area.
Example 3:
Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
Output: 9
Constraints:
2 <= h, w <= 10^9
1 <= horizontalCuts.length < min(h, 10^5)
1 <= verticalCuts.length < min(w, 10^5)
1 <= horizontalCuts[i] < h
1 <= verticalCuts[i] < w
It is guaranteed that all elements in horizontalCuts are distinct.
It is guaranteed that all elements in verticalCuts are distinct.
|
class Solution:
def maxArea(self, h, w, horizontalCuts, verticalCuts):
H = [0] + horizontalCuts + [h]
W = [0] + verticalCuts + [w]
H.sort()
W.sort()
hmax, wmax = 0, 0
for i in range(1, len(H)):
hmax = max(hmax, H[i] - H[i - 1])
for i in range(1, len(W)):
wmax = max(wmax, W[i] - W[i - 1])
return hmax * wmax % 1000000007
|
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cut and similarly, verticalCuts[j] is the distance from the left of the rectangular cake to the jth vertical cut.
Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts. Since the answer can be a huge number, return this modulo 10^9 + 7.
Example 1:
Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
Output: 4
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area.
Example 2:
Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]
Output: 6
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area.
Example 3:
Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
Output: 9
Constraints:
2 <= h, w <= 10^9
1 <= horizontalCuts.length < min(h, 10^5)
1 <= verticalCuts.length < min(w, 10^5)
1 <= horizontalCuts[i] < h
1 <= verticalCuts[i] < w
It is guaranteed that all elements in horizontalCuts are distinct.
It is guaranteed that all elements in verticalCuts are distinct.
|
class Solution:
def maxArea(self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int]) -> int:
# Return ans % (1E9 + 7)
# A = ((vi+1) - vi)*(hi+1 - hi)
def getMaxSpace(cuts, upperLimit):
#cuts = sorted(cuts)
cuts.sort()
if not any(cuts):
return upperLimit
ans = cuts[0] # first piece is 0 to first cut
for i in range(len(cuts)-1):
ans = max(ans, cuts[i+1] - cuts[i])
return max(ans, upperLimit - cuts[-1])
maxHorSpace = getMaxSpace(horizontalCuts, h)
maxVertSpace = getMaxSpace(verticalCuts, w)
mod = 1E9+7
return int((maxHorSpace * maxVertSpace) % mod)
|
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cut and similarly, verticalCuts[j] is the distance from the left of the rectangular cake to the jth vertical cut.
Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts. Since the answer can be a huge number, return this modulo 10^9 + 7.
Example 1:
Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
Output: 4
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area.
Example 2:
Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]
Output: 6
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area.
Example 3:
Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
Output: 9
Constraints:
2 <= h, w <= 10^9
1 <= horizontalCuts.length < min(h, 10^5)
1 <= verticalCuts.length < min(w, 10^5)
1 <= horizontalCuts[i] < h
1 <= verticalCuts[i] < w
It is guaranteed that all elements in horizontalCuts are distinct.
It is guaranteed that all elements in verticalCuts are distinct.
|
class Solution:
def maxArea(self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int]) -> int:
horizontalCuts.sort()
verticalCuts.sort()
max_h = max(horizontalCuts[0], h - horizontalCuts[-1])
if len(horizontalCuts) > 1:
for i in range(len(horizontalCuts)-1):
if max_h < abs(horizontalCuts[i+1] - horizontalCuts[i]):
max_h = abs(horizontalCuts[i+1] - horizontalCuts[i])
max_v = max(verticalCuts[0], w - verticalCuts[-1])
if len(verticalCuts) > 1:
for i in range(len(verticalCuts)-1):
if max_v < abs(verticalCuts[i+1] - verticalCuts[i]):
max_v = abs(verticalCuts[i+1] - verticalCuts[i])
return ((max_h % (10**9 + 7)) * (max_v % (10**9 + 7))) % (10**9 + 7)
|
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cut and similarly, verticalCuts[j] is the distance from the left of the rectangular cake to the jth vertical cut.
Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts. Since the answer can be a huge number, return this modulo 10^9 + 7.
Example 1:
Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
Output: 4
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area.
Example 2:
Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]
Output: 6
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area.
Example 3:
Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
Output: 9
Constraints:
2 <= h, w <= 10^9
1 <= horizontalCuts.length < min(h, 10^5)
1 <= verticalCuts.length < min(w, 10^5)
1 <= horizontalCuts[i] < h
1 <= verticalCuts[i] < w
It is guaranteed that all elements in horizontalCuts are distinct.
It is guaranteed that all elements in verticalCuts are distinct.
|
class Solution:
def maxArea(self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int]) -> int:
horizontalCuts.sort()
verticalCuts.sort()
def maxDelta(cuts, end):
cuts.sort()
cuts.append(end)
cuts.append(0)
md=0
for i in range(len(cuts)-1):
md=max(md,cuts[i]-cuts[i-1])
return md
return (maxDelta(horizontalCuts, h)*maxDelta(verticalCuts, w))%(10**9 + 7)
mh=maxDelta(horizontalCuts, h)
mv=maxDelta(verticalCuts, w)
return (mh*mv)%(10^9+7)
|
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cut and similarly, verticalCuts[j] is the distance from the left of the rectangular cake to the jth vertical cut.
Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts. Since the answer can be a huge number, return this modulo 10^9 + 7.
Example 1:
Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
Output: 4
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area.
Example 2:
Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]
Output: 6
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area.
Example 3:
Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
Output: 9
Constraints:
2 <= h, w <= 10^9
1 <= horizontalCuts.length < min(h, 10^5)
1 <= verticalCuts.length < min(w, 10^5)
1 <= horizontalCuts[i] < h
1 <= verticalCuts[i] < w
It is guaranteed that all elements in horizontalCuts are distinct.
It is guaranteed that all elements in verticalCuts are distinct.
|
class Solution:
def maxArea(self, h: int, w: int, hCuts: List[int], vCuts: List[int]) -> int:
hCuts.sort()
vCuts.sort()
H, V = max(hCuts[0], h - hCuts[-1]), max(vCuts[0], w - vCuts[-1])
for i in range(1, len(hCuts)):
H = max(H, hCuts[i] - hCuts[i - 1])
for i in range(1, len(vCuts)):
V = max(V, vCuts[i] - vCuts[i - 1])
return H * V % 1000000007
|
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cut and similarly, verticalCuts[j] is the distance from the left of the rectangular cake to the jth vertical cut.
Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts. Since the answer can be a huge number, return this modulo 10^9 + 7.
Example 1:
Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
Output: 4
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area.
Example 2:
Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]
Output: 6
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area.
Example 3:
Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
Output: 9
Constraints:
2 <= h, w <= 10^9
1 <= horizontalCuts.length < min(h, 10^5)
1 <= verticalCuts.length < min(w, 10^5)
1 <= horizontalCuts[i] < h
1 <= verticalCuts[i] < w
It is guaranteed that all elements in horizontalCuts are distinct.
It is guaranteed that all elements in verticalCuts are distinct.
|
class Solution:
def maxArea(self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int]) -> int:
horizontalCuts.sort()
verticalCuts.sort()
def helper(ll,num):
'''
ll is a sorted list
num is either h or w
'''
ll.append(num)
ans = ll[0]
for i in range(1,len(ll)):
if ll[i]-ll[i-1] > ans:
ans = ll[i]-ll[i-1]
return ans
return int((helper(horizontalCuts,h) * helper(verticalCuts,w))%(1e9+7))
|
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cut and similarly, verticalCuts[j] is the distance from the left of the rectangular cake to the jth vertical cut.
Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts. Since the answer can be a huge number, return this modulo 10^9 + 7.
Example 1:
Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
Output: 4
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area.
Example 2:
Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]
Output: 6
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area.
Example 3:
Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
Output: 9
Constraints:
2 <= h, w <= 10^9
1 <= horizontalCuts.length < min(h, 10^5)
1 <= verticalCuts.length < min(w, 10^5)
1 <= horizontalCuts[i] < h
1 <= verticalCuts[i] < w
It is guaranteed that all elements in horizontalCuts are distinct.
It is guaranteed that all elements in verticalCuts are distinct.
|
class Solution:
def maxArea(self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int]) -> int:
horizontalCuts = [0] + sorted(horizontalCuts) + [h]
verticalCuts = [0] + sorted(verticalCuts) + [w]
new_h = max([horizontalCuts[i+1]-horizontalCuts[i] for i in range(len(horizontalCuts)-1)])
new_w = max([verticalCuts[i+1]-verticalCuts[i] for i in range(len(verticalCuts)-1)])
return (new_h * new_w) % (10 ** 9 +7)
|
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cut and similarly, verticalCuts[j] is the distance from the left of the rectangular cake to the jth vertical cut.
Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts. Since the answer can be a huge number, return this modulo 10^9 + 7.
Example 1:
Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
Output: 4
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area.
Example 2:
Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]
Output: 6
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area.
Example 3:
Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
Output: 9
Constraints:
2 <= h, w <= 10^9
1 <= horizontalCuts.length < min(h, 10^5)
1 <= verticalCuts.length < min(w, 10^5)
1 <= horizontalCuts[i] < h
1 <= verticalCuts[i] < w
It is guaranteed that all elements in horizontalCuts are distinct.
It is guaranteed that all elements in verticalCuts are distinct.
|
class Solution:
def maxArea(self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int]) -> int:
modu = 1000000007
# horizontal- i
# vertical-j
# 横向记录0-x, x-y, y-m留下最大
# 竖向也一样
h_max = 0
v_max = 0
horizontalCuts.append(0)
horizontalCuts.append(h)
verticalCuts.append(0)
verticalCuts.append(w)
horizontalCuts.sort()
verticalCuts.sort()
for i in range(1,len(horizontalCuts)):
h_max = max(h_max, horizontalCuts[i]-horizontalCuts[i-1])
for j in range(1,len(verticalCuts)):
v_max = max(v_max, verticalCuts[j]-verticalCuts[j-1])
return (v_max*h_max)%modu
|
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cut and similarly, verticalCuts[j] is the distance from the left of the rectangular cake to the jth vertical cut.
Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts. Since the answer can be a huge number, return this modulo 10^9 + 7.
Example 1:
Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
Output: 4
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area.
Example 2:
Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]
Output: 6
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area.
Example 3:
Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
Output: 9
Constraints:
2 <= h, w <= 10^9
1 <= horizontalCuts.length < min(h, 10^5)
1 <= verticalCuts.length < min(w, 10^5)
1 <= horizontalCuts[i] < h
1 <= verticalCuts[i] < w
It is guaranteed that all elements in horizontalCuts are distinct.
It is guaranteed that all elements in verticalCuts are distinct.
|
class Solution:
def maxArea(self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int]) -> int:
horizontalCuts.sort()
verticalCuts.sort()
max_h = horizontalCuts[0]
for h_cut in range(1, len(horizontalCuts)):
max_h = max(max_h, horizontalCuts[h_cut] - horizontalCuts[h_cut-1])
max_h = max(max_h, h - horizontalCuts[-1])
max_v = verticalCuts[0]
for v_cut in range(1, len(verticalCuts)):
max_v = max(max_v, verticalCuts[v_cut] - verticalCuts[v_cut-1])
max_v = max(max_v, w - verticalCuts[-1])
return max_h * max_v % int(1e9 + 7)
|
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cut and similarly, verticalCuts[j] is the distance from the left of the rectangular cake to the jth vertical cut.
Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts. Since the answer can be a huge number, return this modulo 10^9 + 7.
Example 1:
Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
Output: 4
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area.
Example 2:
Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]
Output: 6
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area.
Example 3:
Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
Output: 9
Constraints:
2 <= h, w <= 10^9
1 <= horizontalCuts.length < min(h, 10^5)
1 <= verticalCuts.length < min(w, 10^5)
1 <= horizontalCuts[i] < h
1 <= verticalCuts[i] < w
It is guaranteed that all elements in horizontalCuts are distinct.
It is guaranteed that all elements in verticalCuts are distinct.
|
class Solution:
def maxArea(self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int]) -> int:
horizontalCuts.sort()
verticalCuts.sort()
max_hor_width = horizontalCuts[0]
for i in range(len(horizontalCuts)-1):
max_hor_width = max(max_hor_width, horizontalCuts[i+1]-horizontalCuts[i])
max_hor_width = max(max_hor_width, h-horizontalCuts[len(horizontalCuts)-1])
max_vert_width = verticalCuts[0]
for i in range(len(verticalCuts)-1):
max_vert_width = max(max_vert_width, verticalCuts[i+1]-verticalCuts[i])
max_vert_width = max(max_vert_width, w-verticalCuts[len(verticalCuts)-1])
return (max_vert_width * max_hor_width) % 1000000007
# time complexity - nlogn - mainly sorting
# sub1: corner case for 0, n index
# sub2: result should be module
# does insertion take extra time? - maybe just O(n) - could be avoided with smart comparison
|
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cut and similarly, verticalCuts[j] is the distance from the left of the rectangular cake to the jth vertical cut.
Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts. Since the answer can be a huge number, return this modulo 10^9 + 7.
Example 1:
Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
Output: 4
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area.
Example 2:
Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]
Output: 6
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area.
Example 3:
Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
Output: 9
Constraints:
2 <= h, w <= 10^9
1 <= horizontalCuts.length < min(h, 10^5)
1 <= verticalCuts.length < min(w, 10^5)
1 <= horizontalCuts[i] < h
1 <= verticalCuts[i] < w
It is guaranteed that all elements in horizontalCuts are distinct.
It is guaranteed that all elements in verticalCuts are distinct.
|
class Solution:
def maxArea(self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int]) -> int:
horizontalCuts.sort()
verticalCuts.sort()
ph, pw = 0, 0
msh, msw = 0, 0
for ch in horizontalCuts:
if ch - ph > msh:
msh = ch-ph
ph = ch
if h - ph > msh:
msh = h - ph
for cw in verticalCuts:
if cw - pw > msw:
msw = cw - pw
pw = cw
if w - pw > msw:
msw = w - pw
return msh*msw%(10**9 + 7)
|
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cut and similarly, verticalCuts[j] is the distance from the left of the rectangular cake to the jth vertical cut.
Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts. Since the answer can be a huge number, return this modulo 10^9 + 7.
Example 1:
Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
Output: 4
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area.
Example 2:
Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]
Output: 6
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area.
Example 3:
Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
Output: 9
Constraints:
2 <= h, w <= 10^9
1 <= horizontalCuts.length < min(h, 10^5)
1 <= verticalCuts.length < min(w, 10^5)
1 <= horizontalCuts[i] < h
1 <= verticalCuts[i] < w
It is guaranteed that all elements in horizontalCuts are distinct.
It is guaranteed that all elements in verticalCuts are distinct.
|
class Solution:
def maxArea(self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int]) -> int:
horizontalCuts.sort()
verticalCuts.sort()
print(horizontalCuts, verticalCuts)
pre = 0
hori_max = 0
for i, cut in enumerate(horizontalCuts):
hori_max = max(hori_max, cut - pre)
pre = cut
hori_max = max(hori_max, h - pre)
#print(hori_max)
pre = 0
vert_max = 0
for i, cut in enumerate(verticalCuts):
vert_max = max(vert_max, cut - pre)
pre = cut
vert_max = max(vert_max, w - pre)
#print(vert_max)
return (hori_max * vert_max) % (10**9 + 7)
|
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cut and similarly, verticalCuts[j] is the distance from the left of the rectangular cake to the jth vertical cut.
Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts. Since the answer can be a huge number, return this modulo 10^9 + 7.
Example 1:
Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
Output: 4
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area.
Example 2:
Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]
Output: 6
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area.
Example 3:
Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
Output: 9
Constraints:
2 <= h, w <= 10^9
1 <= horizontalCuts.length < min(h, 10^5)
1 <= verticalCuts.length < min(w, 10^5)
1 <= horizontalCuts[i] < h
1 <= verticalCuts[i] < w
It is guaranteed that all elements in horizontalCuts are distinct.
It is guaranteed that all elements in verticalCuts are distinct.
|
class Solution:
def maxArea(self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int]) -> int:
ver = [0] + sorted(verticalCuts) + [w]
hor = [0] + sorted(horizontalCuts) + [h]
dh = max(hor[i] - hor[i-1] for i in range(1, len(hor)))
dv = max(ver[i] - ver[i-1] for i in range(1, len(ver)))
return dh*dv % (10**9 + 7)
|
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cut and similarly, verticalCuts[j] is the distance from the left of the rectangular cake to the jth vertical cut.
Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts. Since the answer can be a huge number, return this modulo 10^9 + 7.
Example 1:
Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
Output: 4
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area.
Example 2:
Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]
Output: 6
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area.
Example 3:
Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
Output: 9
Constraints:
2 <= h, w <= 10^9
1 <= horizontalCuts.length < min(h, 10^5)
1 <= verticalCuts.length < min(w, 10^5)
1 <= horizontalCuts[i] < h
1 <= verticalCuts[i] < w
It is guaranteed that all elements in horizontalCuts are distinct.
It is guaranteed that all elements in verticalCuts are distinct.
|
class Solution:
def maxArea(self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int]) -> int:
h_max = self.get_max_interval(w, sorted(verticalCuts))
w_max = self.get_max_interval(h, sorted(horizontalCuts))
print(h_max, w_max)
return (h_max*w_max) % (10**9 + 7)
def get_max_interval(self, length, cuts):
current = 0
max_cut = 0
for cut in cuts:
max_cut = max(max_cut, cut - current)
current = cut
max_cut = max(max_cut, length - current)
return max_cut
|
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cut and similarly, verticalCuts[j] is the distance from the left of the rectangular cake to the jth vertical cut.
Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts. Since the answer can be a huge number, return this modulo 10^9 + 7.
Example 1:
Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
Output: 4
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area.
Example 2:
Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]
Output: 6
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area.
Example 3:
Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
Output: 9
Constraints:
2 <= h, w <= 10^9
1 <= horizontalCuts.length < min(h, 10^5)
1 <= verticalCuts.length < min(w, 10^5)
1 <= horizontalCuts[i] < h
1 <= verticalCuts[i] < w
It is guaranteed that all elements in horizontalCuts are distinct.
It is guaranteed that all elements in verticalCuts are distinct.
|
class Solution:
def maxArea(self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int]) -> int:
horizontalStrips = [0] + sorted(horizontalCuts) + [h]
verticalStrips = [0] + sorted(verticalCuts) + [w]
maxStripWidth = max([horizontalStrips[i + 1] - horizontalStrips[i] for i in range(len(horizontalStrips) - 1)])
maxStripHeight = max([verticalStrips[i + 1] - verticalStrips[i] for i in range(len(verticalStrips) - 1)])
return (maxStripWidth * maxStripHeight) % ((10 ** 9) + 7)
|
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cut and similarly, verticalCuts[j] is the distance from the left of the rectangular cake to the jth vertical cut.
Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts. Since the answer can be a huge number, return this modulo 10^9 + 7.
Example 1:
Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
Output: 4
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area.
Example 2:
Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]
Output: 6
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area.
Example 3:
Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
Output: 9
Constraints:
2 <= h, w <= 10^9
1 <= horizontalCuts.length < min(h, 10^5)
1 <= verticalCuts.length < min(w, 10^5)
1 <= horizontalCuts[i] < h
1 <= verticalCuts[i] < w
It is guaranteed that all elements in horizontalCuts are distinct.
It is guaranteed that all elements in verticalCuts are distinct.
|
class Solution:
def maxArea(self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int]) -> int:
# m = 0
# horizontalCuts.sort()
# verticalCuts.sort()
# numHoriz = len(horizontalCuts)
# numVert = len(verticalCuts)
# prevH = 0
# for horiz in range(numHoriz + 1):
# prevV = 0
# for vert in range(numVert + 1):
# if horiz == numHoriz:
# currH = h
# else:
# currH = horizontalCuts[horiz]
# if vert == numVert:
# currV = w
# else:
# currV = verticalCuts[vert]
# m = max(m, (currH - prevH) * (currV - prevV))
# prevV = currV
# prevH = currH
# return m % (10**9 + 7)
horizontalCuts.sort()
verticalCuts.sort()
prev = 0
hDiff = 0
for horiz in horizontalCuts:
print(horiz, prev, horiz - prev)
hDiff = max(hDiff, horiz - prev)
prev = horiz
hDiff = max(h - prev, hDiff)
prev = 0
vDiff = 0
for vert in verticalCuts:
vDiff = max(vDiff, vert - prev)
prev = vert
vDiff = max(w - prev, vDiff)
print(vDiff, hDiff)
return (vDiff * hDiff) % (10**9 + 7)
|
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cut and similarly, verticalCuts[j] is the distance from the left of the rectangular cake to the jth vertical cut.
Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts. Since the answer can be a huge number, return this modulo 10^9 + 7.
Example 1:
Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
Output: 4
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area.
Example 2:
Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]
Output: 6
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area.
Example 3:
Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
Output: 9
Constraints:
2 <= h, w <= 10^9
1 <= horizontalCuts.length < min(h, 10^5)
1 <= verticalCuts.length < min(w, 10^5)
1 <= horizontalCuts[i] < h
1 <= verticalCuts[i] < w
It is guaranteed that all elements in horizontalCuts are distinct.
It is guaranteed that all elements in verticalCuts are distinct.
|
class Solution:
def maxArea(self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int]) -> int:
horizontalCuts.sort()
verticalCuts.sort()
max_width = verticalCuts[0]
max_height = horizontalCuts[0]
for i in range(len(horizontalCuts) - 1):
max_height = max(max_height, (horizontalCuts[i+1] - horizontalCuts[i]))
max_height = max(max_height, (h - horizontalCuts[-1]))
for i in range(len(verticalCuts)-1):
max_width = max(max_width, (verticalCuts[i+1] - verticalCuts[i]))
max_width = max(max_width, (w - verticalCuts[-1]))
return (max_height * max_width) % (10**9 + 7)
|
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cut and similarly, verticalCuts[j] is the distance from the left of the rectangular cake to the jth vertical cut.
Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts. Since the answer can be a huge number, return this modulo 10^9 + 7.
Example 1:
Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
Output: 4
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area.
Example 2:
Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]
Output: 6
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area.
Example 3:
Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
Output: 9
Constraints:
2 <= h, w <= 10^9
1 <= horizontalCuts.length < min(h, 10^5)
1 <= verticalCuts.length < min(w, 10^5)
1 <= horizontalCuts[i] < h
1 <= verticalCuts[i] < w
It is guaranteed that all elements in horizontalCuts are distinct.
It is guaranteed that all elements in verticalCuts are distinct.
|
class Solution:
def maxArea(self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int]) -> int:
horizontalCuts.append(0)
horizontalCuts.append(h)
horizontalCuts.sort()
Maxh = max([horizontalCuts[i] - horizontalCuts[i-1] for i in range(1,len(horizontalCuts))])
verticalCuts.append(0)
verticalCuts.append(w)
verticalCuts.sort()
Maxv = max([verticalCuts[i] - verticalCuts[i-1] for i in range(1,len(verticalCuts))])
return Maxh*Maxv % 1000000007
|
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cut and similarly, verticalCuts[j] is the distance from the left of the rectangular cake to the jth vertical cut.
Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts. Since the answer can be a huge number, return this modulo 10^9 + 7.
Example 1:
Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
Output: 4
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area.
Example 2:
Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]
Output: 6
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area.
Example 3:
Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
Output: 9
Constraints:
2 <= h, w <= 10^9
1 <= horizontalCuts.length < min(h, 10^5)
1 <= verticalCuts.length < min(w, 10^5)
1 <= horizontalCuts[i] < h
1 <= verticalCuts[i] < w
It is guaranteed that all elements in horizontalCuts are distinct.
It is guaranteed that all elements in verticalCuts are distinct.
|
class Solution:
def maxArea(self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int]) -> int:
horizontalCuts, verticalCuts = sorted([0, h]+horizontalCuts), sorted([0, w]+verticalCuts)
return max([after-prev for after, prev in zip(horizontalCuts[1:], horizontalCuts)])*max([after-prev for after, prev in zip(verticalCuts[1:], verticalCuts)])%(10**9+7)
|
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cut and similarly, verticalCuts[j] is the distance from the left of the rectangular cake to the jth vertical cut.
Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts. Since the answer can be a huge number, return this modulo 10^9 + 7.
Example 1:
Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
Output: 4
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area.
Example 2:
Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]
Output: 6
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area.
Example 3:
Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
Output: 9
Constraints:
2 <= h, w <= 10^9
1 <= horizontalCuts.length < min(h, 10^5)
1 <= verticalCuts.length < min(w, 10^5)
1 <= horizontalCuts[i] < h
1 <= verticalCuts[i] < w
It is guaranteed that all elements in horizontalCuts are distinct.
It is guaranteed that all elements in verticalCuts are distinct.
|
class Solution:
def maxArea(self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int]) -> int:
horizontalCuts.sort()
verticalCuts.sort()
def maxDelta(cuts, end):
cuts.sort()
cuts.append(end)
cuts.append(0)
md=0
for i in range(len(cuts)-1):
md=max(md,cuts[i]-cuts[i-1])
return md
def getGap(arr, n):
prev=0
i=0
res=0
while i<len(arr):
res=max(arr[i]-prev, res)
prev=arr[i]
i+=1
return max(n-prev, res)
return (maxDelta(horizontalCuts, h)*maxDelta(verticalCuts, w))%(10**9 + 7)
mh=maxDelta(horizontalCuts, h)
mv=maxDelta(verticalCuts, w)
return (mh*mv)%(10^9+7)
|
Given a rectangular cake with height h and width w, and two arrays of integers horizontalCuts and verticalCuts where horizontalCuts[i] is the distance from the top of the rectangular cake to the ith horizontal cut and similarly, verticalCuts[j] is the distance from the left of the rectangular cake to the jth vertical cut.
Return the maximum area of a piece of cake after you cut at each horizontal and vertical position provided in the arrays horizontalCuts and verticalCuts. Since the answer can be a huge number, return this modulo 10^9 + 7.
Example 1:
Input: h = 5, w = 4, horizontalCuts = [1,2,4], verticalCuts = [1,3]
Output: 4
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green piece of cake has the maximum area.
Example 2:
Input: h = 5, w = 4, horizontalCuts = [3,1], verticalCuts = [1]
Output: 6
Explanation: The figure above represents the given rectangular cake. Red lines are the horizontal and vertical cuts. After you cut the cake, the green and yellow pieces of cake have the maximum area.
Example 3:
Input: h = 5, w = 4, horizontalCuts = [3], verticalCuts = [3]
Output: 9
Constraints:
2 <= h, w <= 10^9
1 <= horizontalCuts.length < min(h, 10^5)
1 <= verticalCuts.length < min(w, 10^5)
1 <= horizontalCuts[i] < h
1 <= verticalCuts[i] < w
It is guaranteed that all elements in horizontalCuts are distinct.
It is guaranteed that all elements in verticalCuts are distinct.
|
class Solution:
def maxArea(self, h: int, w: int, horizontalCuts: List[int], verticalCuts: List[int]) -> int:
horizontalCuts.sort()
verticalCuts.sort()
hr=[0]+horizontalCuts+[h]
vr=[0]+verticalCuts+[w]
hmax,vmax=0,0
for i in range(1,len(hr)):
hmax=max(hmax,hr[i]-hr[i-1])
for i in range(1,len(vr)):
vmax=max(vmax,vr[i]-vr[i-1])
return (hmax*vmax)%(10**9+7)
|
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] * (len(arr) + 1)
stack = []
for i, n in enumerate(arr + [1000000]):
while stack and arr[stack[-1]] < n:
same_height_idx = [stack.pop()]
while stack and arr[stack[-1]] == arr[same_height_idx[0]]:
same_height_idx.append(stack.pop())
for j in same_height_idx:
# jump to left
if i - j <= d:
dp[i] = max(dp[i], dp[j] + 1)
# jump to right
if stack and j - stack[-1] <= d:
dp[stack[-1]] = max(dp[stack[-1]], dp[j] + 1)
stack.append(i)
return max(dp[:-1])
|
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:
# to_delete_index = set()
# for i in range(1, len(arr)):
# if arr[i] == arr[i - 1]:
# to_delete_index.add(i)
# nums = []
# for i in range(0, len(arr)):
# if i not in to_delete_index:
# nums.append(arr[i])
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: # i是poped的右侧第一个相等或更大的元素
dp[i] = max(dp[i], dp[poped] + 1)
if stack and poped - stack[-1] <= d: # stack[-1]是poped的左侧第一个更大的元素
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)
# print(i, dp, stack)
return max(dp[:-1]) # 去掉最后一个inf在的位置的dp值
|
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: # top-down dp
# Time complexity O(ND)
# Space complexity O(N) for dp
def maxJumps(self, arr: List[int], d: int) -> int:
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])
|
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:
res = 0
visited = {}
def dfs(i):
if i in visited:
return visited[i]
step = 1
i_right = i + 1
while i_right < len(arr) and i_right - i <= d:
if arr[i_right] >= arr[i]:
break
step = max(step, 1+dfs(i_right))
i_right += 1
i_left = i - 1
while i_left >= 0 and i - i_left <= d:
if arr[i_left] >= arr[i]:
break
step = max(step, 1+dfs(i_left))
i_left -= 1
visited[i] = step
return step
for i in range(len(arr)):
res = max(res, dfs(i))
return res
|
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)
cache = {}
def f(i):
if i in cache:
return cache[i]
left = True
right = True
ret = 1
for step in range(1,d+1):
m = i - step
if 0 <= m < len(arr) and arr[m] < arr[i] and left:
ret = max(ret,f(m)+1)
else:
left = False
m = i + step
if 0 <= m < len(arr) and arr[m] < arr[i] and right:
ret = max(ret,f(m)+1)
else:
right = False
if (not left) and (not right):
break
cache[i] = ret
return ret
mm = 0
for i in range(len(arr)):
fi = f(i) if i not in cache else cache[i]
mm = max(f(i),mm)
return mm
|
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:
# O(n x d) time, O(n) space
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)))
|
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(index):
res = 0
for direction in [-1, 1]:
for jump in range(1, d + 1):
j = index + direction*jump
if 0 <= j < len(arr) and arr[index] > arr[j]:
res = max(res, dfs(j))
else:
break
return res + 1
return max(dfs(i) for i in range(len(arr)))
|
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
|
from typing import List
class Solution:
def maxJumps(self, arr: List[int], d: int) -> int:
result = 0
mapping = dict()
for index, value in enumerate(arr):
if index not in mapping:
mapping[index] = set()
for dd in range(1, d + 1):
if index + dd >= len(arr):
break
if arr[index + dd] < arr[index]:
mapping[index].add(index + dd)
else:
break
for dd in range(1, d + 1):
if index - dd < 0:
break
if arr[index - dd] < arr[index]:
mapping[index].add(index - dd)
else:
break
pass
cache = dict()
def dfs(cache, mapping, root):
if root in cache:
return cache[root]
if root not in mapping:
return 0
result = 0
for next in mapping[root]:
result = max(result, dfs(cache, mapping, next) + 1)
pass
cache[root] = result
return result
result = 1
for i in range(len(arr)):
result = max(result, dfs(cache, mapping, i) + 1)
return result
|
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)
memo = [0] * n
def dfs(start):
if start < 0 or start >= n:
return 0
if memo[start] > 0:
return memo[start]
visited[start] = 1
ret = 1
for i in range(start-1, max(0, start - d) - 1, -1):
if arr[i] >= arr[start]:
break
if visited[i]:
continue
ret = max(ret, dfs(i)+1)
for j in range(start+1, min(start + d, n-1) + 1):
if arr[j] >= arr[start]:
break
if visited[j]:
continue
ret = max(ret, dfs(j)+1)
visited[start] = 0
memo[start] = ret
return ret
ret = 1
visited = [0] * n
for i in range(n):
ret = max(dfs(i), ret)
return ret
|
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())
'''
mjump = 0
for i, x in enumerate(arr):
top = i
bottom = i
while top+1 < len(arr) and x>arr[top+1]:
top += 1
while bottom-1 >= 0 and x>arr[bottom-1]:
bottom -=1
newarr = arr[bottom:top+1]
jump =[x]
value = x-1
while value>=min(newarr):
if value < jump[-1]-d:
break
elif value in newarr:
jump.append(value)
value -= 1
print(jump)
mjump = max(mjump, len(jump))
return mjump
'''
|
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 helper(self, idx):
if self.dp[idx] != float('inf'):
return self.dp[idx]
max_step = 1
# to left
for c in range(idx - 1, max(0, idx - self.d) - 1, -1):
if self.arr[idx] <= self.arr[c]:
break
max_step = max(self.helper(c) + 1, max_step)
# to right
for c in range(idx + 1, min(self.n, idx + self.d + 1)):
if self.arr[idx] <= self.arr[c]:
break
max_step = max(self.helper(c) + 1, max_step)
self.dp[idx] = max_step
return max_step
def maxJumps(self, arr: List[int], d: int) -> int:
self.n = len(arr)
self.d = d
self.arr = arr
self.dp = [float('inf')] * self.n
self.trans = defaultdict(list)
max_ = 0
for i in range(self.n):
max_ = max(self.helper(i), max_)
return max_
|
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)
memo = {}
def dfs(start):
if start < 0 or start >= n:
return 0
if start in memo:
return memo[start]
visited[start] = 1
ret = 1
for i in range(start-1, max(0, start - d) - 1, -1):
if arr[i] >= arr[start]:
break
if visited[i]:
continue
ret = max(ret, dfs(i)+1)
for j in range(start+1, min(start + d, n-1) + 1):
if arr[j] >= arr[start]:
break
if visited[j]:
continue
ret = max(ret, dfs(j)+1)
visited[start] = 0
memo[start] = ret
return ret
ret = 1
visited = [0] * n
for i in range(n):
ret = max(dfs(i), ret)
return ret
|
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)
memo = {}
def dfs(start):
if start < 0 or start >= n:
return 0
if start in memo:
return memo[start]
visited[start] = 1
ret = 1
for i in range(start-1, max(0, start - d) - 1, -1):
if arr[i] >= arr[start]:
break
if visited[i]:
continue
ret = max(ret, dfs(i)+1)
for j in range(start+1, min(start + d, n-1) + 1):
if arr[j] >= arr[start]:
break
if visited[j]:
continue
ret = max(ret, dfs(j)+1)
visited[start] = 0
memo[start] = ret
return ret
ret = 1
visited = [0] * n
for i in range(n):
ret = max(dfs(i), ret)
return ret
|
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
|
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 p:
# pass
# # print(f'{index}, {i}')
if i >= len(arr):
break
if abs(arr[i]) >= abs(arr[index]):
break
if arr[i] < 0:
continue
# if p:
# print(f'using index-{index},{i}')
left_visited = max(left_visited,helper(arr,i,d,memo))
for j in range(index-1,index-d-1,-1):
# if p:
# pass
# print(f'{index}, {j}')
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]
# if p:
# # pass
# print(f'{index}--{left_visited},{right_visited}')
memo[index] = 1 + max(left_visited,right_visited)
return memo[index]
max_visited = 1
p = False
memo = {}
for index in range(len(arr)):
# if index == 2:
# p = True
visited = helper(arr,index,d,memo)
# print(f'from {index} max is {visited}')
max_visited = max(visited,max_visited)
return max_visited
|
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:
from collections import deque
def maxJumps(self, arr: List[int], d: int) -> int:
q=deque()
n=len(arr)
dp=[-1]*n
def solve(node):
if node<0 or node>=n:
return -1
if dp[node]!=-1:
return dp[node]
ans=0
for i in range(1,d+1):
if node+i>=n or arr[node+i]>=arr[node]:
break
#print(node,i+node)
ans=max(ans,solve(i+node))
for i in range(1,d+1):
if node-i<0 or arr[node-i]>=arr[node]:
break
#print(node,-i+node)
ans=max(ans,solve(node-i))
dp[node]=1+ans
return dp[node]
for i in range(n):
solve(i)
#print(dp)
return max(dp)
|
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)
|
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)
|
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)
|
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
|
import collections
class Solution:
def maxJumps(self, arr: List[int], d: int) -> int:
outEdges = {}
inEdges = {}
for idx, height in enumerate(arr):
jumpRangeRight = list(range(idx+1, min(len(arr), idx+d+1)))
jumpRangeLeft = list(range(idx-1, max(0, idx - d) - 1, -1))
for jumpRange in [jumpRangeLeft, jumpRangeRight]:
for jumpIdx in jumpRange:
if arr[jumpIdx] >= arr[idx]:
break
if idx not in outEdges:
outEdges[idx] = set()
outEdges[idx].add(jumpIdx)
if jumpIdx not in inEdges:
inEdges[jumpIdx] = set()
inEdges[jumpIdx].add(idx)
noInEdges = collections.deque()
for idx in range(len(arr)):
if idx not in inEdges:
noInEdges.append(idx)
topologicalOrder = collections.deque()
while noInEdges:
idx = noInEdges.pop()
topologicalOrder.append(idx)
if idx not in outEdges:
continue
for jumpIdx in outEdges[idx]:
inEdges[jumpIdx].remove(idx)
if not inEdges[jumpIdx]:
noInEdges.append(jumpIdx)
maxJumps = 1
jumpCount = [1] * len(arr)
for idx in topologicalOrder:
if idx not in outEdges:
continue
for jumpIdx in outEdges[idx]:
jumpCount[jumpIdx] = max(jumpCount[idx] + 1, jumpCount[jumpIdx])
maxJumps = max(jumpCount[jumpIdx], maxJumps)
return maxJumps
def maxJumpsBfs(self, arr: List[int], d: int) -> int:
bfs = collections.deque()
for idx in range(len(arr)):
bfs.append((idx, 1))
stepCount = {}
maxSteps = 0
while bfs:
idx, steps = bfs.popleft()
# Already found more steps to this index.
if idx in stepCount and steps <= stepCount[idx]:
continue
stepCount[idx] = steps
maxSteps = max(steps, maxSteps)
# Process jumps to the right
for jumpIdx in range(idx+1, min(len(arr), idx+d+1)):
# Can't jump past taller buildings, break loop
if arr[jumpIdx] >= arr[idx]:
break
# Explore this jump
bfs.append((jumpIdx, steps+1))
# Process jumps to the left
for jumpIdx in range(idx-1, max(0, idx-d) -1, -1):
# Can't jump past taller buildings, break loop
if arr[jumpIdx] >= arr[idx]:
break
# Search the next step
bfs.append((jumpIdx, steps+1))
return maxSteps
|
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
|
import collections
class Solution:
def maxJumps(self, arr: List[int], d: int) -> int:
outEdges = {}
inEdges = {}
for idx, height in enumerate(arr):
jumpRangeRight = list(range(idx+1, min(len(arr), idx+d+1)))
jumpRangeLeft = list(range(idx-1, max(0, idx - d) - 1, -1))
for jumpRange in [jumpRangeLeft, jumpRangeRight]:
for jumpIdx in jumpRange:
if arr[jumpIdx] >= arr[idx]:
break
if idx not in outEdges:
outEdges[idx] = set()
outEdges[idx].add(jumpIdx)
if jumpIdx not in inEdges:
inEdges[jumpIdx] = set()
inEdges[jumpIdx].add(idx)
noInEdges = collections.deque()
for idx in range(len(arr)):
if idx not in inEdges:
noInEdges.append(idx)
topologicalOrder = []
while noInEdges:
idx = noInEdges.pop()
topologicalOrder.append(idx)
if idx not in outEdges:
continue
for jumpIdx in outEdges[idx]:
inEdges[jumpIdx].remove(idx)
if not inEdges[jumpIdx]:
noInEdges.append(jumpIdx)
maxJumps = 1
jumpCount = [1] * len(arr)
for idx in topologicalOrder:
if idx not in outEdges:
continue
for jumpIdx in outEdges[idx]:
jumpCount[jumpIdx] = max(jumpCount[idx] + 1, jumpCount[jumpIdx])
maxJumps = max(jumpCount[jumpIdx], maxJumps)
return maxJumps
def maxJumpsBfs(self, arr: List[int], d: int) -> int:
bfs = collections.deque()
for idx in range(len(arr)):
bfs.append((idx, 1))
stepCount = {}
maxSteps = 0
while bfs:
idx, steps = bfs.popleft()
# Already found more steps to this index.
if idx in stepCount and steps <= stepCount[idx]:
continue
stepCount[idx] = steps
maxSteps = max(steps, maxSteps)
# Process jumps to the right
for jumpIdx in range(idx+1, min(len(arr), idx+d+1)):
# Can't jump past taller buildings, break loop
if arr[jumpIdx] >= arr[idx]:
break
# Explore this jump
bfs.append((jumpIdx, steps+1))
# Process jumps to the left
for jumpIdx in range(idx-1, max(0, idx-d) -1, -1):
# Can't jump past taller buildings, break loop
if arr[jumpIdx] >= arr[idx]:
break
# Search the next step
bfs.append((jumpIdx, steps+1))
return maxSteps
|
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:
result = 1
N = len(arr)
cache = {}
def helper(i : int) -> int:
if i in cache:
return cache[i]
result = 1
if ((i == 0 or arr[i] <= arr[i - 1]) and
(i == N - 1 or arr[i] <= arr[i + 1])):
cache[i] = result
return cache[i]
j = i - 1
while j >= 0 and j >= i - d and arr[j] < arr[i]:
cur = helper(j)
result = max(result, cur + 1)
j = j - 1
j = i + 1
while j < N and j <= i + d and arr[j] < arr[i]:
cur = helper(j)
result = max(result, cur + 1)
j = j + 1
cache[i] = result
return cache[i]
for i in range(N):
cur = helper(i)
result = max(cur, result)
return result
|
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
|
import collections
class Solution:
def maxJumps(self, arr: List[int], d: int) -> int:
outEdges = {}
inEdges = {}
for idx, height in enumerate(arr):
jumpRangeRight = list(range(idx+1, min(len(arr), idx+d+1)))
jumpRangeLeft = list(range(idx-1, max(0, idx - d) - 1, -1))
for jumpRange in [jumpRangeLeft, jumpRangeRight]:
for jumpIdx in jumpRange:
if arr[jumpIdx] >= arr[idx]:
break
if idx not in outEdges:
outEdges[idx] = set()
outEdges[idx].add(jumpIdx)
if jumpIdx not in inEdges:
inEdges[jumpIdx] = set()
inEdges[jumpIdx].add(idx)
noInEdges = collections.deque()
for idx in range(len(arr)):
if idx not in inEdges:
noInEdges.append(idx)
topologicalOrder = collections.deque()
while noInEdges:
idx = noInEdges.pop()
topologicalOrder.append(idx)
if idx not in outEdges:
continue
for jumpIdx in outEdges[idx]:
inEdges[jumpIdx].remove(idx)
if not inEdges[jumpIdx]:
noInEdges.append(jumpIdx)
maxJumps = 1
jumpCount = [1] * len(arr)
while topologicalOrder:
idx = topologicalOrder.popleft()
if idx not in outEdges:
continue
for jumpIdx in outEdges[idx]:
jumpCount[jumpIdx] = max(jumpCount[idx] + 1, jumpCount[jumpIdx])
maxJumps = max(jumpCount[jumpIdx], maxJumps)
return maxJumps
def maxJumpsBfs(self, arr: List[int], d: int) -> int:
bfs = collections.deque()
for idx in range(len(arr)):
bfs.append((idx, 1))
stepCount = {}
maxSteps = 0
while bfs:
idx, steps = bfs.popleft()
# Already found more steps to this index.
if idx in stepCount and steps <= stepCount[idx]:
continue
stepCount[idx] = steps
maxSteps = max(steps, maxSteps)
# Process jumps to the right
for jumpIdx in range(idx+1, min(len(arr), idx+d+1)):
# Can't jump past taller buildings, break loop
if arr[jumpIdx] >= arr[idx]:
break
# Explore this jump
bfs.append((jumpIdx, steps+1))
# Process jumps to the left
for jumpIdx in range(idx-1, max(0, idx-d) -1, -1):
# Can't jump past taller buildings, break loop
if arr[jumpIdx] >= arr[idx]:
break
# Search the next step
bfs.append((jumpIdx, steps+1))
return maxSteps
|
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)
|
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
# print(\"currently at:\", arr[pos], \"maximum location:\", 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:
# print(\"seen:\", i, seen[i])
jumps_to_i = seen[i]
maximum_locations = max(maximum_locations, jumps_to_i + 1)
# print(\"pos:\", pos, \"max visits:\", maximum_locations)
# print(\"Backtracking\")
seen[pos] = maximum_locations
# print(\"pos:\", arr[pos], \"seen:\", seen)
return maximum_locations
indices = [(i, arr[i]) for i in range(len(arr))]
indices.sort(key=lambda x: x[1], reverse=True)
seen = {}
# print(seen)
return max(jump(x[0], 1, seen) for x in indices)
|
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)
# print(\"Backtracking\")
seen[pos] = maximum_locations
# print(\"pos:\", arr[pos], \"seen:\", seen)
return maximum_locations
indices = [(i, arr[i]) for i in range(len(arr))]
indices.sort(key=lambda x: x[1], reverse=True)
seen = {}
# print(seen)
return max(jump(x[0], 1, seen) for x in indices)
|
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]
|
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):
if ans[i] != 1:
return ans[i]
for dr in [-1, 1]:
for k in range(i, i + d * dr + dr, dr):
if k == i:
continue
if not (0 <= k < len(arr) and 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)
|
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)
memo = {}
def dfs(start, visited):
if start < 0 or start >= n:
return 0
if start in memo:
return memo[start]
visited[start] = 1
ret = 1
for i in range(start-1, max(0, start - d) - 1, -1):
if arr[i] >= arr[start]:
break
if visited[i]:
continue
ret = max(ret, dfs(i, visited)+1)
for j in range(start+1, min(start + d, n-1) + 1):
if arr[j] >= arr[start]:
break
if visited[j]:
continue
ret = max(ret, dfs(j, visited)+1)
visited[start] = 0
memo[start] = ret
return ret
ret = 1
for i in range(n):
visited = [0] * n
ret = max(dfs(i, visited), ret)
#print(i,ret, memo)
return ret
|
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:
'''
[6,4,14,6,8,13,9,7,10,6,12], d = 2
dp(i) = 1 +
max{
within i-d <j< i+d
not arr[k] > arr[i] between i, j
}
res = 0
j from i-1 to i-d (0<=j<len(arr)), while arr[j]<arr[i]: res = max(res, recurse j)
i+1 to i+d
memo
return res + 1
'''
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))
# print(memo)
return max_pos
|
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
# if idx == 10: print(lstack, rstack)
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
|
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:
jumps = defaultdict(list)
def check(it):
stack = []
for i in it:
while stack and arr[stack[-1]] < arr[i]:
j = stack.pop()
if abs(i - j) <= d:
jumps[j].append(i)
stack.append(i)
N = len(arr)
check(range(N))
check(reversed(range(N)))
@lru_cache(None)
def dfs(i):
return 1 + max((dfs(j) for j in jumps[i]), default = 0)
return max((dfs(i) for i in range(N)))
|
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)
|
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:
# there is some topological order
# but now it is asking for the longest path
# now we have to start at the root of the tree, and then maximize the
# distance of each node
# building the graph is tricky
# sliding window for building the graph
# i -> j if arr[i] > arr[j] but not the otherway around
# so we have to use a deque and pop out the indices greater than d away
'''
6 4
14 -> 6, (0), 14 -> 4 (1)
14 -> (3),
14 -> (4), (4) -> (3)
'''
n = len(A)
graph = {}
inorder = [0]*n
for i in range(n):
graph[i] = []
is_greater = False
cur_max = -1
for ind in range(i-1, max(-1, i-d-1), -1):
if A[ind] >= A[i]:
if A[ind] >= cur_max and A[ind] > A[i]:
cur_max = A[ind]
graph[ind].append(i)
inorder[i] += 1
is_greater = True
else:
if is_greater: continue
graph[i].append(ind)
inorder[ind] += 1
stack = []
dp = [1]*n
for i in range(n):
if inorder[i] == 0:
stack.append(i)
while stack:
top = stack.pop()
for value in graph[top]:
inorder[value] -= 1
dp[value] = max(dp[value], dp[top] + 1)
if inorder[value] == 0:
stack.append(value)
# print(A[20:60])
# print([(i, v) for i,v in enumerate(dp)][:60])
return max(dp)
|
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:
# there is some topological order
# but now it is asking for the longest path
# now we have to start at the root of the tree, and then maximize the
# distance of each node
# building the graph is tricky
# sliding window for building the graph
# i -> j if arr[i] > arr[j] but not the otherway around
# so we have to use a deque and pop out the indices greater than d away
'''
6 4
14 -> 6, (0), 14 -> 4 (1)
14 -> (3),
14 -> (4), (4) -> (3)
'''
n = len(A)
graph = {}
inorder = [0]*n
for i in range(n):
graph[i] = []
is_greater = False
cur_max = -1
for ind in range(i-1, max(-1, i-d-1), -1):
if A[ind] > A[i]:
if A[ind] >= cur_max:
cur_max = A[ind]
graph[ind].append(i)
inorder[i] += 1
is_greater = True
else:
if is_greater: continue
if A[i] == A[ind]: break
graph[i].append(ind)
inorder[ind] += 1
stack = []
dp = [1]*n
for i in range(n):
if inorder[i] == 0:
stack.append(i)
while stack:
top = stack.pop()
for value in graph[top]:
inorder[value] -= 1
dp[value] = max(dp[value], dp[top] + 1)
if inorder[value] == 0:
stack.append(value)
# print(A[20:60])
# print([(i, v) for i,v in enumerate(dp)][:60])
print(dp)
return max(dp)
|
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()
print(steps)
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)
print(dp)
return max(dp)
|
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)
|
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: # top-down dp
# Time complexity O(ND)
# Space complexity O(N) for dp
def maxJumps(self, arr: List[int], d: int) -> int:
n, inf = len(arr), float('inf')
a, s = [[] for _ in range(n)], [(inf, -1)]
for i, x in enumerate(arr):
while s[-1][0] < x:
if s[-1][1] >= i-d:
a[i].append(s[-1][1])
s.pop()
s.append((x, i))
s = [(inf, -1)]
for i, x in reversed(list(enumerate(arr))):
while s[-1][0] < x:
if s[-1][1] <= i+d:
a[i].append(s[-1][1])
s.pop()
s.append((x, i))
dp, v = [1]*n, [0]*n
def dfs(i):
if v[i]: return
v[i] = 1
for j in a[i]:
dfs(j)
dp[i] = max(dp[i], dp[j]+1)
for i in range(n): dfs(i)
return max(dp)
|
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)))))
|
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+1)
stack = []
for i, a in enumerate(arr + [float('inf')]):
while stack and arr[stack[-1]] < a:
last = [stack.pop()]
while stack and arr[stack[-1]] == arr[last[0]]:
last.append(stack.pop())
for j in last:
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])
|
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, 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])
|
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
|
from collections import *
from functools import *
import numpy as np
from typing import *
class Solution:
def maxJumps(self, nums: List[int], d: int) -> int:
N = len(nums)
left_jumps = [[] for _ in range(N)]
left = [(-1, np.inf)]
for i in range(N):
while left[-1][1] < nums[i]:
j, _ = left.pop()
if abs(j - i) <= d:
left_jumps[i].append(j)
left.append((i, nums[i]))
right_jumps = [[] for _ in range(N)]
right = [(-1, np.inf)]
for i in reversed(range(N)):
while right[-1][1] < nums[i]:
j, _ = right.pop()
if abs(j - i) <= d:
right_jumps[i].append(j)
right.append((i, nums[i]))
@lru_cache(maxsize=None)
def longest_path_from(pos: int):
left_max = max((longest_path_from(p) for p in left_jumps[pos]), default=0)
right_max = max((longest_path_from(p) for p in right_jumps[pos]), default=0)
return max(1 + left_max, 1 + right_max)
return max(longest_path_from(i) for i in range(N))
|
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 = arr
n = len(A)
# comp = 0
@lru_cache(None)
def dp(i):
# nonlocal comp
# comp += 1
count = 0
for j in range(i-1, i-d-1, -1):
if j >= 0 and A[j] < A[i]:
count = max(count, dp(j))
else:
break
for j in range(i+1, i+d+1):
if j < n and A[j] < A[i]:
count = max(count, dp(j))
else:
break
return count + 1
res = 0
for k in range(n):
res = max(res, dp(k))
# print(comp)
return res
|
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:
arr.append(math.inf) # 让所有元素都出栈
dp = [1] * len(arr)
stack = [] # 单调递减栈
for i, cur in enumerate(arr):
while stack and arr[stack[-1]] < cur:
poped_same_val = [stack.pop()]
while stack and arr[stack[-1]] == arr[poped_same_val[0]]: # val一样的元素,他们的right_first_bigger和left_first_bigger都是一样的,要一起拿出来更新。
poped_same_val.append(stack.pop())
for poped in poped_same_val:
if i - poped <= d: # i是poped的右侧第一个相等或更大的元素
dp[i] = max(dp[i], dp[poped] + 1)
if stack and poped - stack[-1] <= d: # stack[-1]是poped的左侧第一个更大的元素
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)
# print(i, dp, stack)
return max(dp[:-1]) # 去掉最后一个inf在的位置的dp值
|
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)])
|
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
|
from functools import lru_cache
class Solution:
def maxJumps(self, arr: List[int], d: int) -> int:
lim = len(arr)
@lru_cache(None)
def r(i):
tmp = [0]
for j in range(i-1,i-d-1,-1):
if j<0:break
if arr[j]<arr[i]:
tmp.append(1+r(j))
else:
break
for j in range(i+1,i+d+1):
if j>=lim:break
if arr[j]<arr[i]:
tmp.append(1+r(j))
else:
break
return max(tmp)
mx = 0
for i in range(0,lim):
val = 1+r(i)
if val>mx:mx = val
return mx
|
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)
|
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
|
import functools
class Solution:
def maxJumps(self, arr, d: int) -> int:
@functools.lru_cache(None)
def helper(i):
ans = 1
for index in range(i + 1, min(len(arr), i + d + 1)):
if arr[i] > arr[index]:
ans = max(ans,1 + helper(index))
else:
break
for index in range(i - 1, max(-1, i - d - 1),-1):
if arr[i] > arr[index]:
ans = max(ans,1 + helper(index))
else:
break
return ans
maxJumps = 1
for i in range(len(arr)):
maxJumps = max(maxJumps, helper(i))
return maxJumps
|
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 = []
n = len(arr)
for i in range(n):l.append([arr[i], i])
l.sort()
l = [i[1] for i in l]
dp = [1] * n
for i in l:
for j in range(i + 1, min(n, i + d + 1)):
if arr[i] > arr[j]:dp[i] = max(dp[i], dp[j] + 1)
else:break
for j in range(i - 1, max(-1, i - d -1), -1):
if arr[i] > arr[j]:dp[i] = max(dp[i], dp[j] + 1)
else:break
return max(dp)
|
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
|
import functools
class Solution:
def maxJumps(self, arr: List[int], d: int) -> int:
@functools.lru_cache(None)
def check_i(inow) :
max_son = 0
for i in range(1, min(d+1, len(arr)-inow)) :
if arr[inow+i] >= arr[inow] :
break
max_son = max(max_son, check_i(inow + i))
for i in range(1, min(d+1, inow+1)) :
if arr[inow-i] >= arr[inow] :
break
max_son = max(max_son, check_i(inow - i))
return max_son+1
return max([check_i(i) for i in range(len(arr))])
|
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
# 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+d*di+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)))
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)
|
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
|
# 1340. Jump Game V
# 20201003 这个题目,也只有用栈解决最快
class Solution: # Decreasing Stack + DP (单调栈 DP)
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])
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.