Given a list of historic stock prices,
write a function that tells when you should have bought and sold to make the most profit.
You can only buy and sell once.
Example input:
[
{date: '2024-02-28', price: 130.0},
{date: '2024-03-01', price: 150.0},
{date: '2024-03-02', price: 132.0},
{date: '2024-03-03', price: 147.0},
{date: '2024-03-04', price: 110.0},
{date: '2024-03-05', price: 199.0},
{date: '2024-03-06', price: 117.0},
{date: '2024-03-07', price: 100.0}
]
Goal:
Return the best buy date and best sell date to maximize profit.
一次遍历:维护当前最小价格,同时计算 当前价 - 最低价 的利润并更新最大利润,记录最佳买卖日期。
VOprep 团队长期陪同学员实战各类大厂 OA 与 VO,包括 Google、Amazon、Citadel、SIG 等,提供实时答案助攻、远程陪练与面试节奏提醒,帮助大家在关键时刻不卡壳。
如果你也在准备公司,可以了解一下我们的定制助攻方案——从编程面到系统设计,全程护航上岸。
正文完