diff --git a/contains-duplicate/hellojoyworldz.py b/contains-duplicate/hellojoyworldz.py new file mode 100644 index 000000000..ad4b5ad48 --- /dev/null +++ b/contains-duplicate/hellojoyworldz.py @@ -0,0 +1,13 @@ +# - 문제: https://leetcode.com/problems/contains-duplicate/ +# - 해설: https://www.algodale.com/problems/contains-duplicate/ + +# 217. Contains Duplicate +# - Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct. + +from typing import List + +class Solution: + def containsDuplicate(self, nums: List[int]) -> bool: + + return len(set(nums)) != len(nums) + diff --git a/house-robber/hellojoyworldz.py b/house-robber/hellojoyworldz.py new file mode 100644 index 000000000..1a84cc81f --- /dev/null +++ b/house-robber/hellojoyworldz.py @@ -0,0 +1,18 @@ +# - 문제: https://leetcode.com/problems/house-robber/ +# - 풀이: https://www.algodale.com/problems/house-robber/ + +from typing import List + + +class Solution: + def rob(self, nums: List[int]) -> int: + prev1 = 0 + prev2 = 0 + + for num in nums: + current = max(prev1, prev2 + num) + prev2 = prev1 + prev1 = current + + return prev1 + diff --git a/longest-consecutive-sequence/hellojoyworldz.py b/longest-consecutive-sequence/hellojoyworldz.py new file mode 100644 index 000000000..cf7e8a5f9 --- /dev/null +++ b/longest-consecutive-sequence/hellojoyworldz.py @@ -0,0 +1,26 @@ +# - 문제: https://leetcode.com/problems/longest-consecutive-sequence/ +# - 풀이: https://www.algodale.com/problems/longest-consecutive-sequence/ + +# Current complexity:O(NlogN) +# Suggested complexity:O(N) + +from typing import List + + +class Solution: + def longestConsecutive(self, nums: List[int]) -> int: + + sorted_nums = sorted(set(nums)) + + max_sequence = 1 + current_sequence = 1 + + for i in range(1, len(sorted_nums)): + if sorted_nums[i] - sorted_nums[i - 1] == 1: + current_sequence += 1 + max_sequence = max(max_sequence, current_sequence) + else: + current_sequence = 1 + + return max(max_sequence, current_sequence) + diff --git a/top-k-frequent-elements/hellojoyworldz.py b/top-k-frequent-elements/hellojoyworldz.py new file mode 100644 index 000000000..4fd0d5cfa --- /dev/null +++ b/top-k-frequent-elements/hellojoyworldz.py @@ -0,0 +1,12 @@ +# - 문제: https://leetcode.com/problems/top-k-frequent-elements/ +# - 풀이: https://www.algodale.com/problems/top-k-frequent-elements/ + +from collections import Counter +from typing import List + + +class Solution: + def topKFrequent(self, nums: List[int], k: int) -> List[int]: + counter = Counter(nums) + return [num for num, count in counter.most_common(k)] + diff --git a/two-sum/hellojoyworldz.py b/two-sum/hellojoyworldz.py new file mode 100644 index 000000000..925ce6214 --- /dev/null +++ b/two-sum/hellojoyworldz.py @@ -0,0 +1,17 @@ +# - 문제: https://leetcode.com/problems/two-sum/ +# - 해설: https://www.algodale.com/problems/two-sum/ + +from typing import List + + +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + for first_index, first_num in enumerate(nums): + for second_index, second_num in enumerate(nums): + if first_index == second_index: + continue + if first_num + second_num == target: + return [first_index, second_index] + + return [] +