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

77 lines
2.1 KiB
C#

// LeetCode 3027: Find The Number Of Ways To Place People Ii
// 難度: Hard
// 日期: 2025-09-03
using System;
using System.Collections.Generic;
using System.Linq;
public class Solution {
public int NumberOfPairs(int[][] points)
{
int n = points.Length;
int result = 0;
for (var i = 0; i < n; i++)
{
for (var j = 0; j < n; j++)
{
if (i == j) continue;
// 判斷 Alice points[i] 是否在 Bob points[j] 左上角
var Alice = points[i];
var Bob = points[j];
if (Alice[0] <= Bob[0] && Alice[1] >= Bob[1])
{
// get border
int minX = Alice[0];
int maxX = Bob[0];
int minY = Bob[1];
int maxY = Alice[1];
bool isAnyPerson = false;
for (var k = 0; k < n; k++)
{
if (i == k || j == k) continue;
var person = points[k];
if (minX <= person[0] && person[0] <= maxX && minY <= person[1] && person[1] <= maxY)
{
isAnyPerson = true;
break;
}
}
if (!isAnyPerson) result++;
}
}
}
return result;
}
}
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}");
}
}