The core insight is the same as the classic 'best time to buy and sell stock with unlimited transactions' problem: just sum up every positive day-over-day price increase.
Recognize that since stocks are independent and you can hold multiple simultaneously, the maximum total profit is the sum of maximum profits for each stock individually. For each stock, sort its records by date, then apply the standard 'best time to buy and sell stock II' greedy algorithm: sum all positive price differences between consecutive days. This yields O(N log N) time due to sorting, or O(N) if records are already grouped and sorted per stock.
Pro tip: Clarify upfront that the 'one unit per stock' constraint means each stock's transactions are independent, so you can solve per stock and sum. Also mention that if the input is already sorted by date, you can avoid the sort and achieve O(N) time.
Confirm that transactions across different stocks are independent, that you can hold multiple stocks at once, and that you must end with no holdings. Ask if the input is sorted by date or if you need to sort it.
Use a hash map to group all records by ticker, so you can process each stock's price series independently.
For each stock, sort its records chronologically. If the input is already sorted, this step can be skipped.
Iterate through the sorted prices and add the positive difference between each consecutive pair. This captures all profitable upswings.
Accumulate the per-stock profits to get the maximum total profit, ensuring you start and end with no holdings.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.