File tree Expand file tree Collapse file tree 2 files changed +52
-0
lines changed
leetcode/topic/sorting/2037 Expand file tree Collapse file tree 2 files changed +52
-0
lines changed Original file line number Diff line number Diff line change 1+ // https://leetcode.com/problems/minimum-number-of-moves-to-seat-everyone/description
2+
3+ package main
4+
5+ import (
6+ "fmt"
7+ "sort"
8+ )
9+
10+ func minMovesToSeat (seats []int , students []int ) int {
11+ sort .Ints (seats )
12+ sort .Ints (students )
13+
14+ total := 0
15+ for i := range len (seats ) {
16+ total += abs (seats [i ] - students [i ])
17+ }
18+
19+ return total
20+ }
21+
22+ func abs (x int ) int {
23+ if x < 0 {
24+ return - x
25+ }
26+ return x
27+ }
28+
29+ func main () {
30+ seats := []int {4 , 1 , 5 , 9 }
31+ students := []int {1 , 3 , 2 , 6 }
32+
33+ fmt .Println (minMovesToSeat (seats , students ))
34+ }
Original file line number Diff line number Diff line change 1+ from typing import List
2+
3+ class Solution :
4+ def minMovesToSeat (self , seats : List [int ], students : List [int ]) -> int :
5+ seats .sort ()
6+ students .sort ()
7+
8+ total = 0
9+ for i in range (len (seats )):
10+ total += abs (seats [i ] - students [i ])
11+
12+ return total
13+
14+ if __name__ == "__main__" :
15+ solution = Solution ()
16+ seats = [4 , 1 , 5 ]
17+ students = [2 , 7 , 3 ]
18+ print (solution .minMovesToSeat (seats , students ))
You can’t perform that action at this time.
0 commit comments