Skip to content

Commit 189911d

Browse files
committed
week03_4_5_solution
1 parent e65d4d4 commit 189911d

2 files changed

Lines changed: 40 additions & 0 deletions

File tree

โ€Ždecode-ways/YOOHYOJEONG.pyโ€Ž

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# https://leetcode.com/problems/decode-ways
2+
3+
class Solution(object):
4+
def numDecodings(self, s):
5+
if not s or s[0] == '0':
6+
return 0
7+
8+
n = len(s)
9+
10+
# dp[i] = i๋ฒˆ์งธ๊นŒ์ง€ ๋ฌธ์ž์—ด์„ ํ•ด์„ํ•˜๋Š” ๋ฐฉ๋ฒ•์˜ ๊ฐœ์ˆ˜
11+
dp = [0] * (n + 1)
12+
dp[0] = 1
13+
dp[1] = 1
14+
15+
for i in range(2, n + 1):
16+
# 1์ž๋ฆฌ ์ฒดํฌ
17+
if s[i-1] != '0':
18+
dp[i] += dp[i-1]
19+
20+
# 2์ž๋ฆฌ ์ฒดํฌ
21+
two_digit = int(s[i-2:i])
22+
if 10 <= two_digit <= 26:
23+
dp[i] += dp[i-2]
24+
25+
return dp[n]
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# https://leetcode.com/problems/maximum-subarray/
2+
3+
# ์ด์ „๊นŒ์ง€ ๋ˆ„์ ํ•œ ๊ฐ’์ด ์Œ์ˆ˜๋ฉด ๋ฒ„๋ฆฌ๊ณ  ํ˜„์žฌ ๊ฐ’๋ถ€ํ„ฐ ๋‹ค์‹œ ์‹œ์ž‘
4+
# ํ•œ ๋ฒˆ๋งŒ ์ˆœํšŒ -> O(n)
5+
6+
class Solution(object):
7+
def maxSubArray(self, nums):
8+
current_sum = nums[0]
9+
max_sum = nums[0]
10+
11+
for i in range(1, len(nums)):
12+
current_sum = max(nums[i], current_sum + nums[i])
13+
max_sum = max(max_sum, current_sum)
14+
15+
return max_sum

0 commit comments

Comments
ย (0)