diff --git a/contains-duplicate/devyulbae.py b/contains-duplicate/devyulbae.py new file mode 100644 index 000000000..b0b6b545b --- /dev/null +++ b/contains-duplicate/devyulbae.py @@ -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 + diff --git a/house-robber/devyulbae.py b/house-robber/devyulbae.py new file mode 100644 index 000000000..182084572 --- /dev/null +++ b/house-robber/devyulbae.py @@ -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) + diff --git a/longest-consecutive-sequence/devyulbae.py b/longest-consecutive-sequence/devyulbae.py new file mode 100644 index 000000000..70dbb9313 --- /dev/null +++ b/longest-consecutive-sequence/devyulbae.py @@ -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 + diff --git a/top-k-frequent-elements/devyulbae.py b/top-k-frequent-elements/devyulbae.py new file mode 100644 index 000000000..2a271037f --- /dev/null +++ b/top-k-frequent-elements/devyulbae.py @@ -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]] + diff --git a/two-sum/devyulbae.py b/two-sum/devyulbae.py new file mode 100644 index 000000000..06a96e70c --- /dev/null +++ b/two-sum/devyulbae.py @@ -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] +