{"id":7645,"date":"2020-11-14T10:26:25","date_gmt":"2020-11-14T18:26:25","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7645"},"modified":"2020-11-14T10:27:16","modified_gmt":"2020-11-14T18:27:16","slug":"leetcode-1652-defuse-the-bomb","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-1652-defuse-the-bomb\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1652. Defuse the Bomb"},"content":{"rendered":"\n<p>You have a bomb to defuse, and your time is running out! Your informer will provide you with a&nbsp;<strong>circular<\/strong>&nbsp;array&nbsp;<code>code<\/code>&nbsp;of length of&nbsp;<code>n<\/code>&nbsp;and a key&nbsp;<code>k<\/code>.<\/p>\n\n\n\n<p>To decrypt the code, you must replace every number. All the numbers are replaced&nbsp;<strong>simultaneously<\/strong>.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>If&nbsp;<code>k &gt; 0<\/code>, replace the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;number with the sum of the&nbsp;<strong>next<\/strong>&nbsp;<code>k<\/code>&nbsp;numbers.<\/li><li>If&nbsp;<code>k &lt; 0<\/code>, replace the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;number with the sum of the&nbsp;<strong>previous<\/strong>&nbsp;<code>k<\/code>&nbsp;numbers.<\/li><li>If&nbsp;<code>k == 0<\/code>, replace the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;number with&nbsp;<code>0<\/code>.<\/li><\/ul>\n\n\n\n<p>As&nbsp;<code>code<\/code>&nbsp;is circular, the next element of&nbsp;<code>code[n-1]<\/code>&nbsp;is&nbsp;<code>code[0]<\/code>, and the previous element of&nbsp;<code>code[0]<\/code>&nbsp;is&nbsp;<code>code[n-1]<\/code>.<\/p>\n\n\n\n<p>Given the&nbsp;<strong>circular<\/strong>&nbsp;array&nbsp;<code>code<\/code>&nbsp;and an integer key&nbsp;<code>k<\/code>, return&nbsp;<em>the decrypted code to defuse the bomb<\/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> code = [5,7,1,4], k = 3\n<strong>Output:<\/strong> [12,10,16,13]\n<strong>Explanation:<\/strong> Each number is replaced by the sum of the next 3 numbers. The decrypted code is [7+1+4, 1+4+5, 4+5+7, 5+7+1]. Notice that the numbers wrap around.\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> code = [1,2,3,4], k = 0\n<strong>Output:<\/strong> [0,0,0,0]\n<strong>Explanation:<\/strong> When k is zero, the numbers are replaced by 0. \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> code = [2,4,9,3], k = -2\n<strong>Output:<\/strong> [12,5,6,13]\n<strong>Explanation:<\/strong> The decrypted code is [3+9, 2+3, 4+2, 9+4]. Notice that the numbers wrap around again. If k is negative, the sum is of the <strong>previous<\/strong> numbers.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>n == code.length<\/code><\/li><li><code>1 &lt;= n&nbsp;&lt;= 100<\/code><\/li><li><code>1 &lt;= code[i] &lt;= 100<\/code><\/li><li><code>-(n - 1) &lt;= k &lt;= n - 1<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: Simulation<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n*k)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\nclass Solution {\npublic:\n  vector<int> decrypt(vector<int>& code, int k) {    \n    const int n = code.size();\n    vector<int> ans(n);\n    int sign = k > 0 ? 1 : -1;\n    for (int i = 0; i < n; ++i)\n      for (int j = 1; j <= abs(k); ++j)\n        ans[i] += code[(i + j * sign + n) % n];\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You have a bomb to defuse, and your time is running out! Your informer will provide you with a&nbsp;circular&nbsp;array&nbsp;code&nbsp;of length of&nbsp;n&nbsp;and a key&nbsp;k. To decrypt&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[20,222,179],"class_list":["post-7645","post","type-post","status-publish","format-standard","hentry","category-array","tag-array","tag-easy","tag-simulation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7645","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=7645"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7645\/revisions"}],"predecessor-version":[{"id":7647,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7645\/revisions\/7647"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7645"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7645"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7645"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}