File tree Expand file tree Collapse file tree 5 files changed +100
-0
lines changed
longest-common-subsequence Expand file tree Collapse file tree 5 files changed +100
-0
lines changed Original file line number Diff line number Diff line change 1+ """
2+ # Time Complexity: O(N)
3+ - N๋ฒ ์ํ
4+ # Space Compelexity: O(N)
5+ - ์ต์
์ ๊ฒฝ์ฐ (์ค๋ณต๋ ๊ฐ์ด ์์ ๊ฒฝ์ฐ) N๊ฐ ์ ์ฅ
6+ """
7+ class Solution :
8+ def containsDuplicate (self , nums : List [int ]) -> bool :
9+ num_dict = {}
10+ for num in nums :
11+ if num not in num_dict :
12+ num_dict [num ] = True
13+ else :
14+ return True
15+ return False
Original file line number Diff line number Diff line change 1+ """
2+ # Time Complexity: O(N)
3+ - N๊ฐ์ ๊ฐ์๋ฅผ ๊ฐ์ง๋ dp ๋ฆฌ์คํธ๋ฅผ ๋ง๋ค๊ณ , ์ด๋ฅผ ์ํ
4+ # Space Compelexity: O(N)
5+ - N๊ฐ์ dp ๋ฆฌ์คํธ ์ ์ฅ
6+ """
7+ class Solution :
8+ def rob (self , nums : List [int ]) -> int :
9+ if len (nums ) == 1 :
10+ return nums [0 ]
11+
12+ dp = [0 for _ in range (len (nums ))]
13+ dp [0 ] = nums [0 ]
14+ dp [1 ] = max (nums [0 ], nums [1 ])
15+
16+ for i in range (len (nums ) - 2 ):
17+ dp [i + 2 ] = max (dp [i ] + nums [i + 2 ], dp [i + 1 ])
18+
19+ return max (dp [- 2 ], dp [- 1 ])
Original file line number Diff line number Diff line change 1+ """
2+ # Time Complexity: O(N)
3+ - lce_dict ์์ฑ: N
4+ - N๊ฐ์ key์ ๋ํ์ฌ ์ํํ๋ฉด์ ๊ฐ ํ์ธ: N
5+ # Space Compelexity: O(k)
6+ - ์ค๋ณต๋์ง ์์ key k๊ฐ ์ ์ฅ
7+ """
8+ class Solution :
9+ def longestConsecutive (self , nums : List [int ]) -> int :
10+ if not nums :
11+ return 0
12+
13+ lce_dict = {}
14+ for num in nums :
15+ lce_dict [num ] = True
16+
17+ answer = 0
18+ for num in nums :
19+ cur_lce = 1
20+ if lce_dict .pop (num , None ) is None :
21+ continue
22+
23+ down_num = num - 1
24+ while down_num in lce_dict :
25+ cur_lce += 1
26+ lce_dict .pop (down_num )
27+ down_num -= 1
28+
29+ up_num = num + 1
30+ while up_num in lce_dict :
31+ cur_lce += 1
32+ lce_dict .pop (up_num )
33+ up_num += 1
34+
35+ answer = answer if answer > cur_lce else cur_lce
36+
37+ return answer
Original file line number Diff line number Diff line change 1+ """
2+ # Time Complexity: O(N log N)
3+ - Counter ์์ฑ: N๋ฒ ์ํ
4+ - most common ์ฐ์ฐ: N log N
5+ # Space Compelexity: O(N)
6+ - ์ต์
์ ๊ฒฝ์ฐ (์ค๋ณต๋ ๊ฐ์ด ์์ ๊ฒฝ์ฐ) N๊ฐ ์ ์ฅ
7+ """
8+ from collections import Counter
9+
10+
11+ class Solution :
12+ def topKFrequent (self , nums : List [int ], k : int ) -> List [int ]:
13+ count_dict = Counter (nums )
14+ top_k_list = count_dict .most_common (k )
15+ answer = [key for key , value in top_k_list ]
16+ return answer
Original file line number Diff line number Diff line change 1+ """
2+ # Time Complexity: O(N)
3+ - N๊ฐ์ char๋ฅผ ๊ฐ๊ฐ ํ๋ฒ์ฉ ์ํ
4+ # Space Compelexity: O(N)
5+ - ์ต์
์ ๊ฒฝ์ฐ (๊ณต๋ฐฑ์ด ์์ ๊ฒฝ์ฐ) N๊ฐ์ char ์ ์ฅ
6+ """
7+ class Solution :
8+ def isPalindrome (self , s : str ) -> bool :
9+ string = [char .lower () for char in s if char .isalnum ()]
10+ for i in range (len (string ) // 2 ):
11+ if string [i ] != string [- i - 1 ]:
12+ return False
13+ return True
You canโt perform that action at this time.
0 commit comments