Super Easy | Beats 98% | Java✔️ | Python ✔️|
Intuition\n Describe your first thoughts on how to solve this problem. \nSorting both arrays allows for effiecient pairing of students to seats minimizing total
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nSorting both arrays allows for effiecient pairing of students to seats minimizing total movement required.\n\n# Approach : Greedy\nArrange both arrays seats and students in ascending order.\nIterate through indices from 0 to the length of either array.\nCalculate the absolute difference between seats[i] and students[i] at each index. This ensures the minimal number of moves needed to align all students with their corresponding seats.\nSorting both arrays simplifies the problem significantly.\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\nTime complexity: O(nlogn)\n\nSpace complexity: O(1)\n# Code\n```java []\nconsole.log(\'Hello world!\')\nclass Solution {\n    public int minMovesToSeat(int[] seats, int[] students) {\n        Arrays.sort(seats);\n        Arrays.sort(students);\n        int moves = 0;\n\n        for(int i = 0; i < seats.length; ++i) {\n            moves += Math.abs(seats[i] - students[i]);\n        }\n\n        return moves;\n    }\n}\n```\n```python []\nprint(\'Hello world!\')\nclass Solution:\n    def minMovesToSeat(self, seats: List[int], students: List[int]) -> int:\n        res=0\n        seats.sort()\n        students.sort()\n        for i in range(len(seats)):\n            res+=abs(seats[i]-students[i])\n        return res\n```\n\n\n
['Sorting', 'Python3']