{"id":7634,"date":"2020-11-08T11:35:26","date_gmt":"2020-11-08T19:35:26","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7634"},"modified":"2020-11-09T16:24:23","modified_gmt":"2020-11-10T00:24:23","slug":"leetcode-1649-create-sorted-array-through-instructions","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-1649-create-sorted-array-through-instructions\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1649. Create Sorted Array through Instructions"},"content":{"rendered":"\n<p>Given an integer array&nbsp;<code>instructions<\/code>, you are asked to create a sorted array from the elements in&nbsp;<code>instructions<\/code>. You start with an empty container&nbsp;<code>nums<\/code>. For each element from&nbsp;<strong>left to right<\/strong>&nbsp;in&nbsp;<code>instructions<\/code>, insert it into&nbsp;<code>nums<\/code>. The&nbsp;<strong>cost<\/strong>&nbsp;of each insertion is the&nbsp;<strong>minimum<\/strong>&nbsp;of the following:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>The number of elements currently in&nbsp;<code>nums<\/code>&nbsp;that are&nbsp;<strong>strictly less than<\/strong>&nbsp;<code>instructions[i]<\/code>.<\/li><li>The number of elements currently in&nbsp;<code>nums<\/code>&nbsp;that are&nbsp;<strong>strictly greater than<\/strong>&nbsp;<code>instructions[i]<\/code>.<\/li><\/ul>\n\n\n\n<p>For example, if inserting element&nbsp;<code>3<\/code>&nbsp;into&nbsp;<code>nums = [1,2,3,5]<\/code>, the&nbsp;<strong>cost<\/strong>&nbsp;of insertion is&nbsp;<code>min(2, 1)<\/code>&nbsp;(elements&nbsp;<code>1<\/code>&nbsp;and&nbsp;<code>2<\/code>&nbsp;are less than&nbsp;<code>3<\/code>, element&nbsp;<code>5<\/code>&nbsp;is greater than&nbsp;<code>3<\/code>) and&nbsp;<code>nums<\/code>&nbsp;will become&nbsp;<code>[1,2,3,3,5]<\/code>.<\/p>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>total cost<\/strong>&nbsp;to insert all elements from&nbsp;<\/em><code>instructions<\/code><em>&nbsp;into&nbsp;<\/em><code>nums<\/code>. Since the answer may be large, return it&nbsp;<strong>modulo<\/strong>&nbsp;<code>10<sup>9<\/sup>&nbsp;+ 7<\/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> instructions = [1,5,6,2]\n<strong>Output:<\/strong> 1\n<strong>Explanation:<\/strong> Begin with nums = [].\nInsert 1 with cost min(0, 0) = 0, now nums = [1].\nInsert 5 with cost min(1, 0) = 0, now nums = [1,5].\nInsert 6 with cost min(2, 0) = 0, now nums = [1,5,6].\nInsert 2 with cost min(1, 2) = 1, now nums = [1,2,5,6].\nThe total cost is 0 + 0 + 0 + 1 = 1.<\/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> instructions = [1,2,3,6,5,4]\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> Begin with nums = [].\nInsert 1 with cost min(0, 0) = 0, now nums = [1].\nInsert 2 with cost min(1, 0) = 0, now nums = [1,2].\nInsert 3 with cost min(2, 0) = 0, now nums = [1,2,3].\nInsert 6 with cost min(3, 0) = 0, now nums = [1,2,3,6].\nInsert 5 with cost min(3, 1) = 1, now nums = [1,2,3,5,6].\nInsert 4 with cost min(3, 2) = 2, now nums = [1,2,3,4,5,6].\nThe total cost is 0 + 0 + 0 + 0 + 1 + 2 = 3.\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> instructions = [1,3,3,3,2,4,2,1,2]\n<strong>Output:<\/strong> 4\n<strong>Explanation:<\/strong> Begin with nums = [].\nInsert 1 with cost min(0, 0) = 0, now nums = [1].\nInsert 3 with cost min(1, 0) = 0, now nums = [1,3].\nInsert 3 with cost min(1, 0) = 0, now nums = [1,3,3].\nInsert 3 with cost min(1, 0) = 0, now nums = [1,3,3,3].\nInsert 2 with cost min(1, 3) = 1, now nums = [1,2,3,3,3].\nInsert 4 with cost min(5, 0) = 0, now nums = [1,2,3,3,3,4].\n\u200b\u200b\u200b\u200b\u200b\u200b\u200bInsert 2 with cost min(1, 4) = 1, now nums = [1,2,2,3,3,3,4].\n\u200b\u200b\u200b\u200b\u200b\u200b\u200bInsert 1 with cost min(0, 6) = 0, now nums = [1,1,2,2,3,3,3,4].\n\u200b\u200b\u200b\u200b\u200b\u200b\u200bInsert 2 with cost min(2, 4) = 2, now nums = [1,1,2,2,2,3,3,3,4].\nThe total cost is 0 + 0 + 0 + 0 + 1 + 0 + 1 + 0 + 2 = 4.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= instructions.length &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= instructions[i] &lt;= 10<sup>5<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Fenwick Tree \/ Binary Indexed Tree<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(nlogm)<br>Space complexity: O(m + n)<\/p>\n\n\n\n<p>m is the maximum value, n is number of values.<\/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 FenwickTree {    \npublic:\n  FenwickTree(int n): sums_(n + 1, 0) {}\n\n  void update(int i, int delta) {\n    while (i < sums_.size()) {\n        sums_[i] += delta;\n        i += lowbit(i);\n    }\n  }\n\n  int query(int i) const {        \n    int sum = 0;\n    while (i > 0) {\n        sum += sums_[i];\n        i -= lowbit(i);\n    }\n    return sum;\n  }\nprivate:\n  static inline int lowbit(int x) { \n    return x & (-x); \n  }\n  vector<int> sums_;\n};\n\nclass Solution {\npublic:\n  int createSortedArray(vector<int>& instructions) {\n    const int n = instructions.size();\n    FenwickTree tree(1e5);\n    long ans = 0;\n    for (int i = 0; i < n; ++i) {\n      int lt = tree.query(instructions[i] - 1);\n      int gt = i - tree.query(instructions[i]);      \n      ans += min(lt, gt);\n      tree.update(instructions[i], 1);\n    }\n    return ans % 1000000007;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given an integer array&nbsp;instructions, you are asked to create a sorted array from the elements in&nbsp;instructions. You start with an empty container&nbsp;nums. For each element&#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":[16,204,217,200],"class_list":["post-7634","post","type-post","status-publish","format-standard","hentry","category-array","tag-bit","tag-fenwick-tree","tag-hard","tag-prefix-sum","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7634","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=7634"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7634\/revisions"}],"predecessor-version":[{"id":7636,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7634\/revisions\/7636"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7634"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7634"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7634"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}