Maximal Stock Return — Problem Statement & Solution Guide
Problem Description
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 indices of the stocks that will yield the highest returns if you select at most one stock.
Examples
Input
[10, 20, 30, 40, 50], [0.1, 0.2, 0.3, 0.4, 0.5]
Output
[4]
Explanation: Step-by-step: with input X, we first calculate the returns for each stock by multiplying the price with the growth rate. Then, we find the maximum return and its index. Since there is only one stock with the maximum return, we return its index.
Input
[10, 20, 30, 40, 50], [0.1, 0.1, 0.1, 0.1, 0.1]
Output
[0, 1, 2, 3, 4]
Explanation: Step-by-step: with input X, we first calculate the returns for each stock by multiplying the price with the growth rate. Then, we find the maximum return and its indices. Since all stocks have the same maximum return, we return all their indices.
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.
Optimal Approach & Strategy
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).
Brute Force Approach
The brute force approach would involve comparing each stock's growth rate to every other stock, resulting in a highly inefficient solution.
Verified Code Solutions
function optimalInvestmentPortfolio(stocks) {
let maxReturn = -Infinity;
let maxReturnIndices = [];
for (let i = 0; i < stocks.length; i++) {
let returnOnInvestment = stocks[i][0] * stocks[i][1];
if (returnOnInvestment > maxReturn) {
maxReturn = returnOnInvestment;
maxReturnIndices = [i];
} else if (returnOnInvestment === maxReturn) {
maxReturnIndices.push(i);
}
}
return maxReturnIndices;
}#include <iostream>
#include <vector>
using namespace std;
int optimalInvestmentPortfolio(vector<vector<double>>& stocks) {
double maxGrowthRate = -1.0;
int maxGrowthRateIndex = -1;
for (int i = 0; i < stocks.size(); i++) {
if (stocks[i][1] > maxGrowthRate) {
maxGrowthRate = stocks[i][1];
maxGrowthRateIndex = i;
}
}
return maxGrowthRateIndex;
}class Solution {
public int[] maximalStockReturn(int[] stockPrices, float[] growthRates) {
// Calculate returns for each stock
float[] returns = new float[stockPrices.length];
for (int i = 0; i < stockPrices.length; i++) {
returns[i] = stockPrices[i] * growthRates[i];
}
// Find the maximum return and its indices
float max_return = Float.MIN_VALUE;
List<Integer> max_indices = new ArrayList<>();
for (int i = 0; i < returns.length; i++) {
if (returns[i] > max_return) {
max_return = returns[i];
max_indices.clear();
max_indices.add(i);
} else if (returns[i] == max_return) {
max_indices.add(i);
}
}
// Convert list to array
int[] result = new int[max_indices.size()];
for (int i = 0; i < max_indices.size(); i++) {
result[i] = max_indices.get(i);
}
return result;
}
}def maximalStockReturn(stockPrices, growthRates):
# Calculate returns for each stock
returns = [price * rate for price, rate in zip(stockPrices, growthRates)]
# Find the maximum return and its indices
max_return = max(returns)
max_indices = [i for i, return_val in enumerate(returns) if return_val == max_return]
return max_indicesfunction optimalInvestmentPortfolio(stocks) {
let maxReturn = -Infinity;
let maxReturnIndices = [];
for (let i = 0; i < stocks.length; i++) {
let returnOnInvestment = stocks[i][0] * stocks[i][1];
if (returnOnInvestment > maxReturn) {
maxReturn = returnOnInvestment;
maxReturnIndices = [i];
} else if (returnOnInvestment === maxReturn) {
maxReturnIndices.push(i);
}
}
return maxReturnIndices;
}Asked in Top Tech Interviews
Solve in Interative Editor
Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.