LeetCode374 guessing size - Array - dichotomy - simple

Subject description:

We're playing a guessing game. Rules of the game are as follows:

I select a number from 1 to n. I guess you need to choose which number.

Every time you guess wrong, I'll tell you this figure is big or small.

You call a pre-defined interfaces guess (int num), it will return three possible outcomes (-1, 1 or 0):

-1: I figure is relatively small

1: I figure relatively large

0: Congratulations! you guessed right!

Example:

Input: n = 10, pick = 6

Output: 6

Reference Links: https://leetcode-cn.com/problems/guess-number-higher-or-lower

Problem-solving ideas:

1, brute force

//暴力破解法
/* The guess API is defined in the parent class GuessGame.
   @param num, your guess
   @return -1 if my number is lower, 1 if my number is higher, otherwise return 0
      int guess(int num); */
//java
public class Solution extends GuessGame 
{
    public int guessNumber(int n) 
    {
        for (int i = 1; i < n; i++)
            if (guess(i) == 0)
                return i;
        return n;
    }
}

Algorithm complexity analysis:

Time complexity: O (n)

Space complexity: O (1) 

2, a binary search

//Java
/* The guess API is defined in the parent class GuessGame.
   @param num, your guess
   @return -1 if my number is lower, 1 if my number is higher, otherwise return 0
      int guess(int num); */

public class Solution extends GuessGame {
    public int guessNumber(int n) 
    {
     
        if (n == 0) 
        {
            return -1;
        }
        int left = 1;
        int right =n;
        //排除上述特殊情况后,依据题目可以确定目标值一定在在左右边界之中
        while (left < right) 
        {
            int mid = left + (right - left) / 2;
            if (guess(mid) ==1) //依据题目排除中位数(此判断中位数小于目标值,而题目要找的是大于或等于目标值的第一个元素) //它说我的数字比较大,也即是取的中位数比较小,小于目标值
            {
                // nums[mid] 的值可以舍弃
                left = mid + 1;
            } 
            else //中位数大于或等于目标值
            {
                // nums[mid] 不能舍弃
                right = mid;
            }
        }
        //循环结束只剩下最后一个值
        return right;
    }   
}

Algorithm complexity:

Time complexity: O (logn)

Space complexity: O (1)

3. Find the rule of thirds

Select two division point, each divided into three parts, two boundary value


/* The guess API is defined in the parent class GuessGame.
   @param num, your guess
   @return -1 if my number is lower, 1 if my number is higher, otherwise return 0
      int guess(int num); */
public class Solution extends GuessGame 
{
    public int guessNumber(int n) 
    {
        int low = 1;//确定边界
        int high = n;
        while (low <= high) 
        {
            int mid1 = low + (high - low) / 3;
            int mid2 = high - (high - low) / 3;
            int res1 = guess(mid1);
            int res2 = guess(mid2);
            if (res1 == 0)
                return mid1;
            if (res2 == 0)
                return mid2;
            else if (res1 < 0)//中间值比目标大
                high = mid1 - 1;
            else if (res2 > 0)//中间值比目标小
                low = mid2 + 1;
            else //res1>0左边区域中间值比目标小   res2<0右边区域中间值比目标大
            {
                low = mid1 + 1;
                high = mid2 - 1;
            }
        }
        return -1;
    }
}

Algorithm complexity analysis:

Time complexity: O (log3 N)

Space complexity: O (1)

Published 136 original articles · won praise 112 · views 90000 +

Guess you like

Origin blog.csdn.net/heda3/article/details/104072751