Find local minimum in n x n matrix in O(n) time

前端 未结 5 1457
被撕碎了的回忆
被撕碎了的回忆 2020-12-02 13:16

So, this is not my home work question, but it is taken from an ungraded homework of the coursera course on algorithms and data structures (which is now complete).

Y

5条回答
  •  余生分开走
    2020-12-02 14:02

    I think this is actually really easy.

    Turn the problem into 3-D one to see why the algorithm works. Put the matrix on a table. Pretend there are pillars extending out of each cell and that the height of the pillar is directly proportional to its value. Put a ball on any pillar. Have the ball always fall onto the adjacent pillar that is the lowest altitude until it is at a local minimum.

提交回复
热议问题