-
Notifications
You must be signed in to change notification settings - Fork 0
/
Contains Duplicate III.java
47 lines (39 loc) · 1.79 KB
/
Contains Duplicate III.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
37
38
39
40
41
42
43
44
45
46
47
/*
Given an array of integers, find out whether there are two distinct indices i and j in the array such that the difference between nums[i]
and nums[j] is at most t and the difference between i and j is at most k.
Link: https://leetcode.com/problems/contains-duplicate-iii/
Example: None
Solution: Sometimes, we can divided problems into large size and small size, then find best solutions.
Source: https://leetcode.com/discuss/78081/time-complexity-solution-using-hashmap-boost-speed-when-large
*/
public class Solution {
public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
if (t < 10) {
//when t is small, the following code runs in O(nt) time, this
//will be much faster than the naive solution when k is large.
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i = 0; i < nums.length; i++) {
for (int j = -t; j <= t; j++) {
int numToFind = nums[i] + j;
Integer lastIndex = map.get(numToFind);
if (lastIndex != null && lastIndex.intValue() + k >= i) {
return true;
}
}
map.put(nums[i], i);
}
return false;
} else {
//the naive solution: iteratively check all indices with difference not larger than k
//Time complexity is O(nk), OK when k is small.
for (int i = 0; i < nums.length - k; i++) {
for (int j = i + 1; j <= i + k; j++) {
if (nums[i] - nums[j] >= -t && nums[i] - nums[j] <= t) {
return true;
}
}
}
return false;
}
}
}