Skip to content
34 changes: 34 additions & 0 deletions climbing-stairs/river20s.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class Solution(object):
def climbStairs(self, n):
"""
:type n: int
:rtype: int
f(n)이 계단을 올라갈 수 있는 방법의 수라면,
f(n) = f(n-1) + f(n-2)
- Time Complexity
재귀로 푸는 경우 지수 시간이 소요되지만,
아래와 같이 다이나믹 프로그래밍으로 푸는 경우
O(n) 소요 됨
- Space Complexity
재귀로 푸는 경우, 재귀 호출 스택이 O(n)만큼의 공간을 사용할 수 있음
아래와 같이 다이나믹 프로그래밍으로 푸는 경우,
리스트 dp에 각 인덱스 별로 결과를 저장하므로 O(n) 사용 됨

"""

# n이 1인 경우 방법은 하나뿐
if n == 1:
return 1

# 길이가 n+1인 리스트 생성
# 인덱스 0: 시작점(0번째)
# 인덱스 n: n번째 계단
dp = [0 for i in range(n + 1)]

dp[1] = 1 # n = 1이면 1을 반환
dp[2] = 2 # n = 2이면 2를 반환

for i in range(3, n + 1):
dp[i] = dp[i - 1] + dp[i - 2]

return dp[n]
20 changes: 20 additions & 0 deletions product-of-array-except-self/river20s.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution(object):
def productExceptSelf(self, nums):
"""
TimeComplexity: O(n)
SpaceComplexity: O(1)
"""
n = len(nums)
answer = [1] * n # 결과를 저장하는 리스트

# i번째 요소 왼쪽에 있는 요소들에 대해 prefix product 수행
for i in range(1, n):
answer[i] = answer[i - 1] * nums[i - 1]

# 오른쪽에 있는 요소들에 대해 suffix product 수행
right = 1 # 오른쪽 누적 곱
for i in range(n - 1, -1, -1):
answer[i] *= right # 현재 인덱스 왼쪽 곱과 오른쪽 곱을 곱함
right *= nums[i] #오른쪽 누적 곱 업데이트

return answer
57 changes: 57 additions & 0 deletions valid-anagram/river20s.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
class Solution(object):
def isAnagram(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool

s와 t의 길이가 같다는 전제 아래
s와 t를 각각 딕셔너리에 저장하고
딕셔너리끼리 비교하여 애너그램 여부를 검사하는 함수.

- 시간 복잡도: O(n)
- 공간 복잡도: O(n)
알파벳 소문자로 한정할 경우 O(1)로 볼 수도 있지만
추가 사항인 UNICODE 문자가 입력될 경우를 고려하여
공간 복잡도를 O(n)으로 계산함.

"""
# 먼저 s와 t의 길이 비교
# s와 t가 애너그램이라면 길이가 같음
if len(s) != len(t):
return False

# 입력 받은 s의 각 문자를 키, 빈도를 값으로 하는 딕셔너리
sdict = {}

# s의 각 문자를 순회하면서 sdict 구축
# O(n) 시간 소요

for char in s:
if char in sdict:
sdict[char] += 1
else:
sdict[char] = 1

# 입력 받은 t의 각 문자를 키, 빈도를 값으로 하는 딕셔너리
tdict = {}

# t의 각 문자를 순회하면서 tdict 구축
# O(n) 시간 소요

for char in t:
if char in tdict:
tdict[char] += 1
else:
tdict[char] = 1

# Python은 키의 순서에 상관 없이 딕셔너리끼리 바로 비교 가능
# sdict와 tdict 비교 후 같으면 True 같지 않으면 False 반환
# 딕셔너리 안의 키의 수가 k이고 모든 문자가 개별적이라면,
# 시간은 O(k)가 필요
# 여기서 k는 O(n) 수준이므로 전체 시간 복잡도는 O(n)

if sdict == tdict:
return True
else:
return False
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

저는 counter만 사용해서 풀었는데 딕셔너리 접근법도 좋은 것 같습니다! 마지막에 개행문자만 추가해주시면 될 것 같아요!