Skip to content

[hu6r1s] WEEK 02 Solutions #1732

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions climbing-stairs/hu6r1s.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Solution:
"""
1일 때, 1 step -> 1개
2일 때, 1 + 1, 2 -> 2개
3일 때, 1 + 1 + 1, 1 + 2, 2 + 1 -> 3개
4일 때, 1 + 1 + 1 + 1, 1 + 1 + 2, 1 + 2 + 1, 2 + 1 + 1, 2 + 2 -> 5개
5일 때, 1 + 1 + 1 + 1 + 1, 1 + 1 + 1 + 2, 1 + 1 + 2 + 1, 1 + 2 + 1 + 1, 2 + 1 + 1 + 1, 1 + 2 + 2, 2 + 1 + 2, 2 + 2 + 1 -> 8개
순서대로 봤을 때, 이전 값과 그 이전 값의 합이 현재 개수이다.
이를 점화식으로 봤을 때, dp[i] = d[i - 1] + dp[i - 2]가 된다.

- Time Complexity: O(n)
- 1부터 n까지 한 번씩 반복하므로 선형 시간
- Space Complexity: O(n)
- dp 배열에 n개의 값을 저장하므로 선형 공간 사용
"""
def climbStairs(self, n: int) -> int:
if n == 1:
return n

dp = [0] * n
dp[0] = 1
dp[1] = 2
for i in range(2, n):
dp[i] = dp[i - 1] + dp[i - 2]
return dp[-1]
33 changes: 33 additions & 0 deletions valid-anagram/hu6r1s.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from collections import defaultdict

class Solution:
"""
1. Counter를 이용한 풀이
- Counter로 풀이하였으나 s, t 길이가 서로 다를 때 해결되지 않음
2. defaultdict 활용 풀이
- 똑같이 개수 세고 제외

TC
- s의 길이: n, t의 길이: n (조건상 같거나 비교 가능한 길이)
- 첫 번째 for 루프: O(n) → s의 모든 문자를 순회
- 두 번째 for 루프: O(n) → t의 모든 문자를 순회
- 총 시간 복잡도 = O(n)

SC
- counter는 알파벳 개수를 세는 용도로만 사용됨
- 공간 복잡도 = O(1)
"""
def isAnagram(self, s: str, t: str) -> bool:
counter = defaultdict(int)

for i in s:
counter[i] += 1

for i in t:
if i not in counter:
return False
counter[i] -= 1

if counter[i] == 0:
del counter[i]
return False if counter else True