File tree Expand file tree Collapse file tree 3 files changed +59
-0
lines changed
product-of-array-except-self Expand file tree Collapse file tree 3 files changed +59
-0
lines changed Original file line number Diff line number Diff line change 1+ """
2+ Time complexity O(n)
3+ Space complexity O(n)
4+
5+ Dynamic programming
6+ """
7+
8+ class Solution :
9+ def climbStairs (self , n : int ) -> int :
10+ dp = [0 , 1 , 2 ] # distinct ways to reach i steps
11+ if n <= 2 :
12+ return dp [n ]
13+
14+ for i in range (3 , n + 1 ):
15+ dp .append (dp [i - 1 ] + dp [i - 2 ])
16+ return dp [n ]
Original file line number Diff line number Diff line change 1+ """
2+ Time complexity O(n)
3+ Space complexity O(n)
4+
5+ Prefix sum
6+ """
7+
8+ class Solution :
9+ def productExceptSelf (self , nums : List [int ]) -> List [int ]:
10+ products = [1 ]
11+ reverse_products = [1 ]
12+
13+ tmp = 1
14+ for n in nums [:- 1 ]:
15+ tmp *= n
16+ products .append (tmp )
17+ tmp = 1
18+ for n in nums [::- 1 ][:- 1 ]:
19+ tmp *= n
20+ reverse_products .append (tmp )
21+
22+ answer = [
23+ products [i ] * reverse_products [- i - 1 ]
24+ for i in range (len (nums ))
25+ ]
26+ return answer
Original file line number Diff line number Diff line change 1+ """
2+ Time complexity O(n)
3+ Space complexity O(n)
4+ """
5+ from collections import defaultdict
6+
7+ class Solution :
8+ def isAnagram (self , s : str , t : str ) -> bool :
9+ s_cnt = defaultdict (int )
10+ t_cnt = defaultdict (int )
11+ for char in s :
12+ s_cnt [char ] += 1
13+ for char in t :
14+ t_cnt [char ] += 1
15+ if s_cnt != t_cnt :
16+ return False
17+ return True
You can’t perform that action at this time.
0 commit comments