{"id":10097,"date":"2024-01-08T03:46:10","date_gmt":"2024-01-08T11:46:10","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=10097"},"modified":"2024-01-08T03:46:41","modified_gmt":"2024-01-08T11:46:41","slug":"leetcode-2974-minimum-number-game","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/uncategorized\/leetcode-2974-minimum-number-game\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2974. Minimum Number Game"},"content":{"rendered":"\n<p>You are given a&nbsp;<strong>0-indexed<\/strong>&nbsp;integer array&nbsp;<code>nums<\/code>&nbsp;of&nbsp;<strong>even<\/strong>&nbsp;length and there is also an empty array&nbsp;<code>arr<\/code>. Alice and Bob decided to play a game where in every round Alice and Bob will do one move. The rules of the game are as follows:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Every round, first Alice will remove the&nbsp;<strong>minimum<\/strong>&nbsp;element from&nbsp;<code>nums<\/code>, and then Bob does the same.<\/li><li>Now, first Bob will append the removed element in the array&nbsp;<code>arr<\/code>, and then Alice does the same.<\/li><li>The game continues until&nbsp;<code>nums<\/code>&nbsp;becomes empty.<\/li><\/ul>\n\n\n\n<p>Return&nbsp;<em>the resulting array&nbsp;<\/em><code>arr<\/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> nums = [5,4,2,3]\n<strong>Output:<\/strong> [3,2,5,4]\n<strong>Explanation:<\/strong> In round one, first Alice removes 2 and then Bob removes 3. Then in arr firstly Bob appends 3 and then Alice appends 2. So arr = [3,2].\nAt the begining of round two, nums = [5,4]. Now, first Alice removes 4 and then Bob removes 5. Then both append in arr which becomes [3,2,5,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,5]\n<strong>Output:<\/strong> [5,2]\n<strong>Explanation:<\/strong> In round one, first Alice removes 2 and then Bob removes 5. Then in arr firstly Bob appends and then Alice appends. So arr = [5,2].\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;= 100<\/code><\/li><li><code>1 &lt;= nums[i] &lt;= 100<\/code><\/li><li><code>nums.length % 2 == 0<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Sort and Swap<\/strong><\/h2>\n\n\n\n<p>Sort the array first and then swap the adjacent elements.<\/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++\">\nclass Solution {\npublic:\n  vector<int> numberGame(vector<int>& nums) {\n    sort(begin(nums), end(nums));\n    for (int i = 0; i < nums.size(); i += 2)\n      swap(nums[i], nums[i + 1]);\n    return nums;\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;of&nbsp;even&nbsp;length and there is also an empty array&nbsp;arr. Alice and Bob decided to play a game where in every round Alice&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-10097","post","type-post","status-publish","format-standard","hentry","category-uncategorized","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10097","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=10097"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10097\/revisions"}],"predecessor-version":[{"id":10099,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/10097\/revisions\/10099"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=10097"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=10097"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=10097"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}