Find offer double-digit figures to prove safety group

1. Topic

Description Title
(same as the length of each one-dimensional array) in a two-dimensional array, each row from left to right in order of ascending sort, to sort each column from top to bottom in increasing order. A complete function, enter such a two-dimensional array and an integer, it is determined whether the array contains the integer.

Source: prove safety offer
links: https://www.nowcoder.com/practice/abc3fe2ce8e146608e868a70efebf62e?tpId=13&tqId=11154&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking

2. My problem solution

Traversal search is to get the right answer, complexity O(length*width), but apparently not the optimal solution.
The key is how to utilize each row from left to right increments, from top to bottom in increasing conditions of each column. At first glance (from top left), the right, the two directions are increased, so the next step can not be selected. However, if the upper-right corner of view, can be found in one direction is increased, a direction is reduced, the choice is determined in such a case, only one selection, complexity O(length+width).
For example, it will clear.
(1) in Example 1-8. If you start from the top left, the first step will be faced with problems, right and which way should I go next? The first step either to the right or down, you can get the optimal path of a 1-8. In other words, the current value, the right direction, there is no direction between the value of the relationship enough to let me determine which direction to go, both directions lines also means uncertainty, this approach is not feasible .
Here Insert Picture Description
(2) in Example 4-9. If start from the upper right, the left direction will find a value less than the current value, the value larger than the current direction, and the contrast between the target current value, the current value is larger on the left, a small current value on the downward, so you can get a uniquely determined and the optimal path. Of course, this path is optimal, but not unique, it's just the only other methods can still get another optimal path under the current method.
Here Insert Picture Description
(3) why not go back? At first glance the method seems to be some truth, but how to ensure that no backtrack to get to the destination it?
This is because the array is incremented from left to right, top to bottom of the incremental nature of the guarantee.
Contradiction may be used, assuming the presence of the results obtained by the method of the present backtrack a path ABCD, as an aid to point F, as shown below:
Here Insert Picture Description
then the nature of the method can be obtained at the point F auxiliary point: F>D, so it back to the point F left to go, do not go down;
increases from the condition data to this question from left to right, from top to bottom increments can be obtained F>D, the relationship between F and D appeared contradictory.
Thus, the present method does not have such paths obtained in going back.

3. someone else's problem solution

3.1 Cross Segmentation

According to the center point of the original rectangle into four rectangular, upper left, lower right are mutually exclusive, the data amount can be reduced 1/4, the other three rectangular recursive search, only until a rectangle element.
Pruning strategy can be applied to reduce the number of recursion. ComplexityO(logn)

class Solution {
public:
    bool Find(int target, vector<vector<int> > array) {
        if(array.size()==0 || array[0].size()==0)return false;
        return ten_seg(array,target,0,array.size()-1,0,array[0].size()-1);
    }
    bool ten_seg(vector<vector<int> > &data,int target,int top,int bottom,int left,int right){
    	//递归结束
        if(top==bottom && left == right){
            if(data[top][left] == target)return true;
            return false;
        }
        //剪枝
        if(target>data[bottom][right] || target< data[top][left])return false;
        //分割
        int i=(top+bottom)/2,j=(left+right)/2;
        if(target==data[i][j])return true;
        //右下
        if((i<bottom && j< right)&&target>data[i][j] && ten_seg(data,target,i+1,bottom,j+1,right))return true;
        //左上
        else if((top<i || left<j) &&target<data[i][j] && ten_seg(data,target,top,i,left,j)) return true;
        //左下
        if(i<bottom && ten_seg(data,target,i+1,bottom,left,j))return true;
        //右上
        if(j<right && ten_seg(data,target,top,i,j+1,right))return true;
        return false;
    }
};

In the above data, for example, 10 to find, the recursive sequence and output as follows:

# top bottom left right
0,3,0,3
2,3,2,3
2,3,0,1
3,3,1,1
3,3,0,0
2,2,1,1
1

4. Summary and Reflection

(1) find the "determined action", that is the next step to satisfy a certain condition and then determine what operation, if the operation more uncertainty there is the next step, the computer can not solve the problem.
(2) the cross segmentation method, in particular, the main processing boundary, to do do not leak; In this problem, the sub-intermediate point to the upper left portion, the lower right to find the number of ranks to add a partial, top right portion of plus a number, the number of rows plus a lower left portion.

Published 32 original articles · won praise 0 · Views 429

Guess you like

Origin blog.csdn.net/weixin_43951240/article/details/103906051