From 9f4c8a9209acaeac3ff2f43396355dd68b569efa Mon Sep 17 00:00:00 2001 From: prograsshopper Date: Fri, 8 Aug 2025 19:54:29 +0900 Subject: [PATCH 1/4] week 3: Valid Palindrome --- valid-palindrome/prograsshopper.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 valid-palindrome/prograsshopper.py diff --git a/valid-palindrome/prograsshopper.py b/valid-palindrome/prograsshopper.py new file mode 100644 index 000000000..631023c12 --- /dev/null +++ b/valid-palindrome/prograsshopper.py @@ -0,0 +1,16 @@ +class Solution: + def isPalindrome(self, s: str) -> bool: + formatted_string = "".join(elem.lower() for elem in s if elem.isalnum()) + + # sol 1 + return formatted_string == formatted_string[::-1] + + # sol 2 + left = 0 + right = len(formatted_string) - 1 + while left < right: + if formatted_string[left] != formatted_string[right]: + return False + left += 1 + right -= 1 + return True From 9bc84ff124e0a972ad6c03597abbd3040462f0ac Mon Sep 17 00:00:00 2001 From: prograsshopper Date: Sat, 9 Aug 2025 01:26:39 +0900 Subject: [PATCH 2/4] week 3: Number of 1 bits --- number-of-1-bits/prograsshopper.py | 10 ++++++++++ valid-palindrome/prograsshopper.py | 2 ++ 2 files changed, 12 insertions(+) create mode 100644 number-of-1-bits/prograsshopper.py diff --git a/number-of-1-bits/prograsshopper.py b/number-of-1-bits/prograsshopper.py new file mode 100644 index 000000000..15c91c71b --- /dev/null +++ b/number-of-1-bits/prograsshopper.py @@ -0,0 +1,10 @@ +class Solution: + def hammingWeight(self, n: int) -> int: + # Time complexity: O(log n) + output = 1 + while n > 1: + remain = n % 2 + n = n // 2 + if remain == 1: + output += 1 + return output diff --git a/valid-palindrome/prograsshopper.py b/valid-palindrome/prograsshopper.py index 631023c12..05d7b92cf 100644 --- a/valid-palindrome/prograsshopper.py +++ b/valid-palindrome/prograsshopper.py @@ -3,9 +3,11 @@ def isPalindrome(self, s: str) -> bool: formatted_string = "".join(elem.lower() for elem in s if elem.isalnum()) # sol 1 + # Time complexity: O(n) return formatted_string == formatted_string[::-1] # sol 2 + # Time complexity: O(n) left = 0 right = len(formatted_string) - 1 while left < right: From 744656ee32e9441160e4c67f044be5aefadcd61c Mon Sep 17 00:00:00 2001 From: prograsshopper Date: Sat, 9 Aug 2025 02:04:07 +0900 Subject: [PATCH 3/4] week 3: decode ways --- decode-ways/prograsshopper.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 decode-ways/prograsshopper.py diff --git a/decode-ways/prograsshopper.py b/decode-ways/prograsshopper.py new file mode 100644 index 000000000..52e5df142 --- /dev/null +++ b/decode-ways/prograsshopper.py @@ -0,0 +1,19 @@ +class Solution: + def numDecodings(self, s: str) -> int: + # sol 1: Time complexity O(n) + dp = {len(s): 1} + + def dfs(start): + if start in dp: + return dp[start] + if s[start] == "0": + return 0 + result = None + if start + 1 < len(s) and int(s[start:start+2]) < 27: + result = dfs(start+1) + dfs(start+2) + else: + result = dfs(start+1) + dp[start] = result + return result + + return dfs(0) From d7450863cecc900935ec77600686b3b340360351 Mon Sep 17 00:00:00 2001 From: prograsshopper Date: Sat, 9 Aug 2025 12:44:31 +0900 Subject: [PATCH 4/4] week 3: combination sum --- combination-sum/prograsshopper.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 combination-sum/prograsshopper.py diff --git a/combination-sum/prograsshopper.py b/combination-sum/prograsshopper.py new file mode 100644 index 000000000..93a024104 --- /dev/null +++ b/combination-sum/prograsshopper.py @@ -0,0 +1,17 @@ +class Solution: + def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]: + # Time complexity: O(n ^ (T / m)) + result = [] + + def dfs(remain_sum, index, path): + if remain_sum < 0: + return + if remain_sum == 0: + result.append(path) + return + + for i in range(index, len(candidates)): + dfs(remain_sum - candidates[i], i, path + [candidates[i]]) + + dfs(target, 0, []) + return result