Maximum Input Within an Operation Budget

For each query, you are given an operation type and a budget B. Your task is to find the largest integer n in the range [0, 109] whose operation cost does not exceed the given budget B. If no such integer n is feasible, return -1.

The cost of an operation for a given input size n is defined by its type:

  • LINEAR: n
  • QUADRATIC: n2
  • CUBIC: n3
  • NLOGN: n*ceil(log2(n)) for n >= 1, with cost(0) = 0
  • EXPONENTIAL: 2n
  • FACTORIAL: n!, with 0! = 1

Return a list of integers representing the maximum feasible n for each query.

Note:

  • The input is given as two arrays. The ith query you need to process is represented by the operation type types[i] and its corresponding maximum limit budgets[i]. Your output should be an array where the ith element is the answer to the ith query.
  • The ceiling function, denoted as ceil(x), returns the smallest integer that is greater than or equal to x

Example 1:

Input:

types = ["QUADRATIC", "EXPONENTIAL", "FACTORIAL"]
budgets = [20, 7, 0] 

Output:

[4, 2, -1] 

Explanation:

  • Query 1 (QUADRATIC, Budget 20): 42 = 16 <= 20. 52 = 25 > 20. The maximum n is 4.
  • Query 2 (EXPONENTIAL, Budget 7): 22 = 4 <= 7. 23 = 8 > 7. The maximum n is 2.
  • Query 3 (FACTORIAL, Budget 0): 0! = 1 > 0. No valid n exists, so the answer is -1.

Example 2:

Input:

types = ["CUBIC", "LINEAR"]
budgets = [30, 1000] 

Output:

[3, 1000] 

Explanation:

  • Query 1 (CUBIC, Budget 30): 33 = 27 <= 30. 43 = 64 > 30. The maximum n is 3.
  • Query 2 (LINEAR, Budget 1000): The cost is strictly equal to n, so the maximum n is 1000.

Now Your Turn!

Pick the correct output for the given input

Input:

types = ["NLOGN", "EXPONENTIAL"]
budgets = [24, 100]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= Q <= 10000
  • 0 <= B <= 1018
  • Allowed types: "LINEAR", "QUADRATIC", "CUBIC", "NLOGN", "EXPONENTIAL", "FACTORIAL"
  • Output max bound: n <= 109

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution {
public:
vector<int> maxFeasibleSizes(vector<string>& types, vector<long long>& budgets) {
// User code goes here
}
};
Test Case

Input:

Budgets
Types