| | 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.MinimumDifferenceBetweenLargestAndSmallestValueInThreeMoves; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class MinimumDifferenceBetweenLargestAndSmallestValueInThreeMovesSorting : |
| | 16 | | IMinimumDifferenceBetweenLargestAndSmallestValueInThreeMoves |
| | 17 | | { |
| | 18 | | /// <summary> |
| | 19 | | /// Time complexity - O(n log n) |
| | 20 | | /// Space complexity - O(log n) |
| | 21 | | /// </summary> |
| | 22 | | /// <param name="nums"></param> |
| | 23 | | /// <returns></returns> |
| | 24 | | public int MinDifference(int[] nums) |
| 4 | 25 | | { |
| 4 | 26 | | if (nums.Length <= 4) |
| 2 | 27 | | { |
| 2 | 28 | | return 0; |
| | 29 | | } |
| | 30 | |
|
| 2 | 31 | | Array.Sort(nums); |
| | 32 | |
|
| 2 | 33 | | var minDifference = int.MaxValue; |
| | 34 | |
|
| 20 | 35 | | for (var left = 0; left < 4; left++) |
| 8 | 36 | | { |
| 8 | 37 | | var right = nums.Length - 4 + left; |
| | 38 | |
|
| 8 | 39 | | minDifference = Math.Min(minDifference, nums[right] - nums[left]); |
| 8 | 40 | | } |
| | 41 | |
|
| 2 | 42 | | return minDifference; |
| 4 | 43 | | } |
| | 44 | | } |