Skip to main content

2037. Minimum Number of Moves to Seat Everyone

class Solution {
public:
int minMovesToSeat(vector<int>& seats, vector<int>& students)
{
sort(seats.begin(), seats.end());
sort(students.begin(), students.end());

int n = seats.size();
int moves = 0;
for (int i = 0; i < n; ++i)
{
moves += abs(seats[i] - students[i]);
}
return moves;
}
};
  • T: O(nlogn)O(n \cdot \log n)
  • S: O(1)O(1)