Skip to content

Commit d87c5c2

Browse files
committed
[:solved] two problems
1 parent 2d4f7c2 commit d87c5c2

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

climbing-stairs/ppxyn1.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# idea: DP
2+
# 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.
3+
# If you have any suggestions for how I can come up with DP, I would appreciate your comments :)
4+
5+
class Solution:
6+
def climbStairs(self, n: int) -> int:
7+
if n <= 2:
8+
return n
9+
dp = [0] * (n+1)
10+
dp[2], dp[3] = 2, 3
11+
12+
#for i in range(4, n): error when n=4
13+
for i in range(4, n+1):
14+
dp[i] = dp[i-1] + dp[i-2]
15+
16+
return dp[n]
17+
18+
19+
20+

valid-anagram/ppxyn1.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#idea: dictionary
2+
from collections import Counter
3+
4+
class Solution:
5+
def isAnagram(self, s: str, t: str) -> bool:
6+
s_dic = Counter(sorted(s))
7+
t_dic = Counter(sorted(t))
8+
print(s_dic, t_dic)
9+
return s_dic==t_dic
10+
11+
12+
13+
# Trial and Error
14+
'''
15+
When you call sorted() on a dictionary, it only extracts and sorts the keys,and the values are completely ignored.
16+
class Solution:
17+
def isAnagram(self, s: str, t: str) -> bool:
18+
s_dic = Counter(s)
19+
t_dic = Counter(t)
20+
print(s_dic, t_dic)
21+
return sorted(s_dic)==sorted(t_dic)
22+
'''
23+
24+
25+
26+
27+

0 commit comments

Comments
 (0)