inputs
stringlengths 50
14k
| targets
stringlengths 4
655k
|
|---|---|
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K.
If there is no non-empty subarray with sum at least K, return -1.
Example 1:
Input: A = [1], K = 1
Output: 1
Example 2:
Input: A = [1,2], K = 4
Output: -1
Example 3:
Input: A = [2,-1,2], K = 3
Output: 3
Note:
1 <= A.length <= 50000
-10 ^ 5 <= A[i] <= 10 ^ 5
1 <= K <= 10 ^ 9
|
class Solution:
def shortestSubarray(self, A: List[int], K: int) -> int:
if K == 0:
return 0
if not A:
return -1
if len(A) == 1:
if A[0] >= K:
return 1
return 0
from collections import deque
ret = float('inf')
for element in A:
if element >= K:
return 1
for i in range(1, len(A)):
A[i] += A[i-1]
current_ele = deque()
current_ele.append((-1, 0))
for i, element in enumerate(A):
while current_ele and current_ele[-1][1] >= element:
current_ele.pop()
current_ele.append((i, element))
while current_ele[-1][1] >= current_ele[0][1]+K:
ret = min(ret, current_ele[-1][0]-current_ele[0][0])
current_ele.popleft()
return ret if ret != float('inf') else -1
|
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K.
If there is no non-empty subarray with sum at least K, return -1.
Example 1:
Input: A = [1], K = 1
Output: 1
Example 2:
Input: A = [1,2], K = 4
Output: -1
Example 3:
Input: A = [2,-1,2], K = 3
Output: 3
Note:
1 <= A.length <= 50000
-10 ^ 5 <= A[i] <= 10 ^ 5
1 <= K <= 10 ^ 9
|
from collections import deque
class Solution:
def shortestSubarray(self, A: List[int], K: int) -> int:
d = deque()
n = len(A)
res = n+1
prefixArr = [0] * (n + 1)
for i in range(n):
prefixArr[i + 1] = A[i] + prefixArr[i]
for end in range(n+1):
while d and prefixArr[end] - prefixArr[d[0]] >= K:
res = min(res, end - d.popleft())
while d and prefixArr[d[-1]] >= prefixArr[end]:
d.pop()
d.append(end)
return -1 if res == n+1 else res
|
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K.
If there is no non-empty subarray with sum at least K, return -1.
Example 1:
Input: A = [1], K = 1
Output: 1
Example 2:
Input: A = [1,2], K = 4
Output: -1
Example 3:
Input: A = [2,-1,2], K = 3
Output: 3
Note:
1 <= A.length <= 50000
-10 ^ 5 <= A[i] <= 10 ^ 5
1 <= K <= 10 ^ 9
|
class Solution:
def shortestSubarray(self, A: List[int], K: int) -> int:
deq = collections.deque([])
prefix = [0]
res = float('inf')
for a in A:
prefix.append(prefix[-1] + a)
for i, x in enumerate(prefix):
while deq and prefix[deq[-1]] >= x:
deq.pop()
while deq and prefix[deq[0]] <= x - K:
res = min(res, i - deq.popleft())
deq.append(i)
return res if res < float('inf') else -1
|
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K.
If there is no non-empty subarray with sum at least K, return -1.
Example 1:
Input: A = [1], K = 1
Output: 1
Example 2:
Input: A = [1,2], K = 4
Output: -1
Example 3:
Input: A = [2,-1,2], K = 3
Output: 3
Note:
1 <= A.length <= 50000
-10 ^ 5 <= A[i] <= 10 ^ 5
1 <= K <= 10 ^ 9
|
class Solution:
def shortestSubarray(self, A: List[int], K: int) -> int:
P = [0]
for x in A:
P.append(P[-1] + x)
q = collections.deque()
min_length_K = -1
for j, Pj in enumerate(P):
while len(q) > 0 and P[q[-1]] >= Pj:
q.pop()
while len(q) > 0 and Pj - P[q[0]] >= K:
if min_length_K == -1 or j - q[0] < min_length_K:
min_length_K = j - q[0]
q.popleft()
q.append(j)
return min_length_K
|
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K.
If there is no non-empty subarray with sum at least K, return -1.
Example 1:
Input: A = [1], K = 1
Output: 1
Example 2:
Input: A = [1,2], K = 4
Output: -1
Example 3:
Input: A = [2,-1,2], K = 3
Output: 3
Note:
1 <= A.length <= 50000
-10 ^ 5 <= A[i] <= 10 ^ 5
1 <= K <= 10 ^ 9
|
class Solution:
def shortestSubarray(self, A: List[int], K: int) -> int:
d = collections.deque([[0, 0]])
res, cur = float('inf'), 0
for i, a in enumerate(A):
cur += a
while d and cur - d[0][1] >= K:
res = min(res, i + 1 - d.popleft()[0])
while d and cur <= d[-1][1]:
d.pop()
d.append([i + 1, cur])
return res if res < float('inf') else -1
|
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K.
If there is no non-empty subarray with sum at least K, return -1.
Example 1:
Input: A = [1], K = 1
Output: 1
Example 2:
Input: A = [1,2], K = 4
Output: -1
Example 3:
Input: A = [2,-1,2], K = 3
Output: 3
Note:
1 <= A.length <= 50000
-10 ^ 5 <= A[i] <= 10 ^ 5
1 <= K <= 10 ^ 9
|
class Solution:
def shortestSubarray(self, A: List[int], K: int) -> int:
#First check this problem >> https://leetcode.com/problems/minimum-size-subarray-sum/
#>>> Important <<<<
#https://leetcode.com/problems/shortest-subarray-with-sum-at-least-k/discuss/262641/Python-Prefix-Sum-and-Sliding-Window
#As left and right index keeps increasing, time complexity is O(N).
n = len(A)
res = n+1
Sum = 0
dq = collections.deque([(-1,0)]) #-1 for index, 0 for sum
for i, val in enumerate(A):
Sum += val
if val > 0:
while dq and Sum - dq[0][1] >= K:
res = min(res, i-dq.popleft()[0])
else:
while dq and Sum < dq[-1][1]:
dq.pop()
dq.append((i, Sum))
if res <= n:
return res
else:
return -1
|
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K.
If there is no non-empty subarray with sum at least K, return -1.
Example 1:
Input: A = [1], K = 1
Output: 1
Example 2:
Input: A = [1,2], K = 4
Output: -1
Example 3:
Input: A = [2,-1,2], K = 3
Output: 3
Note:
1 <= A.length <= 50000
-10 ^ 5 <= A[i] <= 10 ^ 5
1 <= K <= 10 ^ 9
|
from collections import deque
class Solution:
def shortestSubarray(self, A: List[int], K: int) -> int:
dq = deque()
pre = [0]
l = len(A)
ans = l+1
for i in range(l):
pre.append(pre[-1] + A[i])
for i in range(l+1):
while dq and pre[dq[-1]] >= pre[i]:
dq.pop()
while dq and pre[dq[0]] + K <= pre[i]:
ans = min(ans, i-dq.popleft())
dq.append(i)
#print(ans)
return (ans if ans < l+1 else -1)
|
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K.
If there is no non-empty subarray with sum at least K, return -1.
Example 1:
Input: A = [1], K = 1
Output: 1
Example 2:
Input: A = [1,2], K = 4
Output: -1
Example 3:
Input: A = [2,-1,2], K = 3
Output: 3
Note:
1 <= A.length <= 50000
-10 ^ 5 <= A[i] <= 10 ^ 5
1 <= K <= 10 ^ 9
|
from collections import deque
class Solution:
def shortestSubarray(self, A: List[int], K: int) -> int:
if max(A) >= K:
return 1
if K <= 0:
return -1
psum = [0]
for a in A:
psum.append(psum[-1] + a)
window = deque([(0, 0)])
res = float('inf')
for right in range(len(A)):
while window and psum[right + 1] <= window[-1][1]:
window.pop()
window.append((right + 1, psum[right + 1]))
while window and window[-1][1] - window[0][1] >= K:
res = min(res, window[-1][0] - window[0][0])
window.popleft()
return res if res != float('inf') else -1
|
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K.
If there is no non-empty subarray with sum at least K, return -1.
Example 1:
Input: A = [1], K = 1
Output: 1
Example 2:
Input: A = [1,2], K = 4
Output: -1
Example 3:
Input: A = [2,-1,2], K = 3
Output: 3
Note:
1 <= A.length <= 50000
-10 ^ 5 <= A[i] <= 10 ^ 5
1 <= K <= 10 ^ 9
|
class Solution:
def shortestSubarray(self, nums: List[int], K: int) -> int:
sum1=0
flag=False
min1=float('inf')
cums=[0]
dq=collections.deque()
for i in range(len(nums)):
cums.append(cums[-1]+nums[i])
print(cums)
for i in range(len(nums)+1):
while dq and cums[i]-cums[dq[0]]>=K:
val=dq.popleft()
min1=min(min1,i-val)
while dq and cums[i]<=cums[dq[-1]]:
dq.pop()
dq.append(i)
#This loop is used because if there is [2,-1,2] and K=3 for cums=[0,2,1,3] we know that from 2 to 1 the value decreased from 1 to x increased so the min will be from 1 to x rather than 2 to x so eliminated(because length b/w 1 to x < 2 to x)
if min1==float('inf'):
return -1
return min1
|
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K.
If there is no non-empty subarray with sum at least K, return -1.
Example 1:
Input: A = [1], K = 1
Output: 1
Example 2:
Input: A = [1,2], K = 4
Output: -1
Example 3:
Input: A = [2,-1,2], K = 3
Output: 3
Note:
1 <= A.length <= 50000
-10 ^ 5 <= A[i] <= 10 ^ 5
1 <= K <= 10 ^ 9
|
from collections import deque
class Solution:
def shortestSubarray(self, A: List[int], K: int) -> int:
n=len(A)
prefix_sum= [0 for _ in range(n+1)]
for i in range(1,len(prefix_sum)):
prefix_sum[i] = prefix_sum[i-1]+ A[i-1]
dq= deque()
ans=n+1
for i in range(len(prefix_sum)):
while dq and prefix_sum[i]-prefix_sum[dq[0]]>=K:
ans = min(ans,i-dq[0])
dq.popleft()
while dq and prefix_sum[i]<=prefix_sum[dq[-1]]:
dq.pop()
dq.append(i)
return -1 if ans==n+1 else ans
|
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K.
If there is no non-empty subarray with sum at least K, return -1.
Example 1:
Input: A = [1], K = 1
Output: 1
Example 2:
Input: A = [1,2], K = 4
Output: -1
Example 3:
Input: A = [2,-1,2], K = 3
Output: 3
Note:
1 <= A.length <= 50000
-10 ^ 5 <= A[i] <= 10 ^ 5
1 <= K <= 10 ^ 9
|
from collections import deque
class Solution:
def shortestSubarray(self, A: List[int], K: int) -> int:
prefix_arr = [0]
for i in range(len(A)):
prefix_arr.append(prefix_arr[i] + A[i])
deq = deque()
result = len(A) + 1
for i in range(len(prefix_arr)):
while len(deq) > 0 and prefix_arr[i] < prefix_arr[deq[-1]]:
deq.pop()
deq.append(i)
while len(deq) > 0 and prefix_arr[deq[-1]] - prefix_arr[deq[0]] >= K:
result = min(result, deq[-1]-deq[0])
deq.popleft()
if result == len(A) + 1:
return -1
return result
|
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K.
If there is no non-empty subarray with sum at least K, return -1.
Example 1:
Input: A = [1], K = 1
Output: 1
Example 2:
Input: A = [1,2], K = 4
Output: -1
Example 3:
Input: A = [2,-1,2], K = 3
Output: 3
Note:
1 <= A.length <= 50000
-10 ^ 5 <= A[i] <= 10 ^ 5
1 <= K <= 10 ^ 9
|
import heapq
class Solution:
def shortestSubarray(self, A: List[int], K: int) -> int:
q = [(0, -1)]
cum = 0
res = float('inf')
for i, x in enumerate(A):
cum += x
while q and cum - q[0][0] >= K:
res = min(res, i - heapq.heappop(q)[1])
heapq.heappush(q, (cum, i))
return res if res < float('inf') else -1
|
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K.
If there is no non-empty subarray with sum at least K, return -1.
Example 1:
Input: A = [1], K = 1
Output: 1
Example 2:
Input: A = [1,2], K = 4
Output: -1
Example 3:
Input: A = [2,-1,2], K = 3
Output: 3
Note:
1 <= A.length <= 50000
-10 ^ 5 <= A[i] <= 10 ^ 5
1 <= K <= 10 ^ 9
|
class Solution:
def shortestSubarray(self, A: List[int], K: int) -> int:
Sum, res, heap, flag = 0, len(A), [(0, -1)], False
for i, v in enumerate(A):
Sum += v
while heap and Sum - heap[0][0] >= K:
flag = True
res = min(res, i - heapq.heappop(heap)[1])
heapq.heappush(heap, (Sum, i))
return res if flag else -1
|
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K.
If there is no non-empty subarray with sum at least K, return -1.
Example 1:
Input: A = [1], K = 1
Output: 1
Example 2:
Input: A = [1,2], K = 4
Output: -1
Example 3:
Input: A = [2,-1,2], K = 3
Output: 3
Note:
1 <= A.length <= 50000
-10 ^ 5 <= A[i] <= 10 ^ 5
1 <= K <= 10 ^ 9
|
from heapq import heappush, heappop
def kmin(a,k):
cur_sum = 0
h = [(0, -1)]
min_ = float('inf')
for i in range(0, len(a)):
cur_sum +=a[i]
while h and h[0][0]<=cur_sum-k:
min_ = min(min_, i-h[0][1])
heappop(h)
heappush(h, (cur_sum, i))
return min_ if min_ != float('inf') else -1
class Solution:
def shortestSubarray(self, A: List[int], K: int) -> int:
return kmin(A, K)
|
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K.
If there is no non-empty subarray with sum at least K, return -1.
Example 1:
Input: A = [1], K = 1
Output: 1
Example 2:
Input: A = [1,2], K = 4
Output: -1
Example 3:
Input: A = [2,-1,2], K = 3
Output: 3
Note:
1 <= A.length <= 50000
-10 ^ 5 <= A[i] <= 10 ^ 5
1 <= K <= 10 ^ 9
|
class Solution:
def shortestSubarray(self, A: List[int], K: int) -> int:
queue = collections.deque()
Sum, res = 0, float('inf')
queue.append((0, -1))
for i, v in enumerate(A):
Sum += v
while queue and Sum - queue[0][0] >= K:
res = min(res, i - queue.popleft()[1])
while queue and Sum <= queue[-1][0]:
queue.pop()
queue.append((Sum, i))
return res if res < float('inf') else -1
|
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K.
If there is no non-empty subarray with sum at least K, return -1.
Example 1:
Input: A = [1], K = 1
Output: 1
Example 2:
Input: A = [1,2], K = 4
Output: -1
Example 3:
Input: A = [2,-1,2], K = 3
Output: 3
Note:
1 <= A.length <= 50000
-10 ^ 5 <= A[i] <= 10 ^ 5
1 <= K <= 10 ^ 9
|
class Solution:
def shortestSubarray(self, A: List[int], K: int) -> int:
cumsum = itertools.accumulate(A)
mono = collections.deque([(0, -1)])
result = math.inf
for ind, val in enumerate(cumsum):
while mono and mono[-1][0] >= val:
mono.pop()
mono.append((val, ind))
while val - mono[0][0] >= K:
result = min(result, ind - mono.popleft()[1])
if result == math.inf:
return -1
else:
return result
|
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K.
If there is no non-empty subarray with sum at least K, return -1.
Example 1:
Input: A = [1], K = 1
Output: 1
Example 2:
Input: A = [1,2], K = 4
Output: -1
Example 3:
Input: A = [2,-1,2], K = 3
Output: 3
Note:
1 <= A.length <= 50000
-10 ^ 5 <= A[i] <= 10 ^ 5
1 <= K <= 10 ^ 9
|
from collections import deque
class Solution:
def shortestSubarray(self, A: List[int], K: int) -> int:
psum = [0]
for a in A:
psum.append(psum[-1] + a)
window = deque([(0, 0)])
res = float('inf')
for right in range(len(A)):
while window and psum[right + 1] <= window[-1][1]:
window.pop()
window.append((right + 1, psum[right + 1]))
while window and window[-1][1] - window[0][1] >= K:
res = min(res, window[-1][0] - window[0][0])
window.popleft()
return res if res != float('inf') else -1
|
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K.
If there is no non-empty subarray with sum at least K, return -1.
Example 1:
Input: A = [1], K = 1
Output: 1
Example 2:
Input: A = [1,2], K = 4
Output: -1
Example 3:
Input: A = [2,-1,2], K = 3
Output: 3
Note:
1 <= A.length <= 50000
-10 ^ 5 <= A[i] <= 10 ^ 5
1 <= K <= 10 ^ 9
|
class Solution:
def shortestSubarray(self, A: List[int], K: int) -> int:
# Step 1: create a prefix (left) sum for the array
n = len(A)
if n == 0:
return -1
dp = [0] * (n + 1) # dp[i] is the sum of A[0: i]
for i in range(1, n+1):
dp[i] = dp[i-1] + A[i-1]
# Step 2: iterate over a monotonic (increasing) queue, whenever a new
# prefix sum is >= most left ele + K, left pop
queue = collections.deque()
queue.append((0, 0)) # First ele is the index, second ele in the dop array
# The minimum of the sequence length
res = (n + 1)
for i in range(1, n+1):
# Check if dp[i] minus the left end is no less than K
while queue:
qi, qe = queue[0]
if qe + K <= dp[i]:
res = min(res, i - qi)
queue.popleft()
else:
break
# Update the monotonic queue
while queue:
_, qe = queue[-1]
if qe >= dp[i]:
queue.pop()
else:
break
queue.append((i, dp[i]))
if res == (n+1):
return -1
else:
return res
|
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K.
If there is no non-empty subarray with sum at least K, return -1.
Example 1:
Input: A = [1], K = 1
Output: 1
Example 2:
Input: A = [1,2], K = 4
Output: -1
Example 3:
Input: A = [2,-1,2], K = 3
Output: 3
Note:
1 <= A.length <= 50000
-10 ^ 5 <= A[i] <= 10 ^ 5
1 <= K <= 10 ^ 9
|
class Solution:
def shortestSubarray(self, A: List[int], k: int) -> int:
s = collections.deque() # Inc # (sum, ind)
curr = 0
ans = math.inf
for i in range(len(A)):
curr += A[i]
if curr>=k: ans=min(ans, i+1)
while s and curr - s[0][0]>=k:
ans = min(ans, i-s.popleft()[1])
while s and s[-1][0]>curr:
s.pop()
s.append((curr, i))
return ans if ans!=float('inf') else -1
|
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K.
If there is no non-empty subarray with sum at least K, return -1.
Example 1:
Input: A = [1], K = 1
Output: 1
Example 2:
Input: A = [1,2], K = 4
Output: -1
Example 3:
Input: A = [2,-1,2], K = 3
Output: 3
Note:
1 <= A.length <= 50000
-10 ^ 5 <= A[i] <= 10 ^ 5
1 <= K <= 10 ^ 9
|
class Solution:
def shortestSubarray(self, A: List[int], K: int) -> int:
dq = deque([(0,0)])
res = float('inf')
p = 0
for i, a in enumerate(A):
p += a
while dq and p<=dq[-1][0]:
dq.pop()
while dq and p-dq[0][0]>=K:
res = min(res, i+1-dq.popleft()[1])
dq.append((p, i+1))
return res if res!=float('inf') else -1
|
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K.
If there is no non-empty subarray with sum at least K, return -1.
Example 1:
Input: A = [1], K = 1
Output: 1
Example 2:
Input: A = [1,2], K = 4
Output: -1
Example 3:
Input: A = [2,-1,2], K = 3
Output: 3
Note:
1 <= A.length <= 50000
-10 ^ 5 <= A[i] <= 10 ^ 5
1 <= K <= 10 ^ 9
|
from collections import deque
class Solution:
def shortestSubarray(self, A: List[int], k: int) -> int:
for ele in A:
if ele>=k:
return 1
s=[0]
for i in range(len(A)):
s.append((s[-1] if len(s) else 0)+A[i])
print(s)
queue=deque()
#queue.append((0,0))
ans=float('inf')
for i,e in enumerate(s):
#print(queue)
if len(queue)==0:
queue.append((i,e))
elif len(queue) and queue[-1][1]<e:
if e-queue[0][1]>=k:
while len(queue) and e-queue[0][1]>=k:
ans=min(ans,i-queue[0][0])
queue.popleft()
queue.append((i,e))
elif len(queue) and queue[-1][1]>=e:
while len(queue) and queue[-1][1]>=e:
queue.pop()
queue.append((i,e))
if len(queue)>1 and queue[-1][1]>=queue[0][1]+k:
ans=min(ans,i-queue[-1][0])
return -1 if ans==float('inf') else ans
|
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K.
If there is no non-empty subarray with sum at least K, return -1.
Example 1:
Input: A = [1], K = 1
Output: 1
Example 2:
Input: A = [1,2], K = 4
Output: -1
Example 3:
Input: A = [2,-1,2], K = 3
Output: 3
Note:
1 <= A.length <= 50000
-10 ^ 5 <= A[i] <= 10 ^ 5
1 <= K <= 10 ^ 9
|
class Solution:
def shortestSubarray(self, A: List[int], K: int) -> int:
Sum, res, heap, flag = 0, len(A), [(0, -1)], False
for i, v in enumerate(A):
Sum += v
while heap and Sum - heap[0][0] >= K:
flag = True
res = min(res, i - heapq.heappop(heap)[1])
heapq.heappush(heap, (Sum, i))
return res if flag else -1
|
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K.
If there is no non-empty subarray with sum at least K, return -1.
Example 1:
Input: A = [1], K = 1
Output: 1
Example 2:
Input: A = [1,2], K = 4
Output: -1
Example 3:
Input: A = [2,-1,2], K = 3
Output: 3
Note:
1 <= A.length <= 50000
-10 ^ 5 <= A[i] <= 10 ^ 5
1 <= K <= 10 ^ 9
|
class Solution:
def shortestSubarray(self, A: List[int], K: int) -> int:
queue = collections.deque([(0, 0)])
cur_sum = 0
res = len(A) +1
for right, val in enumerate(A):
cur_sum += val
while queue and cur_sum - queue[0][1] >= K:
res = min(res, right+1-queue.popleft()[0])
while queue and queue[-1][1] >= cur_sum:
queue.pop()
queue.append((right+1, cur_sum))
if res <= len(A):
return res
return -1
|
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K.
If there is no non-empty subarray with sum at least K, return -1.
Example 1:
Input: A = [1], K = 1
Output: 1
Example 2:
Input: A = [1,2], K = 4
Output: -1
Example 3:
Input: A = [2,-1,2], K = 3
Output: 3
Note:
1 <= A.length <= 50000
-10 ^ 5 <= A[i] <= 10 ^ 5
1 <= K <= 10 ^ 9
|
class Solution:
def shortestSubarray(self, A: List[int], K: int) -> int:
curr_sum = 0
s_sum = []
s_ind = []
res = math.inf
for i, num in enumerate(A):
curr_sum += num
while s_sum and s_sum[-1] >= curr_sum:
s_sum.pop()
s_ind.pop()
if not s_sum:
if curr_sum >= K:
res = min(res, i + 1)
else:
ind = bisect_right(s_sum, curr_sum - K)
if ind - 1 >= 0:
res = min(res, i - s_ind[ind - 1])
elif curr_sum >= K:
res = min(res, i + 1)
s_sum.append(curr_sum)
s_ind.append(i)
return -1 if math.isinf(res) else res
|
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K.
If there is no non-empty subarray with sum at least K, return -1.
Example 1:
Input: A = [1], K = 1
Output: 1
Example 2:
Input: A = [1,2], K = 4
Output: -1
Example 3:
Input: A = [2,-1,2], K = 3
Output: 3
Note:
1 <= A.length <= 50000
-10 ^ 5 <= A[i] <= 10 ^ 5
1 <= K <= 10 ^ 9
|
import heapq
class Solution:
def shortestSubarray(self, A: List[int], K: int) -> int:
prefix = 0
pq = [[0, -1]]
res = float('inf')
for i, a in enumerate(A):
prefix += a
while pq and prefix - pq[0][0] >= K:
res = min(res, i - heapq.heappop(pq)[1])
heapq.heappush(pq, [prefix, i])
return res if res < float('inf') else -1
# prefix = [0]
# for a in A:
# prefix.append(prefix[-1] + a)
# res = float('inf')
# deq = collections.deque()
# prefix = [0]
# for a in A:
# prefix.append(prefix[-1] + a)
# res = float('inf')
# deq = collections.deque()
# for i, x in enumerate(prefix):
# while deq and prefix[deq[-1]] >= x:
# deq.pop()
# while deq and prefix[deq[0]] <= x - K:
# res = min(res, i - deq.popleft())
# deq.append(i)
# return res if res < float('inf') else -1
|
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K.
If there is no non-empty subarray with sum at least K, return -1.
Example 1:
Input: A = [1], K = 1
Output: 1
Example 2:
Input: A = [1,2], K = 4
Output: -1
Example 3:
Input: A = [2,-1,2], K = 3
Output: 3
Note:
1 <= A.length <= 50000
-10 ^ 5 <= A[i] <= 10 ^ 5
1 <= K <= 10 ^ 9
|
class Solution:
def shortestSubarray(self, A: List[int], K: int) -> int:
prefix = [0] * (len(A)+1)
for i in range(len(A)):
prefix[i+1] = A[i] + prefix[i]
ans = len(A) + 1
monoq = deque()
for i in range(len(prefix)):
while monoq and prefix[i] <= prefix[monoq[-1]]:
monoq.pop()
while monoq and prefix[i] - prefix[monoq[0]]>=K:
ans = min(ans, i-monoq.popleft())
monoq.append(i)
return ans if ans<len(A)+1 else -1
|
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K.
If there is no non-empty subarray with sum at least K, return -1.
Example 1:
Input: A = [1], K = 1
Output: 1
Example 2:
Input: A = [1,2], K = 4
Output: -1
Example 3:
Input: A = [2,-1,2], K = 3
Output: 3
Note:
1 <= A.length <= 50000
-10 ^ 5 <= A[i] <= 10 ^ 5
1 <= K <= 10 ^ 9
|
class Solution:
def shortestSubarray(self, A: List[int], K: int) -> int:
running = [0]
for num in A:
running.append(running[-1] + num)
result = math.inf
deque = collections.deque([0]) # left idx
r = 1
while r < len(running):
if not deque:
deque.append(r)
r += 1
continue
while deque and r < len(running) and running[r] - running[deque[0]] < K:
#pop the last of the queue if it is smaller to maintain mono increasing queue
while deque and running[r] <= running[deque[-1]]:
deque.pop()
deque.append(r)
r += 1
# right reached the end yet still not satisfied
if r == len(running):
break
while deque and running[r] - running[deque[0]] >= K:
l = deque.popleft()
result = min(result, r-l)
# r += 1
return result if result != math.inf else -1
'''
#pop the last of the queue if it is smaller to maintain mono increasing queue
while deque and running[r] <= running[deque[-1]]:
deque.pop()
l = 0
r = 1
while r < len(running):
while r < len(running) and running[r] - running[l] < K:
r += 1
while l < r and running[r] - running[l+1] >= K:
l += 1
result = min(result, r-l)
r += 1
l += 1
return result
while r < len(running):
while deque and running[r] - running[deque[0]] >= K:
result = min(result, i-deque[0])
deque.popleft()
while deque and running[r] <= running[deque[-1]]:
deque.pop()
deque.append(r)
return result if result != math.inf else -1
while r < len(running)-1 and running[r] - running[l] < s:
r += 1
while l < r and running[r] - running[l] >= s:
result = min(result, r-l)
l += 1
r += 1
'''
|
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K.
If there is no non-empty subarray with sum at least K, return -1.
Example 1:
Input: A = [1], K = 1
Output: 1
Example 2:
Input: A = [1,2], K = 4
Output: -1
Example 3:
Input: A = [2,-1,2], K = 3
Output: 3
Note:
1 <= A.length <= 50000
-10 ^ 5 <= A[i] <= 10 ^ 5
1 <= K <= 10 ^ 9
|
import bisect
class Solution:
def shortestSubarray(self, A: List[int], K: int) -> int:
cur, stack, idx, ans = 0, [0], [-1], float('inf')
for i, x in enumerate(A):
cur += x
while len(stack) > 0 and stack[-1] >= cur:
stack.pop()
idx.pop()
j = bisect.bisect_right(stack, cur - K)
if j > 0:
ans = min(ans, i - idx[j - 1])
stack.append(cur)
idx.append(i)
if ans == float('inf'):
return -1
return ans
|
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K.
If there is no non-empty subarray with sum at least K, return -1.
Example 1:
Input: A = [1], K = 1
Output: 1
Example 2:
Input: A = [1,2], K = 4
Output: -1
Example 3:
Input: A = [2,-1,2], K = 3
Output: 3
Note:
1 <= A.length <= 50000
-10 ^ 5 <= A[i] <= 10 ^ 5
1 <= K <= 10 ^ 9
|
class Solution:
def shortestSubarray(self, A, K):
N, acc = len(A), [0]
for v in A:
acc.append(acc[-1] + v)
ans, monoq = float('inf'), deque()
for size, curS in enumerate(acc):
while monoq and curS <= acc[monoq[-1]]:
monoq.pop()
while monoq and curS - acc[monoq[0]] >= K:
ans = min(ans, size - monoq.popleft())
monoq.append(size)
return ans if ans != float('inf') else -1
from heapq import *
class Solution:
def shortestSubarray(self, A, K):
heap, curS, ans = [(0, -1)], 0, float('inf')
for i, v in enumerate(A):
curS += v
while heap and curS - heap[0][0] >= K:
ans = min(ans, i - heappop(heap)[1])
heappush(heap, (curS, i))
return ans if ans != float('inf') else -1
|
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K.
If there is no non-empty subarray with sum at least K, return -1.
Example 1:
Input: A = [1], K = 1
Output: 1
Example 2:
Input: A = [1,2], K = 4
Output: -1
Example 3:
Input: A = [2,-1,2], K = 3
Output: 3
Note:
1 <= A.length <= 50000
-10 ^ 5 <= A[i] <= 10 ^ 5
1 <= K <= 10 ^ 9
|
class Solution:
def shortestSubarray(self, A: List[int], K: int) -> int:
'''
idea 1: sliding window:
r
[2, -1, 2, 3] 4
l.
if cur_sum > k -> move l otherwise move r
'''
P = [0]
for x in A:
P.append(P[-1] + x)
# the largest i for j such that A[j] - A[i] >= k
# [2, -1, 2, 3] -> [0, 2, 1, 3, 5]
# [0, 1, 3, 5]
#
ans = len(A)+1
dq = collections.deque([])
for i, y in enumerate(P):
while dq and y < P[dq[-1]]:
dq.pop()
while dq and y - P[dq[0]] >= K:
ans = min(ans, i - dq.popleft())
dq.append(i)
return ans if ans < len(A)+1 else -1
|
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K.
If there is no non-empty subarray with sum at least K, return -1.
Example 1:
Input: A = [1], K = 1
Output: 1
Example 2:
Input: A = [1,2], K = 4
Output: -1
Example 3:
Input: A = [2,-1,2], K = 3
Output: 3
Note:
1 <= A.length <= 50000
-10 ^ 5 <= A[i] <= 10 ^ 5
1 <= K <= 10 ^ 9
|
class Solution:
def shortestSubarray(self, A: List[int], K: int) -> int:
if not A:
return -1
if len(A) == 1:
if A[0] < K:
return -1
return 1
min_length = float('inf')
temp = [0]
for i in A:
temp.append(i + temp[-1])
queue = []
for c, v in enumerate(temp):
while queue and v <= temp[queue[-1]]:
queue.pop()
while queue and v-temp[queue[0]] >= K:
min_length = min(min_length, c-queue.pop(0))
queue.append(c)
if min_length == float('inf'):
return -1
return min_length
|
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K.
If there is no non-empty subarray with sum at least K, return -1.
Example 1:
Input: A = [1], K = 1
Output: 1
Example 2:
Input: A = [1,2], K = 4
Output: -1
Example 3:
Input: A = [2,-1,2], K = 3
Output: 3
Note:
1 <= A.length <= 50000
-10 ^ 5 <= A[i] <= 10 ^ 5
1 <= K <= 10 ^ 9
|
class Solution:
def shortestSubarray(self, A: List[int], K: int) -> int:
# algorihtm
# find the minimum length of the subarray, where there sum >= k
# when dealing sum, sum prifix?
# sliding window -classis? O(N^2)
# holding a deque ?yes
# decreasing or increasing?
# decresaing? X no need to save the one before ==> choose increasing
# save number or index??? we need to i - index, so save index in deque
# when add / delete our dequeue:
# add ==> in for loop
# delete ==> sum_[i] <= sum_[queue[-1]], pop()
# sum_[i] - sum_[deque[0]] >= K, pop(0)
# deque
sum_ = [0]
for num in A:
sum_.append(sum_[-1] + num)
result = len(A)+1
deque = []
for i,num in enumerate(sum_):
while(deque and num <= sum_[deque[-1]]):
deque.pop()
while(deque and num - sum_[deque[0]] >= K):
result = min(result, i - deque[0])
deque.pop(0)
deque.append(i)
return result if result != len(A)+1 else -1
|
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K.
If there is no non-empty subarray with sum at least K, return -1.
Example 1:
Input: A = [1], K = 1
Output: 1
Example 2:
Input: A = [1,2], K = 4
Output: -1
Example 3:
Input: A = [2,-1,2], K = 3
Output: 3
Note:
1 <= A.length <= 50000
-10 ^ 5 <= A[i] <= 10 ^ 5
1 <= K <= 10 ^ 9
|
class Solution:
def shortestSubarray(self, A: List[int], K: int) -> int:
Sum, res, heap, flag = 0, len(A), [(0, -1)], False
for i, v in enumerate(A):
Sum += v
while heap and Sum - K >= heap[0][0]:
flag = True
res = min(res, i - heapq.heappop(heap)[1])
heapq.heappush(heap, (Sum, i))
return res if flag else -1
|
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K.
If there is no non-empty subarray with sum at least K, return -1.
Example 1:
Input: A = [1], K = 1
Output: 1
Example 2:
Input: A = [1,2], K = 4
Output: -1
Example 3:
Input: A = [2,-1,2], K = 3
Output: 3
Note:
1 <= A.length <= 50000
-10 ^ 5 <= A[i] <= 10 ^ 5
1 <= K <= 10 ^ 9
|
class Solution:
def shortestSubarray(self, A: List[int], K: int) -> int:
N = len(A)
P = [0]
for x in A:
P.append(P[-1] + x)
ans = N+1 # N+1 is impossible
monoq = [] #opt(y) candidates, represented as indices of P
for y, Py in enumerate(P):
#Want opt(y) = largest x with Px <= Py - K
while monoq and Py <= P[monoq[-1]]:
monoq.pop()
while monoq and Py - P[monoq[0]] >= K:
ans = min(ans, y - monoq.pop(0))
monoq.append(y)
return ans if ans < N+1 else -1
|
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K.
If there is no non-empty subarray with sum at least K, return -1.
Example 1:
Input: A = [1], K = 1
Output: 1
Example 2:
Input: A = [1,2], K = 4
Output: -1
Example 3:
Input: A = [2,-1,2], K = 3
Output: 3
Note:
1 <= A.length <= 50000
-10 ^ 5 <= A[i] <= 10 ^ 5
1 <= K <= 10 ^ 9
|
class Solution:
def shortestSubarray(self, A, K):
from heapq import heappush,heapify,heappop
heap = []
length = float('inf')
summation = 0
heapify(heap)
heappush(heap,[0,-1])
for i,number in enumerate(A):
summation += number
difference = summation - K
while heap and (heap[0][0] <=difference or i - heap[0][1] >= length):
previous_summation , previous_index = heappop(heap)
length = min(length,i - previous_index)
heappush(heap,[summation,i])
return length if length != float('inf') else -1
|
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K.
If there is no non-empty subarray with sum at least K, return -1.
Example 1:
Input: A = [1], K = 1
Output: 1
Example 2:
Input: A = [1,2], K = 4
Output: -1
Example 3:
Input: A = [2,-1,2], K = 3
Output: 3
Note:
1 <= A.length <= 50000
-10 ^ 5 <= A[i] <= 10 ^ 5
1 <= K <= 10 ^ 9
|
class Solution:
def shortestSubarray(self, nums: List[int], k: int) -> int:
nums = [0] + nums
deque = [ (0,0) ]
cur = 0
res = 1+len(nums)
for i in range(1,len(nums)):
#print(deque)
cur += nums[i]
while deque and cur-deque[0][1]>=k :
res = min(res,i-deque[0][0])
deque.pop(0)
while deque and deque[-1][1]>=cur:
deque.pop(-1)
deque.append( (i,cur) )
if res>len(nums):
return -1
return res
|
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K.
If there is no non-empty subarray with sum at least K, return -1.
Example 1:
Input: A = [1], K = 1
Output: 1
Example 2:
Input: A = [1,2], K = 4
Output: -1
Example 3:
Input: A = [2,-1,2], K = 3
Output: 3
Note:
1 <= A.length <= 50000
-10 ^ 5 <= A[i] <= 10 ^ 5
1 <= K <= 10 ^ 9
|
class Solution:
def shortestSubarray(self, A: List[int], K: int) -> int:
if len(A) == 1:
return 1 if A[0] <= K else -1
min_len = len(A) + 1
dq = []
pref = [0]
for i in range(len(A)):
pref.append(pref[- 1] + A[i])
for i in range(len(pref)):
# print(dq)
while dq and pref[i] <= pref[dq[-1]]:
dq.pop(-1)
while dq and pref[i] - pref[dq[0]] >= K:
min_len = min(min_len, i - dq[0])
dq.pop(0)
dq.append(i)
return min_len if min_len < len(A) + 1 else -1
|
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K.
If there is no non-empty subarray with sum at least K, return -1.
Example 1:
Input: A = [1], K = 1
Output: 1
Example 2:
Input: A = [1,2], K = 4
Output: -1
Example 3:
Input: A = [2,-1,2], K = 3
Output: 3
Note:
1 <= A.length <= 50000
-10 ^ 5 <= A[i] <= 10 ^ 5
1 <= K <= 10 ^ 9
|
class Solution:
def shortestSubarray(self, A: List[int], K: int) -> int:
N = len(A)
P = [0]
for x in A:
P.append(P[-1] + x)
#Want smallest y-x with Py - Px >= K
ans = N+1 # N+1 is impossible
monoq = collections.deque() #opt(y) candidates, represented as indices of P
for y, Py in enumerate(P):
#Want opt(y) = largest x with Px <= Py - K
while monoq and Py <= P[monoq[-1]]:
monoq.pop()
while monoq and Py - P[monoq[0]] >= K:
ans = min(ans, y - monoq.popleft())
monoq.append(y)
return ans if ans < N+1 else -1
|
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K.
If there is no non-empty subarray with sum at least K, return -1.
Example 1:
Input: A = [1], K = 1
Output: 1
Example 2:
Input: A = [1,2], K = 4
Output: -1
Example 3:
Input: A = [2,-1,2], K = 3
Output: 3
Note:
1 <= A.length <= 50000
-10 ^ 5 <= A[i] <= 10 ^ 5
1 <= K <= 10 ^ 9
|
from collections import deque
class Solution:
def shortestSubarray(self, A, K):
possible_starts = deque([0])
prefix_sums = [0 for i in range(len(A) + 1)]
for i in range(len(A)):
prefix_sums[i + 1] = prefix_sums[i] + A[i]
shortest_found = len(A) + 1
curr_sum = 0
for i in range(len(A) + 1):
while len(possible_starts) > 0 and prefix_sums[i] - prefix_sums[possible_starts[0]] >= K:
shortest_found = min(shortest_found, i - possible_starts.popleft())
while len(possible_starts) > 0 and prefix_sums[i] <= prefix_sums[possible_starts[-1]]:
possible_starts.pop()
possible_starts.append(i)
return shortest_found if shortest_found <= len(A) else -1
|
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K.
If there is no non-empty subarray with sum at least K, return -1.
Example 1:
Input: A = [1], K = 1
Output: 1
Example 2:
Input: A = [1,2], K = 4
Output: -1
Example 3:
Input: A = [2,-1,2], K = 3
Output: 3
Note:
1 <= A.length <= 50000
-10 ^ 5 <= A[i] <= 10 ^ 5
1 <= K <= 10 ^ 9
|
import heapq
class Solution:
def shortestSubarray(self, A: List[int], K: int) -> int:
'''
idea 1: sliding window:
r
[2, -1, 2, 3] 4
l.
if cur_sum > k -> move l otherwise move r
longest?
[0, 2, -3, 5] 5
'''
P = [0]
for x in A:
P.append(P[-1] + x)
# the largest i for j such that A[j] - A[i] >= k
# [2, -1, 2, 3] -> [0, 2, 1, 3, 5] k =3
# [0, 1 ]
# [0, 1, 3, 5]
# [0, 2, 1, 3, 5]
# [1, 3]
#
'''
heapq[1, 2, 3]
'''
heap = []
min_l = len(A)+1
for i, num in enumerate(P):
while heap and num - heap[0][0] >= K:
v , idx = heapq.heappop(heap)
min_l = min(min_l, i-idx)
heapq.heappush(heap, (num, i))
return min_l if min_l < len(A)+1 else -1
# ans = len(A)+1
# dq = collections.deque([])
# for i, y in enumerate(P):
# while dq and y < P[dq[-1]]:
# dq.pop()
# while dq and y - P[dq[0]] >= K:
# ans = min(ans, i - dq.popleft())
# dq.append(i)
# return ans if ans < len(A)+1 else -1
|
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K.
If there is no non-empty subarray with sum at least K, return -1.
Example 1:
Input: A = [1], K = 1
Output: 1
Example 2:
Input: A = [1,2], K = 4
Output: -1
Example 3:
Input: A = [2,-1,2], K = 3
Output: 3
Note:
1 <= A.length <= 50000
-10 ^ 5 <= A[i] <= 10 ^ 5
1 <= K <= 10 ^ 9
|
class Solution:
def shortestSubarray(self, A: List[int], K: int) -> int:
mx = sys.maxsize
if A[0] == K:
return 1
for i in range(1, len(A)):
A[i] += A[i-1]
if A[i] >= K:
mx = min(mx, i+1)
st = [0]
for i in range(1, len(A)):
while len(st) and A[i] - A[st[0]] >= K:
popped = st.pop(0)
mx = min(mx, i - popped)
while len(st) and A[i] <= A[st[-1]]:
st.pop()
st.append(i)
return mx if mx != sys.maxsize else -1
'''
d = []
o = []
res, cur = float('inf'), 0
for i, a in enumerate(A):
cur += a
o.append(cur)
while d and cur - d[0][1] >= K:
res = min(res, i - d.pop(0)[0])
while d and cur <= d[-1][1]:
d.pop()
d.append([i, cur])
print(o)
return res if res < float('inf') else -1
'''
|
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K.
If there is no non-empty subarray with sum at least K, return -1.
Example 1:
Input: A = [1], K = 1
Output: 1
Example 2:
Input: A = [1,2], K = 4
Output: -1
Example 3:
Input: A = [2,-1,2], K = 3
Output: 3
Note:
1 <= A.length <= 50000
-10 ^ 5 <= A[i] <= 10 ^ 5
1 <= K <= 10 ^ 9
|
class Solution:
def shortestSubarray(self, A: List[int], K: int) -> int:
d = [[0, 0]]
res, cur = float('inf'), 0
for i, a in enumerate(A):
cur += a
while d and cur - d[0][1] >= K:
res = min(res, i-d.pop(0)[0]+1)
while d and cur <= d[-1][1]:
d.pop()
d.append([i+1, cur])
return res if res < float('inf') else -1
|
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K.
If there is no non-empty subarray with sum at least K, return -1.
Example 1:
Input: A = [1], K = 1
Output: 1
Example 2:
Input: A = [1,2], K = 4
Output: -1
Example 3:
Input: A = [2,-1,2], K = 3
Output: 3
Note:
1 <= A.length <= 50000
-10 ^ 5 <= A[i] <= 10 ^ 5
1 <= K <= 10 ^ 9
|
from collections import deque
class Solution:
def shortestSubarray(self, A: List[int], K: int) -> int:
# prefix_sums[i] == sum(A[:i - 1])
prefix_sums = [0]
for num in A:
prefix_sums.append(prefix_sums[-1] + num)
shortest_length = math.inf
# a deque of indices i_1, ..., i_k s.t. P[i_1] <= P[i_2] ...
cands = deque()
for i, prefix_sum in enumerate(prefix_sums):
while len(cands) > 0 and prefix_sum <= prefix_sums[cands[-1]]:
cands.pop()
while len(cands) > 0 and prefix_sum - prefix_sums[cands[0]] >= K:
shortest_length = min(shortest_length, i - cands.popleft())
cands.append(i)
return shortest_length if shortest_length != math.inf else -1
|
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K.
If there is no non-empty subarray with sum at least K, return -1.
Example 1:
Input: A = [1], K = 1
Output: 1
Example 2:
Input: A = [1,2], K = 4
Output: -1
Example 3:
Input: A = [2,-1,2], K = 3
Output: 3
Note:
1 <= A.length <= 50000
-10 ^ 5 <= A[i] <= 10 ^ 5
1 <= K <= 10 ^ 9
|
class Solution:
def shortestSubarray(self, A: List[int], K: int) -> int:
prefixSum = [0]*(len(A)+1)
for i in range(1,len(prefixSum)):
prefixSum[i] = prefixSum[i-1]+A[i-1]
dq = deque()
shortestLen = sys.maxsize
for i in range(len(prefixSum)):
while dq and prefixSum[dq[-1]] >= prefixSum[i]:
dq.pop()
while dq and prefixSum[i]-prefixSum[dq[0]] >= K:
shortestLen = min(shortestLen,i-dq[0])
dq.popleft()
dq.append(i)
return shortestLen if shortestLen != sys.maxsize else -1
|
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K.
If there is no non-empty subarray with sum at least K, return -1.
Example 1:
Input: A = [1], K = 1
Output: 1
Example 2:
Input: A = [1,2], K = 4
Output: -1
Example 3:
Input: A = [2,-1,2], K = 3
Output: 3
Note:
1 <= A.length <= 50000
-10 ^ 5 <= A[i] <= 10 ^ 5
1 <= K <= 10 ^ 9
|
from collections import deque
class Solution:
def shortestSubarray(self, A: List[int], K: int) -> int:
n = len(A)
B = [0 for _ in range(n+1)]
for i in range(n):
B[i+1] = A[i] + B[i]
res = n+1
d = deque()
for j in range(n+1):
while d and B[j] - B[d[0]] >= K:
res = min(res, j - d[0])
d.popleft()
while d and B[d[-1]] > B[j]:
d.pop()
d.append(j)
return res if res <= n else -1
|
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K.
If there is no non-empty subarray with sum at least K, return -1.
Example 1:
Input: A = [1], K = 1
Output: 1
Example 2:
Input: A = [1,2], K = 4
Output: -1
Example 3:
Input: A = [2,-1,2], K = 3
Output: 3
Note:
1 <= A.length <= 50000
-10 ^ 5 <= A[i] <= 10 ^ 5
1 <= K <= 10 ^ 9
|
class Solution:
def shortestSubarray(self, A: List[int], K: int) -> int:
p = [0]
p[0] = A[0]
for i in range(1,len(A)):
p.append(p[i-1] + A[i])
s = []
s.append((-1,0))
min_len = float('inf')
for i in range(len(p)):
while s and p[i] - s[0][1] >= K:
min_len = min(min_len, i - s[0][0])
s.pop(0)
while s and p[i] <= s[-1][1]:
s.pop()
s.append((i,p[i]))
if min_len != float('inf'):
return min_len
else:
return -1
|
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K.
If there is no non-empty subarray with sum at least K, return -1.
Example 1:
Input: A = [1], K = 1
Output: 1
Example 2:
Input: A = [1,2], K = 4
Output: -1
Example 3:
Input: A = [2,-1,2], K = 3
Output: 3
Note:
1 <= A.length <= 50000
-10 ^ 5 <= A[i] <= 10 ^ 5
1 <= K <= 10 ^ 9
|
class Solution:
def shortestSubarray(self, A: List[int], K: int) -> int:
ans = 1 if A[0] >= K else sys.maxsize
for i in range(1, len(A)):
A[i] += A[i-1]
if A[i] >= K:
ans = min(ans, i+1)
st = [(A[0],0)]
for i in range(1, len(A)):
while len(st) and A[i] - st[0][0] >= K:
ans = min(ans, i - st[0][1])
st.pop(0)
while len(st) and A[i] <= st[-1][0]:
st.pop()
st.append((A[i], i))
return ans if ans != sys.maxsize else -1
|
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K.
If there is no non-empty subarray with sum at least K, return -1.
Example 1:
Input: A = [1], K = 1
Output: 1
Example 2:
Input: A = [1,2], K = 4
Output: -1
Example 3:
Input: A = [2,-1,2], K = 3
Output: 3
Note:
1 <= A.length <= 50000
-10 ^ 5 <= A[i] <= 10 ^ 5
1 <= K <= 10 ^ 9
|
class Solution:
def shortestSubarray(self, A: List[int], K: int) -> int:
psum = 0
total = 0
dq = deque([(0,-1)])
res = float('inf')
for i, a in enumerate(A):
psum += a
# print(dq, i, a, psum)
while dq and dq[-1][0]>psum:
dq.pop()
while dq and psum-dq[0][0]>=K:
res = min(res, i-dq.popleft()[1])
# print(dq, res)
dq.append((psum, i))
# print(dq)
return res if res!=float('inf') else -1
|
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K.
If there is no non-empty subarray with sum at least K, return -1.
Example 1:
Input: A = [1], K = 1
Output: 1
Example 2:
Input: A = [1,2], K = 4
Output: -1
Example 3:
Input: A = [2,-1,2], K = 3
Output: 3
Note:
1 <= A.length <= 50000
-10 ^ 5 <= A[i] <= 10 ^ 5
1 <= K <= 10 ^ 9
|
class Solution:
def shortestSubarray(self, A: List[int], K: int) -> int:
'''
monoqueue + prefixsum + sliding window
'''
n = len(A)
prefix_sum = [0] * (n + 1)
for i in range(1, n + 1):
prefix_sum[i] = prefix_sum[i - 1] + A[i - 1]
end = 0
res = float('inf')
mono_queue = collections.deque()
while end < n + 1:
while mono_queue and prefix_sum[mono_queue[-1]] >= prefix_sum[end]:
mono_queue.pop()
while mono_queue and prefix_sum[end] - prefix_sum[mono_queue[0]] >= K:
start_index = mono_queue.popleft()
res = min(res, end - start_index)
mono_queue.append(end)
end += 1
return res if res != float('inf') else -1
|
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K.
If there is no non-empty subarray with sum at least K, return -1.
Example 1:
Input: A = [1], K = 1
Output: 1
Example 2:
Input: A = [1,2], K = 4
Output: -1
Example 3:
Input: A = [2,-1,2], K = 3
Output: 3
Note:
1 <= A.length <= 50000
-10 ^ 5 <= A[i] <= 10 ^ 5
1 <= K <= 10 ^ 9
|
class Solution:
def shortestSubarray(self, A: List[int], K: int) -> int:
mx = sys.maxsize
if A[0] == K:
return 1
for i in range(1, len(A)):
A[i] += A[i-1]
if A[i] >= K:
mx = min(mx, i+1)
st = [0]
for i in range(1, len(A)):
while len(st) and A[i] - A[st[0]] >= K:
popped = st.pop(0)
mx = min(mx, i - popped)
while len(st) and A[i] <= A[st[-1]]:
st.pop()
st.append(i)
return mx if mx != sys.maxsize else -1
'''
d = []
o = []
res, cur = float('inf'), 0
for i, a in enumerate(A):
cur += a
o.append(cur)
while d and cur - d[0][1] >= K:
res = min(res, i - d.pop(0)[0])
while d and cur <= d[-1][1]:
d.pop()
d.append([i, cur])
print(o)
return res if res < float('inf') else -1
'''
|
Return the length of the shortest, non-empty, contiguous subarray of A with sum at least K.
If there is no non-empty subarray with sum at least K, return -1.
Example 1:
Input: A = [1], K = 1
Output: 1
Example 2:
Input: A = [1,2], K = 4
Output: -1
Example 3:
Input: A = [2,-1,2], K = 3
Output: 3
Note:
1 <= A.length <= 50000
-10 ^ 5 <= A[i] <= 10 ^ 5
1 <= K <= 10 ^ 9
|
class Solution:
def shortestSubarray(self, A: List[int], K: int) -> int:
total = 0
q = deque()
q.append([-1, 0])
res = float('inf')
for i, v in enumerate(A):
total += v
#print(q[0])
while q and total < q[-1][1]:
q.pop()
q.append([i, total])
while q and total - q[0][1] >=K:
res = min(res, i - q[0][0])
q.popleft()
return res if res != float('inf') else -1
|
Given an integer array bloomDay, an integer m and an integer k.
We need to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.
The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be used in exactly one bouquet.
Return the minimum number of days you need to wait to be able to make m bouquets from the garden. If it is impossible to make m bouquets return -1.
Example 1:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 1
Output: 3
Explanation: Let's see what happened in the first three days. x means flower bloomed and _ means flower didn't bloom in the garden.
We need 3 bouquets each should contain 1 flower.
After day 1: [x, _, _, _, _] // we can only make one bouquet.
After day 2: [x, _, _, _, x] // we can only make two bouquets.
After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3.
Example 2:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 2
Output: -1
Explanation: We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1.
Example 3:
Input: bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
Output: 12
Explanation: We need 2 bouquets each should have 3 flowers.
Here's the garden after the 7 and 12 days:
After day 7: [x, x, x, x, _, x, x]
We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent.
After day 12: [x, x, x, x, x, x, x]
It is obvious that we can make two bouquets in different ways.
Example 4:
Input: bloomDay = [1000000000,1000000000], m = 1, k = 1
Output: 1000000000
Explanation: You need to wait 1000000000 days to have a flower ready for a bouquet.
Example 5:
Input: bloomDay = [1,10,2,9,3,8,4,7,5,6], m = 4, k = 2
Output: 9
Constraints:
bloomDay.length == n
1 <= n <= 10^5
1 <= bloomDay[i] <= 10^9
1 <= m <= 10^6
1 <= k <= n
|
class Solution:
def minDays(self, bloomDay: List[int], m: int, k: int) -> int:
flowersN = len(bloomDay)
if flowersN < m*k:
return -1
def checkFlowers(x):
count = 0
gotFlowers = 0
for num in bloomDay:
if num <= x:
count += 1
if count == k:
gotFlowers += 1
count = 0
else:
count = 0
# print(gotFlowers, x, m)
return gotFlowers >= m
sortedDays = sorted(list(set(bloomDay)))
l = 0
r = len(sortedDays) - 1
if checkFlowers(sortedDays[l]):
return sortedDays[l]
while l < r:
mm = (l + r)//2
if checkFlowers(sortedDays[mm]):
r = mm
else:
l = mm+ 1
return sortedDays[l]
|
Given an integer array bloomDay, an integer m and an integer k.
We need to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.
The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be used in exactly one bouquet.
Return the minimum number of days you need to wait to be able to make m bouquets from the garden. If it is impossible to make m bouquets return -1.
Example 1:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 1
Output: 3
Explanation: Let's see what happened in the first three days. x means flower bloomed and _ means flower didn't bloom in the garden.
We need 3 bouquets each should contain 1 flower.
After day 1: [x, _, _, _, _] // we can only make one bouquet.
After day 2: [x, _, _, _, x] // we can only make two bouquets.
After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3.
Example 2:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 2
Output: -1
Explanation: We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1.
Example 3:
Input: bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
Output: 12
Explanation: We need 2 bouquets each should have 3 flowers.
Here's the garden after the 7 and 12 days:
After day 7: [x, x, x, x, _, x, x]
We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent.
After day 12: [x, x, x, x, x, x, x]
It is obvious that we can make two bouquets in different ways.
Example 4:
Input: bloomDay = [1000000000,1000000000], m = 1, k = 1
Output: 1000000000
Explanation: You need to wait 1000000000 days to have a flower ready for a bouquet.
Example 5:
Input: bloomDay = [1,10,2,9,3,8,4,7,5,6], m = 4, k = 2
Output: 9
Constraints:
bloomDay.length == n
1 <= n <= 10^5
1 <= bloomDay[i] <= 10^9
1 <= m <= 10^6
1 <= k <= n
|
class Solution:
def minDays(self, arr: List[int], m: int, k: int) -> int:
def count(arr, x, k):
out = 0
count = 0
for i in arr:
if i <= x:
count += 1
if count == k:
out += 1
count = 0
else:
count = 0
return out
if len(arr) < m*k:
return -1
res = -1
bi = list(sorted(set(arr)))
start = 0
end = len(bi)-1
while start <= end:
mid = (start+end)//2
if count(arr, bi[mid], k) >= m:
res = bi[mid]
end = mid - 1
else:
start = mid + 1
return res
|
Given an integer array bloomDay, an integer m and an integer k.
We need to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.
The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be used in exactly one bouquet.
Return the minimum number of days you need to wait to be able to make m bouquets from the garden. If it is impossible to make m bouquets return -1.
Example 1:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 1
Output: 3
Explanation: Let's see what happened in the first three days. x means flower bloomed and _ means flower didn't bloom in the garden.
We need 3 bouquets each should contain 1 flower.
After day 1: [x, _, _, _, _] // we can only make one bouquet.
After day 2: [x, _, _, _, x] // we can only make two bouquets.
After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3.
Example 2:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 2
Output: -1
Explanation: We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1.
Example 3:
Input: bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
Output: 12
Explanation: We need 2 bouquets each should have 3 flowers.
Here's the garden after the 7 and 12 days:
After day 7: [x, x, x, x, _, x, x]
We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent.
After day 12: [x, x, x, x, x, x, x]
It is obvious that we can make two bouquets in different ways.
Example 4:
Input: bloomDay = [1000000000,1000000000], m = 1, k = 1
Output: 1000000000
Explanation: You need to wait 1000000000 days to have a flower ready for a bouquet.
Example 5:
Input: bloomDay = [1,10,2,9,3,8,4,7,5,6], m = 4, k = 2
Output: 9
Constraints:
bloomDay.length == n
1 <= n <= 10^5
1 <= bloomDay[i] <= 10^9
1 <= m <= 10^6
1 <= k <= n
|
class Solution:
def minDays(self, bloomDay: List[int], m: int, k: int) -> int:
n = len(bloomDay)
if m * k > n:
return -1
def invalid(threshold):
cur = 0
bouquets = 0
for x in bloomDay:
if x <= threshold:
cur += 1
else:
bouquets += cur // k
cur = 0
bouquets += cur // k
return bouquets < m
left, right = 1, max(bloomDay)
while left <= right:
mid = (left + right) // 2
# print(left, right, mid)
if invalid(mid):
left = mid + 1
else:
right = mid - 1
return left
|
Given an integer array bloomDay, an integer m and an integer k.
We need to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.
The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be used in exactly one bouquet.
Return the minimum number of days you need to wait to be able to make m bouquets from the garden. If it is impossible to make m bouquets return -1.
Example 1:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 1
Output: 3
Explanation: Let's see what happened in the first three days. x means flower bloomed and _ means flower didn't bloom in the garden.
We need 3 bouquets each should contain 1 flower.
After day 1: [x, _, _, _, _] // we can only make one bouquet.
After day 2: [x, _, _, _, x] // we can only make two bouquets.
After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3.
Example 2:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 2
Output: -1
Explanation: We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1.
Example 3:
Input: bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
Output: 12
Explanation: We need 2 bouquets each should have 3 flowers.
Here's the garden after the 7 and 12 days:
After day 7: [x, x, x, x, _, x, x]
We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent.
After day 12: [x, x, x, x, x, x, x]
It is obvious that we can make two bouquets in different ways.
Example 4:
Input: bloomDay = [1000000000,1000000000], m = 1, k = 1
Output: 1000000000
Explanation: You need to wait 1000000000 days to have a flower ready for a bouquet.
Example 5:
Input: bloomDay = [1,10,2,9,3,8,4,7,5,6], m = 4, k = 2
Output: 9
Constraints:
bloomDay.length == n
1 <= n <= 10^5
1 <= bloomDay[i] <= 10^9
1 <= m <= 10^6
1 <= k <= n
|
class Solution:
def minDays(self, arr: List[int], m: int, k: int) -> int:
l, r, n = 0, max(arr) + 1, len(arr)
if m * k > n: return -1
while l < r:
mid = l + (r - l) // 2
cnt, tmp = 0, 0
for i, v in enumerate(arr):
cnt = 0 if v > mid else cnt + 1
if cnt == k:
tmp += 1
cnt = 0
if tmp == m: break
if tmp == m:
r = mid
else:
l = mid + 1
return l
|
Given an integer array bloomDay, an integer m and an integer k.
We need to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.
The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be used in exactly one bouquet.
Return the minimum number of days you need to wait to be able to make m bouquets from the garden. If it is impossible to make m bouquets return -1.
Example 1:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 1
Output: 3
Explanation: Let's see what happened in the first three days. x means flower bloomed and _ means flower didn't bloom in the garden.
We need 3 bouquets each should contain 1 flower.
After day 1: [x, _, _, _, _] // we can only make one bouquet.
After day 2: [x, _, _, _, x] // we can only make two bouquets.
After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3.
Example 2:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 2
Output: -1
Explanation: We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1.
Example 3:
Input: bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
Output: 12
Explanation: We need 2 bouquets each should have 3 flowers.
Here's the garden after the 7 and 12 days:
After day 7: [x, x, x, x, _, x, x]
We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent.
After day 12: [x, x, x, x, x, x, x]
It is obvious that we can make two bouquets in different ways.
Example 4:
Input: bloomDay = [1000000000,1000000000], m = 1, k = 1
Output: 1000000000
Explanation: You need to wait 1000000000 days to have a flower ready for a bouquet.
Example 5:
Input: bloomDay = [1,10,2,9,3,8,4,7,5,6], m = 4, k = 2
Output: 9
Constraints:
bloomDay.length == n
1 <= n <= 10^5
1 <= bloomDay[i] <= 10^9
1 <= m <= 10^6
1 <= k <= n
|
class Solution:
def checkDay(self,day,bloomDay,m,k)->bool:
y,count=0,0
for i in range(len(bloomDay)):
if bloomDay[i] <= day:
#bloomed.append(1)
y+=1
else:
#bloomed.append(0)
y=0
if y==k:
count+=1
y=0
if count==m:
return True
return False
def minDays(self, bloomDay: List[int], m: int, k: int) -> int:
'''
bloomDay: Flower garden, elements are # of days required for that flower to bloom
m: # of bouquets needed
k: # of adjacent flowers needed in each bouquet
First, check to see if mk>len(bloomDay). If so return -1
Then, see how many days needed until mk flowers have bloomed
Check to see if they are adjacent
if so return days
else maybe check how many adjacents are needed, or go to the next day
'''
#bloomDay,m,k = [7,7,7,7,12,7,7],2,3
FN=m*k # (Flowers Needed)
if FN > len(bloomDay):
return -1
# After x days, is it possible to create m bouquets?
bloomed=sorted(set(bloomDay))
lastWorking,count,start,stop=-1,0,0,len(bloomed)
#print(bloomed)
half=stop//2
#day=bloomed[half]
while(start<=half<stop):
day=bloomed[half]
#print('Half: ',half,', Day: ',day,' || Bounds: ',start,', ',stop)
# If current day works, check previous day
if self.checkDay(day,bloomDay,m,k):
#print('Day: ',day,' works')
lastWorking,stop=day,half
# Otherwise check half of new bounds
else:
start=half+1
half=(start+stop)//2
return lastWorking
|
Given an integer array bloomDay, an integer m and an integer k.
We need to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.
The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be used in exactly one bouquet.
Return the minimum number of days you need to wait to be able to make m bouquets from the garden. If it is impossible to make m bouquets return -1.
Example 1:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 1
Output: 3
Explanation: Let's see what happened in the first three days. x means flower bloomed and _ means flower didn't bloom in the garden.
We need 3 bouquets each should contain 1 flower.
After day 1: [x, _, _, _, _] // we can only make one bouquet.
After day 2: [x, _, _, _, x] // we can only make two bouquets.
After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3.
Example 2:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 2
Output: -1
Explanation: We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1.
Example 3:
Input: bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
Output: 12
Explanation: We need 2 bouquets each should have 3 flowers.
Here's the garden after the 7 and 12 days:
After day 7: [x, x, x, x, _, x, x]
We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent.
After day 12: [x, x, x, x, x, x, x]
It is obvious that we can make two bouquets in different ways.
Example 4:
Input: bloomDay = [1000000000,1000000000], m = 1, k = 1
Output: 1000000000
Explanation: You need to wait 1000000000 days to have a flower ready for a bouquet.
Example 5:
Input: bloomDay = [1,10,2,9,3,8,4,7,5,6], m = 4, k = 2
Output: 9
Constraints:
bloomDay.length == n
1 <= n <= 10^5
1 <= bloomDay[i] <= 10^9
1 <= m <= 10^6
1 <= k <= n
|
class Solution:
def minDays(self, bloomDay: List[int], m: int, k: int) -> int:
if len(bloomDay) < m*k:
return -1
left, right = 1, max(bloomDay)
while left < right:
mid = left + (right-left) // 2
if self.__feasible(bloomDay, k, m, mid):
right = mid
else:
left = mid+1
return left
def __feasible(self, bloomDay, k, m, day):
bloom = [day >= bloom_day for bloom_day in bloomDay]
cnt = 0
accu = 0
for b in bloom:
if b:
accu += 1
if accu == k:
cnt += 1
accu = 0
else:
accu = 0
return cnt >= m
|
Given an integer array bloomDay, an integer m and an integer k.
We need to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.
The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be used in exactly one bouquet.
Return the minimum number of days you need to wait to be able to make m bouquets from the garden. If it is impossible to make m bouquets return -1.
Example 1:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 1
Output: 3
Explanation: Let's see what happened in the first three days. x means flower bloomed and _ means flower didn't bloom in the garden.
We need 3 bouquets each should contain 1 flower.
After day 1: [x, _, _, _, _] // we can only make one bouquet.
After day 2: [x, _, _, _, x] // we can only make two bouquets.
After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3.
Example 2:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 2
Output: -1
Explanation: We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1.
Example 3:
Input: bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
Output: 12
Explanation: We need 2 bouquets each should have 3 flowers.
Here's the garden after the 7 and 12 days:
After day 7: [x, x, x, x, _, x, x]
We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent.
After day 12: [x, x, x, x, x, x, x]
It is obvious that we can make two bouquets in different ways.
Example 4:
Input: bloomDay = [1000000000,1000000000], m = 1, k = 1
Output: 1000000000
Explanation: You need to wait 1000000000 days to have a flower ready for a bouquet.
Example 5:
Input: bloomDay = [1,10,2,9,3,8,4,7,5,6], m = 4, k = 2
Output: 9
Constraints:
bloomDay.length == n
1 <= n <= 10^5
1 <= bloomDay[i] <= 10^9
1 <= m <= 10^6
1 <= k <= n
|
class Solution:
def minDays(self, bloomDay: List[int], m: int, k: int) -> int:
def canMakeBouquets(cap):
nBouquets = 0
nFlowers = 0
for i in range(len(bloomDay)):
if bloomDay[i] > cap:
nFlowers = 0
continue
nFlowers += 1
if nFlowers == k:
nBouquets += 1
nFlowers = 0
return nBouquets >= m
if m*k > len(bloomDay):
return -1
lo, hi = min(bloomDay), max(bloomDay)
while lo < hi:
med = lo + (hi-lo)//2
if canMakeBouquets(med):
hi = med
else:
lo = med + 1
return hi
|
Given an integer array bloomDay, an integer m and an integer k.
We need to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.
The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be used in exactly one bouquet.
Return the minimum number of days you need to wait to be able to make m bouquets from the garden. If it is impossible to make m bouquets return -1.
Example 1:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 1
Output: 3
Explanation: Let's see what happened in the first three days. x means flower bloomed and _ means flower didn't bloom in the garden.
We need 3 bouquets each should contain 1 flower.
After day 1: [x, _, _, _, _] // we can only make one bouquet.
After day 2: [x, _, _, _, x] // we can only make two bouquets.
After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3.
Example 2:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 2
Output: -1
Explanation: We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1.
Example 3:
Input: bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
Output: 12
Explanation: We need 2 bouquets each should have 3 flowers.
Here's the garden after the 7 and 12 days:
After day 7: [x, x, x, x, _, x, x]
We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent.
After day 12: [x, x, x, x, x, x, x]
It is obvious that we can make two bouquets in different ways.
Example 4:
Input: bloomDay = [1000000000,1000000000], m = 1, k = 1
Output: 1000000000
Explanation: You need to wait 1000000000 days to have a flower ready for a bouquet.
Example 5:
Input: bloomDay = [1,10,2,9,3,8,4,7,5,6], m = 4, k = 2
Output: 9
Constraints:
bloomDay.length == n
1 <= n <= 10^5
1 <= bloomDay[i] <= 10^9
1 <= m <= 10^6
1 <= k <= n
|
class Solution:
def minDays(self, bloomDay: List[int], m: int, k: int) -> int:
def bouquet_count(t):
count = 0
res = 0
for i in range(n):
if bloomDay[i] <= t:
count += 1
if count == k:
count = 0
res += 1
else:
count = 0
return res
n = len(bloomDay)
if m * k > n:
return -1
low, high = min(bloomDay), max(bloomDay)
while low <= high:
mid = low + (high - low) // 2
if bouquet_count(mid) >= m:
high = mid - 1
else:
low = mid + 1
return low
|
Given an integer array bloomDay, an integer m and an integer k.
We need to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.
The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be used in exactly one bouquet.
Return the minimum number of days you need to wait to be able to make m bouquets from the garden. If it is impossible to make m bouquets return -1.
Example 1:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 1
Output: 3
Explanation: Let's see what happened in the first three days. x means flower bloomed and _ means flower didn't bloom in the garden.
We need 3 bouquets each should contain 1 flower.
After day 1: [x, _, _, _, _] // we can only make one bouquet.
After day 2: [x, _, _, _, x] // we can only make two bouquets.
After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3.
Example 2:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 2
Output: -1
Explanation: We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1.
Example 3:
Input: bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
Output: 12
Explanation: We need 2 bouquets each should have 3 flowers.
Here's the garden after the 7 and 12 days:
After day 7: [x, x, x, x, _, x, x]
We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent.
After day 12: [x, x, x, x, x, x, x]
It is obvious that we can make two bouquets in different ways.
Example 4:
Input: bloomDay = [1000000000,1000000000], m = 1, k = 1
Output: 1000000000
Explanation: You need to wait 1000000000 days to have a flower ready for a bouquet.
Example 5:
Input: bloomDay = [1,10,2,9,3,8,4,7,5,6], m = 4, k = 2
Output: 9
Constraints:
bloomDay.length == n
1 <= n <= 10^5
1 <= bloomDay[i] <= 10^9
1 <= m <= 10^6
1 <= k <= n
|
class Solution:
def checkDay(self,day,bloomDay,m,k)->bool:
y,count=0,0
for i in range(len(bloomDay)):
if bloomDay[i] <= day:
#bloomed.append(1)
y+=1
else:
#bloomed.append(0)
y=0
if y==k:
count+=1
y=0
if count==m:
return True
return False
def minDays(self, bloomDay: List[int], m: int, k: int) -> int:
'''
bloomDay: Flower garden, elements are # of days required for that flower to bloom
m: # of bouquets needed
k: # of adjacent flowers needed in each bouquet
First, check to see if mk>len(bloomDay). If so return -1
Then, see how many days needed until mk flowers have bloomed
Check to see if they are adjacent
if so return days
else maybe check how many adjacents are needed, or go to the next day
'''
#bloomDay,m,k = [7,7,7,7,12,7,7],2,3
FN=m*k # (Flowers Needed)
if FN > len(bloomDay):
return -1
# After x days, is it possible to create m bouquets?
lastWorking,count,start,stop=-1,0,0,len(bloomDay)
bloomed=sorted(bloomDay)
print(bloomed)
half=stop//2
#day=bloomed[half]
while(start<=half<stop):
day=bloomed[half]
print('Half: ',half,', Day: ',day,' || Bounds: ',start,', ',stop)
# If current day works, check previous day
if self.checkDay(day,bloomDay,m,k):
print('Day: ',day,' works')
lastWorking,stop=day,half
# Otherwise check half of new bounds
else:
start=half+1
half=(start+stop)//2
return lastWorking
|
Given an integer array bloomDay, an integer m and an integer k.
We need to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.
The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be used in exactly one bouquet.
Return the minimum number of days you need to wait to be able to make m bouquets from the garden. If it is impossible to make m bouquets return -1.
Example 1:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 1
Output: 3
Explanation: Let's see what happened in the first three days. x means flower bloomed and _ means flower didn't bloom in the garden.
We need 3 bouquets each should contain 1 flower.
After day 1: [x, _, _, _, _] // we can only make one bouquet.
After day 2: [x, _, _, _, x] // we can only make two bouquets.
After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3.
Example 2:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 2
Output: -1
Explanation: We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1.
Example 3:
Input: bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
Output: 12
Explanation: We need 2 bouquets each should have 3 flowers.
Here's the garden after the 7 and 12 days:
After day 7: [x, x, x, x, _, x, x]
We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent.
After day 12: [x, x, x, x, x, x, x]
It is obvious that we can make two bouquets in different ways.
Example 4:
Input: bloomDay = [1000000000,1000000000], m = 1, k = 1
Output: 1000000000
Explanation: You need to wait 1000000000 days to have a flower ready for a bouquet.
Example 5:
Input: bloomDay = [1,10,2,9,3,8,4,7,5,6], m = 4, k = 2
Output: 9
Constraints:
bloomDay.length == n
1 <= n <= 10^5
1 <= bloomDay[i] <= 10^9
1 <= m <= 10^6
1 <= k <= n
|
class Solution:
def minDays(self, bloomDay: List[int], m: int, k: int) -> int:
left = min(bloomDay)
right = max(bloomDay)
n = len(bloomDay)
if m*k > n:
return -1
while left <= right:
mid = (left + right)//2
c = 0
bout = m
print((left, mid, right))
for b in bloomDay:
if b <= mid:
c += 1
else:
c = 0
if c >= k:
bout -= 1
c -= k
if bout == 0: break
if bout == 0: right = mid - 1
else: left = mid + 1
return left
|
Given an integer array bloomDay, an integer m and an integer k.
We need to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.
The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be used in exactly one bouquet.
Return the minimum number of days you need to wait to be able to make m bouquets from the garden. If it is impossible to make m bouquets return -1.
Example 1:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 1
Output: 3
Explanation: Let's see what happened in the first three days. x means flower bloomed and _ means flower didn't bloom in the garden.
We need 3 bouquets each should contain 1 flower.
After day 1: [x, _, _, _, _] // we can only make one bouquet.
After day 2: [x, _, _, _, x] // we can only make two bouquets.
After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3.
Example 2:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 2
Output: -1
Explanation: We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1.
Example 3:
Input: bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
Output: 12
Explanation: We need 2 bouquets each should have 3 flowers.
Here's the garden after the 7 and 12 days:
After day 7: [x, x, x, x, _, x, x]
We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent.
After day 12: [x, x, x, x, x, x, x]
It is obvious that we can make two bouquets in different ways.
Example 4:
Input: bloomDay = [1000000000,1000000000], m = 1, k = 1
Output: 1000000000
Explanation: You need to wait 1000000000 days to have a flower ready for a bouquet.
Example 5:
Input: bloomDay = [1,10,2,9,3,8,4,7,5,6], m = 4, k = 2
Output: 9
Constraints:
bloomDay.length == n
1 <= n <= 10^5
1 <= bloomDay[i] <= 10^9
1 <= m <= 10^6
1 <= k <= n
|
class Solution:
def minDays(self, bloomDay: List[int], m: int, k: int) -> int:
if m * k > len(bloomDay):
return -1
l = 1
r = max(bloomDay)
while l < r:
mid = (l + r) // 2
flow = 0
nums = 0
for i in range(len(bloomDay)):
if nums >= m:
break
if bloomDay[i] > mid:
flow = 0
else:
flow += 1
if flow >= k:
flow = 0
nums += 1
# print(mid, nums)
if nums >= m:
r = mid
else:
l = mid + 1
return l
|
Given an integer array bloomDay, an integer m and an integer k.
We need to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.
The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be used in exactly one bouquet.
Return the minimum number of days you need to wait to be able to make m bouquets from the garden. If it is impossible to make m bouquets return -1.
Example 1:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 1
Output: 3
Explanation: Let's see what happened in the first three days. x means flower bloomed and _ means flower didn't bloom in the garden.
We need 3 bouquets each should contain 1 flower.
After day 1: [x, _, _, _, _] // we can only make one bouquet.
After day 2: [x, _, _, _, x] // we can only make two bouquets.
After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3.
Example 2:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 2
Output: -1
Explanation: We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1.
Example 3:
Input: bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
Output: 12
Explanation: We need 2 bouquets each should have 3 flowers.
Here's the garden after the 7 and 12 days:
After day 7: [x, x, x, x, _, x, x]
We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent.
After day 12: [x, x, x, x, x, x, x]
It is obvious that we can make two bouquets in different ways.
Example 4:
Input: bloomDay = [1000000000,1000000000], m = 1, k = 1
Output: 1000000000
Explanation: You need to wait 1000000000 days to have a flower ready for a bouquet.
Example 5:
Input: bloomDay = [1,10,2,9,3,8,4,7,5,6], m = 4, k = 2
Output: 9
Constraints:
bloomDay.length == n
1 <= n <= 10^5
1 <= bloomDay[i] <= 10^9
1 <= m <= 10^6
1 <= k <= n
|
class Solution:
def minDays(self, bloomDay: List[int], m: int, k: int) -> int:
if len(bloomDay)<m*k:
return -1
n=len(bloomDay)
def counter(day, k):
count, bouquets = 0, 0
for i in range(n):
if bloomDay[i]<=day:
count+=1
else:
count=0
if count==k:
bouquets+=1
count=0
return bouquets
low, high = 1, max(bloomDay)
while(low<high):
mid=(low+high)//2
if counter(mid, k) >= m:
high = mid
else:
low=mid+1
return low
|
Given an integer array bloomDay, an integer m and an integer k.
We need to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.
The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be used in exactly one bouquet.
Return the minimum number of days you need to wait to be able to make m bouquets from the garden. If it is impossible to make m bouquets return -1.
Example 1:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 1
Output: 3
Explanation: Let's see what happened in the first three days. x means flower bloomed and _ means flower didn't bloom in the garden.
We need 3 bouquets each should contain 1 flower.
After day 1: [x, _, _, _, _] // we can only make one bouquet.
After day 2: [x, _, _, _, x] // we can only make two bouquets.
After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3.
Example 2:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 2
Output: -1
Explanation: We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1.
Example 3:
Input: bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
Output: 12
Explanation: We need 2 bouquets each should have 3 flowers.
Here's the garden after the 7 and 12 days:
After day 7: [x, x, x, x, _, x, x]
We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent.
After day 12: [x, x, x, x, x, x, x]
It is obvious that we can make two bouquets in different ways.
Example 4:
Input: bloomDay = [1000000000,1000000000], m = 1, k = 1
Output: 1000000000
Explanation: You need to wait 1000000000 days to have a flower ready for a bouquet.
Example 5:
Input: bloomDay = [1,10,2,9,3,8,4,7,5,6], m = 4, k = 2
Output: 9
Constraints:
bloomDay.length == n
1 <= n <= 10^5
1 <= bloomDay[i] <= 10^9
1 <= m <= 10^6
1 <= k <= n
|
class Solution:
def minDays(self, bloomDay: List[int], m: int, k: int) -> int:
if m * k > len(bloomDay):
return -1
lower_bound = min(bloomDay)
upper_bound = max(bloomDay)
while lower_bound < upper_bound:
days = (lower_bound + upper_bound) // 2
if self.can_make_bouquets(bloomDay, m, k, days):
upper_bound = days
else:
lower_bound = days + 1
return upper_bound
def can_make_bouquets(self, bloomDay, m, k, days):
bouquets_made = 0
adj_count = 0
K = k
for i in range(len(bloomDay)):
if bloomDay[i] <= days:
K -= 1
else:
K = k
if K == 0:
bouquets_made += 1
K = k
return bouquets_made >= m
|
Given an integer array bloomDay, an integer m and an integer k.
We need to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.
The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be used in exactly one bouquet.
Return the minimum number of days you need to wait to be able to make m bouquets from the garden. If it is impossible to make m bouquets return -1.
Example 1:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 1
Output: 3
Explanation: Let's see what happened in the first three days. x means flower bloomed and _ means flower didn't bloom in the garden.
We need 3 bouquets each should contain 1 flower.
After day 1: [x, _, _, _, _] // we can only make one bouquet.
After day 2: [x, _, _, _, x] // we can only make two bouquets.
After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3.
Example 2:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 2
Output: -1
Explanation: We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1.
Example 3:
Input: bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
Output: 12
Explanation: We need 2 bouquets each should have 3 flowers.
Here's the garden after the 7 and 12 days:
After day 7: [x, x, x, x, _, x, x]
We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent.
After day 12: [x, x, x, x, x, x, x]
It is obvious that we can make two bouquets in different ways.
Example 4:
Input: bloomDay = [1000000000,1000000000], m = 1, k = 1
Output: 1000000000
Explanation: You need to wait 1000000000 days to have a flower ready for a bouquet.
Example 5:
Input: bloomDay = [1,10,2,9,3,8,4,7,5,6], m = 4, k = 2
Output: 9
Constraints:
bloomDay.length == n
1 <= n <= 10^5
1 <= bloomDay[i] <= 10^9
1 <= m <= 10^6
1 <= k <= n
|
class Solution:
def minDays(self, bloomDay: List[int], m: int, k: int) -> int:
if m * k > len(bloomDay):
return -1
tmp = sorted(list(set(bloomDay)))
low = 0
high = len(tmp) - 1
def feasible(val):
count = 0
total = m
for day in bloomDay:
if val >= day:
count += 1
if count == k:
total -= 1
count = 0
if total == -1:
break
else:
count = 0
return total <= 0
while low < high:
mid = (low + high) // 2
val = tmp[mid]
if feasible(val):
high = mid
else:
low = mid + 1
return tmp[low]
|
Given an integer array bloomDay, an integer m and an integer k.
We need to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.
The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be used in exactly one bouquet.
Return the minimum number of days you need to wait to be able to make m bouquets from the garden. If it is impossible to make m bouquets return -1.
Example 1:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 1
Output: 3
Explanation: Let's see what happened in the first three days. x means flower bloomed and _ means flower didn't bloom in the garden.
We need 3 bouquets each should contain 1 flower.
After day 1: [x, _, _, _, _] // we can only make one bouquet.
After day 2: [x, _, _, _, x] // we can only make two bouquets.
After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3.
Example 2:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 2
Output: -1
Explanation: We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1.
Example 3:
Input: bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
Output: 12
Explanation: We need 2 bouquets each should have 3 flowers.
Here's the garden after the 7 and 12 days:
After day 7: [x, x, x, x, _, x, x]
We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent.
After day 12: [x, x, x, x, x, x, x]
It is obvious that we can make two bouquets in different ways.
Example 4:
Input: bloomDay = [1000000000,1000000000], m = 1, k = 1
Output: 1000000000
Explanation: You need to wait 1000000000 days to have a flower ready for a bouquet.
Example 5:
Input: bloomDay = [1,10,2,9,3,8,4,7,5,6], m = 4, k = 2
Output: 9
Constraints:
bloomDay.length == n
1 <= n <= 10^5
1 <= bloomDay[i] <= 10^9
1 <= m <= 10^6
1 <= k <= n
|
class Solution:
def minDays(self, F: List[int], m: int, k: int) -> int:
if(m*k>len(F)):
return -1
ans = 0
left = 1
right = max(F)
while(left<=right):
mid = (left+right)//2
if(self.possible(mid,F,m,k)==True):
ans = mid
right = mid-1
else:
left=mid+1
return ans
def possible(self,mid,F,m,k):
# print(mid)
count = 0
a = 0
for i in range(len(F)):
if(a==k):
count+=1
a=0
if(F[i]<=mid):
a+=1
else:
a = 0
if(a==k):
count+=1
if(count>=m):
return True
return False
|
Given an integer array bloomDay, an integer m and an integer k.
We need to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.
The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be used in exactly one bouquet.
Return the minimum number of days you need to wait to be able to make m bouquets from the garden. If it is impossible to make m bouquets return -1.
Example 1:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 1
Output: 3
Explanation: Let's see what happened in the first three days. x means flower bloomed and _ means flower didn't bloom in the garden.
We need 3 bouquets each should contain 1 flower.
After day 1: [x, _, _, _, _] // we can only make one bouquet.
After day 2: [x, _, _, _, x] // we can only make two bouquets.
After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3.
Example 2:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 2
Output: -1
Explanation: We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1.
Example 3:
Input: bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
Output: 12
Explanation: We need 2 bouquets each should have 3 flowers.
Here's the garden after the 7 and 12 days:
After day 7: [x, x, x, x, _, x, x]
We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent.
After day 12: [x, x, x, x, x, x, x]
It is obvious that we can make two bouquets in different ways.
Example 4:
Input: bloomDay = [1000000000,1000000000], m = 1, k = 1
Output: 1000000000
Explanation: You need to wait 1000000000 days to have a flower ready for a bouquet.
Example 5:
Input: bloomDay = [1,10,2,9,3,8,4,7,5,6], m = 4, k = 2
Output: 9
Constraints:
bloomDay.length == n
1 <= n <= 10^5
1 <= bloomDay[i] <= 10^9
1 <= m <= 10^6
1 <= k <= n
|
class Solution:
def minDays(self, bloomDay: List[int], m: int, k: int) -> int:
n = len(bloomDay)
if m*k > n:
return -1
def can_make_bouquets(m: int, k: int, day: int) -> bool:
#print(m, k, day)
total_bouquets = 0
curr_flowers = 0
for index, d in enumerate(bloomDay):
if total_bouquets + 1 + (n-index-1)//k < m:
#print(\"No\")
return False
if d <= day:
curr_flowers += 1
if curr_flowers == k:
total_bouquets += 1
curr_flowers = 0
else:
curr_flowers = 0
if total_bouquets == m:
#print(\"Yes\")
return True
return False
days = sorted(list(set(bloomDay)))
lo, hi = 0, len(days)-1
min_days = float('inf')
#print(days)
while lo <= hi:
mi = (lo+hi)//2
#print(lo, mi, hi)
if can_make_bouquets(m, k, days[mi]):
min_days = min(min_days, days[mi])
hi = mi-1
else:
lo = mi+1
return min_days if min_days < float('inf') else -1
|
Given an integer array bloomDay, an integer m and an integer k.
We need to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.
The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be used in exactly one bouquet.
Return the minimum number of days you need to wait to be able to make m bouquets from the garden. If it is impossible to make m bouquets return -1.
Example 1:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 1
Output: 3
Explanation: Let's see what happened in the first three days. x means flower bloomed and _ means flower didn't bloom in the garden.
We need 3 bouquets each should contain 1 flower.
After day 1: [x, _, _, _, _] // we can only make one bouquet.
After day 2: [x, _, _, _, x] // we can only make two bouquets.
After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3.
Example 2:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 2
Output: -1
Explanation: We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1.
Example 3:
Input: bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
Output: 12
Explanation: We need 2 bouquets each should have 3 flowers.
Here's the garden after the 7 and 12 days:
After day 7: [x, x, x, x, _, x, x]
We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent.
After day 12: [x, x, x, x, x, x, x]
It is obvious that we can make two bouquets in different ways.
Example 4:
Input: bloomDay = [1000000000,1000000000], m = 1, k = 1
Output: 1000000000
Explanation: You need to wait 1000000000 days to have a flower ready for a bouquet.
Example 5:
Input: bloomDay = [1,10,2,9,3,8,4,7,5,6], m = 4, k = 2
Output: 9
Constraints:
bloomDay.length == n
1 <= n <= 10^5
1 <= bloomDay[i] <= 10^9
1 <= m <= 10^6
1 <= k <= n
|
class Solution:
def minDays(self, blooms: List[int], m: int, k: int) -> int:
def check(d):
bouq, b = 0, 0
for i in range(len(blooms)):
if blooms[i] <= d:
b += 1
else:
b = 0
if b == k:
b = 0
bouq += 1
if bouq >= m:
return True
return bouq >= m
lo, hi = 1, max(blooms)
while lo < hi:
mid = (hi+lo)//2
if not check(mid):
lo = mid + 1
else: hi = mid
return lo if check(lo) else -1
|
Given an integer array bloomDay, an integer m and an integer k.
We need to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.
The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be used in exactly one bouquet.
Return the minimum number of days you need to wait to be able to make m bouquets from the garden. If it is impossible to make m bouquets return -1.
Example 1:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 1
Output: 3
Explanation: Let's see what happened in the first three days. x means flower bloomed and _ means flower didn't bloom in the garden.
We need 3 bouquets each should contain 1 flower.
After day 1: [x, _, _, _, _] // we can only make one bouquet.
After day 2: [x, _, _, _, x] // we can only make two bouquets.
After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3.
Example 2:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 2
Output: -1
Explanation: We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1.
Example 3:
Input: bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
Output: 12
Explanation: We need 2 bouquets each should have 3 flowers.
Here's the garden after the 7 and 12 days:
After day 7: [x, x, x, x, _, x, x]
We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent.
After day 12: [x, x, x, x, x, x, x]
It is obvious that we can make two bouquets in different ways.
Example 4:
Input: bloomDay = [1000000000,1000000000], m = 1, k = 1
Output: 1000000000
Explanation: You need to wait 1000000000 days to have a flower ready for a bouquet.
Example 5:
Input: bloomDay = [1,10,2,9,3,8,4,7,5,6], m = 4, k = 2
Output: 9
Constraints:
bloomDay.length == n
1 <= n <= 10^5
1 <= bloomDay[i] <= 10^9
1 <= m <= 10^6
1 <= k <= n
|
class Solution:
def minDays(self, bloomDay: List[int], m: int, k: int) -> int:
n = len(bloomDay)
if n < m*k: return -1
def count(day):
cnt,counter=0,0
for i in range(n):
if bloomDay[i]<=day:
counter +=1
if counter ==k:
cnt +=1
counter = 0
else:
counter =0
return cnt
l,r = min(bloomDay),max(bloomDay)
while l<=r:
mid = l+(r-l)//2
if count(mid) <m:
l = mid+1
else:
r = mid -1
return l
|
Given an integer array bloomDay, an integer m and an integer k.
We need to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.
The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be used in exactly one bouquet.
Return the minimum number of days you need to wait to be able to make m bouquets from the garden. If it is impossible to make m bouquets return -1.
Example 1:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 1
Output: 3
Explanation: Let's see what happened in the first three days. x means flower bloomed and _ means flower didn't bloom in the garden.
We need 3 bouquets each should contain 1 flower.
After day 1: [x, _, _, _, _] // we can only make one bouquet.
After day 2: [x, _, _, _, x] // we can only make two bouquets.
After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3.
Example 2:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 2
Output: -1
Explanation: We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1.
Example 3:
Input: bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
Output: 12
Explanation: We need 2 bouquets each should have 3 flowers.
Here's the garden after the 7 and 12 days:
After day 7: [x, x, x, x, _, x, x]
We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent.
After day 12: [x, x, x, x, x, x, x]
It is obvious that we can make two bouquets in different ways.
Example 4:
Input: bloomDay = [1000000000,1000000000], m = 1, k = 1
Output: 1000000000
Explanation: You need to wait 1000000000 days to have a flower ready for a bouquet.
Example 5:
Input: bloomDay = [1,10,2,9,3,8,4,7,5,6], m = 4, k = 2
Output: 9
Constraints:
bloomDay.length == n
1 <= n <= 10^5
1 <= bloomDay[i] <= 10^9
1 <= m <= 10^6
1 <= k <= n
|
class Solution:
def minDays(self, bloomDay: List[int], m: int, k: int) -> int:
def bouquets_possible(day):
bouquets_count = 0
conse_day = 0
for i, v in enumerate(bloomDay):
if v <= day:
conse_day += 1
if conse_day == k:
bouquets_count += 1
conse_day = 0
else:
conse_day = 0
return bouquets_count
if len(bloomDay) < m * k:
return -1
left = min(bloomDay)
right = max(bloomDay)
while left < right:
mid = left + ( right - left)//2
if bouquets_possible(mid) < m:
left = mid + 1
else:
right = mid
return left
|
Given an integer array bloomDay, an integer m and an integer k.
We need to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.
The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be used in exactly one bouquet.
Return the minimum number of days you need to wait to be able to make m bouquets from the garden. If it is impossible to make m bouquets return -1.
Example 1:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 1
Output: 3
Explanation: Let's see what happened in the first three days. x means flower bloomed and _ means flower didn't bloom in the garden.
We need 3 bouquets each should contain 1 flower.
After day 1: [x, _, _, _, _] // we can only make one bouquet.
After day 2: [x, _, _, _, x] // we can only make two bouquets.
After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3.
Example 2:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 2
Output: -1
Explanation: We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1.
Example 3:
Input: bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
Output: 12
Explanation: We need 2 bouquets each should have 3 flowers.
Here's the garden after the 7 and 12 days:
After day 7: [x, x, x, x, _, x, x]
We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent.
After day 12: [x, x, x, x, x, x, x]
It is obvious that we can make two bouquets in different ways.
Example 4:
Input: bloomDay = [1000000000,1000000000], m = 1, k = 1
Output: 1000000000
Explanation: You need to wait 1000000000 days to have a flower ready for a bouquet.
Example 5:
Input: bloomDay = [1,10,2,9,3,8,4,7,5,6], m = 4, k = 2
Output: 9
Constraints:
bloomDay.length == n
1 <= n <= 10^5
1 <= bloomDay[i] <= 10^9
1 <= m <= 10^6
1 <= k <= n
|
class Solution:
def minDays(self, bloomDay: List[int], m: int, k: int) -> int:
# min -> 0 max - > max(element)
# check for m*k and valid check kar
# condition main keep checking the consecutive flowers
# m -> m bouqets , k adjacent flower
# if len(bloomDay)<m*k:
# return -1
def isPossible(t):
count = 0
res = 0
for i in range(len(bloomDay)):
if bloomDay[i] <= t:
count += 1
if count == k:
count = 0
res += 1
else:
count = 0
return res>=m
s,e = 0, max(bloomDay)+1
while s<e:
mid = (s+e)//2
if isPossible(mid):
e = mid
else:
s = mid+1
if s==max(bloomDay)+1:
return -1
return s
|
Given an integer array bloomDay, an integer m and an integer k.
We need to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.
The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be used in exactly one bouquet.
Return the minimum number of days you need to wait to be able to make m bouquets from the garden. If it is impossible to make m bouquets return -1.
Example 1:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 1
Output: 3
Explanation: Let's see what happened in the first three days. x means flower bloomed and _ means flower didn't bloom in the garden.
We need 3 bouquets each should contain 1 flower.
After day 1: [x, _, _, _, _] // we can only make one bouquet.
After day 2: [x, _, _, _, x] // we can only make two bouquets.
After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3.
Example 2:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 2
Output: -1
Explanation: We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1.
Example 3:
Input: bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
Output: 12
Explanation: We need 2 bouquets each should have 3 flowers.
Here's the garden after the 7 and 12 days:
After day 7: [x, x, x, x, _, x, x]
We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent.
After day 12: [x, x, x, x, x, x, x]
It is obvious that we can make two bouquets in different ways.
Example 4:
Input: bloomDay = [1000000000,1000000000], m = 1, k = 1
Output: 1000000000
Explanation: You need to wait 1000000000 days to have a flower ready for a bouquet.
Example 5:
Input: bloomDay = [1,10,2,9,3,8,4,7,5,6], m = 4, k = 2
Output: 9
Constraints:
bloomDay.length == n
1 <= n <= 10^5
1 <= bloomDay[i] <= 10^9
1 <= m <= 10^6
1 <= k <= n
|
class Solution:
def minDays(self, bloomDay: List[int], m: int, k: int) -> int:
if k*m > len(bloomDay):
return -1
def isPossible(mid):
numFlowers = 0
numBouquets = 0
for i in range(len(bloomDay)):
if bloomDay[i] > mid:
numFlowers = 0
else:
numFlowers += 1
if numFlowers >= k:
numBouquets += 1
numFlowers -= k
# print(mid, numBouquets)
return numBouquets >= m
low = 0
high = max(bloomDay)
while (low < high):
mid = int((low + high)/2)
if (isPossible(mid)):
# print(\"true\", mid)
high = mid
else:
low = mid + 1
return low
|
Given an integer array bloomDay, an integer m and an integer k.
We need to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.
The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be used in exactly one bouquet.
Return the minimum number of days you need to wait to be able to make m bouquets from the garden. If it is impossible to make m bouquets return -1.
Example 1:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 1
Output: 3
Explanation: Let's see what happened in the first three days. x means flower bloomed and _ means flower didn't bloom in the garden.
We need 3 bouquets each should contain 1 flower.
After day 1: [x, _, _, _, _] // we can only make one bouquet.
After day 2: [x, _, _, _, x] // we can only make two bouquets.
After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3.
Example 2:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 2
Output: -1
Explanation: We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1.
Example 3:
Input: bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
Output: 12
Explanation: We need 2 bouquets each should have 3 flowers.
Here's the garden after the 7 and 12 days:
After day 7: [x, x, x, x, _, x, x]
We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent.
After day 12: [x, x, x, x, x, x, x]
It is obvious that we can make two bouquets in different ways.
Example 4:
Input: bloomDay = [1000000000,1000000000], m = 1, k = 1
Output: 1000000000
Explanation: You need to wait 1000000000 days to have a flower ready for a bouquet.
Example 5:
Input: bloomDay = [1,10,2,9,3,8,4,7,5,6], m = 4, k = 2
Output: 9
Constraints:
bloomDay.length == n
1 <= n <= 10^5
1 <= bloomDay[i] <= 10^9
1 <= m <= 10^6
1 <= k <= n
|
class Solution:
def minDays(self, bloomDay: List[int], m: int, k: int) -> int:
def check(days_cand):
nonlocal bloomDay
nonlocal m
nonlocal k
nonlocal res
bukets = 0
flowers_per_cur_bucket = 0
for i in range(len(bloomDay)):
today = bloomDay[i]
if today <= days_cand:
#is flower
flowers_per_cur_bucket +=1
if flowers_per_cur_bucket == k:
bukets +=1
flowers_per_cur_bucket = 0
if bukets == m:
res = min(res, days_cand)
return True
pass
else:
#no flower today
flowers_per_cur_bucket = 0
pass
return False
res = float('inf')
left = min(bloomDay)
right = max(bloomDay)+1
while left < right:
mid = left + (right - left) // 2
if check(mid):
# add to answer, check if can do better decrease mid and look up in left part
right = mid
else:
left = mid + 1
if res < float('inf'):
return left
return -1
|
Given an integer array bloomDay, an integer m and an integer k.
We need to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.
The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be used in exactly one bouquet.
Return the minimum number of days you need to wait to be able to make m bouquets from the garden. If it is impossible to make m bouquets return -1.
Example 1:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 1
Output: 3
Explanation: Let's see what happened in the first three days. x means flower bloomed and _ means flower didn't bloom in the garden.
We need 3 bouquets each should contain 1 flower.
After day 1: [x, _, _, _, _] // we can only make one bouquet.
After day 2: [x, _, _, _, x] // we can only make two bouquets.
After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3.
Example 2:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 2
Output: -1
Explanation: We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1.
Example 3:
Input: bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
Output: 12
Explanation: We need 2 bouquets each should have 3 flowers.
Here's the garden after the 7 and 12 days:
After day 7: [x, x, x, x, _, x, x]
We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent.
After day 12: [x, x, x, x, x, x, x]
It is obvious that we can make two bouquets in different ways.
Example 4:
Input: bloomDay = [1000000000,1000000000], m = 1, k = 1
Output: 1000000000
Explanation: You need to wait 1000000000 days to have a flower ready for a bouquet.
Example 5:
Input: bloomDay = [1,10,2,9,3,8,4,7,5,6], m = 4, k = 2
Output: 9
Constraints:
bloomDay.length == n
1 <= n <= 10^5
1 <= bloomDay[i] <= 10^9
1 <= m <= 10^6
1 <= k <= n
|
class Solution:
def minDays(self, bloomDay: List[int], m: int, k: int) -> int:
n = len(bloomDay)
flowers = collections.defaultdict(list)
for idx, day in enumerate(bloomDay):
flowers[day].append(idx)
root = list(range(n))
num_flowers_left = collections.Counter()
num_flowers_right = collections.Counter()
num_bouquets = 0
for day in sorted(flowers.keys()):
for idx in flowers[day]:
start = idx - num_flowers_left[idx - 1]
num_bouquets -= num_flowers_left[idx - 1] // k
end = idx + num_flowers_right[idx + 1]
num_bouquets -= num_flowers_right[idx + 1] // k
total_num_flowers = end - start + 1
num_bouquets += total_num_flowers // k
num_flowers_left[end] = total_num_flowers
num_flowers_right[start] = total_num_flowers
if num_bouquets >= m:
return day
return -1
|
Given an integer array bloomDay, an integer m and an integer k.
We need to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.
The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be used in exactly one bouquet.
Return the minimum number of days you need to wait to be able to make m bouquets from the garden. If it is impossible to make m bouquets return -1.
Example 1:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 1
Output: 3
Explanation: Let's see what happened in the first three days. x means flower bloomed and _ means flower didn't bloom in the garden.
We need 3 bouquets each should contain 1 flower.
After day 1: [x, _, _, _, _] // we can only make one bouquet.
After day 2: [x, _, _, _, x] // we can only make two bouquets.
After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3.
Example 2:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 2
Output: -1
Explanation: We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1.
Example 3:
Input: bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
Output: 12
Explanation: We need 2 bouquets each should have 3 flowers.
Here's the garden after the 7 and 12 days:
After day 7: [x, x, x, x, _, x, x]
We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent.
After day 12: [x, x, x, x, x, x, x]
It is obvious that we can make two bouquets in different ways.
Example 4:
Input: bloomDay = [1000000000,1000000000], m = 1, k = 1
Output: 1000000000
Explanation: You need to wait 1000000000 days to have a flower ready for a bouquet.
Example 5:
Input: bloomDay = [1,10,2,9,3,8,4,7,5,6], m = 4, k = 2
Output: 9
Constraints:
bloomDay.length == n
1 <= n <= 10^5
1 <= bloomDay[i] <= 10^9
1 <= m <= 10^6
1 <= k <= n
|
class Solution:
def minDays(self, A, m, k):
if m * k > len(A): return -1
left, right = 1, max(A)
while left < right:
mid = (left + right) // 2
flow = bouq = 0
for a in A:
flow = 0 if a > mid else flow + 1
if flow >= k:
flow = 0
bouq += 1
if bouq == m: break
if bouq == m:
right = mid
else:
left = mid + 1
return left
|
Given an integer array bloomDay, an integer m and an integer k.
We need to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.
The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be used in exactly one bouquet.
Return the minimum number of days you need to wait to be able to make m bouquets from the garden. If it is impossible to make m bouquets return -1.
Example 1:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 1
Output: 3
Explanation: Let's see what happened in the first three days. x means flower bloomed and _ means flower didn't bloom in the garden.
We need 3 bouquets each should contain 1 flower.
After day 1: [x, _, _, _, _] // we can only make one bouquet.
After day 2: [x, _, _, _, x] // we can only make two bouquets.
After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3.
Example 2:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 2
Output: -1
Explanation: We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1.
Example 3:
Input: bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
Output: 12
Explanation: We need 2 bouquets each should have 3 flowers.
Here's the garden after the 7 and 12 days:
After day 7: [x, x, x, x, _, x, x]
We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent.
After day 12: [x, x, x, x, x, x, x]
It is obvious that we can make two bouquets in different ways.
Example 4:
Input: bloomDay = [1000000000,1000000000], m = 1, k = 1
Output: 1000000000
Explanation: You need to wait 1000000000 days to have a flower ready for a bouquet.
Example 5:
Input: bloomDay = [1,10,2,9,3,8,4,7,5,6], m = 4, k = 2
Output: 9
Constraints:
bloomDay.length == n
1 <= n <= 10^5
1 <= bloomDay[i] <= 10^9
1 <= m <= 10^6
1 <= k <= n
|
class Solution:
def minDays(self, bloomDay: List[int], m: int, k: int) -> int:
if m * k > len(bloomDay):
return -1
def bouqets(d):
res, c = 0, 0
for n in bloomDay:
c = 0 if n > d else c + 1
if c == k:
res, c = res + 1, 0
return res
days = sorted(set(bloomDay))
lo, hi = 0, len(days)-1
while lo < hi:
mid = (lo + hi) // 2
if bouqets(days[mid]) < m:
lo = mid + 1
else:
hi = mid
return days[hi] if days[hi] >= m else -1
|
Given an integer array bloomDay, an integer m and an integer k.
We need to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.
The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be used in exactly one bouquet.
Return the minimum number of days you need to wait to be able to make m bouquets from the garden. If it is impossible to make m bouquets return -1.
Example 1:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 1
Output: 3
Explanation: Let's see what happened in the first three days. x means flower bloomed and _ means flower didn't bloom in the garden.
We need 3 bouquets each should contain 1 flower.
After day 1: [x, _, _, _, _] // we can only make one bouquet.
After day 2: [x, _, _, _, x] // we can only make two bouquets.
After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3.
Example 2:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 2
Output: -1
Explanation: We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1.
Example 3:
Input: bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
Output: 12
Explanation: We need 2 bouquets each should have 3 flowers.
Here's the garden after the 7 and 12 days:
After day 7: [x, x, x, x, _, x, x]
We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent.
After day 12: [x, x, x, x, x, x, x]
It is obvious that we can make two bouquets in different ways.
Example 4:
Input: bloomDay = [1000000000,1000000000], m = 1, k = 1
Output: 1000000000
Explanation: You need to wait 1000000000 days to have a flower ready for a bouquet.
Example 5:
Input: bloomDay = [1,10,2,9,3,8,4,7,5,6], m = 4, k = 2
Output: 9
Constraints:
bloomDay.length == n
1 <= n <= 10^5
1 <= bloomDay[i] <= 10^9
1 <= m <= 10^6
1 <= k <= n
|
class Solution:
def minDays(self, bloomDay: List[int], m: int, k: int) -> int:
if not bloomDay or m*k>len(bloomDay):
return(-1)
def feasible(days):
bouquets,flower=0,0
for bloom in bloomDay:
if bloom>days:
flower=0
else:
bouquets+=(flower+1)//k
flower=(flower+1)%k
return bouquets>=m
l,r=0,max(bloomDay)
while(l<r):
mid=l+(r-l)//2
if(feasible(mid)):
r=mid
else:
l=mid+1
return l
|
Given an integer array bloomDay, an integer m and an integer k.
We need to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.
The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be used in exactly one bouquet.
Return the minimum number of days you need to wait to be able to make m bouquets from the garden. If it is impossible to make m bouquets return -1.
Example 1:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 1
Output: 3
Explanation: Let's see what happened in the first three days. x means flower bloomed and _ means flower didn't bloom in the garden.
We need 3 bouquets each should contain 1 flower.
After day 1: [x, _, _, _, _] // we can only make one bouquet.
After day 2: [x, _, _, _, x] // we can only make two bouquets.
After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3.
Example 2:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 2
Output: -1
Explanation: We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1.
Example 3:
Input: bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
Output: 12
Explanation: We need 2 bouquets each should have 3 flowers.
Here's the garden after the 7 and 12 days:
After day 7: [x, x, x, x, _, x, x]
We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent.
After day 12: [x, x, x, x, x, x, x]
It is obvious that we can make two bouquets in different ways.
Example 4:
Input: bloomDay = [1000000000,1000000000], m = 1, k = 1
Output: 1000000000
Explanation: You need to wait 1000000000 days to have a flower ready for a bouquet.
Example 5:
Input: bloomDay = [1,10,2,9,3,8,4,7,5,6], m = 4, k = 2
Output: 9
Constraints:
bloomDay.length == n
1 <= n <= 10^5
1 <= bloomDay[i] <= 10^9
1 <= m <= 10^6
1 <= k <= n
|
class Solution:
def minDays(self, bloomDay: List[int], m: int, k: int) -> int:
if m * k > len(bloomDay): return -1
left, right = 1, max(bloomDay)
while left < right:
mid = (left + right) // 2
flow = bouq = 0
for a in bloomDay:
flow = 0 if a > mid else flow + 1
if flow >= k:
flow = 0
bouq += 1
if bouq == m: break
if bouq == m:
right = mid
else:
left = mid + 1
return left
|
Given an integer array bloomDay, an integer m and an integer k.
We need to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.
The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be used in exactly one bouquet.
Return the minimum number of days you need to wait to be able to make m bouquets from the garden. If it is impossible to make m bouquets return -1.
Example 1:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 1
Output: 3
Explanation: Let's see what happened in the first three days. x means flower bloomed and _ means flower didn't bloom in the garden.
We need 3 bouquets each should contain 1 flower.
After day 1: [x, _, _, _, _] // we can only make one bouquet.
After day 2: [x, _, _, _, x] // we can only make two bouquets.
After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3.
Example 2:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 2
Output: -1
Explanation: We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1.
Example 3:
Input: bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
Output: 12
Explanation: We need 2 bouquets each should have 3 flowers.
Here's the garden after the 7 and 12 days:
After day 7: [x, x, x, x, _, x, x]
We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent.
After day 12: [x, x, x, x, x, x, x]
It is obvious that we can make two bouquets in different ways.
Example 4:
Input: bloomDay = [1000000000,1000000000], m = 1, k = 1
Output: 1000000000
Explanation: You need to wait 1000000000 days to have a flower ready for a bouquet.
Example 5:
Input: bloomDay = [1,10,2,9,3,8,4,7,5,6], m = 4, k = 2
Output: 9
Constraints:
bloomDay.length == n
1 <= n <= 10^5
1 <= bloomDay[i] <= 10^9
1 <= m <= 10^6
1 <= k <= n
|
class Solution:
def minDays(self, bloomDay: List[int], m: int, k: int) -> int:
## Function to check required bouquets can be made
## within the parameterized number of days.
def canMakeBouquet(m, k, bloomDay, currDay):
## Variable for keeping track of
## the number of bouquets made.
numBouquets = 0
## Variable for sliding window length.
slidingWindowLen = 0
for i in range(0, len(bloomDay)):
## Check whether the flower has bloomed or not.
if (bloomDay[i] <= currDay):
slidingWindowLen += 1
else:
slidingWindowLen = 0
## Check whether a bouquet can be constructed.
if (slidingWindowLen == k):
numBouquets += 1
slidingWindowLen = 0
## Check whether sufficient bouquets can be made.
return (numBouquets >= m)
## return canMakeBouquet(m, k, bloomDay, 4)
## Initialise binary search bounds.
lowBound = 1
highBound = max(bloomDay)
## Perform Binary Search.
while (lowBound < highBound):
## Compute the midBound.
midBound = lowBound + (highBound - lowBound)//2
## If bouquets cannot be made.
if (not canMakeBouquet(m, k, bloomDay, midBound)):
lowBound = midBound + 1
else:
highBound = midBound
## Perform sanity check.
if (canMakeBouquet(m, k, bloomDay, lowBound)):
return lowBound
else:
return -1
|
Given an integer array bloomDay, an integer m and an integer k.
We need to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.
The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be used in exactly one bouquet.
Return the minimum number of days you need to wait to be able to make m bouquets from the garden. If it is impossible to make m bouquets return -1.
Example 1:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 1
Output: 3
Explanation: Let's see what happened in the first three days. x means flower bloomed and _ means flower didn't bloom in the garden.
We need 3 bouquets each should contain 1 flower.
After day 1: [x, _, _, _, _] // we can only make one bouquet.
After day 2: [x, _, _, _, x] // we can only make two bouquets.
After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3.
Example 2:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 2
Output: -1
Explanation: We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1.
Example 3:
Input: bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
Output: 12
Explanation: We need 2 bouquets each should have 3 flowers.
Here's the garden after the 7 and 12 days:
After day 7: [x, x, x, x, _, x, x]
We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent.
After day 12: [x, x, x, x, x, x, x]
It is obvious that we can make two bouquets in different ways.
Example 4:
Input: bloomDay = [1000000000,1000000000], m = 1, k = 1
Output: 1000000000
Explanation: You need to wait 1000000000 days to have a flower ready for a bouquet.
Example 5:
Input: bloomDay = [1,10,2,9,3,8,4,7,5,6], m = 4, k = 2
Output: 9
Constraints:
bloomDay.length == n
1 <= n <= 10^5
1 <= bloomDay[i] <= 10^9
1 <= m <= 10^6
1 <= k <= n
|
class Solution:
def minDays(self, bloomDay, m: int, k: int) -> int:
beg = min(bloomDay)
end = max(bloomDay)
n = len(bloomDay)
res = -1
while (beg <= end):
mid = beg + int((end - beg) / 2)
#print(mid)
if (self.isValid(bloomDay, n, m, k, mid) >= m):
res = mid
end = mid - 1
else:
beg = mid + 1
return res
def isValid(self, bloomDay, n, m, k, mx):
sum = 0
boquet = 0
k1 = 0
for i in range(n):
k1 += 1
if (bloomDay[i] > mx):
k1 = 0
if(k1==k):
boquet+=1
k1=0
#print(\"boquet\",boquet)
return boquet
|
Given an integer array bloomDay, an integer m and an integer k.
We need to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.
The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be used in exactly one bouquet.
Return the minimum number of days you need to wait to be able to make m bouquets from the garden. If it is impossible to make m bouquets return -1.
Example 1:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 1
Output: 3
Explanation: Let's see what happened in the first three days. x means flower bloomed and _ means flower didn't bloom in the garden.
We need 3 bouquets each should contain 1 flower.
After day 1: [x, _, _, _, _] // we can only make one bouquet.
After day 2: [x, _, _, _, x] // we can only make two bouquets.
After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3.
Example 2:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 2
Output: -1
Explanation: We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1.
Example 3:
Input: bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
Output: 12
Explanation: We need 2 bouquets each should have 3 flowers.
Here's the garden after the 7 and 12 days:
After day 7: [x, x, x, x, _, x, x]
We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent.
After day 12: [x, x, x, x, x, x, x]
It is obvious that we can make two bouquets in different ways.
Example 4:
Input: bloomDay = [1000000000,1000000000], m = 1, k = 1
Output: 1000000000
Explanation: You need to wait 1000000000 days to have a flower ready for a bouquet.
Example 5:
Input: bloomDay = [1,10,2,9,3,8,4,7,5,6], m = 4, k = 2
Output: 9
Constraints:
bloomDay.length == n
1 <= n <= 10^5
1 <= bloomDay[i] <= 10^9
1 <= m <= 10^6
1 <= k <= n
|
class Solution:
def minDays(self, bloomDay: List[int], m: int, k: int) -> int:
if len(bloomDay) < m*k: return -1
# def helper(D_days):
# n_m = 0
# low = 0
# while low + k <= len(bloomDay):
# _range = bloomDay[low: low+k]
# isValid = True
# for fl in _range:
# if fl > D_days:
# low += 1
# isValid = False
# break
# if isValid:
# n_m += 1
# low += k
# if n_m < m: return +1
# else: return -1
def helper(D_days):
n_m = 0
n_k = 0
for index, fl in enumerate(bloomDay):
if fl > D_days:
n_k = 0
else:
n_k += 1
if n_k == k:
n_m +=1
n_k = 0
if n_m < m: return +1
else: return -1
# low = 0
# while low + k <= len(bloomDay):
# _range = bloomDay[low: low+k]
# isValid = True
# for fl in _range:
# if fl > D_days:
# low += 1
# isValid = False
# break
# if isValid:
# n_m += 1
# low += k
# if n_m < m: return +1
# else: return -1
# Binary search
def B_search():
low, high = 0, max(bloomDay)+1
while low < high:
mid = low + (high-low)//2
if helper(mid)==+1:
low = mid +1
else:
high = mid
return low
return B_search()
|
Given an integer array bloomDay, an integer m and an integer k.
We need to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.
The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be used in exactly one bouquet.
Return the minimum number of days you need to wait to be able to make m bouquets from the garden. If it is impossible to make m bouquets return -1.
Example 1:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 1
Output: 3
Explanation: Let's see what happened in the first three days. x means flower bloomed and _ means flower didn't bloom in the garden.
We need 3 bouquets each should contain 1 flower.
After day 1: [x, _, _, _, _] // we can only make one bouquet.
After day 2: [x, _, _, _, x] // we can only make two bouquets.
After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3.
Example 2:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 2
Output: -1
Explanation: We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1.
Example 3:
Input: bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
Output: 12
Explanation: We need 2 bouquets each should have 3 flowers.
Here's the garden after the 7 and 12 days:
After day 7: [x, x, x, x, _, x, x]
We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent.
After day 12: [x, x, x, x, x, x, x]
It is obvious that we can make two bouquets in different ways.
Example 4:
Input: bloomDay = [1000000000,1000000000], m = 1, k = 1
Output: 1000000000
Explanation: You need to wait 1000000000 days to have a flower ready for a bouquet.
Example 5:
Input: bloomDay = [1,10,2,9,3,8,4,7,5,6], m = 4, k = 2
Output: 9
Constraints:
bloomDay.length == n
1 <= n <= 10^5
1 <= bloomDay[i] <= 10^9
1 <= m <= 10^6
1 <= k <= n
|
class Solution:
def minDays(self, bloomDay: List[int], m: int, k: int) -> int:
low = float('inf')
high = float('-inf')
for day in bloomDay:
low = min(low, day)
high = max(high, day)
good = set()
i = 0
while low < high:
guess = (low + high) >> 1
count = 0
boquets = 0
for day in bloomDay:
if day <= guess:
count += 1
if count == k:
boquets += 1
count = 0
if boquets == m:
break
else:
count = 0
day += 1
if boquets < m:
low = guess + 1
else:
good.add(guess)
high = guess
else:
if not good:
count = 0
boquets = 0
for day in bloomDay:
if day <= high:
count += 1
if count == k:
boquets += 1
count = 0
if boquets == m:
return high
else:
count = 0
day += 1
return min(good) if good else -1
|
Given an integer array bloomDay, an integer m and an integer k.
We need to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.
The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be used in exactly one bouquet.
Return the minimum number of days you need to wait to be able to make m bouquets from the garden. If it is impossible to make m bouquets return -1.
Example 1:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 1
Output: 3
Explanation: Let's see what happened in the first three days. x means flower bloomed and _ means flower didn't bloom in the garden.
We need 3 bouquets each should contain 1 flower.
After day 1: [x, _, _, _, _] // we can only make one bouquet.
After day 2: [x, _, _, _, x] // we can only make two bouquets.
After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3.
Example 2:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 2
Output: -1
Explanation: We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1.
Example 3:
Input: bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
Output: 12
Explanation: We need 2 bouquets each should have 3 flowers.
Here's the garden after the 7 and 12 days:
After day 7: [x, x, x, x, _, x, x]
We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent.
After day 12: [x, x, x, x, x, x, x]
It is obvious that we can make two bouquets in different ways.
Example 4:
Input: bloomDay = [1000000000,1000000000], m = 1, k = 1
Output: 1000000000
Explanation: You need to wait 1000000000 days to have a flower ready for a bouquet.
Example 5:
Input: bloomDay = [1,10,2,9,3,8,4,7,5,6], m = 4, k = 2
Output: 9
Constraints:
bloomDay.length == n
1 <= n <= 10^5
1 <= bloomDay[i] <= 10^9
1 <= m <= 10^6
1 <= k <= n
|
class Solution:
def minDays(self, bloomDay: List[int], m: int, k: int) -> int:
def canMakeMBouquetsInKDays(max_days):
flowers = 0
bouquets = 0
for flower in range(len(bloomDay)):
if bloomDay[flower]<=max_days:
flowers+=1
else:
flowers=0
if flowers==k:
bouquets+=1
flowers=0
return bouquets>=m
if m*k>len(bloomDay):
return -1
start = 1
end = max(bloomDay)
while start<=end:
mid = (start+end)//2
if canMakeMBouquetsInKDays(mid):
end = mid-1
else:
start = mid+1
return start
|
Given an integer array bloomDay, an integer m and an integer k.
We need to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.
The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be used in exactly one bouquet.
Return the minimum number of days you need to wait to be able to make m bouquets from the garden. If it is impossible to make m bouquets return -1.
Example 1:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 1
Output: 3
Explanation: Let's see what happened in the first three days. x means flower bloomed and _ means flower didn't bloom in the garden.
We need 3 bouquets each should contain 1 flower.
After day 1: [x, _, _, _, _] // we can only make one bouquet.
After day 2: [x, _, _, _, x] // we can only make two bouquets.
After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3.
Example 2:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 2
Output: -1
Explanation: We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1.
Example 3:
Input: bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
Output: 12
Explanation: We need 2 bouquets each should have 3 flowers.
Here's the garden after the 7 and 12 days:
After day 7: [x, x, x, x, _, x, x]
We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent.
After day 12: [x, x, x, x, x, x, x]
It is obvious that we can make two bouquets in different ways.
Example 4:
Input: bloomDay = [1000000000,1000000000], m = 1, k = 1
Output: 1000000000
Explanation: You need to wait 1000000000 days to have a flower ready for a bouquet.
Example 5:
Input: bloomDay = [1,10,2,9,3,8,4,7,5,6], m = 4, k = 2
Output: 9
Constraints:
bloomDay.length == n
1 <= n <= 10^5
1 <= bloomDay[i] <= 10^9
1 <= m <= 10^6
1 <= k <= n
|
class Solution:
def minDays(self, bloomDay: List[int], m: int, k: int) -> int:
def possible(bloomDay, days, m, k):
bouq = 0
curr = 0
for i in range(len(bloomDay)):
if bloomDay[i] <= days:
curr += 1
else:
curr = 0
if curr == k:
bouq += 1
curr = 0
return bouq >= m
l = 0
r = max(bloomDay)
while l < r:
mid = (l + r) // 2
print(mid)
if possible(bloomDay, mid, m, k):
r = mid
else:
l = mid + 1
# print(l, possible(bloomDay, l, m, k))
if possible(bloomDay, l, m, k):
return l
return -1
|
Given an integer array bloomDay, an integer m and an integer k.
We need to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.
The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be used in exactly one bouquet.
Return the minimum number of days you need to wait to be able to make m bouquets from the garden. If it is impossible to make m bouquets return -1.
Example 1:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 1
Output: 3
Explanation: Let's see what happened in the first three days. x means flower bloomed and _ means flower didn't bloom in the garden.
We need 3 bouquets each should contain 1 flower.
After day 1: [x, _, _, _, _] // we can only make one bouquet.
After day 2: [x, _, _, _, x] // we can only make two bouquets.
After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3.
Example 2:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 2
Output: -1
Explanation: We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1.
Example 3:
Input: bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
Output: 12
Explanation: We need 2 bouquets each should have 3 flowers.
Here's the garden after the 7 and 12 days:
After day 7: [x, x, x, x, _, x, x]
We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent.
After day 12: [x, x, x, x, x, x, x]
It is obvious that we can make two bouquets in different ways.
Example 4:
Input: bloomDay = [1000000000,1000000000], m = 1, k = 1
Output: 1000000000
Explanation: You need to wait 1000000000 days to have a flower ready for a bouquet.
Example 5:
Input: bloomDay = [1,10,2,9,3,8,4,7,5,6], m = 4, k = 2
Output: 9
Constraints:
bloomDay.length == n
1 <= n <= 10^5
1 <= bloomDay[i] <= 10^9
1 <= m <= 10^6
1 <= k <= n
|
class Solution:
def minDays(self, bloomDay: List[int], m: int, k: int) -> int:
def possible(days):
flowers = 0
bouquets = 0
for bloom in bloomDay:
if bloom<=days:
flowers += 1
if flowers>=k:
bouquets += 1
flowers = 0
if bouquets>=m:
return True
else:
flowers = 0
return False
if len(bloomDay)<m*k:
return -1
l = 1
h = max(bloomDay)
while l<h:
days = (l+h)//2
if possible(days):
h = days
else:
l = days+1
return l
|
Given an integer array bloomDay, an integer m and an integer k.
We need to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.
The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be used in exactly one bouquet.
Return the minimum number of days you need to wait to be able to make m bouquets from the garden. If it is impossible to make m bouquets return -1.
Example 1:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 1
Output: 3
Explanation: Let's see what happened in the first three days. x means flower bloomed and _ means flower didn't bloom in the garden.
We need 3 bouquets each should contain 1 flower.
After day 1: [x, _, _, _, _] // we can only make one bouquet.
After day 2: [x, _, _, _, x] // we can only make two bouquets.
After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3.
Example 2:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 2
Output: -1
Explanation: We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1.
Example 3:
Input: bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
Output: 12
Explanation: We need 2 bouquets each should have 3 flowers.
Here's the garden after the 7 and 12 days:
After day 7: [x, x, x, x, _, x, x]
We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent.
After day 12: [x, x, x, x, x, x, x]
It is obvious that we can make two bouquets in different ways.
Example 4:
Input: bloomDay = [1000000000,1000000000], m = 1, k = 1
Output: 1000000000
Explanation: You need to wait 1000000000 days to have a flower ready for a bouquet.
Example 5:
Input: bloomDay = [1,10,2,9,3,8,4,7,5,6], m = 4, k = 2
Output: 9
Constraints:
bloomDay.length == n
1 <= n <= 10^5
1 <= bloomDay[i] <= 10^9
1 <= m <= 10^6
1 <= k <= n
|
class Node:
def __init__(self, val, left=None, right=None):
self.val = val
self.left = left
self.right = right
class BST:
def __init__(self, root = None):
self.root = root
def insert(self, value):
if not self.root:
self.root = Node(value)
else:
self._insert(self.root, value)
def _insert(self, root, v):
if v < root.val:
if not root.left:
root.left = Node(v)
else:
self._insert(root.left, v)
else:
if not root.right:
root.right = Node(v)
else:
self._insert(root.right, v)
def findBounds(self, value, lower, upper):
root = self.root
while root:
if root.val < value:
lower = root.val
root = root.right
else:
upper = root.val
root = root.left
return (lower, upper)
class Solution:
def minDays(self, bloomDay: List[int], m: int, k: int) -> int:
n = len(bloomDay)
if m*k > n: return -1
h = [(-d,f) for f,d in enumerate(bloomDay)]
heapq.heapify(h)
bst = BST()
fit = n//k
while True:
day, flower = heapq.heappop(h)
l, u = bst.findBounds(flower, -1, n)
fit += (flower-l-1)//k + (u-flower-1)//k - (u-l-1)//k
bst.insert(flower)
if fit < m:
return -day
|
Given an integer array bloomDay, an integer m and an integer k.
We need to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.
The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be used in exactly one bouquet.
Return the minimum number of days you need to wait to be able to make m bouquets from the garden. If it is impossible to make m bouquets return -1.
Example 1:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 1
Output: 3
Explanation: Let's see what happened in the first three days. x means flower bloomed and _ means flower didn't bloom in the garden.
We need 3 bouquets each should contain 1 flower.
After day 1: [x, _, _, _, _] // we can only make one bouquet.
After day 2: [x, _, _, _, x] // we can only make two bouquets.
After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3.
Example 2:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 2
Output: -1
Explanation: We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1.
Example 3:
Input: bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
Output: 12
Explanation: We need 2 bouquets each should have 3 flowers.
Here's the garden after the 7 and 12 days:
After day 7: [x, x, x, x, _, x, x]
We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent.
After day 12: [x, x, x, x, x, x, x]
It is obvious that we can make two bouquets in different ways.
Example 4:
Input: bloomDay = [1000000000,1000000000], m = 1, k = 1
Output: 1000000000
Explanation: You need to wait 1000000000 days to have a flower ready for a bouquet.
Example 5:
Input: bloomDay = [1,10,2,9,3,8,4,7,5,6], m = 4, k = 2
Output: 9
Constraints:
bloomDay.length == n
1 <= n <= 10^5
1 <= bloomDay[i] <= 10^9
1 <= m <= 10^6
1 <= k <= n
|
class Solution:
def minDays(self, bloomDay: List[int], m: int, k: int) -> int:
if len(bloomDay) < m * k:
return -1
min_day, max_day = min(bloomDay), max(bloomDay)
while min_day < max_day:
mid = min_day + (max_day - min_day) // 2
if self.check(bloomDay, mid, m , k):
max_day = mid
else:
min_day = mid + 1
return min_day
def check(self, bloomDay, day, m, k):
un_m = 0
last_bloom = -1
n_before_bloom = 0
for i, d in enumerate(bloomDay):
if d <= day:
n_before_bloom += 1
last_bloom = i
if n_before_bloom >= k:
un_m += 1
n_before_bloom -= k
else:
n_before_bloom = 0
return un_m >= m
|
Given an integer array bloomDay, an integer m and an integer k.
We need to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.
The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be used in exactly one bouquet.
Return the minimum number of days you need to wait to be able to make m bouquets from the garden. If it is impossible to make m bouquets return -1.
Example 1:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 1
Output: 3
Explanation: Let's see what happened in the first three days. x means flower bloomed and _ means flower didn't bloom in the garden.
We need 3 bouquets each should contain 1 flower.
After day 1: [x, _, _, _, _] // we can only make one bouquet.
After day 2: [x, _, _, _, x] // we can only make two bouquets.
After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3.
Example 2:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 2
Output: -1
Explanation: We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1.
Example 3:
Input: bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
Output: 12
Explanation: We need 2 bouquets each should have 3 flowers.
Here's the garden after the 7 and 12 days:
After day 7: [x, x, x, x, _, x, x]
We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent.
After day 12: [x, x, x, x, x, x, x]
It is obvious that we can make two bouquets in different ways.
Example 4:
Input: bloomDay = [1000000000,1000000000], m = 1, k = 1
Output: 1000000000
Explanation: You need to wait 1000000000 days to have a flower ready for a bouquet.
Example 5:
Input: bloomDay = [1,10,2,9,3,8,4,7,5,6], m = 4, k = 2
Output: 9
Constraints:
bloomDay.length == n
1 <= n <= 10^5
1 <= bloomDay[i] <= 10^9
1 <= m <= 10^6
1 <= k <= n
|
class Solution:
def minDays(self, bloomDay: List[int], m: int, k: int) -> int:
def canMakeBouquets(cap):
nBouquets = 0
nFlowers = 0
for i in range(len(bloomDay)):
if bloomDay[i] > cap:
nFlowers = 0
continue
nFlowers += 1
if nFlowers == k:
nBouquets += 1
nFlowers = 0
return nBouquets >= m
if m*k > len(bloomDay):
return -1
lo, hi = 1, max(bloomDay)
while lo < hi:
med = lo + (hi-lo)//2
if canMakeBouquets(med):
hi = med
else:
lo = med + 1
return hi
|
Given an integer array bloomDay, an integer m and an integer k.
We need to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.
The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be used in exactly one bouquet.
Return the minimum number of days you need to wait to be able to make m bouquets from the garden. If it is impossible to make m bouquets return -1.
Example 1:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 1
Output: 3
Explanation: Let's see what happened in the first three days. x means flower bloomed and _ means flower didn't bloom in the garden.
We need 3 bouquets each should contain 1 flower.
After day 1: [x, _, _, _, _] // we can only make one bouquet.
After day 2: [x, _, _, _, x] // we can only make two bouquets.
After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3.
Example 2:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 2
Output: -1
Explanation: We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1.
Example 3:
Input: bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
Output: 12
Explanation: We need 2 bouquets each should have 3 flowers.
Here's the garden after the 7 and 12 days:
After day 7: [x, x, x, x, _, x, x]
We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent.
After day 12: [x, x, x, x, x, x, x]
It is obvious that we can make two bouquets in different ways.
Example 4:
Input: bloomDay = [1000000000,1000000000], m = 1, k = 1
Output: 1000000000
Explanation: You need to wait 1000000000 days to have a flower ready for a bouquet.
Example 5:
Input: bloomDay = [1,10,2,9,3,8,4,7,5,6], m = 4, k = 2
Output: 9
Constraints:
bloomDay.length == n
1 <= n <= 10^5
1 <= bloomDay[i] <= 10^9
1 <= m <= 10^6
1 <= k <= n
|
class Solution:
def minDays(self, bloomDay: List[int], m: int, k: int) -> int:
if len(bloomDay)<m*k:
return -1
mi,ma = min(bloomDay),max(bloomDay)
while(mi<ma):
mid = (mi + ma)//2
curr=0
adj=0
for i in range(len(bloomDay)):
if bloomDay[i]>mid:
adj=0
else:
adj+=1
if adj==k:
adj=0
curr+=1
if curr>=m:
break
if curr<m:
mi = mid+1
else:
ma = mid
return (mi)
|
Given an integer array bloomDay, an integer m and an integer k.
We need to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.
The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be used in exactly one bouquet.
Return the minimum number of days you need to wait to be able to make m bouquets from the garden. If it is impossible to make m bouquets return -1.
Example 1:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 1
Output: 3
Explanation: Let's see what happened in the first three days. x means flower bloomed and _ means flower didn't bloom in the garden.
We need 3 bouquets each should contain 1 flower.
After day 1: [x, _, _, _, _] // we can only make one bouquet.
After day 2: [x, _, _, _, x] // we can only make two bouquets.
After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3.
Example 2:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 2
Output: -1
Explanation: We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1.
Example 3:
Input: bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
Output: 12
Explanation: We need 2 bouquets each should have 3 flowers.
Here's the garden after the 7 and 12 days:
After day 7: [x, x, x, x, _, x, x]
We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent.
After day 12: [x, x, x, x, x, x, x]
It is obvious that we can make two bouquets in different ways.
Example 4:
Input: bloomDay = [1000000000,1000000000], m = 1, k = 1
Output: 1000000000
Explanation: You need to wait 1000000000 days to have a flower ready for a bouquet.
Example 5:
Input: bloomDay = [1,10,2,9,3,8,4,7,5,6], m = 4, k = 2
Output: 9
Constraints:
bloomDay.length == n
1 <= n <= 10^5
1 <= bloomDay[i] <= 10^9
1 <= m <= 10^6
1 <= k <= n
|
class Solution:
def minDays(self, bloomDay: List[int], m: int, k: int) -> int:
if k * m > len(bloomDay):
return -1
def flowers(days):
boq, full = 0, 0
adj = False
for flower in bloomDay:
if flower <= days:
full += 1
else:
full = 0
if full == k:
full = 0
boq += 1
return boq >= m
left, right = 0, max(bloomDay)
while left < right:
mid = left + (right - left) // 2
if flowers(mid):
right = mid
else:
left = mid + 1
return left
|
Given an integer array bloomDay, an integer m and an integer k.
We need to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.
The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be used in exactly one bouquet.
Return the minimum number of days you need to wait to be able to make m bouquets from the garden. If it is impossible to make m bouquets return -1.
Example 1:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 1
Output: 3
Explanation: Let's see what happened in the first three days. x means flower bloomed and _ means flower didn't bloom in the garden.
We need 3 bouquets each should contain 1 flower.
After day 1: [x, _, _, _, _] // we can only make one bouquet.
After day 2: [x, _, _, _, x] // we can only make two bouquets.
After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3.
Example 2:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 2
Output: -1
Explanation: We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1.
Example 3:
Input: bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
Output: 12
Explanation: We need 2 bouquets each should have 3 flowers.
Here's the garden after the 7 and 12 days:
After day 7: [x, x, x, x, _, x, x]
We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent.
After day 12: [x, x, x, x, x, x, x]
It is obvious that we can make two bouquets in different ways.
Example 4:
Input: bloomDay = [1000000000,1000000000], m = 1, k = 1
Output: 1000000000
Explanation: You need to wait 1000000000 days to have a flower ready for a bouquet.
Example 5:
Input: bloomDay = [1,10,2,9,3,8,4,7,5,6], m = 4, k = 2
Output: 9
Constraints:
bloomDay.length == n
1 <= n <= 10^5
1 <= bloomDay[i] <= 10^9
1 <= m <= 10^6
1 <= k <= n
|
class Solution:
def minDays(self, bloomDay: List[int], m: int, k: int) -> int:
def feasible(days):
bouquets=flowers=0
for i in range(len(bloomDay)):
if bloomDay[i]<=days:
flowers+=1
if flowers==k:
bouquets+=1
flowers=0
if bouquets>=m:
return True
else:
flowers=0
return False
if m*k>len(bloomDay):
return -1
left,right=min(bloomDay),max(bloomDay)
while left<right:
mid=left+(right-left)//2
if feasible(mid):
right=mid
else:
left=mid+1
return left
|
Given an integer array bloomDay, an integer m and an integer k.
We need to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.
The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be used in exactly one bouquet.
Return the minimum number of days you need to wait to be able to make m bouquets from the garden. If it is impossible to make m bouquets return -1.
Example 1:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 1
Output: 3
Explanation: Let's see what happened in the first three days. x means flower bloomed and _ means flower didn't bloom in the garden.
We need 3 bouquets each should contain 1 flower.
After day 1: [x, _, _, _, _] // we can only make one bouquet.
After day 2: [x, _, _, _, x] // we can only make two bouquets.
After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3.
Example 2:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 2
Output: -1
Explanation: We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1.
Example 3:
Input: bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
Output: 12
Explanation: We need 2 bouquets each should have 3 flowers.
Here's the garden after the 7 and 12 days:
After day 7: [x, x, x, x, _, x, x]
We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent.
After day 12: [x, x, x, x, x, x, x]
It is obvious that we can make two bouquets in different ways.
Example 4:
Input: bloomDay = [1000000000,1000000000], m = 1, k = 1
Output: 1000000000
Explanation: You need to wait 1000000000 days to have a flower ready for a bouquet.
Example 5:
Input: bloomDay = [1,10,2,9,3,8,4,7,5,6], m = 4, k = 2
Output: 9
Constraints:
bloomDay.length == n
1 <= n <= 10^5
1 <= bloomDay[i] <= 10^9
1 <= m <= 10^6
1 <= k <= n
|
class Solution:
def minDays(self, bloomDay: List[int], m: int, k: int) -> int:
n = max(bloomDay)
if m*k > len(bloomDay):
return -1
l, r = 1, n
while l <= r:
mid = (l+r)//2
count, rec = 0, 0
for i in range(len(bloomDay)):
if bloomDay[i] <= mid:
count += 1
if count >= k:
rec += 1
count = 0
if rec >= m:
break
else:
count = 0
if rec < m: #need more bouquets, increase the mid makes it easy to collect flowers
l = mid+1
else:
r = mid-1
return l
|
Given an integer array bloomDay, an integer m and an integer k.
We need to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.
The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be used in exactly one bouquet.
Return the minimum number of days you need to wait to be able to make m bouquets from the garden. If it is impossible to make m bouquets return -1.
Example 1:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 1
Output: 3
Explanation: Let's see what happened in the first three days. x means flower bloomed and _ means flower didn't bloom in the garden.
We need 3 bouquets each should contain 1 flower.
After day 1: [x, _, _, _, _] // we can only make one bouquet.
After day 2: [x, _, _, _, x] // we can only make two bouquets.
After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3.
Example 2:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 2
Output: -1
Explanation: We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1.
Example 3:
Input: bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
Output: 12
Explanation: We need 2 bouquets each should have 3 flowers.
Here's the garden after the 7 and 12 days:
After day 7: [x, x, x, x, _, x, x]
We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent.
After day 12: [x, x, x, x, x, x, x]
It is obvious that we can make two bouquets in different ways.
Example 4:
Input: bloomDay = [1000000000,1000000000], m = 1, k = 1
Output: 1000000000
Explanation: You need to wait 1000000000 days to have a flower ready for a bouquet.
Example 5:
Input: bloomDay = [1,10,2,9,3,8,4,7,5,6], m = 4, k = 2
Output: 9
Constraints:
bloomDay.length == n
1 <= n <= 10^5
1 <= bloomDay[i] <= 10^9
1 <= m <= 10^6
1 <= k <= n
|
class Solution:
def minDays(self, b: List[int], m: int, k: int) -> int:
mi,ma=min(b),max(b)
if m*k>len(b):
return -1
n=len(b)
cur,adj=0,0
while(mi<ma):
mid=(mi+ma)//2
cur,adj=0,0
for i in range(n):
if(mid<b[i]):
adj=0
else:
adj+=1
if(adj==k):
cur+=1
adj=0
if(cur>=m):
break
if(cur<m):
mi=mid+1
else:
ma=mid
return mi
|
Given an integer array bloomDay, an integer m and an integer k.
We need to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.
The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be used in exactly one bouquet.
Return the minimum number of days you need to wait to be able to make m bouquets from the garden. If it is impossible to make m bouquets return -1.
Example 1:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 1
Output: 3
Explanation: Let's see what happened in the first three days. x means flower bloomed and _ means flower didn't bloom in the garden.
We need 3 bouquets each should contain 1 flower.
After day 1: [x, _, _, _, _] // we can only make one bouquet.
After day 2: [x, _, _, _, x] // we can only make two bouquets.
After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3.
Example 2:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 2
Output: -1
Explanation: We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1.
Example 3:
Input: bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
Output: 12
Explanation: We need 2 bouquets each should have 3 flowers.
Here's the garden after the 7 and 12 days:
After day 7: [x, x, x, x, _, x, x]
We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent.
After day 12: [x, x, x, x, x, x, x]
It is obvious that we can make two bouquets in different ways.
Example 4:
Input: bloomDay = [1000000000,1000000000], m = 1, k = 1
Output: 1000000000
Explanation: You need to wait 1000000000 days to have a flower ready for a bouquet.
Example 5:
Input: bloomDay = [1,10,2,9,3,8,4,7,5,6], m = 4, k = 2
Output: 9
Constraints:
bloomDay.length == n
1 <= n <= 10^5
1 <= bloomDay[i] <= 10^9
1 <= m <= 10^6
1 <= k <= n
|
from collections import Counter
from heapq import *
class Solution:
def minDays(self, bloomDay: List[int], m: int, k: int) -> int:
if k*m > len(bloomDay): return -1
que = [[-1*num,ind] for ind,num in enumerate(bloomDay[:k])]
heapify(que)
maxArr = [-1*que[0][0]]
for ind in range(k,len(bloomDay)):
while que and que[0][1] <= ind-k:
heappop(que)
heappush(que, [-1*bloomDay[ind], ind])
maxArr.append(-1*que[0][0])
# print(maxArr)
def isValid(num):
cnt = 0
ind = 0
while ind < len(maxArr):
if maxArr[ind] <= num:
cnt+=1
ind+=k
else:
ind+=1
# print(\"{0} => {1}\".format(num, cnt))
return cnt >= m
low = 1
high = max(bloomDay)+1
ans = -1
while low < high:
mid = low + (high-low)//2
if isValid(mid):
high = mid
ans = mid
else:
low=mid+1
return ans
|
Given an integer array bloomDay, an integer m and an integer k.
We need to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.
The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be used in exactly one bouquet.
Return the minimum number of days you need to wait to be able to make m bouquets from the garden. If it is impossible to make m bouquets return -1.
Example 1:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 1
Output: 3
Explanation: Let's see what happened in the first three days. x means flower bloomed and _ means flower didn't bloom in the garden.
We need 3 bouquets each should contain 1 flower.
After day 1: [x, _, _, _, _] // we can only make one bouquet.
After day 2: [x, _, _, _, x] // we can only make two bouquets.
After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3.
Example 2:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 2
Output: -1
Explanation: We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1.
Example 3:
Input: bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
Output: 12
Explanation: We need 2 bouquets each should have 3 flowers.
Here's the garden after the 7 and 12 days:
After day 7: [x, x, x, x, _, x, x]
We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent.
After day 12: [x, x, x, x, x, x, x]
It is obvious that we can make two bouquets in different ways.
Example 4:
Input: bloomDay = [1000000000,1000000000], m = 1, k = 1
Output: 1000000000
Explanation: You need to wait 1000000000 days to have a flower ready for a bouquet.
Example 5:
Input: bloomDay = [1,10,2,9,3,8,4,7,5,6], m = 4, k = 2
Output: 9
Constraints:
bloomDay.length == n
1 <= n <= 10^5
1 <= bloomDay[i] <= 10^9
1 <= m <= 10^6
1 <= k <= n
|
class Solution:
def minDays(self, bloomDay: List[int], m: int, k: int) -> int:
if len(bloomDay) // k < m:
return -1
low, high = min(bloomDay), max(bloomDay)
def valid(days):
start = 0
count = 0
for i, val in enumerate(bloomDay):
if val <= days:
if i - start + 1 >= k:
count += 1
start = i + 1
else:
start = i + 1
return count >= m
while low < high:
days = (low + high) // 2
if valid(days):
high = days
else:
low = days + 1
return low
|
Given an integer array bloomDay, an integer m and an integer k.
We need to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.
The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be used in exactly one bouquet.
Return the minimum number of days you need to wait to be able to make m bouquets from the garden. If it is impossible to make m bouquets return -1.
Example 1:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 1
Output: 3
Explanation: Let's see what happened in the first three days. x means flower bloomed and _ means flower didn't bloom in the garden.
We need 3 bouquets each should contain 1 flower.
After day 1: [x, _, _, _, _] // we can only make one bouquet.
After day 2: [x, _, _, _, x] // we can only make two bouquets.
After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3.
Example 2:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 2
Output: -1
Explanation: We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1.
Example 3:
Input: bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
Output: 12
Explanation: We need 2 bouquets each should have 3 flowers.
Here's the garden after the 7 and 12 days:
After day 7: [x, x, x, x, _, x, x]
We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent.
After day 12: [x, x, x, x, x, x, x]
It is obvious that we can make two bouquets in different ways.
Example 4:
Input: bloomDay = [1000000000,1000000000], m = 1, k = 1
Output: 1000000000
Explanation: You need to wait 1000000000 days to have a flower ready for a bouquet.
Example 5:
Input: bloomDay = [1,10,2,9,3,8,4,7,5,6], m = 4, k = 2
Output: 9
Constraints:
bloomDay.length == n
1 <= n <= 10^5
1 <= bloomDay[i] <= 10^9
1 <= m <= 10^6
1 <= k <= n
|
class Solution:
def minDays(self, bloomDay: List[int], m: int, k: int) -> int:
if m*k>len(bloomDay):
return -1
def check(days):
i = 0
bouquet = 0
count = 0
for i in range(len(bloomDay)):
if bloomDay[i]<=days:
count+=1
else:
count = 0
if count == k:
bouquet+=1
count = 0
if bouquet == m:
return True
return False
low,high = min(bloomDay),max(bloomDay)
while low<high:
mid = low + (high-low)//2
possible = check(mid)
if possible:
high = mid
else:
low = mid+1
return low
|
Given an integer array bloomDay, an integer m and an integer k.
We need to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.
The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be used in exactly one bouquet.
Return the minimum number of days you need to wait to be able to make m bouquets from the garden. If it is impossible to make m bouquets return -1.
Example 1:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 1
Output: 3
Explanation: Let's see what happened in the first three days. x means flower bloomed and _ means flower didn't bloom in the garden.
We need 3 bouquets each should contain 1 flower.
After day 1: [x, _, _, _, _] // we can only make one bouquet.
After day 2: [x, _, _, _, x] // we can only make two bouquets.
After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3.
Example 2:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 2
Output: -1
Explanation: We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1.
Example 3:
Input: bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
Output: 12
Explanation: We need 2 bouquets each should have 3 flowers.
Here's the garden after the 7 and 12 days:
After day 7: [x, x, x, x, _, x, x]
We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent.
After day 12: [x, x, x, x, x, x, x]
It is obvious that we can make two bouquets in different ways.
Example 4:
Input: bloomDay = [1000000000,1000000000], m = 1, k = 1
Output: 1000000000
Explanation: You need to wait 1000000000 days to have a flower ready for a bouquet.
Example 5:
Input: bloomDay = [1,10,2,9,3,8,4,7,5,6], m = 4, k = 2
Output: 9
Constraints:
bloomDay.length == n
1 <= n <= 10^5
1 <= bloomDay[i] <= 10^9
1 <= m <= 10^6
1 <= k <= n
|
class Solution:
def minDays(self, bloomDay: List[int], m: int, k: int) -> int:
n = len(bloomDay)
if k*m > n:
return -1
l, r = 1, max(bloomDay)
ans = -1
while l <= r:
mid = int((l+r)/2)
bouquetCount = 0
adjacentFlowerCount = 0
for bloom in bloomDay:
if bloom <= mid:
adjacentFlowerCount += 1
if adjacentFlowerCount == k:
bouquetCount += 1
adjacentFlowerCount = 0
if bouquetCount == m:
break
else:
adjacentFlowerCount = 0
if bouquetCount == m:
ans = mid
r = mid - 1
else:
l = mid + 1
return ans
|
Given an integer array bloomDay, an integer m and an integer k.
We need to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.
The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be used in exactly one bouquet.
Return the minimum number of days you need to wait to be able to make m bouquets from the garden. If it is impossible to make m bouquets return -1.
Example 1:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 1
Output: 3
Explanation: Let's see what happened in the first three days. x means flower bloomed and _ means flower didn't bloom in the garden.
We need 3 bouquets each should contain 1 flower.
After day 1: [x, _, _, _, _] // we can only make one bouquet.
After day 2: [x, _, _, _, x] // we can only make two bouquets.
After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3.
Example 2:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 2
Output: -1
Explanation: We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1.
Example 3:
Input: bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
Output: 12
Explanation: We need 2 bouquets each should have 3 flowers.
Here's the garden after the 7 and 12 days:
After day 7: [x, x, x, x, _, x, x]
We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent.
After day 12: [x, x, x, x, x, x, x]
It is obvious that we can make two bouquets in different ways.
Example 4:
Input: bloomDay = [1000000000,1000000000], m = 1, k = 1
Output: 1000000000
Explanation: You need to wait 1000000000 days to have a flower ready for a bouquet.
Example 5:
Input: bloomDay = [1,10,2,9,3,8,4,7,5,6], m = 4, k = 2
Output: 9
Constraints:
bloomDay.length == n
1 <= n <= 10^5
1 <= bloomDay[i] <= 10^9
1 <= m <= 10^6
1 <= k <= n
|
class Solution:
def minDays(self, bloomDay: List[int], m: int, k: int) -> int:
if m*k > len(bloomDay):
return -1
lo = min(bloomDay)
hi = max(bloomDay)
while lo < hi:
mid = lo + (hi-lo)//2
pos_b = 0
cur_b = 0
for f in bloomDay:
if f <= mid:
cur_b += 1
else:
cur_b = 0
if cur_b == k:
pos_b += 1
cur_b = 0
if pos_b >= m:
hi = mid
else:
lo = mid+1
return lo
|
Given an integer array bloomDay, an integer m and an integer k.
We need to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.
The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be used in exactly one bouquet.
Return the minimum number of days you need to wait to be able to make m bouquets from the garden. If it is impossible to make m bouquets return -1.
Example 1:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 1
Output: 3
Explanation: Let's see what happened in the first three days. x means flower bloomed and _ means flower didn't bloom in the garden.
We need 3 bouquets each should contain 1 flower.
After day 1: [x, _, _, _, _] // we can only make one bouquet.
After day 2: [x, _, _, _, x] // we can only make two bouquets.
After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3.
Example 2:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 2
Output: -1
Explanation: We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1.
Example 3:
Input: bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
Output: 12
Explanation: We need 2 bouquets each should have 3 flowers.
Here's the garden after the 7 and 12 days:
After day 7: [x, x, x, x, _, x, x]
We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent.
After day 12: [x, x, x, x, x, x, x]
It is obvious that we can make two bouquets in different ways.
Example 4:
Input: bloomDay = [1000000000,1000000000], m = 1, k = 1
Output: 1000000000
Explanation: You need to wait 1000000000 days to have a flower ready for a bouquet.
Example 5:
Input: bloomDay = [1,10,2,9,3,8,4,7,5,6], m = 4, k = 2
Output: 9
Constraints:
bloomDay.length == n
1 <= n <= 10^5
1 <= bloomDay[i] <= 10^9
1 <= m <= 10^6
1 <= k <= n
|
class Solution:
def minDays(self, bloomDay: List[int], m: int, k: int) -> int:
def feasible(day):
bou = 0
flowers = 0
for bloom in bloomDay:
if bloom > day:
flowers = 0
else:
bou += (flowers+1)//k
flowers = (flowers+1)%k
if bou < m:
return False
return True
if len(bloomDay) < (m*k):
return -1
left = 1
right = max(bloomDay)
while left < right :
mid = (left+right)//2
if feasible(mid):
right = mid
else: left = mid + 1
return left
|
Given an integer array bloomDay, an integer m and an integer k.
We need to make m bouquets. To make a bouquet, you need to use k adjacent flowers from the garden.
The garden consists of n flowers, the ith flower will bloom in the bloomDay[i] and then can be used in exactly one bouquet.
Return the minimum number of days you need to wait to be able to make m bouquets from the garden. If it is impossible to make m bouquets return -1.
Example 1:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 1
Output: 3
Explanation: Let's see what happened in the first three days. x means flower bloomed and _ means flower didn't bloom in the garden.
We need 3 bouquets each should contain 1 flower.
After day 1: [x, _, _, _, _] // we can only make one bouquet.
After day 2: [x, _, _, _, x] // we can only make two bouquets.
After day 3: [x, _, x, _, x] // we can make 3 bouquets. The answer is 3.
Example 2:
Input: bloomDay = [1,10,3,10,2], m = 3, k = 2
Output: -1
Explanation: We need 3 bouquets each has 2 flowers, that means we need 6 flowers. We only have 5 flowers so it is impossible to get the needed bouquets and we return -1.
Example 3:
Input: bloomDay = [7,7,7,7,12,7,7], m = 2, k = 3
Output: 12
Explanation: We need 2 bouquets each should have 3 flowers.
Here's the garden after the 7 and 12 days:
After day 7: [x, x, x, x, _, x, x]
We can make one bouquet of the first three flowers that bloomed. We cannot make another bouquet from the last three flowers that bloomed because they are not adjacent.
After day 12: [x, x, x, x, x, x, x]
It is obvious that we can make two bouquets in different ways.
Example 4:
Input: bloomDay = [1000000000,1000000000], m = 1, k = 1
Output: 1000000000
Explanation: You need to wait 1000000000 days to have a flower ready for a bouquet.
Example 5:
Input: bloomDay = [1,10,2,9,3,8,4,7,5,6], m = 4, k = 2
Output: 9
Constraints:
bloomDay.length == n
1 <= n <= 10^5
1 <= bloomDay[i] <= 10^9
1 <= m <= 10^6
1 <= k <= n
|
class Solution:
def minDays(self, B: List[int], m: int, k: int) -> int:
n = len(B)
if m*k>n: return -1
lo = min(B)
hi = max(B)+1
while lo<hi:
mi = lo+(hi-lo)//2
bouquets = 0
curflowers = 0
for i in range(n):
if B[i]<=mi:
curflowers += 1
else:
curflowers = 0
if curflowers == k:
bouquets += 1
curflowers = 0
#print(m,i,curflowers,bouquets)
if bouquets == m:
#print(m,i,'here')
hi = mi
break
if hi>mi:
lo = mi+1
return lo
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.