-
-
Notifications
You must be signed in to change notification settings - Fork 362
[seueooo] WEEK 02 solutions #2699
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
base: main
Are you sure you want to change the base?
Changes from all commits
238d473
95b667f
99f633f
39944ba
39fd1e0
326c333
a352282
ff91089
087088e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| /** | ||
| * @param {number} n | ||
| * @return {number} | ||
| * | ||
| * 풀이 | ||
| * 1. dp를 사용하여 각 계단까지 올라갈 수 있는 방법의 수를 계산한다. | ||
| * 2. dp[i]는 i번째 계단까지 올라갈 수 있는 방법의 수를 나타낸다. | ||
| * 3. dp[i] = dp[i - 1] + dp[i - 2] : 마지막에 한 번에 1계단을 올라온 경우와 2계단을 올라온 경우를 합친다. | ||
| * 시간 복잡도 - O(n) : 배열을 한 번 순회 | ||
| * 공간 복잡도 - O(n) : dp 배열 생성 | ||
| */ | ||
| var climbStairs = function (n) { | ||
| let dp = []; | ||
| dp[0] = 0; | ||
| dp[1] = 1; | ||
| dp[2] = 2; | ||
|
|
||
| for (let i = 3; i <= n; i++) { | ||
| dp[i] = dp[i - 1] + dp[i - 2]; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| } | ||
|
|
||
| return dp[n]; | ||
| }; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 두 번의 순회를 통해 각 위치의 누적 곱을 계산하여 결과를 구성한다. 개선 제안: 현재 구현이 적절해 보입니다. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| /** | ||
| * 풀이 | ||
| * 1. prefix는 현재 인덱스까지의 곱을 저장하고, suffix는 현재 인덱스 이후의 곱을 저장한다. | ||
| * 2. 첫 번째 반복문에서 prefix를 계산하고, 두 번째 반복문에서 suffix를 계산하여 answer 배열에 곱한다. | ||
| * 시간 복잡도 - O(n) : 배열을 두 번 순회 | ||
| * 공간 복잡도 - O(1) : answer 배열 | ||
| * | ||
| * @param {number[]} nums | ||
| * @return {number[]} | ||
| */ | ||
| var productExceptSelf = function (nums) { | ||
| let prefix = 1; | ||
| let suffix = 1; | ||
| const n = nums.length; | ||
| let answer = new Array(n).fill(1); | ||
|
|
||
| // 순방향 | ||
| for (let i = 0; i < n; i++) { | ||
| answer[i] = prefix; | ||
| prefix *= nums[i]; | ||
| } | ||
|
|
||
| // 역방향 | ||
| for (let i = n - 1; i >= 0; i--) { | ||
| answer[i] *= suffix; | ||
| suffix *= nums[i]; | ||
| } | ||
| return answer; | ||
| }; |
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 한 번의 카운트 증가와 한 번의 감소로 검증한다. 개선 제안: 현재 구현이 적절해 보입니다. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| /** | ||
| * 풀이 | ||
| * 1. 두 문자열의 길이가 다르면 false를 반환한다. | ||
| * 2. 첫 번째 문자열을 순회하며 각 문자의 등장 횟수를 count 객체에 저장한다. | ||
| * 3. 두 번째 문자열을 순회하며 count 객체에서 해당 문자의 등장 횟수를 감소시킨다. | ||
| * 4. 만약 count 객체에 해당 문자가 없거나 등장 횟수가 0이면 false를 반환한다. | ||
| * 5. 모든 문자를 순회한 후에도 false를 반환하지 않았다면 true를 반환한다. | ||
| * 시간 복잡도 - O(n) : 두 문자열을 한 번씩 순회 | ||
| * 공간 복잡도 - O(1) : count 객체는 최대 26개의 알파벳만 저장 | ||
| */ | ||
| var isAnagram = function (s, t) { | ||
| let count = {}; | ||
| if (s.length !== t.length) { | ||
| return false; | ||
| } | ||
| for (const i of s) { | ||
| count[i] = count[i] ? count[i] + 1 : 1; | ||
| } | ||
| for (const i of t) { | ||
| if (!count[i]) return false; | ||
| count[i]--; | ||
| } | ||
| return true; | ||
| }; |
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.
🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: dp 배열을 사용해 i번째 계단에 오르는 방법의 수를 누적 계산한다.
개선 제안: 현재 구현이 적절해 보입니다.