Problem · Dynamic Programming
Selecting Stocks
Learn this problemProblem 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.
saving: amount available for investment - 2.
int currentValue[n]: the current stock values - 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 = 55To 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
- Minimum N-ary Tree Depth DeletionsPHONE SCREEN · Seen Jul 2026
- Simulate a Queued Multi-Rule Rate LimiterPHONE SCREEN · Seen Jul 2026
- Minimum Clicks Between Wiki PagesOA · Seen Jul 2026
- Closest Target CharacterPHONE SCREEN · Seen Jul 2026
- Horizontal Pod AutoscalerOA · Seen Jul 2026
- Minimum HeightOA · Seen Jul 2026
- Vowel SubstringOA · Seen Jun 2026
- String Formation (Also for AI/ML Software Engineer Intern :)OA · Seen Jun 2026