diff --git a/solution/2700-2799/2740.Find the Value of the Partition/README.md b/solution/2700-2799/2740.Find the Value of the Partition/README.md index d3d5ea2c61396..6983f98b48e59 100644 --- a/solution/2700-2799/2740.Find the Value of the Partition/README.md +++ b/solution/2700-2799/2740.Find the Value of the Partition/README.md @@ -135,6 +135,34 @@ func findValueOfPartition(nums []int) int { } ``` +#### TypeScript + +```ts +function findValueOfPartition(nums: number[]): number { + nums.sort((a, b) => a - b); + let ans = Infinity; + for (let i = 1; i < nums.length; ++i) { + ans = Math.min(ans, Math.abs(nums[i] - nums[i - 1])); + } + return ans; +} +``` + +#### Rust + +```rust +impl Solution { + pub fn find_value_of_partition(mut nums: Vec) -> i32 { + nums.sort(); + let mut ans = i32::MAX; + for i in 1..nums.len() { + ans = ans.min(nums[i] - nums[i - 1]); + } + ans + } +} +``` + diff --git a/solution/2700-2799/2740.Find the Value of the Partition/README_EN.md b/solution/2700-2799/2740.Find the Value of the Partition/README_EN.md index 141c414f9431c..d7e4ae811c8b5 100644 --- a/solution/2700-2799/2740.Find the Value of the Partition/README_EN.md +++ b/solution/2700-2799/2740.Find the Value of the Partition/README_EN.md @@ -135,6 +135,34 @@ func findValueOfPartition(nums []int) int { } ``` +#### TypeScript + +```ts +function findValueOfPartition(nums: number[]): number { + nums.sort((a, b) => a - b); + let ans = Infinity; + for (let i = 1; i < nums.length; ++i) { + ans = Math.min(ans, Math.abs(nums[i] - nums[i - 1])); + } + return ans; +} +``` + +#### Rust + +```rust +impl Solution { + pub fn find_value_of_partition(mut nums: Vec) -> i32 { + nums.sort(); + let mut ans = i32::MAX; + for i in 1..nums.len() { + ans = ans.min(nums[i] - nums[i - 1]); + } + ans + } +} +``` + diff --git a/solution/2700-2799/2740.Find the Value of the Partition/Solution.rs b/solution/2700-2799/2740.Find the Value of the Partition/Solution.rs new file mode 100644 index 0000000000000..ce1a3d37c83a0 --- /dev/null +++ b/solution/2700-2799/2740.Find the Value of the Partition/Solution.rs @@ -0,0 +1,10 @@ +impl Solution { + pub fn find_value_of_partition(mut nums: Vec) -> i32 { + nums.sort(); + let mut ans = i32::MAX; + for i in 1..nums.len() { + ans = ans.min(nums[i] - nums[i - 1]); + } + ans + } +} diff --git a/solution/2700-2799/2740.Find the Value of the Partition/Solution.ts b/solution/2700-2799/2740.Find the Value of the Partition/Solution.ts new file mode 100644 index 0000000000000..c52ecffffa6e6 --- /dev/null +++ b/solution/2700-2799/2740.Find the Value of the Partition/Solution.ts @@ -0,0 +1,8 @@ +function findValueOfPartition(nums: number[]): number { + nums.sort((a, b) => a - b); + let ans = Infinity; + for (let i = 1; i < nums.length; ++i) { + ans = Math.min(ans, Math.abs(nums[i] - nums[i - 1])); + } + return ans; +}