Skip to content

Commit 6a2b456

Browse files
authored
Create number-of-burgers-with-no-waste-of-ingredients.cpp
1 parent af1a48b commit 6a2b456

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Time: O(1)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
vector<int> numOfBurgers(int tomatoSlices, int cheeseSlices) {
7+
// let the number of jumbo burger be x, the number of small burger be y:
8+
// 4x + 2y = t
9+
// x + y = c
10+
// =>
11+
// x = t/2-c
12+
// y = 2c-t/2
13+
// since x, y are natural numbers
14+
// => t/2 is integer, t/2-c >= 0, 2c-t/2 >= 0
15+
// => t%2 == 0, 2c <= t <= 4c
16+
if (tomatoSlices % 2 == 0 &&
17+
2 * cheeseSlices <= tomatoSlices &&
18+
tomatoSlices <= 4*cheeseSlices) {
19+
return {tomatoSlices / 2 - cheeseSlices, 2 * cheeseSlices - tomatoSlices / 2};
20+
}
21+
return {};
22+
}
23+
};

0 commit comments

Comments
 (0)