mediumArraysPattern: General
Maximal Stock Return Solution
Problem Statement
You are given an array of integers `stockPrices` representing the current prices of stocks and an array of floats `growthRates` representing their respective growth rates. Determine the index of the stock that will yield the highest returns if you select at most one stock.
Constraints
- The input array will contain at least 1 and at most 20 stock-price pairs.
- Each stock price will be a positive integer between 1 and 1000.
- Each growth rate will be a decimal value between 0.01 and 0.1.
- The output should be an array of at most one index corresponding to the selected stock.
- The budget for investment can be considered unlimited for simplicity.
Time: O(n) Space: O(1)
The optimized approach involves iterating through the array once to find the stock with the highest growth rate, resulting in a linear time complexity of O(n).
Run, Test & Submit Code
Ready to practice this challenge? Launch our interactive compilation environment with compiler validation.
Solve on Interactive WorkspaceTested Solutions
def optimal_investment_portfolio(stocks):
max_growth_rate = float('-inf')
max_growth_rate_index = -1
for i, stock in enumerate(stocks):
if stock[1] > max_growth_rate:
max_growth_rate = stock[1]
max_growth_rate_index = i
return [max_growth_rate_index]