Skip to content

Commit f86ca32

Browse files
committed
88차 1번 문제 풀이
1 parent 795a71d commit f86ca32

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

live8/test88/문제1/박희경.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
def solution(sequence, k):
2+
answer = []
3+
4+
i, j = 0, 0
5+
total = sum(sequence[i: j + 1])
6+
while i + j < len(sequence) * 2 - 1:
7+
if i <= j:
8+
if total == k:
9+
answer.append([i, j])
10+
total -= sequence[i]
11+
i += 1
12+
j += 1
13+
if j < len(sequence):
14+
total += sequence[j]
15+
elif total < k:
16+
j += 1
17+
if j < len(sequence):
18+
total += sequence[j]
19+
else:
20+
total -= sequence[i]
21+
i += 1
22+
23+
# res = []
24+
# if len(answer) == 1:
25+
# return answer[0]
26+
# min_diff = float("inf")
27+
# for ans in answer:
28+
# diff = ans[1] - ans[0]
29+
# if diff < min_diff:
30+
# res = ans
31+
# min_diff = diff
32+
# return res
33+
answer = sorted(answer, key=lambda x: x[1] - x[0]) # 코드 개선
34+
return answer[0]

0 commit comments

Comments
 (0)