-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
72 lines (68 loc) · 2.12 KB
/
main.cpp
File metadata and controls
72 lines (68 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Source: https://leetcode.com/problems/find-closest-person
// Title: Find Closest Person
// Difficulty: Easy
// Author: Mu Yang <http://muyang.pro>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// You are given three integers `x`, `y`, and `z`, representing the positions of three people on a number line:
//
// * `x` is the position of Person 1.
// * `y` is the position of Person 2.
// * `z` is the position of Person 3, who does **not** move.
//
// Both Person 1 and Person 2 move toward Person 3 at the **same** speed.
//
// Determine which person reaches Person 3 **first**:
//
// * Return 1 if Person 1 arrives first.
// * Return 2 if Person 2 arrives first.
// * Return 0 if both arrive at the **same** time.
//
// Return the result accordingly.
//
// **Example 1:**
//
// ```
// Input: x = 2, y = 7, z = 4
// Output: 1
// Explanation:
// * Person 1 is at position 2 and can reach Person 3 (at position 4) in 2 steps.
// * Person 2 is at position 7 and can reach Person 3 in 3 steps.
// Since Person 1 reaches Person 3 first, the output is 1.
// ```
//
// **Example 2:**
//
// ```
// Input: x = 2, y = 5, z = 6
// Output: 2
// Explanation:
// * Person 1 is at position 2 and can reach Person 3 (at position 6) in 4 steps.
// * Person 2 is at position 5 and can reach Person 3 in 1 step.
// Since Person 2 reaches Person 3 first, the output is 2.
// ```
//
// **Example 3:**
//
// ```
// Input: x = 1, y = 5, z = 3
// Output: 0
// Explanation:
// * Person 1 is at position 1 and can reach Person 3 (at position 3) in 2 steps.
// * Person 2 is at position 5 and can reach Person 3 in 2 steps.
// Since both Person 1 and Person 2 reach Person 3 at the same time, the output is 0.
// ```
//
// **Constraints:**
//
// - `1 <= x, y, z <= 100`
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <cstdlib>
using namespace std;
class Solution {
public:
int findClosest(int x, int y, int z) {
auto dx = abs(x - z), dy = abs(y - z);
return (dx == dy) ? 0 : (dx < dy) ? 1 : 2;
}
};