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] 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 diff --git a/valid-palindrome/KwonNayeon.py b/valid-palindrome/KwonNayeon.py index 5bb039da8..c8a7cc970 100644 --- a/valid-palindrome/KwonNayeon.py +++ b/valid-palindrome/KwonNayeon.py @@ -1,21 +1,12 @@ """ -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) -Time Complexity: - - O(n) -Space Complexity: - - O(n) +Space Complexity: O(n) """ class Solution: def isPalindrome(self, s: str) -> bool: @@ -23,4 +14,29 @@ def isPalindrome(self, s: str) -> bool: 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 + return True