diff --git a/solution/3500-3599/3516.Find Closest Person/README.md b/solution/3500-3599/3516.Find Closest Person/README.md index 151700b247766..cb8fba908dd59 100644 --- a/solution/3500-3599/3516.Find Closest Person/README.md +++ b/solution/3500-3599/3516.Find Closest Person/README.md @@ -101,32 +101,83 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3516.Fi -### 方法一 +### 方法一:数学 + +我们计算出第 $1$ 个人和第 $3$ 个人的距离 $a$,第 $2$ 个人和第 $3$ 个人的距离 $b$。 + +- 如果 $a = b$,说明两个人同时到达,返回 $0$; +- 如果 $a \lt b$,说明第 $1$ 个人会先到达,返回 $1$; +- 否则,说明第 $2$ 个人会先到达,返回 $2$。 + +时间复杂度 $O(1)$,空间复杂度 $O(1)$。 #### Python3 ```python - +class Solution: + def findClosest(self, x: int, y: int, z: int) -> int: + a = abs(x - z) + b = abs(y - z) + return 0 if a == b else (1 if a < b else 2) ``` #### Java ```java - +class Solution { + public int findClosest(int x, int y, int z) { + int a = Math.abs(x - z); + int b = Math.abs(y - z); + return a == b ? 0 : (a < b ? 1 : 2); + } +} ``` #### C++ ```cpp - +class Solution { +public: + int findClosest(int x, int y, int z) { + int a = abs(x - z); + int b = abs(y - z); + return a == b ? 0 : (a < b ? 1 : 2); + } +}; ``` #### Go ```go +func findClosest(x int, y int, z int) int { + a, b := abs(x-z), abs(y-z) + if a == b { + return 0 + } + if a < b { + return 1 + } + return 2 +} + +func abs(x int) int { + if x < 0 { + return -x + } + return x +} +``` + +#### TypeScript +```ts +function findClosest(x: number, y: number, z: number): number { + const a = Math.abs(x - z); + const b = Math.abs(y - z); + return a === b ? 0 : a < b ? 1 : 2; +} ``` diff --git a/solution/3500-3599/3516.Find Closest Person/README_EN.md b/solution/3500-3599/3516.Find Closest Person/README_EN.md index 5b82f05a2058f..f35e386b31fa2 100644 --- a/solution/3500-3599/3516.Find Closest Person/README_EN.md +++ b/solution/3500-3599/3516.Find Closest Person/README_EN.md @@ -99,32 +99,83 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3500-3599/3516.Fi -### Solution 1 +### Solution 1: Mathematics + +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. + +- If $a = b$, it means both people arrive at the same time, return $0$; +- If $a \lt b$, it means the 1st person will arrive first, return $1$; +- Otherwise, it means the 2nd person will arrive first, return $2$. + +The time complexity is $O(1)$, and the space complexity is $O(1)$. #### Python3 ```python - +class Solution: + def findClosest(self, x: int, y: int, z: int) -> int: + a = abs(x - z) + b = abs(y - z) + return 0 if a == b else (1 if a < b else 2) ``` #### Java ```java - +class Solution { + public int findClosest(int x, int y, int z) { + int a = Math.abs(x - z); + int b = Math.abs(y - z); + return a == b ? 0 : (a < b ? 1 : 2); + } +} ``` #### C++ ```cpp - +class Solution { +public: + int findClosest(int x, int y, int z) { + int a = abs(x - z); + int b = abs(y - z); + return a == b ? 0 : (a < b ? 1 : 2); + } +}; ``` #### Go ```go +func findClosest(x int, y int, z int) int { + a, b := abs(x-z), abs(y-z) + if a == b { + return 0 + } + if a < b { + return 1 + } + return 2 +} + +func abs(x int) int { + if x < 0 { + return -x + } + return x +} +``` + +#### TypeScript +```ts +function findClosest(x: number, y: number, z: number): number { + const a = Math.abs(x - z); + const b = Math.abs(y - z); + return a === b ? 0 : a < b ? 1 : 2; +} ``` diff --git a/solution/3500-3599/3516.Find Closest Person/Solution.cpp b/solution/3500-3599/3516.Find Closest Person/Solution.cpp new file mode 100644 index 0000000000000..7061bdb6f5ec2 --- /dev/null +++ b/solution/3500-3599/3516.Find Closest Person/Solution.cpp @@ -0,0 +1,8 @@ +class Solution { +public: + int findClosest(int x, int y, int z) { + int a = abs(x - z); + int b = abs(y - z); + return a == b ? 0 : (a < b ? 1 : 2); + } +}; \ No newline at end of file diff --git a/solution/3500-3599/3516.Find Closest Person/Solution.go b/solution/3500-3599/3516.Find Closest Person/Solution.go new file mode 100644 index 0000000000000..50eaa981c8edb --- /dev/null +++ b/solution/3500-3599/3516.Find Closest Person/Solution.go @@ -0,0 +1,17 @@ +func findClosest(x int, y int, z int) int { + a, b := abs(x-z), abs(y-z) + if a == b { + return 0 + } + if a < b { + return 1 + } + return 2 +} + +func abs(x int) int { + if x < 0 { + return -x + } + return x +} \ No newline at end of file diff --git a/solution/3500-3599/3516.Find Closest Person/Solution.java b/solution/3500-3599/3516.Find Closest Person/Solution.java new file mode 100644 index 0000000000000..6106c309aff54 --- /dev/null +++ b/solution/3500-3599/3516.Find Closest Person/Solution.java @@ -0,0 +1,7 @@ +class Solution { + public int findClosest(int x, int y, int z) { + int a = Math.abs(x - z); + int b = Math.abs(y - z); + return a == b ? 0 : (a < b ? 1 : 2); + } +} \ No newline at end of file diff --git a/solution/3500-3599/3516.Find Closest Person/Solution.py b/solution/3500-3599/3516.Find Closest Person/Solution.py new file mode 100644 index 0000000000000..30f44758158ba --- /dev/null +++ b/solution/3500-3599/3516.Find Closest Person/Solution.py @@ -0,0 +1,5 @@ +class Solution: + def findClosest(self, x: int, y: int, z: int) -> int: + a = abs(x - z) + b = abs(y - z) + return 0 if a == b else (1 if a < b else 2) diff --git a/solution/3500-3599/3516.Find Closest Person/Solution.ts b/solution/3500-3599/3516.Find Closest Person/Solution.ts new file mode 100644 index 0000000000000..13c4af4f0170f --- /dev/null +++ b/solution/3500-3599/3516.Find Closest Person/Solution.ts @@ -0,0 +1,5 @@ +function findClosest(x: number, y: number, z: number): number { + const a = Math.abs(x - z); + const b = Math.abs(y - z); + return a === b ? 0 : a < b ? 1 : 2; +}