Files
coding-practice/problems/3516-find-closest-person
2025-09-04 10:10:53 +08:00
..
2025-09-04 10:10:26 +08:00
2025-09-04 10:10:53 +08:00
2025-09-04 10:09:45 +08:00

[3516] Find Closest Person

題目資訊

  • 難度: Easy
  • 標籤: Math
  • 題目連結: LeetCode
  • 練習日期: 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

遇到的困難

改善方向

相關題目


總結: 今天這題考弱智?