diff --git a/solution/1500-1599/1509.Minimum Difference Between Largest and Smallest Value in Three Moves/README.md b/solution/1500-1599/1509.Minimum Difference Between Largest and Smallest Value in Three Moves/README.md index 3bc3c905bf23b..fe9c43ffc82a5 100644 --- a/solution/1500-1599/1509.Minimum Difference Between Largest and Smallest Value in Three Moves/README.md +++ b/solution/1500-1599/1509.Minimum Difference Between Largest and Smallest Value in Three Moves/README.md @@ -167,6 +167,42 @@ func minDifference(nums []int) int { } ``` +#### TypeScript + +```ts +function minDifference(nums: number[]): number { + if (nums.length < 5) { + return 0; + } + nums.sort((a, b) => a - b); + let ans = Number.POSITIVE_INFINITY; + for (let i = 0; i < 4; i++) { + ans = Math.min(ans, nums.at(i - 4)! - nums[i]); + } + return ans; +} +``` + +#### JavaScript + +```js +/** + * @param {number[]} nums + * @return {number} + */ +var minDifference = function (nums) { + if (nums.length < 5) { + return 0; + } + nums.sort((a, b) => a - b); + let ans = Number.POSITIVE_INFINITY; + for (let i = 0; i < 4; i++) { + ans = Math.min(ans, nums.at(i - 4) - nums[i]); + } + return ans; +}; +``` + diff --git a/solution/1500-1599/1509.Minimum Difference Between Largest and Smallest Value in Three Moves/README_EN.md b/solution/1500-1599/1509.Minimum Difference Between Largest and Smallest Value in Three Moves/README_EN.md index 6a926fc2b28dd..571c5fe87b8d0 100644 --- a/solution/1500-1599/1509.Minimum Difference Between Largest and Smallest Value in Three Moves/README_EN.md +++ b/solution/1500-1599/1509.Minimum Difference Between Largest and Smallest Value in Three Moves/README_EN.md @@ -156,6 +156,42 @@ func minDifference(nums []int) int { } ``` +#### TypeScript + +```ts +function minDifference(nums: number[]): number { + if (nums.length < 5) { + return 0; + } + nums.sort((a, b) => a - b); + let ans = Number.POSITIVE_INFINITY; + for (let i = 0; i < 4; i++) { + ans = Math.min(ans, nums.at(i - 4)! - nums[i]); + } + return ans; +} +``` + +#### JavaScript + +```js +/** + * @param {number[]} nums + * @return {number} + */ +var minDifference = function (nums) { + if (nums.length < 5) { + return 0; + } + nums.sort((a, b) => a - b); + let ans = Number.POSITIVE_INFINITY; + for (let i = 0; i < 4; i++) { + ans = Math.min(ans, nums.at(i - 4) - nums[i]); + } + return ans; +}; +``` + diff --git a/solution/1500-1599/1509.Minimum Difference Between Largest and Smallest Value in Three Moves/Solution.js b/solution/1500-1599/1509.Minimum Difference Between Largest and Smallest Value in Three Moves/Solution.js new file mode 100644 index 0000000000000..a8312a51e51a2 --- /dev/null +++ b/solution/1500-1599/1509.Minimum Difference Between Largest and Smallest Value in Three Moves/Solution.js @@ -0,0 +1,15 @@ +/** + * @param {number[]} nums + * @return {number} + */ +var minDifference = function (nums) { + if (nums.length < 5) { + return 0; + } + nums.sort((a, b) => a - b); + let ans = Number.POSITIVE_INFINITY; + for (let i = 0; i < 4; i++) { + ans = Math.min(ans, nums.at(i - 4) - nums[i]); + } + return ans; +}; diff --git a/solution/1500-1599/1509.Minimum Difference Between Largest and Smallest Value in Three Moves/Solution.ts b/solution/1500-1599/1509.Minimum Difference Between Largest and Smallest Value in Three Moves/Solution.ts new file mode 100644 index 0000000000000..dba54a3040c2b --- /dev/null +++ b/solution/1500-1599/1509.Minimum Difference Between Largest and Smallest Value in Three Moves/Solution.ts @@ -0,0 +1,11 @@ +function minDifference(nums: number[]): number { + if (nums.length < 5) { + return 0; + } + nums.sort((a, b) => a - b); + let ans = Number.POSITIVE_INFINITY; + for (let i = 0; i < 4; i++) { + ans = Math.min(ans, nums.at(i - 4)! - nums[i]); + } + return ans; +}