diff --git a/contains-duplicate/jeongyunjae.py b/contains-duplicate/jeongyunjae.py new file mode 100644 index 000000000..2d99650be --- /dev/null +++ b/contains-duplicate/jeongyunjae.py @@ -0,0 +1,3 @@ +class Solution: + def containsDuplicate(self, nums: List[int]) -> bool: + return len(list(set(nums))) != len(nums) diff --git a/house-robber/jeongyunjae.py b/house-robber/jeongyunjae.py new file mode 100644 index 000000000..612180e32 --- /dev/null +++ b/house-robber/jeongyunjae.py @@ -0,0 +1,18 @@ +class Solution: + def rob(self, nums: List[int]) -> int: + result = list(nums) + + for i, data in enumerate(result): + if i <= 1: + continue + + stolen_money = result[i] + before_house_money = result[i-1] + + for j in range(i-1): + if result[i] + result[j] > stolen_money: + stolen_money = result[i] + result[j] + + result[i] = max(before_house_money, stolen_money) + + return max(result) diff --git a/longest-consecutive-sequence/jeongyunjae.py b/longest-consecutive-sequence/jeongyunjae.py new file mode 100644 index 000000000..95d914dce --- /dev/null +++ b/longest-consecutive-sequence/jeongyunjae.py @@ -0,0 +1,16 @@ +class Solution: + def longestConsecutive(self, nums: List[int]) -> int: + if len(nums) <= 1: + return len(nums) + + result = [1] * len(nums) + + # 중복 제거 후 정렬 + nums = sorted(list(set(nums))) + + # 연속된 숫자 찾기 + for i in range(1, len(nums)): + if nums[i] - 1 == nums[i-1]: + result[i] = result[i-1] + 1 + + return max(result) diff --git a/top-k-frequent-elements/jeongyunjae.py b/top-k-frequent-elements/jeongyunjae.py new file mode 100644 index 000000000..c1869f7e6 --- /dev/null +++ b/top-k-frequent-elements/jeongyunjae.py @@ -0,0 +1,24 @@ +class Solution: + def topKFrequent(self, nums: List[int], k: int) -> List[int]: + my_dict = {} + result = [] + temp_list = [] + + # 딕셔너리 생성 + for data in nums: + if data not in my_dict.keys(): + my_dict[data] = 0 + my_dict[data] = my_dict[data] + 1 + + # 딕셔너리를 리스트로 변환 + for data in my_dict.keys(): + temp_list.append([data, my_dict[data]]) + + # 빈도수를 기준으로 정렬 + temp_list.sort(key=lambda a: a[1],reverse=True) + + # 상위 k개 요소 추출 + for i in range(k): + result.append(temp_list[i][0]) + + return result diff --git a/two-sum/jeongyunjae.py b/two-sum/jeongyunjae.py new file mode 100644 index 000000000..1b0925496 --- /dev/null +++ b/two-sum/jeongyunjae.py @@ -0,0 +1,11 @@ +class Solution: + + def twoSum(self, nums: List[int], target: int) -> List[int]: + my_dict = {} + + for i, data in enumerate(nums): + remaining = target - data + if remaining not in my_dict: + my_dict[data] = i + else: + return [my_dict[remaining], i]