Find Positive Integer Solution for a Given Equation LeetCode solution in Java

1
class Solution {
2
private int binarySearch(int ans, int x, CustomFunction func) {
3
int left = 1, right = 1000;
4
while (left <= right) {
5
int mid = left + (right - left) / 2;
6

7
int res = func.f(x, mid);
8

9
if (res == ans) {
10
return mid;
11
}
12
if (res < ans) {
13
left = mid + 1;
14
} else {
15
right = mid - 1;
16
}
17
}
18
return -1;
19
}
20

21
public List<List<Integer>> findSolution(CustomFunction customfunction, int z) {
22
List<List<Integer>> res = new ArrayList<>();
23

24
for (int i = 1; i <= 1000; i++) {
25
int ans = binarySearch(z, i, customfunction);
26
if (ans != -1) {
27
List<Integer> temp = new ArrayList<>();
28
temp.add(i);
29
temp.add(ans);
30
res.add(temp);
31
}
32
if (customfunction.f(i, 1) > z) {
33
break;
34
}
35
}
36
return res;
37
}
38
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0