@@ -48,6 +48,30 @@ There will be no remaining ingredients.
48
48
49
49
## Solutions
50
50
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
+
51
75
<!-- tabs:start -->
52
76
53
77
### ** Python3**
@@ -69,7 +93,7 @@ class Solution {
69
93
int k = 4 * cheeseSlices - tomatoSlices;
70
94
int y = k / 2 ;
71
95
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);
73
97
}
74
98
}
75
99
```
@@ -102,6 +126,34 @@ func numOfBurgers(tomatoSlices int, cheeseSlices int) []int {
102
126
}
103
127
```
104
128
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
+
105
157
### ** ...**
106
158
107
159
```
0 commit comments