Skip to content

Commit ed748c0

Browse files
Merge pull request #2019 from Donghae0230/main
[Donghae0230] WEEK 01 solutions
2 parents a613fe7 + 620b3dd commit ed748c0

File tree

5 files changed

+94
-0
lines changed

5 files changed

+94
-0
lines changed

contains-duplicate/Donghae0230.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# 시간 복잡도
2+
# 입력 list를 set으로 변환 -> 최대 O(n)
3+
# 공간 복잡도
4+
# 입력 list를 set으로 변환 -> 최대 O(n)
5+
class Solution:
6+
def containsDuplicate(self, nums: List[int]) -> bool:
7+
set_nums = set(nums)
8+
list_nums = list(set_nums)
9+
if len(nums) != len(list_nums):
10+
return True
11+
else:
12+
return False

house-robber/Donghae0230.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# 시간 복잡도
2+
# nums의 길이에 여러 분기로 나뉨 -> O(2^n) -> Time Limit Exceeded로 실패
3+
# 공간 복잡도
4+
# 재귀 깊이에 따라 늘어남 -> 최대 O(n)
5+
6+
class Solution:
7+
def temp(self, n, nums, val, lst):
8+
if n+2 >= len(nums):
9+
lst.append(val)
10+
lst = max(lst)
11+
return lst
12+
for i in range(n+2, len(nums)):
13+
self.temp(i, nums, val+nums[i], lst)
14+
lst = max(lst)
15+
return lst
16+
17+
def rob(self, nums: List[int]) -> int:
18+
if len(nums) > 1:
19+
start_0 = self.temp(0, nums, nums[0], [])
20+
start_1 = self.temp(1, nums, nums[1], [])
21+
return max(start_0, start_1)
22+
else:
23+
return nums[0]
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# 시간 복잡도
2+
# 입력 list를 정렬해서 사용 -> 최대 O(n log n)
3+
# 공간 복잡도
4+
# 입력 list를 set으로 변환 -> 최대 O(n)
5+
6+
class Solution:
7+
def longestConsecutive(self, nums: List[int]) -> int:
8+
if len(nums) == 0:
9+
return 0
10+
sorted_nums = list(sorted(set(nums)))
11+
temp = 1
12+
result = 0
13+
# print(sorted_nums)
14+
for i in range(0, len(sorted_nums) - 1):
15+
if sorted_nums[i] + 1 == sorted_nums[i+1]:
16+
temp += 1
17+
else:
18+
result = max(result, temp)
19+
temp = 1
20+
result = max(result, temp)
21+
return result
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# 시간 복잡도
2+
# 입력 list를 정렬해서 사용 -> 최대 O(n log n)
3+
# 공간 복잡도
4+
# 입력 list를 set으로 변환 -> 최대 O(n)
5+
class Solution:
6+
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
7+
nums.sort()
8+
items = {}
9+
n = nums[0]
10+
cnt = 1
11+
if len(nums) > 1:
12+
for i in range(1, len(nums)):
13+
if n != nums[i]:
14+
items[n] = cnt
15+
n = nums[i]
16+
cnt = 1
17+
else:
18+
cnt += 1
19+
items[n] = cnt
20+
else:
21+
items[n] = 1
22+
sorted_items_desc = sorted(items.items(), key=lambda item: item[1], reverse=True)
23+
result = []
24+
for i in sorted_items_desc[:k]:
25+
result.append(i[0])
26+
return result

two-sum/Donghae0230.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# 시간 복잡도
2+
# 입력 list를 이중 for문에서 사용 -> O(n^2)
3+
# 공간 복잡도
4+
# 크기가 고정된 리스트 사용 -> O(1)
5+
class Solution:
6+
def twoSum(self, nums: List[int], target: int) -> List[int]:
7+
for i in range(0, len(nums)):
8+
for l in range(0, len(nums)):
9+
if i == l:
10+
continue
11+
if nums[i] + nums[l] == target:
12+
return [i, l]

0 commit comments

Comments
 (0)