{"id":27332,"date":"2018-01-20T21:16:15","date_gmt":"2018-01-20T15:46:15","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=27332"},"modified":"2018-01-20T21:16:15","modified_gmt":"2018-01-20T15:46:15","slug":"c-algorithm-stock-span-problem-2","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/c-algorithm-stock-span-problem-2\/","title":{"rendered":"Cpp Algorithm &#8211; The Stock Span Problem"},"content":{"rendered":"<p>The stock span problem is a financial problem where we have a series of n daily price quotes for a stock and we need to calculate span of stock\u2019s price for all n days. <span id=\"more-29347\"><\/span><br \/>\nThe span Si of the stock\u2019s price on a given day i is defined as the maximum number of consecutive days just before the given day, for which the price of the stock on the current day is less than or equal to its price on the given day.<br \/>\nFor example, if an array of 7 days prices is given as {100, 80, 60, 70, 60, 75, 85}, then the span values for corresponding 7 days are {1, 1, 1, 2, 1, 4, 6}<\/p>\n<p><strong>A Simple but inefficient method<\/strong><br \/>\nTraverse the input price array. For every element being visited, traverse elements on left of it and increment the span value of it while elements on the left side are smaller.<\/p>\n<p><strong>A Linear Time Complexity Method<\/strong><br \/>\nWe see that S[i] on day i can be easily computed if we know the closest day preceding i, such that the price is greater than on that day than the price on day i. If such a day exists, let\u2019s call it h(i), otherwise, we define h(i) = -1.<br \/>\nThe span is now computed as S[i] = i \u2013 h(i). See the following diagram.<\/p>\n[ad type=\u201dbanner\u201d]\n<p>To implement this logic, we use a stack as an abstract data type to store the days i, h(i), h(h(i)) and so on. When we go from day i-1 to i, we pop the days when the price of the stock was less than or equal to price[i] and then push the value of day i back into the stack.<\/p>\n<p><strong>C++ Programming:<\/strong><\/p>\n[pastacode lang=\u201dcpp\u201d manual=\u201d%2F%2F%20a%20linear%20time%20solution%20for%20stock%20span%20problem%0A%23include%20%3Ciostream%3E%0A%23include%20%3Cstack%3E%0Ausing%20namespace%20std%3B%0A%20%0A%2F%2F%20A%20stack%20based%20efficient%20method%20to%20calculate%20stock%20span%20values%0Avoid%20calculateSpan(int%20price%5B%5D%2C%20int%20n%2C%20int%20S%5B%5D)%0A%7B%0A%20%20%20%2F%2F%20Create%20a%20stack%20and%20push%20index%20of%20first%20element%20to%20it%0A%20%20%20stack%3Cint%3E%20st%3B%0A%20%20%20st.push(0)%3B%0A%20%0A%20%20%20%2F%2F%20Span%20value%20of%20first%20element%20is%20always%201%0A%20%20%20S%5B0%5D%20%3D%201%3B%0A%20%0A%20%20%20%2F%2F%20Calculate%20span%20values%20for%20rest%20of%20the%20elements%0A%20%20%20for%20(int%20i%20%3D%201%3B%20i%20%3C%20n%3B%20i%2B%2B)%0A%20%20%20%7B%0A%20%20%20%20%20%20%2F%2F%20Pop%20elements%20from%20stack%20while%20stack%20is%20not%20empty%20and%20top%20of%0A%20%20%20%20%20%20%2F%2F%20stack%20is%20smaller%20than%20price%5Bi%5D%0A%20%20%20%20%20%20while%20(!st.empty()%20%26%26%20price%5Bst.top()%5D%20%3C%3D%20price%5Bi%5D)%0A%20%20%20%20%20%20%20%20%20st.pop()%3B%0A%20%0A%20%20%20%20%20%20%2F%2F%20If%20stack%20becomes%20empty%2C%20then%20price%5Bi%5D%20is%20greater%20than%20all%20elements%0A%20%20%20%20%20%20%2F%2F%20on%20left%20of%20it%2C%20i.e.%2C%20price%5B0%5D%2C%20price%5B1%5D%2C..price%5Bi-1%5D.%20%20Else%20price%5Bi%5D%0A%20%20%20%20%20%20%2F%2F%20is%20greater%20than%20elements%20after%20top%20of%20stack%0A%20%20%20%20%20%20S%5Bi%5D%20%3D%20(st.empty())%3F%20(i%20%2B%201)%20%3A%20(i%20-%20st.top())%3B%0A%20%0A%20%20%20%20%20%20%2F%2F%20Push%20this%20element%20to%20stack%0A%20%20%20%20%20%20st.push(i)%3B%0A%20%20%20%7D%0A%7D%0A%20%0A%2F%2F%20A%20utility%20function%20to%20print%20elements%20of%20array%0Avoid%20printArray(int%20arr%5B%5D%2C%20int%20n)%0A%7B%0A%20%20%20%20for%20(int%20i%20%3D%200%3B%20i%20%3C%20n%3B%20i%2B%2B)%0A%20%20%20%20%20%20cout%20%3C%3C%20arr%5Bi%5D%20%3C%3C%20%22%20%22%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20above%20function%0Aint%20main()%0A%7B%0A%20%20%20%20int%20price%5B%5D%20%3D%20%7B10%2C%204%2C%205%2C%2090%2C%20120%2C%2080%7D%3B%0A%20%20%20%20int%20n%20%3D%20sizeof(price)%2Fsizeof(price%5B0%5D)%3B%0A%20%20%20%20int%20S%5Bn%5D%3B%0A%20%0A%20%20%20%20%2F%2F%20Fill%20the%20span%20values%20in%20array%20S%5B%5D%0A%20%20%20%20calculateSpan(price%2C%20n%2C%20S)%3B%0A%20%0A%20%20%20%20%2F%2F%20print%20the%20calculated%20span%20values%0A%20%20%20%20printArray(S%2C%20n)%3B%0A%20%0A%20%20%20%20return%200%3B%0A%7D\u201d message=\u201d\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><strong>Output:<\/strong><\/p>\n<pre>1 1 2 4 5 1<\/pre>\n<p><strong>Time Complexity<\/strong>: O(n). It seems more than O(n) at first look. If we take a closer look, we can observe that every element of array is added and removed from stack at most once. So there are total 2n operations at most. Assuming that a stack operation takes O(1) time, we can say that the time complexity is O(n).<\/p>\n<p><strong>Auxiliary Space<\/strong>: O(n) in worst case when all elements are sorted in decreasing order.<\/p>\n[ad type=\u201dbanner\u201d]\n","protected":false},"excerpt":{"rendered":"<p>C++ Algorithm &#8211; The Stock Span Problem &#8211; Stack &#8211; The stock span problem is a financial problem where we have a series of n daily price quotes<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[83515,1,73012,79607],"tags":[81385,81401,81384,81403,81402,81382,81381,81380,81378,81379,81377,81383],"class_list":["post-27332","post","type-post","status-publish","format-standard","hentry","category-c-programming-3","category-coding","category-data-structures","category-stack","tag-best-time-to-buy-and-sell-stock-dynamic-programming","tag-buy-sell-stock-dynamic-programming","tag-largest-rectangle-under-histogram","tag-maximum-profit-by-buying-and-selling-a-share-at-most-twice","tag-stock-buy-sell-to-maximize-profit-java","tag-stock-geeks","tag-stock-problem-geeksforgeeks","tag-stock-span-problem-geeksforgeeks","tag-stock-span-problem-hackerrank","tag-stock-span-problem-using-stack-in-c","tag-stock-span-problem-wiki","tag-what-is-stock-recommendation-problem"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27332","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=27332"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/27332\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=27332"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=27332"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=27332"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}