| | 1 | | // -------------------------------------------------------------------------------- |
| | 2 | | // Copyright (C) 2025 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | 3 | | // All Rights Reserved. |
| | 4 | | // -------------------------------------------------------------------------------- |
| | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | 9 | | // known as Yevhenii Yeriemeieiv). |
| | 10 | | // -------------------------------------------------------------------------------- |
| | 11 | |
|
| | 12 | | namespace LeetCode.Algorithms.MinimumNumberOfMovesToSeatEveryone; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class MinimumNumberOfMovesToSeatEveryoneCountingSort : IMinimumNumberOfMovesToSeatEveryone |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(n + maxPosition) |
| | 19 | | /// Space complexity - O(maxPosition) |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="seats"></param> |
| | 22 | | /// <param name="students"></param> |
| | 23 | | /// <returns></returns> |
| | 24 | | public int MinMovesToSeat(int[] seats, int[] students) |
| 3 | 25 | | { |
| 3 | 26 | | var maxPosition = Math.Max(seats.Max(), students.Max()); |
| | 27 | |
|
| 3 | 28 | | var differences = new int[maxPosition]; |
| | 29 | |
|
| 31 | 30 | | foreach (var seat in seats) |
| 11 | 31 | | { |
| 11 | 32 | | differences[seat - 1]++; |
| 11 | 33 | | } |
| | 34 | |
|
| 31 | 35 | | foreach (var student in students) |
| 11 | 36 | | { |
| 11 | 37 | | differences[student - 1]--; |
| 11 | 38 | | } |
| | 39 | |
|
| 3 | 40 | | var moves = 0; |
| 3 | 41 | | var unmatched = 0; |
| | 42 | |
|
| 53 | 43 | | foreach (var difference in differences) |
| 22 | 44 | | { |
| 22 | 45 | | moves += Math.Abs(unmatched); |
| | 46 | |
|
| 22 | 47 | | unmatched += difference; |
| 22 | 48 | | } |
| | 49 | |
|
| 3 | 50 | | return moves; |
| 3 | 51 | | } |
| | 52 | | } |