← 返回 ramp 的题目列表Maximum Profit from Historical Stock Prices
类型:online_judge
Problem: Maximum Profit from Historical Stock Prices
You are given multiple historical stock price records. Each record contains:
date: a date in YYYY-MM-DD format
stock: the stock symbol/name
price: the stock price on that date
You may complete at most one transaction: buy one stock first, then sell the same stock on a later date. The buy date must be strictly earlier than the sell date.
Return the maximum possible profit among all valid transactions across all stocks.
If no profitable transaction exists, return 0.
Input Format
n
date_1 stock_1 price_1
date_2 stock_2 price_2
...
date_n stock_n price_n
Notes:
n is the number of price records.
price_i is a non-negative integer.
Records are not guaranteed to be sorted by date.
The same stock may appear on multiple dates.
Output Format
Print one integer: the maximum profit.
Constraints
1 <= n <= 200000
0 <= price_i <= 10^9
Stock symbol length is at most 30.
Dates are valid date strings.
Example
Input:
6
2024-01-03 AAPL 110
2024-01-01 AAPL 100
2024-01-02 AAPL 90
2024-01-01 MSFT 200
2024-01-02 MSFT 180
2024-01-03 MSFT 250
Output:
70
Explanation: Buy MSFT on 2024-01-02 at 180, then sell it on 2024-01-03 at 250, for a profit of 70.
Example
Input
6
2024-01-03 AAPL 110
2024-01-01 AAPL 100
2024-01-02 AAPL 90
2024-01-01 MSFT 200
2024-01-02 MSFT 180
2024-01-03 MSFT 250
Output
70