Skip to content

Commit 73b5314

Browse files
add 1509
1 parent 5f413c1 commit 73b5314

File tree

3 files changed

+56
-1
lines changed
  • paginated_contents/algorithms/2nd_thousand
  • src

3 files changed

+56
-1
lines changed

Diff for: paginated_contents/algorithms/2nd_thousand/README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@
211211
| 1518 | [Water Bottles](https://leetcode.com/problems/water-bottles/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1518.java) | | Easy | Greedy |
212212
| 1514 | [Path with Maximum Probability](https://leetcode.com/problems/path-with-maximum-probability/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1514.java) | | Medium | Graph |
213213
| 1512 | [Number of Good Pairs](https://leetcode.com/problems/number-of-good-pairs/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1512.java) | | Easy | Array, HashTable, Math |
214+
| 1509 | [Minimum Difference Between Largest and Smallest Value in Three Moves](https://leetcode.com/problems/minimum-difference-between-largest-and-smallest-value-in-three-moves/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1509.java) | | Medium | Array, Sort, Greedy |
214215
| 1508 | [Range Sum of Sorted Subarray Sums](https://leetcode.com/problems/range-sum-of-sorted-subarray-sums/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1508.java) | | Medium | Array, Sort |
215216
| 1507 | [Reformat Date](https://leetcode.com/problems/reformat-date/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1507.java) | | Easy | String |
216217
| 1502 | [Can Make Arithmetic Progression From Sequence](https://leetcode.com/problems/can-make-arithmetic-progression-from-sequence/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1502.java) | | Easy | Array, Sort |
@@ -321,7 +322,7 @@
321322
| 1339 | [Maximum Product of Splitted Binary Tree](https://leetcode.com/problems/maximum-product-of-splitted-binary-tree/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1339.java) | | Medium | DFS, Tree |
322323
| 1338 | [Reduce Array Size to The Half](https://leetcode.com/problems/reduce-array-size-to-the-half/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1338.java) | | Medium ||
323324
| 1337 | [Remove Palindromic Subsequences](https://leetcode.com/problems/remove-palindromic-subsequences/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1337.java) | | Easy | String |
324-
| 1334 | [Find the City With the Smallest Number of Neighbors at a Threshold Distance](https://leetcode.com/problems/find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1334.java) | | Medium | Dijkstra's algorithm, Graph
325+
| 1334 | [Find the City With the Smallest Number of Neighbors at a Threshold Distance](https://leetcode.com/problems/find-the-city-with-the-smallest-number-of-neighbors-at-a-threshold-distance/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1334.java) | | Medium | Dijkstra's algorithm, Graph
325326
| 1333 | [Filter Restaurants by Vegan-Friendly, Price and Distance](https://leetcode.com/problems/filter-restaurants-by-vegan-friendly-price-and-distance/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1333.java) | | Medium ||
326327
| 1332 | [Remove Palindromic Subsequences](https://leetcode.com/problems/remove-palindromic-subsequences/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1332.java) | | Easy | String |
327328
| 1331 | [Rank Transform of an Array](https://leetcode.com/problems/rank-transform-of-an-array/) | [Solution](https://github.com/fishercoder1534/Leetcode/blob/master/src/main/java/com/fishercoder/solutions/secondthousand/_1331.java) | | Easy ||
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.fishercoder.solutions.secondthousand;
2+
3+
import java.util.Arrays;
4+
5+
public class _1509 {
6+
public static class Solution1 {
7+
public int minDifference(int[] nums) {
8+
if (nums.length <= 4) {
9+
return 0;
10+
}
11+
Arrays.sort(nums);
12+
int len = nums.length;
13+
//try to change three biggest nums to smallest
14+
int minDiff = Math.abs(nums[len - 4] - nums[0]);
15+
16+
//now try to change the three smallest to biggest
17+
minDiff = Math.min(minDiff, nums[len -1] - nums[3]);
18+
19+
//now try to change first two and last one
20+
minDiff = Math.min(minDiff, nums[len - 2] - nums[2]);
21+
22+
//now try to change first one and last two
23+
minDiff = Math.min(minDiff, nums[len - 3] - nums[1]);
24+
return minDiff;
25+
}
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.fishercoder.secondthousand;
2+
3+
import com.fishercoder.solutions.secondthousand._1509;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
9+
public class _1509Test {
10+
private static _1509.Solution1 solution1;
11+
12+
@BeforeEach
13+
public void setup() {
14+
solution1 = new _1509.Solution1();
15+
}
16+
17+
@Test
18+
public void test1() {
19+
assertEquals(2, solution1.minDifference(new int[]{6, 6, 0, 1, 1, 4, 6}));
20+
}
21+
22+
@Test
23+
public void test2() {
24+
assertEquals(1, solution1.minDifference(new int[]{82, 81, 95, 75, 20}));
25+
}
26+
27+
}

0 commit comments

Comments
 (0)