2037. Minimum Number of Moves to Seat Everyone Easy
1/**
2 * [2037] Minimum Number of Moves to Seat Everyone
3 *
4 * 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 i^th seat. You are also given the array students of length n, where students[j] is the position of the j^th student.
5 * You may perform the following move any number of times:
6 *
7 * Increase or decrease the position of the i^th student by 1 (i.e., moving the i^th student from position x to x + 1 or x - 1)
8 *
9 * Return the minimum number of moves required to move each student to a seat such that no two students are in the same seat.
10 * Note that there may be multiple seats or students in the same position at the beginning.
11 *
12 * Example 1:
13 *
14 * Input: seats = [3,1,5], students = [2,7,4]
15 * Output: 4
16 * Explanation: The students are moved as follows:
17 * - The first student is moved from from position 2 to position 1 using 1 move.
18 * - The second student is moved from from position 7 to position 5 using 2 moves.
19 * - The third student is moved from from position 4 to position 3 using 1 move.
20 * In total, 1 + 2 + 1 = 4 moves were used.
21 *
22 * Example 2:
23 *
24 * Input: seats = [4,1,5,9], students = [1,3,2,6]
25 * Output: 7
26 * Explanation: The students are moved as follows:
27 * - The first student is not moved.
28 * - The second student is moved from from position 3 to position 4 using 1 move.
29 * - The third student is moved from from position 2 to position 5 using 3 moves.
30 * - The fourth student is moved from from position 6 to position 9 using 3 moves.
31 * In total, 0 + 1 + 3 + 3 = 7 moves were used.
32 *
33 * Example 3:
34 *
35 * Input: seats = [2,2,6,6], students = [1,3,2,6]
36 * Output: 4
37 * Explanation: Note that there are two seats at position 2 and two seats at position 6.
38 * The students are moved as follows:
39 * - The first student is moved from from position 1 to position 2 using 1 move.
40 * - The second student is moved from from position 3 to position 6 using 3 moves.
41 * - The third student is not moved.
42 * - The fourth student is not moved.
43 * In total, 1 + 3 + 0 + 0 = 4 moves were used.
44 *
45 *
46 * Constraints:
47 *
48 * n == seats.length == students.length
49 * 1 <= n <= 100
50 * 1 <= seats[i], students[j] <= 100
51 *
52 */
53pub struct Solution {}
54
55// problem: https://leetcode.com/problems/minimum-number-of-moves-to-seat-everyone/
56// discuss: https://leetcode.com/problems/minimum-number-of-moves-to-seat-everyone/discuss/?currentPage=1&orderBy=most_votes&query=
57
58// submission codes start here
59
60impl Solution {
61 pub fn min_moves_to_seat(seats: Vec<i32>, students: Vec<i32>) -> i32 {
62 0
63 }
64}
65
66// submission codes end
67
68#[cfg(test)]
69mod tests {
70 use super::*;
71
72 #[test]
73 fn test_2037() {
74 }
75}
76
Back
© 2025 bowen.ge All Rights Reserved.