PYTHON || 100 BEAT %🔥🔥|| 100% EFFICIENT🔥|| OPTIMAL SOLUTION || EASY TO UNDERSTAND ||
if it\'s help, please up \u2B06vote!\u2764\uFE0F and comment\uD83D\uDD25\n# Intuition\n Describe your first thoughts on how to solve this problem. \n- The probl
# if it\'s help, please up \u2B06vote!\u2764\uFE0F and comment\uD83D\uDD25\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n- The problem can be solved by finding an optimal assignment of students to seats that minimizes the total distance (or number of moves) required to move each student to their assigned seat. Since the initial positions of seats and students are given, we can sort both arrays and then match the closest seat and student positions to minimize the total distance.\n# Approach\n<!-- Describe your approach to solving the problem. -->\n- Sort the seats and students arrays in ascending order.\n- Iterate over the sorted arrays simultaneously, and for each pair of corresponding seat and student positions, calculate the absolute difference between them.\n- Add up all the absolute differences to get the total number of moves required.\n- Return the total number of moves.\n\n# Complexity\n- Time complexity: $$O(n \\log n)$$\n  - Sorting the seats and students arrays takes O(n log n) time, where n is the length of the arrays.\n  - Iterating over the sorted arrays and calculating the absolute differences takes O(n) time.\n  - Therefore, the overall time complexity is dominated by the sorting step, which is O(n log n).\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(1)$$\n  - The algorithm uses a constant amount of extra space to store the result variable and the loop counter.\n  - The sorting operation is performed in-place, so no additional space is required for the sorted arrays.\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution(object):\n    def minMovesToSeat(self, seats, students):\n\n        seats.sort()\n        students.sort()\n        result = 0\n        \n        # Calculate the total moves\n        for i in range(len(seats)):\n            result += abs(seats[i] - students[i])\n        \n        return result\n```\n![7c17f983-3b8b-4738-b722-c1c1466f9510_1682940288.2823734.jpeg](https://assets.leetcode.com/users/images/94b2f783-92c8-4826-814b-5eb32b034fbe_1718299360.9153757.jpeg)\n
['Array', 'Greedy', 'Sorting', 'Python', 'Python3']