← 返回 ramp 的题目列表Maximum Profit from Stock Prices by Date and Ticker
类型:online_judge
Problem: Calculate Maximum Profit from Stock Prices by Date and Ticker
You are given a list of stock price records. Each record contains:
date: a date string in YYYY-MM-DD format
ticker: a stock name/symbol without spaces
price: the stock price on that date, represented as a non-negative integer
You may complete at most one transaction:
Choose one stock;
Buy it on one date;
Sell it on a later date;
The buy and sell must be for the same stock;
The sell date must be strictly later than the buy date.
Return the maximum possible profit across all stocks. If no positive profit is possible, return 0.
The input records may not be sorted by date, so you must handle chronological ordering correctly.
Input Format
n
date_1 ticker_1 price_1
date_2 ticker_2 price_2
...
date_n ticker_n price_n
Constraints:
1 <= n <= 200000
date_i is a valid date string in YYYY-MM-DD format
ticker_i contains only letters, digits, and underscores, and has length at most 20
0 <= price_i <= 10^9
Output Format
Print one integer: the maximum profit.
Example 1
6
2024-01-03 AAPL 10
2024-01-01 AAPL 5
2024-01-02 AAPL 8
2024-01-01 MSFT 20
2024-01-04 MSFT 25
2024-01-03 MSFT 18
Output:
7
Explanation: Buy MSFT at 18 and sell later at 25, giving profit 7.
Example 2
4
2024-01-01 AAPL 10
2024-01-02 AAPL 7
2024-01-03 AAPL 5
2024-01-04 AAPL 3
Output:
0
Example
Input
6
2024-01-03 AAPL 10
2024-01-01 AAPL 5
2024-01-02 AAPL 8
2024-01-01 MSFT 20
2024-01-04 MSFT 25
2024-01-03 MSFT 18
Output
7