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
19 changes: 19 additions & 0 deletions contains-duplicate/devyulbae.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"""
Blind75 - 1. Contains Duplicate
https://leetcode.com/problems/contains-duplicate/

Counter를 사용한 풀이
Counter 생성에 n번, 조회에 n번 -> O(n)
"""
from typing import List
from collections import Counter

class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
counter = Counter(nums)
for count in counter.values():
if count > 1:
return True

return False

39 changes: 39 additions & 0 deletions house-robber/devyulbae.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""
blind75 - House Robber
LeetCode Problem: https://leetcode.com/problems/house-robber/

재귀
F(x) = F(x+1), F(x+2) + nums[x] 중 큰 값을 계속 선택하면 된다
-> 재귀를 2번씩 호출하기 때문에 시간복잡도는 O(2^n)으로 Time Limit Exceeded 발생
다음 주차 때 다시 풀어보자.
dp로도 될 거 같다
"""

from typing import List

class Solution:
def rob(self, nums: List[int]) -> int:
if not nums:
return 0

memo = {}

def dfs(s, memo):
if s >= len(nums):
return 0
if s+1 in memo:
prev1 = memo[s+1]
else:
prev1 = dfs(s+1, memo)
memo[s+1] = prev1

if s+2 in memo:
prev2 = memo[s+2]
else:
prev2 = dfs(s+2, memo)
memo[s+2] = prev2

return max(prev1, prev2 + nums[s])

return dfs(0, memo)

30 changes: 30 additions & 0 deletions longest-consecutive-sequence/devyulbae.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""
Blind75 - Longest Consecutive Sequence
https://leetcode.com/problems/longest-consecutive-sequence/

조건 : O(n) 시간복잡도
1. 시작점이면 초기화
2. 아니면 cnt++
3. 끊기면 max 갱신
"""
from typing import List

class Solution:
def longestConsecutive(self, nums: List[int]) -> int:
if len(nums) == 0:
return 0
nums.sort()
length = 1
max_length = 0
for i in range(len(nums)-1):
if nums[i+1] == nums[i]:
continue
elif nums[i+1] == nums[i] + 1:
length += 1
else:
max_length = max(max_length, length)
length = 1

max_length = max(max_length, length)
return max_length

16 changes: 16 additions & 0 deletions top-k-frequent-elements/devyulbae.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
Blind75 - top-k-frequent-elements
https://leetcode.com/problems/top-k-frequent-elements/
"""

from typing import List
from collections import Counter

class Solution:
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
# (1,1,2,2,2,3) -> {1:1, 2:3, 3:1}
num_counter = Counter(nums)
# {1:1, 2:3, 3:1} -> [(2,3), (1,1), (3,1)]
sorted_items = sorted(num_counter.items(), key=lambda x: x[1], reverse=True)
return [item[0] for item in sorted_items[:k]]

12 changes: 12 additions & 0 deletions two-sum/devyulbae.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
"""
https://leetcode.com/problems/two-sum/
"""
from typing import List

class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
for i in range(len(nums)):
for j in range(i+1, len(nums)):
if nums[i] + nums[j] == target:
return [i, j]