From 4efcf3f8df8a89adbef6018a9d2b9c1a2fee7ddf Mon Sep 17 00:00:00 2001 From: Jack Sangdahl Date: Wed, 3 Sep 2025 21:37:16 -0600 Subject: 3516. find closest person --- find_closest_person.c | 22 ++++++++++++++++++++++ find_closest_person.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 find_closest_person.c create mode 100644 find_closest_person.py diff --git a/find_closest_person.c b/find_closest_person.c new file mode 100644 index 0000000..109df33 --- /dev/null +++ b/find_closest_person.c @@ -0,0 +1,22 @@ +// 3516. Find Closest Person +#include "leetcode.h" + +/* + * you are given three integers represnting the positions of three people on a + * number line. 'x' is the position of person 1, 'y' is the position of person + * 2, and 'z' is the position of persion 3 who does not move. both person 1 and + * person 2 move toward person 3 at the same speed. determine which person will + * reach person 3 first. return 1 if person 1 will arrive first, return 2 if + * person 2 will arrive first, and 0 if both arrive at the same time. + */ + +int findClosest(int x, int y, int z) { + int a = abs(x - z), b = abs(y - z); + return (a != b) << (a > b); +} + +int main() { + printf("%d\n", findClosest(2, 7, 4)); // expect: 1 + printf("%d\n", findClosest(2, 5, 6)); // expect: 2 + printf("%d\n", findClosest(1, 5, 3)); // expect: 0 +} diff --git a/find_closest_person.py b/find_closest_person.py new file mode 100644 index 0000000..0e687d3 --- /dev/null +++ b/find_closest_person.py @@ -0,0 +1,29 @@ +# 3516. Find Closest Person + +""" +you are given three integers represnting the positions of three people on a +number line. 'x' is the position of person 1, 'y' is the position of person +2, and 'z' is the position of persion 3 who does not move. both person 1 and +person 2 move toward person 3 at the same speed. determine which person will +reach person 3 first. return 1 if person 1 will arrive first, return 2 if +person 2 will arrive first, and 0 if both arrive at the same time. +""" + + +class Solution(object): + def findClosest(self, x, y, z): + """ + :type x: int + :type y: int + :type z: int + :rtype: int + """ + a, b = abs(x - z), abs(y - z) + return (a != b) << (a > b) + + +if __name__ == "__main__": + obj = Solution() + print(obj.findClosest(2, 7, 4)) + print(obj.findClosest(2, 5, 6)) + print(obj.findClosest(1, 5, 3)) -- cgit v1.2.3