Skip to content

Commit 5844ef0

Browse files
committed
Add solution #3516
1 parent 39b4d36 commit 5844ef0

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2759,6 +2759,7 @@
27592759
3496|[Maximize Score After Pair Deletions](./solutions/3496-maximize-score-after-pair-deletions.js)|Medium|
27602760
3506|[Find Time Required to Eliminate Bacterial Strains](./solutions/3506-find-time-required-to-eliminate-bacterial-strains.js)|Hard|
27612761
3511|[Make a Positive Array](./solutions/3511-make-a-positive-array.js)|Medium|
2762+
3516|[Find Closest Person](./solutions/3516-find-closest-person.js)|Easy|
27622763
3520|[Minimum Threshold for Inversion Pairs Count](./solutions/3520-minimum-threshold-for-inversion-pairs-count.js)|Medium|
27632764
3535|[Unit Conversion II](./solutions/3535-unit-conversion-ii.js)|Medium|
27642765

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* 3516. Find Closest Person
3+
* https://leetcode.com/problems/find-closest-person/
4+
* Difficulty: Easy
5+
*
6+
* You are given three integers x, y, and z, representing the positions of three people
7+
* on a number line:
8+
* - x is the position of Person 1.
9+
* - y is the position of Person 2.
10+
* - z is the position of Person 3, who does not move.
11+
*
12+
* Both Person 1 and Person 2 move toward Person 3 at the same speed.
13+
*
14+
* Determine which person reaches Person 3 first:
15+
* - Return 1 if Person 1 arrives first.
16+
* - Return 2 if Person 2 arrives first.
17+
* - Return 0 if both arrive at the same time.
18+
*
19+
* Return the result accordingly.
20+
*/
21+
22+
/**
23+
* @param {number} x
24+
* @param {number} y
25+
* @param {number} z
26+
* @return {number}
27+
*/
28+
var findClosest = function(x, y, z) {
29+
const distancePerson1 = Math.abs(x - z);
30+
const distancePerson2 = Math.abs(y - z);
31+
32+
if (distancePerson1 < distancePerson2) {
33+
return 1;
34+
} else if (distancePerson2 < distancePerson1) {
35+
return 2;
36+
} else {
37+
return 0;
38+
}
39+
};

0 commit comments

Comments
 (0)