Ctrl K

Stock Picks on a Budget

Medium
There are n stocks. Today stock i costs price_now[i], and in a year it will be worth price_later[i]. You have budget money. Each stock can be bought at most once, and the total cost of your purchases must not exceed the budget. A year later you sell everything you bought. Return the largest profit, the proceeds minus what you spent. Buying nothing is allowed and gives a profit of 0.
Example 1
price_now=[4, 7, 3, 5], price_later=[6, 12, 3, 9], budget=12 → 9

Stock 2 earns nothing (3 → 3). The best is stocks 1 and 3, bought for 7 + 5 = 12 and sold for 12 + 9 = 21: a profit of 9.

Example 2
price_now=[10, 8], price_later=[20, 15], budget=5 → 0

The budget cannot cover any stock, so the profit is 0.

Constraints
  • n == len(price_now) == len(price_later)
  • 1 ≤ n ≤ 1000
  • 0 ≤ price_now[i], price_later[i] ≤ 100
  • 0 ≤ budget ≤ 1000
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsFewest Jumps to the End MediumMost Profitable Streak MediumReach the Last Cell MediumWarehouse Cart Routes MediumCheapest Corner-to-Corner Route Medium
loading Python…
Input
price_now=[4, 7, 3, 5], price_later=[6, 12, 3, 9], budget=12
Expected
9

Run checks the first 3 tests in your browser. Submit checks all 102 tests on the server, hidden ones included.