From 92976e93cf92d1d616eefb5ee417eba70566236a Mon Sep 17 00:00:00 2001 From: Libin YANG Date: Wed, 27 Aug 2025 07:13:52 +0800 Subject: [PATCH] feat: add solutions to lc problems: No.3021,3516 --- .../README.md | 52 +++++++++++++++++++ .../README_EN.md | 52 +++++++++++++++++++ .../Solution.js | 10 ++++ .../Solution.rs | 9 ++++ .../Solution2.js | 8 +++ .../Solution2.rs | 5 ++ .../3516.Find Closest Person/README.md | 46 ++++++++++++++++ .../3516.Find Closest Person/README_EN.md | 46 ++++++++++++++++ .../3516.Find Closest Person/Solution.cs | 7 +++ .../3516.Find Closest Person/Solution.js | 11 ++++ .../3516.Find Closest Person/Solution.rs | 13 +++++ 11 files changed, 259 insertions(+) create mode 100644 solution/3000-3099/3021.Alice and Bob Playing Flower Game/Solution.js create mode 100644 solution/3000-3099/3021.Alice and Bob Playing Flower Game/Solution.rs create mode 100644 solution/3000-3099/3021.Alice and Bob Playing Flower Game/Solution2.js create mode 100644 solution/3000-3099/3021.Alice and Bob Playing Flower Game/Solution2.rs create mode 100644 solution/3500-3599/3516.Find Closest Person/Solution.cs create mode 100644 solution/3500-3599/3516.Find Closest Person/Solution.js create mode 100644 solution/3500-3599/3516.Find Closest Person/Solution.rs diff --git a/solution/3000-3099/3021.Alice and Bob Playing Flower Game/README.md b/solution/3000-3099/3021.Alice and Bob Playing Flower Game/README.md index b88e8d06009ff..247e6685c1b00 100644 --- a/solution/3000-3099/3021.Alice and Bob Playing Flower Game/README.md +++ b/solution/3000-3099/3021.Alice and Bob Playing Flower Game/README.md @@ -151,6 +151,35 @@ function flowerGame(n: number, m: number): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn flower_game(n: i32, m: i32) -> i64 { + let a1 = ((n + 1) / 2) as i64; + let b1 = ((m + 1) / 2) as i64; + let a2 = (n / 2) as i64; + let b2 = (m / 2) as i64; + a1 * b2 + a2 * b1 + } +} +``` + +#### JavaScript + +```js +/** + * @param {number} n + * @param {number} m + * @return {number} + */ +var flowerGame = function (n, m) { + const [a1, b1] = [(n + 1) >> 1, (m + 1) >> 1]; + const [a2, b2] = [n >> 1, m >> 1]; + return a1 * b2 + a2 * b1; +}; +``` + @@ -220,6 +249,29 @@ function flowerGame(n: number, m: number): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn flower_game(n: i32, m: i32) -> i64 { + (n as i64 * m as i64) / 2 + } +} +``` + +#### JavaScript + +```js +/** + * @param {number} n + * @param {number} m + * @return {number} + */ +var flowerGame = function (n, m) { + return Number(((BigInt(n) * BigInt(m)) / 2n) | 0n); +}; +``` + diff --git a/solution/3000-3099/3021.Alice and Bob Playing Flower Game/README_EN.md b/solution/3000-3099/3021.Alice and Bob Playing Flower Game/README_EN.md index 739f74052ae2b..ffd84fe2e8cf0 100644 --- a/solution/3000-3099/3021.Alice and Bob Playing Flower Game/README_EN.md +++ b/solution/3000-3099/3021.Alice and Bob Playing Flower Game/README_EN.md @@ -149,6 +149,35 @@ function flowerGame(n: number, m: number): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn flower_game(n: i32, m: i32) -> i64 { + let a1 = ((n + 1) / 2) as i64; + let b1 = ((m + 1) / 2) as i64; + let a2 = (n / 2) as i64; + let b2 = (m / 2) as i64; + a1 * b2 + a2 * b1 + } +} +``` + +#### JavaScript + +```js +/** + * @param {number} n + * @param {number} m + * @return {number} + */ +var flowerGame = function (n, m) { + const [a1, b1] = [(n + 1) >> 1, (m + 1) >> 1]; + const [a2, b2] = [n >> 1, m >> 1]; + return a1 * b2 + a2 * b1; +}; +``` + @@ -218,6 +247,29 @@ function flowerGame(n: number, m: number): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn flower_game(n: i32, m: i32) -> i64 { + (n as i64 * m as i64) / 2 + } +} +``` + +#### JavaScript + +```js +/** + * @param {number} n + * @param {number} m + * @return {number} + */ +var flowerGame = function (n, m) { + return Number(((BigInt(n) * BigInt(m)) / 2n) | 0n); +}; +``` + diff --git a/solution/3000-3099/3021.Alice and Bob Playing Flower Game/Solution.js b/solution/3000-3099/3021.Alice and Bob Playing Flower Game/Solution.js new file mode 100644 index 0000000000000..8b7ddb806d710 --- /dev/null +++ b/solution/3000-3099/3021.Alice and Bob Playing Flower Game/Solution.js @@ -0,0 +1,10 @@ +/** + * @param {number} n + * @param {number} m + * @return {number} + */ +var flowerGame = function (n, m) { + const [a1, b1] = [(n + 1) >> 1, (m + 1) >> 1]; + const [a2, b2] = [n >> 1, m >> 1]; + return a1 * b2 + a2 * b1; +}; diff --git a/solution/3000-3099/3021.Alice and Bob Playing Flower Game/Solution.rs b/solution/3000-3099/3021.Alice and Bob Playing Flower Game/Solution.rs new file mode 100644 index 0000000000000..ed19cb0973692 --- /dev/null +++ b/solution/3000-3099/3021.Alice and Bob Playing Flower Game/Solution.rs @@ -0,0 +1,9 @@ +impl Solution { + pub fn flower_game(n: i32, m: i32) -> i64 { + let a1 = ((n + 1) / 2) as i64; + let b1 = ((m + 1) / 2) as i64; + let a2 = (n / 2) as i64; + let b2 = (m / 2) as i64; + a1 * b2 + a2 * b1 + } +} diff --git a/solution/3000-3099/3021.Alice and Bob Playing Flower Game/Solution2.js b/solution/3000-3099/3021.Alice and Bob Playing Flower Game/Solution2.js new file mode 100644 index 0000000000000..bb3211c9e13ac --- /dev/null +++ b/solution/3000-3099/3021.Alice and Bob Playing Flower Game/Solution2.js @@ -0,0 +1,8 @@ +/** + * @param {number} n + * @param {number} m + * @return {number} + */ +var flowerGame = function (n, m) { + return Number(((BigInt(n) * BigInt(m)) / 2n) | 0n); +}; diff --git a/solution/3000-3099/3021.Alice and Bob Playing Flower Game/Solution2.rs b/solution/3000-3099/3021.Alice and Bob Playing Flower Game/Solution2.rs new file mode 100644 index 0000000000000..62105d820618e --- /dev/null +++ b/solution/3000-3099/3021.Alice and Bob Playing Flower Game/Solution2.rs @@ -0,0 +1,5 @@ +impl Solution { + pub fn flower_game(n: i32, m: i32) -> i64 { + (n as i64 * m as i64) / 2 + } +} diff --git a/solution/3500-3599/3516.Find Closest Person/README.md b/solution/3500-3599/3516.Find Closest Person/README.md index 244c00baef762..ef1de588945ea 100644 --- a/solution/3500-3599/3516.Find Closest Person/README.md +++ b/solution/3500-3599/3516.Find Closest Person/README.md @@ -184,6 +184,52 @@ function findClosest(x: number, y: number, z: number): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn find_closest(x: i32, y: i32, z: i32) -> i32 { + let a = (x - z).abs(); + let b = (y - z).abs(); + if a == b { + 0 + } else if a < b { + 1 + } else { + 2 + } + } +} +``` + +#### JavaScript + +```js +/** + * @param {number} x + * @param {number} y + * @param {number} z + * @return {number} + */ +var findClosest = function (x, y, z) { + const a = Math.abs(x - z); + const b = Math.abs(y - z); + return a === b ? 0 : a < b ? 1 : 2; +}; +``` + +#### C# + +```cs +public 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); + } +} +``` + 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 1fa434ef10a84..3df1cffd94e9b 100644 --- a/solution/3500-3599/3516.Find Closest Person/README_EN.md +++ b/solution/3500-3599/3516.Find Closest Person/README_EN.md @@ -182,6 +182,52 @@ function findClosest(x: number, y: number, z: number): number { } ``` +#### Rust + +```rust +impl Solution { + pub fn find_closest(x: i32, y: i32, z: i32) -> i32 { + let a = (x - z).abs(); + let b = (y - z).abs(); + if a == b { + 0 + } else if a < b { + 1 + } else { + 2 + } + } +} +``` + +#### JavaScript + +```js +/** + * @param {number} x + * @param {number} y + * @param {number} z + * @return {number} + */ +var findClosest = function (x, y, z) { + const a = Math.abs(x - z); + const b = Math.abs(y - z); + return a === b ? 0 : a < b ? 1 : 2; +}; +``` + +#### C# + +```cs +public 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); + } +} +``` + diff --git a/solution/3500-3599/3516.Find Closest Person/Solution.cs b/solution/3500-3599/3516.Find Closest Person/Solution.cs new file mode 100644 index 0000000000000..3e2e7eefc5d9c --- /dev/null +++ b/solution/3500-3599/3516.Find Closest Person/Solution.cs @@ -0,0 +1,7 @@ +public 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); + } +} diff --git a/solution/3500-3599/3516.Find Closest Person/Solution.js b/solution/3500-3599/3516.Find Closest Person/Solution.js new file mode 100644 index 0000000000000..c09ef7d0cf7de --- /dev/null +++ b/solution/3500-3599/3516.Find Closest Person/Solution.js @@ -0,0 +1,11 @@ +/** + * @param {number} x + * @param {number} y + * @param {number} z + * @return {number} + */ +var findClosest = function (x, y, z) { + 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.rs b/solution/3500-3599/3516.Find Closest Person/Solution.rs new file mode 100644 index 0000000000000..81f70c58e9dec --- /dev/null +++ b/solution/3500-3599/3516.Find Closest Person/Solution.rs @@ -0,0 +1,13 @@ +impl Solution { + pub fn find_closest(x: i32, y: i32, z: i32) -> i32 { + let a = (x - z).abs(); + let b = (y - z).abs(); + if a == b { + 0 + } else if a < b { + 1 + } else { + 2 + } + } +}