diff --git a/combination-sum/parkhojeong.py b/combination-sum/parkhojeong.py new file mode 100644 index 0000000000..58d1bb14b6 --- /dev/null +++ b/combination-sum/parkhojeong.py @@ -0,0 +1,29 @@ +from typing import List + +class Solution: + def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: + candidates = [x for x in sorted(candidates) if x <= target] + res = self.dfs(candidates, target, [], 0) + + return res + + def dfs( + self, candidates: List[int], target: int, + visited: List[int], prev_sum: int + ) -> List[List[int]]: + if prev_sum == target: + return [visited] + + arr = [] + for i in range(len(candidates)): + if prev_sum + candidates[i] > target: + continue + + res = self.dfs( + candidates[i:], target, + visited + [candidates[i]], prev_sum + candidates[i] + ) + for item in res: + arr.append(item) + + return arr diff --git a/decode-ways/parkhojeong.py b/decode-ways/parkhojeong.py new file mode 100644 index 0000000000..cdabefef4c --- /dev/null +++ b/decode-ways/parkhojeong.py @@ -0,0 +1,46 @@ +class Solution: + def numDecodings(self, s: str) -> int: + if s[0] == "0": + return 0 + + for i in range(1, len(s)): + if s[i] == "0": + if s[i - 1] == "0": + return 0 + if s[i - 1] >= "3": + return 0 + + splited_arr = [] + + i = len(s) - 1 + j = len(s) + while i > 0: + if s[i - 1] + s[i] >= "27": + splited_arr.append(s[i:j]) + i -= 1 + j = i + 1 + elif s[i] == "0": + splited_arr.append(s[i + 1:j]) + i -= 2 + j = i + 1 + else: + i -= 1 + + splited_arr.append(s[:j]) + + result = 1 + for s in splited_arr: + result *= self.count_num(s) + + return result + + def count_num(self, s: str) -> int: + if len(s) <= 1: + return 1 + + prev2, prev1, cur = 1, 2, 2 + for i in range(2, len(s)): + cur = prev2 + prev1 + prev2, prev1 = prev1, cur + + return cur diff --git a/maximum-subarray/parkhojeong.py b/maximum-subarray/parkhojeong.py new file mode 100644 index 0000000000..d3ac71bbf3 --- /dev/null +++ b/maximum-subarray/parkhojeong.py @@ -0,0 +1,44 @@ +import sys +from typing import List + +class Solution: + def maxSubArray(self, nums: List[int]) -> int: + max_val = -sys.maxsize + + for num in nums: + max_val = max(max_val, num) + + nums = [x for x in nums if x != 0] + if not nums: + return max_val + + arr = [] + arr.append(nums[0]) + for i in range(1, len(nums)): + if nums[i] * nums[i - 1] > 0: + arr[-1] += nums[i] + else: + arr.append(nums[i]) + + for num in arr: + max_val = max(max_val, num) + + while len(arr) >= 2: + if arr[0] < 0: + arr = arr[1:] + if arr[-1] < 0: + arr = arr[:-1] + + max_val = max(max_val, arr[0]) + if len(arr) >= 2: + arr[1] = arr[0] + arr[1] + arr = arr[1:] + + max_val = max(max_val, arr[-1]) + if len(arr) >= 2: + arr[-2] = arr[-1] + arr[-2] + arr = arr[:-1] + + max_val = max(max_val, arr[0]) + + return max_val diff --git a/number-of-1-bits/parkhojeong.py b/number-of-1-bits/parkhojeong.py new file mode 100644 index 0000000000..21403bef07 --- /dev/null +++ b/number-of-1-bits/parkhojeong.py @@ -0,0 +1,9 @@ +class Solution: + def hammingWeight(self, n: int) -> int: + count = 0 + + while n > 0: + count += n % 2 + n = n // 2 + + return count diff --git a/valid-palindrome/parkhojeong.py b/valid-palindrome/parkhojeong.py new file mode 100644 index 0000000000..b57187da39 --- /dev/null +++ b/valid-palindrome/parkhojeong.py @@ -0,0 +1,8 @@ +class Solution: + def isPalindrome(self, s: str) -> bool: + normalized_str = "" + for ch in s: + if ch.isalnum(): + normalized_str += ch.lower() + + return normalized_str == normalized_str[::-1]