-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CHORE : changed file and added comment for group-anagrams
- Loading branch information
Showing
1 changed file
with
16 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|