Skip to content

feat: add solutions to lc problem: No.1276 #2151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@

**方法一:数学**

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

$$
\begin{aligned}
Expand Down Expand Up @@ -116,7 +116,7 @@ class Solution {
int k = 4 * cheeseSlices - tomatoSlices;
int y = k / 2;
int x = cheeseSlices - y;
return k % 2 != 0 || y < 0 || x < 0 ? Collections.emptyList() : Arrays.asList(x, y);
return k % 2 != 0 || y < 0 || x < 0 ? List.of() : List.of(x, y);
}
}
```
Expand Down Expand Up @@ -149,6 +149,34 @@ func numOfBurgers(tomatoSlices int, cheeseSlices int) []int {
}
```

### **TypeScript**

```ts
function numOfBurgers(tomatoSlices: number, cheeseSlices: number): number[] {
const k = 4 * cheeseSlices - tomatoSlices;
const y = k >> 1;
const x = cheeseSlices - y;
return k % 2 || y < 0 || x < 0 ? [] : [x, y];
}
```

### **Rust**

```rust
impl Solution {
pub fn num_of_burgers(tomato_slices: i32, cheese_slices: i32) -> Vec<i32> {
let k = 4 * cheese_slices - tomato_slices;
let y = k / 2;
let x = cheese_slices - y;
if k % 2 != 0 || y < 0 || x < 0 {
Vec::new()
} else {
vec![x, y]
}
}
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,30 @@ There will be no remaining ingredients.

## Solutions

**Solution 1: Mathematics**

We set the number of Jumbo Burgers as $x$ and the number of Small Burgers as $y$, then we have:

$$
\begin{aligned}
4x + 2y &= tomatoSlices \\
x + y &= cheeseSlices
\end{aligned}
$$

Transforming the above two equations, we can get:

$$
\begin{aligned}
y = (4 \times cheeseSlices - tomatoSlices) / 2 \\
x = cheeseSlices - y
\end{aligned}
$$

Where $x$ and $y$ must be non-negative integers.

The time complexity is $O(1)$, and the space complexity is $O(1)$.

<!-- tabs:start -->

### **Python3**
Expand All @@ -69,7 +93,7 @@ class Solution {
int k = 4 * cheeseSlices - tomatoSlices;
int y = k / 2;
int x = cheeseSlices - y;
return k % 2 != 0 || y < 0 || x < 0 ? Collections.emptyList() : Arrays.asList(x, y);
return k % 2 != 0 || y < 0 || x < 0 ? List.of() : List.of(x, y);
}
}
```
Expand Down Expand Up @@ -102,6 +126,34 @@ func numOfBurgers(tomatoSlices int, cheeseSlices int) []int {
}
```

### **TypeScript**

```ts
function numOfBurgers(tomatoSlices: number, cheeseSlices: number): number[] {
const k = 4 * cheeseSlices - tomatoSlices;
const y = k >> 1;
const x = cheeseSlices - y;
return k % 2 || y < 0 || x < 0 ? [] : [x, y];
}
```

### **Rust**

```rust
impl Solution {
pub fn num_of_burgers(tomato_slices: i32, cheese_slices: i32) -> Vec<i32> {
let k = 4 * cheese_slices - tomato_slices;
let y = k / 2;
let x = cheese_slices - y;
if k % 2 != 0 || y < 0 || x < 0 {
Vec::new()
} else {
vec![x, y]
}
}
}
```

### **...**

```
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
class Solution {
public List<Integer> numOfBurgers(int tomatoSlices, int cheeseSlices) {
int k = 4 * cheeseSlices - tomatoSlices;
int y = k / 2;
int x = cheeseSlices - y;
return k % 2 != 0 || y < 0 || x < 0 ? Collections.emptyList() : Arrays.asList(x, y);
}
class Solution {
public List<Integer> numOfBurgers(int tomatoSlices, int cheeseSlices) {
int k = 4 * cheeseSlices - tomatoSlices;
int y = k / 2;
int x = cheeseSlices - y;
return k % 2 != 0 || y < 0 || x < 0 ? List.of() : List.of(x, y);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
impl Solution {
pub fn num_of_burgers(tomato_slices: i32, cheese_slices: i32) -> Vec<i32> {
let k = 4 * cheese_slices - tomato_slices;
let y = k / 2;
let x = cheese_slices - y;
if k % 2 != 0 || y < 0 || x < 0 {
Vec::new()
} else {
vec![x, y]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
function numOfBurgers(tomatoSlices: number, cheeseSlices: number): number[] {
const k = 4 * cheeseSlices - tomatoSlices;
const y = k >> 1;
const x = cheeseSlices - y;
return k % 2 || y < 0 || x < 0 ? [] : [x, y];
}