原文链接: https://leetcode-cn.com/problems/minimum-number-of-moves-to-seat-everyone
英文原文
There are n
seats and n
students 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
ith
student by1
(i.e., moving theith
student from positionx
tox + 1
orx - 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 from position 2 to position 1 using 1 move. - The second student is moved from from position 7 to position 5 using 2 moves. - The third student is moved from 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 from position 3 to position 4 using 1 move. - The third student is moved from from position 2 to position 5 using 3 moves. - The fourth student is moved from 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 from position 1 to position 2 using 1 move. - The second student is moved from 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.length
1 <= n <= 100
1 <= 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.length
1 <= n <= 100
1 <= seats[i], students[j] <= 100
通过代码
高赞题解
由于座位和学生数相同,一个萝卜一个坑,将座位和学生位置排序后,第 $i$ 个学生可以对应第 $i$ 个座位。
由于交换任意两个学生对应的座位不会产生更少的移动次数(可以画一画,证明略),所以上述对应关系可以产生最少移动次数,累加位置之差即为答案。
func minMovesToSeat(seats, students []int) (ans int) {
sort.Ints(seats)
sort.Ints(students)
for i, p := range seats {
ans += abs(p - students[i])
}
return
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}
统计信息
通过次数 | 提交次数 | AC比率 |
---|---|---|
3611 | 4354 | 82.9% |
提交历史
提交时间 | 提交结果 | 执行时间 | 内存消耗 | 语言 |
---|