| | 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.FindTheNumberOfWaysToPlacePeople2; |
| | 13 | |
|
| | 14 | | /// <inheritdoc /> |
| | 15 | | public class FindTheNumberOfWaysToPlacePeople2SortingGreedy : IFindTheNumberOfWaysToPlacePeople2 |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Time complexity - O(n^2) |
| | 19 | | /// Space complexity - O(log n) |
| | 20 | | /// </summary> |
| | 21 | | /// <param name="points"></param> |
| | 22 | | /// <returns></returns> |
| | 23 | | public int NumberOfPairs(int[][] points) |
| 6 | 24 | | { |
| 6 | 25 | | Array.Sort(points, PointsComparison); |
| | 26 | |
|
| 6 | 27 | | var n = points.Length; |
| | 28 | |
|
| 6 | 29 | | var numberOfPairs = 0; |
| | 30 | |
|
| 48 | 31 | | for (var i = n - 1; i >= 1; i--) |
| 18 | 32 | | { |
| 18 | 33 | | var y1 = points[i][1]; |
| | 34 | |
|
| 18 | 35 | | var minY = int.MaxValue; |
| | 36 | |
|
| 116 | 37 | | for (var j = i - 1; j >= 0; j--) |
| 40 | 38 | | { |
| 40 | 39 | | var y2 = points[j][1]; |
| | 40 | |
|
| 40 | 41 | | if (y2 < y1 || y2 >= minY) |
| 22 | 42 | | { |
| 22 | 43 | | continue; |
| | 44 | | } |
| | 45 | |
|
| 18 | 46 | | minY = y2; |
| | 47 | |
|
| 18 | 48 | | numberOfPairs++; |
| 18 | 49 | | } |
| 18 | 50 | | } |
| | 51 | |
|
| 6 | 52 | | return numberOfPairs; |
| 6 | 53 | | } |
| | 54 | |
|
| | 55 | | private static int PointsComparison(int[] a, int[] b) |
| 39 | 56 | | { |
| 39 | 57 | | var xComparison = a[0].CompareTo(b[0]); |
| | 58 | |
|
| 39 | 59 | | return xComparison == 0 ? b[1].CompareTo(a[1]) : xComparison; |
| 39 | 60 | | } |
| | 61 | | } |