From ed69e60dc0e04e80b9fa9295e388b6a6696d3a5e Mon Sep 17 00:00:00 2001 From: HyoJin Joo Date: Thu, 1 May 2025 21:13:54 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EA=B3=84=EB=8B=A8=20=EC=98=A4=EB=A5=B4?= =?UTF-8?q?=EA=B8=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hyoz/code.py" | 22 +++ .../hyoz/description.ipynb" | 126 ++++++++++++++++++ 2 files changed, 148 insertions(+) create mode 100644 "03_\352\263\204\353\213\250_\354\230\244\353\245\264\352\270\260/hyoz/code.py" create mode 100644 "03_\352\263\204\353\213\250_\354\230\244\353\245\264\352\270\260/hyoz/description.ipynb" diff --git "a/03_\352\263\204\353\213\250_\354\230\244\353\245\264\352\270\260/hyoz/code.py" "b/03_\352\263\204\353\213\250_\354\230\244\353\245\264\352\270\260/hyoz/code.py" new file mode 100644 index 0000000..5765dd0 --- /dev/null +++ "b/03_\352\263\204\353\213\250_\354\230\244\353\245\264\352\270\260/hyoz/code.py" @@ -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 + + # 최대값 배열에 점수 업데이트 + max_scores.append([f_score, s_score]) + +# 4. 목표 위치의 최대 점수 출력 +print(max(max_scores[-1])) \ No newline at end of file diff --git "a/03_\352\263\204\353\213\250_\354\230\244\353\245\264\352\270\260/hyoz/description.ipynb" "b/03_\352\263\204\353\213\250_\354\230\244\353\245\264\352\270\260/hyoz/description.ipynb" new file mode 100644 index 0000000..fbee770 --- /dev/null +++ "b/03_\352\263\204\353\213\250_\354\230\244\353\245\264\352\270\260/hyoz/description.ipynb" @@ -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 +}