Skip to content

Commit

Permalink
CHORE : changed file and added comment for group-anagrams
Browse files Browse the repository at this point in the history
  • Loading branch information
aa601 committed Jan 9, 2025
1 parent c4af0e7 commit c8e3ac8
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions group-anagrams/aa601.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
# ์‹œ๊ฐ„๋ณต์žก๋„ O(n)
# ๊ณต๊ฐ„๋ณต์žก๋„ O(1)
#์‹œ๊ฐ„๋ณต์žก๋„ O(n * klogk)
#๊ณต๊ฐ„๋ณต์žก๋„ O(n * k)
# => n๊ฐœ์˜ ๋‹จ์–ด * ๊ฐ ๋‹จ์–ด์˜ k๋งŒํผ์˜ ๊ธธ์ด
class Solution:
def maxProfit(self, prices: list[int]) -> int:
min_p = prices[0] # ์ตœ์†Œ ๊ฐ€๊ฒฉ ์„ค์ • : ๋ฐฐ์—ด์˜ ์ฒซ ๋ฒˆ์งธ ๊ฐ€๊ฒฉ
cur = 0
max_p = 0
for n in prices:
if n < min_p: # ํ˜„์žฌ ๊ฐ€๊ฒฉ์ด ์ตœ์†Œ ๊ฐ€๊ฒฉ๋ณด๋‹ค ์ž‘๋‹ค๋ฉด ์ตœ์†Œ๊ฐ€๊ฒฉ ๊ฐฑ์‹ 
min_p = n
cur = n - min_p # ํ˜„์žฌ ์ด์ต ๊ณ„์‚ฐ
if max_p < cur: # ํ˜„์žฌ ์ด์ต๊ณผ ์ตœ๋Œ€๋กœ ์–ป์„ ์ˆ˜ ์žˆ๋Š” ์ด์ต ๋น„๊ต
max_p = cur # ์ตœ๋Œ€ ์ด์ต ๊ฐฑ์‹ ์‹ 

return max_p

def groupAnagrams(self, strs: list[str]) -> list[list[str]]:
ans = {}

for word in strs:
# ๋‹จ์–ด์˜ ๋ฌธ์ž๋ฅผ ์ •๋ ฌ ํ›„ ํ‚ค๋กœ ์‚ฌ์šฉ
sortedWord = ''.join(sorted(word)) # sorted()์˜ ์‹œ๊ฐ„๋ณต์žก๋„ : O(klogk)
# ์ดˆ๊ธฐ ๋ฆฌ์ŠคํŠธ ์ƒ์„ฑ
if sortedWord not in ans:
ans[sortedWord] = []
ans[sortedWord].append(word)

# ๋”•์…”๋„ˆ๋ฆฌ์˜ value๋ฅผ list๋กœ ๋ณ€ํ™˜
ansLst = list(ans.values())
return ansLst

0 comments on commit c8e3ac8

Please sign in to comment.