-
-
Notifications
You must be signed in to change notification settings - Fork 360
[tigermint] WEEK 02 Solutions #2679
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
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
|
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: dp 배열로 각 단계의 경우의 수를 저장해 반복적으로 갱신한다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| class Solution { | ||
| fun climbStairs(n: Int): Int { | ||
| if (n == 1) return 1 | ||
| if (n == 2) return 2 | ||
|
|
||
| val dp = IntArray(n + 1) { 0 } | ||
| dp[1] = 1 | ||
| dp[2] = 2 | ||
|
|
||
| for (i in 3 .. n) { | ||
| dp[i] = dp[i - 1] + dp[i - 2] | ||
| } | ||
|
|
||
| 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,34 @@ | ||
| // class Solution { | ||
| // fun productExceptSelf(nums: IntArray): IntArray { | ||
| // val prefixProduct = IntArray(nums.size) { _ -> 1 } | ||
| // val suffixProduct = IntArray(nums.size) { _ -> 1 } | ||
|
|
||
| // for (i in 1 until nums.size) { | ||
| // prefixProduct[i] = prefixProduct[i - 1] * nums[i - 1] | ||
| // } | ||
|
|
||
| // for (i in nums.size - 2 downTo 0) { | ||
| // suffixProduct[i] = suffixProduct[i + 1] * nums[i + 1] | ||
| // } | ||
|
|
||
| // return IntArray(nums.size) { prefixProduct[it] * suffixProduct[it] } | ||
| // } | ||
| // } | ||
|
|
||
| class Solution { | ||
| fun productExceptSelf(nums: IntArray): IntArray { | ||
| val answer = IntArray(nums.size) { _ -> 1 } | ||
|
|
||
| for (i in 1 until nums.size) { | ||
| answer[i] = answer[i - 1] * nums[i - 1] | ||
| } | ||
|
|
||
| var right = 1 | ||
| for (i in nums.size - 1 downTo 0) { | ||
| answer[i] *= right | ||
| right *= 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,20 @@ | ||
| class Solution { | ||
| fun isAnagram(s: String, t: String): Boolean { | ||
| val sCharToCount = mutableMapOf<Char, Int>() | ||
| val tCharToCount = mutableMapOf<Char, Int>() | ||
|
|
||
| s.forEach { sCharToCount[it] = (sCharToCount[it] ?: 0) + 1 } | ||
| t.forEach { tCharToCount[it] = (tCharToCount[it] ?: 0) + 1 } | ||
|
|
||
| val sKeys: MutableSet<Char> = sCharToCount.keys | ||
| val tKeys: MutableSet<Char> = tCharToCount.keys | ||
|
|
||
| if (sKeys != tKeys) return false | ||
| sKeys.forEach { | ||
| if (sCharToCount[it] != tCharToCount[it]) { | ||
| return false | ||
| } | ||
| } | ||
| return true | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.
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.
이거 공간복잡도 O(1)에 시간복잡도 O(LogN)까지 줄일수 있어요!
나중에 시도해보시는것도 좋겠네요
배열이 아니라 필요한 정보만 저장하는 방식을 통해 확실하게 공간복잡도 줄이는건 쉽게 하실수 있으실거에요