@@ -99,32 +99,83 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3516.Fi
9999
100100<!-- solution:start -->
101101
102- ### Solution 1
102+ ### Solution 1: Mathematics
103+
104+ We calculate the distance $a$ between the 1st person and the 3rd person, and the distance $b$ between the 2nd person and the 3rd person.
105+
106+ - If $a = b$, it means both people arrive at the same time, return $0$;
107+ - If $a \lt b$, it means the 1st person will arrive first, return $1$;
108+ - Otherwise, it means the 2nd person will arrive first, return $2$.
109+
110+ The time complexity is $O(1)$, and the space complexity is $O(1)$.
103111
104112<!-- tabs:start -->
105113
106114#### Python3
107115
108116``` python
109-
117+ class Solution :
118+ def findClosest (self , x : int , y : int , z : int ) -> int :
119+ a = abs (x - z)
120+ b = abs (y - z)
121+ return 0 if a == b else (1 if a < b else 2 )
110122```
111123
112124#### Java
113125
114126``` java
115-
127+ class Solution {
128+ public int findClosest (int x , int y , int z ) {
129+ int a = Math . abs(x - z);
130+ int b = Math . abs(y - z);
131+ return a == b ? 0 : (a < b ? 1 : 2 );
132+ }
133+ }
116134```
117135
118136#### C++
119137
120138``` cpp
121-
139+ class Solution {
140+ public:
141+ int findClosest(int x, int y, int z) {
142+ int a = abs(x - z);
143+ int b = abs(y - z);
144+ return a == b ? 0 : (a < b ? 1 : 2);
145+ }
146+ };
122147```
123148
124149#### Go
125150
126151```go
152+ func findClosest(x int, y int, z int) int {
153+ a, b := abs(x-z), abs(y-z)
154+ if a == b {
155+ return 0
156+ }
157+ if a < b {
158+ return 1
159+ }
160+ return 2
161+ }
162+
163+ func abs(x int) int {
164+ if x < 0 {
165+ return -x
166+ }
167+ return x
168+ }
169+ ```
170+
171+ #### TypeScript
127172
173+ ``` ts
174+ function findClosest(x : number , y : number , z : number ): number {
175+ const a = Math .abs (x - z );
176+ const b = Math .abs (y - z );
177+ return a === b ? 0 : a < b ? 1 : 2 ;
178+ }
128179```
129180
130181<!-- tabs: end -->
0 commit comments