|
| 1 | +--- |
| 2 | +comments: true |
| 3 | +difficulty: Easy |
| 4 | +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3516.Find%20Closest%20Person/README_EN.md |
| 5 | +--- |
| 6 | + |
| 7 | +<!-- problem:start --> |
| 8 | + |
| 9 | +# [3516. Find Closest Person](https://leetcode.com/problems/find-closest-person) |
| 10 | + |
| 11 | +[中文文档](/solution/3500-3599/3516.Find%20Closest%20Person/README.md) |
| 12 | + |
| 13 | +## Description |
| 14 | + |
| 15 | +<!-- description:start --> |
| 16 | + |
| 17 | +<p data-end="116" data-start="0">You are given three integers <code data-end="33" data-start="30">x</code>, <code data-end="38" data-start="35">y</code>, and <code data-end="47" data-start="44">z</code>, representing the positions of three people on a number line:</p> |
| 18 | + |
| 19 | +<ul data-end="252" data-start="118"> |
| 20 | + <li data-end="154" data-start="118"><code data-end="123" data-start="120">x</code> is the position of Person 1.</li> |
| 21 | + <li data-end="191" data-start="155"><code data-end="160" data-start="157">y</code> is the position of Person 2.</li> |
| 22 | + <li data-end="252" data-start="192"><code data-end="197" data-start="194">z</code> is the position of Person 3, who does <strong>not</strong> move.</li> |
| 23 | +</ul> |
| 24 | + |
| 25 | +<p data-end="322" data-start="254">Both Person 1 and Person 2 move toward Person 3 at the <strong>same</strong> speed.</p> |
| 26 | + |
| 27 | +<p data-end="372" data-start="324">Determine which person reaches Person 3 <strong>first</strong>:</p> |
| 28 | + |
| 29 | +<ul data-end="505" data-start="374"> |
| 30 | + <li data-end="415" data-start="374">Return 1 if Person 1 arrives first.</li> |
| 31 | + <li data-end="457" data-start="416">Return 2 if Person 2 arrives first.</li> |
| 32 | + <li data-end="505" data-start="458">Return 0 if both arrive at the <strong>same</strong> time.</li> |
| 33 | +</ul> |
| 34 | + |
| 35 | +<p data-end="537" data-is-last-node="" data-is-only-node="" data-start="507">Return the result accordingly.</p> |
| 36 | + |
| 37 | +<p> </p> |
| 38 | +<p><strong class="example">Example 1:</strong></p> |
| 39 | + |
| 40 | +<div class="example-block"> |
| 41 | +<p><strong>Input:</strong> <span class="example-io">x = 2, y = 7, z = 4</span></p> |
| 42 | + |
| 43 | +<p><strong>Output:</strong> <span class="example-io">1</span></p> |
| 44 | + |
| 45 | +<p><strong>Explanation:</strong></p> |
| 46 | + |
| 47 | +<ul data-end="258" data-start="113"> |
| 48 | + <li data-end="193" data-start="113">Person 1 is at position 2 and can reach Person 3 (at position 4) in 2 steps.</li> |
| 49 | + <li data-end="258" data-start="194">Person 2 is at position 7 and can reach Person 3 in 3 steps.</li> |
| 50 | +</ul> |
| 51 | + |
| 52 | +<p data-end="317" data-is-last-node="" data-is-only-node="" data-start="260">Since Person 1 reaches Person 3 first, the output is 1.</p> |
| 53 | +</div> |
| 54 | + |
| 55 | +<p><strong class="example">Example 2:</strong></p> |
| 56 | + |
| 57 | +<div class="example-block"> |
| 58 | +<p><strong>Input:</strong> <span class="example-io">x = 2, y = 5, z = 6</span></p> |
| 59 | + |
| 60 | +<p><strong>Output:</strong> <span class="example-io">2</span></p> |
| 61 | + |
| 62 | +<p><strong>Explanation:</strong></p> |
| 63 | + |
| 64 | +<ul data-end="245" data-start="92"> |
| 65 | + <li data-end="174" data-start="92">Person 1 is at position 2 and can reach Person 3 (at position 6) in 4 steps.</li> |
| 66 | + <li data-end="245" data-start="175">Person 2 is at position 5 and can reach Person 3 in 1 step.</li> |
| 67 | +</ul> |
| 68 | + |
| 69 | +<p data-end="304" data-is-last-node="" data-is-only-node="" data-start="247">Since Person 2 reaches Person 3 first, the output is 2.</p> |
| 70 | +</div> |
| 71 | + |
| 72 | +<p><strong class="example">Example 3:</strong></p> |
| 73 | + |
| 74 | +<div class="example-block"> |
| 75 | +<p><strong>Input:</strong> <span class="example-io">x = 1, y = 5, z = 3</span></p> |
| 76 | + |
| 77 | +<p><strong>Output:</strong> <span class="example-io">0</span></p> |
| 78 | + |
| 79 | +<p><strong>Explanation:</strong></p> |
| 80 | + |
| 81 | +<ul data-end="245" data-start="92"> |
| 82 | + <li data-end="174" data-start="92">Person 1 is at position 1 and can reach Person 3 (at position 3) in 2 steps.</li> |
| 83 | + <li data-end="245" data-start="175">Person 2 is at position 5 and can reach Person 3 in 2 steps.</li> |
| 84 | +</ul> |
| 85 | + |
| 86 | +<p data-end="304" data-is-last-node="" data-is-only-node="" data-start="247">Since both Person 1 and Person 2 reach Person 3 at the same time, the output is 0.</p> |
| 87 | +</div> |
| 88 | + |
| 89 | +<p> </p> |
| 90 | +<p><strong>Constraints:</strong></p> |
| 91 | + |
| 92 | +<ul> |
| 93 | + <li><code>1 <= x, y, z <= 100</code></li> |
| 94 | +</ul> |
| 95 | + |
| 96 | +<!-- description:end --> |
| 97 | + |
| 98 | +## Solutions |
| 99 | + |
| 100 | +<!-- solution:start --> |
| 101 | + |
| 102 | +### Solution 1 |
| 103 | + |
| 104 | +<!-- tabs:start --> |
| 105 | + |
| 106 | +#### Python3 |
| 107 | + |
| 108 | +```python |
| 109 | + |
| 110 | +``` |
| 111 | + |
| 112 | +#### Java |
| 113 | + |
| 114 | +```java |
| 115 | + |
| 116 | +``` |
| 117 | + |
| 118 | +#### C++ |
| 119 | + |
| 120 | +```cpp |
| 121 | + |
| 122 | +``` |
| 123 | + |
| 124 | +#### Go |
| 125 | + |
| 126 | +```go |
| 127 | + |
| 128 | +``` |
| 129 | + |
| 130 | +<!-- tabs:end --> |
| 131 | + |
| 132 | +<!-- solution:end --> |
| 133 | + |
| 134 | +<!-- problem:end --> |
0 commit comments