Skip to content

3516. Find Closest Person 👍

  • Time: $O(1)$
  • Space: $O(1)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Solution {
 public:
  int findClosest(int x, int y, int z) {
    const int xz = abs(x - z);
    const int yz = abs(y - z);
    if (xz == yz)
      return 0;
    return xz < yz ? 1 : 2;
  }
};
1
2
3
4
5
6
7
8
9
class Solution {
  public int findClosest(int x, int y, int z) {
    final int xz = Math.abs(x - z);
    final int yz = Math.abs(y - z);
    if (xz == yz)
      return 0;
    return xz < yz ? 1 : 2;
  }
}
1
2
3
4
5
6
7
class Solution:
  def findClosest(self, x: int, y: int, z: int) -> int:
    xz = abs(x - z)
    yz = abs(y - z)
    if xz == yz:
      return 0
    return 1 if xz < yz else 2