{"id":5565,"date":"2019-09-22T01:34:14","date_gmt":"2019-09-22T08:34:14","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=5565"},"modified":"2019-09-23T20:26:05","modified_gmt":"2019-09-24T03:26:05","slug":"leetcode-1201-ugly-number-iii","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/binary-search\/leetcode-1201-ugly-number-iii\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1201. Ugly Number III"},"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 1201. Ugly Number III - \u5237\u9898\u627e\u5de5\u4f5c EP270\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/gj4JevBj8-Y?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>Write a program to find the&nbsp;<code>n<\/code>-th ugly number.<\/p>\n\n\n\n<p>Ugly numbers are<strong>&nbsp;positive integers<\/strong>&nbsp;which are divisible by&nbsp;<code>a<\/code>&nbsp;<strong>or<\/strong>&nbsp;<code>b<\/code>&nbsp;<strong>or<\/strong>&nbsp;<code>c<\/code>.<\/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> n = 3, a = 2, b = 3, c = 5\n<strong>Output:<\/strong> 4\n<strong>Explanation: <\/strong>The ugly numbers are 2, 3, 4, 5, 6, 8, 9, 10... The 3rd is 4.<\/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> n = 4, a = 2, b = 3, c = 4\n<strong>Output:<\/strong> 6\n<strong>Explanation: <\/strong>The ugly numbers are 2, 3, 4, 6, 8, 9, 12... The 4th is 6.\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> n = 5, a = 2, b = 11, c = 13\n<strong>Output:<\/strong> 10\n<strong>Explanation: <\/strong>The ugly numbers are 2, 4, 6, 8, 10, 11, 12, 13... The 5th is 10.\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> n = 1000000000, a = 2, b = 217983653, c = 336916467\n<strong>Output:<\/strong> 1999999984\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= n, a, b, c &lt;= 10^9<\/code><\/li><li><code>1 &lt;= a * b * c &lt;= 10^18<\/code><\/li><li>It&#8217;s guaranteed that the result will be in range&nbsp;<code>[1,&nbsp;2 * 10^9]<\/code><\/li><\/ul>\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\/09\/1201-ep270.png\" alt=\"\" class=\"wp-image-5582\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/09\/1201-ep270.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/09\/1201-ep270-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2019\/09\/1201-ep270-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<p>Number of ugly numbers that are &lt;= m are:<\/p>\n\n\n\n<p>m \/ a + m \/ b + m \/ c &#8211; (m \/ LCM(a,b) + m \/ LCM(a, c) + m \/ LCM(b, c) + m \/ LCM(a, LCM(b, c))<\/p>\n\n\n\n<p>Time complexity: O(logn)<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 nthUglyNumber(int n, long a, long b, long c) {    \n    long l = 1;\n    long r = INT_MAX;\n    long ab = lcm(a, b);\n    long ac = lcm(a, c);\n    long bc = lcm(b, c);\n    long abc = lcm(a, bc);\n    while (l < r) {\n      long m = l + (r - l) \/ 2;\n      long k = m \/ a + m \/ b + m \/ c - m \/ ab - m \/ ac - m \/ bc + m \/ abc;      \n      if (k >= n) r = m;\n      else l = m + 1;\n    }\n    return l;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Write a program to find the&nbsp;n-th ugly number. Ugly numbers are&nbsp;positive integers&nbsp;which are divisible by&nbsp;a&nbsp;or&nbsp;b&nbsp;or&nbsp;c. Example 1: Input: n = 3, a = 2, b&#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,359,360,31,177,459],"class_list":["post-5565","post","type-post","status-publish","format-standard","hentry","category-binary-search","tag-binary-search","tag-gcd","tag-lcm","tag-math","tag-medium","tag-ologn","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5565","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=5565"}],"version-history":[{"count":4,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5565\/revisions"}],"predecessor-version":[{"id":5583,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/5565\/revisions\/5583"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=5565"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=5565"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=5565"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}