πΉ Problem: 3516 Find Closest Person
Difficulty: #Easy
Tags: #Math, #Comparison
π Problem Summary
First of all, maybe the EASIEST PROBLEM IN LEETCODE HISTORY.
You are given three integers x
, y
, and z
. The task is to determine which of x
or y
is closer to z
.
- If both are equally close β return
0
. - If
x
is closer β return1
. - If
y
is closer β return2
.
π§ My Thought Process
Brute Force Idea:
I think you can solve this eeeasily with simple math.-
Optimized Strategy:
The brute force is optimal here β no loops, no data structures, just math.- Compute
|x - z|
and|y - z|
. - Compare the two distances.
- Decide the answer based on which difference is smaller.
- Compute
Algorithm Used:
Pure mathematical comparison using absolute difference.
βοΈ Code Implementation (Python)
class Solution:
def findClosest(self, x: int, y: int, z: int) -> int:
x_abs = abs(x - z)
y_abs = abs(y - z)
if x_abs == y_abs:
return 0
elif x_abs < y_abs:
return 1
else:
return 2
β±οΈ Time & Space Complexity
- Time: O(1) β Only a few arithmetic operations.
- Space: O(1) β No extra memory used.
π§© Key Takeaways
- β Learned how simple absolute difference comparisons solve closest-number problems.
- π‘ The trick is recognizing that absolute difference directly measures closeness.
- π This technique can be reused in problems like βfind closest element to a target in an arrayβ (but with loops/binary search).
π Reflection (Self-Check)
- [x] Could I solve this without help? (Yes, straightforward math)
- [x] Did I write code from scratch? (Yes)
- [x] Did I understand why it works? (Yes, absolute difference logic)
- [x] Will I be able to recall this in a week? (Yes, very simple pattern)
π Progress Tracker
Metric | Value |
---|---|
Day | 74 |
Total Problems Solved | 437 |
Confidence Today | π |
Leetcode Rating | 1530 |
Top comments (0)