-
Notifications
You must be signed in to change notification settings - Fork 1
[W3] 계단 오르기 #12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dev-hjJoo
wants to merge
1
commit into
main
Choose a base branch
from
hyoz_w3_1
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
[W3] 계단 오르기 #12
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
|
|
||
| # 최대값 배열에 점수 업데이트 | ||
| max_scores.append([f_score, s_score]) | ||
|
|
||
| # 4. 목표 위치의 최대 점수 출력 | ||
| print(max(max_scores[-1])) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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을 해주는 부분이 잘 이해가 안돼서요 😢
혹시 저부분은 어떤 의미를 나타내는건가용?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
안녕하세요 :)
위 구문은 3항 연산자입니다. 따라서 아래 코드와 동일한 역할을 하고 있습니다.
현재 스텝(c_step)이 두 번째 이후일 때부터 고려해주기 위해 c_step 값이 1 이상 (인덱스는 0부터 시작하기 때문)일 때부터 고려합니다.