File tree Expand file tree Collapse file tree 3 files changed +104
-15
lines changed Expand file tree Collapse file tree 3 files changed +104
-15
lines changed Original file line number Diff line number Diff line change
1
+ """
2
+ Conditions:
3
+ - 1 <= s.length <= 100
4
+ - s contains only digits and may contain leading zero(s).
5
+
6
+ Time Complexity: O(n)
7
+ - 각 문자를 한 번씩만 방문함 (문자열의 길이: n)
8
+
9
+ Space Complexity: O(n)
10
+ - dp 배열의 크기는 n+1
11
+ - 이 배열 이외에 추가 공간을 사용하지 않음
12
+
13
+ Base cases:
14
+ - If string is empty or starts with '0', return 0 (impossible to decode)
15
+
16
+ Dynamic Programming approach:
17
+ - dp[i] represents the number of ways to decode first i characters
18
+ - dp array has size n+1 to include the empty string case (dp[0])
19
+ - For each position, consider both single-digit and two-digit decodings
20
+
21
+ 메모:
22
+ - 다른 dp문제 풀어보기
23
+ """
24
+ class Solution :
25
+ def numDecodings (self , s : str ) -> int :
26
+ if not s or s [0 ] == '0' :
27
+ return 0
28
+
29
+ n = len (s )
30
+ dp = [0 ] * (n + 1 )
31
+ dp [0 ] = 1
32
+
33
+ for i in range (1 , n + 1 ):
34
+ if s [i - 1 ] != '0' :
35
+ dp [i ] += dp [i - 1 ]
36
+
37
+ if i > 1 and s [i - 2 ] != '0' :
38
+ two_digit = int (s [i - 2 :i ])
39
+ if 1 <= two_digit <= 26 :
40
+ dp [i ] += dp [i - 2 ]
41
+
42
+ return dp [n ]
Original file line number Diff line number Diff line change
1
+ """
2
+ Conditions:
3
+ - 1 <= nums.length <= 10^5
4
+ - -10^4 <= nums[i] <= 10^4
5
+
6
+ Time Complexity: O(n)
7
+ - 배열을 한 번만 순회함
8
+
9
+ Space Complexity: O(1)
10
+ - 두 변수 이외에 추가 공간을 사용하지 않음
11
+
12
+ 풀이방법:
13
+ 1. Base case: If nums is empty, return 0
14
+ 2. Initialize variables (current_sum and max_sum as the first value in the array)
15
+ 3. Traverse from the value at 1st index to the last, update current_sum
16
+ - Decide whether to add the current value (num) to the existing subarray or start a new one
17
+ 4. Update max_sum
18
+ - Choose the larger between the updated current_sum and the previous max_sum
19
+ """
20
+ class Solution :
21
+ def maxSubArray (self , nums : List [int ]) -> int :
22
+ if not nums :
23
+ return 0
24
+
25
+ current_sum = max_sum = nums [0 ]
26
+
27
+ for num in nums [1 :]:
28
+ current_sum = max (num , current_sum + num )
29
+ max_sum = max (current_sum , max_sum )
30
+
31
+ return max_sum
Original file line number Diff line number Diff line change 1
1
"""
2
- Title: 215. Valid Palindrome
3
- Link: https://leetcode.com/problems/valid-palindrome/
4
-
5
- Summary:
6
- - Palindrome이라면 True, 아니라면 False를 반환하는 문제.
7
- - Palindrome이란, 앞으로 읽어도 뒤에서부터 읽어도 동일한 단어를 뜻함.
8
- - 추가 조건: 대소문자를 구분하지 않으며, 알파벳과 숫자 이외의 문자는 제거해야 함.
9
- - e.g. racecar
10
-
11
2
Conditions:
12
- - 입력 문자열이 Palindrome인 경우: `True` 반환
13
- - Palindrome이 아닌 경우: `False` 반환
3
+ - 1 <= s.length <= 2 * 10^5
4
+ - s consists only of printable ASCII characters.
5
+
6
+ <Solution 1>
7
+ Time Complexity: O(n)
14
8
15
- Time Complexity:
16
- - O(n)
17
- Space Complexity:
18
- - O(n)
9
+ Space Complexity: O(n)
19
10
"""
20
11
class Solution :
21
12
def isPalindrome (self , s : str ) -> bool :
22
13
s = re .sub (r'[^a-zA-z0-9]' , '' , s ).lower ()
23
14
if s == s [::- 1 ]:
24
15
return True
25
16
return False
17
+ """
18
+ <Solution 2>
19
+ Time Complexity: O(n)
20
+ - 팰린드롬일 경우, 각 문자를 한 번씩 검사
21
+
22
+ Space Complexity: O(1)
23
+ - left, right 포인터 외에 추가 공간 사용 없음
24
+ """
25
+ class Solution :
26
+ def isPalindrome (self , s : str ) -> bool :
27
+ left , right = 0 , len (s ) - 1
28
+
29
+ while left < right :
30
+ while left < right and not s [left ].isalnum ():
31
+ left += 1
32
+
33
+ while left < right and not s [right ].isalnum ():
34
+ right -= 1
35
+
36
+ if s [left ].lower () != s [right ].lower ():
37
+ return False
38
+
39
+ left += 1
40
+ right -= 1
26
41
42
+ return True
You can’t perform that action at this time.
0 commit comments