Skip to content

Commit 4b8d1ae

Browse files
committed
feat: add solutions to lcp problem: No.18
1 parent 13382e8 commit 4b8d1ae

File tree

5 files changed

+173
-1
lines changed

5 files changed

+173
-1
lines changed

lcp/LCP 18. 早餐组合/README.md

+92-1
Original file line numberDiff line numberDiff line change
@@ -74,22 +74,113 @@
7474

7575
<!-- 这里可写通用的实现逻辑 -->
7676

77+
二分查找。
78+
7779
<!-- tabs:start -->
7880

7981
### **Python3**
8082

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

8385
```python
84-
86+
class Solution:
87+
def breakfastNumber(self, staple: List[int], drinks: List[int], x: int) -> int:
88+
res, n = 0, len(drinks)
89+
drinks.sort()
90+
for s in staple:
91+
remain = x - s
92+
if remain >= drinks[0]:
93+
left, right = 0, n - 1
94+
while left < right:
95+
mid = (left + right + 1) >> 1
96+
if drinks[mid] <= remain:
97+
left = mid
98+
else:
99+
right = mid - 1
100+
res = (res + left + 1) % 1000000007
101+
return res
85102
```
86103

87104
### **Java**
88105

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

91108
```java
109+
class Solution {
110+
public int breakfastNumber(int[] staple, int[] drinks, int x) {
111+
int res = 0, n = drinks.length;
112+
Arrays.sort(drinks);
113+
for (int s : staple) {
114+
int remain = x - s;
115+
if (remain >= drinks[0]) {
116+
int left = 0, right = n - 1;
117+
while (left < right) {
118+
int mid = (left + right + 1) >>> 1;
119+
if (drinks[mid] <= remain) {
120+
left = mid;
121+
} else {
122+
right = mid - 1;
123+
}
124+
}
125+
res = (res + left + 1) % 1000000007;
126+
}
127+
}
128+
return res;
129+
}
130+
}
131+
```
132+
133+
### **C++**
134+
135+
```cpp
136+
class Solution {
137+
public:
138+
int breakfastNumber(vector<int>& staple, vector<int>& drinks, int x) {
139+
int res = 0, n = drinks.size();
140+
sort(drinks.begin(), drinks.end());
141+
for (int s : staple)
142+
{
143+
int remain = x - s;
144+
if (remain >= drinks[0])
145+
{
146+
int left = 0, right = n - 1;
147+
while (left < right)
148+
{
149+
int mid = left + right + 1 >> 1;
150+
if (drinks[mid] <= remain) left = mid;
151+
else right = mid - 1;
152+
}
153+
res = (res + left + 1) % 1000000007;
154+
}
155+
}
156+
return res;
157+
}
158+
};
159+
```
92160
161+
### **Go**
162+
163+
```go
164+
func breakfastNumber(staple []int, drinks []int, x int) int {
165+
res, n := 0, len(drinks)
166+
sort.Ints(drinks)
167+
for _, s := range staple {
168+
remain := x - s
169+
if remain >= drinks[0] {
170+
left, right := 0, n-1
171+
for left < right {
172+
mid := (left + right + 1) >> 1
173+
if drinks[mid] <= remain {
174+
left = mid
175+
} else {
176+
right = mid - 1
177+
}
178+
}
179+
res = (res + left + 1) % 1000000007
180+
}
181+
}
182+
return res
183+
}
93184
```
94185

95186
### **...**

lcp/LCP 18. 早餐组合/Solution.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
int breakfastNumber(vector<int>& staple, vector<int>& drinks, int x) {
4+
int res = 0, n = drinks.size();
5+
sort(drinks.begin(), drinks.end());
6+
for (int s : staple)
7+
{
8+
int remain = x - s;
9+
if (remain >= drinks[0])
10+
{
11+
int left = 0, right = n - 1;
12+
while (left < right)
13+
{
14+
int mid = left + right + 1 >> 1;
15+
if (drinks[mid] <= remain) left = mid;
16+
else right = mid - 1;
17+
}
18+
res = (res + left + 1) % 1000000007;
19+
}
20+
}
21+
return res;
22+
}
23+
};

lcp/LCP 18. 早餐组合/Solution.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
func breakfastNumber(staple []int, drinks []int, x int) int {
2+
res, n := 0, len(drinks)
3+
sort.Ints(drinks)
4+
for _, s := range staple {
5+
remain := x - s
6+
if remain >= drinks[0] {
7+
left, right := 0, n-1
8+
for left < right {
9+
mid := (left + right + 1) >> 1
10+
if drinks[mid] <= remain {
11+
left = mid
12+
} else {
13+
right = mid - 1
14+
}
15+
}
16+
res = (res + left + 1) % 1000000007
17+
}
18+
}
19+
return res
20+
}
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public int breakfastNumber(int[] staple, int[] drinks, int x) {
3+
int res = 0, n = drinks.length;
4+
Arrays.sort(drinks);
5+
for (int s : staple) {
6+
int remain = x - s;
7+
if (remain >= drinks[0]) {
8+
int left = 0, right = n - 1;
9+
while (left < right) {
10+
int mid = (left + right + 1) >>> 1;
11+
if (drinks[mid] <= remain) {
12+
left = mid;
13+
} else {
14+
right = mid - 1;
15+
}
16+
}
17+
res = (res + left + 1) % 1000000007;
18+
}
19+
}
20+
return res;
21+
}
22+
}

lcp/LCP 18. 早餐组合/Solution.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution:
2+
def breakfastNumber(self, staple: List[int], drinks: List[int], x: int) -> int:
3+
res, n = 0, len(drinks)
4+
drinks.sort()
5+
for s in staple:
6+
remain = x - s
7+
if remain >= drinks[0]:
8+
left, right = 0, n - 1
9+
while left < right:
10+
mid = (left + right + 1) >> 1
11+
if drinks[mid] <= remain:
12+
left = mid
13+
else:
14+
right = mid - 1
15+
res = (res + left + 1) % 1000000007
16+
return res

0 commit comments

Comments
 (0)