diff --git a/contains-duplicate/hyedall.py b/contains-duplicate/hyedall.py new file mode 100644 index 0000000000..ccd58bdea5 --- /dev/null +++ b/contains-duplicate/hyedall.py @@ -0,0 +1,16 @@ +""" +https://leetcode.com/problems/contains-duplicate/ + +시간 복잡도: O(n) +- set 변환 O(n) + +공간 복잡도: O(n) +- set에 저장 +""" + +class Solution: + def containsDuplicate(self, nums: list[int]) -> bool: + if len(set(nums)) == len(nums): + return False + else: + return True diff --git a/house-robber/hyedall.py b/house-robber/hyedall.py new file mode 100644 index 0000000000..5ef95be3e6 --- /dev/null +++ b/house-robber/hyedall.py @@ -0,0 +1,26 @@ +""" +https://leetcode.com/problems/house-robber/description/ + +시간 복잡도: O(n) +- 모든 배열 한 번씩 순회 O(n) +- 선택/미선택 값 비교 O(1) + +공간 복잡도: O(n) +- 각 인덱스까지의 최대 금액 dp 배열에 저장 + +점화식: +- dp[i] = max(nums[i] + dp[i - 2], dp[i - 1]) +- nums[i] + dp[i - 2]: 현재 집을 터는 경우 +- dp[i - 1]: 현재 집을 털지 않는 경우 +""" + +class Solution: + def rob(self, nums: list[int]) -> int: + n = len(nums) + dp = [0] * n + dp[0] = nums[0] + + for i in range(1, n): + dp[i] = max(nums[i] + dp[i - 2], dp[i - 1]) + + return dp[-1] diff --git a/longest-consecutive-sequence/hyedall.py b/longest-consecutive-sequence/hyedall.py new file mode 100644 index 0000000000..82d0ccd9ed --- /dev/null +++ b/longest-consecutive-sequence/hyedall.py @@ -0,0 +1,37 @@ +""" +https://leetcode.com/problems/longest-consecutive-sequence/description/ + +시간 복잡도: O(n) +- set 생성 O(n) +- 각 고유 숫자의 시작점 여부 확인 O(n) +- 연속 수열 탐색 O(n) + +공간 복잡도: O(n) +- 모든 숫자를 set에 저장 + +풀이 : +- 시간 복잡도 O(n)를 만족시키기 위해 set으로 연속되는 다음 숫자의 존재 여부만 확인 +- 이전 숫자가 있는지 확인 후 시작점에서만 순회하여 중복 탐색 방지 +""" + +class Solution: + def longestConsecutive(self, nums: List[int]) -> int: + num_set = set(nums) + answer = 0 + + for num in num_set: + # 시작점인지 확인 + if num - 1 not in num_set: + current = num + length = 1 + + # 연속 숫자 탐색 + while current + 1 in num_set: + current += 1 + length += 1 + + answer = max(answer, length) + + return answer + + diff --git a/top-k-frequent-elements/hyedall.py b/top-k-frequent-elements/hyedall.py new file mode 100644 index 0000000000..ad91e520c7 --- /dev/null +++ b/top-k-frequent-elements/hyedall.py @@ -0,0 +1,21 @@ +""" +https://leetcode.com/problems/top-k-frequent-elements/description/ + +시간 복잡도: O(n log n) +- 모든 숫자의 빈도 계산 O(n) +- 빈도가 높은 k개 선택 O(n log k) +- 결과 리스트 생성 O(k) + +공간 복잡도: O(n) +- 고유 숫자별 빈도 저장 O(n) +- 정답 k개 저장 O(k) +""" + +from collections import Counter + +class Solution: + def topKFrequent(self, nums: list[int], k: int) -> list[int]: + counts = Counter(nums) + answer = [n for n, f in counts.most_common(k)] + + return answer diff --git a/two-sum/hyedall.py b/two-sum/hyedall.py new file mode 100644 index 0000000000..3280cb9441 --- /dev/null +++ b/two-sum/hyedall.py @@ -0,0 +1,21 @@ +""" +https://leetcode.com/problems/two-sum/ + +시간 복잡도: O(n²) +- 첫 번째 반복문에서 최대 n번 순회 +- 각 숫자마다 뒤의 숫자들을 다시 순회 +- 확인하는 숫자 쌍의 개수는 약 n(n - 1) / 2개 + +공간 복잡도: O(1) +- 입력 크기와 관계없이 i, j 등의 고정된 변수만 사용 + +""" + +class Solution: + def twoSum(self, nums: list[int], target: int) -> list[int]: + n = len(nums) + + for i in range(n): + for j in range(i+1, n): + if nums[i] + nums[j] == target: + return [i, j]