Minimum Number of Moves to Seat Everyone

🏠 ⬅️ ➡️

There are n seats and n students in a room. You are given an array seats of length n, where seats[i] is the position of the ith seat. You are also given the array students of length n, where students[j] is the position of the jth student.

You may perform the following move any number of times:

  • 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)

Return the minimum number of moves required to move each student to a seat such that no two students are in the same seat.

Note that there may be multiple seats or students in the same position at the beginning.

Example 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.

Example 2:

Input: seats = [4,1,5,9], students = [1,3,2,6] Output: 7 Explanation: The students are moved as follows: - The first student is not moved. - The second student is moved from from position 3 to position 4 using 1 move. - The third student is moved from from position 2 to position 5 using 3 moves. - The fourth student is moved from from position 6 to position 9 using 3 moves. In total, 0 + 1 + 3 + 3 = 7 moves were used.

Example 3:

Input: seats = [2,2,6,6], students = [1,3,2,6] Output: 4 Explanation: Note that there are two seats at position 2 and two seats at position 6. The students are moved as follows: - The first student is moved from from position 1 to position 2 using 1 move. - The second student is moved from from position 3 to position 6 using 3 moves. - The third student is not moved. - The fourth student is not moved. In total, 1 + 3 + 0 + 0 = 4 moves were used.

Constraints:

  • n == seats.length == students.length
  • 1 <= n <= 100
  • 1 <= seats[i], students[j] <= 100

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program main
    implicit none

    integer, parameter :: n = 3
    integer, dimension(n) :: seats = (/3, 1, 5/)
    integer, dimension(n) :: students = (/2, 7, 4/)
    integer :: i, j, moves

    do i = 1, n
        do j = 1, n
            if (seats(i) == students(j)) then
                print *, "Error: Student ", j, " is already in seat ", i
                stop 1
            end if
        end do
    end do

    moves = solve(seats, students)
    print '(A, I0)', 'Minimum number of moves: ', moves

contains

    function solve(seats, students) result(moves)
        implicit none
        integer, intent(in) :: seats(:), students(:)
        integer :: moves, i, j, k, temp

        moves = 0
        do i = 1, size(students)
            do j = 1, size(seats)
                if (students(i) == seats(j)) then
                    ! Move student i to seat j
                    temp = students(i)
                    students(i) = seats(j)
                    seats(j) = temp
                    moves = moves + 1
                    exit
                end if
            end do
        end do

        do while (any(students == seats))
            do i = 1, size(students)
                do j = 1, size(seats)
                    if (students(i) == seats(j)) then
                        ! Move student i to seat j
                        temp = students(i)
                        students(i) = seats(j)
                        seats(j) = temp
                        moves = moves + 1
                        exit
                    end if
                end do
            end do
        end do

        do i = 1, size(students)
            do j = 1, size(seats)
                if (students(i) == seats(j)) then
                    print *, "Error: Student ", i, " is still in seat ", j
                    stop 1
                end if
            end do
        end do

    end function solve

end program main
Compiled
Executed
Correct
module min_moves_to_seat
  implicit none
contains
  integer function min_moves(seats, students) result(min_moves)
    integer, intent(in) :: seats(:), students(:)
    integer :: i, j, moves, student_pos, seat_pos

    min_moves = 0
    do i = 1, size(students)
      student_pos = students(i)
      do j = 1, size(seats)
        seat_pos = seats(j)
        if (student_pos == seat_pos) then
          exit
        end if
      end do
      if (j > size(seats)) then
        min_moves = min_moves + abs(student_pos - seat_pos)
      end if
    end do
  end function min_moves
end module min_moves_to_seat

program test_min_moves
  use min_moves_to_seat
  implicit none
  integer, parameter :: n = 3
  integer :: seats(n), students(n)
  integer :: min_moves

  seats = [3, 1, 5]
  students = [2, 7, 4]
  min_moves = min_moves(seats, students)
  write (*,*) "Minimum moves:", min_moves

  seats = [4, 1, 5, 9]
  students = [1, 3, 2, 6]
  min_moves = min_moves(seats, students)
  write (*,*) "Minimum moves:", min_moves

  seats = [2, 2, 6, 6]
  students = [1, 3, 2, 6]
  min_moves = min_moves(seats, students)
  write (*,*) "Minimum moves:", min_moves
end program test_min_moves
🌐 Data from online sources
def count_square_triples(n):
    count = 0
    for a in range(1, n + 1):
        for b in range(a, n + 1):
            c = int((a * a + b * b)**0.5)
            if c * c == a * a + b * b and c <= n:
                count += 1
    return count
1. Initialize the count of square triples to 0.
  1. Start two nested loops with variables a and b ranging from 1 to n, inclusive.
  2. Inside the inner loop, calculate c as the square root of a² + b², rounded down to the nearest integer, and check if c² equals a² + b² and c is less than or equal to n.
  3. If the condition is true, increment the count of square triples.
  4. Repeat steps 2-4 for all values of a and b.
  5. Return the count of square triples.
🌐 Data from online sources
#include <cmath>

int countSquareTriples(int n) {
    int count = 0;
    for (int a = 1; a <= n; a++) {
        for (int b = a; b <= n; b++) {
            int c = std::sqrt(a * a + b * b);
            if (c * c == a * a + b * b && c <= n) {
                count++;
            }
        }
    }
    return count;
}
1. Initialize the count of square triples to 0.
  1. Start two nested loops with variables a and b ranging from 1 to n, inclusive.
  2. Inside the inner loop, calculate c as the square root of a² + b², rounded down to the nearest integer, and check if c² equals a² + b² and c is less than or equal to n.
  3. If the condition is true, increment the count of square triples.
  4. Repeat steps 2-4 for all values of a and b.
  5. Return the count of square triples.