44import java .util .Arrays ;
55import java .util .List ;
66
7- /**
8- * 1237. Find Positive Integer Solution for a Given Equation
9- *
10- * Given a function f(x, y) and a value z, return all positive integer pairs x and y where f(x,y) == z.
11- * The function is constantly increasing, i.e.:
12- *
13- * f(x, y) < f(x + 1, y)
14- * f(x, y) < f(x, y + 1)
15- * The function interface is defined like this:
16- *
17- * interface CustomFunction {
18- * public:
19- * // Returns positive integer f(x, y) for any given positive integer x and y.
20- * int f(int x, int y);
21- * };
22- * For custom testing purposes you're given an integer function_id and a target z as input,
23- * where function_id represent one function from an secret internal list, on the examples you'll know only two functions from the list.
24- * You may return the solutions in any order.
25- *
26- * Example 1:
27- * Input: function_id = 1, z = 5
28- * Output: [[1,4],[2,3],[3,2],[4,1]]
29- * Explanation: function_id = 1 means that f(x, y) = x + y
30- *
31- * Example 2:
32- * Input: function_id = 2, z = 5
33- * Output: [[1,5],[5,1]]
34- * Explanation: function_id = 2 means that f(x, y) = x * y
35- *
36- * Constraints:
37- * 1 <= function_id <= 9
38- * 1 <= z <= 100
39- * It's guaranteed that the solutions of f(x, y) == z will be on the range 1 <= x, y <= 1000
40- * It's also guaranteed that f(x, y) will fit in 32 bit signed integer if 1 <= x, y <= 1000
41- */
427public class _1237 {
438
449// This is the custom function interface.
@@ -51,8 +16,10 @@ abstract class CustomFunction {
5116 }
5217
5318public static class Solution1 {
54- /**Time: O(x*y)
55- * Space: O(1)*/
19+ /**
20+ * Time: O(x*y)
21+ * Space: O(1)
22+ */
5623public List <List <Integer >>findSolution (CustomFunction customfunction ,int z ) {
5724List <List <Integer >>result =new ArrayList <>();
5825for (int x =1 ;x <=1000 ;x ++) {
@@ -69,7 +36,7 @@ public List<List<Integer>> findSolution(CustomFunction customfunction, int z) {
6936public static class Solution2 {
7037/**
7138 * linear search
72- *
39+ *<p>
7340 * Time: O(x + y)
7441 * Space: O(1)
7542 */
@@ -94,10 +61,10 @@ public List<List<Integer>> findSolution(CustomFunction customfunction, int z) {
9461public static class Solution3 {
9562/**
9663 * binary search
97- *
64+ * <p>
9865 * Time: O(xlogy)
9966 * Space: O(1)
100- * * /
67+ */
10168public List <List <Integer >>findSolution (CustomFunction customfunction ,int z ) {
10269List <List <Integer >>result =new ArrayList <>();
10370for (int x =1 ;x <=1000 ;x ++) {