Skip to content

Commit 01c3bcf

Browse files
committed
Add solution #1509
1 parent e31efcf commit 01c3bcf

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,337 LeetCode solutions in JavaScript
1+
# 1,338 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1153,6 +1153,7 @@
11531153
1505|[Minimum Possible Integer After at Most K Adjacent Swaps On Digits](./solutions/1505-minimum-possible-integer-after-at-most-k-adjacent-swaps-on-digits.js)|Hard|
11541154
1507|[Reformat Date](./solutions/1507-reformat-date.js)|Easy|
11551155
1508|[Range Sum of Sorted Subarray Sums](./solutions/1508-range-sum-of-sorted-subarray-sums.js)|Medium|
1156+
1509|[Minimum Difference Between Largest and Smallest Value in Three Moves](./solutions/1509-minimum-difference-between-largest-and-smallest-value-in-three-moves.js)|Medium|
11561157
1512|[Number of Good Pairs](./solutions/1512-number-of-good-pairs.js)|Easy|
11571158
1519|[Number of Nodes in the Sub-Tree With the Same Label](./solutions/1519-number-of-nodes-in-the-sub-tree-with-the-same-label.js)|Medium|
11581159
1524|[Number of Sub-arrays With Odd Sum](./solutions/1524-number-of-sub-arrays-with-odd-sum.js)|Medium|
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* 1509. Minimum Difference Between Largest and Smallest Value in Three Moves
3+
* https://leetcode.com/problems/minimum-difference-between-largest-and-smallest-value-in-three-moves/
4+
* Difficulty: Medium
5+
*
6+
* You are given an integer array nums.
7+
*
8+
* In one move, you can choose one element of nums and change it to any value.
9+
*
10+
* Return the minimum difference between the largest and smallest value of nums after performing at
11+
* most three moves.
12+
*/
13+
14+
/**
15+
* @param {number[]} nums
16+
* @return {number}
17+
*/
18+
var minDifference = function(nums) {
19+
if (nums.length <= 4) return 0;
20+
21+
nums.sort((a, b) => a - b);
22+
23+
const n = nums.length;
24+
let result = Infinity;
25+
26+
result = Math.min(result, nums[n - 1] - nums[3]);
27+
result = Math.min(result, nums[n - 2] - nums[2]);
28+
result = Math.min(result, nums[n - 3] - nums[1]);
29+
result = Math.min(result, nums[n - 4] - nums[0]);
30+
31+
return result;
32+
};

0 commit comments

Comments
 (0)