1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-11 02:58:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode/originData/make-lexicographically-smallest-array-by-swapping-elements.json

183 lines
19 KiB
JSON
Raw Normal View History

2023-12-09 01:16:38 +08:00
{
"data": {
"question": {
"questionId": "3219",
"questionFrontendId": "2948",
"boundTopicId": null,
"title": "Make Lexicographically Smallest Array by Swapping Elements",
"titleSlug": "make-lexicographically-smallest-array-by-swapping-elements",
"content": "<p>You are given a <strong>0-indexed</strong> array of <strong>positive</strong> integers <code>nums</code> and a <strong>positive</strong> integer <code>limit</code>.</p>\n\n<p>In one operation, you can choose any two indices <code>i</code> and <code>j</code> and swap <code>nums[i]</code> and <code>nums[j]</code> <strong>if</strong> <code>|nums[i] - nums[j]| &lt;= limit</code>.</p>\n\n<p>Return <em>the <strong>lexicographically smallest array</strong> that can be obtained by performing the operation any number of times</em>.</p>\n\n<p>An array <code>a</code> is lexicographically smaller than an array <code>b</code> if in the first position where <code>a</code> and <code>b</code> differ, array <code>a</code> has an element that is less than the corresponding element in <code>b</code>. For example, the array <code>[2,10,3]</code> is lexicographically smaller than the array <code>[10,2,3]</code> because they differ at index <code>0</code> and <code>2 &lt; 10</code>.</p>\n\n<p>&nbsp;</p>\n<p><strong class=\"example\">Example 1:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [1,5,3,9,8], limit = 2\n<strong>Output:</strong> [1,3,5,8,9]\n<strong>Explanation:</strong> Apply the operation 2 times:\n- Swap nums[1] with nums[2]. The array becomes [1,3,5,9,8]\n- Swap nums[3] with nums[4]. The array becomes [1,3,5,8,9]\nWe cannot obtain a lexicographically smaller array by applying any more operations.\nNote that it may be possible to get the same result by doing different operations.\n</pre>\n\n<p><strong class=\"example\">Example 2:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [1,7,6,18,2,1], limit = 3\n<strong>Output:</strong> [1,6,7,18,1,2]\n<strong>Explanation:</strong> Apply the operation 3 times:\n- Swap nums[1] with nums[2]. The array becomes [1,6,7,18,2,1]\n- Swap nums[0] with nums[4]. The array becomes [2,6,7,18,1,1]\n- Swap nums[0] with nums[5]. The array becomes [1,6,7,18,1,2]\nWe cannot obtain a lexicographically smaller array by applying any more operations.\n</pre>\n\n<p><strong class=\"example\">Example 3:</strong></p>\n\n<pre>\n<strong>Input:</strong> nums = [1,7,28,19,10], limit = 3\n<strong>Output:</strong> [1,7,28,19,10]\n<strong>Explanation:</strong> [1,7,28,19,10] is the lexicographically smallest array we can obtain because we cannot apply the operation on any two indices.\n</pre>\n\n<p>&nbsp;</p>\n<p><strong>Constraints:</strong></p>\n\n<ul>\n\t<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>\n\t<li><code>1 &lt;= nums[i] &lt;= 10<sup>9</sup></code></li>\n\t<li><code>1 &lt;= limit &lt;= 10<sup>9</sup></code></li>\n</ul>\n",
"translatedTitle": null,
"translatedContent": null,
"isPaidOnly": false,
"difficulty": "Medium",
"likes": 176,
"dislikes": 9,
"isLiked": null,
"similarQuestions": "[{\"title\": \"Smallest String With Swaps\", \"titleSlug\": \"smallest-string-with-swaps\", \"difficulty\": \"Medium\", \"translatedTitle\": null}, {\"title\": \"Minimize Hamming Distance After Swap Operations\", \"titleSlug\": \"minimize-hamming-distance-after-swap-operations\", \"difficulty\": \"Medium\", \"translatedTitle\": null}]",
"exampleTestcases": "[1,5,3,9,8]\n2\n[1,7,6,18,2,1]\n3\n[1,7,28,19,10]\n3",
"categoryTitle": "Algorithms",
"contributors": [],
"topicTags": [
{
"name": "Array",
"slug": "array",
"translatedName": null,
"__typename": "TopicTagNode"
},
{
"name": "Union Find",
"slug": "union-find",
"translatedName": null,
"__typename": "TopicTagNode"
},
{
"name": "Sorting",
"slug": "sorting",
"translatedName": null,
"__typename": "TopicTagNode"
}
],
"companyTagStats": null,
"codeSnippets": [
{
"lang": "C++",
"langSlug": "cpp",
"code": "class Solution {\npublic:\n vector<int> lexicographicallySmallestArray(vector<int>& nums, int limit) {\n \n }\n};",
"__typename": "CodeSnippetNode"
},
{
"lang": "Java",
"langSlug": "java",
"code": "class Solution {\n public int[] lexicographicallySmallestArray(int[] nums, int limit) {\n \n }\n}",
"__typename": "CodeSnippetNode"
},
{
"lang": "Python",
"langSlug": "python",
"code": "class Solution(object):\n def lexicographicallySmallestArray(self, nums, limit):\n \"\"\"\n :type nums: List[int]\n :type limit: int\n :rtype: List[int]\n \"\"\"\n ",
"__typename": "CodeSnippetNode"
},
{
"lang": "Python3",
"langSlug": "python3",
"code": "class Solution:\n def lexicographicallySmallestArray(self, nums: List[int], limit: int) -> List[int]:\n ",
"__typename": "CodeSnippetNode"
},
{
"lang": "C",
"langSlug": "c",
"code": "/**\n * Note: The returned array must be malloced, assume caller calls free().\n */\nint* lexicographicallySmallestArray(int* nums, int numsSize, int limit, int* returnSize) {\n \n}",
"__typename": "CodeSnippetNode"
},
{
"lang": "C#",
"langSlug": "csharp",
"code": "public class Solution {\n public int[] LexicographicallySmallestArray(int[] nums, int limit) {\n \n }\n}",
"__typename": "CodeSnippetNode"
},
{
"lang": "JavaScript",
"langSlug": "javascript",
"code": "/**\n * @param {number[]} nums\n * @param {number} limit\n * @return {number[]}\n */\nvar lexicographicallySmallestArray = function(nums, limit) {\n \n};",
"__typename": "CodeSnippetNode"
},
{
"lang": "TypeScript",
"langSlug": "typescript",
"code": "function lexicographicallySmallestArray(nums: number[], limit: number): number[] {\n \n};",
"__typename": "CodeSnippetNode"
},
{
"lang": "PHP",
"langSlug": "php",
"code": "class Solution {\n\n /**\n * @param Integer[] $nums\n * @param Integer $limit\n * @return Integer[]\n */\n function lexicographicallySmallestArray($nums, $limit) {\n \n }\n}",
"__typename": "CodeSnippetNode"
},
{
"lang": "Swift",
"langSlug": "swift",
"code": "class Solution {\n func lexicographicallySmallestArray(_ nums: [Int], _ limit: Int) -> [Int] {\n \n }\n}",
"__typename": "CodeSnippetNode"
},
{
"lang": "Kotlin",
"langSlug": "kotlin",
"code": "class Solution {\n fun lexicographicallySmallestArray(nums: IntArray, limit: Int): IntArray {\n \n }\n}",
"__typename": "CodeSnippetNode"
},
{
"lang": "Dart",
"langSlug": "dart",
"code": "class Solution {\n List<int> lexicographicallySmallestArray(List<int> nums, int limit) {\n \n }\n}",
"__typename": "CodeSnippetNode"
},
{
"lang": "Go",
"langSlug": "golang",
"code": "func lexicographicallySmallestArray(nums []int, limit int) []int {\n \n}",
"__typename": "CodeSnippetNode"
},
{
"lang": "Ruby",
"langSlug": "ruby",
"code": "# @param {Integer[]} nums\n# @param {Integer} limit\n# @return {Integer[]}\ndef lexicographically_smallest_array(nums, limit)\n \nend",
"__typename": "CodeSnippetNode"
},
{
"lang": "Scala",
"langSlug": "scala",
"code": "object Solution {\n def lexicographicallySmallestArray(nums: Array[Int], limit: Int): Array[Int] = {\n \n }\n}",
"__typename": "CodeSnippetNode"
},
{
"lang": "Rust",
"langSlug": "rust",
"code": "impl Solution {\n pub fn lexicographically_smallest_array(nums: Vec<i32>, limit: i32) -> Vec<i32> {\n \n }\n}",
"__typename": "CodeSnippetNode"
},
{
"lang": "Racket",
"langSlug": "racket",
"code": "(define/contract (lexicographically-smallest-array nums limit)\n (-> (listof exact-integer?) exact-integer? (listof exact-integer?))\n )",
"__typename": "CodeSnippetNode"
},
{
"lang": "Erlang",
"langSlug": "erlang",
"code": "-spec lexicographically_smallest_array(Nums :: [integer()], Limit :: integer()) -> [integer()].\nlexicographically_smallest_array(Nums, Limit) ->\n .",
"__typename": "CodeSnippetNode"
},
{
"lang": "Elixir",
"langSlug": "elixir",
"code": "defmodule Solution do\n @spec lexicographically_smallest_array(nums :: [integer], limit :: integer) :: [integer]\n def lexicographically_smallest_array(nums, limit) do\n \n end\nend",
"__typename": "CodeSnippetNode"
}
],
"stats": "{\"totalAccepted\": \"6.2K\", \"totalSubmission\": \"16.9K\", \"totalAcceptedRaw\": 6212, \"totalSubmissionRaw\": 16936, \"acRate\": \"36.7%\"}",
"hints": [
"Construct a virtual graph where all elements in <code>nums</code> are nodes and the pairs satisfying the condition have an edge between them.",
"Instead of constructing all edges, we only care about the connected components.",
"Can we use DSU?",
"Sort <code>nums</code>. Now we just need to consider if the consecutive elements have an edge to check if they belong to the same connected component. Hence, all connected components become a list of position-consecutive elements after sorting.",
"For each index of <code>nums</code> from <code>0</code> to <code>nums.length - 1</code> we can change it to the current minimum value we have in its connected component and remove that value from the connected component."
],
"solution": null,
"status": null,
"sampleTestCase": "[1,5,3,9,8]\n2",
"metaData": "{\n \"name\": \"lexicographicallySmallestArray\",\n \"params\": [\n {\n \"name\": \"nums\",\n \"type\": \"integer[]\"\n },\n {\n \"type\": \"integer\",\n \"name\": \"limit\"\n }\n ],\n \"return\": {\n \"type\": \"integer[]\"\n }\n}",
"judgerAvailable": true,
"judgeType": "large",
"mysqlSchemas": [],
"enableRunCode": true,
"enableTestMode": false,
"enableDebugger": true,
"envInfo": "{\"cpp\": [\"C++\", \"<p>Compiled with <code> clang 11 </code> using the latest C++ 20 standard.</p>\\r\\n\\r\\n<p>Your code is compiled with level two optimization (<code>-O2</code>). <a href=\\\"https://github.com/google/sanitizers/wiki/AddressSanitizer\\\" target=\\\"_blank\\\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\\r\\n\\r\\n<p>Most standard library headers are already included automatically for your convenience.</p>\"], \"java\": [\"Java\", \"<p><code>OpenJDK 17</code>. Java 8 features such as lambda expressions and stream API can be used. </p>\\r\\n\\r\\n<p>Most standard library headers are already included automatically for your convenience.</p>\\r\\n<p>Includes <code>Pair</code> class from https://docs.oracle.com/javase/8/javafx/api/javafx/util/Pair.html.</p>\"], \"python\": [\"Python\", \"<p><code>Python 2.7.12</code>.</p>\\r\\n\\r\\n<p>Most libraries are already imported automatically for your convenience, such as <a href=\\\"https://docs.python.org/2/library/array.html\\\" target=\\\"_blank\\\">array</a>, <a href=\\\"https://docs.python.org/2/library/bisect.html\\\" target=\\\"_blank\\\">bisect</a>, <a href=\\\"https://docs.python.org/2/library/collections.html\\\" target=\\\"_blank\\\">collections</a>. If you need more libraries, you can import it yourself.</p>\\r\\n\\r\\n<p>For Map/TreeMap data structure, you may use <a href=\\\"http://www.grantjenks.com/docs/sortedcontainers/\\\" target=\\\"_blank\\\">sortedcontainers</a> library.</p>\\r\\n\\r\\n<p>Note that Python 2.7 <a href=\\\"https://www.python.org/dev/peps/pep-0373/\\\" target=\\\"_blank\\\">will not be maintained past 2020</a>. For the latest Python, please choose Python3 instead.</p>\"], \"c\": [\"C\", \"<p>Compiled with <code>gcc 8.2</code> using the gnu11 standard.</p>\\r\\n\\r\\n<p>Your code is compiled with level one optimization (<code>-O1</code>). <a href=\\\"https://github.com/google/sanitizers/wiki/AddressSanitizer\\\" target=\\\"_blank\\\">AddressSanitizer</a> is also enabled to help detect out-of-bounds and use-after-free bugs.</p>\\r\\n\\r\\n<p>Most standard library headers are already included automatically for your convenience.</p>\\r\\n\\r\\n<p>For hash table operations, you may use <a href=\\\"https://troydhanson.github.io/uthash/\\\" target=\\\"_blank\\\">uthash</a>. \\\"uthash.h\\\" is included by default. Below are some examples:</p>\\r\\n\\r\\n<p><b>1. Adding an item to a hash.</b>\\r\\n<pre>\\r\\nstruct hash_entry {\\r\\n int id; /* we'll use this field as the key */\\r\\n char name[10];\\r\\n UT_hash_handle hh; /* makes this structure hashable */\\r\\n};\\r\\n\\r\\nstruct hash_entry *users = NULL;\\r\\n\\r\\nvoid add_user(struct hash_entry *s) {\\r\\n HASH_ADD_INT(users, id, s);\\r\\n}\\r\\n</pre>\\r\\n</p>\\r\\n\\r\\n<p><b>2. Looking up an item in a hash:</b>\\r\\n<pre>\\r\\nstruct hash_entry *find_user(int user_id) {\\r\\n struct hash_entry *s;\\r\\n HASH_FIND_INT(users, &user_id, s);\\r\\n return s;\\r\\n}\\r\\n</pre>\\r\\n</p>\\r\\n\\r\\n<p><b>3. Deleting an item in a hash:</b>\\r\\n<pre>\\r\\nvoid delete_user(struct hash_entry *user) {\\r\\n HASH_DEL(users, user); \\r\\n}\\r\\n</pre>\\r\\n</p>\"], \"csharp\": [\"C#\", \"<p><a href=\\\"https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-10\\\" target=\\\"_blank\\\">C# 10 with .NET 6 runtime</a></p>\"], \"javascript\": [\"JavaScript\", \"<p><code>Node.js 16.13.2</code>.</p>\\r\\n\\r\\n<p>Your code is run with <code>--harmony</code> flag, enabling <a href=\\\"http://node.green/\\\" target=\\\"_blank\\\">new ES6 features</a>.</p>\\r\\n\\r\\n<p><a href=\\\"https://lodash.com\\\" target=\\\"_blank\\\">lodash.js</a> library is included by default.</p>\\r\\n\\r\\n<p>For Priority Queue / Queue data structures, you may use 5.3.0 version of <a href=\\\"https://github.com/datastructures-js/priority-queue/tree/fb4fdb984834421279aeb081df7af624d17c2a03\\\" target=\\\"_blank\\\">datastructures-js/priority-queue</a> and 4.2.1 version of <a href=\\\"https://githu
"libraryUrl": null,
"adminUrl": null,
"challengeQuestion": null,
"__typename": "QuestionNode"
}
}
}