import java.util.Arrays;
// 2037. Minimum Number of Moves to Seat Everyone
class Solution {
public int minMovesToSeat(int[] seats, int[] students) {
Arrays.sort(seats);
Arrays.sort(students);
int n = seats.length;
int ans = 0;
for (int i = 0; i < n; ++i) {
ans += Math.abs(seats[i] - students[i]);
}
return ans;
}
}
学习笔记: 这是一道简单的排序、贪心的题目。 排一下从小到大,然后减一下差加总就好了。