Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions 03_계단_오르기/hyoz/code.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
n = int(input())

scores = [int(input()) for _ in range(n)]
max_scores = []

for c_step in range(n):
c_score = scores[c_step]

# f_score: 현재 위치가 첫 번째 스텝일 때 최대값
# s_score: 현재 위치가 두 번째 스텝일 때 최대값
if c_step < 2:
f_score = c_score
else:
f_score = max(max_scores[c_step-2])+c_score

s_score = max_scores[c_step-1][0]+c_score if c_step >= 1 else 0
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

현재 스텝이 두 번째 스텝일때의 최대값을 구하는거여서
max_scores[c_step-1][0]로 한 칸 이전 스텝의 최대값을 가져오는것까지는 이해했는데
c_step >= 1 else 0을 해주는 부분이 잘 이해가 안돼서요 😢
혹시 저부분은 어떤 의미를 나타내는건가용?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s_score = max_scores[c_step-1][0]+c_score if c_step >= 1 else 0

안녕하세요 :)
위 구문은 3항 연산자입니다. 따라서 아래 코드와 동일한 역할을 하고 있습니다.

현재 스텝(c_step)이 두 번째 이후일 때부터 고려해주기 위해 c_step 값이 1 이상 (인덱스는 0부터 시작하기 때문)일 때부터 고려합니다.

if c_step >= 1:
    s_score = max_scores[c_step-1][0]+c_score
else:
    s_score = 0


# 최대값 배열에 점수 업데이트
max_scores.append([f_score, s_score])

# 4. 목표 위치의 최대 점수 출력
print(max(max_scores[-1]))
126 changes: 126 additions & 0 deletions 03_계단_오르기/hyoz/description.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "ee19dcc8-06b5-498a-8dd3-32b33e733ae0",
"metadata": {},
"source": [
"# Description"
]
},
{
"cell_type": "markdown",
"id": "710bb1e9-ca83-49cd-8a35-99c92f6c4fc1",
"metadata": {},
"source": [
"### 문제 개요\n",
"* 문제: [계단 오르기](https://www.acmicpc.net/problem/2579)\n",
"* 문제 요약\n",
" * 문제에서 주어진 규칙을 기반으로 계단 오르기 게임을 수행했을 때 최대 점수를 출력하는 문제이다.\n",
" - 규칙: 연속 세 계단 밟기 X, 두 계단 뛰어넘기 X, 마지막 계단은 꼭 밟기\n",
" * 입력: 계단의 개수(300 이하의 자연수)가 주어지고, 계단의 개수만큼 점수값(1만 이하의 자연수)이 주어진다.\n",
" * 출력: 계단 오르기 후 최대 점수값 (자연수)\n",
"* 문제 유형: DP (Dynamic Programming)"
]
},
{
"cell_type": "markdown",
"id": "219f5710-333b-4cbb-8b6a-6a71042d53cb",
"metadata": {},
"source": [
"# History of Code"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "250c8e8c-5be3-4cf6-9aa1-4e988af5d1ee",
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
" 6\n",
" 10\n",
" 20\n",
" 15\n",
" 25\n",
" 10\n",
" 20\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"75\n"
]
}
],
"source": [
"'''\n",
"시간 복잡도: O(N)\n",
"\n",
"구현 순서\n",
"1. 반복문을 통해 각 위치에서 최대값을 업데이트하며, 최대값은 첫 번째 스텝인지 두 번째 스텝인지에 따라 구분하여 계산한다.\n",
"- 첫 번째 스텝일 때(아래에서 두 계단 제외): `두 계단 아래에서 최대값 + 현재 위치의 점수`를 통해 계산\n",
"- 두 번째 스텝일 때(첫 계단 제외): `한 계단 아래 위치의 첫 번째 스텝 값 + 현재 위치의 점수`를 통해 계산\n",
"2. 가장 마지막 위치의 최대값을 출력한다.\n",
"'''\n",
"\n",
"n = int(input())\n",
"\n",
"scores = [int(input()) for _ in range(n)]\n",
"max_scores = []\n",
"\n",
"for c_step in range(n):\n",
" c_score = scores[c_step]\n",
" \n",
" # 1. 현재 위치가 첫 번째 스텝일 때 최대값\n",
" if c_step < 2:\n",
" f_score = c_score\n",
" else:\n",
" f_score = max(max_scores[c_step-2])+c_score\n",
" \n",
" # 2. 현재 위치가 두 번째 스텝일 때 최대값\n",
" s_score = max_scores[c_step-1][0]+c_score if c_step >= 1 else 0 \n",
" \n",
" # 3. 최대값 배열에 점수 업데이트\n",
" max_scores.append([f_score, s_score])\n",
"\n",
"# 4. 목표 위치의 최대 점수 출력\n",
"print(max(max_scores[-1]))"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d25f219c-6279-451c-8ee2-03a64e126f0e",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}