Binary Search

https://leetcode.com/discuss/general-discussion/786126/python-powerful-ultimate-binary-search-template-solved-many-problems

    public int search(int[] nums, int target) {
        if (nums == null || nums.length == 0) return -1;
        int left = 0;
        int right = nums.length - 1;
        while (left + 1 < right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] == target) return mid;
            if (nums[mid] < target) {
                left = mid;
            } else {
                right = mid;
            }
        }
        if (nums[left] == target) return left;
        if (nums[right] == target) return right;
        return -1;
    }

Find first "T" in the array, when meet the situation below, still move L pointer:

# F F F T T T

# [1, 2, 10, 11.., n]

when nums[mid] > nums[mid - 1] means it is on upslope, move L pointer. Otherwise move R pointer.

Find the first position and the last position separately. Treat differently as nums[mid] == target

Use right most digit as a pivot to determine if mid is on the lower side or the higher side, then determine the position of target to move the pointers.

When nums[mid] == nums[L] and nums[mid] == nums[R] means all three pointers are on same level. Compare target vs nums[R], check if nums[mid] is in between, or on the opposite side.

Use right most element as pivot to determine the direction of the search. If nums[mid] less and equal to pivot, then move R to mid. Otherwise move L to mid.

Define boundaries by expanding right pointer. To keep logarithmic time complexity, let's extend it twice as far: right = right * 2. Then binary search.

Another solution (if getNumber() API is provided):

2 ^ 10 = 4 ^ 5 = 16 ^ 2 * 4

T: O(logn) S: O(1)

For example, in [1,1,3,3,4,4,5,8,8], elements before 5 keep [even, odd] pattern, to make sure that even and odd number sequence. The elements after 5 break this sequence. In this case, we can determine the target is on the left or the right side of pivot.

nums[mid] - nums[left] is the number difference on array, mid - leftis the difference of indices. The difference diff between those two numbers can give you how close are we reaching out to k.

The k needs to decrement by diff when moving left pointer, as k is from the beginning.

The result is nums[left] + k. However the number may exceed right so the result is nums[left] + k + 1 in that situation.

dp is an array storing the smallest tail of all increasing subsequences with length i+1 in dp[i]. For example, say we have nums = [4,5,6,3], then all the available increasing subsequences are:

  • len = 1 : [4], [5], [6], [3] => tails[0] = 3

  • len = 2 : [4, 5], [5, 6] => tails[1] = 5

  • len = 3 : [4, 5, 6] => tails[2] = 6

We can easily prove that tails is a increasing array. Therefore it is possible to do a binary search in tails array to find the one needs update.

Each time we only do one of the two:

  • if x is larger than all tails, append it, increase the size by 1

  • if dp[i-1] < x <= dp[i], update dp[i]

T: O(nlogn)

Count the votes and derive the winner that that time. When querying, store the results in a TreeMap to get the results. Otherwise we can store by using an array and use binary search to find the answer.

Store the array to prefix sum and randomly get an integer between range 0 and max. Use binary search to get the index of that integer is presum.

We can use Binary Search to find a median of two sorted array. The index i in the first array has to follow this relation as i + j = (len1 + len2) / 2 (Actually it is i + j - 1= (len1 + len2 + 1) / 2). Therefore, use binary search to find the position which nums1[i] >= nums2[j - 1].

You will have i, j. Before return, determine if any of the indices are out of bound. Keep the boundary elements by choosing one value between i and j. If is odd total length, only pick one element as median, otherwise get the average of those two boundary elements.

3 binary searches

Binary search is a search algorithm usually used on a sorted sequence to quickly find an element with a given value. In this problem we will evaluate how binary search performs on data that isn't necessarily sorted. An element is said to be binary searchable if an element can be found provided the pivot is chosen everytime as the middle element - like in a regular binary search. We need to find total number of elements which are binary searchable.

Example 1:

[2, 1, 3, 4, 6, 5] - 3 is searchable, 2 is searchable, 1 not searchable, 6 is searchable, 4 is seachable, 5 is not searchable

Output: 4

Example 2:

Input: [1, 3, 2]

Output: 2

Explanation: 3 and 1 are searchable - you look for 3 - find it in the middle, look for 1 - you search the left half....search for 2, you look for it in the left half but didn't find.

Pretend the array is a binary search tree, DFS/BFS all pivot elements and keep track of lower and upper bound for the left and right children. The idea is to figure out how many middle elements in the given start to end range is properly positioned compared to previous middle element - so that they can be binary searchable in the given array.

3453. Separate Squares I

Last updated

Was this helpful?