{"id":5785,"date":"2019-10-27T20:16:56","date_gmt":"2019-10-28T03:16:56","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5785"},"modified":"2019-10-27T20:18:44","modified_gmt":"2019-10-28T03:18:44","slug":"leetcode-1237-find-positive-integer-solution-for-a-given-equation","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/brute-force\/leetcode-1237-find-positive-integer-solution-for-a-given-equation\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1237. Find Positive Integer Solution for a Given Equation"},"content":{"rendered":"\n<p>Given a&nbsp;function&nbsp;&nbsp;<code>f(x, y)<\/code>&nbsp;and a value&nbsp;<code>z<\/code>, return all positive integer&nbsp;pairs&nbsp;<code>x<\/code>&nbsp;and&nbsp;<code>y<\/code>&nbsp;where&nbsp;<code>f(x,y) == z<\/code>.<\/p>\n\n\n\n<p>The function is constantly increasing, i.e.:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>f(x, y) &lt; f(x + 1, y)<\/code><\/li><li><code>f(x, y) &lt; f(x, y + 1)<\/code><\/li><\/ul>\n\n\n\n<p>The function interface is defined like this:&nbsp;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\">interface CustomFunction {\npublic:\n&nbsp; \/\/ Returns positive integer f(x, y) for any given positive integer x and y.\n&nbsp; int f(int x, int y);\n};\n<\/pre>\n\n\n\n<p>For custom testing purposes you&#8217;re given an integer&nbsp;<code>function_id<\/code>&nbsp;and a target&nbsp;<code>z<\/code>&nbsp;as input, where&nbsp;<code>function_id<\/code>&nbsp;represent one function from an secret internal list, on the examples you&#8217;ll know only two functions from the list. &nbsp;<\/p>\n\n\n\n<p>You may return the solutions in any order.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> function_id = 1, z = 5\n<strong>Output:<\/strong> [[1,4],[2,3],[3,2],[4,1]]\n<strong>Explanation:<\/strong>&nbsp;function_id = 1 means that f(x, y) = x + y<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> function_id = 2, z = 5\n<strong>Output:<\/strong> [[1,5],[5,1]]\n<strong>Explanation:<\/strong>&nbsp;function_id = 2 means that f(x, y) = x * y\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= function_id &lt;= 9<\/code><\/li><li><code>1 &lt;= z &lt;= 100<\/code><\/li><li>It&#8217;s guaranteed that the solutions of&nbsp;<code>f(x, y) == z<\/code>&nbsp;will be on the range&nbsp;<code>1 &lt;= x, y &lt;= 1000<\/code><\/li><li>It&#8217;s also guaranteed that&nbsp;<code>f(x, y)<\/code>&nbsp;will fit in 32 bit signed integer if&nbsp;<code>1 &lt;= x, y &lt;= 1000<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution1 : Brute Force<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(1000*1000)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"C++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  vector<vector<int>> findSolution(CustomFunction& customfunction, int z) {\n    vector<vector<int>> ans;    \n    for (int x = 1; x <= 1000; ++x) {\n      if (customfunction.f(x, 1) > z) break;\n      for (int y = 1; y <= 1000; ++y) {\n        int t = customfunction.f(x, y);\n        if (t > z) break;\n        if (t == z) ans.push_back({x, y});        \n      }\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a&nbsp;function&nbsp;&nbsp;f(x, y)&nbsp;and a value&nbsp;z, return all positive integer&nbsp;pairs&nbsp;x&nbsp;and&nbsp;y&nbsp;where&nbsp;f(x,y) == z. The function is constantly increasing, i.e.: f(x, y) &lt; f(x + 1, y) f(x,&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[513],"tags":[106,222,253,366],"class_list":["post-5785","post","type-post","status-publish","format-standard","hentry","category-brute-force","tag-brute-force","tag-easy","tag-interactive","tag-omn","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5785","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=5785"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5785\/revisions"}],"predecessor-version":[{"id":5787,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5785\/revisions\/5787"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5785"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5785"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5785"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}