HackerRank: Poisonous Plants ( Stack, DS ) (Find me on HackerRank @ Handle: BishalG ) Problem Summary: In this problem, we are given some value , say pesticide levels of each plants. A plant will dies if all plants to its left side have smaller such value than that of its own. We have to calculate the number of days after which no plants will die!! Solution Idea: For every position find out: "How many times it will be hitted from right side", say hits(i) maximum among all hits(i) is the desired answer!! To find this, keep track of values in stack and try to eliminate all values in current stack which are greater than p(i) from each points !! At certain point, if we find element which is to be counted as kth hit counter but that element itself require more than k hits to be stable then, we count that element as hit source of maximum value. In this way, we will not repetitive actions and one element is added and popped from stack at most one time which leads to the O(n) time ...
Comments
Post a Comment