{"id":6373,"date":"2020-02-23T12:00:46","date_gmt":"2020-02-23T20:00:46","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=6373"},"modified":"2020-02-23T12:02:52","modified_gmt":"2020-02-23T20:02:52","slug":"leetcode-1362-closest-divisors","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-1362-closest-divisors\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1362. Closest Divisors"},"content":{"rendered":"\n<p>Given an integer&nbsp;<code>num<\/code>, find the closest two integers in absolute difference whose product equals&nbsp;<code>num + 1<\/code>&nbsp;or&nbsp;<code>num + 2<\/code>.<\/p>\n\n\n\n<p>Return the two integers 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> num = 8\n<strong>Output:<\/strong> [3,3]\n<strong>Explanation:<\/strong> For num + 1 = 9, the closest divisors are 3 &amp; 3, for num + 2 = 10, the closest divisors are 2 &amp; 5, hence 3 &amp; 3 is chosen.\n<\/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> num = 123\n<strong>Output:<\/strong> [5,25]\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> num = 999\n<strong>Output:<\/strong> [40,25]\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= num &lt;= 10^9<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Brute Force<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(sqrt(n))<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<int> closestDivisors(int num) {\n    auto divisors = [](int n) {\n      for (int i = sqrt(n); i >= 2; --i)\n        if (n % i == 0) return vector<int>{i, n \/ i};\n      return vector<int>{1, n};\n    };\n    \n    auto ans1 = divisors(num + 1);\n    auto ans2 = divisors(num + 2);\n    return ans1[1] - ans1[0] < ans2[1] - ans2[0] ? ans1 : ans2;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\">\n# Author: Huahua\nclass Solution:\n  def closestDivisors(self, num: int) -> List[int]:\n    def divisors(n: int) -> List[int]:\n      for i in range(int(math.sqrt(n)), 1, -1):\n        if n % i == 0: return [i, n \/\/ i]\n      return [1, n]\n    \n    a1, a2 = divisors(num + 1), divisors(num + 2)\n    return a1 if a1[1] - a1[0] < a2[1] - a2[0] else a2<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an integer&nbsp;num, find the closest two integers in absolute difference whose product equals&nbsp;num + 1&nbsp;or&nbsp;num + 2. Return the two integers in any order.&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[31,177,561,61],"class_list":["post-6373","post","type-post","status-publish","format-standard","hentry","category-math","tag-math","tag-medium","tag-osqrtn","tag-sqrt","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6373","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=6373"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6373\/revisions"}],"predecessor-version":[{"id":6375,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/6373\/revisions\/6375"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=6373"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=6373"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=6373"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}