Datasets:

inputs
stringlengths
50
14k
targets
stringlengths
4
655k
Normally, the factorial of a positive integer n is the product of all positive integers less than or equal to n.  For example, factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1. We instead make a clumsy factorial: using the integers in decreasing order, we swap out the multiply operations for a fixed rotation of operations: multiply (*), divide (/), add (+) and subtract (-) in this order. For example, clumsy(10) = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1.  However, these operations are still applied using the usual order of operations of arithmetic: we do all multiplication and division steps before any addition or subtraction steps, and multiplication and division steps are processed left to right. Additionally, the division that we use is floor division such that 10 * 9 / 8 equals 11.  This guarantees the result is an integer. Implement the clumsy function as defined above: given an integer N, it returns the clumsy factorial of N.   Example 1: Input: 4 Output: 7 Explanation: 7 = 4 * 3 / 2 + 1 Example 2: Input: 10 Output: 12 Explanation: 12 = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1   Note: 1 <= N <= 10000 -2^31 <= answer <= 2^31 - 1  (The answer is guaranteed to fit within a 32-bit integer.)
class Solution: def clumsy(self, N: int) -> int: all_nums = [i for i in range(N, 0, -1)] prio_nums = [] for i in range(0, len(all_nums) - 3, 4): prio_nums.append((all_nums[i] * all_nums[i + 1]) // all_nums[i + 2]) prio_nums.append(all_nums[i + 3]) if N % 4 == 3: prio_nums.append((all_nums[-3] * all_nums[-2]) // all_nums[-1]) elif N % 4 == 2: prio_nums.append(all_nums[-2] * all_nums[-1]) elif N % 4 == 1: prio_nums.append(all_nums[-1]) total = prio_nums[0] print(prio_nums) for i in range(1, len(prio_nums)): if i % 2 != 0: total = total + prio_nums[i] else: total = total - prio_nums[i] return total
Normally, the factorial of a positive integer n is the product of all positive integers less than or equal to n.  For example, factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1. We instead make a clumsy factorial: using the integers in decreasing order, we swap out the multiply operations for a fixed rotation of operations: multiply (*), divide (/), add (+) and subtract (-) in this order. For example, clumsy(10) = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1.  However, these operations are still applied using the usual order of operations of arithmetic: we do all multiplication and division steps before any addition or subtraction steps, and multiplication and division steps are processed left to right. Additionally, the division that we use is floor division such that 10 * 9 / 8 equals 11.  This guarantees the result is an integer. Implement the clumsy function as defined above: given an integer N, it returns the clumsy factorial of N.   Example 1: Input: 4 Output: 7 Explanation: 7 = 4 * 3 / 2 + 1 Example 2: Input: 10 Output: 12 Explanation: 12 = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1   Note: 1 <= N <= 10000 -2^31 <= answer <= 2^31 - 1  (The answer is guaranteed to fit within a 32-bit integer.)
class Solution: def clumsy(self, N: int) -> int: temp = [i for i in range(N,0,-1)] # temp[0] *= -1 if N > 3: s = (temp[0]*temp[1]//temp[2]+temp[3]) for i in range(1,N//4): s -= (temp[i*4]*temp[i*4+1]//temp[i*4+2] - temp[i*4+3]) print(s) # print(temp[i*4]*temp[i*4+1]//temp[i*4+2]) if N % 4 == 3: s -= temp[-3] * temp[-2] // temp[-1] elif N % 4 == 2: s -= temp[-2] * temp[-1] elif N % 4 == 1: s -= temp[-1] else: s = 0 if N % 4 == 3: s += temp[-3] * temp[-2] // temp[-1] elif N % 4 == 2: s += temp[-2] * temp[-1] elif N % 4 == 1: s += temp[-1] return s
Normally, the factorial of a positive integer n is the product of all positive integers less than or equal to n.  For example, factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1. We instead make a clumsy factorial: using the integers in decreasing order, we swap out the multiply operations for a fixed rotation of operations: multiply (*), divide (/), add (+) and subtract (-) in this order. For example, clumsy(10) = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1.  However, these operations are still applied using the usual order of operations of arithmetic: we do all multiplication and division steps before any addition or subtraction steps, and multiplication and division steps are processed left to right. Additionally, the division that we use is floor division such that 10 * 9 / 8 equals 11.  This guarantees the result is an integer. Implement the clumsy function as defined above: given an integer N, it returns the clumsy factorial of N.   Example 1: Input: 4 Output: 7 Explanation: 7 = 4 * 3 / 2 + 1 Example 2: Input: 10 Output: 12 Explanation: 12 = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1   Note: 1 <= N <= 10000 -2^31 <= answer <= 2^31 - 1  (The answer is guaranteed to fit within a 32-bit integer.)
class Solution: def clumsy(self, N: int) -> int: count=1 ans=[N] for i in range(N-1,0,-1): if count%4==1: ans[-1]*=i elif count%4==2: ans[-1]=int(ans[-1]/i) elif count%4==3: ans.append(i) else: ans.append(-i) count+=1 return sum(ans)
Normally, the factorial of a positive integer n is the product of all positive integers less than or equal to n.  For example, factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1. We instead make a clumsy factorial: using the integers in decreasing order, we swap out the multiply operations for a fixed rotation of operations: multiply (*), divide (/), add (+) and subtract (-) in this order. For example, clumsy(10) = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1.  However, these operations are still applied using the usual order of operations of arithmetic: we do all multiplication and division steps before any addition or subtraction steps, and multiplication and division steps are processed left to right. Additionally, the division that we use is floor division such that 10 * 9 / 8 equals 11.  This guarantees the result is an integer. Implement the clumsy function as defined above: given an integer N, it returns the clumsy factorial of N.   Example 1: Input: 4 Output: 7 Explanation: 7 = 4 * 3 / 2 + 1 Example 2: Input: 10 Output: 12 Explanation: 12 = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1   Note: 1 <= N <= 10000 -2^31 <= answer <= 2^31 - 1  (The answer is guaranteed to fit within a 32-bit integer.)
class Solution: def clumsy(self, N: int) -> int: flag=1 temp=0 cur=0 i=1 for j in range(N,0,-1): if(i%4==1): cur=flag*j elif(i%4==2): cur*=j elif(i%4==3): cur=int(cur/j) else: cur+=j temp+=cur flag=-1 cur=0 i+=1 return temp+cur
Normally, the factorial of a positive integer n is the product of all positive integers less than or equal to n.  For example, factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1. We instead make a clumsy factorial: using the integers in decreasing order, we swap out the multiply operations for a fixed rotation of operations: multiply (*), divide (/), add (+) and subtract (-) in this order. For example, clumsy(10) = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1.  However, these operations are still applied using the usual order of operations of arithmetic: we do all multiplication and division steps before any addition or subtraction steps, and multiplication and division steps are processed left to right. Additionally, the division that we use is floor division such that 10 * 9 / 8 equals 11.  This guarantees the result is an integer. Implement the clumsy function as defined above: given an integer N, it returns the clumsy factorial of N.   Example 1: Input: 4 Output: 7 Explanation: 7 = 4 * 3 / 2 + 1 Example 2: Input: 10 Output: 12 Explanation: 12 = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1   Note: 1 <= N <= 10000 -2^31 <= answer <= 2^31 - 1  (The answer is guaranteed to fit within a 32-bit integer.)
class Solution: def clumsy(self, N: int) -> int: numbers = list(range(N + 1))[1:] numbers.reverse() inter_results = [] for idx, v in enumerate(numbers): if idx % 4 == 0: inter_results.append(v) elif idx % 4 == 1: inter_results[-1] *= v elif idx % 4 == 2: inter_results[-1] = int(inter_results[-1] / v) elif idx % 4 == 3: inter_results.append(v) # print(inter_results) final_results = 0 for idx, v in enumerate(inter_results): if idx == 0: final_results += v elif idx % 2 == 1: final_results += v elif idx % 2 == 0: final_results -= v return final_results
Normally, the factorial of a positive integer n is the product of all positive integers less than or equal to n.  For example, factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1. We instead make a clumsy factorial: using the integers in decreasing order, we swap out the multiply operations for a fixed rotation of operations: multiply (*), divide (/), add (+) and subtract (-) in this order. For example, clumsy(10) = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1.  However, these operations are still applied using the usual order of operations of arithmetic: we do all multiplication and division steps before any addition or subtraction steps, and multiplication and division steps are processed left to right. Additionally, the division that we use is floor division such that 10 * 9 / 8 equals 11.  This guarantees the result is an integer. Implement the clumsy function as defined above: given an integer N, it returns the clumsy factorial of N.   Example 1: Input: 4 Output: 7 Explanation: 7 = 4 * 3 / 2 + 1 Example 2: Input: 10 Output: 12 Explanation: 12 = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1   Note: 1 <= N <= 10000 -2^31 <= answer <= 2^31 - 1  (The answer is guaranteed to fit within a 32-bit integer.)
class Solution: def clumsy(self, n: int) -> int: if n == 1: return 1 arr = [] temp = 0 for i in range(n,0,-1): if temp == 0 or temp == 3: arr.append(i) elif temp == 1: arr[len(arr)-1] = arr[len(arr)-1]*i elif temp == 2: arr[len(arr)-1] = arr[len(arr)-1]//i temp = (temp+1)%4 c = arr[0] for i in range(1,len(arr)): if i%2 == 1: c+=arr[i] else: c-=arr[i] return c
Normally, the factorial of a positive integer n is the product of all positive integers less than or equal to n.  For example, factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1. We instead make a clumsy factorial: using the integers in decreasing order, we swap out the multiply operations for a fixed rotation of operations: multiply (*), divide (/), add (+) and subtract (-) in this order. For example, clumsy(10) = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1.  However, these operations are still applied using the usual order of operations of arithmetic: we do all multiplication and division steps before any addition or subtraction steps, and multiplication and division steps are processed left to right. Additionally, the division that we use is floor division such that 10 * 9 / 8 equals 11.  This guarantees the result is an integer. Implement the clumsy function as defined above: given an integer N, it returns the clumsy factorial of N.   Example 1: Input: 4 Output: 7 Explanation: 7 = 4 * 3 / 2 + 1 Example 2: Input: 10 Output: 12 Explanation: 12 = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1   Note: 1 <= N <= 10000 -2^31 <= answer <= 2^31 - 1  (The answer is guaranteed to fit within a 32-bit integer.)
class Solution: def clumsy(self, N: int) -> int: op = 0 s = N N = N - 1 sum_list = [] sign_list = [1] while N > 0: if op == 0: s = s * N elif op == 1: s = s // N elif op == 2: sum_list.append(s) sign_list.append(1) s = N else: sum_list.append(s) sign_list.append(-1) s = N op = (op + 1) % 4 N -= 1 sum_list.append(s) s = 0 for i, v in enumerate(sign_list): s+= sign_list[i] * sum_list[i] return s
Normally, the factorial of a positive integer n is the product of all positive integers less than or equal to n.  For example, factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1. We instead make a clumsy factorial: using the integers in decreasing order, we swap out the multiply operations for a fixed rotation of operations: multiply (*), divide (/), add (+) and subtract (-) in this order. For example, clumsy(10) = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1.  However, these operations are still applied using the usual order of operations of arithmetic: we do all multiplication and division steps before any addition or subtraction steps, and multiplication and division steps are processed left to right. Additionally, the division that we use is floor division such that 10 * 9 / 8 equals 11.  This guarantees the result is an integer. Implement the clumsy function as defined above: given an integer N, it returns the clumsy factorial of N.   Example 1: Input: 4 Output: 7 Explanation: 7 = 4 * 3 / 2 + 1 Example 2: Input: 10 Output: 12 Explanation: 12 = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1   Note: 1 <= N <= 10000 -2^31 <= answer <= 2^31 - 1  (The answer is guaranteed to fit within a 32-bit integer.)
class Solution: def clumsy(self, N: int) -> int: stack = [] j = -1 for i in range(N, 0, -1): stack.append(i) if (j % 4 == 0 or j % 4 == 1 and len(stack) > 1): x = stack.pop() y = stack.pop() if (j % 4 == 0): stack.append(x*y) elif (j % 4 == 1): stack.append(y//x) j += 1 res = stack[0] for j in range(1,len(stack), 2): res += stack[j] if (j != len(stack) - 1): res -= stack[j+1] return res
Normally, the factorial of a positive integer n is the product of all positive integers less than or equal to n.  For example, factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1. We instead make a clumsy factorial: using the integers in decreasing order, we swap out the multiply operations for a fixed rotation of operations: multiply (*), divide (/), add (+) and subtract (-) in this order. For example, clumsy(10) = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1.  However, these operations are still applied using the usual order of operations of arithmetic: we do all multiplication and division steps before any addition or subtraction steps, and multiplication and division steps are processed left to right. Additionally, the division that we use is floor division such that 10 * 9 / 8 equals 11.  This guarantees the result is an integer. Implement the clumsy function as defined above: given an integer N, it returns the clumsy factorial of N.   Example 1: Input: 4 Output: 7 Explanation: 7 = 4 * 3 / 2 + 1 Example 2: Input: 10 Output: 12 Explanation: 12 = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1   Note: 1 <= N <= 10000 -2^31 <= answer <= 2^31 - 1  (The answer is guaranteed to fit within a 32-bit integer.)
class Solution: def clumsy(self, N: int) -> int: result = N product_part = 0 sum_part = 0 def g(x): return N - x + 1 for i in range(0, ceil(N / 4)): nums = list(range(4 * i + 1, min(4 * i + 5, N + 1))) p = g(nums[0]) s = 0 if len(nums) > 1: p *= g(nums[1]) if len(nums) > 2: p //= g(nums[2]) if len(nums) > 3: s = g(nums[3]) if i > 0: p *= -1 product_part += p sum_part += s return product_part + sum_part
Normally, the factorial of a positive integer n is the product of all positive integers less than or equal to n.  For example, factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1. We instead make a clumsy factorial: using the integers in decreasing order, we swap out the multiply operations for a fixed rotation of operations: multiply (*), divide (/), add (+) and subtract (-) in this order. For example, clumsy(10) = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1.  However, these operations are still applied using the usual order of operations of arithmetic: we do all multiplication and division steps before any addition or subtraction steps, and multiplication and division steps are processed left to right. Additionally, the division that we use is floor division such that 10 * 9 / 8 equals 11.  This guarantees the result is an integer. Implement the clumsy function as defined above: given an integer N, it returns the clumsy factorial of N.   Example 1: Input: 4 Output: 7 Explanation: 7 = 4 * 3 / 2 + 1 Example 2: Input: 10 Output: 12 Explanation: 12 = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1   Note: 1 <= N <= 10000 -2^31 <= answer <= 2^31 - 1  (The answer is guaranteed to fit within a 32-bit integer.)
class Solution: def clumsy(self, N: int) -> int: if N == 1: return N i = N - 1 operations = [] curr = N which_op = 0 while i >= 1: if which_op == 0: curr *= i curr = int(curr) which_op += 1 elif which_op == 1: curr /= i curr = int(curr) which_op += 1 elif which_op == 2: operations.append((curr, 2)) curr = i which_op += 1 elif which_op == 3: operations.append((curr, 3)) which_op = 0 curr = i which_op = 0 if i == 1: operations.append((curr, 0)) i -= 1 #print(operations) sol = 0 index = 0 if len(operations) == 1: return operations[0][0] while index < len(operations) - 1: if index == 0: sol = operations[index][0] if operations[index][1] == 2: sol+= operations[index + 1][0] else: sol -= operations[index + 1][0] index+=1 return sol
Normally, the factorial of a positive integer n is the product of all positive integers less than or equal to n.  For example, factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1. We instead make a clumsy factorial: using the integers in decreasing order, we swap out the multiply operations for a fixed rotation of operations: multiply (*), divide (/), add (+) and subtract (-) in this order. For example, clumsy(10) = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1.  However, these operations are still applied using the usual order of operations of arithmetic: we do all multiplication and division steps before any addition or subtraction steps, and multiplication and division steps are processed left to right. Additionally, the division that we use is floor division such that 10 * 9 / 8 equals 11.  This guarantees the result is an integer. Implement the clumsy function as defined above: given an integer N, it returns the clumsy factorial of N.   Example 1: Input: 4 Output: 7 Explanation: 7 = 4 * 3 / 2 + 1 Example 2: Input: 10 Output: 12 Explanation: 12 = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1   Note: 1 <= N <= 10000 -2^31 <= answer <= 2^31 - 1  (The answer is guaranteed to fit within a 32-bit integer.)
class Solution: def clumsy(self, N: int) -> int: res=[] danhanvachiachua=False i=N while i>0: if danhanvachiachua==False: if i>=3: res.append((i*(i-1))//(i-2)) elif i==2: res.append(2) elif i==1: res.append(1) i=i-3 danhanvachiachua=True else: res.append(i) i=i-1 danhanvachiachua=False print(res) dau=0 clumsy=res[0] for i in range(1,len(res)): if dau%2==0: clumsy+=res[i] else: clumsy-=res[i] dau+=1 return clumsy
Normally, the factorial of a positive integer n is the product of all positive integers less than or equal to n.  For example, factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1. We instead make a clumsy factorial: using the integers in decreasing order, we swap out the multiply operations for a fixed rotation of operations: multiply (*), divide (/), add (+) and subtract (-) in this order. For example, clumsy(10) = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1.  However, these operations are still applied using the usual order of operations of arithmetic: we do all multiplication and division steps before any addition or subtraction steps, and multiplication and division steps are processed left to right. Additionally, the division that we use is floor division such that 10 * 9 / 8 equals 11.  This guarantees the result is an integer. Implement the clumsy function as defined above: given an integer N, it returns the clumsy factorial of N.   Example 1: Input: 4 Output: 7 Explanation: 7 = 4 * 3 / 2 + 1 Example 2: Input: 10 Output: 12 Explanation: 12 = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1   Note: 1 <= N <= 10000 -2^31 <= answer <= 2^31 - 1  (The answer is guaranteed to fit within a 32-bit integer.)
class Solution: def clumsy(self, n): def helper(arr): s = arr[0][0] for i in range(1,len(arr)): if i%2 != 0: s += arr[i][0] else: s -= arr[i][0] return s if n < 2: return 1 arr = [[n]] n -= 1 x = 1 while n != 0: if x == 5: x = 1 if x == 1: arr[len(arr) - 1][0] *= n if x == 2: arr[len(arr) - 1][0] = int(arr[len(arr) - 1][0]/n) if x == 3 or x == 4: arr.append([n]) n -= 1 x += 1 return helper(arr)
Normally, the factorial of a positive integer n is the product of all positive integers less than or equal to n.  For example, factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1. We instead make a clumsy factorial: using the integers in decreasing order, we swap out the multiply operations for a fixed rotation of operations: multiply (*), divide (/), add (+) and subtract (-) in this order. For example, clumsy(10) = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1.  However, these operations are still applied using the usual order of operations of arithmetic: we do all multiplication and division steps before any addition or subtraction steps, and multiplication and division steps are processed left to right. Additionally, the division that we use is floor division such that 10 * 9 / 8 equals 11.  This guarantees the result is an integer. Implement the clumsy function as defined above: given an integer N, it returns the clumsy factorial of N.   Example 1: Input: 4 Output: 7 Explanation: 7 = 4 * 3 / 2 + 1 Example 2: Input: 10 Output: 12 Explanation: 12 = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1   Note: 1 <= N <= 10000 -2^31 <= answer <= 2^31 - 1  (The answer is guaranteed to fit within a 32-bit integer.)
class Solution: def clumsy(self, N: int) -> int: ans = self.multiplyDivide(N) N -= 3 while N > 0: ans += max(N, 0) ans -= self.multiplyDivide(N-1) N -= 4 return ans def multiplyDivide(self, n: int) -> int: return max(0, n) * max(1, n-1) // max(1, n-2)
Normally, the factorial of a positive integer n is the product of all positive integers less than or equal to n.  For example, factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1. We instead make a clumsy factorial: using the integers in decreasing order, we swap out the multiply operations for a fixed rotation of operations: multiply (*), divide (/), add (+) and subtract (-) in this order. For example, clumsy(10) = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1.  However, these operations are still applied using the usual order of operations of arithmetic: we do all multiplication and division steps before any addition or subtraction steps, and multiplication and division steps are processed left to right. Additionally, the division that we use is floor division such that 10 * 9 / 8 equals 11.  This guarantees the result is an integer. Implement the clumsy function as defined above: given an integer N, it returns the clumsy factorial of N.   Example 1: Input: 4 Output: 7 Explanation: 7 = 4 * 3 / 2 + 1 Example 2: Input: 10 Output: 12 Explanation: 12 = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1   Note: 1 <= N <= 10000 -2^31 <= answer <= 2^31 - 1  (The answer is guaranteed to fit within a 32-bit integer.)
class Solution: def clumsy(self, N: int) -> int: fac = [0, 1, 2, 6, 7, 7, 8, 6] if N <= 6: return fac[N] for i in range(8, N + 1): temp = i * (i - 1) // (i - 2) + (i - 3) - (i - 4) * (i - 5) // (i - 6) * 2 + fac[i - 4] fac.append(temp) print(fac) return fac[-1]
Normally, the factorial of a positive integer n is the product of all positive integers less than or equal to n.  For example, factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1. We instead make a clumsy factorial: using the integers in decreasing order, we swap out the multiply operations for a fixed rotation of operations: multiply (*), divide (/), add (+) and subtract (-) in this order. For example, clumsy(10) = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1.  However, these operations are still applied using the usual order of operations of arithmetic: we do all multiplication and division steps before any addition or subtraction steps, and multiplication and division steps are processed left to right. Additionally, the division that we use is floor division such that 10 * 9 / 8 equals 11.  This guarantees the result is an integer. Implement the clumsy function as defined above: given an integer N, it returns the clumsy factorial of N.   Example 1: Input: 4 Output: 7 Explanation: 7 = 4 * 3 / 2 + 1 Example 2: Input: 10 Output: 12 Explanation: 12 = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1   Note: 1 <= N <= 10000 -2^31 <= answer <= 2^31 - 1  (The answer is guaranteed to fit within a 32-bit integer.)
class Solution: def clumsy(self, N: int) -> int: import functools l = list(reversed(list(range(1, N+1)))) ll= list([z[1] for z in [x for x in enumerate(l) if x[0] % 4 == 0 or x[0] % 4 == 1 or x[0] % 4 == 2]]) rs = sum(list([z[1] for z in [x for x in enumerate(l) if x[0] % 4 == 3]])) pr, acc = [], 1 for( i, el) in enumerate(ll): if i % 3 == 0:acc = el elif i % 3 == 1:acc = acc * el else: acc = math.floor(acc / el) if i %3 == 2 or i == len(ll) -1: pr.append(acc) # print(l, ll, rs, pr) return rs + pr[0] + sum(list([-x for x in pr[1:]]))
Normally, the factorial of a positive integer n is the product of all positive integers less than or equal to n.  For example, factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1. We instead make a clumsy factorial: using the integers in decreasing order, we swap out the multiply operations for a fixed rotation of operations: multiply (*), divide (/), add (+) and subtract (-) in this order. For example, clumsy(10) = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1.  However, these operations are still applied using the usual order of operations of arithmetic: we do all multiplication and division steps before any addition or subtraction steps, and multiplication and division steps are processed left to right. Additionally, the division that we use is floor division such that 10 * 9 / 8 equals 11.  This guarantees the result is an integer. Implement the clumsy function as defined above: given an integer N, it returns the clumsy factorial of N.   Example 1: Input: 4 Output: 7 Explanation: 7 = 4 * 3 / 2 + 1 Example 2: Input: 10 Output: 12 Explanation: 12 = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1   Note: 1 <= N <= 10000 -2^31 <= answer <= 2^31 - 1  (The answer is guaranteed to fit within a 32-bit integer.)
class Solution: def clumsy(self, N: int) -> int: self.N = N k = N // 4 r = N % 4 def result(n): if r == 1: return 1 elif r == 2: return 2 #2 * 1 elif r == 3: return 6 #3 * 2 // 1 elif r == 0: return 5 #4 * 3 // 2 - 1 cFac = 2 * (N - 1) if N < 4: return result(r) elif N == 4: cFac += 1 else: if r == 0: cFac += -4 * (k-2) else: cFac += -4 * (k-1) cFac -= result(r) return cFac
Normally, the factorial of a positive integer n is the product of all positive integers less than or equal to n.  For example, factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1. We instead make a clumsy factorial: using the integers in decreasing order, we swap out the multiply operations for a fixed rotation of operations: multiply (*), divide (/), add (+) and subtract (-) in this order. For example, clumsy(10) = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1.  However, these operations are still applied using the usual order of operations of arithmetic: we do all multiplication and division steps before any addition or subtraction steps, and multiplication and division steps are processed left to right. Additionally, the division that we use is floor division such that 10 * 9 / 8 equals 11.  This guarantees the result is an integer. Implement the clumsy function as defined above: given an integer N, it returns the clumsy factorial of N.   Example 1: Input: 4 Output: 7 Explanation: 7 = 4 * 3 / 2 + 1 Example 2: Input: 10 Output: 12 Explanation: 12 = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1   Note: 1 <= N <= 10000 -2^31 <= answer <= 2^31 - 1  (The answer is guaranteed to fit within a 32-bit integer.)
class Solution: def clumsy(self, N: int) -> int: x = ['*', '//', '+', '-'] * (10000//4) x = iter(x) ans = str(N) i = N-1 while i > 0: op = next(x) ans += (op+str(i)) i -= 1 return eval(ans)
Normally, the factorial of a positive integer n is the product of all positive integers less than or equal to n.  For example, factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1. We instead make a clumsy factorial: using the integers in decreasing order, we swap out the multiply operations for a fixed rotation of operations: multiply (*), divide (/), add (+) and subtract (-) in this order. For example, clumsy(10) = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1.  However, these operations are still applied using the usual order of operations of arithmetic: we do all multiplication and division steps before any addition or subtraction steps, and multiplication and division steps are processed left to right. Additionally, the division that we use is floor division such that 10 * 9 / 8 equals 11.  This guarantees the result is an integer. Implement the clumsy function as defined above: given an integer N, it returns the clumsy factorial of N.   Example 1: Input: 4 Output: 7 Explanation: 7 = 4 * 3 / 2 + 1 Example 2: Input: 10 Output: 12 Explanation: 12 = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1   Note: 1 <= N <= 10000 -2^31 <= answer <= 2^31 - 1  (The answer is guaranteed to fit within a 32-bit integer.)
class Solution: def clumsy(self, N: int) -> int: op = ['*', '//', '+', '-'] op_idx = -1 idx = N result = str(N) while idx > 1: idx -= 1 op_idx = (op_idx+1)%4 result += op[op_idx] + str(idx) return eval(result)
Normally, the factorial of a positive integer n is the product of all positive integers less than or equal to n.  For example, factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1. We instead make a clumsy factorial: using the integers in decreasing order, we swap out the multiply operations for a fixed rotation of operations: multiply (*), divide (/), add (+) and subtract (-) in this order. For example, clumsy(10) = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1.  However, these operations are still applied using the usual order of operations of arithmetic: we do all multiplication and division steps before any addition or subtraction steps, and multiplication and division steps are processed left to right. Additionally, the division that we use is floor division such that 10 * 9 / 8 equals 11.  This guarantees the result is an integer. Implement the clumsy function as defined above: given an integer N, it returns the clumsy factorial of N.   Example 1: Input: 4 Output: 7 Explanation: 7 = 4 * 3 / 2 + 1 Example 2: Input: 10 Output: 12 Explanation: 12 = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1   Note: 1 <= N <= 10000 -2^31 <= answer <= 2^31 - 1  (The answer is guaranteed to fit within a 32-bit integer.)
class Solution: def clumsy(self, N: int) -> int: res = [] counter = 0 for num in reversed(list(range(1, N + 1))): res.append(str(num)) if counter == 0: res.append('*') elif counter == 1: res.append('//') elif counter == 2: res.append('+') elif counter == 3: res.append('-') counter += 1 if counter == 4: counter = 0 res.pop() return eval(''.join(res))
Normally, the factorial of a positive integer n is the product of all positive integers less than or equal to n.  For example, factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1. We instead make a clumsy factorial: using the integers in decreasing order, we swap out the multiply operations for a fixed rotation of operations: multiply (*), divide (/), add (+) and subtract (-) in this order. For example, clumsy(10) = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1.  However, these operations are still applied using the usual order of operations of arithmetic: we do all multiplication and division steps before any addition or subtraction steps, and multiplication and division steps are processed left to right. Additionally, the division that we use is floor division such that 10 * 9 / 8 equals 11.  This guarantees the result is an integer. Implement the clumsy function as defined above: given an integer N, it returns the clumsy factorial of N.   Example 1: Input: 4 Output: 7 Explanation: 7 = 4 * 3 / 2 + 1 Example 2: Input: 10 Output: 12 Explanation: 12 = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1   Note: 1 <= N <= 10000 -2^31 <= answer <= 2^31 - 1  (The answer is guaranteed to fit within a 32-bit integer.)
class Solution: def clumsy(self, N: int) -> int: A = [] for i in range(N): A.append(str(N - i)) if i % 4 == 0: A.append('*') elif i % 4 == 1: A.append('//') elif i % 4 == 2: A.append('+') else: A.append('-') A.pop() return eval(''.join(A))
Normally, the factorial of a positive integer n is the product of all positive integers less than or equal to n.  For example, factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1. We instead make a clumsy factorial: using the integers in decreasing order, we swap out the multiply operations for a fixed rotation of operations: multiply (*), divide (/), add (+) and subtract (-) in this order. For example, clumsy(10) = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1.  However, these operations are still applied using the usual order of operations of arithmetic: we do all multiplication and division steps before any addition or subtraction steps, and multiplication and division steps are processed left to right. Additionally, the division that we use is floor division such that 10 * 9 / 8 equals 11.  This guarantees the result is an integer. Implement the clumsy function as defined above: given an integer N, it returns the clumsy factorial of N.   Example 1: Input: 4 Output: 7 Explanation: 7 = 4 * 3 / 2 + 1 Example 2: Input: 10 Output: 12 Explanation: 12 = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1   Note: 1 <= N <= 10000 -2^31 <= answer <= 2^31 - 1  (The answer is guaranteed to fit within a 32-bit integer.)
class Solution: def clumsy(self, N: int) -> int: res = None while N > 0: val = N N -= 1 print(N, val, res) if N > 0: val = val * N N -= 1 print(N, val, res) if N > 0: val = val // N N -= 1 print(N, val, res) if res is None: if N > 0: val = val + N N -= 1 print(N, val, res) res = val else: if N > 0: val = val - N N -= 1 print(N, val, res) res -= val print(N, val, res) return res
Normally, the factorial of a positive integer n is the product of all positive integers less than or equal to n.  For example, factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1. We instead make a clumsy factorial: using the integers in decreasing order, we swap out the multiply operations for a fixed rotation of operations: multiply (*), divide (/), add (+) and subtract (-) in this order. For example, clumsy(10) = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1.  However, these operations are still applied using the usual order of operations of arithmetic: we do all multiplication and division steps before any addition or subtraction steps, and multiplication and division steps are processed left to right. Additionally, the division that we use is floor division such that 10 * 9 / 8 equals 11.  This guarantees the result is an integer. Implement the clumsy function as defined above: given an integer N, it returns the clumsy factorial of N.   Example 1: Input: 4 Output: 7 Explanation: 7 = 4 * 3 / 2 + 1 Example 2: Input: 10 Output: 12 Explanation: 12 = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1   Note: 1 <= N <= 10000 -2^31 <= answer <= 2^31 - 1  (The answer is guaranteed to fit within a 32-bit integer.)
''' 1006. Clumsy Factorial. Medium Normally, the factorial of a positive integer n is the product of all positive integers less than or equal to n. For example, factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1. We instead make a clumsy factorial: using the integers in decreasing order, we swap out the multiply operations for a fixed rotation of operations: multiply (*), divide (/), add (+) and subtract (-) in this order. For example, clumsy(10) = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1. However, these operations are still applied using the usual order of operations of arithmetic: we do all multiplication and division steps before any addition or subtraction steps, and multiplication and division steps are processed left to right. Additionally, the division that we use is floor division such that 10 * 9 / 8 equals 11. This guarantees the result is an integer. Implement the clumsy function as defined above: given an integer N, it returns the clumsy factorial of N. Example 1: Input: 4 Output: 7 Explanation: 7 = 4 * 3 / 2 + 1 Example 2: Input: 10 Output: 12 Explanation: 12 = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1 Note: 1 <= N <= 10000 -2^31 <= answer <= 2^31 - 1 The answer is guaranteed to fit within a 32-bit integer. Accepted 11, 010 / 20, 603 submissions. ''' class SolutionWhile: ''' Runtime: 36 ms, faster than 77.40% of Python3 online submissions for Clumsy Factorial. Memory Usage: 14.2 MB, less than 41.24% of Python3 online submissions for Clumsy Factorial. Runtime: 40 ms, faster than 68.26% in Python3. Memory Usage: 12.8 MB, less than 100.00% in Python3. ''' def clumsy(self, N: int) -> int: ''' 1006. Clumsy Factorial ''' R = N # set_trace() N -= 1 if N: R *= N N -= 1 if N: R //= N N -= 1 if N: R += N N -= 1 while N: if N > 2: R -= N*(N-1)//(N-2) N -= 3 elif N > 1: R -= N*(N-1) N -= 2 elif N > 0: R -= N N -= 1 if not N: break R += N N -= 1 return R ############################################################################### class Solution: pass Solution = SolutionWhile # Solution = SolutionMathTricks
Normally, the factorial of a positive integer n is the product of all positive integers less than or equal to n.  For example, factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1. We instead make a clumsy factorial: using the integers in decreasing order, we swap out the multiply operations for a fixed rotation of operations: multiply (*), divide (/), add (+) and subtract (-) in this order. For example, clumsy(10) = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1.  However, these operations are still applied using the usual order of operations of arithmetic: we do all multiplication and division steps before any addition or subtraction steps, and multiplication and division steps are processed left to right. Additionally, the division that we use is floor division such that 10 * 9 / 8 equals 11.  This guarantees the result is an integer. Implement the clumsy function as defined above: given an integer N, it returns the clumsy factorial of N.   Example 1: Input: 4 Output: 7 Explanation: 7 = 4 * 3 / 2 + 1 Example 2: Input: 10 Output: 12 Explanation: 12 = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1   Note: 1 <= N <= 10000 -2^31 <= answer <= 2^31 - 1  (The answer is guaranteed to fit within a 32-bit integer.)
''' 1006. Clumsy Factorial. Medium Normally, the factorial of a positive integer n is the product of all positive integers less than or equal to n. For example, factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1. We instead make a clumsy factorial: using the integers in decreasing order, we swap out the multiply operations for a fixed rotation of operations: multiply (*), divide (/), add (+) and subtract (-) in this order. For example, clumsy(10) = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1. However, these operations are still applied using the usual order of operations of arithmetic: we do all multiplication and division steps before any addition or subtraction steps, and multiplication and division steps are processed left to right. Additionally, the division that we use is floor division such that 10 * 9 / 8 equals 11. This guarantees the result is an integer. Implement the clumsy function as defined above: given an integer N, it returns the clumsy factorial of N. Example 1: Input: 4 Output: 7 Explanation: 7 = 4 * 3 / 2 + 1 Example 2: Input: 10 Output: 12 Explanation: 12 = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1 Note: 1 <= N <= 10000 -2^31 <= answer <= 2^31 - 1 The answer is guaranteed to fit within a 32-bit integer. Accepted 11, 010 / 20, 603 submissions. ''' class SolutionWhile: def clumsy(self, N: int) -> int: ''' 1006. Clumsy Factorial Runtime: 40 ms, faster than 68.26% in Python3. Memory Usage: 12.8 MB, less than 100.00% in Python3. ''' R = N # set_trace() N -= 1 if N: R *= N N -= 1 if N: R //= N N -= 1 if N: R += N N -= 1 while N: if N > 2: R -= N*(N-1)//(N-2) N -= 3 elif N > 1: R -= N*(N-1) N -= 2 elif N > 0: R -= N N -= 1 if not N: break R += N N -= 1 return R ############################################################################### class Solution: pass Solution = SolutionWhile # Solution = SolutionMathTricks
Normally, the factorial of a positive integer n is the product of all positive integers less than or equal to n.  For example, factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1. We instead make a clumsy factorial: using the integers in decreasing order, we swap out the multiply operations for a fixed rotation of operations: multiply (*), divide (/), add (+) and subtract (-) in this order. For example, clumsy(10) = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1.  However, these operations are still applied using the usual order of operations of arithmetic: we do all multiplication and division steps before any addition or subtraction steps, and multiplication and division steps are processed left to right. Additionally, the division that we use is floor division such that 10 * 9 / 8 equals 11.  This guarantees the result is an integer. Implement the clumsy function as defined above: given an integer N, it returns the clumsy factorial of N.   Example 1: Input: 4 Output: 7 Explanation: 7 = 4 * 3 / 2 + 1 Example 2: Input: 10 Output: 12 Explanation: 12 = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1   Note: 1 <= N <= 10000 -2^31 <= answer <= 2^31 - 1  (The answer is guaranteed to fit within a 32-bit integer.)
class Solution: def clumsy(self, N: int) -> int: res = 0 for i in range(N, 0, -4): if i >= 4: if i == N: res += i * (i-1) // (i-2) + (i-3) else: res -= i * (i-1) // (i-2) - (i-3) #print(res) neg = [1, -1][int(N >= 4)] if N % 4 == 3: res += 6 * neg elif N % 4 == 2: res += 2 * neg elif N % 4 == 1: res += 1 * neg return int(res)
Normally, the factorial of a positive integer n is the product of all positive integers less than or equal to n.  For example, factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1. We instead make a clumsy factorial: using the integers in decreasing order, we swap out the multiply operations for a fixed rotation of operations: multiply (*), divide (/), add (+) and subtract (-) in this order. For example, clumsy(10) = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1.  However, these operations are still applied using the usual order of operations of arithmetic: we do all multiplication and division steps before any addition or subtraction steps, and multiplication and division steps are processed left to right. Additionally, the division that we use is floor division such that 10 * 9 / 8 equals 11.  This guarantees the result is an integer. Implement the clumsy function as defined above: given an integer N, it returns the clumsy factorial of N.   Example 1: Input: 4 Output: 7 Explanation: 7 = 4 * 3 / 2 + 1 Example 2: Input: 10 Output: 12 Explanation: 12 = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1   Note: 1 <= N <= 10000 -2^31 <= answer <= 2^31 - 1  (The answer is guaranteed to fit within a 32-bit integer.)
class Solution: def clumsy(self, N: int) -> int: return clumsy(N, 1) def clumsy(N: int, sign: int) -> int: if N == 1: return sign * 1 if N == 2: return sign * 2 # sign * 2 * 1 if N == 3: return sign * 6 # sign * 3 * 2 // 1 if N == 4: return sign * 6 + 1 # sign * 4 * 3 // 2 + 1 return sign * (N * (N-1) // (N-2)) + (N-3) + clumsy(N-4, -1) # 10 * 9 // 8 + 7 + (-6 * 5 // 4 + 3) + (-2 * 1) # 11 + 7 - 7 + 3 - 2 == 12
Normally, the factorial of a positive integer n is the product of all positive integers less than or equal to n.  For example, factorial(10) = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1. We instead make a clumsy factorial: using the integers in decreasing order, we swap out the multiply operations for a fixed rotation of operations: multiply (*), divide (/), add (+) and subtract (-) in this order. For example, clumsy(10) = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1.  However, these operations are still applied using the usual order of operations of arithmetic: we do all multiplication and division steps before any addition or subtraction steps, and multiplication and division steps are processed left to right. Additionally, the division that we use is floor division such that 10 * 9 / 8 equals 11.  This guarantees the result is an integer. Implement the clumsy function as defined above: given an integer N, it returns the clumsy factorial of N.   Example 1: Input: 4 Output: 7 Explanation: 7 = 4 * 3 / 2 + 1 Example 2: Input: 10 Output: 12 Explanation: 12 = 10 * 9 / 8 + 7 - 6 * 5 / 4 + 3 - 2 * 1   Note: 1 <= N <= 10000 -2^31 <= answer <= 2^31 - 1  (The answer is guaranteed to fit within a 32-bit integer.)
class Solution: def clumsy(self, N: int) -> int: # if N <= 2: # return N # if N <= 4: # return N + 3 # if (N - 4) % 4 == 0: # return N + 1 # elif (N - 4) % 4 <= 2: # return N + 2 # else: # return N - 1 magic = [1, 2, 2, -1, 0, 0, 3, 3] return N + (magic[N % 4] if N > 4 else magic[N + 3])
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: for i,r in enumerate(ranges): l = max(0,i-r) ranges[l] = max(i+r, ranges[l]) res = lo = hi = 0 while hi < n: lo, hi = hi, max(ranges[lo:hi+1]) if hi == lo: return -1 res += 1 return res
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: if not ranges: return 0 N = len(ranges) # For all location of taps, store the largest right reach point max_right_end = list(range(N)) for i, a in enumerate(ranges): max_right_end[max(i - a, 0)] = min(i + a, N-1) print(max_right_end) res, l, r = 0, 0, max_right_end[0] while True: res += 1 # if r can reach to the end of the whole garden, return res if r>=N-1:return res new_r = max(max_right_end[l:r+1]) # if next r is same as current r, it means we can not move forward, return -1 if new_r == r:return -1 l, r = r, new_r
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: # from collections import defaultdict # if n==0: # return -1 # plots = defaultdict(list) # for i in range(n): # l, r = i - ranges[i], i + ranges[i] # for j in [k for k in range(n) if k>=l and k<=r]: # plots[j].append(i) # print(plots) # if len(plots.keys()) == n: # for i in plots.items(): # print(i) # else: # return -1 def parse_ranges(ranges): intervals = [] for idx, distance in enumerate(ranges): left = max(0, idx-distance) right = min(n, idx+distance) intervals.append([left, right]) return intervals watered = [] intervals = parse_ranges(ranges) intervals.sort(key=lambda time: (time[0], -time[1])) for start, end in intervals: if watered and watered[-1][1] >= end: continue if watered and start - watered[-1][1] > 0: return -1 if len(watered) >= 2 and start <= watered[-2][1]: # print(watered, (start,end)) watered[-1] = [start, end] else: watered.append([start, end]) return len(watered) if watered[-1][-1] >= n else -1
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: dp = [0] + [n+2] * n for i, x in enumerate(ranges): for j in range(max(i-x+1, 0), min(i+x, n) + 1): dp[j] = min(dp[j], dp[max(i-x, 0)] + 1) return dp[n] if dp[n] < n+2 else -1
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: dp = [0] + [n+2] * n for i, v in enumerate(ranges): for j in range(max(0, i-v+1), min(n, i+v)+1): dp[j] = min(dp[j], dp[max(0, i-v)] + 1) return dp[n] if dp[n] < n + 2 else -1
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: def dfs(i): if i == -1: return [0] + [n+2]*n dp = dfs(i-1) x = ranges[i] for j in range(max(i-x, 0), min(i+x, n) + 1): dp[j] = min(dp[j], dp[max(i-x, 0)] + 1) return dp dp = dfs(n) return dp[n] if dp[n] < n+2 else -1
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: dp = [0] + [n+2] * n for i in range(len(ranges)): x = ranges[i] for j in range(max(i-x, 0), min(i+x, n) + 1): dp[j] = min(dp[j], dp[max(i-x, 0)] + 1 ) return dp[n] if dp[n] < n+2 else -1
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: dp = [0] + [n+2] * n for i in range(len(ranges)): x = ranges[i] for j in range(max(i-x+1, 0), min(i+x, n) + 1): dp[j] = min(dp[j], dp[max(i-x, 0)] + 1) return dp[n] if dp[n] < n+2 else -1
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: dp = [n+2] * n + [0] for i in range(len(ranges)-1, -1, -1): x = ranges[i] for j in range(max(i-x, 0), min(i+x, n) + 1): dp[j] = min(dp[j], dp[min(i+x, n)] +1 ) return dp[0] if dp[0] < n+2 else -1
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: dp = [0] + [n+2] * n def dfs(i): x = ranges[i] for j in range(max(i-x, 0), min(i+x, n) + 1): dp[j] = min(dp[j], dp[max(i-x, 0)] + 1) for i in range(len(ranges)): dfs(i) return dp[n] if dp[n] < n+2 else -1
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: # dp[i] min taps to water [0, i] # dp[0] = 0 dp = [0] + [n+2] * n # n+1 is possible value for i, v in enumerate(ranges): left = max(i-v, 0) right = min(i+v, n) for j in range(left+1, right+1): dp[j] = min(dp[j], dp[max(0, i-v)]+1) if dp[n] < n+2: return dp[n] return -1
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: dp=[n+2]*(n+1) dp[0]=0 for i in range(n+1): for j in range(max(0,i-ranges[i])+1, min(n, i+ranges[i])+1): dp[j]=min(dp[j], dp[max(0,i-ranges[i])]+1) print(dp) return dp[-1] if dp[-1]<n+2 else -1
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: dp = [0] + [n+2] * n for i, x in enumerate(ranges): for j in range(max(i-x, 0), min(i+x, n) + 1): dp[j] = min(dp[j], dp[max(i-x, 0)] + 1) return dp[n] if dp[n] < n+2 else -1
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: dp = [0] + [n+2] * n def dfs(i): if i == -1: return [0] + [n+2]*n dp = dfs(i-1) x = ranges[i] for j in range(max(i-x, 0), min(i+x, n) + 1): dp[j] = min(dp[j], dp[max(i-x, 0)] + 1) return dp dp = dfs(n) return dp[n] if dp[n] < n+2 else -1
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n: int, ranges) -> int: dp = [0] + [n + 2] * n for i, x in enumerate(ranges): for j in range(max(i - x + 1, 0), min(i + x, n) + 1): dp[j] = min(dp[j], dp[max(0, i - x)] + 1) return dp[n] if dp[n] < n + 2 else -1
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n, A): dp = [0] + [n + 2] * n for i, x in enumerate(A): for j in range(max(i - x + 1, 0), min(i + x, n) + 1): dp[j] = min(dp[j], dp[max(0, i - x)] + 1) return dp[n] if dp[n] < n + 2 else -1
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n: int, ranges) -> int: dp = [0] + [float('inf')] * n for i, x in enumerate(ranges): for j in range(max(i - x + 1, 0), min(i + x, n) + 1): dp[j] = min(dp[j], dp[max(0, i - x)] + 1) print(dp) return dp[n] if dp[n] != float('inf') else -1
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n, A): dp = [0] + [n + 2] * n for i, x in enumerate(A): for j in range(max(i - x + 1, 0), min(i + x, n) + 1): dp[j] = min(dp[j], dp[max(0, i - x)] + 1) if dp[-1] < n + 2: return dp[-1] return -1
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: min_range, max_range, open_taps = 0, 0 ,0 while max_range < n: for i, r in enumerate(ranges): if i - r <= min_range and i + r >= max_range: max_range = i + r if min_range == max_range: return -1 open_taps += 1 min_range = max_range return open_taps
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n, A): dp = [0] + [n + 2] * n for i, x in enumerate(A): for j in range(max(i - x + 1, 0), min(i + x, n) + 1): dp[j] = min(dp[j], dp[max(0, i - x)] + 1) return dp[n] if dp[n] < n + 2 else -1
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: dp = [0] + [n + 2] * n for i, x in enumerate(ranges): for j in range(max(i - x + 1, 0), min(i + x, n) + 1): dp[j] = min(dp[j], dp[max(0, i - x)] + 1) return dp[n] if dp[n] < n + 2 else -1
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: lowest_missing = 0 taps = 0 while lowest_missing < n: highest = None for (idx, tap) in enumerate(ranges): if idx - tap > lowest_missing or idx + tap < lowest_missing + 1: continue if highest is None or highest < idx + tap: highest = idx + tap if highest is None: return -1 lowest_missing = highest taps += 1 return taps
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: dp=[sys.maxsize]*(n+1) dp[0]=0 for i in range(n+1): for j in range(max(0,i-ranges[i]),(min(n,i+ranges[i]))+1): dp[j]=min(dp[j],1+dp[max(0,i-ranges[i])]) if(dp[n]==sys.maxsize): return -1 return dp[n]
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: dp = [0] + [n+1] * n for i, x in enumerate(ranges): for j in range(max(i- x + 1, 0), min(n,i+x)+ 1): dp[j] = min(dp[j], dp[max(0,i-x)]+ 1) return dp[n] if dp[n]!=n+1 else -1 # dp = [0] + [n + 2] * n # for i, x in enumerate(ranges): # for j in range(max(i - x + 1, 0), min(i + x, n) + 1): # dp[j] = min(dp[j], dp[max(0, i - x)] + 1) # return dp[n] if dp[n] < n + 2 else -1
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: dp = [0] + [n+2]*n for i, r in enumerate(ranges): for j in range(max(0, i-r+1), min(n, i+r)+1): dp[j] = min(dp[j], dp[max(0, i-r)]+1) return dp[n] if dp[n] < n+2 else -1
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: dp = [0] + [n + 2] * n for i, x in enumerate(ranges): for j in range(max(i - x + 1, 0), min(i + x, n) + 1): dp[j] = min(dp[j], dp[max(0, i - x)] + 1) return dp[n] if dp[n] < n + 2 else -1
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: res = 0 DP = [float('inf')] * (n+1) farest = 0 DP[0] = 0 for i, r in enumerate(ranges): for x in range(max(i-r, 0), min(i+r+1, n+1)): DP[x] = min(DP[x], DP[max(i-r, 0)]+1) return DP[-1] if DP[-1] != float('inf') else -1
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: # max_range = [0] * (n + 1) # for idx, r in enumerate(ranges): # left, right = max(0, idx - r), min(n, idx + r) # max_range[left] = max(max_range[left], right - left) # # it's a jump game now # start, end, step = 0, 0, 0 # while end < n: # step += 1 # start, end = end, max(i + max_range[i] for i in range(start, end + 1)) # if start == end: # return -1 # return step dp = [float('inf')] * (n + 1) dp[0] = 0 for idx, r in enumerate(ranges): for j in range(max(idx - r + 1, 0), min(idx + r, n) + 1): dp[j] = min(dp[j], dp[max(0, idx - r)] + 1) return dp[n] if dp[n] < float('inf') else -1 # dp = [0] + [float('inf')] * n # for idx, r in enumerate(ranges): # for j in range(max(idx - r + 1, 0), min(idx + r, n) + 1): # dp[j] = min(dp[j], dp[max(0, idx - r)] + 1) # return dp[n] if dp[n] < float('inf') else -1
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps1(self, n: int, ranges: List[int]) -> int: intervals = [] for i in range(n+1): if ranges[i]: left = max(0, i - ranges[i]) right = min(n, i + ranges[i]) intervals.append((left, right)) intervals.sort() res = 0 max_pos = 0 prev_max_pos = -1 for i, j in intervals: # max_pos >= n: all range covered if max_pos >= n: break # i > max_pos: [max_pos, i] not covered if i > max_pos: return -1 elif prev_max_pos < i <= max_pos: # (i, j) will cover new interval res = res + 1 prev_max_pos = max_pos print((i, j), res) max_pos = max(max_pos, j) return res if max_pos >= n else -1 def minTaps(self, n: int, ranges: List[int]) -> int: dp = [0] + [n + 1] * n for i, x in enumerate(ranges): for j in range(max(i - x + 1, 0), min(i + x, n) + 1): dp[j] = min(dp[j], dp[max(0, i - x)] + 1) # print(dp) return dp[n] if dp[n] < n + 1 else -1
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
from heapq import heappush, heappop class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: taps_by_start = [] # ranked by start for i, r in enumerate(ranges): if r != 0: tap = (max(0, i-r), i+r) # start, end heappush(taps_by_start, tap) # print(taps_by_start) taps_by_end = [] # the largest end at top current_min = 0 num_of_pipe= 0 while current_min < n: while taps_by_start and taps_by_start[0][0] <= current_min: tap = heappop(taps_by_start) tap_reverse = (-tap[1], tap[0]) heappush(taps_by_end, tap_reverse) if taps_by_end: next_pipe = heappop(taps_by_end) # -end, start current_min = - next_pipe[0] num_of_pipe +=1 else: return -1 return num_of_pipe
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
#[Runtime: 1212 ms, faster than 5.27%] DP #O(N * 200) #f(i): the minimum number of taps that we can cover range: 0~i and i is opended #Since 0~n is already sorted sequence of number #f(i) = 1 if ranges[i][BEGIN] <= 0 #f(i) = min( f(j) where max(0, ranges[i][BEGIN]-100) <= j < i and ranges[j][END] >= ranges[i][BEGIN] ) + 1 #NOTE: WA: return min { f(i) if i + ranges[i][END] >= n } from math import inf class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: dp = [inf] * (n+1) for i in range(n+1): if not ranges[i]: continue #skip impossible tapes to prevent from empty arg of `min` s = i - ranges[i] if s <= 0: dp[i] = 1 else: dp[i] = 1 + min([dp[j] for j in range(max(0, s - 100), i) if ranges[j] and j + ranges[j] >= s] + [inf]) res = min([dp[i] for i in range(n+1) if ranges[i] and i + ranges[i] >= n] + [inf]) return res if res != inf else -1
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: min_range, max_range, open_taps, idx = 0, 0 ,0, 0 while max_range < n: for i in range(len(ranges)): if i - ranges[i] <= min_range and i + ranges[i] >= max_range: max_range = i + ranges[i] if min_range == max_range: return -1 open_taps += 1 min_range = max_range return open_taps
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n, A): dp = [0] + [n + 2] * n for i, x in enumerate(A): left = i- x right = i+x for j in range(max(left+1, 0), min(right, n) + 1): dp[j] = min(dp[j], dp[max(0, left)] + 1) return dp[n] if dp[n] < n + 2 else -1
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: dp = [n+2 for i in range(n+1)] dp[0] = 0 for i, r in enumerate(ranges): for j in range(max(0, i-r), min(len(ranges), i+r+1)): dp[j] = min(dp[j], dp[max(0, i-r)] + 1) print(dp) return dp[-1] if dp[-1] < n+2 else -1
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: dp = {i: 2*n for i in range(n+1)} dp[0] = 0 for i in range(0, n+1): for j in range(max(0, i-ranges[i]), min(n+1, i+ranges[i]+1)): dp[j] = min(dp[j], dp[max(0, i-ranges[i])] + 1) # print(dp) return dp[n] if dp[n] < 2*n else -1
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: dp = [0] + [n+2] * n for i, value in enumerate(ranges): for j in range(max(i-value,0), min(i+value, n) + 1): dp[j] = min(dp[j], dp[max(i-value, 0)] + 1) return dp[n] if dp[n] < n+2 else -1
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: farthestRightAt = [-1] * (n+1) for i, r in enumerate(ranges): if r == 0: continue left = i-r right = i+r for j in range(max(0, left), min(right+1, n+1)): farthestRightAt[j] = min(n, max(farthestRightAt[j], right)) if -1 in farthestRightAt: return -1 i = 0 count = 0 while i < n: i = farthestRightAt[i] count += 1 return count
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: sorted_ranges = ([(max(0,i-v), min(n, i+ v)) for i,v in enumerate(ranges)]) dicts = {} for i in sorted_ranges: dicts[i[0]] = max(i[1], dicts.get(i[0], -10e9)) reduced_ranges = sorted([(k,v) for k, v in dicts.items()]) prev_max = max_e = reduced_ranges[0][1] min_start = reduced_ranges[0][0] taps = 1 i = 0 s,e = reduced_ranges[i] print(reduced_ranges) while max_e < n: # print(\"cluster\", s, e, prev_max, \"reached to \" ,max_e, \"with\" , taps) taps += 1 while True: s,e = reduced_ranges[i + 1] if s > prev_max: # print(\"break at i\",s,e, i) break max_e = max(e, max_e) # print(reduced_ranges[i], \"new_max\", max_e) i += 1 if max_e >= n: # print(\"last one\") return taps if max_e == prev_max: return -1 prev_max = max_e max_e = max_e return taps
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: taps=[] for i in range(len(ranges)): taps.append((i-ranges[i],i+ranges[i])) taps.sort() @lru_cache(None) def helper(i): # minimum number of taps to open if we open i to reach end if i>=n+1:return float('inf') if taps[i][1]>=n:return 1 ans=float('inf') # lets do binary search here for j in range(i+1,n+1): if taps[j][0]>taps[i][1]: break if taps[j][0]<=taps[i][1] and taps[j][1]>taps[i][1]: # benefit of picking this guy ans=min(ans,1+helper(j)) return ans ans=float('inf') for i in range(n,-1,-1): res=helper(i) if taps[i][0]<=0 and res<ans: ans=res return -1 if ans==float('inf') else ans
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n, A): dp = [0] + [n + 2] * n for i, x in enumerate(A): for j in range(max(i - x + 1, 0), min(i + x, n) + 1): dp[j] = min(dp[j], dp[max(0, i - x)] + 1) return dp[n] if dp[n] < n + 2 else -1 # class Solution: # def minTaps(self, n: int, ranges: List[int]) -> int: # covers = sorted([(i - ranges[i], i + ranges[i], i) for i in range(n + 1) if ranges[i]], key=lambda x: (x[0], -x[1])) # print(covers) # cnt = 0 # most_right = float('-inf') # lst = [] # for curr_left, curr_right, curr_idx in covers: # if curr_right > most_right: # most_right = curr_right # cnt += 1 # lst.append(curr_idx) # print(cnt) # return cnt if most_right >= n else -1
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: stack = [] for i in range(n + 1): l, r = i - ranges[i], i + ranges[i] if not stack or (l <= 0 and r >= stack[-1]): stack = [r] elif l <= stack[-1] < r: while len(stack) > 1: prev = stack.pop() if l > stack[-1]: stack.append(prev) break stack.append(r) if stack[-1] >= n: return len(stack) return -1 class Solution: #DP def minTaps(self, n, A): dp = [0] + [n + 2] * n for i, x in enumerate(A): for j in range(max(i - x + 1, 0), min(i + x, n) + 1): dp[j] = min(dp[j], dp[max(0, i - x)] + 1) return dp[n] if dp[n] < n + 2 else -1 # class Solution: # def minTaps(self, n: int, ranges: List[int]) -> int: # covers = sorted([(i - ranges[i], i + ranges[i], i) for i in range(n + 1) if ranges[i]], key=lambda x: (x[0], -x[1])) # print(covers) # cnt = 0 # most_right = float('-inf') # lst = [] # for curr_left, curr_right, curr_idx in covers: # if curr_right > most_right: # most_right = curr_right # cnt += 1 # lst.append(curr_idx) # print(cnt) # return cnt if most_right >= n else -1
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: # 傻瓜一点想法 DP # 每个点 左边loop 当前最小值和左边最小值+1 # 有点每个点 同理 dp = [0] + [n + 2] * n for i , cur in enumerate(ranges): for j in range(max(0,i-cur+1),min(n,i+cur)+1): dp[j] = min(dp[j], dp[max(0,i-cur)] + 1) return dp[n] if dp[n] < n + 2 else -1 # 0-5 最多有6个 n+1
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: minimum=0 maximum=0 total=0 while maximum<n: for i in range(len(ranges)): left=i-ranges[i] right=i+ranges[i] if left<=minimum and right>maximum: maximum=right if minimum==maximum: return -1 minimum=maximum total+=1 return total
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: ranges = [[i-val, i+val] for i, val in enumerate(ranges)] ranges.sort() print(ranges) max_reach = 0 i = 0 taps=0 while max_reach <n: curr_reach = 0 if max_reach == 6: print(3) while i < n+1 and ranges[i][0] <= max_reach: curr_reach = max(curr_reach, ranges[i][1]) i += 1 print(curr_reach , max_reach) if curr_reach <= max_reach: return -1 max_reach = curr_reach taps += 1 return taps
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: windows = [] for i, range in enumerate(ranges): windows.append((max(0, i-range), i+range)) windows.sort() i = 0 max_water = 0 num_taps = 0 while i < len(windows): if max_water >= n: return num_taps num_taps += 1 prev_water = max_water j = i while j < len(windows) and windows[j][0] <= prev_water: if windows[j][1] > max_water: max_water = windows[j][1] j += 1 if max_water == prev_water: break i = j return -1 if max_water < n else num_taps
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
# 1326. Minimum Number of Taps to Open to Water a Garden def redundant (a, b): (al, ar), (bl, br) = a, b return al <= bl <= br <= ar or bl <= al <= ar <= br def union (a, b): (al, ar), (bl, br) = a, b return (min (al, bl), max (ar, br)) def remove_overlap (ranges): ans = [] for rng in ranges: ans.append (rng) while len (ans) >= 2 and redundant (ans[-2], ans[-1]): b = ans.pop (); a = ans.pop () ans.append (union (a, b)) return ans def union_covers (a, b, c): (al, ar), (bl, br), (cl, cr) = a, b, c return al <= cl <= ar <= cr and al <= bl <= br <= cr def min_taps (n, radii): assert n >= 1 assert len (radii) == n + 1 ranges = [] for (center, radius) in enumerate (radii): if radius > 0: left_end = max (0, center - radius) right_end = min (n, center + radius) ranges.append ((left_end, right_end)) ranges = remove_overlap (ranges) final_ranges = [] for rng in ranges: if len (final_ranges) >= 2 and union_covers (final_ranges[-2], final_ranges[-1], rng): final_ranges.pop () final_ranges.append (rng) if final_ranges and final_ranges[0][0] == 0 and final_ranges[-1][-1] == n and all (final_ranges[i][1] >= final_ranges[i+1][0] for i in range (len (final_ranges) - 1)): return len (final_ranges) else: return -1 class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: return min_taps(n, ranges)
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: covers = [] for i,r in enumerate(ranges): if r==0: continue covers.append([max(0,i-r), min(n, i+r)]) if len(covers)==0: return -1 covers.sort(key=lambda x:x[0]) result = 0 reach = 0 while len(covers)!=0: temp = None while len(covers)!=0: if covers[0][0] - reach>0: break x = covers.pop(0) if temp==None or temp[1]<x[1]: temp = x if temp is None: return -1 result+=1 reach = temp[1] if reach==n: break return result
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
#[] Greedy #O(NlogN) BEGIN, END = 0, 1 class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: stitch = max_reach = cnt = i = 0 clips = sorted((i-diff, i+diff) for i, diff in enumerate(ranges) if diff) #sort by start N = len(clips) while max_reach < n: #NOTE: WA: max_reach < `n` instead of `N` #try to find furthest end-point while i < N and clips[i][BEGIN] <= stitch: max_reach = max(max_reach, clips[i][END]) i += 1 if stitch == max_reach: return -1 #unable to reach n cnt += 1 stitch = max_reach #put clips[i] into solution and extend the next end-point return cnt
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: # def minTaps(self, n: int, ranges: List[int]) -> int: def minTaps(self, n: int, ranges: List[int]) -> int: opens, closes, closed = [[] for _ in range(n)], [[] for _ in range(n)], set() for i in range(len(ranges)): idx = 0 if i - ranges[i] > 0: idx = i - ranges[i] if idx < len(opens): opens[idx].append(i) if i + ranges[i] < n: closes[i + ranges[i]].append(i) heap, cur_open_tap, res = [], None, 0 for i in range(n): for op in opens[i]: heappush(heap, [-(op + ranges[op]), op]) for cl in closes[i]: closed.add(cl) if cl == cur_open_tap: cur_open_tap = None while cur_open_tap is None: if not heap: return -1 if heap[0][1] in closed: heappop(heap) else: cur_open_tap = heap[0][1] res += 1 return res
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: max_right = [0] * (n + 1) for i,x in enumerate(ranges): max_right[max(0, i - x)] = max(max_right[max(0, i - x)], i + x) i = 0 best_right = 0 ans = 0 j = 0 while i < n: while j <= i: best_right = max(max_right[j], best_right) j += 1 if best_right <= i: return -1 ans += 1 i = best_right return ans
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: r, R = 0, sorted([x-r, x+r] for x, r in enumerate(ranges)) + [[9**9]] did = can = count = 0 for x in range(n): while R[r][0] <= x: can = max(can, R[r][1]) r += 1 if did == x: if can <= x: return -1 did = can count += 1 if did == n: break return count
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: taps = [] for i, r in enumerate(ranges): if r > 0: taps.append([max(0, i - r), -min(n, i + r)]) if not taps: return -1 taps.sort() if taps[0][0] != 0: return -1 stack = [[taps[0][0], abs(taps[0][1])]] #print(taps) for t in taps[1:]: if stack[-1][1] == n: break l, r = t[0], abs(t[1]) if r <= stack[-1][1]: continue if l > stack[-1][1]: return - 1 if len(stack) == 1: stack.append([l, r]) else: if l <= stack[-2][1]: stack.pop() stack.append([l, r]) #print(stack) return len(stack)
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: needed = [100000]*(n+1) for i in range(len(ranges)): #print(needed) l_i = i - ranges[i] if l_i <= 0: need = 1 else: need = needed[l_i] + 1 right_most = min(i + ranges[i],len(ranges)-1) while need < needed[right_most] and right_most > i-1: needed[right_most] = need right_most -= 1 if needed[-1]>10000: return -1 return needed[-1]
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: steps = [0]*(n+1) for i, r in enumerate(ranges): if i - r < 0: steps[0] = max(i + r, steps[0]) else: steps[i - r] = max(i + r, steps[i - r]) #print(steps) num = 1 cur = 0 cur_max = steps[0] while cur_max < n: next_max = cur_max for loc in range(cur + 1, cur_max + 1): next_max = max(next_max, steps[loc]) #print(cur, cur_max, next_max) if next_max == cur_max: return -1 num += 1 cur = cur_max cur_max = next_max return num
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: for i, val in enumerate(ranges): if i - val < 0: ranges[0] = max(i + val, ranges[0]) else: ranges[i - val] = max(val * 2, ranges[i - val]) i = 0 prev = -1 counter = 0 while i != prev: distance = ranges[i] if i + distance >= n: return counter + 1 maxIndex = i for j in range(i+1, i + distance + 1): if j + ranges[j] >= maxIndex + ranges[maxIndex]: maxIndex = j j += 1 prev = i i = maxIndex counter += 1 return -1
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: ls = [] for i in range(n+1): ls.append((max(i-ranges[i],0),min(i+ranges[i],n+1))) ls.sort(key = lambda x:(x[0],-x[1])) ans,a,b = 0,-1,0 while b < n: c,d = ls.pop(0) if c > b: return -1 elif d <= b: continue elif c > a: ans += 1 a,b = b,d elif c <= a: b = d return ans
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: # max_right from each point max_right = [0] * (n+1) for i, x in enumerate(ranges): l = max(0, i-x) r = min(n, i+x) max_right[l] = r # jump game ans = 0 curr_reach = max_reach = 0 print(max_right) for i in range(n+1): max_reach = max(max_reach, max_right[i]) if i == curr_reach: print((i, curr_reach, max_reach)) curr_reach = max_reach ans += 1 if curr_reach == n: return ans return ans if curr_reach >= n else -1
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: for i, r in enumerate(ranges): left = max(0, i-r) right = i+r ranges[left] = max(ranges[left], right) r, res = 0, 0 while r < n: tmp = r r = max(ranges[0:r+1]) res += 1 if tmp == r: return -1 return res
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: if n == 0: return 0 steps = [0] * (n + 1) for i, r in enumerate(ranges): left = max(0, i - r) right = min(n, i + r) for j in range(left, right+1): steps[j] = right #print(steps) res = 1 prev = 0 cur = steps[0] while cur > prev: if cur == n: return res tmp = cur for i in range(prev+1, cur+1): cur = max(cur, steps[i]) prev = tmp res += 1 return -1
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: for i, r in enumerate(ranges): left = max(0, i-r) right = i+r ranges[left] = max(ranges[left], right) l, r, res = 0, 0, 0 while r < n: jump = max(ranges[l:r+1]) if r == jump: return -1 res += 1 l, r = r, jump return res
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: l=[] for i in range(n+1): l.append((i-ranges[i], i+ranges[i])) l.sort() maxlen=0 curpos=0 res=0 while maxlen<n: new=maxlen while curpos<=n and l[curpos][0]<=maxlen: if l[curpos][1]>maxlen: new=max(new, l[curpos][1]) curpos+=1 if maxlen==new: return -1 res+=1 maxlen=new return res
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: max_range = [0 for i in range(n+1)] for i in range(n+1): left, right = max(0,i-ranges[i]), min(i+ranges[i], n) max_range[left] = max(max_range[left], right - left) jumps = 0 curlen, endlen = 0, 0 print(max_range) for i in range(n+1): if max_range[i] == 0 and endlen < i: return -1 curlen = max(curlen, i+max_range[i]) if i == endlen: endlen = curlen jumps += 1 if endlen == n: return jumps return jumps
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n: int, ranges): taps = [[max(0, i - ranges[i]), min(n, i + ranges[i])] for i in range(n + 1)] taps.sort() ans = i = last = 0 while i < len(taps): if last == n: return ans if taps[i][0] > last: return -1 if last < n else ans temp = -1 while i < len(taps) and taps[i][0] <= last: temp = max(temp, taps[i][1]) i += 1 if temp > last: ans += 1 last = temp else: return -1 return ans if last == n else -1
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n: int, minjump: List[int]) -> int: for i in range(n+1): left= max(0, i-minjump[i]) right= min(n, i+minjump[i]) minjump[left]=max(minjump[left],right-left) # print(minjump) jump=1 steps=minjump[0] maxreach=minjump[0] lastindex,maxjump=0,minjump[0] for i in range(1,n+1): steps-=1 if steps<0: return -1 if i+minjump[i]>maxreach: maxreach=minjump[i]+i maxjump=minjump[i] lastindex=i if steps==0 and i!=n: steps= lastindex+maxjump-i jump+=1 return jump
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: for i, r in enumerate(ranges): if r > 0: left = max(0, i-r) right = i+r ranges[left] = max(ranges[left], right) r, res = 0, 0 while r < n: tmp = r r = max(ranges[0:r+1]) res += 1 if tmp == r: return -1 return res
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: new_ranges = [] for i in range(len(ranges)): new_ranges.append([max(0, i-ranges[i]), min(n, i+ranges[i])]) new_ranges = sorted(new_ranges, key=lambda x: x[0]) tap_counter = 0 ranges_counter = 0 i = 0 while i < n: # Find all ranges with a start value lower than the current_counter max_right = -1 while ranges_counter < len(new_ranges) and new_ranges[ranges_counter][0] <= i: max_right = max(max_right, new_ranges[ranges_counter][1]) ranges_counter += 1 if max_right == -1: return -1 tap_counter += 1 i = max_right return tap_counter
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: if len(ranges) == 0: return 0 segments = [(max(0, index - num), min(n, index + num)) for index, num in enumerate(ranges)] segments = sorted(segments) left, right = -1, 0 cnt = 0 for a, b in segments: if a > right: break # very important if right == n: break # there are segments with [n, n], we don't want to count it again. if right >= a > left: cnt += 1 left = right right = max(right, b) if right == n: return cnt return -1
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: hp = [] for i, r in enumerate(ranges): if r != 0: heapq.heappush(hp, (max(i-r, 0), -(i+r))) #1-5,-2-6, 3-7, 4-8 ans = [] while hp: start, end = heapq.heappop(hp) end = -end if not ans: ans.append((start, end)) else: #Not overlapped if start > ans[-1][1]: return -1 #Already covered if end <= ans[-1][1]: continue if len(ans) >= 2 and start <= ans[-2][1]: ans[-1] = (start, end) elif start <= ans[-1][1]: ans.append((start, end)) #print(\"here\") #print(ans) if ans and ans[-1][1] >= n: break print(ans) return len(ans) if ans and ans[-1][1] >= n else -1
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
import bisect class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: uc = 0 taps = [] taps_r = [] for idx, r in enumerate(ranges): if r > 0: tap = (idx-r if idx-r>0 else 0, idx+r) tapr = (idx+r, idx-r if idx-r>0 else 0) taps_r.append(tapr) taps.append(tap) if not taps: return -1 taps.sort() curr_idx = bisect.bisect_left(taps,(1,0))-1 curr_idx = curr_idx if curr_idx > 0 else 0 if taps[curr_idx][0] > 0: return -1 e_range = taps[curr_idx][1] res = 1 if e_range>=n: return 1 lo = curr_idx hi = len(taps) while e_range < n: hi = bisect.bisect_left(taps, (e_range+1, 0), lo, hi) print(hi) max_e = 0 for i in range(lo,hi): if taps[i][1] > max_e: curr_idx = i max_e = taps[i][1] if max_e <= e_range: return -1 e_range = max_e lo = curr_idx hi = len(taps) res += 1 return res
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n: int, ranges) -> int: def process_input(ranges): spans = [] for i in range(len(ranges)): spans.append([max(i - ranges[i], 0), i + ranges[i]]) return spans spans = process_input(ranges) spans.sort(key=lambda time: (time[0], -time[1])) sol = [] step = .5 for i in range(len(spans)): if step > n: continue if spans[i][0] < step and spans[i][1] > step: try: if spans[i][0] <= sol[-2][1]: sol.pop() except: pass sol.append(spans[i]) step = spans[i][1] + .5 if step < n: return -1 return len(sol)
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: items = [] for i, num in enumerate(ranges): items.append((max(0, i - num), min(i + num, n))) res = 0 right = 0 items.sort() i = 0 while i < len(items): farreach = right while i < len(items) and items[i][0] <= right: farreach = max(farreach, items[i][1]) i += 1 if farreach == right: return -1 res += 1 if farreach >= n: return res right = farreach return -1
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
from typing import List, Tuple from sortedcontainers import SortedSet class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: # print('-----') # print(f'n: {n}') # print(f'ranges: {ranges}') intervals = SortedSet() for i, w in enumerate(ranges): newInterval = (max(i - w, 0), min(i + w, n)) idx = intervals.bisect_left(newInterval) if idx < len(intervals) and self.aContainsB(intervals[idx], newInterval): continue elif idx > 0 and self.aContainsB(intervals[idx-1], newInterval): continue else: change = True while change: change = False idx = intervals.bisect_left(newInterval) if idx < len(intervals) and self.aContainsB(newInterval, intervals[idx]): del intervals[idx] change = True elif idx > 0 and self.aContainsB(newInterval, intervals[idx-1]): del intervals[idx-1] change = True elif idx > 1 and self.overlaps(newInterval, intervals[idx-2]): del intervals[idx-1] change = True intervals.add(newInterval) # print(f'intervals={intervals}') prevEnd = 0 for (start, end) in intervals: if start > prevEnd: return -1 prevEnd = end if end < n: return -1 return len(intervals) def aContainsB(self, a: Tuple[int, int], b: Tuple[int, int]) -> bool: a_start, a_end = a b_start, b_end = b return a_start <= b_start and a_end >= b_end def overlaps(self, a: Tuple[int, int], b: Tuple[int, int]) -> bool: a_start, a_end = a b_start, b_end = b return a_end >= b_start and b_end >= a_start
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: # Start at 6:00PM # Get ranges import heapq print(len(ranges)) range_tuples = [] for i, val in enumerate(ranges): if val == 0: continue range_tuples.append((max(0, i - val), min(n, i + val))) range_tuples = sorted(range_tuples) heap = [(0, 0)] for (start_pos, end_pos) in range_tuples: # Remove elements from the heap less than start_pos while heap and heap[0][0] < start_pos: heapq.heappop(heap) curr_min_cost = float('inf') while heap and heap[0][0] == start_pos: cost = heapq.heappop(heap)[1] if cost < curr_min_cost: curr_min_cost = cost if curr_min_cost == float('inf'): # Get the next closest point cost if not heap: return -1 else: heapq.heappush(heap, (start_pos, curr_min_cost)) curr_min_cost = min([a for (_, a) in heap]) heapq.heappush(heap, (end_pos, curr_min_cost + 1)) while heap and heap[0][0] < n: heapq.heappop(heap) min_cost = float('inf') while heap and heap[0][0] == n: cost = heap[0][1] if cost < min_cost: min_cost = cost heapq.heappop(heap) if min_cost == float('inf'): return -1 return min_cost
There is a one-dimensional garden on the x-axis. The garden starts at the point 0 and ends at the point n. (i.e The length of the garden is n). There are n + 1 taps located at points [0, 1, ..., n] in the garden. Given an integer n and an integer array ranges of length n + 1 where ranges[i] (0-indexed) means the i-th tap can water the area [i - ranges[i], i + ranges[i]] if it was open. Return the minimum number of taps that should be open to water the whole garden, If the garden cannot be watered return -1.   Example 1: Input: n = 5, ranges = [3,4,1,1,0,0] Output: 1 Explanation: The tap at point 0 can cover the interval [-3,3] The tap at point 1 can cover the interval [-3,5] The tap at point 2 can cover the interval [1,3] The tap at point 3 can cover the interval [2,4] The tap at point 4 can cover the interval [4,4] The tap at point 5 can cover the interval [5,5] Opening Only the second tap will water the whole garden [0,5] Example 2: Input: n = 3, ranges = [0,0,0,0] Output: -1 Explanation: Even if you activate all the four taps you cannot water the whole garden. Example 3: Input: n = 7, ranges = [1,2,1,0,2,1,0,1] Output: 3 Example 4: Input: n = 8, ranges = [4,0,0,0,0,0,0,0,4] Output: 2 Example 5: Input: n = 8, ranges = [4,0,0,0,4,0,0,0,4] Output: 1   Constraints: 1 <= n <= 10^4 ranges.length == n + 1 0 <= ranges[i] <= 100
class Solution: def minTaps(self, n: int, ranges: List[int]) -> int: # max_right from each point max_right = [0] * (n+1) for i, x in enumerate(ranges): l = max(0, i-x) r = min(n, i+x) max_right[l] = r # jump game ans = 0 reach = 0 while reach < n: max_reach = max(r for r in max_right[:reach+1]) if max_reach <= reach: return -1 reach = max_reach ans += 1 return ans