File tree Expand file tree Collapse file tree 4 files changed +76
-0
lines changed Expand file tree Collapse file tree 4 files changed +76
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def threeSum (self , nums : List [int ]) -> List [List [int ]]:
3
+ nums .sort ()
4
+ result = []
5
+ for i in range (len (nums )):
6
+ if i > 0 and nums [i ] == nums [i - 1 ]:
7
+ continue
8
+
9
+ left = i + 1
10
+ right = len (nums ) - 1
11
+ while left < right :
12
+ total = nums [i ] + nums [left ] + nums [right ]
13
+ if total < 0 :
14
+ left += 1
15
+ elif total > 0 :
16
+ right -= 1
17
+ else :
18
+ result .append ([nums [i ], nums [left ], nums [right ]])
19
+ left += 1
20
+ right -= 1
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
+ return result
Original file line number Diff line number Diff line change
1
+ """
2
+ ๐ Problem: LeetCode 70 - Climbing Stairs
3
+ ๐
Date: 2025-04-07
4
+
5
+ ๐ Approach:
6
+ - Bottom-up DP using an array
7
+ - dp[i] = dp[i-1] + dp[i-2]
8
+
9
+ โฑ๏ธ Time Complexity: O(n)
10
+ ๐พ Space Complexity: O(n)
11
+
12
+ ๐ Notes:
13
+ - Base case: dp[0] = 1, dp[1] = 1
14
+ - dp[i]: i๋ฒ์งธ ๊ณ๋จ์ผ๋ก ๋๋ฌํ๊ธฐ ์ํ ๋ชจ๋ ๊ฒฝ์ฐ์ ์๋ฅผ ๊ฐ์ง
15
+ - n <= 2์ ๊ฒฝ์ฐ๋ f(1) + f(0)์ด ํฉํด์ง ๊ฒฝ์ฐ์ด๊ธฐ ๋๋ฌธ์ n์ ๋ฐํ
16
+ """
17
+ class Solution :
18
+ def climbStairs (self , n : int ) -> int :
19
+ if n <= 2 :
20
+ return n
21
+ dp = [0 ] * (n + 1 ) # n ๋ฒ์งธ์ ๊ณ๋จ์ ์ค๋ฅด๋ ๋ฐฉ๋ฒ์ ์ฐพ๊ธฐ ์ํด dp๋ฐฐ์ด ์์ฑ
22
+ dp [0 ] = 1
23
+ dp [1 ] = 1
24
+ # n๋ฒ์งธ์ ๊ณ๋จ์ ์ค๋ฅด๊ธฐ ์ํด์๋
25
+ # n-1, n-2๋ฒ์งธ์ ๊ณ๋จ์์ ์ฌ์์๋ ๊ฒฝ์ฐ์ ์๋ค์ ํฉ์ด n๋ฒ์งธ ๊ณ๋จ์ ์ค๋ฅด๊ธฐ ์ํ ๋ชจ๋ ๊ฒฝ์ฐ์ ์
26
+ for i in range (2 , n + 1 ):
27
+ dp [i ] = dp [i - 1 ] + dp [i - 2 ]
28
+ return dp [n ]
Original file line number Diff line number Diff line change
1
+ # Solution 1: using Counter, heapq
2
+ from collections import Counter
3
+ import heapq
4
+
5
+ class Solution :
6
+ def topKFrequent (self , nums : List [int ], k : int ) -> List [int ]:
7
+ count_dict = Counter (nums )
8
+ return heapq .nlargest (k , count_dict .keys (), key = count_dict .get )
9
+
10
+ # Solution 2: create dict, use sorted function
11
+ # class Solution:
12
+ # def topKFrequent(self, nums: List[int], k: int) -> List[int]:
13
+ # freq_dict = {}
14
+ # for num in nums:
15
+ # if num in freq_dict:
16
+ # freq_dict[num] += 1
17
+ # else:
18
+ # freq_dict[num] = 1
19
+ # sorted_list = sorted(freq_dict.keys(), key = lambda x: freq_dict[x], reverse=True)
20
+ # return sorted_list[:k]
Original file line number Diff line number Diff line change
1
+ class Solution :
2
+ def isAnagram (self , s : str , t : str ) -> bool :
3
+ return Counter (s ) == Counter (t )
You canโt perform that action at this time.
0 commit comments