浏览代码

add max_profit

yan chuanli 2 年之前
父节点
当前提交
0ac0c7a7a7
共有 1 个文件被更改,包括 14 次插入0 次删除
  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
+
+
+