-
Notifications
You must be signed in to change notification settings - Fork 1
UniquePaths 문제 풀이 #9
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
gringrape
wants to merge
1
commit into
main
Choose a base branch
from
unique-paths
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
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,18 @@ | ||
| # Unique Paths | ||
| ## 문제 설명 | ||
| - `m x n` 격자가 주어진다. | ||
| - 로봇은 격자의 좌상단 맨끝에서 출발해서 우하단 맨끝으로 이동한다. | ||
| - 로봇은 한 번 이동할때, 우측으로 한칸 또는 아래로 한칸 이동한다. | ||
| - 이때, 로봇이 지나는 경로 중 unique 한 것들의 개수를 구하여라. | ||
|
|
||
| ## 풀이 계획 | ||
| - 위치별 경로 개수간의 관계가 존재함. | ||
| - 관계를 점화식으로 표현가능. | ||
|
|
||
| ``` | ||
| pathCount(row, column) = pathCount(row + 1, column) + pathCount(row, column + 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,42 @@ | ||
| def uniquePaths_simple_recursion(m, n): | ||
| def countPaths(r, c): | ||
| if r == m - 1 and c == n - 1: | ||
| return 1 | ||
|
|
||
| if r < 0 or r >= m or c < 0 or c >= n: | ||
| return 0 | ||
|
|
||
| return countPaths(r + 1, c) + countPaths(r, c + 1) | ||
|
|
||
| return countPaths(0, 0) | ||
|
|
||
|
|
||
| def uniquePaths_memoization(m, n): | ||
| counts = dict() | ||
|
|
||
| def countPaths(r, c): | ||
| if r == m - 1 and c == n - 1: | ||
| return 1 | ||
|
|
||
| if r < 0 or r >= m or c < 0 or c >= n: | ||
| return 0 | ||
|
|
||
| position = f'{r}-{c}' | ||
| if position not in counts: | ||
| counts[f'{r}-{c}'] = countPaths(r + 1, c) + countPaths(r, c + 1) | ||
|
|
||
| return counts[position] | ||
|
|
||
| return countPaths(0, 0) | ||
|
|
||
|
|
||
| solutions = [ | ||
| uniquePaths_simple_recursion, | ||
| uniquePaths_memoization, | ||
| ] | ||
|
|
||
|
|
||
| def test_unique_paths(): | ||
| for uniquePaths in solutions: | ||
| assert uniquePaths(m=3, n=7) == 28 | ||
| assert uniquePaths(m=3, n=2) == 3 | ||
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.
position 수정