Skip to content

Commit f60756f

Browse files
committed
백준 1806번 부분합 v2
문제 링크: https://www.acmicpc.net/problem/1806
1 parent 951700b commit f60756f

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

백준 1806번 부분합 v2/main.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import sys
2+
3+
4+
def solve():
5+
input = sys.stdin.readline
6+
N, S = list(map(int, input().strip().split()))
7+
dts = list(map(int, input().strip().split()))
8+
9+
idx_left, idx_right = 0, 0
10+
sum_local = dts[0]
11+
answer = sys.maxsize
12+
13+
while idx_left <= idx_right and idx_right < N:
14+
if sum_local >= S:
15+
answer = min(answer, idx_right - idx_left + 1)
16+
sum_local -= dts[idx_left]
17+
idx_left += 1
18+
else:
19+
idx_right += 1
20+
if idx_right < N:
21+
sum_local += dts[idx_right]
22+
else:
23+
break
24+
25+
26+
if answer == sys.maxsize:
27+
print(0)
28+
else:
29+
print(answer)
30+
31+
32+
if __name__ == '__main__':
33+
solve()

백준 1806번 부분합 v2/test1.txt

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
10 15
2+
5 1 3 5 10 7 4 9 2 8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import sys
2+
from pathlib import Path
3+
from unittest import TestCase
4+
from main import solve
5+
6+
7+
class Test(TestCase):
8+
def my_solve(self, testcase_input):
9+
sys.stdin = open(testcase_input, 'r')
10+
stdout = sys.stdout
11+
sys.stdout = open('stdout.txt', 'w')
12+
solve()
13+
sys.stdout.close()
14+
sys.stdout = stdout
15+
16+
def test_solve(self, testcase_number: str):
17+
self.my_solve('test' + testcase_number + '.txt')
18+
self.assertEqual(
19+
Path('test' + testcase_number + '_answer.txt').read_text().strip(),
20+
Path('stdout.txt').read_text().strip())
21+
22+
def test1_solve(self):
23+
self.test_solve('1')

0 commit comments

Comments
 (0)