Skip to content
Merged
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
32 changes: 29 additions & 3 deletions climbing-stairs/ppxyn1.py
Copy link
Contributor

Choose a reason for hiding this comment

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

DP 배열을 이용해서 dp[i] = dp[i-1] + dp[i-2]를 적용한 정석 풀이인 것 같습니다.
해당 문제는 이전 두 값만 사용하기 때문에 배열을 사용하지 않고 두 변수만 유지하면 공간 복잡도를 O(1)로 줄일 수도 있을 것 같다고 생각합니다.

Original file line number Diff line number Diff line change
@@ -1,6 +1,33 @@
# idea: DP
# I'm not always sure how to approach DP problems. I just try working through a few examples step by step and then check that it would be DP.
# If you have any suggestions for how I can come up with DP, I would appreciate your comments :)
# Time Complexity : O(n)
'''
n = 4
-------------------------------
(dp[3] + 1step)
1step + 1step + 1step + 1step
1step + 2step + 1step
2step + 1step + 1step

(dp[2] + 2step)
1step + 1step + 2step
2step + 2step

================================
n = 5
--------------------------------
(dp[4] + 1step)
1step + 1step + 1step + 1step + 1step
1step + 2step + 1step + 1step
1step + 1step + 2step + 1step
2step + 1step + 1step + 1step
2step + 2step + 1step

(dp[3] + 2step)
1step + 2step + 2step
2step + 1step + 2step
1step + 1step + 1step + 2step
'''


class Solution:
def climbStairs(self, n: int) -> int:
Expand All @@ -9,7 +36,6 @@ def climbStairs(self, n: int) -> int:
dp = [0] * (n+1)
dp[2], dp[3] = 2, 3

#for i in range(4, n): error when n=4
for i in range(4, n+1):
dp[i] = dp[i-1] + dp[i-2]

Expand Down
35 changes: 17 additions & 18 deletions valid-anagram/ppxyn1.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
#idea: dictionary
from collections import Counter
# class Solution:
# def isAnagram(self, s: str, t: str) -> bool:
# s_dic = Counter(sorted(s))
# t_dic = Counter(sorted(t))
# return s_dic==t_dic

class Solution:
def isAnagram(self, s: str, t: str) -> bool:
s_dic = Counter(sorted(s))
t_dic = Counter(sorted(t))
print(s_dic, t_dic)
return s_dic==t_dic
# fixed
# do not need to both Counter and sorting



# Trial and Error
'''
When you call sorted() on a dictionary, it only extracts and sorts the keys,and the values are completely ignored.
# Ans 1 (Only Counter)
# Time Complexity: O(N)
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
s_dic = Counter(s)
t_dic = Counter(t)
print(s_dic, t_dic)
return sorted(s_dic)==sorted(t_dic)
'''



return s_dic==t_dic

# Ans 2 (Only Sorting)
# Time Complexity: O(Nlog(N))
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
s_arr = sorted(s)
t_arr = sorted(t)
return s_arr==t_arr