본문 바로가기
Beakjoon&프로그래머스/파이썬

[백준/파이썬]11659번 구간 합 구하기 4

by 현장 2021. 7. 26.

-코드

from sys import stdin
n, m = map(int, input().split())
num = list(map(int, stdin.readline().split()))
S = [0]
temp = 0
for i in num:
    temp += i
    S.append(temp)

for i in range(m):
    i, j = map(int, stdin.readline().split())
    print(S[j] - S[i - 1])

처음에 for문으로 합을 구했으나 시간 초과로 틀려서 찾아보니 미리 합을 구하고 구한 값을 조합하여 해결하는 문제여서 i와 j값으로 num위치를 조합하여 해결하였지만 혼자서 해결을 못한 것 같아서 아쉬웠습니다.