Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 5 additions & 8 deletions contains-duplicate/doh6077.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
# 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)
30 changes: 30 additions & 0 deletions longest-consecutive-sequence/doh6077.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# 배열을 정렬하고 포인터를 두개 사용
"""
class Solution:
def longestConsecutive(self, nums: List[int]) -> int:
if not nums:
Expand Down Expand Up @@ -32,3 +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

return max_count
40 changes: 32 additions & 8 deletions top-k-frequent-elements/doh6077.py
Original file line number Diff line number Diff line change
@@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorted를 사용할 때 reverse 옵션 외에 key 옵션을 넣어 값을 비교할 수 있다는 점을 알았습니다.


# 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
30 changes: 23 additions & 7 deletions two-sum/doh6077.py
Original file line number Diff line number Diff line change
@@ -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):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

enumerate를 사용해서 index와 num을 동시에 가져온 점이 좋았습니다.

find_val = target - num

if find_val in nums_hm:
return [nums_hm[find_val], i]

nums_hm[num] = i