Files
coding-practice/problems/3025-find-the-number-of-ways-to-place-people-i/csharp/Program.cs

87 lines
2.4 KiB
C#

// LeetCode 3025: Find The Number Of Ways To Place People I
// 難度: Medium
// 日期: 2025-09-02
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
public class Solution
{
public int NumberOfPairs(int[][] points)
{
// 遍歷所有點對 (A, B)
// 檢查每個pair形成的矩形, 裡面跟邊界會不會包含其他點
// points[0].Length == 2
int n = points.Length;
int count = 0;
for (var i = 0; i < n; i++)
{
for (var j = 0; j < n; j++)
{
if (i == j) continue;
int[] pointA = points[i];
int[] pointB = points[j];
// check if A.x <= B.x && A.y >= B.y
// if yes, check if any points in rectangle
if (pointA[0] <= pointB[0] && pointA[1] >= pointB[1])
{
// difinition of border
bool hasOtherPoint = false;
int minX = pointA[0];
int maxX = pointB[0];
int minY = pointB[1];
int maxY = pointA[1];
// check other point if its in border
for (var k = 0; k < n; k++)
{
if (k == i || k == j) continue;
var pointC = points[k];
if (minX <= pointC[0] && pointC[0] <= maxX
&& minY <= pointC[1] && pointC[1] <= maxY)
{
hasOtherPoint = true;
break;
}
}
if (!hasOtherPoint) count++;
}
}
}
return count;
}
}
public class Program {
public static void Main() {
Solution solution = new Solution();
// 測試案例
TestCase(solution);
}
static void TestCase(Solution solution) {
// Input:
int[][] points = new int[][] {
new int[] {6, 2},
new int[] {4, 4},
new int[] {2, 6}
};
// Expected:
Console.WriteLine($"Test Case: [[6,2],[4,4],[2,6]]");
Console.WriteLine($"Expected: 2");
// Actual:
int result1 = solution.NumberOfPairs(points);
Console.WriteLine($"Result: {result1}");
}
}