Browse Source

add max_profit

yan chuanli 2 years ago
parent
commit
0ac0c7a7a7
1 changed files with 14 additions and 0 deletions
  1. 14 0
      array/max_profit.py

+ 14 - 0
array/max_profit.py

@@ -0,0 +1,14 @@
+from typing import List
+import heapq
+
+def maxProfit(prices: List[int]) -> int:
+    inf = int(1e9)
+    min_price = inf
+    max_profit = 0
+    for price in prices:
+        max_profit = max(price - min_price, max_profit)
+        min_price = min(price, min_price)
+    return max_profit
+
+
+