Problem · Dynamic Programming

Selecting Stocks

Learn this problem
MediumSnowflake logoSnowflakeINTERNOA
See Snowflake hiring insights

Problem statement

An investor has saved some money and wants to invest in the stock market. There are a number of stocks to choose from, and they want to buy at most 1 share in any company. The total invested cannot exceed the funds available. A friend who is a stock market expert has predicted the values of each stock after 1 year. Determine the maximum profit that can be earned at the end of the year assuming the predictions come true.

Function

selectStock(saving: int, currentValue: int[], futureValue: int[]) → int

Complete the function selectStock in the editor below. The function should return an integer that denotes the maximum profit after one year.

selectStock has the following parameter(s):

  1. 1. saving: amount available for investment
  2. 2. int currentValue[n]: the current stock values
  3. 3. int futureValue[n]: the values of the stocks after one year

Examples

Example 1

saving = 250currentValue = [175, 133, 109, 210, 97]futureValue = [200, 125, 128, 228, 133]return = 55
To maximize profits, the investor should buy stocks at indices 2 and 4 for an investment of 109 + 97 = 206. At the end of the year the stocks are sold for 128 + 133 = 261, so total profit is 261 - 206 = 55.

Constraints

🍎🍎

More Snowflake problems

drafts saved locally
public int selectStock(int saving, int[] currentValue, int[] futureValue) {
  // write your code here
}
saving250
currentValue[175, 133, 109, 210, 97]
futureValue[200, 125, 128, 228, 133]
expected55
checking account