|
74 | 74 |
|
75 | 75 | <!-- 这里可写通用的实现逻辑 -->
|
76 | 76 |
|
| 77 | +二分查找。 |
| 78 | + |
77 | 79 | <!-- tabs:start -->
|
78 | 80 |
|
79 | 81 | ### **Python3**
|
80 | 82 |
|
81 | 83 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
82 | 84 |
|
83 | 85 | ```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 |
85 | 102 | ```
|
86 | 103 |
|
87 | 104 | ### **Java**
|
88 | 105 |
|
89 | 106 | <!-- 这里可写当前语言的特殊实现逻辑 -->
|
90 | 107 |
|
91 | 108 | ```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 | +``` |
92 | 160 |
|
| 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 | +} |
93 | 184 | ```
|
94 | 185 |
|
95 | 186 | ### **...**
|
|
0 commit comments