diff --git a/logs/2025-09.md b/logs/2025-09.md index 921e3d0..9f4c178 100644 --- a/logs/2025-09.md +++ b/logs/2025-09.md @@ -6,9 +6,10 @@ | 日期 | 題目 | 難度 | 語言 | 耗時 | 狀態 | 心得 | |------|------|------|------|------|------|------| | 09/01 | Maximum Average Pass Ratio | Medium | C# | 1hr | Done | 第一次用到Priority Queue | -| 09/02 | Find The Number of Ways to Place People I | Medium | C# | 0.5hr | Done | 二維點位判斷 | -| 09/02 | Find The Number of Ways to Place People I | Medium | go | 0.2hr | Done | go Prictice | +| 09/02 | Find The Number of Ways to Place People I | Medium | C#, go | 0.7hr | Done | 二維點位判斷 | | 09/03 | Find The Number of Ways to Place People II | Hard | C# | 0.2hr | Done | Same as [#3025] | +| 09/04 | Find Closest Person | Easy | C#, go | 0.1hr | Done | 數學判斷 | + ### Week 2 | 日期 | 題目 | 難度 | 語言 | 耗時 | 狀態 | 心得 | @@ -28,13 +29,13 @@ ## 📈 本月統計 ### 完成情況 -- **總練習天數**: 3 天 -- **完成題數**: 3題 -- **語言分布**: C# 3(題), Go 1(題) -- **難度分布**: Easy 0(題), Medium 2(題), Hard 1(題) +- **總練習天數**: 4 天 +- **完成題數**: 4題 +- **語言分布**: C# 4(題), Go 2(題) +- **難度分布**: Easy 1(題), Medium 2(題), Hard 1(題) ### 時間投入 -- **總時間**: 1.9小時 +- **總時間**: 2小時 - **平均每題**: 分鐘 - **每日平均**: 分鐘 diff --git a/problems/3516-find-closest-person/README.md b/problems/3516-find-closest-person/README.md new file mode 100644 index 0000000..3080eb1 --- /dev/null +++ b/problems/3516-find-closest-person/README.md @@ -0,0 +1,70 @@ +# [3516] Find Closest Person + +## 題目資訊 +- **難度**: Easy +- **標籤**: Math +- **題目連結**: [LeetCode](https://leetcode.com/problems/find-closest-person/) +- **練習日期**: 2025-09-04 + +## 題目描述 +You are given three integers `x`, `y`, and `z`, representing the positions of three people on a number line: + +`x` is the position of Person 1. +`y` is the position of Person 2. +`z` is the position of Person 3, who does **not** move. +Both Person 1 and Person 2 move toward Person 3 at the same speed. + +Determine which person reaches Person 3 first: + +Return 1 if Person 1 arrives first. +Return 2 if Person 2 arrives first. +Return 0 if both arrive at the same time. +Return the result accordingly. + +## 解題思路 + +### 初步分析 +- 這題主要考察什麼概念? + 距離計算和比較。使用絕對值計算兩點間距離 +- 有什麼關鍵限制條件? + 比較兩個距離的大小關係 +- 預期時間/空間複雜度? + 時間 O(1),空間 O(1) - 只需要常數時間的計算 + +### 解法概述 +**解法**: + - 思路: + 分別計算 Person 1 和 Person 2 到 Person 3 的距離,直接比較大小 + - 時間複雜度:O(1) + - 空間複雜度:O(1) + +## 測試案例 + +### 範例輸入輸出 +``` +Input: x = 2, y = 7, z = 4 +Output: 1 +Explanation: +- Person 1 is at position 2 and can reach Person 3 (at position 4) in 2 steps. +- Person 2 is at position 7 and can reach Person 3 in 3 steps. +Since Person 1 reaches Person 3 first, the output is 1. +``` + +### 邊界情況 +`1 <= x, y, z <= 100` + +## 學習筆記 + +### 今天學到什麼? +- 稍微練習了一下go func + +### 遇到的困難 +- 無 + +### 改善方向 +- 無 + +### 相關題目 + +--- +**總結**: 今天這題考弱智? diff --git a/problems/3516-find-closest-person/csharp/Program.cs b/problems/3516-find-closest-person/csharp/Program.cs new file mode 100644 index 0000000..bb71b41 --- /dev/null +++ b/problems/3516-find-closest-person/csharp/Program.cs @@ -0,0 +1,60 @@ +// LeetCode 3516: Find Closest Person +// 難度: Easy +// 日期: 2025-09-04 + +using System; +using System.Collections.Generic; +using System.Linq; + +public class Solution { + public int FindClosest(int x, int y, int z) + { + var distanceX = Math.Abs(x - z); + var distanceY = Math.Abs(y - z); + + return distanceX == distanceY ? 0 : distanceX > distanceY ? 2 : 1; + } +} + +// 測試程式 +public class Program +{ + public static void Main() + { + TestCase1(); + TestCase2(); + TestCase3(); + } + + // 基本測試案例 - Example 1 + static void TestCase1() + { + var solution = new Solution(); + // Input: x = 2, y = 7, z = 4 + // Expected: 1 (Person 1 距離較近: |2-4| = 2 < |7-4| = 3) + var actual = solution.FindClosest(2, 7, 4); + var expected = 1; + Console.WriteLine($"Test 1: Input(2, 7, 4) Expected: {expected}, Actual: {actual}, Result: {(actual == expected ? "PASS" : "FAIL")}"); + } + + // 基本測試案例 - Example 2 + static void TestCase2() + { + var solution = new Solution(); + // Input: x = 2, y = 5, z = 6 + // Expected: 2 (Person 2 距離較近: |5-6| = 1 < |2-6| = 4) + var actual = solution.FindClosest(2, 5, 6); + var expected = 2; + Console.WriteLine($"Test 2: Input(2, 5, 6) Expected: {expected}, Actual: {actual}, Result: {(actual == expected ? "PASS" : "FAIL")}"); + } + + // 基本測試案例 - Example 3 + static void TestCase3() { + var solution = new Solution(); + // Input: x = 1, y = 5, z = 3 + // Expected: 0 (距離相等: |1-3| = 2 = |5-3| = 2) + var actual = solution.FindClosest(1, 5, 3); + var expected = 0; + Console.WriteLine($"Test 3: Input(1, 5, 3) Expected: {expected}, Actual: {actual}, Result: {(actual == expected ? "PASS" : "FAIL")}"); + } +} diff --git a/problems/3516-find-closest-person/csharp/csharp.csproj b/problems/3516-find-closest-person/csharp/csharp.csproj new file mode 100644 index 0000000..2150e37 --- /dev/null +++ b/problems/3516-find-closest-person/csharp/csharp.csproj @@ -0,0 +1,10 @@ + + + + Exe + net8.0 + enable + enable + + + diff --git a/problems/3516-find-closest-person/go/go.mod b/problems/3516-find-closest-person/go/go.mod new file mode 100644 index 0000000..15d6b8c --- /dev/null +++ b/problems/3516-find-closest-person/go/go.mod @@ -0,0 +1,3 @@ +module leetcode-3516 + +go 1.18 diff --git a/problems/3516-find-closest-person/go/main.go b/problems/3516-find-closest-person/go/main.go new file mode 100644 index 0000000..18a5e08 --- /dev/null +++ b/problems/3516-find-closest-person/go/main.go @@ -0,0 +1,71 @@ +// LeetCode 3516: Find Closest Person +// 難度: Easy +// 日期: 2025-09-04 + +package main + +import "fmt" + +// TODO: 實作解法 +func findClosest(x, y, z int) int { + distanceX := abs(x - z) + distanceY := abs(y - z) + + if distanceX == distanceY{ + return 0 + }else if distanceX > distanceY{ + return 2 + }else{ + return 1 + } +} + +func abs(n int) int{ + if n < 0 { + return -n + } + return n +} + +func main() { + testCase1() + testCase2() + testCase3() +} + +// 基本測試案例 - Example 1 +func testCase1() { + // Input: x = 2, y = 7, z = 4 + // Expected: 1 (Person 1 距離較近: |2-4| = 2 < |7-4| = 3) + actual := findClosest(2, 7, 4) + expected := 1 + fmt.Printf("Test 1: Input(2, 7, 4) Expected: %d, Actual: %d, Result: %s\n", + expected, actual, getResult(actual == expected)) +} + +// 基本測試案例 - Example 2 +func testCase2() { + // Input: x = 2, y = 5, z = 6 + // Expected: 2 (Person 2 距離較近: |5-6| = 1 < |2-6| = 4) + actual := findClosest(2, 5, 6) + expected := 2 + fmt.Printf("Test 2: Input(2, 5, 6) Expected: %d, Actual: %d, Result: %s\n", + expected, actual, getResult(actual == expected)) +} + +// 基本測試案例 - Example 3 +func testCase3() { + // Input: x = 1, y = 5, z = 3 + // Expected: 0 (距離相等: |1-3| = 2 = |5-3| = 2) + actual := findClosest(1, 5, 3) + expected := 0 + fmt.Printf("Test 3: Input(1, 5, 3) Expected: %d, Actual: %d, Result: %s\n", + expected, actual, getResult(actual == expected)) +} + +func getResult(pass bool) string { + if pass { + return "PASS" + } + return "FAIL" +} \ No newline at end of file