Skip to content
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
12 changes: 12 additions & 0 deletions best-time-to-buy-and-sell-stock/njngwn.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
  • 설명: 최소값 추적과 현재 값과의 차이를 최대화하는 단일 패스 워크루프 구조로, 최적의 이익을 곱셈 없이 즉시 계산하는 그리디 패턴과 순방향 탐색의 조합이다.

📊 시간/공간 복잡도 분석

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

풀이 1: Solution.maxProfit — Time: ✅ O(n) → O(n) / Space: ✅ O(1) → O(1)
유저 분석 실제 분석 결과
Time O(n) O(n)
Space O(1) O(1)

피드백: 최대 이익은 현재 최소가 대비 차이를 비교해 갱신하고, 한 번의 for 루프로 전체 가격을 탐색한다.

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

풀이 2: Solution.climbStairs — Time: O(n) / Space: O(1)
복잡도
Time O(n)
Space O(1)

피드백: 상수 공간에 두 개의 이전 값을 저장하고 순회하며 계산한다.

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

풀이 3: Solution.encode — Time: ✅ O(n) → O(n) / Space: O(1)
유저 분석 실제 분석 결과
Time O(n) O(n)
Space - O(1) -

피드백: 각 문자열마다 길이 접두사를 추가하는 방식으로 안전하게 구분한다.

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

풀이 4: Solution.decode — Time: O(n) / Space: O(1)
복잡도
Time O(n)
Space O(1)

피드백: 정확하게 파싱하여 문자열 리스트를 재구성한다.

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

풀이 5: Solution.groupAnagrams — Time: ❌ O(n) → O(n * k log k) / Space: ❌ O(1) → O(n)
유저 분석 실제 분석 결과
Time O(n) O(n * k log k)
Space O(1) O(n)

피드백: 정렬 기반 키 생성으로 아나그램을 그룹화한다.

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

풀이 6: Solution.isAnagram — Time: ❌ O(n) → O(n + m) / Space: O(1)
유저 분석 실제 분석 결과
Time O(n) O(n + m)
Space - O(1) -

피드백: Counter를 이용한 간단하고 직관적인 비교이다.

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

풀이 7: Solution.wordBreak — Time: ❌ O(n) → O(n * average_word_length) / Space: ❌ O(1) → O(n)
유저 분석 실제 분석 결과
Time O(n) O(n * average_word_length)
Space O(1) O(n)

피드백: 메모이제이션으로 중복 서브 문제를 제거하며 탐색한다.

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

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution:
# Time Complexity: O(n), n: len(prices)
# Space Complexity: O(1)
def maxProfit(self, prices: List[int]) -> int:
min_price = prices[0]
profit = 0

for i in range(1, len(prices)):
min_price = min(min_price, prices[i])
profit = max(profit, prices[i] - min_price)

return profit
12 changes: 12 additions & 0 deletions climbing-stairs/njngwn.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
  • 설명: 주어진 코드는 피보나치 계열로 n번째 값을 점화식으로 계산하고, bottom-up 방식의 DP를 활용하여 시간 복잡도 O(n), 공간 복잡도 O(1)로 풀이합니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution:
# dynamic programming with bottom-up
# Time Complexity: O(n)
# Space Complexity: O(1)
def climbStairs(self, n: int) -> int:
if n <= 2: return n
prev1, prev2 = 2, 1

for i in range(3, n+1):
prev1, prev2 = prev2 + prev1, prev1

return prev1
26 changes: 26 additions & 0 deletions encode-and-decode-strings/njngwn.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, Hash Map / Hash Set, Two Pointers
  • 설명: 문자열 인코딩/디코딩에서 각 문자열을 길이와 구분자 '#'로 구분해 저장하고, 이를 순차적으로 파싱하는 방식으로 진행됩니다. 인코딩은 순회로, 디코딩은 포인터를 이동하며 원하는 부분을 잘라내는 구조로 구성되어 있습니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Solution:

def encode(self, strs: List[str]) -> str:
encoded = ''

for s in strs:
encoded += str(len(s)) + '#' + s

return encoded

def decode(self, s: str) -> List[str]:
decoded = []

i = 0
while i < len(s):
j = i # j is a pointer for beginning of string
# find '#' in string
while s[j] != '#':
j += 1

length = int(s[i:j])
word = s[j + 1:j + length + 1]
decoded.append(word)
i = j + length + 1

return decoded
14 changes: 14 additions & 0 deletions group-anagrams/njngwn.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.

🏷️ 알고리즘 패턴 분석

  • 패턴: Hash Map / Hash Set, Greedy
  • 설명: 문자열을 정렬한 결과를 키로 삼아 그룹화하는 방식으로 해시맵에 분류하는 패턴이다. 각 단어를 정렬한 뒤 같은 정렬 문자열을 가진 원소를 모으는 전형적인 해시 맵 기반 그룹화이다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution:
# Time Complexity: O(n), n: len(strs)
# Space Complexity: O(n), n: len(strs)
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
anagram_groups = dict() # key: string in an alphabetical order, values: strings

for s in strs:
word = ''.join(sorted(s))
if word in anagram_groups:
anagram_groups[word].extend([s])
else:
anagram_groups[word] = [s]

return list(anagram_groups.values())
7 changes: 7 additions & 0 deletions valid-anagram/njngwn.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.

🏷️ 알고리즘 패턴 분석

  • 패턴: Hash Map / Hash Set, Counter / Multiset
  • 설명: 두 문자열의 문자 빈도를 비교해 같은지 여부를 판단하는 방식으로, 해시 맵의 카운터를 이용한 비교 패턴에 해당합니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from collections import Counter

class Solution:
# Time Complexity: O(n), n: max(len(s), len(t))
# Space Complexity: O(k), k: number of letters
def isAnagram(self, s: str, t: str) -> bool:
return Counter(s) == Counter(t)
16 changes: 16 additions & 0 deletions word-break/njngwn.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, Backtracking
  • 설명: 해당 코드는 재귀와 캐시를 이용해 부분 문자열로 전체를 구성 가능한지 탐색하며, 중복 호출을 제거하는 방식으로 문제를 다룹니다. 부분 문제를 해결해 재귀적으로 연결하고 결과를 저장하여 효율을 높이는 다이나믹 프로그래밍/백트래킹의 조합에 속합니다.

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from functools import cache


class Solution:
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
@cache
def check(cur):
if cur == len(s):
return True
for word in wordDict:
if s[cur: cur + len(word)] == word:
if check(cur + len(word)):
return True
return False

return check(0)
Loading