Skip to content

Commit d33fb1e

Browse files
committed
feat: add solutions to lc problem: No.1276
No.1276.Number of Burgers with No Waste of Ingredients
1 parent f8b04de commit d33fb1e

File tree

6 files changed

+133
-10
lines changed

6 files changed

+133
-10
lines changed

solution/1200-1299/1276.Number of Burgers with No Waste of Ingredients/README.md

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,22 +67,86 @@
6767

6868
<!-- 这里可写通用的实现逻辑 -->
6969

70+
**方法一:数学**
71+
72+
设巨无霸汉堡数量为 $x$,小皇堡数量为 $y$,则有:
73+
74+
$$
75+
\begin{aligned}
76+
4x + 2y &= tomatoSlices \\
77+
x + y &= cheeseSlices
78+
\end{aligned}
79+
$$
80+
81+
将上述两式转换,可以得到:
82+
83+
$$
84+
\begin{aligned}
85+
y = (4 \times cheeseSlices - tomatoSlices) / 2 \\
86+
x = cheeseSlices - y
87+
\end{aligned}
88+
$$
89+
90+
其中 $x$ 和 $y$ 必须为非负整数。
91+
92+
时间复杂度 $O(1)$,空间复杂度 $O(1)$。
93+
7094
<!-- tabs:start -->
7195

7296
### **Python3**
7397

7498
<!-- 这里可写当前语言的特殊实现逻辑 -->
7599

76100
```python
77-
101+
class Solution:
102+
def numOfBurgers(self, tomatoSlices: int, cheeseSlices: int) -> List[int]:
103+
k = 4 * cheeseSlices - tomatoSlices
104+
y = k // 2
105+
x = cheeseSlices - y
106+
return [] if k % 2 or y < 0 or x < 0 else [x, y]
78107
```
79108

80109
### **Java**
81110

82111
<!-- 这里可写当前语言的特殊实现逻辑 -->
83112

84113
```java
114+
class Solution {
115+
public List<Integer> numOfBurgers(int tomatoSlices, int cheeseSlices) {
116+
int k = 4 * cheeseSlices - tomatoSlices;
117+
int y = k / 2;
118+
int x = cheeseSlices - y;
119+
return k % 2 != 0 || y < 0 || x < 0 ? Collections.emptyList() : Arrays.asList(x, y);
120+
}
121+
}
122+
```
123+
124+
### **C++**
125+
126+
```cpp
127+
class Solution {
128+
public:
129+
vector<int> numOfBurgers(int tomatoSlices, int cheeseSlices) {
130+
int k = 4 * cheeseSlices - tomatoSlices;
131+
int y = k / 2;
132+
int x = cheeseSlices - y;
133+
return k % 2 || x < 0 || y < 0 ? vector<int>{} : vector<int>{x, y};
134+
}
135+
};
136+
```
85137
138+
### **Go**
139+
140+
```go
141+
func numOfBurgers(tomatoSlices int, cheeseSlices int) []int {
142+
k := 4*cheeseSlices - tomatoSlices
143+
y := k / 2
144+
x := cheeseSlices - y
145+
if k%2 != 0 || x < 0 || y < 0 {
146+
return []int{}
147+
}
148+
return []int{x, y}
149+
}
86150
```
87151

88152
### **...**

solution/1200-1299/1276.Number of Burgers with No Waste of Ingredients/README_EN.md

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,53 @@ There will be no remaining ingredients.
5353
### **Python3**
5454

5555
```python
56-
56+
class Solution:
57+
def numOfBurgers(self, tomatoSlices: int, cheeseSlices: int) -> List[int]:
58+
k = 4 * cheeseSlices - tomatoSlices
59+
y = k // 2
60+
x = cheeseSlices - y
61+
return [] if k % 2 or y < 0 or x < 0 else [x, y]
5762
```
5863

5964
### **Java**
6065

6166
```java
67+
class Solution {
68+
public List<Integer> numOfBurgers(int tomatoSlices, int cheeseSlices) {
69+
int k = 4 * cheeseSlices - tomatoSlices;
70+
int y = k / 2;
71+
int x = cheeseSlices - y;
72+
return k % 2 != 0 || y < 0 || x < 0 ? Collections.emptyList() : Arrays.asList(x, y);
73+
}
74+
}
75+
```
76+
77+
### **C++**
78+
79+
```cpp
80+
class Solution {
81+
public:
82+
vector<int> numOfBurgers(int tomatoSlices, int cheeseSlices) {
83+
int k = 4 * cheeseSlices - tomatoSlices;
84+
int y = k / 2;
85+
int x = cheeseSlices - y;
86+
return k % 2 || x < 0 || y < 0 ? vector<int>{} : vector<int>{x, y};
87+
}
88+
};
89+
```
6290
91+
### **Go**
92+
93+
```go
94+
func numOfBurgers(tomatoSlices int, cheeseSlices int) []int {
95+
k := 4*cheeseSlices - tomatoSlices
96+
y := k / 2
97+
x := cheeseSlices - y
98+
if k%2 != 0 || x < 0 || y < 0 {
99+
return []int{}
100+
}
101+
return []int{x, y}
102+
}
63103
```
64104

65105
### **...**
Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
class Solution {
22
public:
33
vector<int> numOfBurgers(int tomatoSlices, int cheeseSlices) {
4-
int x = (tomatoSlices - 2 * cheeseSlices);
5-
if (x < 0 || x % 2 == 1)
6-
return {};
7-
x /= 2;
8-
int y = cheeseSlices - x;
9-
if (y < 0)
10-
return {};
11-
return {x, y};
4+
int k = 4 * cheeseSlices - tomatoSlices;
5+
int y = k / 2;
6+
int x = cheeseSlices - y;
7+
return k % 2 || x < 0 || y < 0 ? vector<int>{} : vector<int>{x, y};
128
}
139
};
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
func numOfBurgers(tomatoSlices int, cheeseSlices int) []int {
2+
k := 4*cheeseSlices - tomatoSlices
3+
y := k / 2
4+
x := cheeseSlices - y
5+
if k%2 != 0 || x < 0 || y < 0 {
6+
return []int{}
7+
}
8+
return []int{x, y}
9+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution {
2+
public List<Integer> numOfBurgers(int tomatoSlices, int cheeseSlices) {
3+
int k = 4 * cheeseSlices - tomatoSlices;
4+
int y = k / 2;
5+
int x = cheeseSlices - y;
6+
return k % 2 != 0 || y < 0 || x < 0 ? Collections.emptyList() : Arrays.asList(x, y);
7+
}
8+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Solution:
2+
def numOfBurgers(self, tomatoSlices: int, cheeseSlices: int) -> List[int]:
3+
k = 4 * cheeseSlices - tomatoSlices
4+
y = k // 2
5+
x = cheeseSlices - y
6+
return [] if k % 2 or y < 0 or x < 0 else [x, y]

0 commit comments

Comments
 (0)