Skip to content

Commit 0a278ba

Browse files
chore: add LeetCode daily solution
1 parent 6f23d88 commit 0a278ba

File tree

5 files changed

+69
-0
lines changed

5 files changed

+69
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Find Closest Person (Easy)
2+
3+
**Problem ID:** 3516
4+
**Date:** 2025-09-04
5+
**Link:** https://leetcode.com/problems/find-closest-person/
6+
7+
## Approach
8+
9+
To solve the problem of determining which person reaches Person 3 first on a number line, we can break down the solution into a few clear steps:
10+
11+
### Main Idea:
12+
The core idea is to calculate the distance each person (Person 1 and Person 2) needs to travel to reach Person 3 and then compare these distances. The person with the shorter distance will reach Person 3 first.
13+
14+
### Steps:
15+
1. **Calculate Distances**:
16+
- Compute the absolute distance from Person 1 (position `x`) to Person 3 (position `z`): `distance1 = |x - z|`.
17+
- Compute the absolute distance from Person 2 (position `y`) to Person 3 (position `z`): `distance2 = |y - z|`.
18+
19+
2. **Compare Distances**:
20+
- If `distance1` is less than `distance2`, return `1` (Person 1 arrives first).
21+
- If `distance1` is greater than `distance2`, return `2` (Person 2 arrives first).
22+
- If both distances are equal, return `0` (both arrive at the same time).
23+
24+
### Data Structures:
25+
This problem does not require any complex data structures. Simple integer variables are sufficient to store the positions and distances.
26+
27+
### Complexity:
28+
- **Time Complexity**: O(1), as we perform a constant number of operations regardless of input size.
29+
- **Space Complexity**: O(1), since we only use a fixed amount of space to store the distances and results.
30+
31+
This approach is efficient and straightforward, leveraging basic arithmetic operations to arrive at the solution.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
public int findClosestPerson(int x, int y, int z) {
3+
int distance1 = Math.abs(x - z);
4+
int distance2 = Math.abs(y - z);
5+
6+
if (distance1 < distance2) {
7+
return 1;
8+
} else if (distance1 > distance2) {
9+
return 2;
10+
} else {
11+
return 0;
12+
}
13+
}
14+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function findClosestPerson(x, y, z) {
2+
const distance1 = Math.abs(x - z);
3+
const distance2 = Math.abs(y - z);
4+
5+
if (distance1 < distance2) {
6+
return 1;
7+
} else if (distance2 < distance1) {
8+
return 2;
9+
} else {
10+
return 0;
11+
}
12+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def findClosestPerson(self, x: int, y: int, z: int) -> int:
3+
dist1 = abs(x - z)
4+
dist2 = abs(y - z)
5+
6+
if dist1 < dist2:
7+
return 1
8+
elif dist2 < dist1:
9+
return 2
10+
else:
11+
return 0

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,4 @@ Each problem includes:
138138
- 2025-09-01 — [Maximum Average Pass Ratio](https://leetcode.com/problems/maximum-average-pass-ratio/) (Medium) → `Medium/2025-09-01-1792-Maximum-Average-Pass-Ratio`
139139
- 2025-09-02 — [Find the Number of Ways to Place People I](https://leetcode.com/problems/find-the-number-of-ways-to-place-people-i/) (Medium) → `Medium/2025-09-02-3025-Find-the-Number-of-Ways-to-Place-People-I`
140140
- 2025-09-03 — [Find the Number of Ways to Place People II](https://leetcode.com/problems/find-the-number-of-ways-to-place-people-ii/) (Hard) → `Hard/2025-09-03-3027-Find-the-Number-of-Ways-to-Place-People-II`
141+
- 2025-09-04 — [Find Closest Person](https://leetcode.com/problems/find-closest-person/) (Easy) → `Easy/2025-09-04-3516-Find-Closest-Person`

0 commit comments

Comments
 (0)