[#3025] feat(numberOfPairs): add C# solution, add notes
This commit is contained in:
@@ -18,14 +18,19 @@ Return the count.
|
|||||||
## 解題思路
|
## 解題思路
|
||||||
|
|
||||||
### 初步分析
|
### 初步分析
|
||||||
- 這題主要考察什麼概念?
|
- 核心概念
|
||||||
- 有什麼關鍵限制條件?
|
幾何關係判斷 + 區域內點的檢測
|
||||||
|
- 關鍵限制條件
|
||||||
|
1. A必須在B的左上方, A.x <= B.x, A.y >= B.y
|
||||||
|
2. 矩形區域內(包含邊界)不能有其他點
|
||||||
- 預期時間/空間複雜度?
|
- 預期時間/空間複雜度?
|
||||||
|
|
||||||
### 解法概述
|
### 解法概述
|
||||||
1. **暴力解法**:
|
1. **暴力解法**:
|
||||||
- 思路:
|
- 思路:
|
||||||
- 時間複雜度:O(?)
|
- 遍歷所有點對 (A, B)
|
||||||
|
- 檢查每個pair形成的矩形, 裡面跟邊界會不會包含其他點
|
||||||
|
- 時間複雜度:O(N^3)
|
||||||
- 空間複雜度:O(?)
|
- 空間複雜度:O(?)
|
||||||
|
|
||||||
2. **優化解法**:
|
2. **優化解法**:
|
||||||
@@ -54,17 +59,16 @@ Explanation:
|
|||||||
## 學習筆記
|
## 學習筆記
|
||||||
|
|
||||||
### 今天學到什麼?
|
### 今天學到什麼?
|
||||||
-
|
- 二維空間判斷點大小
|
||||||
|
|
||||||
### 遇到的困難
|
### 遇到的困難
|
||||||
-
|
- 無
|
||||||
|
|
||||||
### 改善方向
|
### 改善方向
|
||||||
-
|
- 無
|
||||||
|
|
||||||
### 相關題目
|
### 相關題目
|
||||||
- [題目編號] 題目名稱 - 相似概念
|
- [#223](https://leetcode.com/problems/rectangle-area/) Rectangle Area
|
||||||
- [題目編號] 題目名稱 - 進階版本
|
|
||||||
|
|
||||||
---
|
---
|
||||||
**總結**: 這題的核心概念是...,適合練習...技巧。
|
**總結**: 這題的核心概念是...,適合練習...技巧。
|
||||||
|
@@ -4,33 +4,83 @@
|
|||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Drawing;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
|
||||||
public class Solution {
|
public class Solution
|
||||||
public void Solve() {
|
{
|
||||||
// TODO: 實作解法
|
public int NumberOfPairsWithBruteForce(int[][] points)
|
||||||
Console.WriteLine("Hello LeetCode 3025!");
|
{
|
||||||
|
// 遍歷所有點對 (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 class Program {
|
||||||
public static void Main() {
|
public static void Main() {
|
||||||
Solution sol = new Solution();
|
Solution solution = new Solution();
|
||||||
sol.Solve();
|
|
||||||
|
// 測試案例
|
||||||
// TODO: 加入測試案例
|
TestCase(solution);
|
||||||
// TestCase1();
|
|
||||||
// TestCase2();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 測試案例模板
|
static void TestCase(Solution solution) {
|
||||||
/*
|
|
||||||
static void TestCase1() {
|
|
||||||
// Input:
|
// Input:
|
||||||
|
int[][] points = new int[][] {
|
||||||
|
new int[] {6, 2},
|
||||||
|
new int[] {4, 4},
|
||||||
|
new int[] {2, 6}
|
||||||
|
};
|
||||||
// Expected:
|
// Expected:
|
||||||
|
Console.WriteLine($"測試案例 1: [[6,2],[4,4],[2,6]]");
|
||||||
|
Console.WriteLine($"預期: 2");
|
||||||
|
|
||||||
// Actual:
|
// Actual:
|
||||||
Console.WriteLine("Test 1: ");
|
int result1 = solution.NumberOfPairsWithBruteForce(points);
|
||||||
|
Console.WriteLine($"Brute Force: {result1}");
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user