From ab0eeae83c03af0b074ee2eb591a8fbf942b6a63 Mon Sep 17 00:00:00 2001 From: yenru0 Date: Thu, 9 Jan 2025 17:28:48 +0900 Subject: [PATCH] complete 1806.py --- zeta_python/1806.py | 14 ------------ zeta_python/completed/1806.py | 42 +++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 14 deletions(-) delete mode 100644 zeta_python/1806.py create mode 100644 zeta_python/completed/1806.py diff --git a/zeta_python/1806.py b/zeta_python/1806.py deleted file mode 100644 index 913d0b6..0000000 --- a/zeta_python/1806.py +++ /dev/null @@ -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) diff --git a/zeta_python/completed/1806.py b/zeta_python/completed/1806.py new file mode 100644 index 0000000..1737c22 --- /dev/null +++ b/zeta_python/completed/1806.py @@ -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))