File tree Expand file tree Collapse file tree 6 files changed +133
-10
lines changed
solution/1200-1299/1276.Number of Burgers with No Waste of Ingredients Expand file tree Collapse file tree 6 files changed +133
-10
lines changed Original file line number Diff line number Diff line change 67
67
68
68
<!-- 这里可写通用的实现逻辑 -->
69
69
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
+
70
94
<!-- tabs:start -->
71
95
72
96
### ** Python3**
73
97
74
98
<!-- 这里可写当前语言的特殊实现逻辑 -->
75
99
76
100
``` 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]
78
107
```
79
108
80
109
### ** Java**
81
110
82
111
<!-- 这里可写当前语言的特殊实现逻辑 -->
83
112
84
113
``` 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
+ ```
85
137
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
+ }
86
150
```
87
151
88
152
### ** ...**
Original file line number Diff line number Diff line change @@ -53,13 +53,53 @@ There will be no remaining ingredients.
53
53
### ** Python3**
54
54
55
55
``` 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]
57
62
```
58
63
59
64
### ** Java**
60
65
61
66
``` 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
+ ```
62
90
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
+ }
63
103
```
64
104
65
105
### ** ...**
Original file line number Diff line number Diff line change 1
1
class Solution {
2
2
public:
3
3
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};
12
8
}
13
9
};
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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 ]
You can’t perform that action at this time.
0 commit comments