forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNumber of Burgers with No Waste of Ingredients.cpp
35 lines (35 loc) · 1.28 KB
/
Number of Burgers with No Waste of Ingredients.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Runtime: 13 ms (Top 17.35%) | Memory: 7.1 MB (Top 91.58%)
class Solution {
public:
vector<int> numOfBurgers(int tomatoSlices, int cheeseSlices) {
// Observation
// Total Number of Burgers is Equal to Number of cheeseSlices
// Try to make 1 --> cheeseSlices Amount of Jumbo Burgers and
// remaining will be Small Burger
vector <int> ans;
if(tomatoSlices == 0 and cheeseSlices == 0) {
ans.push_back(0), ans.push_back(0);
return ans;
}
// Do Binary Search to Get Ideal Division.
int low = 0, high = cheeseSlices;
while(low < high) {
int mid = (low + high) / 2;
int jumbo = mid, small = cheeseSlices - mid;
// Jumbo needs 4 tomatoes per burger
// Small needs 2 tomatoes per burger
int needJumboTom = jumbo * 4;
int needSmallTom = small * 2;
// Should Add Upto tomatoSlices
if(needJumboTom + needSmallTom == tomatoSlices) {
ans.push_back(jumbo), ans.push_back(small);
break;
} else if(needJumboTom + needSmallTom < tomatoSlices) {
low = mid + 1;
} else {
high = mid;
}
}
return ans;
}
};