Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit f6bc256

Browse files
committed
Add solution #2037
1 parent 244f290 commit f6bc256

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* 2037. Minimum Number of Moves to Seat Everyone
3+
* https://leetcode.com/problems/minimum-number-of-moves-to-seat-everyone/
4+
* Difficulty: Easy
5+
*
6+
* There are n availabe seats and n students standing in a room. You are given an array
7+
* seats of length n, where seats[i] is the position of the ith seat. You are also given
8+
* the array students of length n, where students[j] is the position of the jth student.
9+
*
10+
* You may perform the following move any number of times:
11+
* - Increase or decrease the position of the ith student by 1 (i.e., moving the ith
12+
* student from position x to x + 1 or x - 1)
13+
*
14+
* Return the minimum number of moves required to move each student to a seat such that
15+
* no two students are in the same seat.
16+
*
17+
* Note that there may be multiple seats or students in the same position at the beginning.
18+
*/
19+
20+
/**
21+
* @param {number[]} seats
22+
* @param {number[]} students
23+
* @return {number}
24+
*/
25+
var minMovesToSeat = function(seats, students) {
26+
seats.sort((a, b) => a - b);
27+
students.sort((a, b) => a - b);
28+
return seats.reduce((a, b, i) => a += Math.abs(seats[i] - students[i]), 0);
29+
};

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@
368368
2011|[Final Value of Variable After Performing Operations](./2011-final-value-of-variable-after-performing-operations.js)|Easy|
369369
2016|[Maximum Difference Between Increasing Elements](./2016-maximum-difference-between-increasing-elements.js)|Easy|
370370
2027|[Minimum Moves to Convert String](./2027-minimum-moves-to-convert-string.js)|Easy|
371+
2037|[Minimum Number of Moves to Seat Everyone](./2037-minimum-number-of-moves-to-seat-everyone.js)|Easy|
371372
2047|[Number of Valid Words in a Sentence](./2047-number-of-valid-words-in-a-sentence.js)|Easy|
372373
2053|[Kth Distinct String in an Array](./2053-kth-distinct-string-in-an-array.js)|Medium|
373374
2085|[Count Common Words With One Occurrence](./2085-count-common-words-with-one-occurrence.js)|Easy|

0 commit comments

Comments
 (0)