Puge algorithm dichotomy

Posted by kindoman on Wed, 22 Dec 2021 06:25:42 +0100

Dichotomy?

Let's look at a very interesting passage first

One day Xiao Ming went to the library to borrow N books. When he got out of the library, the alarm sounded, so the security guard stopped Xiao Ming to check which book was not registered for lending. Xiao Ming is going to pass each book under the alarm to find out the book that triggered the alarm, but the security guard looks disdainful: can't you even find it? So the security guard divided the books into two piles, let the first pile pass the alarm, and the alarm sounded; So he divided the pile of books into two piles... Finally, after checking the log N times, the security guard successfully found the book that caused the alarm, showing a proud and mocking smile. So Xiao Ming left with the rest of the books on his back. Since then, the library lost N - 1 books.

I believe you have been exposed to many topics about dichotomy since primary school. Bloggers will not talk about some ideas about dichotomy here

So today's blogger will discuss it from the perspective of programming algorithm. If you encounter the problem of dichotomy, you should answer it like this.

Examples

First of all, we should know that there is a condition for using dichotomy, that is, it must be sorted in order, which we should pay attention to.

 public static void main(String[] args) {
        int[] nums = {-1, 0, 3, 5, 9, 12};
        int target = 9;
        int n = 9;
        //Call the following method
        
        //Array lookup
        int i = search(nums,target);
        System.out.println(i+1);
        
        //Wrong version number
       int j = search2(n);
       System.out.println("The wrong version number is:"+j);
       
        //Output insertion position
        int k = search3(nums, target);
        System.out.println("The insertion position is"+(k+1));
    }

Array lookup

Given an array and a target value, you need to find the position of the target value in the array and print it. If not, output - 1

    private static int search1(int[] nums, int target) {
//        Defines the left and right pointers to the array
        int left = 0, right = nums.length - 1;
//        Judgment condition: when the left pointer is greater than the right pointer, we end the loop by default
        while (left < right) {
//          Define a number. The position selected by receiving dichotomy = left pointer position + half of the remaining length
            int mid = left + (right - left) / 2;
//           Judgment conditions
            if (nums[mid] > target) {//If the selected number is greater than the target value, the target value is on its left
//                At this time, you need to adjust the right pointer
                right = mid - 1;//Note that the array subscript is out of bounds
            } else if (nums[mid] < target) {//vice versa
                left = mid + 1;
            } else {
                return mid;
            }
        }
        return -2;
    }

Wrong version number

You are a product manager and are currently leading a team to develop new products. Suppose you have n versions [1, 2,..., n], you want to find the first wrong version that causes errors in all subsequent versions.

You can call the bool isBadVersion(version) interface to determine whether the version number is wrong in the unit test. Implement a function to find the first wrong version. You should minimize the number of calls to the API.

    private static int search2(int n) {
//        In this code, we only need to define a left pointer
            int left = 0;
//            The judgment condition is still when the left boundary is greater than the right boundary
            while (left < n) {
               int mid = left + (n - left) / 2;
//               In this problem, the given condition is to judge whether the version is the wrong version
                if (isBadVersion(mid)) {
//                    So we can assign this value directly to n
                    n = mid;
                } else {
                    left = mid + 1;
                }
            }
            return n;
        }

Insertion position

This problem is somewhat different from the position search in the array position above. If the value of this position does not exist, the index of this position is returned.

  private static int search3(int[] nums, int target) {
        int left = 0, right = nums.length - 1;
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] == target) {
                return mid;
            } else if (nums[mid] > target) {
                right = mid - 1;
            }else{
                left = mid + 1;
            }
        }
        //The last thing to return is their location
        return left;
    }
   

Well, let's give these three examples in detail. That's all for the introduction of dichotomy. Subsequent bloggers will give detailed explanations on algorithm topics. If you like, leave three links. I'll see you in the next blog.

Topics: Algorithm data structure leetcode