Introduction
Given three types of pizzas — Small, Medium, and Large — we know their respective areas and costs.
The goal is to buy pizzas such that the total area is at least x, while spending the minimum possible amount of money.
We can buy any number of pizzas of each type.
This problem can be solved efficiently using Dynamic Programming (DP).
Problem Statement
We are given:
s→ Area of Small pizzam→ Area of Medium pizzal→ Area of Large pizzacs→ Cost of Small pizzacm→ Cost of Medium pizzacl→ Cost of Large pizzax→ Required minimum area
We need to return the minimum cost required to obtain a total pizza area of at least x.
Example
Input:
x = 16
s = 3, m = 6, l = 9
cs = 50, cm = 150, cl = 300
Output:
300
One possible solution is:
6 Small pizzas
Total Area = 6 × 3 = 18
Total Cost = 6 × 50 = 300
Since 18 >= 16, this satisfies the requirement.
Therefore:
Minimum Cost = 300
Key Idea
At first, we might think that choosing the pizza with the lowest cost is enough.
But that is not always correct.
For example:
Small Pizza:
Area = 3
Cost = 50
Large Pizza:
Area = 9
Cost = 300
A small pizza is cheaper individually, but we may need several small pizzas to reach the required area.
Therefore, we need to consider different combinations of pizzas.
Dynamic Programming allows us to calculate the minimum cost for every required area and reuse those results.
Dynamic Programming Definition
Let:
dp[i]
represent the minimum cost required to obtain at least i area.
Initially:
dp[0] = 0
because we need no money to obtain zero area.
For every i, we can choose one of the three pizzas.
Choose Small Pizza
If we buy a Small pizza:
dp[i - s] + cs
The previous required area is i - s, and we add the cost of the Small pizza.
Choose Medium Pizza
dp[i - m] + cm
Choose Large Pizza
dp[i - l] + cl
Therefore:
dp[i] = minimum of
dp[i - s] + cs
dp[i - m] + cm
dp[i - l] + cl
Handling Area Greater Than x
The problem says the total area should be at least x, not exactly x.
For example:
x = 16
An area of:
16
17
18
19
...
is acceptable.
This is why when a pizza itself has an area greater than the currently required area, buying that pizza alone is already enough.
For example:
Required area = 5
Large pizza area = 9
Large pizza cost = 300
Buying one Large pizza gives:
9 >= 5
so the cost is simply:
300
Algorithm
Create a DP array of size
x + 1.Set
dp[0] = 0.Initialize the remaining values with a large value.
For every area from
1tox:Consider buying a Small pizza.
Consider buying a Medium pizza.
Consider buying a Large pizza.
Take the minimum cost among the three choices.
Return
dp[x].
Java Implementation
class Solution {
public int minimumCost(int x, int s, int m, int l,
int cs, int cm, int cl) {
int[] dp = new int[x + 1];
// Initialize DP array
for (int i = 1; i <= x; i++) {
dp[i] = Integer.MAX_VALUE;
}
dp[0] = 0;
for (int i = 1; i <= x; i++) {
// Choose Small Pizza
if (i >= s) {
dp[i] = Math.min(dp[i],
dp[i - s] + cs);
} else {
dp[i] = Math.min(dp[i], cs);
}
// Choose Medium Pizza
if (i >= m) {
dp[i] = Math.min(dp[i],
dp[i - m] + cm);
} else {
dp[i] = Math.min(dp[i], cm);
}
// Choose Large Pizza
if (i >= l) {
dp[i] = Math.min(dp[i],
dp[i - l] + cl);
} else {
dp[i] = Math.min(dp[i], cl);
}
}
return dp[x];
}
}

Join the conversation! Your thoughts help the community grow.