@@ -95,19 +95,41 @@ x=5, y=1 -> f(5, 1) = 5 * 1 = 5</pre>
95
95
96
96
97
97
class Solution :
98
- def findSolution (self , customfunction : ' CustomFunction' , z : int ) -> List[List[int ]]:
99
- res = []
98
+ def findSolution (self , customfunction : " CustomFunction" , z : int ) -> List[List[int ]]:
99
+ ans = []
100
100
for x in range (1 , 1001 ):
101
- left, right = 1 , 1000
102
- while left < right:
103
- mid = (left + right) >> 1
104
- if customfunction.f(x, mid) >= z:
105
- right = mid
106
- else :
107
- left = mid + 1
108
- if customfunction.f(x, left) == z:
109
- res.append([x, left])
110
- return res
101
+ y = 1 + bisect_left(range (1 , 1001 ), z, key = lambda y : customfunction.f(x, y))
102
+ if customfunction.f(x, y) == z:
103
+ ans.append([x, y])
104
+ return ans
105
+ ```
106
+
107
+ ``` python
108
+ """
109
+ This is the custom function interface.
110
+ You should not implement it, or speculate about its implementation
111
+ class CustomFunction:
112
+ # Returns f(x, y) for any given positive integers x and y.
113
+ # Note that f(x, y) is increasing with respect to both x and y.
114
+ # i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)
115
+ def f(self, x, y):
116
+
117
+ """
118
+
119
+ class Solution :
120
+ def findSolution (self , customfunction : ' CustomFunction' , z : int ) -> List[List[int ]]:
121
+ ans = []
122
+ x, y = 1 , 1000
123
+ while x <= 1000 and y:
124
+ t = customfunction.f(x, y)
125
+ if t < z:
126
+ x += 1
127
+ elif t > z:
128
+ y -= 1
129
+ else :
130
+ ans.append([x, y])
131
+ x, y = x + 1 , y - 1
132
+ return ans
111
133
```
112
134
113
135
### ** Java**
@@ -126,58 +148,56 @@ class Solution:
126
148
* };
127
149
*/
128
150
129
- class Solution {
151
+ class Solution {
130
152
public List<List<Integer > > findSolution (CustomFunction customfunction , int z ) {
131
- List<List<Integer > > res = new ArrayList<> ();
132
- for (int i = 1 ; i <= 1000 ; ++ i ) {
133
- int left = 1 , right = 1000 ;
134
- while (left < right ) {
135
- int mid = (left + right ) >> 1 ;
136
- if (customfunction. f(i , mid) >= z) {
137
- right = mid;
153
+ List<List<Integer > > ans = new ArrayList<> ();
154
+ for (int x = 1 ; x <= 1000 ; ++ x ) {
155
+ int l = 1 , r = 1000 ;
156
+ while (l < r ) {
157
+ int mid = (l + r ) >> 1 ;
158
+ if (customfunction. f(x , mid) >= z) {
159
+ r = mid;
138
160
} else {
139
- left = mid + 1 ;
161
+ l = mid + 1 ;
140
162
}
141
163
}
142
- if (customfunction. f(i, left ) == z) {
143
- res . add(Arrays . asList(i, left ));
164
+ if (customfunction. f(x, l ) == z) {
165
+ ans . add(Arrays . asList(x, l ));
144
166
}
145
167
}
146
- return res ;
168
+ return ans ;
147
169
}
148
170
}
149
171
```
150
172
151
- ### ** TypeScript**
152
-
153
- ``` ts
154
- /**
155
- * // This is the CustomFunction's API interface.
173
+ ``` java
174
+ /*
175
+ * // This is the custom function interface.
156
176
* // You should not implement it, or speculate about its implementation
157
177
* class CustomFunction {
158
- * f(x: number, y: number): number {}
159
- * }
178
+ * // Returns f(x, y) for any given positive integers x and y.
179
+ * // Note that f(x, y) is increasing with respect to both x and y.
180
+ * // i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)
181
+ * public int f(int x, int y);
182
+ * };
160
183
*/
161
184
162
- function findSolution( customfunction : CustomFunction , z : number ) : number [][] {
163
- // 二分
164
- let ans = [] ;
165
- for ( let i = 1 ; i < = 1000 ; i ++ ) {
166
- let left = 1 ,
167
- right = 1000 ;
168
- while ( left < right ) {
169
- let mid = ( left + right ) >> 1 ;
170
- if (customfunction . f ( i , mid ) >= z ) {
171
- right = mid ;
185
+ class Solution {
186
+ public List< List< Integer > > findSolution ( CustomFunction customfunction , int z ) {
187
+ List< List< Integer > > ans = new ArrayList<> () ;
188
+ int x = 1 , y = 1000 ;
189
+ while (x <= 1000 && y > 0 ) {
190
+ int t = customfunction . f(x, y) ;
191
+ if (t < z ) {
192
+ ++ x ;
193
+ } else if (t > z) {
194
+ -- y ;
172
195
} else {
173
- left = mid + 1 ;
196
+ ans . add( Arrays . asList(x ++ , y -- )) ;
174
197
}
175
198
}
176
- if (customfunction .f (i , left ) == z ) {
177
- ans .push ([i , left ]);
178
- }
199
+ return ans;
179
200
}
180
- return ans ;
181
201
}
182
202
```
183
203
@@ -199,22 +219,55 @@ function findSolution(customfunction: CustomFunction, z: number): number[][] {
199
219
class Solution {
200
220
public:
201
221
vector<vector<int >> findSolution(CustomFunction& customfunction, int z) {
202
- vector<vector<int >> res ;
203
- for (int i = 1; i <= 1000; ++i ) {
204
- int left = 1, right = 1000;
205
- while (left < right ) {
206
- int mid = left + right >> 1;
207
- if (customfunction.f(i , mid) >= z) {
208
- right = mid;
222
+ vector<vector<int >> ans ;
223
+ for (int x = 1; x <= 1000; ++x ) {
224
+ int l = 1, r = 1000;
225
+ while (l < r ) {
226
+ int mid = (l + r) >> 1;
227
+ if (customfunction.f(x , mid) >= z) {
228
+ r = mid;
209
229
} else {
210
- left = mid + 1;
230
+ l = mid + 1;
211
231
}
212
232
}
213
- if (customfunction.f(i, left ) == z) {
214
- res .push_back({i, left });
233
+ if (customfunction.f(x, l ) == z) {
234
+ ans .push_back({x, l });
215
235
}
216
236
}
217
- return res;
237
+ return ans;
238
+ }
239
+ };
240
+ ```
241
+
242
+ ```cpp
243
+ /*
244
+ * // This is the custom function interface.
245
+ * // You should not implement it, or speculate about its implementation
246
+ * class CustomFunction {
247
+ * public:
248
+ * // Returns f(x, y) for any given positive integers x and y.
249
+ * // Note that f(x, y) is increasing with respect to both x and y.
250
+ * // i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)
251
+ * int f(int x, int y);
252
+ * };
253
+ */
254
+
255
+ class Solution {
256
+ public:
257
+ vector<vector<int>> findSolution(CustomFunction& customfunction, int z) {
258
+ vector<vector<int>> ans;
259
+ int x = 1, y = 1000;
260
+ while (x <= 1000 && y) {
261
+ int t = customfunction.f(x, y);
262
+ if (t < z) {
263
+ ++x;
264
+ } else if (t > z) {
265
+ --y;
266
+ } else {
267
+ ans.push_back({x++, y--});
268
+ }
269
+ }
270
+ return ans;
218
271
}
219
272
};
220
273
```
@@ -231,23 +284,100 @@ public:
231
284
* i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)
232
285
*/
233
286
234
- func findSolution(customFunction func(int, int) int, z int) [][]int {
235
- res := [][]int{}
236
- for i := 1; i <= 1000; i++ {
237
- left, right := 1, 1000
238
- for left < right {
239
- mid := (left + right) >> 1
240
- if customFunction(i, mid) >= z {
241
- right = mid
242
- } else {
243
- left = mid + 1
244
- }
287
+ func findSolution (customFunction func (int , int ) int , z int ) (ans [][]int ) {
288
+ for x := 1 ; x <= 1000 ; x++ {
289
+ y := 1 + sort.Search (999 , func (y int ) bool { return customFunction (x, y+1 ) >= z })
290
+ if customFunction (x, y) == z {
291
+ ans = append (ans, []int {x, y})
245
292
}
246
- if customFunction(i, left) == z {
247
- res = append(res, []int{i, left})
293
+ }
294
+ return
295
+ }
296
+ ```
297
+
298
+ ``` go
299
+ /* *
300
+ * This is the declaration of customFunction API.
301
+ * @param x int
302
+ * @param x int
303
+ * @return Returns f(x, y) for any given positive integers x and y.
304
+ * Note that f(x, y) is increasing with respect to both x and y.
305
+ * i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)
306
+ */
307
+
308
+ func findSolution (customFunction func (int , int ) int , z int ) (ans [][]int ) {
309
+ x , y := 1 , 1000
310
+ for x <= 1000 && y > 0 {
311
+ t := customFunction (x, y)
312
+ if t < z {
313
+ x++
314
+ } else if t > z {
315
+ y--
316
+ } else {
317
+ ans = append (ans, []int {x, y})
318
+ x, y = x+1 , y-1
248
319
}
249
320
}
250
- return res
321
+ return
322
+ }
323
+ ```
324
+
325
+ ### ** TypeScript**
326
+
327
+ ``` ts
328
+ /**
329
+ * // This is the CustomFunction's API interface.
330
+ * // You should not implement it, or speculate about its implementation
331
+ * class CustomFunction {
332
+ * f(x: number, y: number): number {}
333
+ * }
334
+ */
335
+
336
+ function findSolution(customfunction : CustomFunction , z : number ): number [][] {
337
+ const ans: number [][] = [];
338
+ for (let x = 1 ; x <= 1000 ; ++ x ) {
339
+ let l = 1 ;
340
+ let r = 1000 ;
341
+ while (l < r ) {
342
+ const mid = (l + r ) >> 1 ;
343
+ if (customfunction .f (x , mid ) >= z ) {
344
+ r = mid ;
345
+ } else {
346
+ l = mid + 1 ;
347
+ }
348
+ }
349
+ if (customfunction .f (x , l ) == z ) {
350
+ ans .push ([x , l ]);
351
+ }
352
+ }
353
+ return ans ;
354
+ }
355
+ ```
356
+
357
+ ``` ts
358
+ /**
359
+ * // This is the CustomFunction's API interface.
360
+ * // You should not implement it, or speculate about its implementation
361
+ * class CustomFunction {
362
+ * f(x: number, y: number): number {}
363
+ * }
364
+ */
365
+
366
+ function findSolution(customfunction : CustomFunction , z : number ): number [][] {
367
+ let x = 1 ;
368
+ let y = 1000 ;
369
+ const ans: number [][] = [];
370
+ while (x <= 1000 && y ) {
371
+ const t = customfunction .f (x , y );
372
+ if (t < z ) {
373
+ ++ x ;
374
+ } else if (t > z ) {
375
+ -- y ;
376
+ } else {
377
+ ans .push ([x -- , y -- ]);
378
+ }
379
+ }
380
+ return ans ;
251
381
}
252
382
```
253
383
0 commit comments