From 81934ec0e790738669f4424783d928efd526ab29 Mon Sep 17 00:00:00 2001 From: Nayeon Date: Sun, 13 Apr 2025 17:56:48 +0900 Subject: [PATCH 1/4] Add placeholder for solution 2 --- valid-palindrome/KwonNayeon.py | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/valid-palindrome/KwonNayeon.py b/valid-palindrome/KwonNayeon.py index 5bb039da8..94bc7ec72 100644 --- a/valid-palindrome/KwonNayeon.py +++ b/valid-palindrome/KwonNayeon.py @@ -1,22 +1,14 @@ """ -Title: 215. Valid Palindrome -Link: https://leetcode.com/problems/valid-palindrome/ - -Summary: - - Palindrome이라면 True, 아니라면 False를 반환하는 문제. - - Palindrome이란, 앞으로 읽어도 뒤에서부터 읽어도 동일한 단어를 뜻함. - - 추가 조건: 대소문자를 구분하지 않으며, 알파벳과 숫자 이외의 문자는 제거해야 함. - - e.g. racecar - Conditions: - - 입력 문자열이 Palindrome인 경우: `True` 반환 - - Palindrome이 아닌 경우: `False` 반환 +- 1 <= s.length <= 2 * 10^5 +- s consists only of printable ASCII characters. Time Complexity: - - O(n) +- O(n) Space Complexity: - - O(n) +- O(n) """ +# Solution 1 class Solution: def isPalindrome(self, s: str) -> bool: s = re.sub(r'[^a-zA-z0-9]', '', s).lower() @@ -24,3 +16,4 @@ def isPalindrome(self, s: str) -> bool: return True return False +# Solution 2 From 8b6e42103a06f70ece4dca8140ee580de5d4a5b9 Mon Sep 17 00:00:00 2001 From: Nayeon Date: Wed, 16 Apr 2025 10:46:09 +0900 Subject: [PATCH 2/4] Add the second solution for Valid Palindrome problem. --- valid-palindrome/KwonNayeon.py | 35 ++++++++++++++++++++++++++++------ 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/valid-palindrome/KwonNayeon.py b/valid-palindrome/KwonNayeon.py index 94bc7ec72..c8a7cc970 100644 --- a/valid-palindrome/KwonNayeon.py +++ b/valid-palindrome/KwonNayeon.py @@ -3,17 +3,40 @@ - 1 <= s.length <= 2 * 10^5 - s consists only of printable ASCII characters. -Time Complexity: -- O(n) -Space Complexity: -- O(n) + +Time Complexity: O(n) + +Space Complexity: O(n) """ -# Solution 1 class Solution: def isPalindrome(self, s: str) -> bool: s = re.sub(r'[^a-zA-z0-9]', '', s).lower() if s == s[::-1]: return True return False +""" + +Time Complexity: O(n) +- 팰린드롬일 경우, 각 문자를 한 번씩 검사 + +Space Complexity: O(1) +- left, right 포인터 외에 추가 공간 사용 없음 +""" +class Solution: + def isPalindrome(self, s: str) -> bool: + left, right = 0, len(s) - 1 + + while left < right: + while left < right and not s[left].isalnum(): + left += 1 + + while left < right and not s[right].isalnum(): + right -= 1 + + if s[left].lower() != s[right].lower(): + return False + + left += 1 + right -= 1 -# Solution 2 + return True From 998a10b058fd7faf818d61d8b5a00c91a94e81b1 Mon Sep 17 00:00:00 2001 From: Nayeon Date: Thu, 17 Apr 2025 12:44:47 +0900 Subject: [PATCH 3/4] Add solution for Maximun subarray --- maximum-subarray/KwonNayeon.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 maximum-subarray/KwonNayeon.py diff --git a/maximum-subarray/KwonNayeon.py b/maximum-subarray/KwonNayeon.py new file mode 100644 index 000000000..d12a244b9 --- /dev/null +++ b/maximum-subarray/KwonNayeon.py @@ -0,0 +1,31 @@ +""" +Conditions: +- 1 <= nums.length <= 10^5 +- -10^4 <= nums[i] <= 10^4 + +Time Complexity: O(n) +- 배열을 한 번만 순회함 + +Space Complexity: O(1) +- 두 변수 이외에 추가 공간을 사용하지 않음 + +풀이방법: +1. Base case: If nums is empty, return 0 +2. Initialize variables (current_sum and max_sum as the first value in the array) +3. Traverse from the value at 1st index to the last, update current_sum + - Decide whether to add the current value (num) to the existing subarray or start a new one +4. Update max_sum + - Choose the larger between the updated current_sum and the previous max_sum +""" +class Solution: + def maxSubArray(self, nums: List[int]) -> int: + if not nums: + return 0 + + current_sum = max_sum = nums[0] + + for num in nums[1:]: + current_sum = max(num, current_sum + num) + max_sum = max(current_sum, max_sum) + + return max_sum From 1c68147eb8dae855e34de9e315d4796a1b799042 Mon Sep 17 00:00:00 2001 From: Nayeon Date: Fri, 18 Apr 2025 15:39:01 +0900 Subject: [PATCH 4/4] Add solution for Decode Ways problem --- decode-ways/KwonNayeon.py | 42 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 decode-ways/KwonNayeon.py diff --git a/decode-ways/KwonNayeon.py b/decode-ways/KwonNayeon.py new file mode 100644 index 000000000..f2f523bbb --- /dev/null +++ b/decode-ways/KwonNayeon.py @@ -0,0 +1,42 @@ +""" +Conditions: +- 1 <= s.length <= 100 +- s contains only digits and may contain leading zero(s). + +Time Complexity: O(n) +- 각 문자를 한 번씩만 방문함 (문자열의 길이: n) + +Space Complexity: O(n) +- dp 배열의 크기는 n+1 +- 이 배열 이외에 추가 공간을 사용하지 않음 + +Base cases: +- If string is empty or starts with '0', return 0 (impossible to decode) + +Dynamic Programming approach: +- dp[i] represents the number of ways to decode first i characters +- dp array has size n+1 to include the empty string case (dp[0]) +- For each position, consider both single-digit and two-digit decodings + +메모: +- 다른 dp문제 풀어보기 +""" +class Solution: + def numDecodings(self, s: str) -> int: + if not s or s[0] == '0': + return 0 + + n = len(s) + dp = [0] * (n + 1) + dp[0] = 1 + + for i in range(1, n + 1): + if s[i-1] != '0': + dp[i] += dp[i-1] + + if i > 1 and s[i-2] != '0': + two_digit = int(s[i-2:i]) + if 1 <= two_digit <= 26: + dp[i] += dp[i-2] + + return dp[n]