Ride-hailing app~15 min2s · 256 MBtarget O(m) time, O(1) spacecomplexity
A ride app runs route-planning jobs at night. Each job has a growth class and an input size. The time limit and the machine's speed give a budget of steps. A machine does about 100,000,000 simple steps a second, so a limit of 1 second gives a budget of 100,000,000.
The team wants to know how many jobs fit the budget, without running any of them. You will count the steps of each job with its class formula.
Write count_jobs_that_fit(kinds, sizes, budget).
i has class kinds[i] and size sizes[i].floor(log2 n), and it is 0 for n = 0 and n = 1. The steps of n log n are n times that number.budget.Input: kinds = [1, 3, 2, 5, 4], sizes = [50000000, 20000, 1000000, 25, 500], budget = 100000000
Output: 3The budget is 100,000,000 steps. Kinds: 1 is n, 2 is n log n, 3 is n squared, 4 is n cubed, 5 is 2 to the n.
| Job | Kind | Size | Steps | Fits? |
|---|---|---|---|---|
| 0 | n | 50,000,000 | 50,000,000 | yes |
| 1 | n squared | 20,000 | 400,000,000 | no |
| 2 | n log n | 1,000,000 | 19,000,000 | yes |
| 3 | 2 to the n | 25 | 33,554,432 | yes |
| 4 | n cubed | 500 | 125,000,000 | no |
Three jobs fit, so the answer is 3.
Input: kinds = [3, 3], sizes = [10000, 10001], budget = 100000000
Output: 110,000 squared is exactly 100,000,000, so it fits. 10,001 squared is 100,020,001, which does not. The answer is 1.
Input: kinds = [], sizes = [], budget = 5
Output: 0With no jobs, none fits: 0.
0 ≤ kinds.length = sizes.length ≤ 100,000 (jobs)0 ≤ kinds[i] ≤ 5 (growth classes)0 ≤ sizes[i] ≤ 1,000,000,000 (input sizes)1 ≤ budget ≤ 1,000,000,000 (steps)Counting steps with loops can take 10^9 steps for one job, and 10^12 or more for the whole list. Use each class's formula instead. The steps of n squared reach 10^18, so use 64-bit integers, and guard n cubed and 2 to the n, which overflow.