Number of Burgers with No Waste of Ingredients LeetCode solution in C++

1
class Solution {
2
public:
3
vector<int> numOfBurgers(int tomatoSlices, int cheeseSlices) {
4
// Observation
5
// Total Number of Burgers is Equal to Number of cheeseSlices
6
// Try to make 1 --> cheeseSlices Amount of Jumbo Burgers and
7
// remaining will be Small Burger
8
vector<int> ans;
9
if (tomatoSlices == 0 and cheeseSlices == 0) {
10
ans.push_back(0), ans.push_back(0);
11
return ans;
12
}
13
// Do Binary Search to Get Ideal Division.
14
int low = 0, high = cheeseSlices;
15
while (low < high) {
16
int mid = (low + high) / 2;
17
int jumbo = mid, small = cheeseSlices - mid;
18
// Jumbo needs 4 tomatoes per burger
19
// Small needs 2 tomatoes per burger
20
int needJumboTom = jumbo * 4;
21
int needSmallTom = small * 2;
22
// Should Add Upto tomatoSlices
23
if (needJumboTom + needSmallTom == tomatoSlices) {
24
ans.push_back(jumbo), ans.push_back(small);
25
break;
26
} else if (needJumboTom + needSmallTom < tomatoSlices) {
27
low = mid + 1;
28
} else {
29
high = mid;
30
}
31
}
32
return ans;
33
}
34
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0