{"id":8284,"date":"2021-03-27T21:56:23","date_gmt":"2021-03-28T04:56:23","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8284"},"modified":"2021-03-27T22:23:19","modified_gmt":"2021-03-28T05:23:19","slug":"leetcode-1806-minimum-number-of-operations-to-reinitialize-a-permutation","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-1806-minimum-number-of-operations-to-reinitialize-a-permutation\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1806. Minimum Number of Operations to Reinitialize a Permutation"},"content":{"rendered":"\n<p>You are given an&nbsp;<strong>even<\/strong>&nbsp;integer&nbsp;<code>n<\/code>\u200b\u200b\u200b\u200b\u200b\u200b. You initially have a permutation&nbsp;<code>perm<\/code>&nbsp;of size&nbsp;<code>n<\/code>\u200b\u200b where&nbsp;<code>perm[i] == i<\/code>\u200b&nbsp;<strong>(0-indexed)<\/strong>\u200b\u200b\u200b\u200b.<\/p>\n\n\n\n<p>In one operation, you will create a new array&nbsp;<code>arr<\/code>, and for each&nbsp;<code>i<\/code>:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>If&nbsp;<code>i % 2 == 0<\/code>, then&nbsp;<code>arr[i] = perm[i \/ 2]<\/code>.<\/li><li>If&nbsp;<code>i % 2 == 1<\/code>, then&nbsp;<code>arr[i] = perm[n \/ 2 + (i - 1) \/ 2]<\/code>.<\/li><\/ul>\n\n\n\n<p>You will then assign&nbsp;<code>arr<\/code>\u200b\u200b\u200b\u200b to&nbsp;<code>perm<\/code>.<\/p>\n\n\n\n<p>Return&nbsp;<em>the minimum&nbsp;<strong>non-zero<\/strong>&nbsp;number of operations you need to perform on&nbsp;<\/em><code>perm<\/code><em>&nbsp;to return the permutation to its initial value.<\/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> n = 2\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong> prem = [0,1] initially.\nAfter the 1<sup>st<\/sup> operation, prem = [0,1]\nSo it takes only 1 operation.\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> n = 4\n<strong>Output:<\/strong> 2\n<strong>Explanation:<\/strong> prem = [0,1,2,3] initially.\nAfter the 1<sup>st<\/sup> operation, prem = [0,2,1,3]\nAfter the 2<sup>nd<\/sup> operation, prem = [0,1,2,3]\nSo it takes only 2 operations.\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 = 6\n<strong>Output:<\/strong> 4\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>2 &lt;= n &lt;= 1000<\/code><\/li><li><code>n<\/code>\u200b\u200b\u200b\u200b\u200b\u200b is even.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Brute Force \/ Simulation<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n<sup>2<\/sup>) ?<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++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  int reinitializePermutation(int n) {\n    vector<int> perm(n);\n    vector<int> arr(n);\n    for (int i = 0; i < n; ++i) perm[i] = i;\n    int ans = 0;\n    bool flag = true;\n    while (flag &#038;&#038; ++ans) {\n      flag = false;\n      for (int i = 0; i < n; ++i) {\n        arr[i] = i &#038; 1 ? perm[n \/ 2 + (i - 1) \/ 2] : perm[i \/ 2];\n        flag |= arr[i] != i;        \n      }\n      swap(perm, arr);\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given an&nbsp;even&nbsp;integer&nbsp;n\u200b\u200b\u200b\u200b\u200b\u200b. You initially have a permutation&nbsp;perm&nbsp;of size&nbsp;n\u200b\u200b where&nbsp;perm[i] == i\u200b&nbsp;(0-indexed)\u200b\u200b\u200b\u200b. In one operation, you will create a new array&nbsp;arr, and for each&nbsp;i:&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[177,121,179],"class_list":["post-8284","post","type-post","status-publish","format-standard","hentry","category-simulation","tag-medium","tag-permutation","tag-simulation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8284","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=8284"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8284\/revisions"}],"predecessor-version":[{"id":8286,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8284\/revisions\/8286"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8284"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8284"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8284"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}