diff --git a/3sum/chordpli.py b/3sum/chordpli.py new file mode 100644 index 000000000..498b7d232 --- /dev/null +++ b/3sum/chordpli.py @@ -0,0 +1,31 @@ +from typing import List + + +class Solution: + def threeSum(self, nums: List[int]) -> List[List[int]]: + answer = [] + nums.sort() + nums_len = len(nums) + + for i in range(nums_len - 2): + if i > 0 and nums[i] == nums[i - 1]: + continue + left, right = i + 1, nums_len - 1 + + while left < right: + current_sum = nums[i] + nums[left] + nums[right] + if current_sum < 0: + left += 1 + elif current_sum > 0: + right -= 1 + + else: + answer.append([nums[i], nums[left], nums[right]]) + while left < right and nums[left] == nums[left + 1]: + left += 1 + while left < right and nums[right] == nums[right - 1]: + right -= 1 + left += 1 + right -= 1 + + return answer diff --git a/climbing-stairs/chordpli.py b/climbing-stairs/chordpli.py new file mode 100644 index 000000000..7169e9358 --- /dev/null +++ b/climbing-stairs/chordpli.py @@ -0,0 +1,13 @@ +class Solution: + def climbStairs(self, n: int) -> int: + if n <= 2: + return n + + dp = [0] * (n + 1) + dp[1] = 1 + dp[2] = 2 + + for i in range(3, n + 1): + dp[i] = dp[i - 1] + dp[i - 2] + + return dp[n] diff --git a/contains-duplicate/chordpli.py b/contains-duplicate/chordpli.py new file mode 100644 index 000000000..b61243832 --- /dev/null +++ b/contains-duplicate/chordpli.py @@ -0,0 +1,12 @@ +from typing import List + +class Solution: + def containsDuplicate(self, nums: List[int]) -> bool: + dic = {} + + for num in nums: + if dic.get(num): + return True + dic[num] = 1 + + return False diff --git a/two-sum/chordpli.py b/two-sum/chordpli.py new file mode 100644 index 000000000..2e269a040 --- /dev/null +++ b/two-sum/chordpli.py @@ -0,0 +1,11 @@ +from typing import List + +class Solution: + def twoSum(self, nums: List[int], target: int) -> List[int]: + map = {} + for i, num in enumerate(nums): + complement = target - num + if complement in map: + return [map[complement], i] + + map[num] = i diff --git a/valid-anagram/chordpli.py b/valid-anagram/chordpli.py new file mode 100644 index 000000000..5aa040151 --- /dev/null +++ b/valid-anagram/chordpli.py @@ -0,0 +1,19 @@ +class Solution: + # dictionary 사용 11ms + def isAnagram(self, s: str, t: str) -> bool: + check = {} + for char in s: + if char not in check: + check[char] = 0 + check[char] += 1 + for char in t: + if char not in check: + return False + check[char] -= 1 + if check[char] == 0: + del check[char] + return not check + + # 정렬 사용 15 - 20ms + # def isAnagram(self, s: str, t: str) -> bool: + # return sorted(s) == sorted(t)