aboutsummaryrefslogtreecommitdiff
path: root/find_pos_int_soln_given_eq.py
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-08-30 10:03:07 -0600
committerJack Sangdahl <[email protected]>2025-08-30 10:03:07 -0600
commit06f004e1d27ec3a4a75220f7b61ccfe47b4db75e (patch)
treebdb210a9f31adc9596e769bceef43dcdbe6e82a9 /find_pos_int_soln_given_eq.py
parent9bdedfa9d768668c571d721d0b7a30f0eae1b58c (diff)
downloadleetcode-06f004e1d27ec3a4a75220f7b61ccfe47b4db75e.tar.gz
leetcode-06f004e1d27ec3a4a75220f7b61ccfe47b4db75e.zip
1237. find positive integer solution for a given equation
Diffstat (limited to 'find_pos_int_soln_given_eq.py')
-rw-r--r--find_pos_int_soln_given_eq.py42
1 files changed, 42 insertions, 0 deletions
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))