Skip to content

[KwonNayeon] Week 3 solutions #1278

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions decode-ways/KwonNayeon.py
Original file line number Diff line number Diff line change
@@ -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]
31 changes: 31 additions & 0 deletions maximum-subarray/KwonNayeon.py
Original file line number Diff line number Diff line change
@@ -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
46 changes: 31 additions & 15 deletions valid-palindrome/KwonNayeon.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,42 @@
"""
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.

<Solution 1>
Time Complexity: O(n)

Time Complexity:
- O(n)
Space Complexity:
- O(n)
Space Complexity: O(n)
"""
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
"""
<Solution 2>
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