From e890414c9b4bfd92caad5add935513fb39ad2130 Mon Sep 17 00:00:00 2001 From: dohee Date: Tue, 3 Mar 2026 10:36:37 -0500 Subject: [PATCH 1/6] 217. Contains Duplicate Solution --- contains-duplicate/doh6077.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/contains-duplicate/doh6077.py b/contains-duplicate/doh6077.py index bc14e47f24..f755a81036 100644 --- a/contains-duplicate/doh6077.py +++ b/contains-duplicate/doh6077.py @@ -1,10 +1,8 @@ +# https://leetcode.com/problems/contains-duplicate/description/ -# set에 저장하면서 중복 여부 확인하기 class Solution: - def containsDuplicate(self, nums: list[int]) -> bool: - hashset = set() - for i in nums: - if i in hashset: - return True - hashset.add(i) - return False + def containsDuplicate(self, nums: List[int]) -> bool: + # Create a set to store unique numbers from nums + nums_set = set(nums) + return len(nums_set) != len(nums) + \ No newline at end of file From 03641f93aae037e5a23398cdd76df82bfc54954e Mon Sep 17 00:00:00 2001 From: dohee Date: Tue, 3 Mar 2026 10:43:08 -0500 Subject: [PATCH 2/6] =?UTF-8?q?=EC=A4=84=EB=B0=94=EA=BF=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- contains-duplicate/doh6077.py | 1 - 1 file changed, 1 deletion(-) diff --git a/contains-duplicate/doh6077.py b/contains-duplicate/doh6077.py index f755a81036..11a03c3ca6 100644 --- a/contains-duplicate/doh6077.py +++ b/contains-duplicate/doh6077.py @@ -5,4 +5,3 @@ def containsDuplicate(self, nums: List[int]) -> bool: # Create a set to store unique numbers from nums nums_set = set(nums) return len(nums_set) != len(nums) - \ No newline at end of file From 7755c5d899e67dab9b29379e90dd86b486ddf2b6 Mon Sep 17 00:00:00 2001 From: dohee Date: Tue, 3 Mar 2026 11:20:36 -0500 Subject: [PATCH 3/6] Two Sum solution --- two-sum/doh6077.py | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/two-sum/doh6077.py b/two-sum/doh6077.py index 2054ef0def..d8c8a3683a 100644 --- a/two-sum/doh6077.py +++ b/two-sum/doh6077.py @@ -1,8 +1,24 @@ +# 6기 +# class Solution: +# def twoSum(self, nums: list[int], target: int) -> list[int]: +# prevMap = {} # val : index +# for i, n in enumerate(nums): +# diff = target - n +# if diff in prevMap: +# return [prevMap[diff], i] +# prevMap[n] = i + +# 7기 +# https://leetcode.com/problems/two-sum/description/ class Solution: - def twoSum(self, nums: list[int], target: int) -> list[int]: - prevMap = {} # val : index - for i, n in enumerate(nums): - diff = target - n - if diff in prevMap: - return [prevMap[diff], i] - prevMap[n] = i + def twoSum(self, nums: List[int], target: int) -> List[int]: + # use Hash map to save num and index + nums_hm = {} + + for i, num in enumerate(nums): + find_val = target - num + + if find_val in nums_hm: + return [nums_hm[find_val], i] + + nums_hm[num] = i From f4cafbcf09e2c22bd9548c36bf325c8bd2fbf8fc Mon Sep 17 00:00:00 2001 From: dohee Date: Wed, 4 Mar 2026 10:48:08 -0500 Subject: [PATCH 4/6] Top K Frequent Elements solution --- top-k-frequent-elements/doh6077.py | 40 ++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/top-k-frequent-elements/doh6077.py b/top-k-frequent-elements/doh6077.py index d5d1d11a6a..772861cf66 100644 --- a/top-k-frequent-elements/doh6077.py +++ b/top-k-frequent-elements/doh6077.py @@ -1,12 +1,36 @@ + +# 6기 +# class Solution: +# # dictionary use +# def topKFrequent(self, nums: List[int], k: int) -> List[int]: +# result = {} # key: 원소, value: 등장 횟수 +# for n in nums: +# if n in result: +# result[n] = result[n] + 1 +# else: +# result[n] = 1 + +# # 가장 자주 등장한 원소 k개 반환 +# return sorted(result.keys(), key=lambda x: result[x], reverse=True)[:k] + +# 7기 class Solution: - # dictionary use def topKFrequent(self, nums: List[int], k: int) -> List[int]: - result = {} # key: 원소, value: 등장 횟수 - for n in nums: - if n in result: - result[n] = result[n] + 1 + # 1. Count frequency of each number + freq = {} + + for num in nums: + if num not in freq: + freq[num] = 1 else: - result[n] = 1 + freq[num] += 1 + + # 2. Sort by frequency in descending order + sorted_items = sorted(freq.items(), key=lambda item: item[1], reverse=True) + + # 3. Take the first k elements + result = [] + for i in range(k): + result.append(sorted_items[i][0]) - # 가장 자주 등장한 원소 k개 반환 - return sorted(result.keys(), key=lambda x: result[x], reverse=True)[:k] + return result From 3e895314d562f674b7b34738812df5f2a6119d8b Mon Sep 17 00:00:00 2001 From: dohee Date: Fri, 6 Mar 2026 16:02:39 -0500 Subject: [PATCH 5/6] comment out the previous solution --- longest-consecutive-sequence/doh6077.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/longest-consecutive-sequence/doh6077.py b/longest-consecutive-sequence/doh6077.py index 75df1d3fe0..0baacd70d6 100644 --- a/longest-consecutive-sequence/doh6077.py +++ b/longest-consecutive-sequence/doh6077.py @@ -32,3 +32,10 @@ def longestConsecutive(self, nums: List[int]) -> int: r += 1 return longest + + + + +nums = [1,2,3,5] +nums.sort() +print(nums) From f51e31c250fb69344edab24288a47eb8cf20c17e Mon Sep 17 00:00:00 2001 From: dohee Date: Fri, 6 Mar 2026 16:05:58 -0500 Subject: [PATCH 6/6] Longest Consecutive Sequence Solution --- longest-consecutive-sequence/doh6077.py | 29 ++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/longest-consecutive-sequence/doh6077.py b/longest-consecutive-sequence/doh6077.py index 0baacd70d6..a8ad7a28fe 100644 --- a/longest-consecutive-sequence/doh6077.py +++ b/longest-consecutive-sequence/doh6077.py @@ -1,4 +1,5 @@ # 배열을 정렬하고 포인터를 두개 사용 +""" class Solution: def longestConsecutive(self, nums: List[int]) -> int: if not nums: @@ -32,10 +33,32 @@ def longestConsecutive(self, nums: List[int]) -> int: r += 1 return longest +""" +from typing import List +class Solution: + def longestConsecutive(self, nums: List[int]) -> int: + if len(nums) <= 1: + return len(nums) + + nums.sort() + r = 1 + curr = nums[0] + count = 1 + max_count = 1 + while r < len(nums): + if nums[r] == curr: + r += 1 + continue + + if nums[r] == curr + 1: + count += 1 + else: + count = 1 + curr = nums[r] + max_count = max(max_count, count) + r += 1 -nums = [1,2,3,5] -nums.sort() -print(nums) + return max_count