BOJ/백준-1912-python

cosmos·2021년 1월 18일
4
post-thumbnail

문제📖

풀이🙏

  • 첫째 줄에 정수 n(range(1,1000000+1)이 입력)
  • 둘째 줄에 n개의 정수로 이루어진 수열 한 줄 입력
    -> list(map(int,input().split())) 사용
  • 수열의 최대합 출력
    -> python 내장함수인 max를 사용

시간단축을 위해 언어는 pypy3, input은 sys.stdin.readline()을 사용

코드💻

# boj, 1912 : 연속합, python3
import sys

n = int(sys.stdin.readline())

if n > 1000000 or n < 1:
    print("n range error")
else:
    num = list(map(int,sys.stdin.readline().split()))
    if len(num) != n:
        print("error")
    else:
        result = [0] * n
        result[0] = num[0]
        for i in range(1, n):
            result[i] = max(num[i], num[i]+result[i-1])
        print(max(result))

결과😎

출처📝

https://www.acmicpc.net/problem/1912

github

github

0개의 댓글