Sunday, 15 June 2014

algorithm - what is the greedy approach of this? -



algorithm - what is the greedy approach of this? -

suppose have 2d boxes in container surrounded water. unfortunately there hole in left hand side wall of container. height of hole more height of boxes.length of boxes 1 m , height integer.all boxes set in 1 line.in every sec amount of water insert through container 1 square meter(take care our problem in 2d context).we want find how long takes till given box 1 meter under water. there greedy algorithm efficiently solve this?

i think algorithm can solve in o(n).

from i-th box first go end of line find first box higher i-th box.since differences between boxes' height @ to the lowest degree 1 meter water can't go farther box.then go i-th box origin of line.we save height of i-th box in max_height variable.each time find box higher max_height update max-height.so whenever find box shorter max_height add together (max_height - box's height) our result.at end have computed area of previous , next wells.so need compute distance between first next higher box , first previous higher box , add together our result. that's it.

algorithm greedy

No comments:

Post a Comment