{"id":5942,"date":"2019-12-08T02:31:54","date_gmt":"2019-12-08T10:31:54","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5942"},"modified":"2019-12-08T14:44:33","modified_gmt":"2019-12-08T22:44:33","slug":"leetcode-1283-find-the-smallest-divisor-given-a-threshold","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/binary-search\/leetcode-1283-find-the-smallest-divisor-given-a-threshold\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1283. Find the Smallest Divisor Given a Threshold"},"content":{"rendered":"\n<figure class=\"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-4-3 wp-has-aspect-ratio\"><div class=\"wp-block-embed__wrapper\">\n<iframe loading=\"lazy\" title=\"\u82b1\u82b1\u9171 LeetCode 1283. Find the Smallest Divisor Given a Threshold - \u5237\u9898\u627e\u5de5\u4f5c EP282\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/ZBOGrSS_xiM?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>Given an array of integers&nbsp;<code>nums<\/code>&nbsp;and an integer&nbsp;<code>threshold<\/code>, we will choose a positive integer divisor and&nbsp;divide&nbsp;all the array by it and sum the result of the division. Find the&nbsp;<strong>smallest<\/strong>&nbsp;divisor such that the result mentioned above is less than&nbsp;or equal to&nbsp;<code>threshold<\/code>.<\/p>\n\n\n\n<p>Each&nbsp;result of&nbsp;division is rounded&nbsp;to the nearest integer greater than or equal to that element.&nbsp;(For example: 7\/3 = 3 and 10\/2 = 5).<\/p>\n\n\n\n<p>It is guaranteed that there will be an answer.<\/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> nums = [1,2,5,9], threshold = 6\n<strong>Output:<\/strong> 5\n<strong>Explanation:<\/strong> We can get a sum to 17 (1+2+5+9) if the divisor is 1. \nIf the divisor is 4 we can get a sum to 7 (1+1+2+3) and if the divisor is 5 the sum will be 5 (1+1+1+2). \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> nums = [2,3,5,7,11], threshold = 11\n<strong>Output:<\/strong> 3\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> nums = [19], threshold = 5\n<strong>Output:<\/strong> 4<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Binary Search<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"960\" height=\"540\" src=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/12\/1283-ep282.png\" alt=\"\" class=\"wp-image-5949\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/12\/1283-ep282.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/12\/1283-ep282-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/12\/1283-ep282-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<p>Time complexity: O(nlogk)<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  int smallestDivisor(vector<int>& nums, int threshold) {\n    auto sums = [&](int d) {\n      int s = 0;\n      for (int n : nums)\n        s += (n + (d - 1)) \/ d;\n      return s;\n    };\n    int l = 1;\n    int r = 1e6;\n    while (l < r) {\n      int m = l + (r - l) \/ 2;\n      if (sums(m) <= threshold)\n        r = m;\n      else\n        l = m + 1;\n    }\n    return l;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an array of integers&nbsp;nums&nbsp;and an integer&nbsp;threshold, we will choose a positive integer divisor and&nbsp;divide&nbsp;all the array by it and sum the result of the&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[149],"tags":[52,177],"class_list":["post-5942","post","type-post","status-publish","format-standard","hentry","category-binary-search","tag-binary-search","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5942","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=5942"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5942\/revisions"}],"predecessor-version":[{"id":5950,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5942\/revisions\/5950"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5942"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5942"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5942"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}