Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |
Tags
- 스택
- 인스타
- 백준
- 운체 1주차
- c언어 제어문
- 1주차(1)
- c언어 기본
- python기본
- 자료구조
- python자료형
- python기초
- git오류
- 인텔리제이
- 파이썬 알고리즘 인터뷰
- 최단거리
- DP
- 코테
- 5장
- 4장
- 데베시 1주차
- 참고X
- #코린이 #코딩 #할 수 있다
- Workbench
- Git
- c언어
- 도커
- git 오류
- git기초
- 코딩테스트
- 그리디
Archives
- Today
- Total
하루살이 개발자
[BaekJoon 2579번] 계단오르기 문제(Python) 본문
코딩테스트 연습 3일차
계단오르기 문제입니다.
문제링크: https://www.acmicpc.net/problem/2579
2579번: 계단 오르기
계단 오르기 게임은 계단 아래 시작점부터 계단 꼭대기에 위치한 도착점까지 가는 게임이다. <그림 1>과 같이 각각의 계단에는 일정한 점수가 쓰여 있는데 계단을 밟으면 그 계단에 쓰여 있는 점
www.acmicpc.net
Solution
import sys
input = sys.stdin.readline
n = int(input()) #계단의 개수
score = []
sum = []
for _ in range(n): #점수 넣기
score.append(int(input()))
if n==1:
print(score[0])
elif n == 2:
print(max(score[0]+score[1], score[1]))
else:
sum.append(score[0])
sum.append(max(score[0] + score[1], score[1]))
sum.append(max(score[0] + score[2], score[1] + score[2]))
for i in range(3, n):
sum.append(max(sum[i-3] + score[i-1] + score[i], sum[i-2] + score[i]))
print(sum[-1])
또는
import sys
input = sys.stdin.readline
n = int(input()) #계단의 개수
score = []
sum = []
for _ in range(n): #점수 넣기
score.append(int(input()))
if n==1:
print(score[0])
exit()
elif n == 2:
print(max(score[0]+score[1], score[1]))
exit()
sum.append(score[0])
sum.append(max(score[0] + score[1], score[1]))
sum.append(max(score[0] + score[2], score[1] + score[2]))
for i in range(3, n):
sum.append(max(sum[i-3] + score[i-1] + score[i], sum[i-2] + score[i]))
print(sum[-1])
풀이
마지막 계단 전 계단을 포함하느냐, 안하느냐에 따라 나누기
'코딩테스트' 카테고리의 다른 글
[BaekJoon 14916번] 거스름돈 문제(Python) (0) | 2022.01.10 |
---|---|
[BaekJoon 2839번] 설탕배달 문제(Python) (0) | 2022.01.10 |
[BaekJoon 20365번] 블로그2 문제(Python) (0) | 2022.01.05 |
[BaekJoon 20115번] 에너지 드링크 문제(Python) (0) | 2022.01.05 |
[BaekJoon 13305번] 주유소 문제(Python) (0) | 2022.01.05 |