@@ -73,7 +73,23 @@ x=5, y=1 -> f(5, 1) = 5 * 1 = 5</pre>
73
73
74
74
<!-- 这里可写通用的实现逻辑 -->
75
75
76
- 二分查找。
76
+ ** 方法一:枚举 + 二分查找**
77
+
78
+ 根据题目我们可以知道,函数 $f(x, y)$ 是单调递增函数,因此,我们可以枚举 $x$,然后在 $[ 1,...z] $ 中二分查找 $y$,使得 $f(x, y) = z$。如果找到了,就将 $(x, y)$ 加入答案中。
79
+
80
+ 时间复杂度 $(z \log z)$,空间复杂度 $O(1)$。本题中 $z \le 100$。
81
+
82
+ ** 方法二:双指针**
83
+
84
+ 我们可以定义两个指针 $x$ 和 $y$,初始时 $x = 1$, $y = z$。
85
+
86
+ - 如果 $f(x, y) = z$,我们将 $(x, y)$ 加入答案中,然后 $x \leftarrow x + 1$, $y \leftarrow y - 1$;
87
+ - 如果 $f(x, y) \lt z$,此时对任意的 $y' \lt y$,都有 $f(x, y') \lt f(x, y) \lt z$,因此我们不能将 $y$ 减小,只能将 $x$ 增大,所以 $x \leftarrow x + 1$;
88
+ - 如果 $f(x, y) \gt z$,此时对任意的 $x' \gt x$,都有 $f(x', y) \gt f(x, y) \gt z$,因此我们不能将 $x$ 增大,只能将 $y$ 减小,所以 $y \leftarrow y - 1$。
89
+
90
+ 循环结束后,返回答案。
91
+
92
+ 时间复杂度 $O(z)$,空间复杂度 $O(1)$。本题中 $z \le 100$。
77
93
78
94
<!-- tabs:start -->
79
95
@@ -97,8 +113,8 @@ x=5, y=1 -> f(5, 1) = 5 * 1 = 5</pre>
97
113
class Solution :
98
114
def findSolution (self , customfunction : " CustomFunction" , z : int ) -> List[List[int ]]:
99
115
ans = []
100
- for x in range (1 , 1001 ):
101
- y = 1 + bisect_left(range (1 , 1001 ), z, key = lambda y : customfunction.f(x, y))
116
+ for x in range (1 , z + 1 ):
117
+ y = 1 + bisect_left(range (1 , z + 1 ), z, key = lambda y : customfunction.f(x, y))
102
118
if customfunction.f(x, y) == z:
103
119
ans.append([x, y])
104
120
return ans
@@ -113,14 +129,15 @@ class Solution:
113
129
# Note that f(x, y) is increasing with respect to both x and y.
114
130
# i.e. f(x, y) < f(x + 1, y), f(x, y) < f(x, y + 1)
115
131
def f(self, x, y):
116
-
132
+
117
133
"""
118
134
135
+
119
136
class Solution :
120
- def findSolution (self , customfunction : ' CustomFunction' , z : int ) -> List[List[int ]]:
137
+ def findSolution (self , customfunction : " CustomFunction" , z : int ) -> List[List[int ]]:
121
138
ans = []
122
- x, y = 1 , 1000
123
- while x <= 1000 and y:
139
+ x, y = 1 , z
140
+ while x <= z and y:
124
141
t = customfunction.f(x, y)
125
142
if t < z:
126
143
x += 1
@@ -151,8 +168,8 @@ class Solution:
151
168
class Solution {
152
169
public List<List<Integer > > findSolution (CustomFunction customfunction , int z ) {
153
170
List<List<Integer > > ans = new ArrayList<> ();
154
- for (int x = 1 ; x <= 1000 ; ++ x) {
155
- int l = 1 , r = 1000 ;
171
+ for (int x = 1 ; x <= z ; ++ x) {
172
+ int l = 1 , r = z ;
156
173
while (l < r) {
157
174
int mid = (l + r) >> 1 ;
158
175
if (customfunction. f(x, mid) >= z) {
@@ -185,13 +202,13 @@ class Solution:
185
202
class Solution {
186
203
public List<List<Integer > > findSolution (CustomFunction customfunction , int z ) {
187
204
List<List<Integer > > ans = new ArrayList<> ();
188
- int x = 1 , y = 1000 ;
189
- while (x <= 1000 && y > 0 ) {
205
+ int x = 1 , y = z ;
206
+ while (x <= z && y > 0 ) {
190
207
int t = customfunction. f(x, y);
191
208
if (t < z) {
192
- ++ x ;
209
+ x ++ ;
193
210
} else if (t > z) {
194
- -- y ;
211
+ y -- ;
195
212
} else {
196
213
ans. add(Arrays . asList(x++ , y-- ));
197
214
}
@@ -220,8 +237,8 @@ class Solution {
220
237
public:
221
238
vector<vector<int >> findSolution(CustomFunction& customfunction, int z) {
222
239
vector<vector<int >> ans;
223
- for (int x = 1; x <= 1000 ; ++x) {
224
- int l = 1, r = 1000 ;
240
+ for (int x = 1; x <= z ; ++x) {
241
+ int l = 1, r = z ;
225
242
while (l < r) {
226
243
int mid = (l + r) >> 1;
227
244
if (customfunction.f(x, mid) >= z) {
@@ -256,13 +273,13 @@ class Solution {
256
273
public:
257
274
vector<vector<int>> findSolution(CustomFunction& customfunction, int z) {
258
275
vector<vector<int>> ans;
259
- int x = 1, y = 1000 ;
260
- while (x <= 1000 && y) {
276
+ int x = 1, y = z ;
277
+ while (x <= z && y) {
261
278
int t = customfunction.f(x, y);
262
279
if (t < z) {
263
- ++x ;
280
+ x++ ;
264
281
} else if (t > z) {
265
- --y ;
282
+ y-- ;
266
283
} else {
267
284
ans.push_back({x++, y--});
268
285
}
@@ -285,8 +302,8 @@ public:
285
302
*/
286
303
287
304
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 })
305
+ for x := 1 ; x <= z ; x++ {
306
+ y := 1 + sort.Search (z , func (y int ) bool { return customFunction (x, y+1 ) >= z })
290
307
if customFunction (x, y) == z {
291
308
ans = append (ans, []int {x, y})
292
309
}
@@ -306,8 +323,8 @@ func findSolution(customFunction func(int, int) int, z int) (ans [][]int) {
306
323
*/
307
324
308
325
func findSolution (customFunction func (int , int ) int , z int ) (ans [][]int ) {
309
- x , y := 1 , 1000
310
- for x <= 1000 && y > 0 {
326
+ x , y := 1 , z
327
+ for x <= z && y > 0 {
311
328
t := customFunction (x, y)
312
329
if t < z {
313
330
x++
@@ -335,9 +352,9 @@ func findSolution(customFunction func(int, int) int, z int) (ans [][]int) {
335
352
336
353
function findSolution(customfunction : CustomFunction , z : number ): number [][] {
337
354
const ans: number [][] = [];
338
- for (let x = 1 ; x <= 1000 ; ++ x ) {
355
+ for (let x = 1 ; x <= z ; ++ x ) {
339
356
let l = 1 ;
340
- let r = 1000 ;
357
+ let r = z ;
341
358
while (l < r ) {
342
359
const mid = (l + r ) >> 1 ;
343
360
if (customfunction .f (x , mid ) >= z ) {
@@ -365,9 +382,9 @@ function findSolution(customfunction: CustomFunction, z: number): number[][] {
365
382
366
383
function findSolution(customfunction : CustomFunction , z : number ): number [][] {
367
384
let x = 1 ;
368
- let y = 1000 ;
385
+ let y = z ;
369
386
const ans: number [][] = [];
370
- while (x <= 1000 && y ) {
387
+ while (x <= z && y ) {
371
388
const t = customfunction .f (x , y );
372
389
if (t < z ) {
373
390
++ x ;
0 commit comments