Skip to content

Commit 95ae29e

Browse files
authored
Update Best Time to Buy and Sell Stock - Leetcode 121.py
1 parent b406411 commit 95ae29e

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

Best Time to Buy and Sell Stock - Leetcode 121.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
class Solution:
2+
def maxProfit(self, prices: List[int]) -> int:
3+
max_profit = float('-inf')
4+
for i in range(len(prices)):
5+
for j in range(i+1, len(prices)):
6+
profit = prices[j] - prices[i]
7+
8+
if profit > 0:
9+
max_profit = max(max_profit, profit)
10+
11+
return max_profit if max_profit > float('-inf') else 0
12+
# Time: O(N^2) (Brute Force)
13+
# Space: O(1)
14+
15+
116
class Solution:
217
def maxProfit(self, prices: List[int]) -> int:
318
# Time: O(n)

0 commit comments

Comments
 (0)