File tree 5 files changed +150
-0
lines changed
construct-binary-tree-from-preorder-and-inorder-traversal
5 files changed +150
-0
lines changed Original file line number Diff line number Diff line change
1
+ from typing import List
2
+
3
+ class Solution :
4
+ def threeSum (self , nums : List [int ]) -> List [List [int ]]:
5
+ result = []
6
+ nums .sort () # sort nums before using two-pointers
7
+
8
+ for i , num in enumerate (nums ):
9
+ # skip duplicated targets
10
+ if i > 0 and nums [i ] == nums [i - 1 ]:
11
+ continue
12
+
13
+ target = - num
14
+ left , right = i + 1 , len (nums ) - 1
15
+
16
+ while left < right :
17
+ if nums [left ] + nums [right ] == target :
18
+ result .append ([num , nums [left ], nums [right ]])
19
+
20
+ # skip duplicated numbers ( ex. nums = [-3 0 0 0 3 3] )
21
+ while left < right and nums [left ] == nums [left + 1 ]:
22
+ left += 1
23
+ while left < right and nums [right ] == nums [right - 1 ]:
24
+ right -= 1
25
+
26
+ left += 1
27
+ right -= 1
28
+ elif nums [left ] + nums [right ] < target :
29
+ left += 1
30
+ else :
31
+ right -= 1
32
+
33
+ return result
34
+
35
+
36
+ # Time Complexity: O(n^2)
37
+ # - Sorting takes O(n log n).
38
+ # - The outer loop runs O(n) times, and the two-pointer approach inside runs O(n) for each iteration.
39
+ # - Combined, the overall time complexity is O(n^2).
40
+
41
+ # Space Complexity: O(k)
42
+ # - The result list uses O(k) space, where k is the number of unique triplets in the output.
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def climbStairs (self , n : int ) -> int :
3
+ # dp[i] represents the number of distinct ways to climb to the ith step.
4
+ # Base cases:
5
+ # - There is 1 way to reach step 0 (doing nothing).
6
+ # - There is 1 way to reach step 1 (a single step).
7
+ dp = [0 ] * (n + 1 )
8
+ dp [0 ], dp [1 ] = 1 , 1
9
+
10
+ for i in range (2 , n + 1 ):
11
+ dp [i ] = dp [i - 1 ] + dp [i - 2 ]
12
+
13
+ return dp [n ]
14
+
15
+ # Complexity
16
+ # - time: O(n)
17
+ # - space: O(n)
18
+
19
+ class Solution :
20
+ def climbStairs (self , n : int ) -> int :
21
+ prev , curr = 1 , 1
22
+
23
+ for _ in range (2 , n + 1 ):
24
+ prev , curr = curr , prev + curr
25
+
26
+ return curr
27
+
28
+ # Complexity
29
+ # - time: O(n)
30
+ # - space: O(1)
Original file line number Diff line number Diff line change
1
+ from typing import List , Optional
2
+
3
+ # Definition for a binary tree node.
4
+ class TreeNode :
5
+ def __init__ (self , val = 0 , left = None , right = None ):
6
+ self .val = val
7
+ self .left = left
8
+ self .right = right
9
+
10
+ class Solution :
11
+ def buildTree (self , preorder : List [int ], inorder : List [int ]) -> Optional [TreeNode ]:
12
+ inorder_map = {value : idx for idx , value in enumerate (inorder )}
13
+ self .preorder_idx = 0
14
+
15
+ def helper (left : int , right : int ) -> Optional [TreeNode ]:
16
+ if left > right :
17
+ return None
18
+
19
+ root_val = preorder [self .preorder_idx ]
20
+ self .preorder_idx += 1
21
+
22
+ root = TreeNode (root_val )
23
+ root .left = helper (left , inorder_map [root_val ] - 1 )
24
+ root .right = helper (inorder_map [root_val ] + 1 , right )
25
+
26
+ return root
27
+
28
+ return helper (0 , len (inorder ) - 1 )
29
+
30
+
31
+ # Time Complexity: O(n)
32
+ # - Each node is visited exactly once in preorder, and the dictionary lookup in inorder_map takes O(1) per node.
33
+
34
+ # Space Complexity: O(n)
35
+ # - The hash map (inorder_map) uses O(n) space.
36
+ # - The recursion stack uses up to O(h) space, where h is the height of the tree (O(n) in the worst case, O(log n) for a balanced tree).
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def numDecodings (self , s : str ) -> int :
3
+ if s [0 ] == "0" :
4
+ return 0
5
+
6
+ prev , curr = 1 , 1
7
+
8
+ for i in range (1 , len (s )):
9
+ temp = curr
10
+
11
+ if s [i ] == "0" :
12
+ if s [i - 1 ] in ("1" , "2" ):
13
+ curr = prev
14
+ else :
15
+ return 0
16
+ else :
17
+ two_num = int (s [i - 1 ] + s [i ])
18
+ is_two_num_decoded = 10 <= two_num <= 26
19
+ if is_two_num_decoded :
20
+ curr += prev
21
+
22
+ prev = temp
23
+
24
+ return curr
25
+
26
+
27
+ # Time Complexity: O(n)
28
+ # - The loop iterates through the string once, where n is the length of the string.
29
+
30
+ # Space Complexity: O(1)
31
+ # - Only two variables (prev and curr) are used, independent of the input size.
Original file line number Diff line number Diff line change
1
+ from collections import Counter
2
+
3
+ class Solution :
4
+ def isAnagram (self , s : str , t : str ) -> bool :
5
+ return Counter (s ) == Counter (t )
6
+
7
+ # Python์ collections.Counter๋ ์ ๋์ฝ๋ ๋ฌธ์๋ฅผ ์ง์ํฉ๋๋ค.
8
+ # ์๊ฐ๋ณต์ก๋: O(n)
9
+ # Counter๋ ํด์ ํ
์ด๋ธ ๊ธฐ๋ฐ์ ์ฐ์ฐ์ผ๋ก ๋์ํ๋ฉฐ, ๊ฐ ๋ฌธ์์ด์ ๋ฌธ์๋ฅผ ํ ๋ฒ์ฉ ์ํํฉ๋๋ค.
10
+ # ๊ณต๊ฐ๋ณต์ก๋: O(n)
11
+ # ๊ฐ ๋ฌธ์์ด์ ๋ํด Counter ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ฏ๋ก, ์ต์
์ ๊ฒฝ์ฐ ๊ฐ ๋ฌธ์๋ง๋ค ๋น๋๋ฅผ ์ ์ฅํ๋ ๋ฐ O(n) ํฌ๊ธฐ์ ํด์ํ
์ด๋ธ์ด ํ์ํฉ๋๋ค.
You canโt perform that action at this time.
0 commit comments