{"id":8666,"date":"2021-11-04T23:00:14","date_gmt":"2021-11-05T06:00:14","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8666"},"modified":"2021-11-04T23:01:44","modified_gmt":"2021-11-05T06:01:44","slug":"leetcode-2059-minimum-operations-to-convert-number","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/searching\/leetcode-2059-minimum-operations-to-convert-number\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2059. Minimum Operations to Convert Number"},"content":{"rendered":"\n<p>You are given a&nbsp;<strong>0-indexed<\/strong>&nbsp;integer array&nbsp;<code>nums<\/code>&nbsp;containing&nbsp;<strong>distinct<\/strong>&nbsp;numbers, an integer&nbsp;<code>start<\/code>, and an integer&nbsp;<code>goal<\/code>. There is an integer&nbsp;<code>x<\/code>&nbsp;that is initially set to&nbsp;<code>start<\/code>, and you want to perform operations on&nbsp;<code>x<\/code>&nbsp;such that it is converted to&nbsp;<code>goal<\/code>. You can perform the following operation repeatedly on the number&nbsp;<code>x<\/code>:<\/p>\n\n\n\n<p>If&nbsp;<code>0 &lt;= x &lt;= 1000<\/code>, then for any index&nbsp;<code>i<\/code>&nbsp;in the array (<code>0 &lt;= i &lt; nums.length<\/code>), you can set&nbsp;<code>x<\/code>&nbsp;to any of the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>x + nums[i]<\/code><\/li><li><code>x - nums[i]<\/code><\/li><li><code>x ^ nums[i]<\/code>&nbsp;(bitwise-XOR)<\/li><\/ul>\n\n\n\n<p>Note that you can use each&nbsp;<code>nums[i]<\/code>&nbsp;any number of times in any order. Operations that set&nbsp;<code>x<\/code>&nbsp;to be out of the range&nbsp;<code>0 &lt;= x &lt;= 1000<\/code>&nbsp;are valid, but no more operations can be done afterward.<\/p>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>minimum<\/strong>&nbsp;number of operations needed to convert&nbsp;<\/em><code>x = start<\/code><em>&nbsp;into&nbsp;<\/em><code>goal<\/code><em>, and&nbsp;<\/em><code>-1<\/code><em>&nbsp;if it is not possible<\/em>.<\/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,3], start = 6, goal = 4\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong>\nWe can go from 6 \u2192 7 \u2192 4 with the following 2 operations.\n- 6 ^ 1 = 7\n- 7 ^ 3 = 4\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,4,12], start = 2, goal = 12\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong>\nWe can go from 2 \u2192 14 \u2192 12 with the following 2 operations.\n- 2 + 12 = 14\n- 14 - 2 = 12\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 = [3,5,7], start = 0, goal = -4\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong>\nWe can go from 0 \u2192 3 \u2192 -4 with the following 2 operations. \n- 0 + 3 = 3\n- 3 - 7 = -4\nNote that the last operation sets x out of the range 0 &lt;= x &lt;= 1000, which is valid.\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> nums = [2,8,16], start = 0, goal = 1\n<strong>Output:<\/strong> -1\n<strong>Explanation:<\/strong>\nThere is no way to convert 0 into 1.<\/pre>\n\n\n\n<p><strong>Example 5:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><strong>Input:<\/strong> nums = [1], start = 0, goal = 3\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> \nWe can go from 0 \u2192 1 \u2192 2 \u2192 3 with the following 3 operations. \n- 0 + 1 = 1 \n- 1 + 1 = 2\n- 2 + 1 = 3\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= nums.length &lt;= 1000<\/code><\/li><li><code>-10<sup>9<\/sup>&nbsp;&lt;= nums[i], goal &lt;= 10<sup>9<\/sup><\/code><\/li><li><code>0 &lt;= start &lt;= 1000<\/code><\/li><li><code>start != goal<\/code><\/li><li>All the integers in&nbsp;<code>nums<\/code>&nbsp;are distinct.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: BFS<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n*m)<br>Space complexity: O(m)<\/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 minimumOperations(vector<int>& nums, int start, int goal) {\n    vector<int> seen(1001);\n    queue<int> q{{start}};  \n    for (int ans = 1, s = 1; !q.empty(); ++ans, s = q.size()) {\n      while (s--) {\n        int x = q.front(); q.pop();\n        for (int n : nums)\n          for (int t : {x + n, x - n, x ^ n})\n            if (t == goal) \n              return ans;\n            else if (t < 0 || t > 1000 || seen[t]++)\n              continue;\n            else\n              q.push(t);\n      }\n    }\n    return -1;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a&nbsp;0-indexed&nbsp;integer array&nbsp;nums&nbsp;containing&nbsp;distinct&nbsp;numbers, an integer&nbsp;start, and an integer&nbsp;goal. There is an integer&nbsp;x&nbsp;that is initially set to&nbsp;start, and you want to perform operations on&nbsp;x&nbsp;such&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44],"tags":[34,177],"class_list":["post-8666","post","type-post","status-publish","format-standard","hentry","category-searching","tag-bfs","tag-medium","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8666","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=8666"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8666\/revisions"}],"predecessor-version":[{"id":8668,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8666\/revisions\/8668"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8666"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8666"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8666"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}