|
| 1 | +--- |
| 2 | +id: minimum-number-of-moves-to-seat-everyone |
| 3 | +title: Minimum Number of Moves to Seat Everyone |
| 4 | +sidebar_label: 2037-Minimum Number of Moves to Seat Everyone |
| 5 | +tags: |
| 6 | + - Brute Force |
| 7 | + - Greedy |
| 8 | + - LeetCode |
| 9 | + - Java |
| 10 | + - Python |
| 11 | + - C++ |
| 12 | +description: "This is a solution to theMinimum Number of Moves to Seat Everyone problem on LeetCode." |
| 13 | +sidebar_position: 2 |
| 14 | +--- |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | +## Problem Description |
| 19 | + |
| 20 | +Given `n` available 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 `i`-th seat. You are also given the array `students` of length `n`, where `students[j]` is the position of the `j`-th student. You can increase or decrease the position of the `i`-th student by 1 any number of times. Return the minimum number of moves required to move each student to a seat such that no two students are in the same seat. |
| 21 | + |
| 22 | +### Examples |
| 23 | + |
| 24 | +**Example 1:** |
| 25 | + |
| 26 | +``` |
| 27 | +Input: seats = [3,1,5], students = [2,7,4] |
| 28 | +Output: 4 |
| 29 | +Explanation: The students are moved as follows: |
| 30 | +- The first student is moved from position 2 to position 1 using 1 move. |
| 31 | +- The second student is moved from position 7 to position 5 using 2 moves. |
| 32 | +- The third student is moved from position 4 to position 3 using 1 move. |
| 33 | +In total, 1 + 2 + 1 = 4 moves were used. |
| 34 | +``` |
| 35 | + |
| 36 | +**Example 2:** |
| 37 | + |
| 38 | +``` |
| 39 | +Input: seats = [4,1,5,9], students = [1,3,2,6] |
| 40 | +Output: 7 |
| 41 | +Explanation: The students are moved as follows: |
| 42 | +- The first student is not moved. |
| 43 | +- The second student is moved from position 3 to position 4 using 1 move. |
| 44 | +- The third student is moved from position 2 to position 5 using 3 moves. |
| 45 | +- The fourth student is moved from position 6 to position 9 using 3 moves. |
| 46 | +In total, 0 + 1 + 3 + 3 = 7 moves were used. |
| 47 | +``` |
| 48 | + |
| 49 | +**Example 3:** |
| 50 | + |
| 51 | +``` |
| 52 | +Input: seats = [2,2,6,6], students = [1,3,2,6] |
| 53 | +Output: 4 |
| 54 | +Explanation: Note that there are two seats at position 2 and two seats at position 6. |
| 55 | +The students are moved as follows: |
| 56 | +- The first student is moved from position 1 to position 2 using 1 move. |
| 57 | +- The second student is moved from position 3 to position 6 using 3 moves. |
| 58 | +- The third student is not moved. |
| 59 | +- The fourth student is not moved. |
| 60 | +In total, 1 + 3 + 0 + 0 = 4 moves were used. |
| 61 | +``` |
| 62 | + |
| 63 | +### Constraints |
| 64 | + |
| 65 | +- `n == seats.length == students.length` |
| 66 | +- `1 <= n <= 100` |
| 67 | +- `1 <= seats[i], students[j] <= 100` |
| 68 | + |
| 69 | +--- |
| 70 | + |
| 71 | +## Solution for Minimum Moves to Seat Students Problem |
| 72 | + |
| 73 | +### Intuition and Approach |
| 74 | + |
| 75 | +The problem can be solved using a brute force approach or an optimized greedy approach. The brute force approach examines all possible assignments of students to seats, while the greedy approach sorts the seats and students and pairs them to minimize the total number of moves. |
| 76 | + |
| 77 | +<Tabs> |
| 78 | +<tabItem value="Brute Force" label="Brute Force"> |
| 79 | + |
| 80 | +### Approach 1: Brute Force (Naive) |
| 81 | + |
| 82 | +The brute force approach iterates through each possible permutation of the students and calculates the total number of moves for each permutation to find the minimum. |
| 83 | + |
| 84 | +#### Code in Different Languages |
| 85 | + |
| 86 | +<Tabs> |
| 87 | +<TabItem value="C++" label="C++" default> |
| 88 | +<SolutionAuthor name="@ImmidiSivani"/> |
| 89 | + |
| 90 | +```cpp |
| 91 | +#include <vector> |
| 92 | +#include <algorithm> |
| 93 | + |
| 94 | +class Solution { |
| 95 | +public: |
| 96 | + int minMovesToSeat(std::vector<int>& seats, std::vector<int>& students) { |
| 97 | + int min_moves = INT_MAX; |
| 98 | + std::sort(seats.begin(), seats.end()); |
| 99 | + std::sort(students.begin(), students.end()); |
| 100 | + do { |
| 101 | + int moves = 0; |
| 102 | + for (int i = 0; i < seats.size(); ++i) { |
| 103 | + moves += abs(seats[i] - students[i]); |
| 104 | + } |
| 105 | + min_moves = std::min(min_moves, moves); |
| 106 | + } while (std::next_permutation(students.begin(), students.end())); |
| 107 | + return min_moves; |
| 108 | + } |
| 109 | +}; |
| 110 | +``` |
| 111 | +
|
| 112 | +</TabItem> |
| 113 | +<TabItem value="Java" label="Java"> |
| 114 | +<SolutionAuthor name="@ImmidiSivani"/> |
| 115 | +
|
| 116 | +```java |
| 117 | +import java.util.Arrays; |
| 118 | +
|
| 119 | +class Solution { |
| 120 | + public int minMovesToSeat(int[] seats, int[] students) { |
| 121 | + Arrays.sort(seats); |
| 122 | + Arrays.sort(students); |
| 123 | + int minMoves = Integer.MAX_VALUE; |
| 124 | +
|
| 125 | + do { |
| 126 | + int moves = 0; |
| 127 | + for (int i = 0; i < seats.length; i++) { |
| 128 | + moves += Math.abs(seats[i] - students[i]); |
| 129 | + } |
| 130 | + minMoves = Math.min(minMoves, moves); |
| 131 | + } while (nextPermutation(students)); |
| 132 | +
|
| 133 | + return minMoves; |
| 134 | + } |
| 135 | +
|
| 136 | + private boolean nextPermutation(int[] nums) { |
| 137 | + int i = nums.length - 2; |
| 138 | + while (i >= 0 && nums[i] >= nums[i + 1]) { |
| 139 | + i--; |
| 140 | + } |
| 141 | + if (i < 0) { |
| 142 | + return false; |
| 143 | + } |
| 144 | + int j = nums.length - 1; |
| 145 | + while (nums[j] <= nums[i]) { |
| 146 | + j--; |
| 147 | + } |
| 148 | + swap(nums, i, j); |
| 149 | + reverse(nums, i + 1, nums.length - 1); |
| 150 | + return true; |
| 151 | + } |
| 152 | +
|
| 153 | + private void swap(int[] nums, int i, int j) { |
| 154 | + int temp = nums[i]; |
| 155 | + nums[i] = nums[j]; |
| 156 | + nums[j] = temp; |
| 157 | + } |
| 158 | +
|
| 159 | + private void reverse(int[] nums, int start, int end) { |
| 160 | + while (start < end) { |
| 161 | + int temp = nums[start]; |
| 162 | + nums[start] = nums[end]; |
| 163 | + nums[end] = temp; |
| 164 | + start++; |
| 165 | + end--; |
| 166 | + } |
| 167 | + } |
| 168 | +} |
| 169 | +``` |
| 170 | + |
| 171 | +</TabItem> |
| 172 | +<TabItem value="Python" label="Python"> |
| 173 | +<SolutionAuthor name="@ImmidiSivani"/> |
| 174 | + |
| 175 | +```python |
| 176 | +import itertools |
| 177 | + |
| 178 | +class Solution: |
| 179 | + def minMovesToSeat(self, seats: List[int], students: List[int]) -> int: |
| 180 | + seats.sort() |
| 181 | + students.sort() |
| 182 | + min_moves = float('inf') |
| 183 | + |
| 184 | + for perm in itertools.permutations(students): |
| 185 | + moves = sum(abs(seat - student) for seat, student in zip(seats, perm)) |
| 186 | + min_moves = min(min_moves, moves) |
| 187 | + |
| 188 | + return min_moves |
| 189 | +``` |
| 190 | + |
| 191 | +</TabItem> |
| 192 | +</Tabs> |
| 193 | + |
| 194 | +#### Complexity Analysis |
| 195 | + |
| 196 | +- Time Complexity: $O(n!*n)$ due to generating all permutations and calculating moves. |
| 197 | +- Space Complexity: $O(n)$ for storing permutations and temporary variables. |
| 198 | +- Where `n` is the length of the seats and students arrays. |
| 199 | +- The time complexity is $O(n!*n)$ because we generate all permutations. |
| 200 | +- The space complexity is $O(n)$ because we store temporary variables and permutations. |
| 201 | + |
| 202 | +</tabItem> |
| 203 | +<tabItem value="Greedy" label="Greedy"> |
| 204 | + |
| 205 | +### Approach 2: Greedy (Optimized) |
| 206 | + |
| 207 | +The greedy approach involves sorting the seats and students arrays and then pairing each student with the corresponding seat. This ensures that the total number of moves is minimized. |
| 208 | + |
| 209 | +#### Code in Different Languages |
| 210 | + |
| 211 | +<Tabs> |
| 212 | +<TabItem value="C++" label="C++" default> |
| 213 | +<SolutionAuthor name="@ImmidiSivani"/> |
| 214 | + |
| 215 | +```cpp |
| 216 | +#include <vector> |
| 217 | +#include <algorithm> |
| 218 | + |
| 219 | +class Solution { |
| 220 | +public: |
| 221 | + int minMovesToSeat(std::vector<int>& seats, std::vector<int>& students) { |
| 222 | + std::sort(seats.begin(), seats.end()); |
| 223 | + std::sort(students.begin(), students.end()); |
| 224 | + |
| 225 | + int moves = 0; |
| 226 | + for (int i = 0; i < seats.size(); ++i) { |
| 227 | + moves += abs(seats[i] - students[i]); |
| 228 | + } |
| 229 | + |
| 230 | + return moves; |
| 231 | + } |
| 232 | +}; |
| 233 | +``` |
| 234 | +
|
| 235 | +</TabItem> |
| 236 | +<TabItem value="Java" label="Java"> |
| 237 | +<SolutionAuthor name="@ImmidiSivani"/> |
| 238 | +
|
| 239 | +```java |
| 240 | +import java.util.Arrays; |
| 241 | +
|
| 242 | +class Solution { |
| 243 | + public int minMovesToSeat(int[] seats, int[] students) { |
| 244 | + Arrays.sort(seats); |
| 245 | + Arrays.sort(students); |
| 246 | + |
| 247 | + int moves = 0; |
| 248 | + for (int i = 0; i < seats.length; ++i) { |
| 249 | + moves += Math.abs(seats[i] - students[i]); |
| 250 | + } |
| 251 | + |
| 252 | + return moves; |
| 253 | + } |
| 254 | +} |
| 255 | +``` |
| 256 | + |
| 257 | +</TabItem> |
| 258 | +<TabItem value="Python" label="Python"> |
| 259 | +<SolutionAuthor name="@ImmidiSivani"/> |
| 260 | + |
| 261 | +```python |
| 262 | +class Solution: |
| 263 | + def minMovesToSeat(self, seats: List[int], students: List[int]) -> int: |
| 264 | + seats.sort() |
| 265 | + students.sort() |
| 266 | + |
| 267 | + moves = 0 |
| 268 | + for seat, student in zip(seats, students): |
| 269 | + moves += abs(seat - student) |
| 270 | + |
| 271 | + return moves |
| 272 | +``` |
| 273 | + |
| 274 | +</TabItem> |
| 275 | +</Tabs> |
| 276 | + |
| 277 | +#### Complexity Analysis |
| 278 | + |
| 279 | +- Time Complexity: $O(nlogn)$ due to sorting. |
| 280 | +- Space Complexity: $O(1)$ if sorting in place. |
| 281 | +- Where `n` is the length of the seats and |
| 282 | + |
| 283 | + students arrays. |
| 284 | +- The time complexity is $O(nlogn)$ because of the sorting operations. |
| 285 | +- The space complexity is $O(1)$ if the sorting is done in place, otherwise $O(n)$. |
| 286 | + |
| 287 | +</tabItem> |
| 288 | +</Tabs> |
| 289 | + |
| 290 | +--- |
| 291 | + |
| 292 | +<h2>Authors:</h2> |
| 293 | + |
| 294 | +<div style={{display: 'flex', flexWrap: 'wrap', justifyContent: 'space-between', gap: '10px'}}> |
| 295 | +{['ImmidiSivani'].map(username => ( |
| 296 | + <Author key={username} username={username} /> |
| 297 | +))} |
| 298 | +</div> |
0 commit comments