diff options
-rw-r--r-- | find_pos_int_soln_given_eq.c | 59 | ||||
-rw-r--r-- | find_pos_int_soln_given_eq.py | 42 |
2 files changed, 101 insertions, 0 deletions
diff --git a/find_pos_int_soln_given_eq.c b/find_pos_int_soln_given_eq.c new file mode 100644 index 0000000..b17ecf6 --- /dev/null +++ b/find_pos_int_soln_given_eq.c @@ -0,0 +1,59 @@ +// 1237. Find Positive Integer Solution for a Given Equation +#include "leetcode.h" + +/* + * given a callable function 'f(x, y)' with a hidden formula and a value 'z', + * reverse engineer the formula and return all positive integer pairs 'x' and + * 'y' where 'f(x, y) == z'. you may return the pairs in any order. while the + * exact formula is hidden, the function is monotonically increasing. + */ + +int function_id = 0; + +int customFunction(int x, int y) { + switch (function_id) { + case 1: + return x + y; + case 2: + return x * y; + } +} + +int **findSolution(int (*customFunction)(int, int), int z, int *returnSize, + int **returnColumnSizes) { + int **ans = (int **)malloc(z * sizeof(int *)), k = 0; + int *cols = *returnColumnSizes = (int *)malloc(z * sizeof(int)); + for (int i = 1; i <= z; i++) + for (int j = z; j >= 1; j--) + if (customFunction(i, j) == z) { + (ans[k] = (int *)malloc(sizeof(int[cols[k] = 2])))[0] = i; + ans[k++][1] = j; + } + *returnSize = k; + return ans; +} + +int main() { + function_id = 1; + int rs1, *rcs1, **fs1 = findSolution(customFunction, 5, &rs1, &rcs1); + for (int i = 0; i < rs1; i++) { + for (int j = 0; j < rcs1[i]; j++) + printf("%d ", fs1[i][j]); // expect: [[1,4],[2,3],[3,2],[4,1]] + printf("\n"); + } + function_id = 2; + int rs2, *rcs2, **fs2 = findSolution(customFunction, 5, &rs2, &rcs2); + for (int i = 0; i < rs2; i++) { + for (int j = 0; j < rcs2[i]; j++) + printf("%d ", fs2[i][j]); // expect: [[1,5],[5,1]] + printf("\n"); + } + for (int i = 0; i < rs1; i++) + free(fs1[i]); + free(rcs1); + free(fs1); + for (int i = 0; i < rs2; i++) + free(fs2[i]); + free(rcs2); + free(fs2); +} diff --git a/find_pos_int_soln_given_eq.py b/find_pos_int_soln_given_eq.py new file mode 100644 index 0000000..5474513 --- /dev/null +++ b/find_pos_int_soln_given_eq.py @@ -0,0 +1,42 @@ +# 1237. Find Positive Integer Solution for a Given Equation + +""" +given a callable function 'f(x, y)' with a hidden formula and a value 'z', +reverse engineer the formula and return all positive integer pairs 'x' and +'y' where 'f(x, y) == z'. you may return the pairs in any order. while the +exact formula is hidden, the function is monotonically increasing. +""" + +function_id = 0 + + +class customfunction(object): + def f(self, x, y): + if function_id == 1: + return x + y + if function_id == 2: + return x * y + + +class Solution(object): + def findSolution(self, customfunction, z): + """ + :type num: int + :type z: int + :rtype: List[List[int]] + """ + ans, y = [], 1000 + for x in range(1, 1001): + while y > 1 and customfunction.f(x, y) > z: + y -= 1 + if customfunction.f(x, y) == z: + ans.append([x, y]) + return ans + + +if __name__ == "__main__": + obj = Solution() + function_id = 1 + print(obj.findSolution(customfunction, 5)) + function_id = 2 + print(obj.findSolution(customfunction, 5)) |