Skip to content

Commit 2940e67

Browse files
authored
Merge pull request #867 from Jeldo/main
[jeldo3] Week5
2 parents f1f3e4e + 9baac3a commit 2940e67

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
# O(n), n = len(prices)
3+
def maxProfit(self, prices: List[int]) -> int:
4+
min_price = float('inf')
5+
profit = 0
6+
for price in prices:
7+
min_price = min(min_price, price)
8+
profit = max(profit, price - min_price)
9+
return profit

group-anagrams/jeldo.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from collections import defaultdict
2+
3+
4+
class Solution:
5+
# O(n*mlogm), n = len(str), m = the length of the longest string
6+
def groupAnagrams(self, strs: list[str]) -> list[list[str]]:
7+
group = defaultdict(list)
8+
for s in strs:
9+
group[''.join(sorted(s))].append(s)
10+
return list(group.values())

0 commit comments

Comments
 (0)