File tree Expand file tree Collapse file tree 5 files changed +182
-0
lines changed
product-of-array-except-self
validate-binary-search-tree Expand file tree Collapse file tree 5 files changed +182
-0
lines changed Original file line number Diff line number Diff line change 1+ """
2+ # Intuition
3+ ์ฒ์์๋ 1์ฃผ์ฐจ์ Two Sum ๋ฌธ์ ํ์ด๋ฅผ ์์ฉํ์ฌ, ๋ฐฐ์ด ์ํ + ํด์ ํ
์ด๋ธ ๋ง๋ค์ด ๊ฐ ์ฐพ๊ธฐ๋ฅผ ์๋ํ์ผ๋
4+ ์ ๋ ฌ + ํฌํฌ์ธํฐ ํ์ด๊ฐ ๋ ๋น ๋ฅด๋ฏ๋ก ๊ทธ๋ ๊ฒ ์ ์ถํ์ต๋๋ค.
5+
6+ # Approach
7+ nums ๋ฐฐ์ด์ ์ํํ๋ฉด์ -nums[i]๋ฅผ ํฉ์ผ๋ก ํ๋ ๋ ์๋ฅผ ๋๋จธ์ง ๋ฐฐ์ด ๋ถ๋ถ์์ ์ฐพ๋๋ค.
8+ ์ฐพ์ ๋๋ ์ ๋ ฌ & ํฌํฌ์ธํฐ๋ฅผ ํ์ฉํ์ฌ ์ค๋ณต์ ๊ฑด๋ ๋ฐ๋ ๋ฐฉ์์ผ๋ก ์๋๋ฅผ ๋์ธ๋ค.
9+
10+
11+ # Complexity
12+ - Time complexity: ์ ๋ ฌ + ํฌํฌ์ธํฐ ์ด์ค ๋ฐ๋ณต์ผ๋ก O(N^2)
13+
14+ - Space complexity: ์ ๋ ฌ ํ๋๋ฐ์ O(N) , answer ๋ฐฐ์ด ์์ฑํ๋๋ฐ์ O(M)
15+ """
16+
17+
18+ class Solution :
19+ def threeSum (self , nums : list [int ]) -> list [list [int ]]:
20+ nums .sort () # O(NlogN)
21+ n = len (nums )
22+ answer = []
23+
24+ for i in range (n - 2 ): # ์ด์ค ๋ฐ๋ณต๋ฌธ O(N^2)
25+ # ์ค๋ณต ์ ๊ฑฐ
26+ if i > 0 and nums [i ] == nums [i - 1 ]:
27+ continue
28+
29+ # nums[i]๊ฐ 0๋ณด๋ค ํฌ๋ฉด ๋ค๋ ๋ค ์์๋ผ ์ข
๋ฃ ๊ฐ๋ฅ
30+ if nums [i ] > 0 :
31+ break
32+
33+ left , right = i + 1 , n - 1
34+
35+ while left < right :
36+ total = nums [i ] + nums [left ] + nums [right ]
37+
38+ if total == 0 :
39+ answer .append ([nums [i ], nums [left ], nums [right ]])
40+ left += 1
41+ right -= 1
42+
43+ # left/right ์ค๋ณต ์ ๊ฑฐ
44+ while left < right and nums [left ] == nums [left - 1 ]:
45+ left += 1
46+ while left < right and nums [right ] == nums [right + 1 ]:
47+ right -= 1
48+
49+ elif total < 0 :
50+ left += 1
51+ else :
52+ right -= 1
53+
54+ return answer
Original file line number Diff line number Diff line change 1+ """
2+ # Intuition
3+ ๋ง์ง๋ง ๊ณ๋จ์ ๋์ฐฉํ๋ ๊ฒฝ์ฐ์ ์๋, 2์คํ
์ ์ ๊ฒฝ์ฐ์ ์์ 1์คํ
์ ์ ๊ฒฝ์ฐ์ ์๋ฅผ ํฉํ ๊ฐ์
๋๋ค.
4+
5+ # Approach
6+ dp[n] = dp[n-1] + dp[n-2]
7+
8+ # Complexity
9+ - Time complexity: O(n)
10+
11+ - Space complexity: O(1)
12+ """
13+
14+
15+ class Solution :
16+ def climbStairs (self , n : int ) -> int :
17+ b = 1 # n์ด 1์ผ๋
18+ if n == 1 :
19+ return b
20+ a = 2 # n์ด 2์ผ๋
21+ if n == 2 :
22+ return a
23+
24+ for n in range (3 , n + 1 ): # O(n)
25+ a , b = a + b , a
26+ return a
Original file line number Diff line number Diff line change 1+ """
2+ # Approach
3+ ๋ชจ๋ ์์๋ผ๋ฆฌ ๊ณฑํ ๋ค์์ nums ๋ฐฐ์ด์ ์ํํ๋ฉฐ ๊ทธ ๊ฐ์ ํด๋น ์์๋ก ๋๋๋ค.
4+ ์ด๋ 0์ ๊ฐ์์ 0์ ์ ์ธํ ๊ณฑ์ ๊ฐ๋ ๊ฐ์ด ๊ตฌํ๋ค.
5+ 0์ด 2๊ฐ ์ด์์ด๋ฉด ์ ๋ต์ ๋ชจ๋ 0์ด๋ฉฐ,
6+ 0์ด 1๊ฐ๋ฉด 0์ ์ ์ธํ ๊ณฑ์ ๊ฐ์ ์ฌ์ฉํ๋ค.
7+
8+ # Complexity
9+ - Time complexity: O(n)
10+
11+ - Space complexity: O(n)
12+ """
13+
14+
15+ # Code
16+ class Solution :
17+ def productExceptSelf (self , nums : List [int ]) -> List [int ]:
18+ product = 1
19+ non_zero_product = 1
20+ zero_count = 0
21+ for num in nums : # O(n) Time complexity
22+ if num == 0 :
23+ zero_count += 1
24+ non_zero_product *= 1
25+ else :
26+ non_zero_product *= num
27+ product *= num
28+
29+ if zero_count > 1 :
30+ return [0 ] * len (nums ) # O(n) space complexity
31+
32+ answer = [] # O(n) space complexity
33+ for i in range (len (nums )): # O(n) Time complexity
34+ if nums [i ] == 0 :
35+ answer .append (non_zero_product )
36+ else :
37+ answer .append (int (product / nums [i ]))
38+ return answer
Original file line number Diff line number Diff line change 1+ """
2+ # Approach
3+ ๋ฌธ์์ด s๋ฅผ ๊ตฌ์ฑํ๋ ๋ฌธ์์ ๊ฐ์๋ฅผ ์ธ๋ ๋์
๋๋ฆฌ word_dict๋ฅผ ๋ง๋ค๊ณ ,
4+ ๋ฌธ์์ด t๋ฅผ ์ํํ๋ฉฐ word_dict์ ํค๊ฐ ์๋์ง ํ์ธํฉ๋๋ค.
5+ ํค๊ฐ ์์ผ๋ฉด: ์๋๊ทธ๋จ ๋ถ๊ฐ๋ฅ.
6+ ํค๊ฐ ์์ผ๋ฉด: ๊ฐ์์ -1ํ๊ณ , ์์์ธ์ง ํ์ธํ์ฌ ์๋๊ทธ๋จ ์ฌ๋ถ๋ฅผ ํ๋ณํฉ๋๋ค.
7+
8+ # Complexity
9+ - Time complexity: s์ ๊ธธ์ด๊ฐ N์ด๊ณ , t์ ๊ธธ์ด๊ฐ M์ผ ๋ O(N+M)
10+
11+ - Space complexity: O(N+M)
12+ """
13+
14+ from collections import defaultdict
15+
16+
17+ class Solution :
18+ def isAnagram (self , s : str , t : str ) -> bool :
19+ if len (s ) != len (t ):
20+ return False
21+
22+ word_dict = defaultdict (int )
23+ for ch in s : # O(N)
24+ word_dict [ch ] += 1
25+
26+ for ch in t : # O(M)
27+ if ch not in word_dict :
28+ return False
29+ word_dict [ch ] -= 1
30+ if word_dict [ch ] < 0 :
31+ return False
32+
33+ return True
Original file line number Diff line number Diff line change 1+ """
2+ # https://leetcode.com/problems/validate-binary-search-tree/description/
3+ # Intuition
4+ BST๋ฅผ ์ค์ ์ํํ๋ฉด ์ค๋ฆ์ฐจ์์ผ๋ก ๊ฐ์ด ์ ๋ ฌ๋๋ค๋ ์ ์ ์ฐฉ์ํ์ต๋๋ค.
5+
6+ # Complexity
7+ - Time complexity: ๋
ธ๋์ ๊ฐ์๋ฅผ N์ด๋ผ๊ณ ํ ๋, O(N)
8+
9+ - Space complexity: ์ฌ๊ท๋ก ํธ๋ฆฌ๋ฅผ ์ํํ๋ฉด์ ํธ์ถ ์คํ์ด ์์ด๋๋ฐ
10+ ํธ๋ฆฌ์ ๋์ด๋ฅผ H๋ผ๊ณ ํ ๋, ํธ์ถ ์คํ์ H ๋งํผ ์์
๋๋ค. => ๊ณต๊ฐ ๋ณต์ก๋ O(H)
11+ """
12+
13+
14+ class Solution :
15+ def isValidBST (self , root : Optional [TreeNode ]) -> bool :
16+ prev = [None ]
17+
18+ def inorder (node ):
19+ if not node :
20+ return True
21+
22+ if not inorder (node .left ):
23+ return False
24+
25+ if prev [0 ] is not None and prev [0 ] >= node .val :
26+ return False
27+ prev [0 ] = node .val
28+
29+ return inorder (node .right )
30+
31+ return inorder (root )
You canโt perform that action at this time.
0 commit comments