inputs
stringlengths 50
14k
| targets
stringlengths 4
655k
|
|---|---|
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n).
There are n + 1 taps located at points [0, 1, ..., n] in the garden.
Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open.
Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.
Example 1:
Input: n = 5, ranges = [3,4,1,1,0,0]
Output: 1
Explanation: The tap at point 0 can cover the interval [-3,3]
The tap at point 1 can cover the interval [-3,5]
The tap at point 2 can cover the interval [1,3]
The tap at point 3 can cover the interval [2,4]
The tap at point 4 can cover the interval [4,4]
The tap at point 5 can cover the interval [5,5]
Opening Only the second tap will water the whole garden [0,5]
Example 2:
Input: n = 3, ranges = [0,0,0,0]
Output: -1
Explanation: Even if you activate all the four taps you cannot water the whole garden.
Example 3:
Input: n = 7, ranges = [1,2,1,0,2,1,0,1]
Output: 3
Example 4:
Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4]
Output: 2
Example 5:
Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4]
Output: 1
Constraints:
1 <= n <= 10^4
ranges.length == n + 1
0 <= ranges[i] <= 100
|
class Solution:
def minTaps(self, n: int, ranges: List[int]) -> int:
min_range, max_range, open_taps, idx = 0, 0 ,0, 0
while max_range < n:
for i in range(idx, len(ranges)):
if i - ranges[i] <= min_range and i + ranges[i] >= max_range:
max_range = i + ranges[i]
idx = i
if min_range == max_range:
return -1
open_taps += 1
min_range = max_range
return open_taps
|
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n).
There are n + 1 taps located at points [0, 1, ..., n] in the garden.
Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open.
Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.
Example 1:
Input: n = 5, ranges = [3,4,1,1,0,0]
Output: 1
Explanation: The tap at point 0 can cover the interval [-3,3]
The tap at point 1 can cover the interval [-3,5]
The tap at point 2 can cover the interval [1,3]
The tap at point 3 can cover the interval [2,4]
The tap at point 4 can cover the interval [4,4]
The tap at point 5 can cover the interval [5,5]
Opening Only the second tap will water the whole garden [0,5]
Example 2:
Input: n = 3, ranges = [0,0,0,0]
Output: -1
Explanation: Even if you activate all the four taps you cannot water the whole garden.
Example 3:
Input: n = 7, ranges = [1,2,1,0,2,1,0,1]
Output: 3
Example 4:
Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4]
Output: 2
Example 5:
Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4]
Output: 1
Constraints:
1 <= n <= 10^4
ranges.length == n + 1
0 <= ranges[i] <= 100
|
class Solution:
def minTaps(self, n: int, ranges: List[int]) -> int:
open_ranges = sorted([(idx-span, idx+span) for idx, span in enumerate(ranges)])
start, end, count = 0, 0, 0
i = 0
while i < len(ranges):
for j in range(i, len(ranges)):
l, r = open_ranges[j]
if l <= start:
if r >= end:
end = r
i = j
count += 1
if end >= n:
return count
if start == end:
return -1
start = end
|
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n).
There are n + 1 taps located at points [0, 1, ..., n] in the garden.
Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open.
Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.
Example 1:
Input: n = 5, ranges = [3,4,1,1,0,0]
Output: 1
Explanation: The tap at point 0 can cover the interval [-3,3]
The tap at point 1 can cover the interval [-3,5]
The tap at point 2 can cover the interval [1,3]
The tap at point 3 can cover the interval [2,4]
The tap at point 4 can cover the interval [4,4]
The tap at point 5 can cover the interval [5,5]
Opening Only the second tap will water the whole garden [0,5]
Example 2:
Input: n = 3, ranges = [0,0,0,0]
Output: -1
Explanation: Even if you activate all the four taps you cannot water the whole garden.
Example 3:
Input: n = 7, ranges = [1,2,1,0,2,1,0,1]
Output: 3
Example 4:
Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4]
Output: 2
Example 5:
Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4]
Output: 1
Constraints:
1 <= n <= 10^4
ranges.length == n + 1
0 <= ranges[i] <= 100
|
class Solution:
def minTaps(self, n: int, ranges: List[int]) -> int:
open_ranges = sorted([(idx-span, idx+span) for idx, span in enumerate(ranges)])
start, end, count = 0, 0, 0
i = 0
while i < len(ranges):
for j in range(i, len(ranges)):
l, r = open_ranges[j]
if l <= start:
if r >= end:
end = r
i = j
count += 1
if end >= n:
return count
if start == end:
return -1
start = end
|
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n).
There are n + 1 taps located at points [0, 1, ..., n] in the garden.
Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open.
Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.
Example 1:
Input: n = 5, ranges = [3,4,1,1,0,0]
Output: 1
Explanation: The tap at point 0 can cover the interval [-3,3]
The tap at point 1 can cover the interval [-3,5]
The tap at point 2 can cover the interval [1,3]
The tap at point 3 can cover the interval [2,4]
The tap at point 4 can cover the interval [4,4]
The tap at point 5 can cover the interval [5,5]
Opening Only the second tap will water the whole garden [0,5]
Example 2:
Input: n = 3, ranges = [0,0,0,0]
Output: -1
Explanation: Even if you activate all the four taps you cannot water the whole garden.
Example 3:
Input: n = 7, ranges = [1,2,1,0,2,1,0,1]
Output: 3
Example 4:
Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4]
Output: 2
Example 5:
Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4]
Output: 1
Constraints:
1 <= n <= 10^4
ranges.length == n + 1
0 <= ranges[i] <= 100
|
class Solution:
def minTaps(self, n: int, ranges: List[int]) -> int:
max_right = [0]*(n+1)
for i,r in enumerate(ranges):
max_right[max(0,i-r)] = max(max_right[max(0,i-r)],min(n,i+ranges[i]))
dp = [float('inf')]*len(max_right)
dp[0] = 0
for i, x in enumerate(ranges):
for j in range(i+1,max_right[i]+1):
dp[j] = min(dp[j], dp[i] + 1)
return dp[n] if dp[n] != float('inf') else -1
|
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n).
There are n + 1 taps located at points [0, 1, ..., n] in the garden.
Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open.
Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.
Example 1:
Input: n = 5, ranges = [3,4,1,1,0,0]
Output: 1
Explanation: The tap at point 0 can cover the interval [-3,3]
The tap at point 1 can cover the interval [-3,5]
The tap at point 2 can cover the interval [1,3]
The tap at point 3 can cover the interval [2,4]
The tap at point 4 can cover the interval [4,4]
The tap at point 5 can cover the interval [5,5]
Opening Only the second tap will water the whole garden [0,5]
Example 2:
Input: n = 3, ranges = [0,0,0,0]
Output: -1
Explanation: Even if you activate all the four taps you cannot water the whole garden.
Example 3:
Input: n = 7, ranges = [1,2,1,0,2,1,0,1]
Output: 3
Example 4:
Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4]
Output: 2
Example 5:
Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4]
Output: 1
Constraints:
1 <= n <= 10^4
ranges.length == n + 1
0 <= ranges[i] <= 100
|
# 思路
# 贪心策略,我们尽量找到能够覆盖最远(右边)位置的水龙头,并记录它最右覆盖的土地。
# 我们使用 furthest[i] 来记录经过每一个水龙头 i 能够覆盖的最右侧土地。
# 一共有 n+1 个水龙头,我们遍历 n + 1 次。
# 对于每次我们计算水龙头的左右边界,[i - ranges[i], i + ranges[i]]
# 我们更新左右边界范围内的水龙头的 furthest
# 最后从土地 0 开始,一直到土地 n ,记录水龙头数目
# 复杂度分析
# 时间复杂度:时间复杂度取决 l 和 r,也就是说取决于 ranges 数组的值,假设 ranges 的平均大小为 Size 的话,那么时间复杂度为 $O(N * Size)$。
# 空间复杂度:我们使用了 furthest 数组, 因此空间复杂度为 $O(N)$。
class Solution:
def minTaps(self, n: int, ranges: List[int]) -> int:
furthest, cnt, cur = [0] * n, 0, 0 #做initialization
for i in range(n + 1):
l = max(0, i - ranges[i]) #左边最远到哪里
r = min(n, i + ranges[i]) #n是自己
for j in range(l, r): #在左右两边,表示右边能到的最远的地方,
furthest[j] = max(furthest[j], r) #经过这个水龙头最多能走到哪里,跟自己无关(没那么有关),跟经过我最远到哪里有关系
while cur < n: #从头到尾扫一遍,看看有米有没灌溉到的地方
if furthest[cur] == 0: return -1
cur = furthest[cur] # 比这个数值小的都是能扫到的部分,所以可以直接跳过去
cnt += 1
return cnt #左边有问题的话会-1
|
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n).
There are n + 1 taps located at points [0, 1, ..., n] in the garden.
Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open.
Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.
Example 1:
Input: n = 5, ranges = [3,4,1,1,0,0]
Output: 1
Explanation: The tap at point 0 can cover the interval [-3,3]
The tap at point 1 can cover the interval [-3,5]
The tap at point 2 can cover the interval [1,3]
The tap at point 3 can cover the interval [2,4]
The tap at point 4 can cover the interval [4,4]
The tap at point 5 can cover the interval [5,5]
Opening Only the second tap will water the whole garden [0,5]
Example 2:
Input: n = 3, ranges = [0,0,0,0]
Output: -1
Explanation: Even if you activate all the four taps you cannot water the whole garden.
Example 3:
Input: n = 7, ranges = [1,2,1,0,2,1,0,1]
Output: 3
Example 4:
Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4]
Output: 2
Example 5:
Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4]
Output: 1
Constraints:
1 <= n <= 10^4
ranges.length == n + 1
0 <= ranges[i] <= 100
|
class Solution:
def minTaps(self, n: int, ranges: List[int]) -> int:
intervals = []
for i, r in enumerate(ranges):
intervals.append((i - r if i - r > 0 else 0, i + r))
intervals.sort(key=lambda t: (t[0], -t[1]))
l = len(intervals)
i = 0
ans = 1
while i < l:
if intervals[i][1] >= n:
return ans
right = -1
next_i = -1
for j in range(i + 1, l):
if intervals[j][0] <= intervals[i][1] and intervals[j][1] >= right:
right = intervals[j][1]
next_i = j
if next_i == -1:
return -1
ans += 1
i = next_i
return ans
|
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n).
There are n + 1 taps located at points [0, 1, ..., n] in the garden.
Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open.
Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.
Example 1:
Input: n = 5, ranges = [3,4,1,1,0,0]
Output: 1
Explanation: The tap at point 0 can cover the interval [-3,3]
The tap at point 1 can cover the interval [-3,5]
The tap at point 2 can cover the interval [1,3]
The tap at point 3 can cover the interval [2,4]
The tap at point 4 can cover the interval [4,4]
The tap at point 5 can cover the interval [5,5]
Opening Only the second tap will water the whole garden [0,5]
Example 2:
Input: n = 3, ranges = [0,0,0,0]
Output: -1
Explanation: Even if you activate all the four taps you cannot water the whole garden.
Example 3:
Input: n = 7, ranges = [1,2,1,0,2,1,0,1]
Output: 3
Example 4:
Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4]
Output: 2
Example 5:
Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4]
Output: 1
Constraints:
1 <= n <= 10^4
ranges.length == n + 1
0 <= ranges[i] <= 100
|
class Solution:
def minTaps(self, n: int, ranges: List[int]) -> int:
# taps=[]
# for i in range(len(ranges)):
# taps.append((i-ranges[i],i+ranges[i]))
# taps.sort()
# @lru_cache(None)
# def helper(i):
# # minimum number of taps to open if we open i to reach end
# if i>=n+1:return float('inf')
# if taps[i][1]>=n:return 1
# ans=float('inf')
# # lets do binary search here
# start=i+1
# end=n
# lastindex=-1
# # get the last index which is smaller than taps[i][1]
# while start<=end:
# mid=start+(end-start)//2
# if taps[mid][0]<=taps[i][1]:
# lastindex=mid
# start=mid+1
# else:
# end=mid-1
# if lastindex==-1:return ans
# for j in range(i+1,lastindex+1):
# if taps[j][0]>taps[i][1]:
# break
# if taps[j][0]<=taps[i][1] and taps[j][1]>taps[i][1]:
# # benefit of picking this guy
# ans=min(ans,1+helper(j))
# # sequential code to do that
# # dont go for binary unless interviewer say so
# # ask him in the approach if he wants to
# # for j in range(i+1,n+1):
# # if taps[j][0]>taps[i][1]:
# # break
# # if taps[j][0]<=taps[i][1] and taps[j][1]>taps[i][1]:
# # # benefit of picking this guy
# # ans=min(ans,1+helper(j))
# return ans
# ans=float('inf')
# for i in range(n,-1,-1):
# res=helper(i)
# if taps[i][0]<=0 and res<ans:
# ans=res
# return -1 if ans==float('inf') else ans
best = [float('inf') for _ in ranges]
for i, r in enumerate(ranges):
if r > 0:
start, end = i - r, i + r
best_with = 1 + (best[start] if start > 0 else 0)
for j in range(max(0, start), min(len(ranges), end+1)):
best[j] = min(best[j], best_with)
if any(v for v in best if v == float('inf')):
return -1
return best[-1]
|
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n).
There are n + 1 taps located at points [0, 1, ..., n] in the garden.
Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open.
Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.
Example 1:
Input: n = 5, ranges = [3,4,1,1,0,0]
Output: 1
Explanation: The tap at point 0 can cover the interval [-3,3]
The tap at point 1 can cover the interval [-3,5]
The tap at point 2 can cover the interval [1,3]
The tap at point 3 can cover the interval [2,4]
The tap at point 4 can cover the interval [4,4]
The tap at point 5 can cover the interval [5,5]
Opening Only the second tap will water the whole garden [0,5]
Example 2:
Input: n = 3, ranges = [0,0,0,0]
Output: -1
Explanation: Even if you activate all the four taps you cannot water the whole garden.
Example 3:
Input: n = 7, ranges = [1,2,1,0,2,1,0,1]
Output: 3
Example 4:
Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4]
Output: 2
Example 5:
Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4]
Output: 1
Constraints:
1 <= n <= 10^4
ranges.length == n + 1
0 <= ranges[i] <= 100
|
class Solution:
def minTaps(self, n: int, ranges: List[int]) -> int:
max_right = [0]*(n+1)
for i,r in enumerate(ranges):
max_right[max(0,i-r)] = max(max_right[max(0,i-r)],min(n,i+ranges[i]))
dp = [float('inf')]*len(max_right)
dp[0] = 0
for i, x in enumerate(ranges):
for j in range(i+1,max_right[i]+1):
dp[j] = min(dp[j], dp[i] + 1)
return dp[n] if dp[n] < n + 2 else -1
|
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n).
There are n + 1 taps located at points [0, 1, ..., n] in the garden.
Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open.
Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.
Example 1:
Input: n = 5, ranges = [3,4,1,1,0,0]
Output: 1
Explanation: The tap at point 0 can cover the interval [-3,3]
The tap at point 1 can cover the interval [-3,5]
The tap at point 2 can cover the interval [1,3]
The tap at point 3 can cover the interval [2,4]
The tap at point 4 can cover the interval [4,4]
The tap at point 5 can cover the interval [5,5]
Opening Only the second tap will water the whole garden [0,5]
Example 2:
Input: n = 3, ranges = [0,0,0,0]
Output: -1
Explanation: Even if you activate all the four taps you cannot water the whole garden.
Example 3:
Input: n = 7, ranges = [1,2,1,0,2,1,0,1]
Output: 3
Example 4:
Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4]
Output: 2
Example 5:
Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4]
Output: 1
Constraints:
1 <= n <= 10^4
ranges.length == n + 1
0 <= ranges[i] <= 100
|
class Solution:
def minTaps(self, n: int, ranges: List[int]) -> int:
if n < 1 or ranges is None: return -1
dp = [None]*len(ranges)
# for each tap
for i in range(len(dp)):
# calc tap left and right extent
l = max(0, i-ranges[i])
r = min(len(dp)-1, i+ranges[i])
# for each cell in extent, take the min of the cell or the left extent of the tap
for j in range(l, r+1):
if dp[j] is None:
dp[j] = l
else:
dp[j] = min(dp[j], l)
# since we can reach to front from last, only one tap is required
if dp[-1] == 0:
return 1
# start from end, jump backwards incrementing tap count each time
# careful to exit if no jump can be made
i = len(dp)-1
c = 0
while i > 0 and dp[i] is not None and dp[i] != i:
i = dp[i]
c += 1
# if we can jump all the way to the front, then return the count else -1
return c if i == 0 else -1
|
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n).
There are n + 1 taps located at points [0, 1, ..., n] in the garden.
Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open.
Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.
Example 1:
Input: n = 5, ranges = [3,4,1,1,0,0]
Output: 1
Explanation: The tap at point 0 can cover the interval [-3,3]
The tap at point 1 can cover the interval [-3,5]
The tap at point 2 can cover the interval [1,3]
The tap at point 3 can cover the interval [2,4]
The tap at point 4 can cover the interval [4,4]
The tap at point 5 can cover the interval [5,5]
Opening Only the second tap will water the whole garden [0,5]
Example 2:
Input: n = 3, ranges = [0,0,0,0]
Output: -1
Explanation: Even if you activate all the four taps you cannot water the whole garden.
Example 3:
Input: n = 7, ranges = [1,2,1,0,2,1,0,1]
Output: 3
Example 4:
Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4]
Output: 2
Example 5:
Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4]
Output: 1
Constraints:
1 <= n <= 10^4
ranges.length == n + 1
0 <= ranges[i] <= 100
|
class Solution:
def minTaps(self, n: int, ranges: List[int]) -> int:
inf = 1 << 50
prev = collections.defaultdict(lambda: inf)
for i, r in enumerate(ranges):
prev[min(i+r, n)] = min(prev[min(i+r, n)],max(i-r,0))
dp = [inf] * (n + 1)
dp[0] = 0
for x in range(n+1):
for y in range(prev[x], x):
dp[x] = min(dp[y] + 1, dp[x])
res = dp[n]
if res >= inf:
return -1
return res
|
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n).
There are n + 1 taps located at points [0, 1, ..., n] in the garden.
Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open.
Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.
Example 1:
Input: n = 5, ranges = [3,4,1,1,0,0]
Output: 1
Explanation: The tap at point 0 can cover the interval [-3,3]
The tap at point 1 can cover the interval [-3,5]
The tap at point 2 can cover the interval [1,3]
The tap at point 3 can cover the interval [2,4]
The tap at point 4 can cover the interval [4,4]
The tap at point 5 can cover the interval [5,5]
Opening Only the second tap will water the whole garden [0,5]
Example 2:
Input: n = 3, ranges = [0,0,0,0]
Output: -1
Explanation: Even if you activate all the four taps you cannot water the whole garden.
Example 3:
Input: n = 7, ranges = [1,2,1,0,2,1,0,1]
Output: 3
Example 4:
Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4]
Output: 2
Example 5:
Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4]
Output: 1
Constraints:
1 <= n <= 10^4
ranges.length == n + 1
0 <= ranges[i] <= 100
|
class Solution:
def minTaps(self, n: int, ranges: List[int]) -> int:
dp=[sys.maxsize]*(n+1)
dp[0]=0
for i in range(n+1):
l=max(0,i-ranges[i])
r=min(n,i+ranges[i])
for j in range(l,r+1):
dp[j]=min(dp[j],1+dp[l])
if(dp[n]==sys.maxsize):
return -1
return dp[n]
|
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n).
There are n + 1 taps located at points [0, 1, ..., n] in the garden.
Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open.
Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.
Example 1:
Input: n = 5, ranges = [3,4,1,1,0,0]
Output: 1
Explanation: The tap at point 0 can cover the interval [-3,3]
The tap at point 1 can cover the interval [-3,5]
The tap at point 2 can cover the interval [1,3]
The tap at point 3 can cover the interval [2,4]
The tap at point 4 can cover the interval [4,4]
The tap at point 5 can cover the interval [5,5]
Opening Only the second tap will water the whole garden [0,5]
Example 2:
Input: n = 3, ranges = [0,0,0,0]
Output: -1
Explanation: Even if you activate all the four taps you cannot water the whole garden.
Example 3:
Input: n = 7, ranges = [1,2,1,0,2,1,0,1]
Output: 3
Example 4:
Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4]
Output: 2
Example 5:
Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4]
Output: 1
Constraints:
1 <= n <= 10^4
ranges.length == n + 1
0 <= ranges[i] <= 100
|
class Solution:
def minTaps(self, n: int, ranges: List[int]) -> int:
'''
# top-down TLE
dp = [0] + [float('inf')]*n
def helper(k):
if not (1<= k <= n):
return 0
if dp[k]!=float('inf'):
return dp[k]
dp[k] = min([helper(i-x)+1 for i, x in enumerate(ranges)\\
if k in range(i-x+1, i+x+1)] or [-1])
return dp[k]
return helper(n)
'''
if n == 0:
return 0
dp = [0] + [float('inf')]*n
for i, x in enumerate(ranges):
start, end = max(0, i-x), min(n, i+x)
for j in range(start, end+1):
dp[j] = min(dp[j], dp[start]+1)
return dp[-1] if dp[-1]!=float('inf') else -1
'''
# greedy
covers = sorted([[i-x, i+x] for i, x in enumerate(ranges)], key = lambda x: (x[0], -x[1]))
#print(covers)
res, prev_end, end = 0, -float('inf'), 0
for src, dest in covers:
if src > end or end >= n:
break
if prev_end < src <= end:
prev_end, res = end, res + 1
end = max(end, dest)
return res if end >= n else -1
'''
|
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n).
There are n + 1 taps located at points [0, 1, ..., n] in the garden.
Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open.
Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.
Example 1:
Input: n = 5, ranges = [3,4,1,1,0,0]
Output: 1
Explanation: The tap at point 0 can cover the interval [-3,3]
The tap at point 1 can cover the interval [-3,5]
The tap at point 2 can cover the interval [1,3]
The tap at point 3 can cover the interval [2,4]
The tap at point 4 can cover the interval [4,4]
The tap at point 5 can cover the interval [5,5]
Opening Only the second tap will water the whole garden [0,5]
Example 2:
Input: n = 3, ranges = [0,0,0,0]
Output: -1
Explanation: Even if you activate all the four taps you cannot water the whole garden.
Example 3:
Input: n = 7, ranges = [1,2,1,0,2,1,0,1]
Output: 3
Example 4:
Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4]
Output: 2
Example 5:
Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4]
Output: 1
Constraints:
1 <= n <= 10^4
ranges.length == n + 1
0 <= ranges[i] <= 100
|
class Solution:
def minTaps(self, n: int, ranges: List[int]) -> int:
best = [n+1 for _ in ranges]
for i, r in enumerate(ranges):
if r > 0:
start, end = i - r, i + r
best_with = 1 + (best[start] if start > 0 else 0)
for j in range(max(0, start), min(len(ranges), end+1)):
best[j] = min(best[j], best_with)
if any(v for v in best if v == n+1):
return -1
return best[-1]
|
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n).
There are n + 1 taps located at points [0, 1, ..., n] in the garden.
Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open.
Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.
Example 1:
Input: n = 5, ranges = [3,4,1,1,0,0]
Output: 1
Explanation: The tap at point 0 can cover the interval [-3,3]
The tap at point 1 can cover the interval [-3,5]
The tap at point 2 can cover the interval [1,3]
The tap at point 3 can cover the interval [2,4]
The tap at point 4 can cover the interval [4,4]
The tap at point 5 can cover the interval [5,5]
Opening Only the second tap will water the whole garden [0,5]
Example 2:
Input: n = 3, ranges = [0,0,0,0]
Output: -1
Explanation: Even if you activate all the four taps you cannot water the whole garden.
Example 3:
Input: n = 7, ranges = [1,2,1,0,2,1,0,1]
Output: 3
Example 4:
Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4]
Output: 2
Example 5:
Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4]
Output: 1
Constraints:
1 <= n <= 10^4
ranges.length == n + 1
0 <= ranges[i] <= 100
|
class Solution:
def minTaps(self, n: int, ranges: List[int]) -> int:
dp = [float('inf')] * (n+1)
dp[0] = 0
for cur in range(n+1):
left = max(0,cur - ranges[cur] + 1)
right = min(n,cur+ranges[cur])
lastend = max(0, cur - ranges[cur])
for idx in range(left, right+1):
dp[idx] = min(dp[idx], dp[lastend] + 1)
return dp[-1] if dp[-1] < float('inf') else -1
|
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n).
There are n + 1 taps located at points [0, 1, ..., n] in the garden.
Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open.
Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.
Example 1:
Input: n = 5, ranges = [3,4,1,1,0,0]
Output: 1
Explanation: The tap at point 0 can cover the interval [-3,3]
The tap at point 1 can cover the interval [-3,5]
The tap at point 2 can cover the interval [1,3]
The tap at point 3 can cover the interval [2,4]
The tap at point 4 can cover the interval [4,4]
The tap at point 5 can cover the interval [5,5]
Opening Only the second tap will water the whole garden [0,5]
Example 2:
Input: n = 3, ranges = [0,0,0,0]
Output: -1
Explanation: Even if you activate all the four taps you cannot water the whole garden.
Example 3:
Input: n = 7, ranges = [1,2,1,0,2,1,0,1]
Output: 3
Example 4:
Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4]
Output: 2
Example 5:
Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4]
Output: 1
Constraints:
1 <= n <= 10^4
ranges.length == n + 1
0 <= ranges[i] <= 100
|
class Solution:
def minTaps(self, n: int, ranges: List[int]) -> int:
dp = [-1 for x in range(n+1)]
for i in range(n+1):
minId = max(0, i-ranges[i])
maxId = min(n, i+ranges[i])
for i in range(minId, maxId+1):
dp[i] = max(dp[i], maxId)
if dp[0] == -1:
return -1
maxReach = dp[0]
count = 1
while maxReach < n:
nxt = dp[maxReach]
if nxt == maxReach:
return -1
maxReach = nxt
count += 1
return count
|
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n).
There are n + 1 taps located at points [0, 1, ..., n] in the garden.
Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open.
Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.
Example 1:
Input: n = 5, ranges = [3,4,1,1,0,0]
Output: 1
Explanation: The tap at point 0 can cover the interval [-3,3]
The tap at point 1 can cover the interval [-3,5]
The tap at point 2 can cover the interval [1,3]
The tap at point 3 can cover the interval [2,4]
The tap at point 4 can cover the interval [4,4]
The tap at point 5 can cover the interval [5,5]
Opening Only the second tap will water the whole garden [0,5]
Example 2:
Input: n = 3, ranges = [0,0,0,0]
Output: -1
Explanation: Even if you activate all the four taps you cannot water the whole garden.
Example 3:
Input: n = 7, ranges = [1,2,1,0,2,1,0,1]
Output: 3
Example 4:
Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4]
Output: 2
Example 5:
Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4]
Output: 1
Constraints:
1 <= n <= 10^4
ranges.length == n + 1
0 <= ranges[i] <= 100
|
class Solution:
def minTaps(self, n: int, ranges: List[int]) -> int:
# dp[i] min taps to water [0, i]
# dp[0] = 0
dp = [0] + [n+2] * n # n+1 is possible value
for i, v in enumerate(ranges):
left = max(i-v, 0)
right = min(i+v, n)
for j in range(left, right+1):
dp[j] = min(dp[j], dp[left]+1)
if dp[n] < n+2:
return dp[n]
return -1
|
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n).
There are n + 1 taps located at points [0, 1, ..., n] in the garden.
Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open.
Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.
Example 1:
Input: n = 5, ranges = [3,4,1,1,0,0]
Output: 1
Explanation: The tap at point 0 can cover the interval [-3,3]
The tap at point 1 can cover the interval [-3,5]
The tap at point 2 can cover the interval [1,3]
The tap at point 3 can cover the interval [2,4]
The tap at point 4 can cover the interval [4,4]
The tap at point 5 can cover the interval [5,5]
Opening Only the second tap will water the whole garden [0,5]
Example 2:
Input: n = 3, ranges = [0,0,0,0]
Output: -1
Explanation: Even if you activate all the four taps you cannot water the whole garden.
Example 3:
Input: n = 7, ranges = [1,2,1,0,2,1,0,1]
Output: 3
Example 4:
Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4]
Output: 2
Example 5:
Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4]
Output: 1
Constraints:
1 <= n <= 10^4
ranges.length == n + 1
0 <= ranges[i] <= 100
|
class Solution:
def minTaps(self, n: int, ranges: List[int]) -> int:
full_ranges = []
for i in range(n + 1):
r = (i - ranges[i], i + ranges[i])
full_ranges.append(r)
full_ranges.sort()
range_idx = 0
covered_max = 0
num_taps = 0
while covered_max < n and range_idx < len(full_ranges):
if full_ranges[range_idx][0] > covered_max:
return -1
furthest = -1
while range_idx < len(full_ranges) and full_ranges[range_idx][0] <= covered_max:
furthest = max(furthest, full_ranges[range_idx][1])
range_idx += 1
assert(furthest != -1)
num_taps += 1
covered_max = furthest
if covered_max < n:
return -1
else:
return num_taps
|
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n).
There are n + 1 taps located at points [0, 1, ..., n] in the garden.
Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open.
Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.
Example 1:
Input: n = 5, ranges = [3,4,1,1,0,0]
Output: 1
Explanation: The tap at point 0 can cover the interval [-3,3]
The tap at point 1 can cover the interval [-3,5]
The tap at point 2 can cover the interval [1,3]
The tap at point 3 can cover the interval [2,4]
The tap at point 4 can cover the interval [4,4]
The tap at point 5 can cover the interval [5,5]
Opening Only the second tap will water the whole garden [0,5]
Example 2:
Input: n = 3, ranges = [0,0,0,0]
Output: -1
Explanation: Even if you activate all the four taps you cannot water the whole garden.
Example 3:
Input: n = 7, ranges = [1,2,1,0,2,1,0,1]
Output: 3
Example 4:
Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4]
Output: 2
Example 5:
Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4]
Output: 1
Constraints:
1 <= n <= 10^4
ranges.length == n + 1
0 <= ranges[i] <= 100
|
class Solution:
def minTaps(self, n: int, ranges: List[int]) -> int:
max = 0
min = 0
index = 0
total = 0
while(max<n):
for i in range(index,len(ranges)):
left = i-ranges[i]
right = i+ranges[i]
if left <= min and right > max:
max = right
index = i
if min == max:
return -1
total+=1
min = max
return total
|
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n).
There are n + 1 taps located at points [0, 1, ..., n] in the garden.
Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open.
Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.
Example 1:
Input: n = 5, ranges = [3,4,1,1,0,0]
Output: 1
Explanation: The tap at point 0 can cover the interval [-3,3]
The tap at point 1 can cover the interval [-3,5]
The tap at point 2 can cover the interval [1,3]
The tap at point 3 can cover the interval [2,4]
The tap at point 4 can cover the interval [4,4]
The tap at point 5 can cover the interval [5,5]
Opening Only the second tap will water the whole garden [0,5]
Example 2:
Input: n = 3, ranges = [0,0,0,0]
Output: -1
Explanation: Even if you activate all the four taps you cannot water the whole garden.
Example 3:
Input: n = 7, ranges = [1,2,1,0,2,1,0,1]
Output: 3
Example 4:
Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4]
Output: 2
Example 5:
Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4]
Output: 1
Constraints:
1 <= n <= 10^4
ranges.length == n + 1
0 <= ranges[i] <= 100
|
class Solution:
def minTaps(self, n: int, ranges: List[int]) -> int:
rangesCovered = {}
# key = spot, value = range as tuple
for i, ran in enumerate(ranges):
if (ran == 0):
continue
rangesCovered[i] = (max(0, i - ranges[i]), min(n, i + ranges[i]))
# print(rangesCovered)
dp = [n + 2] * (n + 1)
dp[0] = 0
# dp[i] = min number of taps to water all [0, i]
for curRange in list(rangesCovered.values()):
# print(curRange)
startInd = curRange[0]
endInd = curRange[1]
for i in range(startInd + 1, endInd + 1):
dp[i] = min(dp[i], dp[startInd] + 1)
# print(dp)
if (dp[n] == (n + 2)):
return -1
else:
return dp[n]
# taps = []
# for i in range(n + 1):
# isCovered[i] = False
# taps.append(i)
# curMins = float(\"inf\")
# def recur(tapsRem, curCount):
# # print(curCount, tapsRem)
# # print(isCovered)
# nonlocal curMins
# if (all(isCovered.values())):
# curMins = min(curMins, curCount)
# return
# else:
# if (curCount > curMins):
# return
# for i, val in enumerate(tapsRem):
# newRem = tapsRem[:i] + tapsRem[i + 1:]
# wasFalses = []
# for j in range(rangesCovered[i][0], rangesCovered[i][1] + 1):
# if isCovered[j] == False:
# wasFalses.append(j)
# isCovered[j] = True
# recur(newRem, curCount + 1)
# for ind in wasFalses:
# isCovered[ind] = False
# # print(taps)
# recur(taps, 0)
# if (curMins == float(\"inf\")):
# return -1
# return curMins
|
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n).
There are n + 1 taps located at points [0, 1, ..., n] in the garden.
Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open.
Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.
Example 1:
Input: n = 5, ranges = [3,4,1,1,0,0]
Output: 1
Explanation: The tap at point 0 can cover the interval [-3,3]
The tap at point 1 can cover the interval [-3,5]
The tap at point 2 can cover the interval [1,3]
The tap at point 3 can cover the interval [2,4]
The tap at point 4 can cover the interval [4,4]
The tap at point 5 can cover the interval [5,5]
Opening Only the second tap will water the whole garden [0,5]
Example 2:
Input: n = 3, ranges = [0,0,0,0]
Output: -1
Explanation: Even if you activate all the four taps you cannot water the whole garden.
Example 3:
Input: n = 7, ranges = [1,2,1,0,2,1,0,1]
Output: 3
Example 4:
Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4]
Output: 2
Example 5:
Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4]
Output: 1
Constraints:
1 <= n <= 10^4
ranges.length == n + 1
0 <= ranges[i] <= 100
|
class Solution:
def minTaps(self, n: int, ranges: List[int]) -> int:
# cover[i] shows the list number of taps needed to cover 0-i
# inf for impossible
cover = [math.inf] * n
for i, r in enumerate(ranges):
start = max(i-r, 0)
end = min(i+r, n)
pre_min = cover[start-1] if start > 0 else 0
for j in range(start, end):
cover[j] = min(cover[j], 1+pre_min)
return cover[-1] if cover[-1] != math.inf else -1
|
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n).
There are n + 1 taps located at points [0, 1, ..., n] in the garden.
Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open.
Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.
Example 1:
Input: n = 5, ranges = [3,4,1,1,0,0]
Output: 1
Explanation: The tap at point 0 can cover the interval [-3,3]
The tap at point 1 can cover the interval [-3,5]
The tap at point 2 can cover the interval [1,3]
The tap at point 3 can cover the interval [2,4]
The tap at point 4 can cover the interval [4,4]
The tap at point 5 can cover the interval [5,5]
Opening Only the second tap will water the whole garden [0,5]
Example 2:
Input: n = 3, ranges = [0,0,0,0]
Output: -1
Explanation: Even if you activate all the four taps you cannot water the whole garden.
Example 3:
Input: n = 7, ranges = [1,2,1,0,2,1,0,1]
Output: 3
Example 4:
Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4]
Output: 2
Example 5:
Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4]
Output: 1
Constraints:
1 <= n <= 10^4
ranges.length == n + 1
0 <= ranges[i] <= 100
|
class Solution:
def minTaps(self, n: int, ranges: List[int]) -> int:
dp = [sys.maxsize]*(n+1)
dp[0]=0
for i,tmp in enumerate (ranges):
left = max(0,i-tmp)
right = min(n,i+tmp)
for j in range(left,right+1):
dp[j]=min(dp[j],dp[left]+1)
return dp[n] if dp[n]<sys.maxsize else -1
|
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n).
There are n + 1 taps located at points [0, 1, ..., n] in the garden.
Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open.
Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.
Example 1:
Input: n = 5, ranges = [3,4,1,1,0,0]
Output: 1
Explanation: The tap at point 0 can cover the interval [-3,3]
The tap at point 1 can cover the interval [-3,5]
The tap at point 2 can cover the interval [1,3]
The tap at point 3 can cover the interval [2,4]
The tap at point 4 can cover the interval [4,4]
The tap at point 5 can cover the interval [5,5]
Opening Only the second tap will water the whole garden [0,5]
Example 2:
Input: n = 3, ranges = [0,0,0,0]
Output: -1
Explanation: Even if you activate all the four taps you cannot water the whole garden.
Example 3:
Input: n = 7, ranges = [1,2,1,0,2,1,0,1]
Output: 3
Example 4:
Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4]
Output: 2
Example 5:
Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4]
Output: 1
Constraints:
1 <= n <= 10^4
ranges.length == n + 1
0 <= ranges[i] <= 100
|
class Solution:
def minTaps(self, n: int, ranges: List[int]) -> int:
dp = [n+2 for _ in range(n+1)] #minimum number of taps to water i
dp[0] = 0
for i, amount in enumerate(ranges):
left = max(0,i - amount)
right = min(n, i + amount)
for j in range(left + 1, right+ 1):
dp[j] = min(dp[j], dp[left] +1)
return -1 if dp[n] == n+2 else dp[n]
|
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n).
There are n + 1 taps located at points [0, 1, ..., n] in the garden.
Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open.
Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.
Example 1:
Input: n = 5, ranges = [3,4,1,1,0,0]
Output: 1
Explanation: The tap at point 0 can cover the interval [-3,3]
The tap at point 1 can cover the interval [-3,5]
The tap at point 2 can cover the interval [1,3]
The tap at point 3 can cover the interval [2,4]
The tap at point 4 can cover the interval [4,4]
The tap at point 5 can cover the interval [5,5]
Opening Only the second tap will water the whole garden [0,5]
Example 2:
Input: n = 3, ranges = [0,0,0,0]
Output: -1
Explanation: Even if you activate all the four taps you cannot water the whole garden.
Example 3:
Input: n = 7, ranges = [1,2,1,0,2,1,0,1]
Output: 3
Example 4:
Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4]
Output: 2
Example 5:
Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4]
Output: 1
Constraints:
1 <= n <= 10^4
ranges.length == n + 1
0 <= ranges[i] <= 100
|
class Solution:
def minTaps(self, n: int, ranges: List[int]) -> int:
dp = [n+2 for _ in range(n+2)] #minimum number of taps to water i
dp[0] = 0
for i, amount in enumerate(ranges):
left = max(0,i-amount)
right = min(n, i + amount)
for j in range(left + 1, right+ 1):
dp[j] = min(dp[j], dp[left] +1)
return -1 if dp[n] == n+2 else dp[n]
|
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n).
There are n + 1 taps located at points [0, 1, ..., n] in the garden.
Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open.
Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.
Example 1:
Input: n = 5, ranges = [3,4,1,1,0,0]
Output: 1
Explanation: The tap at point 0 can cover the interval [-3,3]
The tap at point 1 can cover the interval [-3,5]
The tap at point 2 can cover the interval [1,3]
The tap at point 3 can cover the interval [2,4]
The tap at point 4 can cover the interval [4,4]
The tap at point 5 can cover the interval [5,5]
Opening Only the second tap will water the whole garden [0,5]
Example 2:
Input: n = 3, ranges = [0,0,0,0]
Output: -1
Explanation: Even if you activate all the four taps you cannot water the whole garden.
Example 3:
Input: n = 7, ranges = [1,2,1,0,2,1,0,1]
Output: 3
Example 4:
Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4]
Output: 2
Example 5:
Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4]
Output: 1
Constraints:
1 <= n <= 10^4
ranges.length == n + 1
0 <= ranges[i] <= 100
|
class Solution:
def minTaps(self, n: int, ranges: List[int]) -> int:
dp = [n+2 for _ in range(n + 1)] #min number of water i
dp[0] = 0
for i, amount in enumerate(ranges):
left = max(0, i - amount)
right = min(n, i + amount)
for j in range(left, right+1):
dp[j] = min(dp[j], dp[left] + 1)
return -1 if dp[n] == n+2 else dp[n]
|
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n).
There are n + 1 taps located at points [0, 1, ..., n] in the garden.
Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open.
Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.
Example 1:
Input: n = 5, ranges = [3,4,1,1,0,0]
Output: 1
Explanation: The tap at point 0 can cover the interval [-3,3]
The tap at point 1 can cover the interval [-3,5]
The tap at point 2 can cover the interval [1,3]
The tap at point 3 can cover the interval [2,4]
The tap at point 4 can cover the interval [4,4]
The tap at point 5 can cover the interval [5,5]
Opening Only the second tap will water the whole garden [0,5]
Example 2:
Input: n = 3, ranges = [0,0,0,0]
Output: -1
Explanation: Even if you activate all the four taps you cannot water the whole garden.
Example 3:
Input: n = 7, ranges = [1,2,1,0,2,1,0,1]
Output: 3
Example 4:
Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4]
Output: 2
Example 5:
Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4]
Output: 1
Constraints:
1 <= n <= 10^4
ranges.length == n + 1
0 <= ranges[i] <= 100
|
class Solution:
def minTaps(self, n: int, ranges: List[int]) -> int:
rangelist = [None] * (n +1)
for i in range(n+1):
s = max(i - ranges[i], 0)
e = min(i + ranges[i], n)
if rangelist[s] == None:
rangelist[s] = (s,e)
else:
rangelist[s] = (s, max(rangelist[s][1], e))
curr = mx = 0
count = 0
i = 0
while i < len(rangelist):
while True and i < len(rangelist):
if(rangelist[i] == None):
i +=1
continue
s,e = rangelist[i]
if s > mx: return -1
if s > curr:
break
mx = max(e,mx)
i += 1
if curr != mx:
curr = mx
count += 1
if mx < n: return -1
return count
|
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n).
There are n + 1 taps located at points [0, 1, ..., n] in the garden.
Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open.
Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.
Example 1:
Input: n = 5, ranges = [3,4,1,1,0,0]
Output: 1
Explanation: The tap at point 0 can cover the interval [-3,3]
The tap at point 1 can cover the interval [-3,5]
The tap at point 2 can cover the interval [1,3]
The tap at point 3 can cover the interval [2,4]
The tap at point 4 can cover the interval [4,4]
The tap at point 5 can cover the interval [5,5]
Opening Only the second tap will water the whole garden [0,5]
Example 2:
Input: n = 3, ranges = [0,0,0,0]
Output: -1
Explanation: Even if you activate all the four taps you cannot water the whole garden.
Example 3:
Input: n = 7, ranges = [1,2,1,0,2,1,0,1]
Output: 3
Example 4:
Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4]
Output: 2
Example 5:
Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4]
Output: 1
Constraints:
1 <= n <= 10^4
ranges.length == n + 1
0 <= ranges[i] <= 100
|
class Solution:
def minTaps(self, n: int, ranges: List[int]) -> int:
# dp[i] minimum taps required to water garden[:i + 1]
# for every ranges[i], update dp[i - ranges[i]:i + ranges[i] + 1]
# when i - ranges[i] - 1 < 0, return 0
# return -1 when dp[-1] is infinity
# check if it is possible to have a hole in the middle
dp = [float('inf')] * (n + 1)
for i in range(len(ranges)):
if i - ranges[i] <= 0:
taps = 1
else:
taps = dp[i - ranges[i]] + 1
lo = max(i - ranges[i], 0)
hi = min(i + ranges[i], n)
for j in range(lo, hi + 1):
dp[j] = min(dp[j], taps)
return dp[-1] if dp[-1] != float('inf') else -1
|
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n).
There are n + 1 taps located at points [0, 1, ..., n] in the garden.
Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open.
Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.
Example 1:
Input: n = 5, ranges = [3,4,1,1,0,0]
Output: 1
Explanation: The tap at point 0 can cover the interval [-3,3]
The tap at point 1 can cover the interval [-3,5]
The tap at point 2 can cover the interval [1,3]
The tap at point 3 can cover the interval [2,4]
The tap at point 4 can cover the interval [4,4]
The tap at point 5 can cover the interval [5,5]
Opening Only the second tap will water the whole garden [0,5]
Example 2:
Input: n = 3, ranges = [0,0,0,0]
Output: -1
Explanation: Even if you activate all the four taps you cannot water the whole garden.
Example 3:
Input: n = 7, ranges = [1,2,1,0,2,1,0,1]
Output: 3
Example 4:
Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4]
Output: 2
Example 5:
Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4]
Output: 1
Constraints:
1 <= n <= 10^4
ranges.length == n + 1
0 <= ranges[i] <= 100
|
class Solution:
def minTaps(self, n: int, A: List[int]) -> int:
dp=[sys.maxsize]*(n+1)
dp[0]=0
for i,tap_range in enumerate(A):
left=max(0,i-tap_range)
right=min(n,i+tap_range)
for j in range(left,right+1):
dp[j]=min(dp[j],dp[left]+1)
return dp[n] if dp[n]<sys.maxsize else -1
|
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n).
There are n + 1 taps located at points [0, 1, ..., n] in the garden.
Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open.
Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.
Example 1:
Input: n = 5, ranges = [3,4,1,1,0,0]
Output: 1
Explanation: The tap at point 0 can cover the interval [-3,3]
The tap at point 1 can cover the interval [-3,5]
The tap at point 2 can cover the interval [1,3]
The tap at point 3 can cover the interval [2,4]
The tap at point 4 can cover the interval [4,4]
The tap at point 5 can cover the interval [5,5]
Opening Only the second tap will water the whole garden [0,5]
Example 2:
Input: n = 3, ranges = [0,0,0,0]
Output: -1
Explanation: Even if you activate all the four taps you cannot water the whole garden.
Example 3:
Input: n = 7, ranges = [1,2,1,0,2,1,0,1]
Output: 3
Example 4:
Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4]
Output: 2
Example 5:
Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4]
Output: 1
Constraints:
1 <= n <= 10^4
ranges.length == n + 1
0 <= ranges[i] <= 100
|
class Solution:
def minTaps(self, n: int, ranges: List[int]) -> int:
dp=[sys.maxsize]*(n+1)
for i in range(n+1):
if ranges[i]==0:
continue
l=max(0,i-ranges[i])
r=min(n,i+ranges[i])
for j in range(l,r+1):
if l==0:
dp[j]=1
else:
dp[j]=min(dp[j],1+dp[l])
if(dp[n]==sys.maxsize):
return -1
return dp[n]
|
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n).
There are n + 1 taps located at points [0, 1, ..., n] in the garden.
Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open.
Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.
Example 1:
Input: n = 5, ranges = [3,4,1,1,0,0]
Output: 1
Explanation: The tap at point 0 can cover the interval [-3,3]
The tap at point 1 can cover the interval [-3,5]
The tap at point 2 can cover the interval [1,3]
The tap at point 3 can cover the interval [2,4]
The tap at point 4 can cover the interval [4,4]
The tap at point 5 can cover the interval [5,5]
Opening Only the second tap will water the whole garden [0,5]
Example 2:
Input: n = 3, ranges = [0,0,0,0]
Output: -1
Explanation: Even if you activate all the four taps you cannot water the whole garden.
Example 3:
Input: n = 7, ranges = [1,2,1,0,2,1,0,1]
Output: 3
Example 4:
Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4]
Output: 2
Example 5:
Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4]
Output: 1
Constraints:
1 <= n <= 10^4
ranges.length == n + 1
0 <= ranges[i] <= 100
|
class Solution:
def minTaps(self, n: int, ranges: List[int]) -> int:
for i, val in enumerate(ranges):
if i - val < 0:
ranges[0] = max(i + val, ranges[0])
else:
ranges[i - val] = max(val * 2, ranges[i - val])
i = 0
prev = -1
counter = 0
while i != prev:
distance = ranges[i]
if i + distance >= n:
return counter + 1
maxIndex = i
for j in range(i + distance + 1):
if j + ranges[j] >= maxIndex + ranges[maxIndex]:
maxIndex = j
j += 1
prev = i
i = maxIndex
counter += 1
return -1
|
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n).
There are n + 1 taps located at points [0, 1, ..., n] in the garden.
Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open.
Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.
Example 1:
Input: n = 5, ranges = [3,4,1,1,0,0]
Output: 1
Explanation: The tap at point 0 can cover the interval [-3,3]
The tap at point 1 can cover the interval [-3,5]
The tap at point 2 can cover the interval [1,3]
The tap at point 3 can cover the interval [2,4]
The tap at point 4 can cover the interval [4,4]
The tap at point 5 can cover the interval [5,5]
Opening Only the second tap will water the whole garden [0,5]
Example 2:
Input: n = 3, ranges = [0,0,0,0]
Output: -1
Explanation: Even if you activate all the four taps you cannot water the whole garden.
Example 3:
Input: n = 7, ranges = [1,2,1,0,2,1,0,1]
Output: 3
Example 4:
Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4]
Output: 2
Example 5:
Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4]
Output: 1
Constraints:
1 <= n <= 10^4
ranges.length == n + 1
0 <= ranges[i] <= 100
|
class Solution:
def minTaps(self, n: int, ranges: List[int]) -> int:
arr=[float('inf') for _ in range(n+1)]
arr[0]=0
i=0
while i<len(arr):
left=max(0,i-ranges[i])
right=min(len(arr)-1,i+ranges[i])
j=left+1
while j<=right:
arr[j]=min(arr[j],1+arr[left])
j+=1
i+=1
if arr[-1]==float('inf'):
return -1
return arr[-1]
|
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n).
There are n + 1 taps located at points [0, 1, ..., n] in the garden.
Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open.
Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.
Example 1:
Input: n = 5, ranges = [3,4,1,1,0,0]
Output: 1
Explanation: The tap at point 0 can cover the interval [-3,3]
The tap at point 1 can cover the interval [-3,5]
The tap at point 2 can cover the interval [1,3]
The tap at point 3 can cover the interval [2,4]
The tap at point 4 can cover the interval [4,4]
The tap at point 5 can cover the interval [5,5]
Opening Only the second tap will water the whole garden [0,5]
Example 2:
Input: n = 3, ranges = [0,0,0,0]
Output: -1
Explanation: Even if you activate all the four taps you cannot water the whole garden.
Example 3:
Input: n = 7, ranges = [1,2,1,0,2,1,0,1]
Output: 3
Example 4:
Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4]
Output: 2
Example 5:
Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4]
Output: 1
Constraints:
1 <= n <= 10^4
ranges.length == n + 1
0 <= ranges[i] <= 100
|
class Solution:
def minTaps(self, n: int, ranges: List[int]) -> int:
dp = [n for _ in range(n+1)]
for index,ranged in enumerate(ranges):
start = max(0,index - ranged)
end = min(n,index + ranged)
if start == 0:
extra = 0
else:
extra = dp[start]
for i in range(start+1,end+1):
dp[i]= min(dp[i],1 + extra)
if dp[-1]!=n:
return dp[-1]
else:
return -1
|
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n).
There are n + 1 taps located at points [0, 1, ..., n] in the garden.
Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open.
Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.
Example 1:
Input: n = 5, ranges = [3,4,1,1,0,0]
Output: 1
Explanation: The tap at point 0 can cover the interval [-3,3]
The tap at point 1 can cover the interval [-3,5]
The tap at point 2 can cover the interval [1,3]
The tap at point 3 can cover the interval [2,4]
The tap at point 4 can cover the interval [4,4]
The tap at point 5 can cover the interval [5,5]
Opening Only the second tap will water the whole garden [0,5]
Example 2:
Input: n = 3, ranges = [0,0,0,0]
Output: -1
Explanation: Even if you activate all the four taps you cannot water the whole garden.
Example 3:
Input: n = 7, ranges = [1,2,1,0,2,1,0,1]
Output: 3
Example 4:
Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4]
Output: 2
Example 5:
Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4]
Output: 1
Constraints:
1 <= n <= 10^4
ranges.length == n + 1
0 <= ranges[i] <= 100
|
class Solution:
def minTaps(self, n: int, ranges: List[int]) -> int:
intervals=[]
for i,r in enumerate(ranges):
intervals.append([i-r,i+r])
intervals.sort(key=lambda x:[x[0],-x[1]])
#print(intervals)
count=0
def getnext(l):
rel=[]
for i in intervals:
if l>=i[0] and l<i[1]:
rel.append(i)
rel.sort(key=lambda x:x[1],reverse=True)
return rel[0] if rel else None
j=0
while j<n:
iv=getnext(j)
#print(count,j,iv)
if iv:
j=iv[1]
count+=1
else:
return -1
return count
|
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n).
There are n + 1 taps located at points [0, 1, ..., n] in the garden.
Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open.
Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.
Example 1:
Input: n = 5, ranges = [3,4,1,1,0,0]
Output: 1
Explanation: The tap at point 0 can cover the interval [-3,3]
The tap at point 1 can cover the interval [-3,5]
The tap at point 2 can cover the interval [1,3]
The tap at point 3 can cover the interval [2,4]
The tap at point 4 can cover the interval [4,4]
The tap at point 5 can cover the interval [5,5]
Opening Only the second tap will water the whole garden [0,5]
Example 2:
Input: n = 3, ranges = [0,0,0,0]
Output: -1
Explanation: Even if you activate all the four taps you cannot water the whole garden.
Example 3:
Input: n = 7, ranges = [1,2,1,0,2,1,0,1]
Output: 3
Example 4:
Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4]
Output: 2
Example 5:
Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4]
Output: 1
Constraints:
1 <= n <= 10^4
ranges.length == n + 1
0 <= ranges[i] <= 100
|
class Solution:
def minTaps(self, n: int, ranges: List[int]) -> int:
intervals = []
for i, x in enumerate(ranges):
intervals.append([i - x, i + x])
# Always pick the first
def mySortKey(x):
# print(x)
left = x[0]
if left <= 0:
left = 0
right = x[1]
return left * 100 - right
intervals.sort(key=mySortKey)
print(intervals)
greedyFurthest = intervals[0][1]
nextGreedyFurthest = 0
j = 0
count = 1
while j < len(intervals):
connect = False
if greedyFurthest >= n:
return count
while j < len(intervals) and intervals[j][0] <= greedyFurthest:
if intervals[j][1] > nextGreedyFurthest:
nextGreedyFurthest = intervals[j][1]
j += 1
connect = True
greedyFurthest = nextGreedyFurthest
count += 1
if not connect:
return -1
if greedyFurthest >= n:
return count
else:
return -1
|
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n).
There are n + 1 taps located at points [0, 1, ..., n] in the garden.
Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open.
Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.
Example 1:
Input: n = 5, ranges = [3,4,1,1,0,0]
Output: 1
Explanation: The tap at point 0 can cover the interval [-3,3]
The tap at point 1 can cover the interval [-3,5]
The tap at point 2 can cover the interval [1,3]
The tap at point 3 can cover the interval [2,4]
The tap at point 4 can cover the interval [4,4]
The tap at point 5 can cover the interval [5,5]
Opening Only the second tap will water the whole garden [0,5]
Example 2:
Input: n = 3, ranges = [0,0,0,0]
Output: -1
Explanation: Even if you activate all the four taps you cannot water the whole garden.
Example 3:
Input: n = 7, ranges = [1,2,1,0,2,1,0,1]
Output: 3
Example 4:
Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4]
Output: 2
Example 5:
Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4]
Output: 1
Constraints:
1 <= n <= 10^4
ranges.length == n + 1
0 <= ranges[i] <= 100
|
class Solution:
def minTaps(self, n: int, ranges: List[int]) -> int:
coords = []
for i, rng in enumerate(ranges):
if not rng:
continue
coords.append([max(0, i - rng), min(i + rng, n)])
coords.sort()
dp = [float('inf')] * (n + 1)
dp[0] = 0
for [start, end] in coords:
if start > n:
break
prev = dp[start]
for i in range(start, min(end, n)):
dp[i + 1] = min(prev + 1, dp[i + 1])
return dp[-1] if dp[-1] < float('inf') else -1
|
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n).
There are n + 1 taps located at points [0, 1, ..., n] in the garden.
Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open.
Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.
Example 1:
Input: n = 5, ranges = [3,4,1,1,0,0]
Output: 1
Explanation: The tap at point 0 can cover the interval [-3,3]
The tap at point 1 can cover the interval [-3,5]
The tap at point 2 can cover the interval [1,3]
The tap at point 3 can cover the interval [2,4]
The tap at point 4 can cover the interval [4,4]
The tap at point 5 can cover the interval [5,5]
Opening Only the second tap will water the whole garden [0,5]
Example 2:
Input: n = 3, ranges = [0,0,0,0]
Output: -1
Explanation: Even if you activate all the four taps you cannot water the whole garden.
Example 3:
Input: n = 7, ranges = [1,2,1,0,2,1,0,1]
Output: 3
Example 4:
Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4]
Output: 2
Example 5:
Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4]
Output: 1
Constraints:
1 <= n <= 10^4
ranges.length == n + 1
0 <= ranges[i] <= 100
|
class Solution:
def minTaps(self, n: int, ranges: List[int]) -> int:
dp = [float('inf')]*(n+1)
dp[0] = 0
for i in range(n+1):
L = max(0,i-ranges[i])
R = min(n,i+ranges[i])
for j in range(L+1,R+1):
dp[j] = min(dp[j], dp[L]+1)
if dp[n]>=n+2:
return -1
return dp[-1]
|
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n).
There are n + 1 taps located at points [0, 1, ..., n] in the garden.
Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open.
Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.
Example 1:
Input: n = 5, ranges = [3,4,1,1,0,0]
Output: 1
Explanation: The tap at point 0 can cover the interval [-3,3]
The tap at point 1 can cover the interval [-3,5]
The tap at point 2 can cover the interval [1,3]
The tap at point 3 can cover the interval [2,4]
The tap at point 4 can cover the interval [4,4]
The tap at point 5 can cover the interval [5,5]
Opening Only the second tap will water the whole garden [0,5]
Example 2:
Input: n = 3, ranges = [0,0,0,0]
Output: -1
Explanation: Even if you activate all the four taps you cannot water the whole garden.
Example 3:
Input: n = 7, ranges = [1,2,1,0,2,1,0,1]
Output: 3
Example 4:
Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4]
Output: 2
Example 5:
Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4]
Output: 1
Constraints:
1 <= n <= 10^4
ranges.length == n + 1
0 <= ranges[i] <= 100
|
class Solution:
def minTaps(self, n: int, ranges: List[int]) -> int:
areas = [(x - ranges[x], x + ranges[x]) for x in range(n+1)]
areas = sorted(areas, key= lambda x: x[0])
def get_intersections(current, intervals):
results = []
for interval in intervals:
if current[0] <= interval[0] <= current[1] and interval[1] > current[1]:
results.append(interval)
return results
first = areas[0]
for intersection in areas:
if intersection[0] <= 0 and intersection[1] > first[1]:
first = intersection
selected = [first]
area_to_cover = (first[1], n)
while area_to_cover[0] < area_to_cover[1]:
prev = selected[-1]
intersections = get_intersections(prev, areas)
if len(intersections) is 0:
return -1
intersections = sorted(intersections, key=lambda x: x[1])
rightmost = intersections[-1]
if rightmost[1] < area_to_cover[0]:
return -1
selected.append(rightmost)
area_to_cover = (rightmost[1], n)
return len(selected)
|
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n).
There are n + 1 taps located at points [0, 1, ..., n] in the garden.
Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open.
Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.
Example 1:
Input: n = 5, ranges = [3,4,1,1,0,0]
Output: 1
Explanation: The tap at point 0 can cover the interval [-3,3]
The tap at point 1 can cover the interval [-3,5]
The tap at point 2 can cover the interval [1,3]
The tap at point 3 can cover the interval [2,4]
The tap at point 4 can cover the interval [4,4]
The tap at point 5 can cover the interval [5,5]
Opening Only the second tap will water the whole garden [0,5]
Example 2:
Input: n = 3, ranges = [0,0,0,0]
Output: -1
Explanation: Even if you activate all the four taps you cannot water the whole garden.
Example 3:
Input: n = 7, ranges = [1,2,1,0,2,1,0,1]
Output: 3
Example 4:
Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4]
Output: 2
Example 5:
Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4]
Output: 1
Constraints:
1 <= n <= 10^4
ranges.length == n + 1
0 <= ranges[i] <= 100
|
class Solution:
def minTaps(self, n: int, ranges: List[int]) -> int:
dp = [n+2]*(n+1)
dp[0] = 0
for i in range(n+1):
start = max(0,i-ranges[i])
end = min(n,i+ranges[i])
for j in range(start,end+1):
dp[j] = min(dp[j],dp[start]+1)
if dp[n]==n+2:
return -1
return dp[n]
|
You have n super washing machines on a line. Initially, each washing machine has some dresses or is empty.
For each move, you could choose any m (1 ≤ m ≤ n) washing machines, and pass one dress of each washing machine to one of its adjacent washing machines at the same time .
Given an integer array representing the number of dresses in each washing machine from left to right on the line, you should find the minimum number of moves to make all the washing machines have the same number of dresses. If it is not possible to do it, return -1.
Example1
Input: [1,0,5]
Output: 3
Explanation:
1st move: 1 0 1 1 4
2nd move: 1 2 1 3
3rd move: 2 1 2 2 2
Example2
Input: [0,3,0]
Output: 2
Explanation:
1st move: 0 1 2 0
2nd move: 1 2 --> 0 => 1 1 1
Example3
Input: [0,2,0]
Output: -1
Explanation:
It's impossible to make all the three washing machines have the same number of dresses.
Note:
The range of n is [1, 10000].
The range of dresses number in a super washing machine is [0, 1e5].
|
class Solution:
def findMinMoves(self, machines):
"""
:type machines: List[int]
:rtype: int
"""
if sum(machines) % len(machines) != 0:
return -1
mean = sum(machines) // len(machines)
cum, step = 0, 0
for x in machines:
cum += x - mean
step = max(step, abs(cum), x-mean)
return step
|
You have n super washing machines on a line. Initially, each washing machine has some dresses or is empty.
For each move, you could choose any m (1 ≤ m ≤ n) washing machines, and pass one dress of each washing machine to one of its adjacent washing machines at the same time .
Given an integer array representing the number of dresses in each washing machine from left to right on the line, you should find the minimum number of moves to make all the washing machines have the same number of dresses. If it is not possible to do it, return -1.
Example1
Input: [1,0,5]
Output: 3
Explanation:
1st move: 1 0 1 1 4
2nd move: 1 2 1 3
3rd move: 2 1 2 2 2
Example2
Input: [0,3,0]
Output: 2
Explanation:
1st move: 0 1 2 0
2nd move: 1 2 --> 0 => 1 1 1
Example3
Input: [0,2,0]
Output: -1
Explanation:
It's impossible to make all the three washing machines have the same number of dresses.
Note:
The range of n is [1, 10000].
The range of dresses number in a super washing machine is [0, 1e5].
|
class Solution:
def findMinMoves(self, machines):
"""
:type machines: List[int]
:rtype: int
"""
total = sum(machines)
length = len(machines)
if total%length!=0:
return -1
avg = int(total/length)
cum = 0
target = 0
result = 0
for m in machines:
cum+=m
target+=avg
max_share = m-avg # example [0,3,0] 3 must output 1 to one side at a time, so it needs 2 steps: one for each side.
result = max(result, abs(target-cum), max_share)
# When cum-target<0, it means right side must transfer abs(cum-target) to the left side
# example: [0, 0, 11, 5], avg=4, at the second 0, cum=0, target=2*avg=8, we need to transfer 8-0=8 to the left side
# the second 0 is the only interface to the right side, so the right side need 8 steps to fill the left side
# When cum-target>0, it means left side must transfer abs(cum-target) to the right side
# example: [4,3,2,1,0], avg=2, at 3, cum=7, target=2*avg=4, we need to transfer 7-4=3 to the right side
# 3 is the only interface to the right side, so the left side need 3 steps to fill the right side
return result;
|
You have n super washing machines on a line. Initially, each washing machine has some dresses or is empty.
For each move, you could choose any m (1 ≤ m ≤ n) washing machines, and pass one dress of each washing machine to one of its adjacent washing machines at the same time .
Given an integer array representing the number of dresses in each washing machine from left to right on the line, you should find the minimum number of moves to make all the washing machines have the same number of dresses. If it is not possible to do it, return -1.
Example1
Input: [1,0,5]
Output: 3
Explanation:
1st move: 1 0 1 1 4
2nd move: 1 2 1 3
3rd move: 2 1 2 2 2
Example2
Input: [0,3,0]
Output: 2
Explanation:
1st move: 0 1 2 0
2nd move: 1 2 --> 0 => 1 1 1
Example3
Input: [0,2,0]
Output: -1
Explanation:
It's impossible to make all the three washing machines have the same number of dresses.
Note:
The range of n is [1, 10000].
The range of dresses number in a super washing machine is [0, 1e5].
|
class Solution:
def findMinMoves(self, machines):
"""
:type machines: List[int]
:rtype: int
"""
N = len(machines)
if sum(machines)%N!=0: return -1
avg = sum(machines)//N
result = 0
toNext = 0
for i in range(N):
fromPrev = toNext
toNext = fromPrev+machines[i]-avg
if fromPrev<0 and toNext>0:
result = max(result,-fromPrev+toNext)
else:
result = max(result,abs(fromPrev),abs(toNext))
return result
|
You have n super washing machines on a line. Initially, each washing machine has some dresses or is empty.
For each move, you could choose any m (1 ≤ m ≤ n) washing machines, and pass one dress of each washing machine to one of its adjacent washing machines at the same time .
Given an integer array representing the number of dresses in each washing machine from left to right on the line, you should find the minimum number of moves to make all the washing machines have the same number of dresses. If it is not possible to do it, return -1.
Example1
Input: [1,0,5]
Output: 3
Explanation:
1st move: 1 0 1 1 4
2nd move: 1 2 1 3
3rd move: 2 1 2 2 2
Example2
Input: [0,3,0]
Output: 2
Explanation:
1st move: 0 1 2 0
2nd move: 1 2 --> 0 => 1 1 1
Example3
Input: [0,2,0]
Output: -1
Explanation:
It's impossible to make all the three washing machines have the same number of dresses.
Note:
The range of n is [1, 10000].
The range of dresses number in a super washing machine is [0, 1e5].
|
class Solution:
def findMinMoves(self, machines):
"""
:type machines: List[int]
:rtype: int
"""
n = len(machines)
if sum(machines) % n != 0:
return -1
s = sum(machines)
k = s//n
presum = 0
right = 0
max_net = 0
for i in range(n):
presum += machines[i]
left = -right
right = presum - k*(i+1)
net = left + right
max_net = max(max_net, abs(left), abs(right), net)
return max_net
|
You have n super washing machines on a line. Initially, each washing machine has some dresses or is empty.
For each move, you could choose any m (1 ≤ m ≤ n) washing machines, and pass one dress of each washing machine to one of its adjacent washing machines at the same time .
Given an integer array representing the number of dresses in each washing machine from left to right on the line, you should find the minimum number of moves to make all the washing machines have the same number of dresses. If it is not possible to do it, return -1.
Example1
Input: [1,0,5]
Output: 3
Explanation:
1st move: 1 0 1 1 4
2nd move: 1 2 1 3
3rd move: 2 1 2 2 2
Example2
Input: [0,3,0]
Output: 2
Explanation:
1st move: 0 1 2 0
2nd move: 1 2 --> 0 => 1 1 1
Example3
Input: [0,2,0]
Output: -1
Explanation:
It's impossible to make all the three washing machines have the same number of dresses.
Note:
The range of n is [1, 10000].
The range of dresses number in a super washing machine is [0, 1e5].
|
class Solution:
def findMinMoves(self, machines):
"""
:type machines: List[int]
:rtype: int
"""
sum_before = [0]
i = 0
for m in machines:
sum_before.append(m + sum_before[i])
i+=1
if sum_before[len(machines)]%len(machines)!=0:
return -1
average = int(sum_before[len(machines)]/len(machines))
result = 0
i = 0
for m in machines:
left_require = average * i - sum_before[i]
right_require = average * (len(machines)-i-1) - (sum_before[len(machines)] - sum_before[i+1])
if left_require > 0 and right_require > 0:
max_tran = left_require + right_require
else:
max_tran = max(abs(left_require), abs(right_require))
result = max(result, max_tran)
i+=1
return result
|
You have n super washing machines on a line. Initially, each washing machine has some dresses or is empty.
For each move, you could choose any m (1 ≤ m ≤ n) washing machines, and pass one dress of each washing machine to one of its adjacent washing machines at the same time .
Given an integer array representing the number of dresses in each washing machine from left to right on the line, you should find the minimum number of moves to make all the washing machines have the same number of dresses. If it is not possible to do it, return -1.
Example1
Input: [1,0,5]
Output: 3
Explanation:
1st move: 1 0 1 1 4
2nd move: 1 2 1 3
3rd move: 2 1 2 2 2
Example2
Input: [0,3,0]
Output: 2
Explanation:
1st move: 0 1 2 0
2nd move: 1 2 --> 0 => 1 1 1
Example3
Input: [0,2,0]
Output: -1
Explanation:
It's impossible to make all the three washing machines have the same number of dresses.
Note:
The range of n is [1, 10000].
The range of dresses number in a super washing machine is [0, 1e5].
|
class Solution:
def findMinMoves(self, machines):
"""
:type machines: List[int]
:rtype: int
"""
total = sum(machines)
length = len(machines)
if total%length!=0:
return -1
avg = int(total/length)
cnt = 0
result = 0
for m in machines:
cnt += m-avg; #load-avg is "gain/lose"
result = max(result, abs(cnt), m-avg)
return result;
|
You have n super washing machines on a line. Initially, each washing machine has some dresses or is empty.
For each move, you could choose any m (1 ≤ m ≤ n) washing machines, and pass one dress of each washing machine to one of its adjacent washing machines at the same time .
Given an integer array representing the number of dresses in each washing machine from left to right on the line, you should find the minimum number of moves to make all the washing machines have the same number of dresses. If it is not possible to do it, return -1.
Example1
Input: [1,0,5]
Output: 3
Explanation:
1st move: 1 0 1 1 4
2nd move: 1 2 1 3
3rd move: 2 1 2 2 2
Example2
Input: [0,3,0]
Output: 2
Explanation:
1st move: 0 1 2 0
2nd move: 1 2 --> 0 => 1 1 1
Example3
Input: [0,2,0]
Output: -1
Explanation:
It's impossible to make all the three washing machines have the same number of dresses.
Note:
The range of n is [1, 10000].
The range of dresses number in a super washing machine is [0, 1e5].
|
class Solution:
def findMinMoves(self,machines):
s = sum(machines)
l = len(machines)
if s%l: return -1
a, c, ans = int(s/l), 0, 0
for x in machines:
y = x - a
c += y
ans = max(ans, y, abs(c))
return ans
"""
:type machines: List[int]
:rtype: int
"""
|
You have n super washing machines on a line. Initially, each washing machine has some dresses or is empty.
For each move, you could choose any m (1 ≤ m ≤ n) washing machines, and pass one dress of each washing machine to one of its adjacent washing machines at the same time .
Given an integer array representing the number of dresses in each washing machine from left to right on the line, you should find the minimum number of moves to make all the washing machines have the same number of dresses. If it is not possible to do it, return -1.
Example1
Input: [1,0,5]
Output: 3
Explanation:
1st move: 1 0 1 1 4
2nd move: 1 2 1 3
3rd move: 2 1 2 2 2
Example2
Input: [0,3,0]
Output: 2
Explanation:
1st move: 0 1 2 0
2nd move: 1 2 --> 0 => 1 1 1
Example3
Input: [0,2,0]
Output: -1
Explanation:
It's impossible to make all the three washing machines have the same number of dresses.
Note:
The range of n is [1, 10000].
The range of dresses number in a super washing machine is [0, 1e5].
|
class Solution:
def __init__(self):
pass
def findMinMoves(self, machines):
"""
:type machines: List[int]
:rtype: int
"""
N = len(machines)
if N == 0:
return 0
val_sum = sum(machines)
if val_sum%N != 0:
return -1
val_each = val_sum//N
go_left, go_right, net_change, max_step = 0, 0, 0, 0
for i in range(N):
go_left = -go_right
go_right = go_right + machines[i] - val_each
net_change = go_left + go_right
step = max(abs(go_left), abs(go_right), net_change)
max_step = max(max_step, step)
return max_step
|
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n.
Example:
Given n = 2, return 91. (The answer should be the total numbers in the range of 0 ≤ x < 100, excluding [11,22,33,44,55,66,77,88,99])
Credits:Special thanks to @memoryless for adding this problem and creating all test cases.
|
class Solution:
def countNumbersWithUniqueDigits(self, n):
"""
:type n: int
:rtype: int
"""
ls = [1,10,91]
mul = 9
res = 0
for i in range(8):
mul = 9
m = 9
for j in range(i+2):
mul *= m
m -= 1
#print(mul)
ls.append(mul +ls[-1])
if n >=9:
return ls[9]
else:
return ls[n]
|
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n.
Example:
Given n = 2, return 91. (The answer should be the total numbers in the range of 0 ≤ x < 100, excluding [11,22,33,44,55,66,77,88,99])
Credits:Special thanks to @memoryless for adding this problem and creating all test cases.
|
class Solution:
def countNumbersWithUniqueDigits(self, n):
"""
:type n: int
:rtype: int
"""
answer = 10 ** n
for i in range(1, n + 1):
all_count = 9 * 10 ** (i - 1)
invalid = 9
for j in range(1, i):
invalid *= (10 - j)
answer -= (all_count - invalid)
return answer
|
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n.
Example:
Given n = 2, return 91. (The answer should be the total numbers in the range of 0 ≤ x < 100, excluding [11,22,33,44,55,66,77,88,99])
Credits:Special thanks to @memoryless for adding this problem and creating all test cases.
|
class Solution:
def countNumbersWithUniqueDigits(self, n):
"""
:type n: int
:rtype: int
"""
choices = [9, 9, 8, 7, 6, 5, 4, 3, 2, 1]
ans, product = 1, 1
for i in range(n if n <= 10 else 10):
product *= choices[i]
ans += product
return ans
|
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n.
Example:
Given n = 2, return 91. (The answer should be the total numbers in the range of 0 ≤ x < 100, excluding [11,22,33,44,55,66,77,88,99])
Credits:Special thanks to @memoryless for adding this problem and creating all test cases.
|
class Solution:
def countNumbersWithUniqueDigits(self, n):
if n == 0: return 1
if n > 10: return self.countNumbersWithUniqueDigits(10)
count = 0
for i in range(1, n + 1): # number of digits
temp = 1
for j in range(i):
temp *= 10 - j
if temp > 10:
temp = temp // 10 * 9
count += temp
return count
|
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n.
Example:
Given n = 2, return 91. (The answer should be the total numbers in the range of 0 ≤ x < 100, excluding [11,22,33,44,55,66,77,88,99])
Credits:Special thanks to @memoryless for adding this problem and creating all test cases.
|
class Solution:
def countNumbersWithUniqueDigits(self, n):
"""
:type n: int
:rtype: int
"""
digits = [1, 10, 91, 739, 5275, 32491, 168571, 712891, 2345851, 5611771, 8877691]
if n < len(digits):
return digits[n]
return digits[-1]
|
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n.
Example:
Given n = 2, return 91. (The answer should be the total numbers in the range of 0 ≤ x < 100, excluding [11,22,33,44,55,66,77,88,99])
Credits:Special thanks to @memoryless for adding this problem and creating all test cases.
|
class Solution:
def countNumbersWithUniqueDigits(self, n):
"""
:type n: int
:rtype: int
"""
if n == 0:
return 1
ans = 10
d = 9
prod = 9
for i in range(2, n+1):
prod *= d
d -= 1
ans += prod
return ans
|
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n.
Example:
Given n = 2, return 91. (The answer should be the total numbers in the range of 0 ≤ x < 100, excluding [11,22,33,44,55,66,77,88,99])
Credits:Special thanks to @memoryless for adding this problem and creating all test cases.
|
class Solution:
def countNumbersWithUniqueDigits(self, n):
"""
:type n: int
:rtype: int
"""
if n == 0:
return 1
digits = [9,9,8,7,6,5,4,3,2,1]
ans = 0
while n > 0:
if n == 1:
ans += 10
else:
res = 1
for i in range(n):
res *= digits[i]
ans += res
n -= 1
return ans
|
Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10n.
Example:
Given n = 2, return 91. (The answer should be the total numbers in the range of 0 ≤ x < 100, excluding [11,22,33,44,55,66,77,88,99])
Credits:Special thanks to @memoryless for adding this problem and creating all test cases.
|
class Solution:
def countNumbersWithUniqueDigits(self, n):
"""
:type n: int
:rtype: int
"""
if n ==0:
return 1
elif n == 1:
return 10
else:
return (self.countNumbersWithUniqueDigits(n-1) - self.countNumbersWithUniqueDigits(n-2))*(11-n) + self.countNumbersWithUniqueDigits(n-1)
|
Given an array of non-negative integers, you are initially positioned at the first index of the array.
Each element in the array represents your maximum jump length at that position.
Your goal is to reach the last index in the minimum number of jumps.
Example:
Input: [2,3,1,1,4]
Output: 2
Explanation: The minimum number of jumps to reach the last index is 2.
Jump 1 step from index 0 to 1, then 3 steps to the last index.
Note:
You can assume that you can always reach the last index.
|
class Solution:
def jump(self,nums):
"""
:type nums: List[int]
:rtype: int
"""
if len(nums) == 1:
return 0
else:
step = 0
pos = 0
while pos != len(nums) - 1:
bestStep = -1
bestValue = -1
for i in range(nums[pos], 0, -1):
if len(nums) - 1 == pos + i:
bestStep = i
break
if (pos + i < len(nums) and nums[pos + i] != 0 and nums[pos + i] + i > bestValue):
bestStep = i
bestValue = nums[pos + i] + i
print(bestStep)
pos += bestStep
step += 1
return step
|
Given an array of non-negative integers, you are initially positioned at the first index of the array.
Each element in the array represents your maximum jump length at that position.
Your goal is to reach the last index in the minimum number of jumps.
Example:
Input: [2,3,1,1,4]
Output: 2
Explanation: The minimum number of jumps to reach the last index is 2.
Jump 1 step from index 0 to 1, then 3 steps to the last index.
Note:
You can assume that you can always reach the last index.
|
class Solution:
def jump(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
res = 0
n = len(nums)
i, j = 0, 0
while j < n - 1:
print((i, j))
ii, jj = i, j
for k in range(ii, jj + 1):
j = max(j, min(n - 1, k + nums[k]))
i = jj + 1
res += 1
return res
|
Given an array of non-negative integers, you are initially positioned at the first index of the array.
Each element in the array represents your maximum jump length at that position.
Your goal is to reach the last index in the minimum number of jumps.
Example:
Input: [2,3,1,1,4]
Output: 2
Explanation: The minimum number of jumps to reach the last index is 2.
Jump 1 step from index 0 to 1, then 3 steps to the last index.
Note:
You can assume that you can always reach the last index.
|
class Solution:
def jump(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if not nums: return 0
if len(nums) == 1: return 0
step, far, maxV = 0, 0, 0
for i in range(len(nums)):
if i + nums[i] > maxV and i + nums[i] >= len(nums) - 1: return step + 1
maxV = max(maxV, i + nums[i])
if i == far:
step += 1
far = maxV
return step
|
Given an array of non-negative integers, you are initially positioned at the first index of the array.
Each element in the array represents your maximum jump length at that position.
Your goal is to reach the last index in the minimum number of jumps.
Example:
Input: [2,3,1,1,4]
Output: 2
Explanation: The minimum number of jumps to reach the last index is 2.
Jump 1 step from index 0 to 1, then 3 steps to the last index.
Note:
You can assume that you can always reach the last index.
|
class Solution:
def jump(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
p = [0]
for i in range(len(nums) - 1):
while(i + nums[i] >= len(p) and len(p) < len(nums)):
p.append(p[i] + 1)
return p[-1]
|
Given an array of non-negative integers, you are initially positioned at the first index of the array.
Each element in the array represents your maximum jump length at that position.
Your goal is to reach the last index in the minimum number of jumps.
Example:
Input: [2,3,1,1,4]
Output: 2
Explanation: The minimum number of jumps to reach the last index is 2.
Jump 1 step from index 0 to 1, then 3 steps to the last index.
Note:
You can assume that you can always reach the last index.
|
class Solution:
def jump(self, nums):
"""
:type nums: List[int]
:rtype: int
if len(nums)<=1:
return 0
dist=1
explored ={}
Q =[]
explored[0]=dist
Q.append(0)
levelup = 0
while Q[-1]+1 < len(nums):
print(Q)
v = Q[0]
Q.pop(0)
before=len(explored)
for i in range(nums[v]):
if not (v+i+1) in explored:
explored[v+i+1] = dist
Q.append(v+i+1)
if v== levelup:
dist+=1
levelup=Q[-1]
print(Q)
return explored[len(nums)-1]
"""
cur = 0
ans = 0
maxstep =0
for i in range (0,len(nums)-1):
maxstep = max(maxstep,i+nums[i])
if i ==cur:
ans +=1
cur = maxstep
return ans
|
Given an array of non-negative integers, you are initially positioned at the first index of the array.
Each element in the array represents your maximum jump length at that position.
Your goal is to reach the last index in the minimum number of jumps.
Example:
Input: [2,3,1,1,4]
Output: 2
Explanation: The minimum number of jumps to reach the last index is 2.
Jump 1 step from index 0 to 1, then 3 steps to the last index.
Note:
You can assume that you can always reach the last index.
|
class Solution:
def jump(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if len(nums) == 0 or len(nums) == 1:
return 0;
s = 0; e = 0; step = 0
while (e < len(nums) - 1):
fst = e
step += 1
for i in range(s, e + 1):
if fst < nums[i] + i:
fst = nums[i] + i
s = e + 1
e = fst
return step
|
Given an array of non-negative integers, you are initially positioned at the first index of the array.
Each element in the array represents your maximum jump length at that position.
Your goal is to reach the last index in the minimum number of jumps.
Example:
Input: [2,3,1,1,4]
Output: 2
Explanation: The minimum number of jumps to reach the last index is 2.
Jump 1 step from index 0 to 1, then 3 steps to the last index.
Note:
You can assume that you can always reach the last index.
|
class Solution:
def jump(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
current_step = 0
last_step = 0
i = 0
n_jumps = 0
while last_step < len(nums)-1:
while i <= last_step:
current_step = max(i+nums[i],current_step)
i += 1
last_step = current_step
n_jumps += 1
return n_jumps
|
Given an array of non-negative integers, you are initially positioned at the first index of the array.
Each element in the array represents your maximum jump length at that position.
Your goal is to reach the last index in the minimum number of jumps.
Example:
Input: [2,3,1,1,4]
Output: 2
Explanation: The minimum number of jumps to reach the last index is 2.
Jump 1 step from index 0 to 1, then 3 steps to the last index.
Note:
You can assume that you can always reach the last index.
|
class Solution:
def jump(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
n, start, end, step = len(nums), 0, 0, 0
while end < n - 1:
step += 1
maxend = end + 1
for i in range(start, end + 1):
if i + nums[i] >= n - 1:
return step
maxend = max(maxend, i + nums[i])
start, end = end + 1, maxend
return step
|
Given an array of non-negative integers, you are initially positioned at the first index of the array.
Each element in the array represents your maximum jump length at that position.
Your goal is to reach the last index in the minimum number of jumps.
Example:
Input: [2,3,1,1,4]
Output: 2
Explanation: The minimum number of jumps to reach the last index is 2.
Jump 1 step from index 0 to 1, then 3 steps to the last index.
Note:
You can assume that you can always reach the last index.
|
class Solution:
def jump(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if nums is None or len(nums) <= 1:
return 0
start = currEnd = nextEnd = jump = 0
while currEnd - start + 1 > 0:
jump += 1
for i in range(start, currEnd+1):
nextEnd = max(nums[i] + i, nextEnd)
if nextEnd >= len(nums) - 1:
return jump
start = currEnd + 1
currEnd = nextEnd
return jump
|
Given an array of non-negative integers, you are initially positioned at the first index of the array.
Each element in the array represents your maximum jump length at that position.
Your goal is to reach the last index in the minimum number of jumps.
Example:
Input: [2,3,1,1,4]
Output: 2
Explanation: The minimum number of jumps to reach the last index is 2.
Jump 1 step from index 0 to 1, then 3 steps to the last index.
Note:
You can assume that you can always reach the last index.
|
class Solution:
def jump(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if len(nums) <= 2:
return len(nums)-1
stack = []
i = len(nums) - 2
while i >= 0:
# print(nums,stack)
if nums[i] == 1:
stack.append(i)
elif nums[i] >= (len(nums)-1-i):
if stack:
stack.clear()
stack.append(i)
else:
j = 0
while stack and j < len(stack):
if nums[i] >= (stack[j]-i):
stack = stack[0:j+1]
stack.append(i)
break
else:
j += 1
i -= 1
# print(stack)
return len(stack)
|
Given an array of non-negative integers, you are initially positioned at the first index of the array.
Each element in the array represents your maximum jump length at that position.
Your goal is to reach the last index in the minimum number of jumps.
Example:
Input: [2,3,1,1,4]
Output: 2
Explanation: The minimum number of jumps to reach the last index is 2.
Jump 1 step from index 0 to 1, then 3 steps to the last index.
Note:
You can assume that you can always reach the last index.
|
class Solution:
def jump(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
L = len(nums)
lo = 0
hi = 0
jumps = 0
while hi < L-1:
jumps += 1
nexthi = hi
for i in range(lo, hi+1):
nexthi = max(nexthi, i + nums[i])
if nexthi >= L-1:
return jumps
lo, hi = hi + 1, nexthi
return jumps
|
Given an array of non-negative integers, you are initially positioned at the first index of the array.
Each element in the array represents your maximum jump length at that position.
Your goal is to reach the last index in the minimum number of jumps.
Example:
Input: [2,3,1,1,4]
Output: 2
Explanation: The minimum number of jumps to reach the last index is 2.
Jump 1 step from index 0 to 1, then 3 steps to the last index.
Note:
You can assume that you can always reach the last index.
|
class Solution:
def jump(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if not nums:
return 0
dic = {}
length = len(nums)
dic[length - 1] = 0
for j in range(length - 1)[::-1]:
#print(length - j + 1)
if(nums[j] >= nums[j + 1] + 1 and not j + 1 == length - 1):
minstep = dic[j + 1] - 1
for k in range(max(3, nums[j+1] + 2) ,min(length - j , nums[j] + 1)):
#print(j,k)
if dic[j + k] < minstep:
minstep = dic[j + k]
dic[j] = minstep + 1
continue
minstep = dic[j + 1]
for k in range(2,min(length - j , nums[j] + 1)):
#print(j,k)
if dic[j + k] < minstep:
minstep = dic[j + k]
dic[j] = minstep + 1
#print(dic)
return dic[0]
|
Koko loves to eat bananas. There are N piles of bananas, the i-th pile has piles[i] bananas. The guards have gone and will come back in H hours.
Koko can decide her bananas-per-hour eating speed of K. Each hour, she chooses some pile of bananas, and eats K bananas from that pile. If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour.
Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back.
Return the minimum integer K such that she can eat all the bananas within H hours.
Example 1:
Input: piles = [3,6,7,11], H = 8
Output: 4
Example 2:
Input: piles = [30,11,23,4,20], H = 5
Output: 30
Example 3:
Input: piles = [30,11,23,4,20], H = 6
Output: 23
Constraints:
1 <= piles.length <= 10^4
piles.length <= H <= 10^9
1 <= piles[i] <= 10^9
|
class Solution:
def minEatingSpeed(self, piles: List[int], H: int) -> int:
bananas = sum(piles)
K = bananas // H + (bananas % H != 0)
while True:
hours_needed = 0
for pile in piles:
hours_needed += pile // K
if pile % K != 0:
hours_needed += 1
if hours_needed <= H:
return K
K += 1
|
Koko loves to eat bananas. There are N piles of bananas, the i-th pile has piles[i] bananas. The guards have gone and will come back in H hours.
Koko can decide her bananas-per-hour eating speed of K. Each hour, she chooses some pile of bananas, and eats K bananas from that pile. If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour.
Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back.
Return the minimum integer K such that she can eat all the bananas within H hours.
Example 1:
Input: piles = [3,6,7,11], H = 8
Output: 4
Example 2:
Input: piles = [30,11,23,4,20], H = 5
Output: 30
Example 3:
Input: piles = [30,11,23,4,20], H = 6
Output: 23
Constraints:
1 <= piles.length <= 10^4
piles.length <= H <= 10^9
1 <= piles[i] <= 10^9
|
import math
class Solution:
def minEatingSpeed(self, piles: List[int], H: int) -> int:
total_bananas = sum(piles)
left, right = math.ceil(total_bananas / H), math.ceil(total_bananas / (H - len(piles) + 1))
while left < right:
mid = (left + right) // 2
if not sum([math.ceil(pile/mid) for pile in piles]) <= H:
left = mid + 1
else:
right = mid
return left
|
Koko loves to eat bananas. There are N piles of bananas, the i-th pile has piles[i] bananas. The guards have gone and will come back in H hours.
Koko can decide her bananas-per-hour eating speed of K. Each hour, she chooses some pile of bananas, and eats K bananas from that pile. If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour.
Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back.
Return the minimum integer K such that she can eat all the bananas within H hours.
Example 1:
Input: piles = [3,6,7,11], H = 8
Output: 4
Example 2:
Input: piles = [30,11,23,4,20], H = 5
Output: 30
Example 3:
Input: piles = [30,11,23,4,20], H = 6
Output: 23
Constraints:
1 <= piles.length <= 10^4
piles.length <= H <= 10^9
1 <= piles[i] <= 10^9
|
class Solution:
def minEatingSpeed(self, piles: List[int], H: int) -> int:
def time_need(K):
return sum(p//K + (1 if p%K else 0) for p in piles)
low = max(1, sum(piles)//H)
high = max(piles)
while low < high:
if time_need(low)<=H: return low
low += 1
mid = (low+high)//2
if time_need(mid) > H:
low = mid + 1
else:
high = mid
return low
|
Koko loves to eat bananas. There are N piles of bananas, the i-th pile has piles[i] bananas. The guards have gone and will come back in H hours.
Koko can decide her bananas-per-hour eating speed of K. Each hour, she chooses some pile of bananas, and eats K bananas from that pile. If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour.
Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back.
Return the minimum integer K such that she can eat all the bananas within H hours.
Example 1:
Input: piles = [3,6,7,11], H = 8
Output: 4
Example 2:
Input: piles = [30,11,23,4,20], H = 5
Output: 30
Example 3:
Input: piles = [30,11,23,4,20], H = 6
Output: 23
Constraints:
1 <= piles.length <= 10^4
piles.length <= H <= 10^9
1 <= piles[i] <= 10^9
|
import math
class Solution:
def minEatingSpeed(self, piles: List[int], H: int) -> int:
def helper(k):
ret=0
for pile in piles:
ret+=math.ceil(pile/k)
if ret>H:
return False
return True
l,r=1,max(piles)
while l<=r:
mid=l+(r-l)//2
if helper(mid):
r=mid-1
else:
l=mid+1
return l
|
Koko loves to eat bananas. There are N piles of bananas, the i-th pile has piles[i] bananas. The guards have gone and will come back in H hours.
Koko can decide her bananas-per-hour eating speed of K. Each hour, she chooses some pile of bananas, and eats K bananas from that pile. If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour.
Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back.
Return the minimum integer K such that she can eat all the bananas within H hours.
Example 1:
Input: piles = [3,6,7,11], H = 8
Output: 4
Example 2:
Input: piles = [30,11,23,4,20], H = 5
Output: 30
Example 3:
Input: piles = [30,11,23,4,20], H = 6
Output: 23
Constraints:
1 <= piles.length <= 10^4
piles.length <= H <= 10^9
1 <= piles[i] <= 10^9
|
class Solution:
def minEatingSpeed(self, piles: List[int], H: int) -> int:
# if len(piles) == 1:return -(piles[0] // -H)
self.piles = piles
l, r = 1, max(piles)
while l <= r:
K = (l + r)//2
if self.eat_time(K) <= H:
r = K - 1
else:
l = K + 1
return l
def eat_time(self, K):
cnt = 0
for banana in self.piles:
q, mod = divmod(banana, K)
cnt += q
if mod != 0: cnt += 1
# cnt += banana / K
# if banana%K != 0: cnt += 1
return cnt
|
Koko loves to eat bananas. There are N piles of bananas, the i-th pile has piles[i] bananas. The guards have gone and will come back in H hours.
Koko can decide her bananas-per-hour eating speed of K. Each hour, she chooses some pile of bananas, and eats K bananas from that pile. If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour.
Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back.
Return the minimum integer K such that she can eat all the bananas within H hours.
Example 1:
Input: piles = [3,6,7,11], H = 8
Output: 4
Example 2:
Input: piles = [30,11,23,4,20], H = 5
Output: 30
Example 3:
Input: piles = [30,11,23,4,20], H = 6
Output: 23
Constraints:
1 <= piles.length <= 10^4
piles.length <= H <= 10^9
1 <= piles[i] <= 10^9
|
class Solution:
def minEatingSpeed(self, piles: List[int], H: int) -> int:
def enough(n):
hrs = 0
for i in piles:
if n > i:
hrs += 1
elif i % n == 0:
hrs += i // n
else:
hrs += (i // n) + 1
return hrs <= H
l,r = 1,sum(piles)
while l < r:
m = (l + r) // 2
if not enough(m):
l = m + 1
else:
r = m
return l
|
Koko loves to eat bananas. There are N piles of bananas, the i-th pile has piles[i] bananas. The guards have gone and will come back in H hours.
Koko can decide her bananas-per-hour eating speed of K. Each hour, she chooses some pile of bananas, and eats K bananas from that pile. If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour.
Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back.
Return the minimum integer K such that she can eat all the bananas within H hours.
Example 1:
Input: piles = [3,6,7,11], H = 8
Output: 4
Example 2:
Input: piles = [30,11,23,4,20], H = 5
Output: 30
Example 3:
Input: piles = [30,11,23,4,20], H = 6
Output: 23
Constraints:
1 <= piles.length <= 10^4
piles.length <= H <= 10^9
1 <= piles[i] <= 10^9
|
class Solution:
def minEatingSpeed(self, piles: List[int], H: int) -> int:
left, right = 1, max(piles)
while left <= right:
mid = left + (right - left) // 2
if self.condition(piles, H, mid):
right = mid - 1
else:
left = mid + 1
return left
def condition(self, piles, H, k):
hours = 0
for pile in piles:
div, rem = divmod(pile, k)
hours += div
if rem != 0:
hours += 1
return hours <= H
|
Koko loves to eat bananas. There are N piles of bananas, the i-th pile has piles[i] bananas. The guards have gone and will come back in H hours.
Koko can decide her bananas-per-hour eating speed of K. Each hour, she chooses some pile of bananas, and eats K bananas from that pile. If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour.
Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back.
Return the minimum integer K such that she can eat all the bananas within H hours.
Example 1:
Input: piles = [3,6,7,11], H = 8
Output: 4
Example 2:
Input: piles = [30,11,23,4,20], H = 5
Output: 30
Example 3:
Input: piles = [30,11,23,4,20], H = 6
Output: 23
Constraints:
1 <= piles.length <= 10^4
piles.length <= H <= 10^9
1 <= piles[i] <= 10^9
|
class Solution:
def minEatingSpeed(self, piles: List[int], H: int) -> int:
def can(K):
cur = H
for a in piles:
cur -= a//K
cur -= (a%K!=0)
return cur >= 0
l, r = 1, sum([a+ 1 for a in piles])
while l<r:
mid = (l + r)//2
if can(mid):
r = mid
else:
l = mid + 1
return l
|
Koko loves to eat bananas. There are N piles of bananas, the i-th pile has piles[i] bananas. The guards have gone and will come back in H hours.
Koko can decide her bananas-per-hour eating speed of K. Each hour, she chooses some pile of bananas, and eats K bananas from that pile. If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour.
Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back.
Return the minimum integer K such that she can eat all the bananas within H hours.
Example 1:
Input: piles = [3,6,7,11], H = 8
Output: 4
Example 2:
Input: piles = [30,11,23,4,20], H = 5
Output: 30
Example 3:
Input: piles = [30,11,23,4,20], H = 6
Output: 23
Constraints:
1 <= piles.length <= 10^4
piles.length <= H <= 10^9
1 <= piles[i] <= 10^9
|
import math
class Solution:
def minEatingSpeed(self, piles: List[int], H: int) -> int:
end = max(piles)
start = 1
while start + 1 < end:
mid = start + (end - start) // 2
if self.possible(mid, piles, H):
end = mid
else:
start = mid
if self.possible(start,piles,H):
return start
if self.possible(end, piles, H):
return end
def possible(self, eat_load, total_load, hour):
return sum([math.ceil(-k / -eat_load) for k in total_load]) <= hour
|
Koko loves to eat bananas. There are N piles of bananas, the i-th pile has piles[i] bananas. The guards have gone and will come back in H hours.
Koko can decide her bananas-per-hour eating speed of K. Each hour, she chooses some pile of bananas, and eats K bananas from that pile. If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour.
Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back.
Return the minimum integer K such that she can eat all the bananas within H hours.
Example 1:
Input: piles = [3,6,7,11], H = 8
Output: 4
Example 2:
Input: piles = [30,11,23,4,20], H = 5
Output: 30
Example 3:
Input: piles = [30,11,23,4,20], H = 6
Output: 23
Constraints:
1 <= piles.length <= 10^4
piles.length <= H <= 10^9
1 <= piles[i] <= 10^9
|
class Solution:
def minEatingSpeed(self, piles: List[int], H: int) -> int:
# Minimum value we can have is the sum of piles / H
lo = sum(piles) // H
# Maximum would be the maximum value in the list
hi = max(piles)
#
# lo = 0
# hi = float('-inf')
# for pile in piles:
# lo += pile
# hi = max(hi, pile)
# lo =// H
if len(piles) == 1:
return ceil(piles[0] / H)
while lo <= hi:
mid = (lo + hi) // 2
count = sum(map(lambda x: ceil(x / mid), piles))
if count > H:
lo = mid + 1
else:
hi = mid - 1
return lo
|
Koko loves to eat bananas. There are N piles of bananas, the i-th pile has piles[i] bananas. The guards have gone and will come back in H hours.
Koko can decide her bananas-per-hour eating speed of K. Each hour, she chooses some pile of bananas, and eats K bananas from that pile. If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour.
Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back.
Return the minimum integer K such that she can eat all the bananas within H hours.
Example 1:
Input: piles = [3,6,7,11], H = 8
Output: 4
Example 2:
Input: piles = [30,11,23,4,20], H = 5
Output: 30
Example 3:
Input: piles = [30,11,23,4,20], H = 6
Output: 23
Constraints:
1 <= piles.length <= 10^4
piles.length <= H <= 10^9
1 <= piles[i] <= 10^9
|
class Solution:
def minEatingSpeed(self, piles: List[int], H: int) -> int:
def useDays(pile, speed):
days = pile // speed
return days if pile % speed == 0 else days + 1
def CanDone(piles, speed, H):
numDays = 0
for num in piles:
numDays += useDays(num, speed)
return numDays <= H
maxEat = max(piles)
left,right = 1, maxEat + 1
while left < right:
mid = left + ((right-left)>>1)
if CanDone(piles, mid, H):
right = mid
else:
left = mid + 1
return left
|
Koko loves to eat bananas. There are N piles of bananas, the i-th pile has piles[i] bananas. The guards have gone and will come back in H hours.
Koko can decide her bananas-per-hour eating speed of K. Each hour, she chooses some pile of bananas, and eats K bananas from that pile. If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour.
Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back.
Return the minimum integer K such that she can eat all the bananas within H hours.
Example 1:
Input: piles = [3,6,7,11], H = 8
Output: 4
Example 2:
Input: piles = [30,11,23,4,20], H = 5
Output: 30
Example 3:
Input: piles = [30,11,23,4,20], H = 6
Output: 23
Constraints:
1 <= piles.length <= 10^4
piles.length <= H <= 10^9
1 <= piles[i] <= 10^9
|
class Solution:
def minEatingSpeed(self, piles: List[int], H: int) -> int:
#piles.sort()
lo, hi = 1, max(piles)
ans = hi
while lo <= hi:
k = (lo + hi) // 2
time = 0
for i in piles:
t = int(ceil(i / k))
if not t:
t += 1
time += t
#print(k, time)
if time <= H:
hi = k - 1
ans = k
else:
lo = k + 1
return ans
|
Koko loves to eat bananas. There are N piles of bananas, the i-th pile has piles[i] bananas. The guards have gone and will come back in H hours.
Koko can decide her bananas-per-hour eating speed of K. Each hour, she chooses some pile of bananas, and eats K bananas from that pile. If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour.
Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back.
Return the minimum integer K such that she can eat all the bananas within H hours.
Example 1:
Input: piles = [3,6,7,11], H = 8
Output: 4
Example 2:
Input: piles = [30,11,23,4,20], H = 5
Output: 30
Example 3:
Input: piles = [30,11,23,4,20], H = 6
Output: 23
Constraints:
1 <= piles.length <= 10^4
piles.length <= H <= 10^9
1 <= piles[i] <= 10^9
|
class Solution:
def minEatingSpeed(self, piles: List[int], H: int) -> int:
piles.sort()
lo, hi = 1, piles[-1]
ans = hi
while lo <= hi:
k = (lo + hi) // 2
time = 0
for i in piles:
t = int(ceil(i / k))
if not t:
t += 1
time += t
#print(k, time)
if time <= H:
hi = k - 1
ans = k
else:
lo = k + 1
return ans
|
Koko loves to eat bananas. There are N piles of bananas, the i-th pile has piles[i] bananas. The guards have gone and will come back in H hours.
Koko can decide her bananas-per-hour eating speed of K. Each hour, she chooses some pile of bananas, and eats K bananas from that pile. If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour.
Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back.
Return the minimum integer K such that she can eat all the bananas within H hours.
Example 1:
Input: piles = [3,6,7,11], H = 8
Output: 4
Example 2:
Input: piles = [30,11,23,4,20], H = 5
Output: 30
Example 3:
Input: piles = [30,11,23,4,20], H = 6
Output: 23
Constraints:
1 <= piles.length <= 10^4
piles.length <= H <= 10^9
1 <= piles[i] <= 10^9
|
import math as m
class Solution:
def check_time(piles,speed):
hrs=0
for i in piles:
hrs+=int(m.ceil(i/speed))
return hrs
def minEatingSpeed(self, piles: List[int], H: int) -> int:
low=1
high=float('-inf')
for i in piles:
high=max(high,i)
if H==len(piles):
return high
while(low<=high):
mid=(low+high)//2
hrs=Solution.check_time(piles,mid)
if hrs>H:
low=mid+1
else:
high=mid-1
return low
|
Koko loves to eat bananas. There are N piles of bananas, the i-th pile has piles[i] bananas. The guards have gone and will come back in H hours.
Koko can decide her bananas-per-hour eating speed of K. Each hour, she chooses some pile of bananas, and eats K bananas from that pile. If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour.
Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back.
Return the minimum integer K such that she can eat all the bananas within H hours.
Example 1:
Input: piles = [3,6,7,11], H = 8
Output: 4
Example 2:
Input: piles = [30,11,23,4,20], H = 5
Output: 30
Example 3:
Input: piles = [30,11,23,4,20], H = 6
Output: 23
Constraints:
1 <= piles.length <= 10^4
piles.length <= H <= 10^9
1 <= piles[i] <= 10^9
|
class Solution:
def minEatingSpeed(self, piles: List[int], H: int) -> int:
# want to search for K - this is the variable to change
# if piles in sorted order - based on length of pile and hours - need to get average eats in the pile
# let K = 1 to max(piles)
# per hour - go through the piles and eat K bananas before time limit is up
# O(N) time to go through all piles N
# K is split via binary search log(K)
# log(K) * N times
low = 1
high = max(piles)
# the piles is not the one we want to find - its K
# take too much time to eat - raise K
# too little time to eat - lower K
# set timer
# always a solution
def possible(k):
time = 0
# find out how much time it takes
for pile in piles:
time += (pile - 1) // k + 1
return time <= H
while low <= high:
mid = low + (high - low) // 2
outcome = possible(mid)
# check if its possible
if possible(mid): # just right
high = mid - 1
else:
low = mid + 1
return low # should not hit - always solution
|
Koko loves to eat bananas. There are N piles of bananas, the i-th pile has piles[i] bananas. The guards have gone and will come back in H hours.
Koko can decide her bananas-per-hour eating speed of K. Each hour, she chooses some pile of bananas, and eats K bananas from that pile. If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour.
Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back.
Return the minimum integer K such that she can eat all the bananas within H hours.
Example 1:
Input: piles = [3,6,7,11], H = 8
Output: 4
Example 2:
Input: piles = [30,11,23,4,20], H = 5
Output: 30
Example 3:
Input: piles = [30,11,23,4,20], H = 6
Output: 23
Constraints:
1 <= piles.length <= 10^4
piles.length <= H <= 10^9
1 <= piles[i] <= 10^9
|
class Solution:
def minEatingSpeed(self, piles: List[int], H: int) -> int:
if len(piles)==H:
return max(piles)
low=1
high=max(piles)
ans=high
while low<=high:
mid=(low+high)//2
h=H
for i in range(len(piles)):
if piles[i]<=mid:
h-=1
else:
if piles[i]%mid==0:
h-=piles[i]//mid
else:
h-=(piles[i]//mid)+1
if h>=0:
ans =min(ans,mid)
high=mid-1
else:
low=mid+1
return ans
|
Koko loves to eat bananas. There are N piles of bananas, the i-th pile has piles[i] bananas. The guards have gone and will come back in H hours.
Koko can decide her bananas-per-hour eating speed of K. Each hour, she chooses some pile of bananas, and eats K bananas from that pile. If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour.
Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back.
Return the minimum integer K such that she can eat all the bananas within H hours.
Example 1:
Input: piles = [3,6,7,11], H = 8
Output: 4
Example 2:
Input: piles = [30,11,23,4,20], H = 5
Output: 30
Example 3:
Input: piles = [30,11,23,4,20], H = 6
Output: 23
Constraints:
1 <= piles.length <= 10^4
piles.length <= H <= 10^9
1 <= piles[i] <= 10^9
|
from math import ceil
class Solution:
def ok(self, piles: List[int], k: int, H: int) -> bool:
return sum([ ceil(piles[i] / k) for i in range(len(piles)) ]) <= H
def minEatingSpeed(self, piles: List[int], H: int) -> int:
st = 1
en = sum(piles)
ans = en
while st <= en:
mid = st + (en - st) // 2
if self.ok(piles, mid, H):
ans = mid
en = mid - 1
else:
st = mid + 1
return ans
|
Koko loves to eat bananas. There are N piles of bananas, the i-th pile has piles[i] bananas. The guards have gone and will come back in H hours.
Koko can decide her bananas-per-hour eating speed of K. Each hour, she chooses some pile of bananas, and eats K bananas from that pile. If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour.
Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back.
Return the minimum integer K such that she can eat all the bananas within H hours.
Example 1:
Input: piles = [3,6,7,11], H = 8
Output: 4
Example 2:
Input: piles = [30,11,23,4,20], H = 5
Output: 30
Example 3:
Input: piles = [30,11,23,4,20], H = 6
Output: 23
Constraints:
1 <= piles.length <= 10^4
piles.length <= H <= 10^9
1 <= piles[i] <= 10^9
|
import math
class Solution:
def minEatingSpeed(self, piles: List[int], H: int) -> int:
L = len(piles)
if L==H:
return max(piles)
k2 = max(piles)
k1 = 1
res = k2
k = k1
while k1<k2-1:
cur = 0
for j in range(L):
cur += math.ceil(piles[j]/k)
if cur>H:
k1 = k
break
if cur<=H:
k2 = k
res = min(res,k)
#print(k1,k2,k,cur)
k = (k1+k2)//2
return res
|
Koko loves to eat bananas. There are N piles of bananas, the i-th pile has piles[i] bananas. The guards have gone and will come back in H hours.
Koko can decide her bananas-per-hour eating speed of K. Each hour, she chooses some pile of bananas, and eats K bananas from that pile. If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour.
Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back.
Return the minimum integer K such that she can eat all the bananas within H hours.
Example 1:
Input: piles = [3,6,7,11], H = 8
Output: 4
Example 2:
Input: piles = [30,11,23,4,20], H = 5
Output: 30
Example 3:
Input: piles = [30,11,23,4,20], H = 6
Output: 23
Constraints:
1 <= piles.length <= 10^4
piles.length <= H <= 10^9
1 <= piles[i] <= 10^9
|
class Solution:
def minEatingSpeed(self, piles: List[int], H: int) -> int:
def time(val):
t = len(piles)
for pile in piles:
t += (pile - 1) // val
return t
l, r = 1, max(piles)
while l <= r:
mid = l + (r - l) // 2
t = time(mid)
if t <= H:
r = mid - 1
else:
l = mid + 1
return l
|
Koko loves to eat bananas. There are N piles of bananas, the i-th pile has piles[i] bananas. The guards have gone and will come back in H hours.
Koko can decide her bananas-per-hour eating speed of K. Each hour, she chooses some pile of bananas, and eats K bananas from that pile. If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour.
Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back.
Return the minimum integer K such that she can eat all the bananas within H hours.
Example 1:
Input: piles = [3,6,7,11], H = 8
Output: 4
Example 2:
Input: piles = [30,11,23,4,20], H = 5
Output: 30
Example 3:
Input: piles = [30,11,23,4,20], H = 6
Output: 23
Constraints:
1 <= piles.length <= 10^4
piles.length <= H <= 10^9
1 <= piles[i] <= 10^9
|
class Solution:
def minEatingSpeed(self, piles: List[int], H: int) -> int:
# want to search for K - this is the variable to change
# if piles in sorted order - based on length of pile and hours - need to get average eats in the pile
# let K = 1 to max(piles)
# per hour - go through the piles and eat K bananas before time limit is up
# O(N) time to go through all piles N
# K is split via binary search log(K)
# log(K) * N times
low = 1
high = max(piles)
# the piles is not the one we want to find - its K
# take too much time to eat - raise K
# too little time to eat - lower K
# set timer
# always a solution
def possible(k):
time = 0
# find out how much time it takes
for pile in piles:
time += (pile + k - 1) // k # if k >= pile then it will be 1.xxxxx that means we ate the pile
# if there is 0, then that means
return time <= H
while low < high:
mid = low + (high - low) // 2
outcome = possible(mid)
# check if its possible
if possible(mid): # reduce because anything greater will be satisfied - need to find left bound
high = mid
else: # not possible - need to increase K to eat faster
low = mid + 1
# low == high
return high # should not hit - always solution
|
Koko loves to eat bananas. There are N piles of bananas, the i-th pile has piles[i] bananas. The guards have gone and will come back in H hours.
Koko can decide her bananas-per-hour eating speed of K. Each hour, she chooses some pile of bananas, and eats K bananas from that pile. If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour.
Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back.
Return the minimum integer K such that she can eat all the bananas within H hours.
Example 1:
Input: piles = [3,6,7,11], H = 8
Output: 4
Example 2:
Input: piles = [30,11,23,4,20], H = 5
Output: 30
Example 3:
Input: piles = [30,11,23,4,20], H = 6
Output: 23
Constraints:
1 <= piles.length <= 10^4
piles.length <= H <= 10^9
1 <= piles[i] <= 10^9
|
class Solution:
def minEatingSpeed(self, piles: List[int], H: int) -> int:
# want to search for K - this is the variable to change
# if piles in sorted order - based on length of pile and hours - need to get average eats in the pile
# let K = 1 to max(piles)
# per hour - go through the piles and eat K bananas before time limit is up
# O(N) time to go through all piles N
# K is split via binary search log(K)
# log(K) * N times
low = 1
high = max(piles)
# the piles is not the one we want to find - its K
# take too much time to eat - raise K
# too little time to eat - lower K
# set timer
# always a solution
def possible(k):
time = 0
# find out how much time it takes
for pile in piles:
time += (pile + k - 1) // k # if k >= pile then it will be 1.xxxxx that means we ate the pile
# if there is 0, then that means
return time <= H
while low < high:
mid = low + (high - low) // 2
outcome = possible(mid)
# check if its possible
if possible(mid): # reduce because anything greater will be satisfied - need to find left bound
high = mid
else: # not possible - need to increase K to eat faster
low = mid + 1
return low # should not hit - always solution
|
Koko loves to eat bananas. There are N piles of bananas, the i-th pile has piles[i] bananas. The guards have gone and will come back in H hours.
Koko can decide her bananas-per-hour eating speed of K. Each hour, she chooses some pile of bananas, and eats K bananas from that pile. If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour.
Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back.
Return the minimum integer K such that she can eat all the bananas within H hours.
Example 1:
Input: piles = [3,6,7,11], H = 8
Output: 4
Example 2:
Input: piles = [30,11,23,4,20], H = 5
Output: 30
Example 3:
Input: piles = [30,11,23,4,20], H = 6
Output: 23
Constraints:
1 <= piles.length <= 10^4
piles.length <= H <= 10^9
1 <= piles[i] <= 10^9
|
class Solution:
def minEatingSpeed(self, array: List[int], hour: int) -> int:
if len(array)==hour:
return max(array)
low=1
high=max(array)
ans=high
while low<=high:
mid=(low+high)//2
h=hour
for i in range(len(array)):
if array[i]<=mid:
h-=1
else:
if array[i]%mid==0:
h-=array[i]//mid
else:
h-=(array[i]//mid)+1
if h>=0:
ans =min(ans,mid)
high=mid-1
else:
low=mid+1
return ans
|
Koko loves to eat bananas. There are N piles of bananas, the i-th pile has piles[i] bananas. The guards have gone and will come back in H hours.
Koko can decide her bananas-per-hour eating speed of K. Each hour, she chooses some pile of bananas, and eats K bananas from that pile. If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour.
Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back.
Return the minimum integer K such that she can eat all the bananas within H hours.
Example 1:
Input: piles = [3,6,7,11], H = 8
Output: 4
Example 2:
Input: piles = [30,11,23,4,20], H = 5
Output: 30
Example 3:
Input: piles = [30,11,23,4,20], H = 6
Output: 23
Constraints:
1 <= piles.length <= 10^4
piles.length <= H <= 10^9
1 <= piles[i] <= 10^9
|
class Solution:
def minEatingSpeed(self, piles, H):
speed = math.floor(sum(piles) / H)
while True:
time = sum([math.ceil(x / speed) if speed else float('inf') for x in piles])
print(f'{speed=}, {time=}, {H=}')
if time <= H: break
speed += 1
return speed
|
Koko loves to eat bananas. There are N piles of bananas, the i-th pile has piles[i] bananas. The guards have gone and will come back in H hours.
Koko can decide her bananas-per-hour eating speed of K. Each hour, she chooses some pile of bananas, and eats K bananas from that pile. If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour.
Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back.
Return the minimum integer K such that she can eat all the bananas within H hours.
Example 1:
Input: piles = [3,6,7,11], H = 8
Output: 4
Example 2:
Input: piles = [30,11,23,4,20], H = 5
Output: 30
Example 3:
Input: piles = [30,11,23,4,20], H = 6
Output: 23
Constraints:
1 <= piles.length <= 10^4
piles.length <= H <= 10^9
1 <= piles[i] <= 10^9
|
import math
class Solution:
def minEatingSpeed(self, piles: List[int], H: int) -> int:
self.store = {}
l = 1
r = max(piles)
while l <= r:
m = l + (r-l)//2
if self.calc_h(piles, m) > H:
l = m + 1
else: # calc_h(piles, m) <= H
if m == 1 or self.calc_h(piles, m-1) > H:
return m
r = m - 1
def calc_h(self, piles, K):
if K not in self.store:
h = [p // K + (p%K > 0) for p in piles]
self.store[K] = int(sum(h))
return self.store[K]
|
Koko loves to eat bananas. There are N piles of bananas, the i-th pile has piles[i] bananas. The guards have gone and will come back in H hours.
Koko can decide her bananas-per-hour eating speed of K. Each hour, she chooses some pile of bananas, and eats K bananas from that pile. If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour.
Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back.
Return the minimum integer K such that she can eat all the bananas within H hours.
Example 1:
Input: piles = [3,6,7,11], H = 8
Output: 4
Example 2:
Input: piles = [30,11,23,4,20], H = 5
Output: 30
Example 3:
Input: piles = [30,11,23,4,20], H = 6
Output: 23
Constraints:
1 <= piles.length <= 10^4
piles.length <= H <= 10^9
1 <= piles[i] <= 10^9
|
import math
class Solution:
def minEatingSpeed(self, piles: List[int], H: int) -> int:
l = 1
r = max(piles)
while l <= r:
m = l + (r-l)//2
if calc_h(piles, m) > H:
l = m + 1
else: # calc_h(piles, m) <= H
if m == 1 or calc_h(piles, m-1) > H:
return m
r = m - 1
def calc_h(piles, K):
h = [p // K + (p%K > 0) for p in piles]
return int(sum(h))
|
Koko loves to eat bananas. There are N piles of bananas, the i-th pile has piles[i] bananas. The guards have gone and will come back in H hours.
Koko can decide her bananas-per-hour eating speed of K. Each hour, she chooses some pile of bananas, and eats K bananas from that pile. If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour.
Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back.
Return the minimum integer K such that she can eat all the bananas within H hours.
Example 1:
Input: piles = [3,6,7,11], H = 8
Output: 4
Example 2:
Input: piles = [30,11,23,4,20], H = 5
Output: 30
Example 3:
Input: piles = [30,11,23,4,20], H = 6
Output: 23
Constraints:
1 <= piles.length <= 10^4
piles.length <= H <= 10^9
1 <= piles[i] <= 10^9
|
import math
class Solution:
def minEatingSpeed(self, piles: List[int], H: int) -> int:
total_bananas = sum(piles)
left, right = math.ceil(total_bananas / H), total_bananas
print(left, right)
while left < right:
mid = (left + right) // 2
if not sum([math.ceil(pile/mid) for pile in piles]) <= H:
left = mid + 1
else:
right = mid
return left
|
Koko loves to eat bananas. There are N piles of bananas, the i-th pile has piles[i] bananas. The guards have gone and will come back in H hours.
Koko can decide her bananas-per-hour eating speed of K. Each hour, she chooses some pile of bananas, and eats K bananas from that pile. If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour.
Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back.
Return the minimum integer K such that she can eat all the bananas within H hours.
Example 1:
Input: piles = [3,6,7,11], H = 8
Output: 4
Example 2:
Input: piles = [30,11,23,4,20], H = 5
Output: 30
Example 3:
Input: piles = [30,11,23,4,20], H = 6
Output: 23
Constraints:
1 <= piles.length <= 10^4
piles.length <= H <= 10^9
1 <= piles[i] <= 10^9
|
class Solution:
def minEatingSpeed(self, piles: List[int], H: int) -> int:
def condition(k) -> bool:
count = 0
for v in piles:
count += int(math.ceil(v / k))
if count > H:
return False
return True
left,right = 1,max(piles)
while left < right:
mid = left + (right - left) // 2
if condition(mid):
right = mid
else:
left = mid + 1
return left
|
Koko loves to eat bananas. There are N piles of bananas, the i-th pile has piles[i] bananas. The guards have gone and will come back in H hours.
Koko can decide her bananas-per-hour eating speed of K. Each hour, she chooses some pile of bananas, and eats K bananas from that pile. If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour.
Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back.
Return the minimum integer K such that she can eat all the bananas within H hours.
Example 1:
Input: piles = [3,6,7,11], H = 8
Output: 4
Example 2:
Input: piles = [30,11,23,4,20], H = 5
Output: 30
Example 3:
Input: piles = [30,11,23,4,20], H = 6
Output: 23
Constraints:
1 <= piles.length <= 10^4
piles.length <= H <= 10^9
1 <= piles[i] <= 10^9
|
class Solution:
def minEatingSpeed(self, piles: List[int], H: int) -> int:
if not piles: return -1
low,high=1,sum(piles)
def isPossible(piles,target,H):
return sum(math.ceil(p/target) for p in piles) <= H
while low < high:
mid = low+(high-low)//2
# print(low,high,mid)
if isPossible(piles,mid,H):
high = mid
else:
low = mid+1
return low
|
Koko loves to eat bananas. There are N piles of bananas, the i-th pile has piles[i] bananas. The guards have gone and will come back in H hours.
Koko can decide her bananas-per-hour eating speed of K. Each hour, she chooses some pile of bananas, and eats K bananas from that pile. If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour.
Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back.
Return the minimum integer K such that she can eat all the bananas within H hours.
Example 1:
Input: piles = [3,6,7,11], H = 8
Output: 4
Example 2:
Input: piles = [30,11,23,4,20], H = 5
Output: 30
Example 3:
Input: piles = [30,11,23,4,20], H = 6
Output: 23
Constraints:
1 <= piles.length <= 10^4
piles.length <= H <= 10^9
1 <= piles[i] <= 10^9
|
class Solution:
def minEatingSpeed(self, piles: List[int], H: int) -> int:
left = 1
right = max(piles)
while left < right:
mid = (left + right) // 2
if self.get_total_days(piles, mid) > H:
left = mid + 1
else:
right = mid
return left
def get_total_days(self, piles: List[int], num_eat: int)-> int:
if num_eat == 0:
return 0
total_days = 0
for pile in piles:
total_days += math.ceil(float(pile) / num_eat)
return total_days
|
Koko loves to eat bananas. There are N piles of bananas, the i-th pile has piles[i] bananas. The guards have gone and will come back in H hours.
Koko can decide her bananas-per-hour eating speed of K. Each hour, she chooses some pile of bananas, and eats K bananas from that pile. If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour.
Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back.
Return the minimum integer K such that she can eat all the bananas within H hours.
Example 1:
Input: piles = [3,6,7,11], H = 8
Output: 4
Example 2:
Input: piles = [30,11,23,4,20], H = 5
Output: 30
Example 3:
Input: piles = [30,11,23,4,20], H = 6
Output: 23
Constraints:
1 <= piles.length <= 10^4
piles.length <= H <= 10^9
1 <= piles[i] <= 10^9
|
class Solution:
def minEatingSpeed(self, array: List[int], hour: int) -> int:
if len(array)==hour:
return max(array)
low=1
high=max(array)
ans=high
while low<=high:
mid=(low+high)//2
h=hour
for i in range(len(array)):
if array[i]<=mid:
h-=1
else:
if array[i]%mid==0:
h-=array[i]//mid
else:
h-=(array[i]//mid)+1
if h<0:
break
if h>=0:
ans =mid
high=mid-1
else:
low=mid+1
return ans
|
Koko loves to eat bananas. There are N piles of bananas, the i-th pile has piles[i] bananas. The guards have gone and will come back in H hours.
Koko can decide her bananas-per-hour eating speed of K. Each hour, she chooses some pile of bananas, and eats K bananas from that pile. If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour.
Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back.
Return the minimum integer K such that she can eat all the bananas within H hours.
Example 1:
Input: piles = [3,6,7,11], H = 8
Output: 4
Example 2:
Input: piles = [30,11,23,4,20], H = 5
Output: 30
Example 3:
Input: piles = [30,11,23,4,20], H = 6
Output: 23
Constraints:
1 <= piles.length <= 10^4
piles.length <= H <= 10^9
1 <= piles[i] <= 10^9
|
class Solution:
def minEatingSpeed(self, piles: List[int], H: int) -> int:
def possible(K):
return sum((p-1) //K+ 1 for p in piles) <= H
lo, hi = 1, max(piles)
while lo < hi:
mi = (lo + hi) //2
if not possible(mi):
lo = mi + 1
else:
hi = mi
return lo
|
Koko loves to eat bananas. There are N piles of bananas, the i-th pile has piles[i] bananas. The guards have gone and will come back in H hours.
Koko can decide her bananas-per-hour eating speed of K. Each hour, she chooses some pile of bananas, and eats K bananas from that pile. If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour.
Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back.
Return the minimum integer K such that she can eat all the bananas within H hours.
Example 1:
Input: piles = [3,6,7,11], H = 8
Output: 4
Example 2:
Input: piles = [30,11,23,4,20], H = 5
Output: 30
Example 3:
Input: piles = [30,11,23,4,20], H = 6
Output: 23
Constraints:
1 <= piles.length <= 10^4
piles.length <= H <= 10^9
1 <= piles[i] <= 10^9
|
class Solution:
def minEatingSpeed(self, piles: List[int], H: int) -> int:
maxB = max(piles)
def getH(k):
if not k:
return float('inf')
numHours = 0
for pile in piles:
numHours += pile//k + int(bool(pile%k))
return numHours
def bs(start, end):
if start > end:
return float('inf')
mid = (start + end + 1)//2
h = getH(mid)
# print(mid, h)
if h < H:
if mid != start :
return min(mid,bs(start, mid-1))
if h > H:
return bs(mid+1, end)
if h == H:
while getH(mid-1) == H:
mid -= 1
return mid
return bs(0, maxB)
|
Koko loves to eat bananas. There are N piles of bananas, the i-th pile has piles[i] bananas. The guards have gone and will come back in H hours.
Koko can decide her bananas-per-hour eating speed of K. Each hour, she chooses some pile of bananas, and eats K bananas from that pile. If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour.
Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back.
Return the minimum integer K such that she can eat all the bananas within H hours.
Example 1:
Input: piles = [3,6,7,11], H = 8
Output: 4
Example 2:
Input: piles = [30,11,23,4,20], H = 5
Output: 30
Example 3:
Input: piles = [30,11,23,4,20], H = 6
Output: 23
Constraints:
1 <= piles.length <= 10^4
piles.length <= H <= 10^9
1 <= piles[i] <= 10^9
|
class Solution:
def minEatingSpeed(self, piles: List[int], H: int) -> int:
def time_needed(K):
return sum(p//K + (1 if p%K else 0) for p in piles)
lower = max(1, sum(piles)//H)
upper = max(piles)
while lower < upper:
if time_needed(lower) <= H:
return lower
lower += 1
mid = (lower + upper)//2
if time_needed(mid) <= H:
upper = mid
else:
lower = mid + 1
return lower
|
Koko loves to eat bananas. There are N piles of bananas, the i-th pile has piles[i] bananas. The guards have gone and will come back in H hours.
Koko can decide her bananas-per-hour eating speed of K. Each hour, she chooses some pile of bananas, and eats K bananas from that pile. If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour.
Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back.
Return the minimum integer K such that she can eat all the bananas within H hours.
Example 1:
Input: piles = [3,6,7,11], H = 8
Output: 4
Example 2:
Input: piles = [30,11,23,4,20], H = 5
Output: 30
Example 3:
Input: piles = [30,11,23,4,20], H = 6
Output: 23
Constraints:
1 <= piles.length <= 10^4
piles.length <= H <= 10^9
1 <= piles[i] <= 10^9
|
class Solution:
def minEatingSpeed(self, piles: List[int], H: int) -> int:
#piles.sort()
lo, hi = 1, max(piles)
ans = hi
while lo <= hi:
k = (lo + hi) // 2
time = 0
for i in piles:
t = int(ceil(i / k))
if not t:
t += 1
time += t
if time > H:
break
#print(k, time)
if time <= H:
hi = k - 1
ans = k
else:
lo = k + 1
return ans
|
Koko loves to eat bananas. There are N piles of bananas, the i-th pile has piles[i] bananas. The guards have gone and will come back in H hours.
Koko can decide her bananas-per-hour eating speed of K. Each hour, she chooses some pile of bananas, and eats K bananas from that pile. If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour.
Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back.
Return the minimum integer K such that she can eat all the bananas within H hours.
Example 1:
Input: piles = [3,6,7,11], H = 8
Output: 4
Example 2:
Input: piles = [30,11,23,4,20], H = 5
Output: 30
Example 3:
Input: piles = [30,11,23,4,20], H = 6
Output: 23
Constraints:
1 <= piles.length <= 10^4
piles.length <= H <= 10^9
1 <= piles[i] <= 10^9
|
class Solution:
def minEatingSpeed(self, piles: List[int], H: int) -> int:
maxB = max(piles)
def getH(k):
if not k:
return float('inf')
numHours = 0
for pile in piles:
numHours += pile//k + int(bool(pile%k))
return numHours
def bs(start, end):
if start > end:
return float('inf')
mid = (start + end + 1)//2
h = getH(mid)
# print(mid, h)
if h < H:
if mid > start :
return min(mid,bs(start, mid-1))
if h > H:
return bs(mid+1, end)
if h == H:
while getH(mid-1) == H:
mid -= 1
return mid
return bs(0, maxB)
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.