File tree Expand file tree Collapse file tree 5 files changed +81
-0
lines changed
longest-consecutive-sequence Expand file tree Collapse file tree 5 files changed +81
-0
lines changed Original file line number Diff line number Diff line change
1
+ # Big-O ์์ : O(n)
2
+ class Solution :
3
+ def containsDuplicate (self , nums : List [int ]) -> bool :
4
+ num_dict = {}
5
+ for num in nums :
6
+ if num in num_dict :
7
+ return True
8
+ else :
9
+ num_dict [num ] = 1
10
+ return False
11
+
Original file line number Diff line number Diff line change
1
+ # Big-O ์์ : O(n)
2
+ class Solution :
3
+ def rob (self , nums : List [int ]) -> int :
4
+ a = [0 ] * len (nums )
5
+
6
+ if len (nums ) == 1 :
7
+ return nums [0 ]
8
+ elif len (nums ) == 2 :
9
+ return max (nums [0 ], nums [1 ])
10
+
11
+ a [0 ] = nums [0 ]
12
+ a [1 ] = nums [1 ]
13
+ a [2 ] = max (a [0 ] + nums [2 ], a [1 ])
14
+
15
+ for i in range (3 , len (nums )):
16
+ a [i ] = max (a [i - 3 ], a [i - 2 ]) + nums [i ]
17
+
18
+ return max (a )
19
+
Original file line number Diff line number Diff line change
1
+ # Big-O ์์ : O(nlog(n))
2
+ class Solution :
3
+ def longestConsecutive (self , nums : List [int ]) -> int :
4
+ nums = sorted (list (set (nums )))
5
+ if len (nums ) == 0 :
6
+ return 0
7
+ elif len (nums ) == 1 :
8
+ return 1
9
+ cur_long = 1
10
+ longest = 1
11
+ for i , num in enumerate (nums ):
12
+ if i == 0 :
13
+ continue
14
+ else :
15
+ if nums [i - 1 ] + 1 == nums [i ]:
16
+ cur_long += 1
17
+ if longest < cur_long :
18
+ longest = cur_long
19
+ else :
20
+ cur_long = 1
21
+ return longest
22
+
Original file line number Diff line number Diff line change
1
+ # Big-O ์์ : O(nlog(n))
2
+ import heapq
3
+ class Solution :
4
+ def topKFrequent (self , nums : List [int ], k : int ) -> List [int ]:
5
+
6
+ num_dict = {}
7
+ for num in nums :
8
+ num_dict [num ] = num_dict .get (num , 0 ) + 1
9
+ heap = []
10
+ for k_ , v in num_dict .items ():
11
+ heapq .heappush (heap , [- v , k_ ])
12
+ ans = []
13
+ for i in range (k ):
14
+ ans .append (heapq .heappop (heap )[1 ])
15
+ return ans
16
+
Original file line number Diff line number Diff line change
1
+ # Big-O ์์ : O(n)
2
+ class Solution :
3
+ def isPalindrome (self , s : str ) -> bool :
4
+ s = "" .join (s .lower ().split (" " ))
5
+ new_s = ""
6
+ for item in s :
7
+ if (ord ("a" ) <= ord (item ) <= ord ("z" )) or (ord ("0" ) <= ord (item ) <= ord ("9" )):
8
+ new_s += item
9
+ output = True
10
+ new_s_2 = new_s [::- 1 ]
11
+ return new_s_2 == new_s
12
+ return output
13
+
You canโt perform that action at this time.
0 commit comments