Uber | Onsite | Crop Farming

Difficulty: MEDIUMCategory: InterviewExperienceFrequency: 1Asked On 2019-07-11Added By Anonymous
Source: Link

Company: Uber
\nRole: software engineer i, software engineer ii
\nInterview Stage: Onsite
\nLocation: unknown
\nYear: unknown

\n

Original Post Content:

\n

Given `cost` and `profit` array of farming pattern. Maximize profit for `m` months. For capital `x` only crops with cost below or equal to `x` can be farmed. Each month you can farm only one crop. Once a crop is farmed it can't be used again. After each crop the total capital becomes `x + profit`.

\n

Example 1:
\nInput: cost = [1, 8, 9], profit = [2, 3, 8], capital = 7, m = 2
\nOutput: 17
\nExplanation: In the begining only crop with cost 1 can be farmed. After first month capital = 7 + 2 = 9.
\nNext crop 9 should be farmed as profit is max for that to give 9 + 8 = 17. So max profit after 2 months will be 17.

\n

Expected time complexity less than O(n^2).

Comments