{"id":2718,"date":"2020-10-06T00:04:27","date_gmt":"2020-10-06T00:04:27","guid":{"rendered":"https:\/\/data-science.gotoauthority.com\/2020\/10\/06\/maximum-element-in-a-stack-hack\/"},"modified":"2020-10-06T00:04:27","modified_gmt":"2020-10-06T00:04:27","slug":"maximum-element-in-a-stack-hack","status":"publish","type":"post","link":"https:\/\/wealthrevelation.com\/data-science\/2020\/10\/06\/maximum-element-in-a-stack-hack\/","title":{"rendered":"Maximum Element In A Stack Hack"},"content":{"rendered":"<div>\n<figure class=\"wp-block-image size-large\"><img data-srcset=\"https:\/\/nycdsa-blog-files.s3.us-east-2.amazonaws.com\/2020\/10\/daniel-brancusi\/backlit-computer-keyboard-696969-FKSSuYYc-300x200.jpg 300w, https:\/\/nycdsa-blog-files.s3.us-east-2.amazonaws.com\/2020\/10\/daniel-brancusi\/backlit-computer-keyboard-696969-FKSSuYYc-600x400.jpg 600w, https:\/\/nycdsa-blog-files.s3.us-east-2.amazonaws.com\/2020\/10\/daniel-brancusi\/backlit-computer-keyboard-696969-FKSSuYYc-768x512.jpg 768w, https:\/\/nycdsa-blog-files.s3.us-east-2.amazonaws.com\/2020\/10\/daniel-brancusi\/backlit-computer-keyboard-696969-FKSSuYYc-1024x683.jpg 1024w\" loading=\"lazy\" width=\"1024\" height=\"683\" alt=\"\" data-src=\"https:\/\/nycdsa-blog-files.s3.us-east-2.amazonaws.com\/2020\/10\/daniel-brancusi\/backlit-computer-keyboard-696969-FKSSuYYc-1024x683.jpg\" data-sizes=\"(max-width: 1024px) 100vw, 1024px\" class=\"wp-image-67465 lazyload\" src=\"image\/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==\"><img loading=\"lazy\" width=\"1024\" height=\"683\" src=\"https:\/\/nycdsa-blog-files.s3.us-east-2.amazonaws.com\/2020\/10\/daniel-brancusi\/backlit-computer-keyboard-696969-FKSSuYYc-1024x683.jpg\" alt=\"\" class=\"wp-image-67465\"><\/figure>\n<h4>A Favorite From HackerRank<\/h4>\n<p>You have an empty sequence, and you will be given <em>N<\/em><span id=\"MathJax-Element-1-Frame\" class=\"MathJax_SVG\"><\/span> queries. Each query is one of these three types:<\/p>\n<pre class=\"wp-block-code\"><code>1 x  -Push the element x into the stack.\n2    -Delete the element present at the top of the stack.\n3    -Print the maximum element in the stack.<\/code><\/pre>\n<div class=\"msB challenge_input_format_body\">\n<div class=\"hackdown-content\">\n<p>The first line of input contains an integer, <em>N<\/em><span id=\"MathJax-Element-1-Frame\" class=\"MathJax_SVG\"><\/span>. The next <em>N<\/em><span id=\"MathJax-Element-2-Frame\" class=\"MathJax_SVG\"><\/span> lines each contain an above mentioned query. <em>(It is guaranteed that each query is valid.)<\/em><\/p>\n<p><strong>Constraints<\/strong><\/p>\n<ul>\n<li>N will always be an integer between 1 and 1e6<\/li>\n<li>x will always be greater than or equal to 1 and less than or equal to 1e10<\/li>\n<li>The type will always be an integer in range (1, 3) inclusive<\/li>\n<\/ul>\n<div class=\"msB challenge_output_format_body\">\n<div class=\"hackdown-content\">\n<p>For each type 3\u00a0<span id=\"MathJax-Element-1-Frame\" class=\"MathJax_SVG\"><\/span>query, print the maximum element in the stack on a new line.<\/p>\n<p><strong>Sample Input<\/strong><\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<pre class=\"wp-block-code\"><code>10\n1 97\n2\n1 20\n2\n1 26\n1 20\n2\n3\n1 91\n3<\/code><\/pre>\n<p><strong>Sample Output<\/strong><\/p>\n<pre class=\"wp-block-code\"><code>26\n91<\/code><\/pre>\n<h2>Our Approach<\/h2>\n<p>At first glance this can seem like a challenging task. \u00a0So let&#8217;s go through exactly what the question is asking for:<\/p>\n<ul>\n<li>We have to keep track of the largest element of the sequence<\/li>\n<li>AND THAT&#8217;S IT!<\/li>\n<\/ul>\n<p>Yup, the only thing we need to do for certain is keep track of the largest element in the list. \u00a0Do we need to keep track of the ordered sequence? No! \u00a0Do we need to return any element other than the largest? No! \u00a0Can we use this to come up with a fast solution? Absolutely!<\/p>\n<p>The trick that we are going to employ revolves around keeping track of the largest element in a separate list and only appending items to that list which are either larger than the current largest element or we will append the current largest element to the list again. \u00a0Therefore, the current largest element will always be at position -1 of our separate list. \u00a0Let&#8217;s go through the code to clarify:<\/p>\n<pre class=\"wp-block-code\"><code>#set initial value of our maximum element tracking at 0 so #that any value passed via input will exceed this value and #become the subsequent maximum.  \nmax_tracker = [0]\n\n# we cover the entire range of N queries\nfor i in range(int(input())):\n    input_list = list(map(int, input().split()))\n    if input_list[0] == 1:\n      max_tracker.append(max(input_list[1],max_tracker[-1]))\n    elif input_list[0] == 2:\n        max_tracker.pop()\n    else:\n        print(max_tracker[-1])<\/code><\/pre>\n<p>Looking at the line:\u00a0max_tracker.append( max ( input_list[1], max_tracker[-1] ) ) we see that if the current value is larger than our previous maximum then the current value is appended to the list. \u00a0However, if the current value is less than our current maximum, then the current maximum is appended to the list again. \u00a0Therefore we always have the maximum element in the [-1] position and we can simply pop the last element if we need to remove the element present at the top of the stack while not disrupting the order needed to solve the question. \u00a0<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>https:\/\/nycdatascience.com\/blog\/student-works\/maximum-element-in-a-stack\/<\/p>\n","protected":false},"author":0,"featured_media":2719,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[],"_links":{"self":[{"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/posts\/2718"}],"collection":[{"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/comments?post=2718"}],"version-history":[{"count":0,"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/posts\/2718\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/media\/2719"}],"wp:attachment":[{"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/media?parent=2718"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/categories?post=2718"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wealthrevelation.com\/data-science\/wp-json\/wp\/v2\/tags?post=2718"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}