diff --git a/contains-duplicate/heypaprika.py b/contains-duplicate/heypaprika.py new file mode 100644 index 000000000..e2dd01bc8 --- /dev/null +++ b/contains-duplicate/heypaprika.py @@ -0,0 +1,11 @@ +# Big-O 예상 : O(n) +class Solution: + def containsDuplicate(self, nums: List[int]) -> bool: + num_dict = {} + for num in nums: + if num in num_dict: + return True + else: + num_dict[num] = 1 + return False + diff --git a/house-robber/heypaprika.py b/house-robber/heypaprika.py new file mode 100644 index 000000000..c62cbf0ec --- /dev/null +++ b/house-robber/heypaprika.py @@ -0,0 +1,19 @@ +# Big-O 예상 : O(n) +class Solution: + def rob(self, nums: List[int]) -> int: + a = [0] * len(nums) + + if len(nums) == 1: + return nums[0] + elif len(nums) == 2: + return max(nums[0], nums[1]) + + a[0] = nums[0] + a[1] = nums[1] + a[2] = max(a[0] + nums[2], a[1]) + + for i in range(3, len(nums)): + a[i] = max(a[i-3], a[i-2]) + nums[i] + + return max(a) + diff --git a/longest-consecutive-sequence/heypaprika.py b/longest-consecutive-sequence/heypaprika.py new file mode 100644 index 000000000..6122ea730 --- /dev/null +++ b/longest-consecutive-sequence/heypaprika.py @@ -0,0 +1,22 @@ +# Big-O 예상 : O(nlog(n)) +class Solution: + def longestConsecutive(self, nums: List[int]) -> int: + nums = sorted(list(set(nums))) + if len(nums) == 0: + return 0 + elif len(nums) == 1: + return 1 + cur_long = 1 + longest = 1 + for i, num in enumerate(nums): + if i == 0: + continue + else: + if nums[i-1] + 1 == nums[i]: + cur_long += 1 + if longest < cur_long: + longest = cur_long + else: + cur_long = 1 + return longest + diff --git a/top-k-frequent-elements/heypaprika.py b/top-k-frequent-elements/heypaprika.py new file mode 100644 index 000000000..c440d1f86 --- /dev/null +++ b/top-k-frequent-elements/heypaprika.py @@ -0,0 +1,16 @@ +# Big-O 예상 : O(nlog(n)) +import heapq +class Solution: + def topKFrequent(self, nums: List[int], k: int) -> List[int]: + + num_dict = {} + for num in nums: + num_dict[num] = num_dict.get(num, 0) + 1 + heap = [] + for k_, v in num_dict.items(): + heapq.heappush(heap, [-v, k_]) + ans = [] + for i in range(k): + ans.append(heapq.heappop(heap)[1]) + return ans + diff --git a/valid-palindrome/heypaprika.py b/valid-palindrome/heypaprika.py new file mode 100644 index 000000000..64b03e9bd --- /dev/null +++ b/valid-palindrome/heypaprika.py @@ -0,0 +1,13 @@ +# Big-O 예상 : O(n) +class Solution: + def isPalindrome(self, s: str) -> bool: + s = "".join(s.lower().split(" ")) + new_s = "" + for item in s: + if (ord("a") <= ord(item) <= ord("z")) or (ord("0") <= ord(item) <= ord("9")): + new_s += item + output = True + new_s_2 = new_s[::-1] + return new_s_2 == new_s + return output +