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
11 changes: 11 additions & 0 deletions best-time-to-buy-and-sell-stock/prograsshopper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Solution:
def maxProfit(self, prices: List[int]) -> int:
max_profit = 0
buy_price = float('inf')

for price in prices:
buy_price = min(buy_price, price)
current_profit = price - buy_price
max_profit = max(max_profit, current_profit)

return max_profit
28 changes: 28 additions & 0 deletions group-anagrams/prograsshopper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
# sol 1 -> Time Exceed
from collections import defaultdict
word_dict = defaultdict(list)

for string in strs:
exist = False
for elem in word_dict.keys():
if sorted(elem) == sorted(string):
word_dict[elem].append(string)
exist = True
break
if not exist:
word_dict[string] = [string, ]
result = []
for elem in word_dict.values():
result.append(elem)
return result

# sol 2
# Time Complexity O(mn)
result = defaultdict(list)

for string in strs:
key = "".join(sorted(string))
result[key].append(string)
return list(result.values())