← 返回 ramp 的题目列表Maximum Profit from Unordered Stock Price Records
类型:online_judge
Problem: Compute Maximum Profit from Unordered Stock Price Records
You are given n stock price records. Each record contains:
date: a date in YYYY-MM-DD format
stock: a stock symbol without spaces
price: the price of that stock on that date, represented as a positive integer
The records are not guaranteed to be sorted by date.
You may complete at most one transaction:
Buy one share of one stock on some day;
Sell the same stock on a later day;
You must buy before selling;
Short selling is not allowed;
If no positive profit can be made, the profit is 0.
Compute the maximum profit obtainable over all stocks and dates using at most one transaction.
Input Format
n
date_1 stock_1 price_1
date_2 stock_2 price_2
...
date_n stock_n price_n
Output Format
Output one integer: the maximum obtainable profit.
Constraints
1 <= n <= 200000
date is in YYYY-MM-DD format
stock has length from 1 to 20
1 <= price <= 10^9
The same stock may appear on multiple different dates
Input records are not guaranteed to be sorted
Example
Input:
5
2024-01-03 AAPL 120
2024-01-01 AAPL 100
2024-01-02 AAPL 90
2024-01-04 AAPL 150
2024-01-01 MSFT 200
Output:
60
Explanation: Buy AAPL at 90 on 2024-01-02 and sell it at 150 on 2024-01-04, gaining profit 60.
Example
Input
5
2024-01-03 AAPL 120
2024-01-01 AAPL 100
2024-01-02 AAPL 90
2024-01-04 AAPL 150
2024-01-01 MSFT 200
Output
60