Repository navigation
[Yu-Jaeyoung] WEEK 02 Solutions #2935
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(1) | ||
| # With 1 <= n <= 45, all counts fit within a 32-bit signed integer. | ||
| # Approach: Build each count by adding the previous two counts. | ||
| # | ||
| # Solution history | ||
| # 1. Counted arrangements using C(n - k, k) for each possible k. | ||
| # 2. Replaced combination calls with additions of the previous two counts. | ||
| # Keeps only two values. Current solution. | ||
| class Solution: | ||
| def climbStairs(self, n: int) -> int: | ||
| if n <= 2: | ||
| return n | ||
|
|
||
| prev, curr = 1, 2 | ||
|
|
||
| for _ in range(3, n + 1): | ||
| prev, curr = curr, prev + curr | ||
|
|
||
| return curr | ||
|
|
||
|
|
||
| # Previous solution (Attempt 1: Combinations) | ||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(1) | ||
| # These bounds assume 1 <= n <= 45, where CPython's math.comb | ||
| # uses fixed-size integer fast paths and all values remain small. | ||
| # | ||
| # from math import comb | ||
| # | ||
| # class Solution: | ||
| # def climbStairs(self, n: int) -> int: | ||
| # step = 0 | ||
| # for k in range(n // 2 + 1): | ||
| # step += comb(n - k, k) | ||
| # | ||
| # return step | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # Time: O(n + m) on average, Space: O(1) | ||
| # n = len(s), m = len(t); at most 26 distinct lowercase letters. | ||
| class Solution: | ||
| def isAnagram(self, s: str, t: str) -> bool: | ||
| count_s = {} | ||
| for char in s: | ||
| if char in count_s: | ||
| count_s[char] += 1 | ||
| else: | ||
| count_s[char] = 1 | ||
|
Comment on lines
+7
to
+10
Member
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. 저라면 원래 이 부분을 count_s[char] = count_s.get(char, 0) + 1로 줄였을 텐데,
Contributor
Author
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. 남은 주간 저도 열심히 리뷰해보겠습니다. |
||
|
|
||
| count_t = {} | ||
| for char in t: | ||
| if char in count_t: | ||
| count_t[char] += 1 | ||
| else: | ||
| count_t[char] = 1 | ||
|
|
||
| return count_s == count_t | ||
|
Comment on lines
+5
to
+19
Member
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. 안녕하세요:)
Contributor
Author
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. Python이 익숙하지 않아 ord 함수가 무엇인지 찾아보았네요 .. 🤣 |
||
|
|
||
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.
기존 풀이를 조합으로 접근하신 게 새롭네요!
저는 피보나치 수열 1, 2, 3, 5, 8, ...처럼 접근하였는데
2칸을 k번 밟으면 1칸은 n - 2k번이니, 총 이동 횟수 n - k번 중 2칸을 밟을 자리 k개를 고르는 방식으로 접근하셨네요
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.
확률과 통계를 기억하며 조합으로 풀이해봤습니다 !
깔끔하게 풀리더라구요 😃