2037. 使每位学生都有座位的最少移动次数
2037. 使每位学生都有座位的最少移动次数
🟢 🔖 贪心 数组 计数排序 排序 🔗 力扣 LeetCode
题目
There are n availabe seats and n students standing in a room. You are given an array seats of length n, where seats[i] is the position of the ith seat. You are also given the array students of length n, where students[j] is the position of the jth student.
You may perform the following move any number of times:
- Increase or decrease the position of the
ithstudent by1(i.e., moving theithstudent from positionxtox + 1orx - 1)
Return the minimum number of moves required to move each student to a seat such that no two students are in the same seat.
Note that there may be multiple seats or students in the same position at the beginning.
Example 1:
Input: seats = [3,1,5], students = [2,7,4]
Output: 4
Explanation: The students are moved as follows:
- The first student is moved from position 2 to position 1 using 1 move.
- The second student is moved from position 7 to position 5 using 2 moves.
- The third student is moved from position 4 to position 3 using 1 move.
In total, 1 + 2 + 1 = 4 moves were used.
Example 2:
Input: seats = [4,1,5,9], students = [1,3,2,6]
Output: 7
Explanation: The students are moved as follows:
- The first student is not moved.
- The second student is moved from position 3 to position 4 using 1 move.
- The third student is moved from position 2 to position 5 using 3 moves.
- The fourth student is moved from position 6 to position 9 using 3 moves.
In total, 0 + 1 + 3 + 3 = 7 moves were used.
Example 3:
Input: seats = [2,2,6,6], students = [1,3,2,6]
Output: 4
Explanation: Note that there are two seats at position 2 and two seats at position 6.
The students are moved as follows:
- The first student is moved from position 1 to position 2 using 1 move.
- The second student is moved from position 3 to position 6 using 3 moves.
- The third student is not moved.
- The fourth student is not moved.
In total, 1 + 3 + 0 + 0 = 4 moves were used.
Constraints:
n == seats.length == students.length1 <= n <= 1001 <= seats[i], students[j] <= 100
题目大意
一个房间里有 n 个 空闲 座位和 n 名 站着的 学生,房间用一个数轴表示。给你一个长度为 n 的数组 seats ,其中 seats[i] 是第 i 个座位的位置。同时给你一个长度为 n 的数组 students ,其中 students[j] 是第 j 位学生的位置。
你可以执行以下操作任意次:
- 增加或者减少第
i位学生的位置,每次变化量为1(也就是将第i位学生从位置x移动到x + 1或者x - 1)
请你返回使所有学生都有座位坐的 最少移动次数 ,并确保没有两位学生的座位相同。
请注意,初始时有可能有多个座位或者多位学生在 同一 位置。
示例 1:
输入: seats = [3,1,5], students = [2,7,4]
输出: 4
解释: 学生移动方式如下:
- 第一位学生从位置 2 移动到位置 1 ,移动 1 次。
- 第二位学生从位置 7 移动到位置 5 ,移动 2 次。
- 第三位学生从位置 4 移动到位置 3 ,移动 1 次。
总共 1 + 2 + 1 = 4 次移动。
示例 2:
输入: seats = [4,1,5,9], students = [1,3,2,6]
输出: 7
解释: 学生移动方式如下:
- 第一位学生不移动。
- 第二位学生从位置 3 移动到位置 4 ,移动 1 次。
- 第三位学生从位置 2 移动到位置 5 ,移动 3 次。
- 第四位学生从位置 6 移动到位置 9 ,移动 3 次。
总共 0 + 1 + 3 + 3 = 7 次移动。
示例 3:
输入: seats = [2,2,6,6], students = [1,3,2,6]
输出: 4
解释: 学生移动方式如下:
- 第一位学生从位置 1 移动到位置 2 ,移动 1 次。
- 第二位学生从位置 3 移动到位置 6 ,移动 3 次。
- 第三位学生不移动。
- 第四位学生不移动。
总共 1 + 3 + 0 + 0 = 4 次移动。
提示:
n == seats.length == students.length1 <= n <= 1001 <= seats[i], students[j] <= 100
解题思路
思路一:差值数组法
- 用一个数组
diff表示某个位置需要的学生和实际拥有的学生的差值。- 对座位位置 +1 表示有一个空位。
- 对学生位置 -1 表示需要一个空位。
- 遍历
diff,根据累积的unmatched(未匹配的学生或座位数)计算步数。- 累加的绝对值
moves表示把多余的空位或学生分配到下一个位置的最小移动步数。
- 累加的绝对值
复杂度分析
- 时间复杂度:
O(n + m),其中n, m是seats和students中的最大值。 - 空间复杂度:
O(maxPosition),使用了一个长度为maxPosition的差值数组。
思路二:排序匹配法
- 排序后的
seats和students可以直接进行一一匹配,匹配结果即为最小总移动步数。 - 遍历排序后的数组,直接累加对应位置的绝对差值。
复杂度分析
- 时间复杂度:
O(n log n),主要来源于排序。 - 空间复杂度:
O(1),仅需常量额外空间。
代码
/**
* @param {number[]} seats
* @param {number[]} students
* @return {number}
*/
var minMovesToSeat = function (seats, students) {
let maxPosition = Math.max(...seats, ...students);
let diff = new Array(maxPosition).fill(0);
for (let position of seats) {
diff[position - 1] += 1; // 座位 +1
}
for (let position of students) {
diff[position - 1] -= 1; // 学生 -1
}
let moves = 0;
let unmatched = 0; // 累计未匹配的座位或学生
for (let num of diff) {
moves += Math.abs(unmatched); // 累积移动步数
unmatched += num;
}
return moves;
};
/**
* @param {number[]} seats
* @param {number[]} students
* @return {number}
*/
var minMovesToSeat = function (seats, students) {
seats.sort((a, b) => a - b); // 座位排序
students.sort((a, b) => a - b); // 学生排序
let moves = 0;
for (let i = 0; i < seats.length; i++) {
moves += Math.abs(students[i] - seats[i]); // 累加每对的移动距离
}
return moves;
};
