-
-
Notifications
You must be signed in to change notification settings - Fork 361
[okyungjin] WEEK 02 solutions #2683
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,24 @@ | ||||||||||
| # 시간 복잡도: O(N) | ||||||||||
| # 공간 복잡도: O(1) | ||||||||||
|
|
||||||||||
| # [요구사항] | ||||||||||
| # 1. 계단의 개수 n이 주어진다. | ||||||||||
| # 2. 계단은 1칸 또는 2칸 올라갈 수 있다. | ||||||||||
| # 3. 마지막 칸에 도달할 수 있는 경우의 수를 반환한다. | ||||||||||
|
|
||||||||||
| # [접근법] | ||||||||||
| # 1. 계단에 올라올 수 있는 경우의 수는 전전칸까지의 경우의 수 (prev2) + 전칸까지의 경우의 수 (prev1) 를 더한 값이다. | ||||||||||
| class Solution: | ||||||||||
| def climbStairs(self, n: int) -> int: | ||||||||||
| if n <= 2: | ||||||||||
| return n | ||||||||||
|
|
||||||||||
| prev2 = 1 # 전전칸 까지의 경우의 수 | ||||||||||
| prev1 = 2 # 전칸 까지의 경우의 수 | ||||||||||
|
|
||||||||||
| for _ in range(2, 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. 개인적으로는 3번째 계단에서 시작하는 거면, 3이라고 명시하는 걸 더 선호해서요. 이렇게도 쓸 수 있을 것 같아요.
Suggested change
|
||||||||||
| curr = prev2 + prev1 # 현재 계단 | ||||||||||
| prev2 = prev1 | ||||||||||
| prev1 = curr | ||||||||||
|
Comment on lines
+20
to
+22
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.
Suggested change
파이썬이니까 동시 할당을 활용하는 방법도 있을 것 같아요. |
||||||||||
|
|
||||||||||
| return prev1 | ||||||||||
|
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. 🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: Counter를 사용해 문자 빈도를 관리하고, 순회 중 차감으로 동작을 확인합니다. 개선 제안: 현재 구현이 적절해 보입니다.
|
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,32 @@ | ||||||||||||||||||||||||||||||
| # https://leetcode.com/problems/house-robber/description/ | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| # [요구사항] | ||||||||||||||||||||||||||||||
| # 문자열 두 개가 주어졌을 때 애너그램이 맞는지 아닌지를 반환하는 문제 | ||||||||||||||||||||||||||||||
| # 애너그램이란? 하나의 단어나 구에 들어 있는 글자들을 모두, 각각 정확히 한 번씩만 사용해서 순서를 바꾸어 만든 다른 단어나 구를 의미한다. | ||||||||||||||||||||||||||||||
| # 두 문자열이 애너그램이면 True, 아니면 False 반환 | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| # [접근법] | ||||||||||||||||||||||||||||||
| # 1. 길이가 다르면 애너그램이 아니므로 바로 False를 반환한다. | ||||||||||||||||||||||||||||||
| # 2. s의 문자 개수를 센 뒤, t를 순회하며 하나씩 차감한다. | ||||||||||||||||||||||||||||||
| # 3. 없는 문자이거나 개수가 부족한 문자를 만나면 False를 반환한다. | ||||||||||||||||||||||||||||||
| # 4. 모든 문자를 정상적으로 처리하면 True를 반환한다. | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| # Time: O(N) | ||||||||||||||||||||||||||||||
| # Space: O(N) | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| from collections import Counter | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| class Solution: | ||||||||||||||||||||||||||||||
| def isAnagram(self, s: str, t: str) -> bool: | ||||||||||||||||||||||||||||||
| if len(s) != len(t): | ||||||||||||||||||||||||||||||
| return False | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| counter = Counter(s) | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| for char in t: | ||||||||||||||||||||||||||||||
| if counter[char] >= 1: | ||||||||||||||||||||||||||||||
| counter[char] -= 1 | ||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||
| return False | ||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||
| return True | ||||||||||||||||||||||||||||||
|
Comment on lines
+21
to
+32
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.
Suggested change
|
||||||||||||||||||||||||||||||
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.
🏷️ 알고리즘 패턴 분석
📊 시간/공간 복잡도 분석
피드백: 상태를 두 변수로 유지하며 순차적으로 누적 합을 계산하므로 시간은 선형이고 추가 공간은 상수입니다.
개선 제안: 현재 구현이 적절해 보입니다.