Posts

Showing posts with the label Binary Search Tree

[LeetCode] 220. Contains Duplicate III

Given an array of integers, find out whether there are two distinct indices  i  and  j  in the array such that the  absolute  difference between  nums[i]  and  nums[j]  is at most  t  and the  absolute  difference between  i  and  j  is at most  k . Thought process: Iterate through the array. Use a binary search tree (TreeSet in Java) to keep track of the most recent k numbers. For each number, get the ceiling (smallest number that's greater than num) and floor (greatest number that's less than num) of it, and check if either's difference with num is at most t.  If the tree's size exceeds k, remove the least recent number. Note: there may be integer overflow when subtracting a negative number from a positive number. Solution 1: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 public class Solution { public boolean containsNearbyAlmostDuplicate ( int [] nums ,...