Skip to content

Commit 4f387d6

Browse files
committed
feat: add ts solution to lc problem: No.1509
1 parent f2a4791 commit 4f387d6

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

solution/1500-1599/1509.Minimum Difference Between Largest and Smallest Value in Three Moves/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,22 @@ func minDifference(nums []int) int {
167167
}
168168
```
169169

170+
#### TypeScript
171+
172+
```ts
173+
function minDifference(nums: number[]): number {
174+
if (nums.length < 5) return 0;
175+
nums.sort((a, b) => a - b);
176+
let res = Number.POSITIVE_INFINITY;
177+
178+
for (let i = 0; i < 4; i++) {
179+
res = Math.min(res, nums.at(i - 4)! - nums[i]);
180+
}
181+
182+
return res;
183+
}
184+
```
185+
170186
<!-- tabs:end -->
171187

172188
<!-- solution:end -->

solution/1500-1599/1509.Minimum Difference Between Largest and Smallest Value in Three Moves/README_EN.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,22 @@ func minDifference(nums []int) int {
156156
}
157157
```
158158

159+
#### TypeScript
160+
161+
```ts
162+
function minDifference(nums: number[]): number {
163+
if (nums.length < 5) return 0;
164+
nums.sort((a, b) => a - b);
165+
let res = Number.POSITIVE_INFINITY;
166+
167+
for (let i = 0; i < 4; i++) {
168+
res = Math.min(res, nums.at(i - 4)! - nums[i]);
169+
}
170+
171+
return res;
172+
}
173+
```
174+
159175
<!-- tabs:end -->
160176

161177
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function minDifference(nums: number[]): number {
2+
if (nums.length < 5) return 0;
3+
nums.sort((a, b) => a - b);
4+
let ans = Number.POSITIVE_INFINITY;
5+
6+
for (let i = 0; i < 4; i++) {
7+
ans = Math.min(ans, nums.at(i - 4)! - nums[i]);
8+
}
9+
10+
return ans;
11+
}

0 commit comments

Comments
 (0)