Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
13 changes: 13 additions & 0 deletions best-time-to-buy-and-sell-stock/dolphinflow86.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.

깔끔하게 잘 해결해 주셨네요!
best time to buy and sell stock, 즉 해당 문제는
뒤에 로마 숫자를 붙혀서 1, 2, 3, 4, 5 총 다섯종류가 있는데요
dp 연습하기에 정말 괜찮은 문제라고 생각해서
2번문제
II는 한번 풀어보시길 추천드려요!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

오! 네 문제 추천 감사합니다! 한번 풀어볼게요 👍

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.

🏷️ 알고리즘 패턴 분석

  • 패턴: Two Pointers, Greedy, Hash Map / Hash Set
  • 설명: 주어진 코드는 단순히 현재 최소가를 유지하며 가격을 순회하면서 최대 이익을 갱신하는 방식으로 구현되어 있습니다. 한 번의 순회로 최적해를 구하는 그리디 접근이며, 투 포인터의 개념으로 좌우의 값을 관리합니다.

📊 시간/공간 복잡도 분석

유저 분석 실제 분석 결과
Time O(N) O(n)
Space O(1) O(1)

피드백: 한 번의 순회를 통해 현재 가격과 최소 가격을 비교하며 최대 이익을 갱신합니다.

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# 1) Keep track of local minimum and use local minimum to update max profile while interating prices.
# TC: O(N) where N is the length of prices
# SC: O(1)
class Solution:
def maxProfit(self, prices: List[int]) -> int:
min_price = prices[0]
max_profit = 0

for price in prices:
max_profit = max(max_profit, price - min_price)
min_price = min(min_price, price)
Comment on lines +9 to +11

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.

사소하지만 더 최적화 할 수 있는 부분은 min, max 일 거 같아요. 고민해보셔도 좋을 거 같습니다!

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.

같은 의견입니다! min, max가 생각보다 비용이 크더라구요

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

두분 의견 감사합니다! if문으로 인라인 처리하면 함수 호출 오버헤드 등을 줄일 수 있겠네요.


return max_profit
13 changes: 13 additions & 0 deletions group-anagrams/dolphinflow86.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, Divide and Conquer
  • 설명: 문자열의 정렬 결과를 키로 사용해 해시 맵에 그룹화하는 방식으로, 키 생성과 묶음은 해시 맵 기반의 카운트/그룹화 패턴에 해당합니다. 각 문자열을 정렬해 동일한 키를 모으는 전형적인 해시 맵 활용 예시입니다.

📊 시간/공간 복잡도 분석

복잡도
Time O(n * k log k)
Space O(n)

피드백: 각 문자열을 정렬하는 비용이 주요 요인이며, 해시 맵으로 묶는 비효율 없이 처리합니다.

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

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

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.

리뷰 누락이 있어서 추가로 남깁니다.

  1. str은 파이썬 클래스와 혼동이 있을 수 있어서 가급적이면 변수명으로 쓰지 않는 것이 좋을 것 같습니다.
  2. 저도 리뷰 받았던 내용인데, sorted의 정렬 대신 다르게 구현하여 시간 복잡도를 개선해보면 좋을 것 같습니다.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

c++ 습관이 자꾸 나오네요 ㅎㅎ str은 사용하지 않아야겠습니다. 두번째 방법도 한번 생각해볼게요! 꼼꼼하게 리뷰해주셔서 감사합니다 🙏

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# 1) Group words by their sorted form using defaultdict. While iterating the strs, sort each word and append original word to the corresponding list. After then convert dict to 2 dimensional list and return the list.
# TC: O(N*LlogL) where N is length of strs, L is max length of a word.
# SC: O(N*L)

class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
groups = defaultdict(list)

for str in strs:
sorted_str = "".join(sorted(str))
groups[sorted_str].append(str)

return list(groups.values())
Loading