complete 1806.py
This commit is contained in:
@@ -1,14 +0,0 @@
|
|||||||
def p_sum(cumulative, left, right):
|
|
||||||
if 0 <= left <= right <= N:
|
|
||||||
return cumulative[right] - cumulative[left]
|
|
||||||
else:
|
|
||||||
return 100000000000000000000000000
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
N, S = map(int, input().split())
|
|
||||||
A: list[int] = list(map(int, input().split()))
|
|
||||||
CA: list[int] = [0]
|
|
||||||
|
|
||||||
|
|
||||||
print(right - left)
|
|
||||||
42
zeta_python/completed/1806.py
Normal file
42
zeta_python/completed/1806.py
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
import sys
|
||||||
|
|
||||||
|
input = sys.stdin.readline
|
||||||
|
|
||||||
|
|
||||||
|
class PrefixSum:
|
||||||
|
def __init__(self, N: int, arr: list[int]):
|
||||||
|
self.N = N
|
||||||
|
self.__arr = arr
|
||||||
|
self.__cumulative = [0]
|
||||||
|
for i in range(N):
|
||||||
|
self.__cumulative.append(self.__cumulative[-1] + self.__arr[i])
|
||||||
|
|
||||||
|
def sum_range(self, left: int, right: int) -> int:
|
||||||
|
return self.__cumulative[right] - self.__cumulative[left]
|
||||||
|
|
||||||
|
def solve(self, S: int) -> int:
|
||||||
|
mindist = 10000001
|
||||||
|
left = 0
|
||||||
|
right = 0
|
||||||
|
while right <= N:
|
||||||
|
if left > right:
|
||||||
|
right += 1
|
||||||
|
continue
|
||||||
|
if self.sum_range(left, right) >= S:
|
||||||
|
mindist = min([right - left, mindist])
|
||||||
|
left += 1
|
||||||
|
else:
|
||||||
|
right += 1
|
||||||
|
if mindist > self.N:
|
||||||
|
return 0
|
||||||
|
elif mindist == 0:
|
||||||
|
return 1
|
||||||
|
return mindist
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
N, S = map(int, input().split())
|
||||||
|
solver = PrefixSum(N, list(map(int, input().split())))
|
||||||
|
|
||||||
|
print(solver.solve(S))
|
||||||
Reference in New Issue
Block a user