File tree Expand file tree Collapse file tree 5 files changed +117
-0
lines changed
longest-consecutive-sequence Expand file tree Collapse file tree 5 files changed +117
-0
lines changed Original file line number Diff line number Diff line change
1
+ """
2
+ 시간 복잡도 O(n)
3
+ 공간 복잡도 O(n)
4
+
5
+ 코드 가독성 향상 코드
6
+ class Solution:
7
+ def containsDuplicate(self, nums: List[int]) -> bool:
8
+ return len(nums) != len(set(nums))
9
+ """
10
+ class Solution :
11
+ def containsDuplicate (self , nums : List [int ]) -> bool :
12
+ check = set ([])
13
+ for i in nums :
14
+ if i not in check :
15
+ check .add (i )
16
+ else :
17
+ return True
18
+ return False
Original file line number Diff line number Diff line change
1
+ """
2
+ 시간 복잡도 O(N)
3
+ 공간 복잡도 O(N)
4
+
5
+
6
+ """
7
+
8
+ class Solution :
9
+ def rob (self , nums : List [int ]) -> int :
10
+ memo = {}
11
+
12
+ def backtracking (k : int ) -> int :
13
+ if k >= len (nums ):
14
+ return 0
15
+ if k in memo :
16
+ return memo [k ]
17
+
18
+ rob = nums [k ] + backtracking (k + 2 )
19
+ skip = backtracking (k + 1 )
20
+
21
+ memo [k ] = max (rob , skip )
22
+ return memo [k ]
23
+
24
+ return backtracking (0 )
Original file line number Diff line number Diff line change
1
+ """
2
+ 시간 복잡도 O(N)
3
+ 공간 복잡도 O(N)
4
+ """
5
+ class Solution :
6
+ def longestConsecutive (self , nums : List [int ]) -> int :
7
+ nums_set = set (nums )
8
+ result = 0
9
+
10
+ for i in nums_set :
11
+ if i - 1 not in nums_set :
12
+ length = 1
13
+ while i + length in nums_set :
14
+ length += 1
15
+ result = max (result , length )
16
+
17
+ return result
Original file line number Diff line number Diff line change
1
+ """
2
+ 시간 복잡도 O(N)
3
+ 공간 복잡도 O(N)
4
+
5
+ Priority Queue 를 이용한 풀이
6
+ 시간 복잡도 O(Nlog(N))
7
+ 공간 복잡도 O(N)
8
+ from queue import PriorityQueue
9
+ from collections import Counter
10
+
11
+ class Solution:
12
+ def topKFrequent(self, nums: List[int], k: int) -> List[int]:
13
+ counter = Counter(nums)
14
+ que = PriorityQueue()
15
+
16
+ for num, freq in counter.items():
17
+ que.put((-freq, num))
18
+
19
+ res = []
20
+
21
+ for _ in range(k):
22
+ res.append(que.get()[1])
23
+
24
+ return res
25
+ """
26
+ from collections import Counter
27
+
28
+ class Solution :
29
+ def topKFrequent (self , nums : List [int ], k : int ) -> List [int ]:
30
+ freq_map = Counter (nums )
31
+
32
+ bucket = [[] for _ in range (len (nums ) + 1 )]
33
+
34
+ for num , freq in freq_map .items ():
35
+ bucket [freq ].append (num )
36
+
37
+ result = []
38
+
39
+ for i in range (len (bucket ) - 1 , - 1 , - 1 ):
40
+ if bucket [i ]:
41
+ for num in bucket [i ]:
42
+ result .append (num )
43
+ if len (result ) == k :
44
+ return result
Original file line number Diff line number Diff line change
1
+ """
2
+ 시간 복잡도 O(n)
3
+ 공간 복잡도 O(n)
4
+ """
5
+ class Solution :
6
+ def twoSum (self , nums : List [int ], target : int ) -> List [int ]:
7
+ nums_map = {}
8
+
9
+ for i , v in enumerate (nums ):
10
+ x = target - v
11
+ if x in nums_map :
12
+ j = nums_map [x ]
13
+ return [j , i ]
14
+ nums_map [v ] = i
You can’t perform that action at this time.
0 commit comments