Skip to content

[sounmind] WEEK 03 solutions #1318

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 2 commits into from
Apr 22, 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
48 changes: 33 additions & 15 deletions decode-ways/sounmind.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,42 @@
def numDecodings(s: str) -> int:
if not s:
return 0
"""
Count the number of ways to decode a string where:
'A' -> '1', 'B' -> '2', ..., 'Z' -> '26'

n = len(s)
Args:
s: A string of digits

# dp[i] represents the number of ways to decode the string s[:i]
dp = [0] * (n + 1)
dp[0] = 1
Returns:
The number of ways to decode the input string
"""
# Handle edge cases
if not s or s[0] == "0":
return 0

dp[1] = 1 if s[0] != "0" else 0
# Optimization: we only need to track the previous two results
prev = 1 # Ways to decode up to index i-2
curr = 1 # Ways to decode up to index i-1

for i in range(2, n + 1):
if s[i - 1] != "0":
# If the one-digit number is valid, we can decode it
dp[i] += dp[i - 1]
for i in range(1, len(s)):
# Start with 0 for the new current calculation
next_val = 0 # Ways to decode up to index i

two_digit = int(s[i - 2 : i])
# Single digit decode - if current digit is not '0'
if s[i] != "0":
# We can decode it independently,
# adding all ways to decode up to the previous position.
next_val += curr

# Two digit decode - if the last two digits form a valid letter (10-26)
two_digit = int(s[i - 1 : i + 1])
if 10 <= two_digit <= 26:
# If the two-digit number is valid, we can decode it
dp[i] += dp[i - 2]
next_val += prev

# Shift two trackers for the next iteration
prev, curr = curr, next_val

# If there's no way to decode at this point, the whole string is invalid
if curr == 0:
return 0

return dp[n]
return curr
25 changes: 19 additions & 6 deletions maximum-subarray/sounmind.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,24 @@

class Solution:
def maxSubArray(self, nums: List[int]) -> int:
global_max_sum = nums[0]
local_max_sum = nums[0]

"""
Find the contiguous subarray with the largest sum using Kadane's algorithm.

Args:
nums: List of integers

Returns:
Maximum subarray sum
"""
if not nums:
return 0

global_max = local_max = nums[0]

for num in nums[1:]:
local_max_sum = max(num, local_max_sum + num)
global_max_sum = max(global_max_sum, local_max_sum)
# Either start a new subarray with current element or extend previous subarray
local_max = max(num, local_max + num)
# Update global maximum if current local maximum is greater
global_max = max(global_max, local_max)

return global_max_sum
return global_max