24. Buy Maximum Stocks if i stocks can be bought on i-th day
Last updated
Was this helpful?
Last updated
Was this helpful?
The problem can be found at the following link:
To solve this problem, I used a greedy approach.
I first created a vector of pairs, where each pair represents the stock price and its corresponding day.
Then, I sorted this vector based on stock prices in ascending order.
Next, I iterated through the sorted vector, calculating the maximum number of stocks that can be bought on each day without exceeding the available budget.
I kept track of the total number of stocks bought, and the remaining budget after each purchase.
Time Complexity: The time complexity is dominated by the sorting operation, making it O(n log n)
, where 'n' is the number of stocks.
Auxiliary Space Complexity: O(n)
, where 'n' is the number of stocks, for storing the vector of pairs.
For discussions, questions, or doubts related to this solution, please visit our . We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star
to the repository.