File tree Expand file tree Collapse file tree
longest-consecutive-sequence Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ """
2+ https://leetcode.com/problems/contains-duplicate/
3+
4+ μκ° λ³΅μ‘λ: O(n)
5+ - set λ³ν O(n)
6+
7+ κ³΅κ° λ³΅μ‘λ: O(n)
8+ - setμ μ μ₯
9+ """
10+
11+ class Solution :
12+ def containsDuplicate (self , nums : list [int ]) -> bool :
13+ if len (set (nums )) == len (nums ):
14+ return False
15+ else :
16+ return True
Original file line number Diff line number Diff line change 1+ """
2+ https://leetcode.com/problems/house-robber/description/
3+
4+ μκ° λ³΅μ‘λ: O(n)
5+ - λͺ¨λ λ°°μ΄ ν λ²μ© μν O(n)
6+ - μ ν/λ―Έμ ν κ° λΉκ΅ O(1)
7+
8+ κ³΅κ° λ³΅μ‘λ: O(n)
9+ - κ° μΈλ±μ€κΉμ§μ μ΅λ κΈμ‘ dp λ°°μ΄μ μ μ₯
10+
11+ μ νμ:
12+ - dp[i] = max(nums[i] + dp[i - 2], dp[i - 1])
13+ - nums[i] + dp[i - 2]: νμ¬ μ§μ ν°λ κ²½μ°
14+ - dp[i - 1]: νμ¬ μ§μ νΈμ§ μλ κ²½μ°
15+ """
16+
17+ class Solution :
18+ def rob (self , nums : list [int ]) -> int :
19+ n = len (nums )
20+ dp = [0 ] * n
21+ dp [0 ] = nums [0 ]
22+
23+ for i in range (1 , n ):
24+ dp [i ] = max (nums [i ] + dp [i - 2 ], dp [i - 1 ])
25+
26+ return dp [- 1 ]
Original file line number Diff line number Diff line change 1+ """
2+ https://leetcode.com/problems/longest-consecutive-sequence/description/
3+
4+ μκ° λ³΅μ‘λ: O(n)
5+ - set μμ± O(n)
6+ - κ° κ³ μ μ«μμ μμμ μ¬λΆ νμΈ O(n)
7+ - μ°μ μμ΄ νμ O(n)
8+
9+ κ³΅κ° λ³΅μ‘λ: O(n)
10+ - λͺ¨λ μ«μλ₯Ό setμ μ μ₯
11+
12+ νμ΄ :
13+ - μκ° λ³΅μ‘λ O(n)λ₯Ό λ§μ‘±μν€κΈ° μν΄ setμΌλ‘ μ°μλλ λ€μ μ«μμ μ‘΄μ¬ μ¬λΆλ§ νμΈ
14+ - μ΄μ μ«μκ° μλμ§ νμΈ ν μμμ μμλ§ μννμ¬ μ€λ³΅ νμ λ°©μ§
15+ """
16+
17+ class Solution :
18+ def longestConsecutive (self , nums : List [int ]) -> int :
19+ num_set = set (nums )
20+ answer = 0
21+
22+ for num in num_set :
23+ # μμμ μΈμ§ νμΈ
24+ if num - 1 not in num_set :
25+ current = num
26+ length = 1
27+
28+ # μ°μ μ«μ νμ
29+ while current + 1 in num_set :
30+ current += 1
31+ length += 1
32+
33+ answer = max (answer , length )
34+
35+ return answer
36+
37+
Original file line number Diff line number Diff line change 1+ """
2+ https://leetcode.com/problems/top-k-frequent-elements/description/
3+
4+ μκ° λ³΅μ‘λ: O(n log n)
5+ - λͺ¨λ μ«μμ λΉλ κ³μ° O(n)
6+ - λΉλκ° λμ kκ° μ ν O(n log k)
7+ - κ²°κ³Ό 리μ€νΈ μμ± O(k)
8+
9+ κ³΅κ° λ³΅μ‘λ: O(n)
10+ - κ³ μ μ«μλ³ λΉλ μ μ₯ O(n)
11+ - μ λ΅ kκ° μ μ₯ O(k)
12+ """
13+
14+ from collections import Counter
15+
16+ class Solution :
17+ def topKFrequent (self , nums : list [int ], k : int ) -> list [int ]:
18+ counts = Counter (nums )
19+ answer = [n for n , f in counts .most_common (k )]
20+
21+ return answer
Original file line number Diff line number Diff line change 1+ """
2+ https://leetcode.com/problems/two-sum/
3+
4+ μκ° λ³΅μ‘λ: O(nΒ²)
5+ - 첫 λ²μ§Έ λ°λ³΅λ¬Έμμ μ΅λ nλ² μν
6+ - κ° μ«μλ§λ€ λ€μ μ«μλ€μ λ€μ μν
7+ - νμΈνλ μ«μ μμ κ°μλ μ½ n(n - 1) / 2κ°
8+
9+ κ³΅κ° λ³΅μ‘λ: O(1)
10+ - μ
λ ₯ ν¬κΈ°μ κ΄κ³μμ΄ i, j λ±μ κ³ μ λ λ³μλ§ μ¬μ©
11+
12+ """
13+
14+ class Solution :
15+ def twoSum (self , nums : list [int ], target : int ) -> list [int ]:
16+ n = len (nums )
17+
18+ for i in range (n ):
19+ for j in range (i + 1 , n ):
20+ if nums [i ] + nums [j ] == target :
21+ return [i , j ]
You canβt perform that action at this time.
0 commit comments