Nine chapters algorithm surface question 30 shortest distance and

Source: Internet
Author: User

Nine Chapters count judges Net-original websitehttp://www.jiuzhang.com/problem/30/


Topics

First Order: In a n*m matrix, there are k points, the distance from the K point in the Matrix and the nearest point.

Advanced: What if this point is not coincident with the given K-point?

Note: The distance here is based on the Manhattan distance--|x0-x1| + |y0-y1|

Answer

First Order: Because of the Manhattan distance, the x and Y coordinates can be considered separately. By sorting the x-coordinate of the K-point, you know that the required x-coordinate must be at the x-coordinate of the K-point, scan it over and count the x-coordinate distance of each point, and find the x-coordinate that makes the distance and the smallest. This step only requires O (k) time complexity, not O (k^2), how to optimize here not much to repeat. Do the same for y coordinates to get the answer. Time complexity O (klogk), sorting complexity.

Advanced: The first order of the algorithm to get an optimal position, if this position and K points coincident, then start the search from this position, the point around the points and the corresponding distance into a heap, each time from the heap to remove the minimum distance points, and then the point around the points into the heap, The point is not coincident with the given K point until it is removed. The time complexity is KLOGK because a point that is not coincident with the given K points can be found by removing k+1 points from the heap at most. The heap is LOGK per operation.


Interviewer Angle

The optimal algorithm is more difficult to think about. So if the company does not require high, answer the O (Nm) method can be. O (nm) is due to the assumption that we know the distance (x, y) at this location and for S, so when (x, y) is moved to (X+1,y) and (x,y+1), we can update S at O (1) time. The method is to preprocess the number of points in the K-point above/below each row, and the number of points in the left/right of each column that have a K-point. The above solution is based on nm>>klogk, and if K is larger, then the O (Nm) method is better. You need to answer the question in the case of a given parameter under different circumstances using the algorithm.



Nine chapters algorithm surface question 30 shortest distance and

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.