Skip to content

Commit ace0342

Browse files
committed
feat: add solutions to lc problem: No.2037
No.2037.Minimum Number of Moves to Seat Everyone
1 parent f0a92ad commit ace0342

File tree

5 files changed

+135
-0
lines changed

5 files changed

+135
-0
lines changed

solution/2000-2099/2037.Minimum Number of Moves to Seat Everyone/README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,56 @@ func abs(x int) int {
145145
}
146146
```
147147

148+
### **TypeScript**
149+
150+
```ts
151+
function minMovesToSeat(seats: number[], students: number[]): number {
152+
seats.sort((a, b) => a - b);
153+
students.sort((a, b) => a - b);
154+
const n = seats.length;
155+
let ans = 0;
156+
for (let i = 0; i < n; i++) {
157+
ans += Math.abs(seats[i] - students[i]);
158+
}
159+
return ans;
160+
}
161+
```
162+
163+
### **Rust**
164+
165+
```rust
166+
impl Solution {
167+
pub fn min_moves_to_seat(mut seats: Vec<i32>, mut students: Vec<i32>) -> i32 {
168+
seats.sort();
169+
students.sort();
170+
let n = seats.len();
171+
let mut ans = 0;
172+
for i in 0..n {
173+
ans += (seats[i] - students[i]).abs();
174+
}
175+
ans
176+
}
177+
}
178+
```
179+
180+
### **C**
181+
182+
```c
183+
int cmp(const void *a, const void *b) {
184+
return *(int *) a - *(int *) b;
185+
}
186+
187+
int minMovesToSeat(int *seats, int seatsSize, int *students, int studentsSize) {
188+
qsort(seats, seatsSize, sizeof(int), cmp);
189+
qsort(students, studentsSize, sizeof(int), cmp);
190+
int ans = 0;
191+
for (int i = 0; i < seatsSize; i++) {
192+
ans += abs(seats[i] - students[i]);
193+
}
194+
return ans;
195+
}
196+
```
197+
148198
### **...**
149199
150200
```

solution/2000-2099/2037.Minimum Number of Moves to Seat Everyone/README_EN.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,56 @@ func abs(x int) int {
133133
}
134134
```
135135

136+
### **TypeScript**
137+
138+
```ts
139+
function minMovesToSeat(seats: number[], students: number[]): number {
140+
seats.sort((a, b) => a - b);
141+
students.sort((a, b) => a - b);
142+
const n = seats.length;
143+
let ans = 0;
144+
for (let i = 0; i < n; i++) {
145+
ans += Math.abs(seats[i] - students[i]);
146+
}
147+
return ans;
148+
}
149+
```
150+
151+
### **Rust**
152+
153+
```rust
154+
impl Solution {
155+
pub fn min_moves_to_seat(mut seats: Vec<i32>, mut students: Vec<i32>) -> i32 {
156+
seats.sort();
157+
students.sort();
158+
let n = seats.len();
159+
let mut ans = 0;
160+
for i in 0..n {
161+
ans += (seats[i] - students[i]).abs();
162+
}
163+
ans
164+
}
165+
}
166+
```
167+
168+
### **C**
169+
170+
```c
171+
int cmp(const void *a, const void *b) {
172+
return *(int *) a - *(int *) b;
173+
}
174+
175+
int minMovesToSeat(int *seats, int seatsSize, int *students, int studentsSize) {
176+
qsort(seats, seatsSize, sizeof(int), cmp);
177+
qsort(students, studentsSize, sizeof(int), cmp);
178+
int ans = 0;
179+
for (int i = 0; i < seatsSize; i++) {
180+
ans += abs(seats[i] - students[i]);
181+
}
182+
return ans;
183+
}
184+
```
185+
136186
### **...**
137187
138188
```
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
int cmp(const void *a, const void *b) {
2+
return *(int *) a - *(int *) b;
3+
}
4+
5+
int minMovesToSeat(int *seats, int seatsSize, int *students, int studentsSize) {
6+
qsort(seats, seatsSize, sizeof(int), cmp);
7+
qsort(students, studentsSize, sizeof(int), cmp);
8+
int ans = 0;
9+
for (int i = 0; i < seatsSize; i++) {
10+
ans += abs(seats[i] - students[i]);
11+
}
12+
return ans;
13+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
impl Solution {
2+
pub fn min_moves_to_seat(mut seats: Vec<i32>, mut students: Vec<i32>) -> i32 {
3+
seats.sort();
4+
students.sort();
5+
let n = seats.len();
6+
let mut ans = 0;
7+
for i in 0..n {
8+
ans += (seats[i] - students[i]).abs();
9+
}
10+
ans
11+
}
12+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function minMovesToSeat(seats: number[], students: number[]): number {
2+
seats.sort((a, b) => a - b);
3+
students.sort((a, b) => a - b);
4+
const n = seats.length;
5+
let ans = 0;
6+
for (let i = 0; i < n; i++) {
7+
ans += Math.abs(seats[i] - students[i]);
8+
}
9+
return ans;
10+
}

0 commit comments

Comments
 (0)