-
Notifications
You must be signed in to change notification settings - Fork 14
/
Solution220.java
36 lines (27 loc) · 1012 Bytes
/
Solution220.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package leetcode.tree.redblacktree;
import java.util.TreeSet;
public class Solution220 {
public static void main(String[] args) {
// true
System.out.println(new Solution220().containsNearbyAlmostDuplicate(new int[]{1, 2, 3, 1}, 3, 0));
}
public boolean containsNearbyAlmostDuplicate(int[] nums, int indexDiff, int valueDiff) {
int left = 0, right = 0;
TreeSet<Integer> treeSet = new TreeSet<>();
while (right < nums.length) {
Integer floor = treeSet.floor(nums[right]);
Integer higher = treeSet.higher(nums[right]);
if (floor != null && nums[right] - floor <= valueDiff) {
return true;
}
if (higher != null && higher - nums[right] <= valueDiff) {
return true;
}
if (right - left >= indexDiff) {
treeSet.remove(nums[left++]);
}
treeSet.add(nums[right++]);
}
return false;
}
}