{"id":8617,"date":"2021-10-18T17:15:11","date_gmt":"2021-10-19T00:15:11","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8617"},"modified":"2021-10-18T17:49:36","modified_gmt":"2021-10-19T00:49:36","slug":"leetcode-2037-minimum-number-of-moves-to-seat-everyone","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-2037-minimum-number-of-moves-to-seat-everyone\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2037. Minimum Number of Moves to Seat Everyone"},"content":{"rendered":"\n<p>There are&nbsp;<code>n<\/code>&nbsp;seats and&nbsp;<code>n<\/code>&nbsp;students in a room. You are given an array&nbsp;<code>seats<\/code>&nbsp;of length&nbsp;<code>n<\/code>, where&nbsp;<code>seats[i]<\/code>&nbsp;is the position of the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;seat. You are also given the array&nbsp;<code>students<\/code>&nbsp;of length&nbsp;<code>n<\/code>, where&nbsp;<code>students[j]<\/code>&nbsp;is the position of the&nbsp;<code>j<sup>th<\/sup><\/code>&nbsp;student.<\/p>\n\n\n\n<p>You may perform the following move any number of times:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Increase or decrease the position of the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;student by&nbsp;<code>1<\/code>&nbsp;(i.e., moving the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;student from position&nbsp;<code>x<\/code>&nbsp;to&nbsp;<code>x + 1<\/code>&nbsp;or&nbsp;<code>x - 1<\/code>)<\/li><\/ul>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>minimum number of moves<\/strong>&nbsp;required to move each student to a seat<\/em><em>&nbsp;such that no two students are in the same seat.<\/em><\/p>\n\n\n\n<p>Note that there may be multiple seats or students in the&nbsp;<strong>same&nbsp;<\/strong>position at the beginning.<\/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> seats = [3,1,5], students = [2,7,4]\n<strong>Output:<\/strong> 4\n<strong>Explanation:<\/strong>&nbsp;The students are moved as follows:\n- The first student is moved from from position 2 to position 1 using 1 move.\n- The second student is moved from from position 7 to position 5 using 2 moves.\n- The third student is moved from from position 4 to position 3 using 1 move.\nIn total, 1 + 2 + 1 = 4 moves were used.\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> seats = [4,1,5,9], students = [1,3,2,6]\n<strong>Output:<\/strong> 7\n<strong>Explanation:<\/strong>&nbsp;The students are moved as follows:\n- The first student is not moved.\n- The second student is moved from from position 3 to position 4 using 1 move.\n- The third student is moved from from position 2 to position 5 using 3 moves.\n- The fourth student is moved from from position 6 to position 9 using 3 moves.\nIn total, 0 + 1 + 3 + 3 = 7 moves were used.\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> seats = [2,2,6,6], students = [1,3,2,6]\n<strong>Output:<\/strong> 4\n<strong>Explanation:<\/strong>&nbsp;The students are moved as follows:\n- The first student is moved from from position 1 to position 2 using 1 move.\n- The second student is moved from from position 3 to position 6 using 3 moves.\n- The third student is not moved.\n- The fourth student is not moved.\nIn total, 1 + 3 + 0 + 0 = 4 moves were used.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>n == seats.length == students.length<\/code><\/li><li><code>1 &lt;= n &lt;= 100<\/code><\/li><li><code>1 &lt;= seats[i], students[j] &lt;= 100<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Greedy<\/strong><\/h2>\n\n\n\n<p>Sort both arrays, move students[i] to seats[i].<\/p>\n\n\n\n<p>Time complexity: O(nlogn)<br>Space complexity: O(1)<\/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 minMovesToSeat(vector<int>& seats, vector<int>& students) {\n    sort(begin(seats), end(seats));\n    sort(begin(students), end(students));\n    int ans = 0;\n    for (size_t i = 0; i < seats.size(); ++i)\n      ans += abs(seats[i] - students[i]);\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>There are&nbsp;n&nbsp;seats and&nbsp;n&nbsp;students in a room. You are given an array&nbsp;seats&nbsp;of length&nbsp;n, where&nbsp;seats[i]&nbsp;is the position of the&nbsp;ith&nbsp;seat. You are also given the array&nbsp;students&nbsp;of length&nbsp;n, where&nbsp;students[j]&nbsp;is&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[222,88],"class_list":["post-8617","post","type-post","status-publish","format-standard","hentry","category-greedy","tag-easy","tag-greedy","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8617","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=8617"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8617\/revisions"}],"predecessor-version":[{"id":8619,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8617\/revisions\/8619"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8617"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8617"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8617"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}