[LeetCode]#2037. Minimum Number of Moves to Seat Everyone

  • Increase or decrease the position of the ith student by 1 (i.e., moving the ith student from position x to x + 1 or x - 1)
Input: seats = [3,1,5], students = [2,7,4]
Output: 4
Explanation: The students are moved as follows:
- The first student is moved from from position 2 to position 1 using 1 move.
- The second student is moved from from position 7 to position 5 using 2 moves.
- The third student is moved from from position 4 to position 3 using 1 move.
In total, 1 + 2 + 1 = 4 moves were used.
  1. Sort seats and students.
  2. Use for loop.
  3. Get difference abs(seats[i]-students[i]) and sum them.
class Solution:
def minMovesToSeat(self, seats, students):
ans=0
seats.sort()
students.sort()
for i in range(len(seats)):
ans=ans+abs(seats[i]-students[i])
return ans

--

--

--

Interesting in any computer science.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

The Building Blocks Thesis -Volume 1

On vAMM’s unnecessity for a liquidity pool

Kotlin on Google Cloud Platform

What happens when you type `ls -l *.c` in the shell

Azure Logic Apps Anywhere — Develop & Run Locally in VS Code

What are no code development platforms?

The Process of Hiring Full Stack Developers for your next project

[LeetCode]#908. Smallest Range I

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Fatboy Slim

Fatboy Slim

Interesting in any computer science.

More from Medium

LeetCode Patterns Adventure 12 — Palindrome Linked List

Introduction to LinkedList

Implementing Linked List Operations in Python

Leetcode 534 Game Play Analysis III without sum window function