Skip to content

Commit 9215436

Browse files
solves minimum number of moves to seat everyone
1 parent 4ed6fc5 commit 9215436

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@
485485
| 2022 | [Convert 1D Array Into 2D Array](https://leetcode.com/problems/convert-1d-array-into-2d-array) | [![Java](assets/java.png)](src/Convert1DArrayInto2DArray.java) | |
486486
| 2027 | [Minimum Moves to Convert String](https://leetcode.com/problems/minimum-moves-to-convert-string) | [![Java](assets/java.png)](src/MinimumMovesToConvertString.java) | |
487487
| 2032 | [Two Out of Three](https://leetcode.com/problems/two-out-of-three) | [![Java](assets/java.png)](src/TwoOutOfThree.java) | |
488-
| 2037 | [Minimum Number of Moves to Seat Everyone](https://leetcode.com/problems/minimum-number-of-moves-to-seat-everyone) | | |
488+
| 2037 | [Minimum Number of Moves to Seat Everyone](https://leetcode.com/problems/minimum-number-of-moves-to-seat-everyone) | [![Java](assets/java.png)](src/MinimumNumberOfMovesToSeatEveryone.java) | |
489489
| 2042 | [Check if Numbers Are Ascending in a Sentence](https://leetcode.com/problems/check-if-numbers-are-ascending-in-a-sentence) | | |
490490
| 2047 | [Number of Valid Words in a Sentence](https://leetcode.com/problems/number-of-valid-words-in-a-sentence) | | |
491491
| 2053 | [Kth Distinct String in an Array](https://leetcode.com/problems/kth-distinct-string-in-an-array) | | |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// https://leetcode.com/problems/minimum-number-of-moves-to-seat-everyone
2+
// T: O(n log(n))
3+
// S: O(1)
4+
5+
import java.util.Arrays;
6+
7+
public class MinimumNumberOfMovesToSeatEveryone {
8+
public int minMovesToSeat(int[] seats, int[] students) {
9+
Arrays.sort(seats);
10+
Arrays.sort(students);
11+
int minMoves = 0;
12+
for (int i = 0 ; i < students.length ; i++) {
13+
minMoves += Math.abs(seats[i] - students[i]);
14+
}
15+
return minMoves;
16+
}
17+
}

0 commit comments

Comments
 (0)