Skip to content

Commit 6b54d90

Browse files
refactor 220
1 parent bb6bf1d commit 6b54d90

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

src/main/java/com/fishercoder/solutions/_220.java

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
/**
66
* 220. Contains Duplicate III
7-
* <p>
7+
*
88
* Given an array of integers, find out whether there are two
99
* distinct indices i and j in the array such that the difference between nums[i] and nums[j] is at
1010
* most t and the difference between i and j is at most k.
@@ -22,15 +22,13 @@ public static class Solution1 {
2222
/**
2323
* TreeSet turns out to be a super handy data structure for this problem. It implements Set
2424
* interface and keeps elements in sorted order, plus it has two very handy APIs:
25-
* <p>
2625
* https://docs.oracle.com/javase/7/docs/api/java/util/TreeSet.html#ceiling(E): Returns the
2726
* least element in this set greater than or equal to the given element, or null if there is no
2827
* such element.
29-
* <p>
3028
* https://docs.oracle.com/javase/7/docs/api/java/util/TreeSet.html#floor(E): Returns the
3129
* greatest element in this set less than or equal to the given element, or null if there is no
3230
* such element.
33-
* <p>
31+
*
3432
* Idea: loop through this array, keep adding each element into the TreeSet, also keep an eye on
3533
* the size of the TreeSet, if it's greater than the required distance of k, then we remove the
3634
* left-most or oldest one from the set. For each element, we get the current floor and the
@@ -39,23 +37,22 @@ public static class Solution1 {
3937
*/
4038

4139
public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
42-
TreeSet<Integer> set = new TreeSet<>();
40+
/**case to Long to avoid Integer overflow.*/
41+
TreeSet<Long> set = new TreeSet<>();
4342
for (int i = 0; i < nums.length; ++i) {
44-
// Find the successor of current element
45-
Integer s = set.ceiling(nums[i]);
46-
if (s != null && s <= nums[i] + t) {
43+
Long s = set.ceiling((long) nums[i]);
44+
if (s != null && s - nums[i] <= t) {
4745
return true;
4846
}
4947

50-
// Find the predecessor of current element
51-
Integer g = set.floor(nums[i]);
52-
if (g != null && nums[i] <= g + t) {
48+
Long g = set.floor((long) nums[i]);
49+
if (g != null && nums[i] - g <= t) {
5350
return true;
5451
}
5552

56-
set.add(nums[i]);
53+
set.add((long) nums[i]);
5754
if (set.size() > k) {
58-
set.remove(nums[i - k]);
55+
set.remove((long) nums[i - k]);
5956
}
6057
}
6158
return false;

src/test/java/com/fishercoder/_220Test.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,10 @@ public void test6() {
5151
assertEquals(false, solution1.containsNearbyAlmostDuplicate(nums, 1, 2147483647));
5252
}
5353

54+
@Test
55+
public void test7() {
56+
nums = new int[]{-1, 2147483647};
57+
assertEquals(false, solution1.containsNearbyAlmostDuplicate(nums, 1, 2147483647));
58+
}
59+
5460
}

0 commit comments

Comments
 (0)