{"id":4547,"date":"2018-12-28T12:14:11","date_gmt":"2018-12-28T20:14:11","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=4547"},"modified":"2018-12-29T09:31:50","modified_gmt":"2018-12-29T17:31:50","slug":"leetcode-964-least-operators-to-express-number","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/uncategorized\/leetcode-964-least-operators-to-express-number\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 964. Least Operators to Express Number"},"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 964. Least Operators to Express Number - \u5237\u9898\u627e\u5de5\u4f5c EP237\" width=\"500\" height=\"375\" src=\"https:\/\/www.youtube.com\/embed\/BTUYfPf2WY0?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 a single positive integer&nbsp;<code>x<\/code>, we will write an expression of the form&nbsp;<code>x (op1) x (op2) x (op3) x ...<\/code>&nbsp;where each operator&nbsp;<code>op1<\/code>,&nbsp;<code>op2<\/code>, etc. is either addition, subtraction, multiplication, or division (<code>+<\/code>,&nbsp;<code>-<\/code>,&nbsp;<code>*<\/code>, or&nbsp;<code>\/)<\/code>.&nbsp; For example, with&nbsp;<code>x = 3<\/code>, we might write&nbsp;<code>3 * 3 \/ 3 + 3 - 3<\/code>&nbsp;which is a value of&nbsp;3.<\/p>\n\n\n\n<p>When writing such an expression, we adhere to the following conventions:<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>The division operator (<code>\/<\/code>) returns rational numbers.<\/li><li>There are no parentheses placed anywhere.<\/li><li>We use the usual order of operations: multiplication and division happens before addition and subtraction.<\/li><li>It&#8217;s not allowed to use the unary negation&nbsp;operator (<code>-<\/code>).&nbsp; For example, &#8220;<code>x&nbsp;- x<\/code>&#8221;&nbsp;is a valid expression as it only uses subtraction, but &#8220;<code>-x +&nbsp;x<\/code>&#8221; is not because it uses negation.<\/li><\/ol>\n\n\n\n<p>We would like to write an expression with the least number of operators such that the expression equals the given&nbsp;<code>target<\/code>.&nbsp; Return the least number of operators used.<\/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>x = 3, target = 19 <br><strong>Output: <\/strong>5 <br><strong>Explanation: <\/strong>3 * 3 + 3 * 3 + 3 \/ 3.  The expression contains 5 operations. <\/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>x = 5, target = 501 <br><strong>Output: <\/strong>8 <br><strong>Explanation: <\/strong>5 * 5 * 5 * 5 - 5 * 5 * 5 + 5 \/ 5.  The expression contains 8 operations. <\/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>x = 100, target = 100000000 <br><strong>Output: <\/strong>3 <br><strong>Explanation: <\/strong>100 * 100 * 100 * 100.  The expression contains 3 operations.<\/pre>\n\n\n\n<p><strong>Note:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>2 &lt;= x &lt;= 100<\/code><\/li><li><code>1 &lt;= target &lt;= 2 * 10^8<\/code><\/li><\/ul>\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\/2018\/12\/964-ep237-1.png\" alt=\"\" class=\"wp-image-4556\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/12\/964-ep237-1.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/12\/964-ep237-1-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/12\/964-ep237-1-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\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\/2018\/12\/964-ep237-2.png\" alt=\"\" class=\"wp-image-4551\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/12\/964-ep237-2.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/12\/964-ep237-2-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/12\/964-ep237-2-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>Solution:&nbsp;Dijkstra<\/strong><\/h1>\n\n\n\n<p>Find the shortest path from target to 0 using ops.<\/p>\n\n\n\n<p>Time complexity: O(nlogn)<br>Space complexity: O(nlogn)  <br>n = x * log(t) \/ log(x)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++\/set<\/h2>\n<div class=\"tabcontent\">\n\n<pre>\nclass Solution {\npublic:\n  int leastOpsExpressTarget(int x, int target) {\n    set<pair<int, int>> q; \/\/ # x -> number\n    unordered_set<int> s;\n    q.emplace(0, target);\n    while (!q.empty()) {\n      const auto it = begin(q);\n      const int c = it->first;\n      const int t = it->second;\n      q.erase(it);\n      if (t == 0) return c - 1;\n      if (s.count(t)) continue;\n      s.insert(t);      \n      int n = log(t) \/ log(x);\n      int l = t - pow(x, n);\n      q.emplace(c + (n == 0 ? 2 : n), l);\n      int r = pow(x, n + 1) - t;\n      q.emplace(c + n + 1, r);\n    }\n    return -1;\n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">C++\/heap<\/h2>\n<div class=\"tabcontent\">\n\n<pre>\nclass Solution {\npublic:\n    int leastOpsExpressTarget(int x, int target) {\n      priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;\n      unordered_set<int> s;\n      q.emplace(0, target);\n      while (!q.empty()) {\n        const int c = q.top().first;\n        const int t = q.top().second;\n        q.pop();\n        if (t == 0) return c - 1;\n        if (s.count(t)) continue;\n        s.insert(t);\n        int n = log(t) \/ log(x);\n        int l = t - pow(x, n);        \n        q.emplace(c + (n == 0 ? 2 : n), l);\n        int r = pow(x, n + 1) - t;        \n        q.emplace(c + n + 1, r);\n      }\n      return -1;\n    }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h1 class=\"wp-block-heading\"><strong>Solution 2:&nbsp;DFS&nbsp;+&nbsp;Memoization<\/strong><\/h1>\n\n\n\n<p>Time complexity: O(x * log(t)\/log(x))<br>Space complexity: O(x * log(t)\/log(x))<\/p>\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\/2018\/12\/964-ep237-3.png\" alt=\"\" class=\"wp-image-4558\" srcset=\"https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/12\/964-ep237-3.png 960w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/12\/964-ep237-3-300x169.png 300w, https:\/\/zxi.mytechroad.com\/blog\/wp-content\/uploads\/2018\/12\/964-ep237-3-768x432.png 768w\" sizes=\"auto, (max-width: 960px) 100vw, 960px\" \/><\/figure>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre>\n\/\/ Author: Huahua, running time: 8 ms\nclass Solution {\npublic:\n  int leastOpsExpressTarget(int x, int target) {\n    return dp(x, target);\n  }\nprivate:\n  unordered_map<int, int> m_;\n  int dp(int x, int t) {\n    if (t == 0) return 0;\n    if (t < x) return min(2 * t - 1, 2 * (x - t));\n    if (m_.count(t)) return m_.at(t);\n    int k = log(t) \/ log(x);\n    long p = pow(x, k);\n    if (t == p) return m_[t] = k - 1;\n    int ans = dp(x, t - p) + k; \/\/ t - p < t\n    long left = p * x - t;\n    if (left < t) \/\/ only rely on smaller sub-problems\n      ans = min(ans, dp(x, left) + k + 1);\n    return m_[t] = ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a single positive integer&nbsp;x, we will write an expression of the form&nbsp;x (op1) x (op2) x (op3) x &#8230;&nbsp;where each operator&nbsp;op1,&nbsp;op2, etc. is either&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4547","post","type-post","status-publish","format-standard","hentry","category-uncategorized","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4547","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=4547"}],"version-history":[{"count":7,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4547\/revisions"}],"predecessor-version":[{"id":4562,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/4547\/revisions\/4562"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=4547"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=4547"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=4547"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}