diff --git a/contains-duplicate/yayyz.py b/contains-duplicate/yayyz.py new file mode 100644 index 000000000..c56bbd270 --- /dev/null +++ b/contains-duplicate/yayyz.py @@ -0,0 +1,4 @@ +class Solution: + def containsDuplicate(self, nums: List[int]) -> bool: + + return len(nums) != len(set(nums)) diff --git a/house-robber/yayyz.py b/house-robber/yayyz.py new file mode 100644 index 000000000..2dc384d68 --- /dev/null +++ b/house-robber/yayyz.py @@ -0,0 +1,16 @@ +class Solution: + def rob(self, nums: List[int]) -> int: + n = len(nums) + + if n == 1: + return nums[0] + + dp = [0] * n + dp[0] = nums[0] + dp[1] = max(nums[0], nums[1]) + + # i 번째를 털지 않은 조건과 i번째를 털은 조건 중 max를 비교 + for i in range(2, n): + dp[i] = max(dp[i-1], dp[i-2] + nums[i]) + + return dp[-1] diff --git a/longest-consecutive-sequence/yayyz.py b/longest-consecutive-sequence/yayyz.py new file mode 100644 index 000000000..f6589a471 --- /dev/null +++ b/longest-consecutive-sequence/yayyz.py @@ -0,0 +1,21 @@ +class Solution: + def longestConsecutive(self, nums: List[int]) -> int: + if len(nums) == 0: + return 0 + + nums = sorted(set(nums)) + + currLength = 1 + lastNum = nums[0] + result = 1 + + for i in range(1, len(nums)): + if nums[i] == lastNum + 1: + currLength += 1 + else: + currLength = 1 + + result = max(result, currLength) + lastNum = nums[i] + + return result diff --git a/product-of-array-except-self/yayyz.py b/product-of-array-except-self/yayyz.py new file mode 100644 index 000000000..e7baf402e --- /dev/null +++ b/product-of-array-except-self/yayyz.py @@ -0,0 +1,16 @@ +class Solution: + # prefix, postfix probelm :( + def productExceptSelf(self, nums: List[int]) -> List[int]: + output = [1] * len(nums) + + prefix = 1 + for i in range(len(nums)): + output[i] = prefix + prefix *= nums[i] + + postfix = 1 + for i in range(len(nums) -1, -1, -1): + output[i] *= postfix + postfix *= nums[i] + + return output diff --git a/two-sum/yayyz.py b/two-sum/yayyz.py new file mode 100644 index 000000000..c62b495cc --- /dev/null +++ b/two-sum/yayyz.py @@ -0,0 +1,10 @@ +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + hashmap = {} + for i, num in enumerate(nums): + complement = target - num + if complement in hashmap: + return [hashmap[complement], i] + hashmap[num] = i + +