[#3025] feat(numberOfPairs): add C# solution, add notes

This commit is contained in:
2025-09-02 11:11:15 +08:00
parent 262a934d89
commit 5018b4ab9f
2 changed files with 78 additions and 24 deletions

View File

@@ -18,14 +18,19 @@ Return the count.
## 解題思路
### 初步分析
- 這題主要考察什麼概念
- 有什麼關鍵限制條件?
- 核心概念
幾何關係判斷 + 區域內點的檢測
- 關鍵限制條件
1. A必須在B的左上方, A.x <= B.x, A.y >= B.y
2. 矩形區域內(包含邊界)不能有其他點
- 預期時間/空間複雜度?
### 解法概述
1. **暴力解法**:
- 思路:
- 時間複雜度O(?)
- 遍歷所有點對 (A, B)
- 檢查每個pair形成的矩形, 裡面跟邊界會不會包含其他點
- 時間複雜度O(N^3)
- 空間複雜度O(?)
2. **優化解法**:
@@ -54,17 +59,16 @@ Explanation:
## 學習筆記
### 今天學到什麼?
-
- 二維空間判斷點大小
### 遇到的困難
-
-
### 改善方向
-
-
### 相關題目
- [題目編號] 題目名稱 - 相似概念
- [題目編號] 題目名稱 - 進階版本
- [#223](https://leetcode.com/problems/rectangle-area/) Rectangle Area
---
**總結**: 這題的核心概念是...,適合練習...技巧。

View File

@@ -4,33 +4,83 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
public class Solution {
public void Solve() {
// TODO: 實作解法
Console.WriteLine("Hello LeetCode 3025!");
public class Solution
{
public int NumberOfPairsWithBruteForce(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 sol = new Solution();
sol.Solve();
Solution solution = new Solution();
// TODO: 加入測試案例
// TestCase1();
// TestCase2();
// 測試案例
TestCase(solution);
}
// 測試案例模板
/*
static void TestCase1() {
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($"測試案例 1: [[6,2],[4,4],[2,6]]");
Console.WriteLine($"預期: 2");
// Actual:
Console.WriteLine("Test 1: ");
int result1 = solution.NumberOfPairsWithBruteForce(points);
Console.WriteLine($"Brute Force: {result1}");
}
*/
}