Two Pointers (Part 2)
532 K-diff Pairs in an Array Two pointers, O(NlgN) time and O(1) space (a) sort the array (b) Use the first pointer (the rear pointer) to iterate each element in the array. Set the second pointer (the front pointer) to first pointer plus one. (c) while the second pointer is still valid (index within range) and the diff between two pointer is less than k. increase the front pointer. (d) check whether the diff is exactly equal to k, if so increase the counter. (e) while next of rear pointer is still valid and the pointee value is equal to the next pointee, increase rear pointer (f) return the counter in the end. 826 Most Profit Assigning Work (a) zip difficulty and profit as jobs. (b) sort jobs and sort 'worker'. (c) 2 pointers idea, for each worker, find his maximum profit he can make under his ability. Because we have sorted jobs and worker, we will go through two lists only once. It will be only O(M+N). Time Complexity O(NlogN + ...