Problem · Array
HardInMobi logoInMobiFULLTIMEPHONE SCREEN

Problem statement

You are given n projects. Project i produces a pure profit of profits[i] and requires at least capital[i] current capital before it can be started.

You begin with capital w. After completing a project, immediately add its profit to your capital. Each project may be completed at most once.

Choose at most k distinct projects to maximize your final capital. Return that maximum final capital.

Function

findMaximizedCapital(k: int, w: int, profits: int[], capital: int[]) → int

Examples

Example 1

k = 2w = 0profits = [1,2,3]capital = [0,1,1]return = 4

Start project 0 to grow the capital from 0 to 1. Projects 1 and 2 then become affordable; choosing project 2 produces final capital 4.

Example 2

k = 3w = 0profits = [1,2,3]capital = [1,1,2]return = 0

No project is affordable with initial capital 0, so no project can be completed and the final capital remains 0.

Constraints

  • 1 <= profits.length = capital.length <= 10^5
  • 1 <= k <= profits.length
  • 0 <= w <= 10^9
  • 0 <= profits[i] <= 10^9
  • 0 <= capital[i] <= 10^9
  • The maximum final capital fits in a signed 32-bit integer.

More InMobi problems

drafts saved locally
public int findMaximizedCapital(int k, int w, int[] profits, int[] capital) {
    // Write your code here
}
k2
w0
profits[1,2,3]
capital[0,1,1]
expected4
checking account