Skip to content
51 changes: 51 additions & 0 deletions combination-sum/alphaorderly.py

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏷️ 알고리즘 패턴 분석

  • 패턴: Backtracking, Dynamic Programming
  • 설명: 주석으로 보듯 최초 구현은 백트래킹으로 모든 조합을 탐색하는 방식이며, 두 번째 구현은 각 합에 대한 부분해를 저장하는 동적계획(DP) 접근으로 문제를 해결합니다. 두 가지 패턴이 혼합되어 코드에 반영되어 있습니다.

📊 시간/공간 복잡도 분석

ℹ️ 이 파일에는 2가지 풀이가 포함되어 있어 각각 분석합니다.

풀이 1: Solution.combinationSum — Time: O(n * target) / Space: O(target)
복잡도
Time O(n * target)
Space O(target)

피드백: 목적 합이 target에 도달하도록 각 합에 대한 부분 해결책들을 저장하고, 이를 이용해 최종 결과를 구성한다.

개선 제안: 현재 구현이 효과적입니다.

풀이 2: Solution.combinationSum — Time: O(N^target) / Space: O(target)
복잡도
Time O(N^target)
Space O(target)

피드백: 깊은 탐색으로 모든 조합을 시도하므로 지수적 시간 복잡도가 발생할 수 있다.

개선 제안: 현재 구현이 적절해 보입니다.

💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!

Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@

"""
Time Complexity: O(N^target)
Space Complexity: O(target)

Classic backtracking approach.
- Use a helper function to backtrack and generate all possible combinations.
"""
# class Solution:
# def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
# N = len(candidates)
# candidates.sort()

# ans = []
# app = []

# def backtracking(last: int, left: int) -> None:
# if left == 0:
# ans.append(app[:])
# return

# for i in range(last, N):
# if candidates[i] > left:
# continue

# app.append(candidates[i])
# backtracking(i, left - candidates[i])
# app.pop()

# backtracking(0, target)

# return ans

"""
Time Complexity: O(N * target)
Space Complexity: O(target)

- Use a dynamic programming approach to store the combinations that sum to each target.
- dp[j] is a list of lists that sum to j.
"""
class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
dp = [list() for _ in range(target + 1)]
dp[0] = [[]]

for c in candidates:
for j in range(c, target + 1):
for partial in dp[j - c]:
dp[j].append(partial + [c])

return dp[-1]
31 changes: 31 additions & 0 deletions decode-ways/alphaorderly.py

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏷️ 알고리즘 패턴 분석

  • 패턴: Dynamic Programming, Two Pointers, Hash Map / Hash Set
  • 설명: 문자열의 각 위치까지의 해를 누적적으로 계산하는 DP 패턴으로, 두 문자 조합과 한 문자 해독여부를 상태로 활용하여 순차적으로 해결한다.

📊 시간/공간 복잡도 분석

복잡도
Time O(n)
Space O(1)

피드백: 두 가지 경우를 분리해 누적 가능한 방법의 수를 갱신한다.

개선 제안: 현재 구현이 적절해 보입니다.

💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""
Time Complexity: O(N)
Space Complexity: O(1)

prev_2 : number of ways to decode the string ending with the previous two characters
prev_1 : number of ways to decode the string ending with the previous character
new_prev : number of ways to decode the string ending with the current character
"""
class Solution:
def numDecodings(self, s: str) -> int:
N = len(s)

if s[0] == "0":
return 0

if N == 1:
return 1

prev_2 = 1
prev_1 = int(1 <= int(s[0] + s[1]) <= 26) + int(s[1] != "0")

for i in range(2, N):
new_prev = 0
if 1 <= int(s[i - 1] + s[i]) <= 26 and s[i - 1] != "0":
new_prev += prev_2
if s[i] != "0":
new_prev += prev_1

prev_2, prev_1 = prev_1, new_prev

return prev_1
71 changes: 71 additions & 0 deletions maximum-subarray/alphaorderly.py

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏷️ 알고리즘 패턴 분석

  • 패턴: Dynamic Programming, Greedy
  • 설명: 두 번째 구현은 Kadane 알고리즘으로 연속 부분배열의 최댓값을 구하는 패턴이다. 이전 합과 현재 값을 비교해 누적하며 최댓값을 갱신하므로 DP의 최적부분구조와 그리디 성격이 혼합된 형태로 분류된다.

📊 시간/공간 복잡도 분석

복잡도
Time O(n)
Space O(1)

피드백: 현재 구현은 선형 시간에 최댓값을 유지하는 표준 방법이다.

개선 제안: 현재 구현이 적절해 보입니다.

💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!

Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@


"""
Time Complexity: O(NLogN)
Space Complexity: O(N) ( Reason: Recursive stack pointer for every element )

Divide and Conquer Approach

1. Divide the array into two halves
2. Find the maximum subarray sum in the left half
3. Find the maximum subarray sum in the right half
4. Find the maximum subarray sum that crosses the midpoint
5. Return the maximum of the three sums
"""
# class Solution:
# def maxSubArray(self, nums: List[int]) -> int:

# def divide(start: int, end: int) -> int:
# if start >= end:
# return nums[start]

# mid = (start + end) // 2

# left = divide(start, mid)
# right = divide(mid + 1, end)

# left_largest = -float('inf')
# right_largest = -float('inf')

# left_acc = 0
# right_acc = 0

# left_index = mid
# right_index = mid + 1

# while left_index >= start:
# left_acc += nums[left_index]
# left_largest = max(left_largest, left_acc)
# left_index -= 1

# while right_index <= end:
# right_acc += nums[right_index]
# right_largest = max(right_largest, right_acc)
# right_index += 1

# return max(left_largest + right_largest, left, right)

# return divide(0, len(nums) - 1)




"""
Time Complexity: O(N)
Space Complexity: O(1)

prev : maximum sum of the subarray ending with the previous element
ans : maximum sum of the subarray
curr : maximum sum of the subarray ending with the current element
"""
class Solution:
def maxSubArray(self, nums: List[int]) -> int:
N = len(nums)
prev = nums[0]
ans = nums[0]

for i in range(1, N):
prev = max(nums[i], prev + nums[i])
ans = max(ans, prev)

return ans
8 changes: 8 additions & 0 deletions number-of-1-bits/alphaorderly.py

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏷️ 알고리즘 패턴 분석

  • 패턴: Bit Manipulation
  • 설명: 주어진 코드는 이진수의 각 자릿수를 비트 연산으로 확인해 1의 개수를 세는 방법으로, 비트 조작을 이용한 패턴이다. while 루프로 n의 모든 비트를 순회하며 1의 개수를 누적한다.

📊 시간/공간 복잡도 분석

복잡도
Time O(number_of_bits)
Space O(1)

피드백: 단순 비트 순회 방식으로 동작한다.

개선 제안: 현재 구현이 적절해 보입니다.

💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Solution:
def hammingWeight(self, n: int) -> int:
ans = 0
while n:
ans += n & 1
n >>= 1
return ans

6 changes: 6 additions & 0 deletions valid-palindrome/alphaorderly.py

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏷️ 알고리즘 패턴 분석

  • 패턴: Greedy, Two Pointers, Hash Map / Hash Set
  • 설명: 문자열 전처리 후 양 끝에서 비교하는 방식으로 대칭 여부를 판단하는 패턴은 Two Pointers의 기본 아이디어에 해당하며, 불필요한 문자 제거로 단일 문자열을 구성하는 과정에서 문자열 비교를 수행하는 점이 Greedy의 일반적 접근과도 맞닿습니다. 또한 알파벳과 숫자 확인에 대한 조건 검사로 Hash Map/Hash Set의 직접 활용은 없지만 문자 필터링 단계에서 집합적 판단이 포함됩니다.

📊 시간/공간 복잡도 분석

복잡도
Time O(n)
Space O(n)

피드백: 전처리와 대칭 비교를 통해 회문 여부를 판단한다.

개선 제안: 현재 구현이 적절해 보입니다.

💡 풀이에 시간/공간 복잡도를 주석으로 남겨보세요!

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Solution:
def isPalindrome(self, s: str) -> bool:
processed = "".join(ch.lower() for ch in s if ch.isalnum())

return processed == processed[::-1]

Loading