FastPrepFastPrep
Problem Brief

Maximum Total Number of Orders Fulfilled

OA

A technology company announced that a new supply of P monitors would soon be available at their store. There were N orders (numbered from 0 to N-1) placed by customers who wanted to buy those monitors. The K-th order has to be delivered to a location at distance D[K] from the store and is for exactly C[K] monitors.

Now the time has come for the monitors to be delivered. The orders will be fulfilled one by one. To minimize the shipping time, it has been decided that the deliveries will be made in order of increasing distance from the store. If there are many customers at the same distance, they can be processed in any order. Monitors to more distant customers will be delivered only once all orders to customers closer to the store have already been fulfilled.

What is the maximum total number of orders that can be fulfilled?

Function Description

Write a function:

class Solution { public int solution(int[] D, int[] C, int P); }

that, given two arrays of integers D and C, and an integer P, returns the maximum total number of orders that can be fulfilled.

1Example 1

Input
D = [5, 11, 1, 3], C = [6, 1, 3, 2], P = 7
Output
2
Explanation
The customers at distances 1 and 3 will have their orders fulfilled and 3 + 2 = 5 monitors will be delivered.

2Example 2

Input
D = [10, 15, 1], C = [10, 1, 2], P = 3
Output
1
Explanation
Only the order for the customer at distance 1 will be fulfilled. There will not be enough monitors in the store for the customer at distance 10. Therefore, orders for customers at distances 10 and 15 will not be fulfilled.

3Example 3

Input
D = [11, 18, 1], C = [9, 18, 8], P = 7
Output
0
Explanation
There are not enough monitors to fulfill any orders.

4Example 4

Input
D = [1, 4, 2, 5], C = [4, 9, 2, 3], P = 19
Output
4
Explanation
All orders can be fulfilled as there are enough monitors available.

Constraints

Limits and guarantees your solution can rely on.

  • N is an integer within the range [1..100,000].
  • Each element of arrays D and C is an integer within the range [1..1,000,000,000].
  • P is an integer within the range [0...10,000,000,000].
public int solution(int[] D, int[] C, int P) {
   // write your code here
}
Input

D

[5, 11, 1, 3]

C

[6, 1, 3, 2]

P

7

Output

2

Sign in to submit your solution.