Skip to content

Commit 71205c7

Browse files
authored
feat: add solutions to lc problem: No.1276 (doocs#2151)
No.1276.Number of Burgers with No Waste of Ingredients
1 parent 34adbe2 commit 71205c7

File tree

5 files changed

+108
-10
lines changed

5 files changed

+108
-10
lines changed

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

+30-2
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969

7070
**方法一:数学**
7171

72-
设巨无霸汉堡数量为 $x$,小皇堡数量为 $y$,则有:
72+
我们设巨无霸汉堡数量为 $x$,小皇堡数量为 $y$,则有:
7373

7474
$$
7575
\begin{aligned}
@@ -116,7 +116,7 @@ class Solution {
116116
int k = 4 * cheeseSlices - tomatoSlices;
117117
int y = k / 2;
118118
int x = cheeseSlices - y;
119-
return k % 2 != 0 || y < 0 || x < 0 ? Collections.emptyList() : Arrays.asList(x, y);
119+
return k % 2 != 0 || y < 0 || x < 0 ? List.of() : List.of(x, y);
120120
}
121121
}
122122
```
@@ -149,6 +149,34 @@ func numOfBurgers(tomatoSlices int, cheeseSlices int) []int {
149149
}
150150
```
151151

152+
### **TypeScript**
153+
154+
```ts
155+
function numOfBurgers(tomatoSlices: number, cheeseSlices: number): number[] {
156+
const k = 4 * cheeseSlices - tomatoSlices;
157+
const y = k >> 1;
158+
const x = cheeseSlices - y;
159+
return k % 2 || y < 0 || x < 0 ? [] : [x, y];
160+
}
161+
```
162+
163+
### **Rust**
164+
165+
```rust
166+
impl Solution {
167+
pub fn num_of_burgers(tomato_slices: i32, cheese_slices: i32) -> Vec<i32> {
168+
let k = 4 * cheese_slices - tomato_slices;
169+
let y = k / 2;
170+
let x = cheese_slices - y;
171+
if k % 2 != 0 || y < 0 || x < 0 {
172+
Vec::new()
173+
} else {
174+
vec![x, y]
175+
}
176+
}
177+
}
178+
```
179+
152180
### **...**
153181

154182
```

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

+53-1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,30 @@ There will be no remaining ingredients.
4848

4949
## Solutions
5050

51+
**Solution 1: Mathematics**
52+
53+
We set the number of Jumbo Burgers as $x$ and the number of Small Burgers as $y$, then we have:
54+
55+
$$
56+
\begin{aligned}
57+
4x + 2y &= tomatoSlices \\
58+
x + y &= cheeseSlices
59+
\end{aligned}
60+
$$
61+
62+
Transforming the above two equations, we can get:
63+
64+
$$
65+
\begin{aligned}
66+
y = (4 \times cheeseSlices - tomatoSlices) / 2 \\
67+
x = cheeseSlices - y
68+
\end{aligned}
69+
$$
70+
71+
Where $x$ and $y$ must be non-negative integers.
72+
73+
The time complexity is $O(1)$, and the space complexity is $O(1)$.
74+
5175
<!-- tabs:start -->
5276

5377
### **Python3**
@@ -69,7 +93,7 @@ class Solution {
6993
int k = 4 * cheeseSlices - tomatoSlices;
7094
int y = k / 2;
7195
int x = cheeseSlices - y;
72-
return k % 2 != 0 || y < 0 || x < 0 ? Collections.emptyList() : Arrays.asList(x, y);
96+
return k % 2 != 0 || y < 0 || x < 0 ? List.of() : List.of(x, y);
7397
}
7498
}
7599
```
@@ -102,6 +126,34 @@ func numOfBurgers(tomatoSlices int, cheeseSlices int) []int {
102126
}
103127
```
104128

129+
### **TypeScript**
130+
131+
```ts
132+
function numOfBurgers(tomatoSlices: number, cheeseSlices: number): number[] {
133+
const k = 4 * cheeseSlices - tomatoSlices;
134+
const y = k >> 1;
135+
const x = cheeseSlices - y;
136+
return k % 2 || y < 0 || x < 0 ? [] : [x, y];
137+
}
138+
```
139+
140+
### **Rust**
141+
142+
```rust
143+
impl Solution {
144+
pub fn num_of_burgers(tomato_slices: i32, cheese_slices: i32) -> Vec<i32> {
145+
let k = 4 * cheese_slices - tomato_slices;
146+
let y = k / 2;
147+
let x = cheese_slices - y;
148+
if k % 2 != 0 || y < 0 || x < 0 {
149+
Vec::new()
150+
} else {
151+
vec![x, y]
152+
}
153+
}
154+
}
155+
```
156+
105157
### **...**
106158

107159
```
Original file line numberDiff line numberDiff line change
@@ -1,8 +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-
}
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 ? List.of() : List.of(x, y);
7+
}
88
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
impl Solution {
2+
pub fn num_of_burgers(tomato_slices: i32, cheese_slices: i32) -> Vec<i32> {
3+
let k = 4 * cheese_slices - tomato_slices;
4+
let y = k / 2;
5+
let x = cheese_slices - y;
6+
if k % 2 != 0 || y < 0 || x < 0 {
7+
Vec::new()
8+
} else {
9+
vec![x, y]
10+
}
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
function numOfBurgers(tomatoSlices: number, cheeseSlices: number): number[] {
2+
const k = 4 * cheeseSlices - tomatoSlices;
3+
const y = k >> 1;
4+
const x = cheeseSlices - y;
5+
return k % 2 || y < 0 || x < 0 ? [] : [x, y];
6+
}

0 commit comments

Comments
 (0)