-
-
Notifications
You must be signed in to change notification settings - Fork 304
[sangyyypark] WEEK 02 solutions #2073
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,30 @@ | ||
| /** | ||
| 1. 문제 이해 | ||
| N이 주어지면 1부터 N까지의 숫자를 이용해서 N까지 도달 가능한 경우의 수(+1,+2만 가능)를 반환해야 하는 문제임. 중복을 허용하는 순열을 찾는 문제이다. | ||
|
|
||
| 2. naive algorithm 도출 | ||
| 피보나치 수열임. | ||
| N=1일때 가능한 경우의 수는 1 | ||
| N=2일때 가능한 경우의 수는 2 | ||
| N=3일때 가능한 경우의 수는 3 (n-2번째와 n-1번째의 합) | ||
|
|
||
| 3. 시간복잡도 분석 | ||
|
|
||
| O(N) | ||
| 4. 코드작성 | ||
| */ | ||
| class sangyyypark { | ||
| public int climbStairs(int n) { | ||
| if(n ==1) return 1; | ||
| if(n==2) return 2; | ||
| int [] dp = new int[n+1]; | ||
| dp[0] = 0; | ||
| dp[1] = 1; | ||
| dp[2] = 2; | ||
| for(int i = 3; i <= n; i++) { | ||
| dp[i] = dp[i-2] + dp[i-1]; | ||
| } | ||
| return dp[n]; | ||
| } | ||
| } | ||
|
|
||
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,36 @@ | ||
| /** | ||
| 1. 문제 이해 | ||
| answer[i] 에는 nums[i]를 제외한 나머지 수들을 모두 곱셈 했을때의 결과값이 들어가는 배열을 반환하는 문제 | ||
|
|
||
| 2. naive algorithm도출 | ||
|
|
||
| 가장 간단한 방법은 answer[i]에 값을 넣을때 nums배열을 탐색해서 nums[i]를 제외한 수를 곱셈해서 넣으면 끝이다. | ||
| 하지만, nums의 길이가 길면 시간복잡도가 O(N^2)이다. | ||
|
|
||
| answer[i]에는 nums[i]의 왼쪼까지의 곱과 오른쪽 까지의 곱을 곱하면 끝이므로 | ||
| answer[i]에 nums[i]의 왼쪽까지 합을 넣어놓고 answer[i]에 nums[i]의 오른쪽가지의 곱을 곱한다. | ||
|
|
||
| 3. 시간복잡도 분석 | ||
|
|
||
| 4. 코드구현 | ||
| */ | ||
| class sangyyypark { | ||
| public int[] productExceptSelf(int[] nums) { | ||
| int [] answer = new int[nums.length]; | ||
|
|
||
| int left = 1; | ||
| for(int i = 0; i < nums.length; i++) { | ||
| answer[i] = left; | ||
| left = nums[i] * left; | ||
| } | ||
|
|
||
| int right = 1; | ||
| for(int i = nums.length - 1; i >= 0; i--) { | ||
| answer[i] = answer[i] * right; | ||
| right = nums[i] * right; | ||
| } | ||
| return answer; | ||
| } | ||
|
|
||
| } | ||
|
|
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.
오 문제를 풀기위해 이해과정을 서술한게 보기 좋네요👍