💯easy peasy🎯✅ | 99.56%🔥| 4 Easy Steps : 🎯| Simple (5 line) to understand💯Explained
Intuition\nThe problem requires minimizing the number of moves to seat all students such that no two students sit in the same seat. The best approach to minimiz
# Intuition\nThe problem requires minimizing the number of moves to seat all students such that no two students sit in the same seat. The best approach to minimize the distance each student has to move is to sort both arrays and then pair each student with the corresponding seat.\n\n# Approach\n1. Sort the `seats` array and the `students` array.\n2. Initialize a variable `moves` to keep track of the total moves needed.\n3. Iterate through the sorted arrays and calculate the absolute difference between the seat and the student for each position. Add this difference to `moves`.\n4. Return the total `moves`.\n\n# Complexity\n- Time complexity:\nThe time complexity is \\(O(n \\log n)\\) due to the sorting of the arrays.\n\n- Space complexity:\nThe space complexity is \\(O(1)\\) as we are using a constant amount of extra space.\n\n\n```javascript []\n/**\n * @param {number[]} seats\n * @param {number[]} students\n * @return {number}\n */\n\nvar minMovesToSeat = function(seats, students) {\n    seats.sort((a, b) => a - b);\n    students.sort((a, b) => a - b);\n    let moves = 0;\n    for (let i = 0; i < seats.length; i++) {\n        moves += Math.abs(seats[i] - students[i]);\n    }\n    return moves;\n};\n```\n\n```python []\ndef minMovesToSeat(seats, students):\n    seats.sort()\n    students.sort()\n    moves = 0\n    for i in range(len(seats)):\n        moves += abs(seats[i] - students[i])\n    return moves\n```\n\n```java []\nimport java.util.Arrays;\n\nclass Solution {\n    public int minMovesToSeat(int[] seats, int[] students) {\n        Arrays.sort(seats);\n        Arrays.sort(students);\n        int moves = 0;\n        for (int i = 0; i < seats.length; i++) {\n            moves += Math.abs(seats[i] - students[i]);\n        }\n        return moves;\n    }\n}\n```\n\n```cpp []\n#include <vector>\n#include <algorithm>\n#include <cmath>\n\nclass Solution {\npublic:\n    int minMovesToSeat(std::vector<int>& seats, std::vector<int>& students) {\n        std::sort(seats.begin(), seats.end());\n        std::sort(students.begin(), students.end());\n        int moves = 0;\n        for (int i = 0; i < seats.size(); i++) {\n            moves += std::abs(seats[i] - students[i]);\n        }\n        return moves;\n    }\n};\n```\n\n```kotlin []\nclass Solution {\n    fun minMovesToSeat(seats: IntArray, students: IntArray): Int {\n        seats.sort()\n        students.sort()\n        var moves = 0\n        for (i in seats.indices) {\n            moves += kotlin.math.abs(seats[i] - students[i])\n        }\n        return moves\n    }\n}\n```\n\n```ruby []\ndef minMovesToSeat(seats, students)\n  seats.sort!\n  students.sort!\n  moves = 0\n  seats.each_with_index do |seat, i|\n    moves += (seat - students[i]).abs\n  end\n  moves\nend\n```\n\n
['Array', 'Greedy', 'Sorting', 'Python', 'C++', 'Java', 'Python3', 'Ruby', 'Kotlin', 'JavaScript']