387. Maximum Profit From Trading Stocks

You are given two 0-indexed integer arrays of the same length present and future where present[i] is the current price of the ith stock and future[i] is the price of the ith stock a year in the future. You may buy each stock at most once. You are also given an integer budget representing the amount of money you currently have.

Return the maximum amount of profit you can make.

Example 1:

Input: present = [5,4,6,2,3], future = [8,5,4,3,5], budget = 10

Output: 6

Explanation: One possible way to maximize your profit is to:

Buy the 0th, 3rd, and 4th stocks for a total of 5 + 2 + 3 = 10.

Next year, sell all three stocks for a total of 8 + 3 + 5 = 16.

The profit you made is 16 - 10 = 6.

It can be shown that the maximum profit you can make is 6.

Example 2:

Input: present = [2,2,5], future = [3,4,10], budget = 6

Output: 5

Explanation: The only possible way to maximize your profit is to:

Buy the 2nd stock, and make a profit of 10 - 5 = 5.

It can be shown that the maximum profit you can make is 5.

Now Your Turn!

Pick the correct output for the given input

Consider this set of stocks present = [2, 3, 4], future = [2, 1, 5] budget = 8. What is the maximum profit you can achieve?

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • n == present.length == future.length
  • 1 <= n <= 1000
  • 0 <= present[i], future[i] <= 100
  • 0 <= budget <= 1000

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution {
public:
int maximumProfit(vector<int>& present, vector<int>& future, int budget) {
// Your code goes here
}
};
Test Case

Input:

Budget
Future
Present