From 21ebd8564a92e4e744b5d4a5d8a0b5d29fed57a3 Mon Sep 17 00:00:00 2001 From: suhyeonglee Date: Fri, 18 Apr 2025 16:31:06 -0400 Subject: [PATCH 1/2] refactor: optimize numDecodings function by removing unnecessary dynamic programming array --- decode-ways/sounmind.py | 48 ++++++++++++++++++++++++++++------------- 1 file changed, 33 insertions(+), 15 deletions(-) diff --git a/decode-ways/sounmind.py b/decode-ways/sounmind.py index 8a0535f0d..44aa80db2 100644 --- a/decode-ways/sounmind.py +++ b/decode-ways/sounmind.py @@ -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 From 1a5966cc6d608d733920ec0f64523a27e9bc9108 Mon Sep 17 00:00:00 2001 From: suhyeonglee Date: Fri, 18 Apr 2025 16:41:21 -0400 Subject: [PATCH 2/2] refactor: improve maxSubArray implementation - Enhance code readability with clearer variable names - Add detailed docstring for function explanation - Maintain functionality while optimizing structure --- maximum-subarray/sounmind.py | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/maximum-subarray/sounmind.py b/maximum-subarray/sounmind.py index f60fc07fe..1472830e0 100644 --- a/maximum-subarray/sounmind.py +++ b/maximum-subarray/sounmind.py @@ -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