diff --git a/combination-sum/Seoya0512.py b/combination-sum/Seoya0512.py new file mode 100644 index 0000000000..90632d9ca0 --- /dev/null +++ b/combination-sum/Seoya0512.py @@ -0,0 +1,15 @@ +''' +Approach +- target값에서 candidate 값을 빼고 그 누적합을 사용해야한다는 흐름은 파악했지지만 구현이 어려웠습니다. +-그래서 알고달레를 참고해서 최대한 이해하고 혼자 작성해보려고 했습니다.... +''' +class Solution: + def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: + dp = [[] for _ in range(target + 1)] + dp[0] = [[]] + + for candidate in candidates: + for num in range(candidate, target +1): + for combination in dp[num - candidate]: + dp[num].append(combination + [candidate]) + return dp[target] diff --git a/number-of-1-bits/Seoya0512.py b/number-of-1-bits/Seoya0512.py new file mode 100644 index 0000000000..982123dc3d --- /dev/null +++ b/number-of-1-bits/Seoya0512.py @@ -0,0 +1,20 @@ +''' +Approach +- 십진법을 이진법으로 변환하는 방식과 누적합을 사용함 + +Time Complexity: O(log n) +- while 문에서 숫자(num)을 계속해서 2로 나누는데 소요되는 시간 + +Space Complexity: O(1) +- 상수 bits와 nums를 저장하는 공간 +''' +class Solution: + def hammingWeight(self, n: int) -> int: + bits = 0 + # 주어진 숫자를 2로 나눈 나머지 값이 1인 경우 bits에 누적함 + num = n + while num != 0 : + if num % 2 == 1 : + bits += 1 + num //= 2 + return bits diff --git a/valid-palindrome/Seoya0512.py b/valid-palindrome/Seoya0512.py new file mode 100644 index 0000000000..53144a5f19 --- /dev/null +++ b/valid-palindrome/Seoya0512.py @@ -0,0 +1,23 @@ +''' +Approach +- 파이썬에서 제공되는 메소드를 사용해서 문자열 필터링 처리후 왼쪽과 오른쪽 값을 비교 + +Time Complexity: O(N) +- 문자열 s의 길이를 N이라고 할 때, 문자열을 순회하며 필터링하는 데 O(N) 시간이 걸림 +- 이후 필터링된 문자열을 뒤집어 right 값을 만드는 데도 O(N) 시간이 걸림 +- left와 right 값을 비교하는 데도 O(N) 시간이 걸림 + +Space Complexity: O(N) +- 필터링된 문자열을 저장하는 데 O(N) 공간이 필요함 +- left와 right 값을 저장하는 데도 각각 O(N) 공간이 필요함 +''' +class Solution: + def isPalindrome(self, s: str) -> bool: + # 문자열이 아닌 경우만 소문자 변환 필터링 + char_str = [char.lower() for char in s if char.isalnum()] + # 왼쪽 값 + left = ''.join(char_str) + # 오른쪽 값 + right = ''.join(char_str[::-1]) + + return left == right