Skip to content

Commit 6f915fd

Browse files
authored
Merge pull request #2370 from doh6077/main
[doh6077] WEEK 01 solutions
2 parents 62d77df + f51e31c commit 6f915fd

File tree

4 files changed

+90
-23
lines changed

4 files changed

+90
-23
lines changed

contains-duplicate/doh6077.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1+
# https://leetcode.com/problems/contains-duplicate/description/
12

2-
# set에 저장하면서 중복 여부 확인하기
33
class Solution:
4-
def containsDuplicate(self, nums: list[int]) -> bool:
5-
hashset = set()
6-
for i in nums:
7-
if i in hashset:
8-
return True
9-
hashset.add(i)
10-
return False
4+
def containsDuplicate(self, nums: List[int]) -> bool:
5+
# Create a set to store unique numbers from nums
6+
nums_set = set(nums)
7+
return len(nums_set) != len(nums)

longest-consecutive-sequence/doh6077.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# 배열을 정렬하고 포인터를 두개 사용
2+
"""
23
class Solution:
34
def longestConsecutive(self, nums: List[int]) -> int:
45
if not nums:
@@ -32,3 +33,32 @@ def longestConsecutive(self, nums: List[int]) -> int:
3233
r += 1
3334
3435
return longest
36+
"""
37+
from typing import List
38+
39+
class Solution:
40+
def longestConsecutive(self, nums: List[int]) -> int:
41+
if len(nums) <= 1:
42+
return len(nums)
43+
44+
nums.sort()
45+
r = 1
46+
curr = nums[0]
47+
count = 1
48+
max_count = 1
49+
50+
while r < len(nums):
51+
if nums[r] == curr:
52+
r += 1
53+
continue
54+
55+
if nums[r] == curr + 1:
56+
count += 1
57+
else:
58+
count = 1
59+
60+
curr = nums[r]
61+
max_count = max(max_count, count)
62+
r += 1
63+
64+
return max_count

top-k-frequent-elements/doh6077.py

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,36 @@
1+
2+
# 6기
3+
# class Solution:
4+
# # dictionary use
5+
# def topKFrequent(self, nums: List[int], k: int) -> List[int]:
6+
# result = {} # key: 원소, value: 등장 횟수
7+
# for n in nums:
8+
# if n in result:
9+
# result[n] = result[n] + 1
10+
# else:
11+
# result[n] = 1
12+
13+
# # 가장 자주 등장한 원소 k개 반환
14+
# return sorted(result.keys(), key=lambda x: result[x], reverse=True)[:k]
15+
16+
# 7기
117
class Solution:
2-
# dictionary use
318
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
4-
result = {} # key: 원소, value: 등장 횟수
5-
for n in nums:
6-
if n in result:
7-
result[n] = result[n] + 1
19+
# 1. Count frequency of each number
20+
freq = {}
21+
22+
for num in nums:
23+
if num not in freq:
24+
freq[num] = 1
825
else:
9-
result[n] = 1
26+
freq[num] += 1
27+
28+
# 2. Sort by frequency in descending order
29+
sorted_items = sorted(freq.items(), key=lambda item: item[1], reverse=True)
30+
31+
# 3. Take the first k elements
32+
result = []
33+
for i in range(k):
34+
result.append(sorted_items[i][0])
1035

11-
# 가장 자주 등장한 원소 k개 반환
12-
return sorted(result.keys(), key=lambda x: result[x], reverse=True)[:k]
36+
return result

two-sum/doh6077.py

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,24 @@
1+
# 6기
2+
# class Solution:
3+
# def twoSum(self, nums: list[int], target: int) -> list[int]:
4+
# prevMap = {} # val : index
5+
# for i, n in enumerate(nums):
6+
# diff = target - n
7+
# if diff in prevMap:
8+
# return [prevMap[diff], i]
9+
# prevMap[n] = i
10+
11+
# 7기
12+
# https://leetcode.com/problems/two-sum/description/
113
class Solution:
2-
def twoSum(self, nums: list[int], target: int) -> list[int]:
3-
prevMap = {} # val : index
4-
for i, n in enumerate(nums):
5-
diff = target - n
6-
if diff in prevMap:
7-
return [prevMap[diff], i]
8-
prevMap[n] = i
14+
def twoSum(self, nums: List[int], target: int) -> List[int]:
15+
# use Hash map to save num and index
16+
nums_hm = {}
17+
18+
for i, num in enumerate(nums):
19+
find_val = target - num
20+
21+
if find_val in nums_hm:
22+
return [nums_hm[find_val], i]
23+
24+
nums_hm[num] = i

0 commit comments

Comments
 (0)