Skip to content

Commit 7fde7a1

Browse files
easy: 2037. Minimum Number of Moves to Seat Everyone
1 parent 33cf75e commit 7fde7a1

3 files changed

Lines changed: 76 additions & 0 deletions

File tree

.idea/material_theme_project_new.xml

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @param {number[]} seats
3+
* @param {number[]} students
4+
* @return {number}
5+
*/
6+
function minMovesToSeat(seats, students) {
7+
// Sort both arrays
8+
seats.sort((a, b) => a - b);
9+
students.sort((a, b) => a - b);
10+
11+
let totalMoves = 0;
12+
13+
// Calculate the total moves required
14+
for (let i = 0; i < seats.length; i++) {
15+
totalMoves += Math.abs(seats[i] - students[i]);
16+
}
17+
18+
return totalMoves;
19+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# [2037. Minimum Number of Moves to Seat Everyone](https://leetcode.com/problems/minimum-number-of-moves-to-seat-everyone)
2+
3+
---
4+
title: "Minimize Moves to Seat Students"
5+
summary: "An algorithm to minimize the number of moves required to seat students in the correct order by sorting and calculating the absolute difference."
6+
date: 2024-06-13
7+
modifiedDate: 2024-06-13
8+
tags: ["algorithm", "sorting", "javascript", "complexity"]
9+
slug: "minimize-moves-to-seat-students"
10+
11+
---
12+
13+
![image.png](https://assets.leetcode.com/users/images/5014e99d-638d-4e12-be8c-859fcc3e8800_1718280552.7824206.png)
14+
15+
16+
# Intuition
17+
The initial intuition to solve this problem is to match the students to the seats in such a way that the total number of moves is minimized. This can be achieved by pairing the smallest available seat with the smallest student, the next smallest seat with the next smallest student, and so on.
18+
19+
# Approach
20+
1. **Sort the Arrays**: Start by sorting both the seats and the students arrays. This ensures that the smallest seat is paired with the smallest student, minimizing the movement.
21+
2. **Calculate Moves**: Iterate through the sorted arrays and compute the absolute difference between the corresponding elements. Sum these differences to get the total number of moves required.
22+
23+
# Complexity
24+
- Time complexity: O(n log n) due to the sorting operation on both arrays.
25+
- Space complexity: O(1) if we do not count the input arrays, as we are using a constant amount of additional space.
26+
27+
# Code
28+
```javascript
29+
/**
30+
* @param {number[]} seats
31+
* @param {number[]} students
32+
* @return {number}
33+
*/
34+
function minMovesToSeat(seats, students) {
35+
// Sort both arrays
36+
seats.sort((a, b) => a - b);
37+
students.sort((a, b) => a - b);
38+
39+
let totalMoves = 0;
40+
41+
// Calculate the total moves required
42+
for (let i = 0; i < seats.length; i++) {
43+
totalMoves += Math.abs(seats[i] - students[i]);
44+
}
45+
46+
return totalMoves;
47+
}

0 commit comments

Comments
 (0)