

{"id":163,"date":"2022-03-05T19:57:25","date_gmt":"2022-03-06T00:57:25","guid":{"rendered":"https:\/\/sites.temple.edu\/vahid\/?p=163"},"modified":"2022-03-05T19:57:25","modified_gmt":"2022-03-06T00:57:25","slug":"hackerrank-solutions-jesse-and-cookies","status":"publish","type":"post","link":"https:\/\/sites.temple.edu\/vahid\/2022\/03\/05\/hackerrank-solutions-jesse-and-cookies\/","title":{"rendered":"HackerRank Solutions: Jesse and Cookies"},"content":{"rendered":"\n<p class=\"has-text-align-center\"><strong>Problem<\/strong><\/p>\n\n\n\n<p>Jesse loves cookies and wants the sweetness of some cookies to be greater than value&nbsp;. To do this, two cookies with the least sweetness are repeatedly mixed. This creates a special combined cookie with:<\/p>\n\n\n\n<p><em>sweetness<\/em>&nbsp;&nbsp;<em>Least sweet cookie<\/em>&nbsp;&nbsp;&nbsp;<em>2nd least sweet cookie<\/em>).<\/p>\n\n\n\n<p>This occurs until all the cookies have a sweetness&nbsp;.<\/p>\n\n\n\n<p>Given the sweetness of a number of cookies, determine the minimum number of operations required. If it is not possible, return&nbsp;.<\/p>\n\n\n\n<p><strong>Example<\/strong><br><\/p>\n\n\n\n<p>The smallest values are&nbsp;.<br>Remove them then return&nbsp;&nbsp;to the array. Now&nbsp;.<br>Remove&nbsp;&nbsp;and return&nbsp;&nbsp;to the array. Now&nbsp;.<br>Remove&nbsp;, return&nbsp;&nbsp;and&nbsp;.<br>Finally, remove&nbsp;&nbsp;and return&nbsp;&nbsp;to&nbsp;. Now&nbsp;.<br>All values are&nbsp;&nbsp;so the process stops after&nbsp;&nbsp;iterations. Return&nbsp;.<\/p>\n\n\n\n<p><strong>Function Description<\/strong><br>Complete the&nbsp;<em>cookies<\/em>&nbsp;function in the editor below.<\/p>\n\n\n\n<p><em>cookies<\/em>&nbsp;has the following parameters:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><em>int k:<\/em>&nbsp;the threshold value<\/li><li><em>int A[n]:<\/em>&nbsp;an array of sweetness values<\/li><\/ul>\n\n\n\n<p><strong>Returns<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><em>int:<\/em>&nbsp;the number of iterations required or&nbsp;<\/li><\/ul>\n\n\n\n<p><strong>Input Format<\/strong><\/p>\n\n\n\n<p>The first line has two space-separated integers,&nbsp;&nbsp;and&nbsp;, the size of&nbsp;&nbsp;and the minimum required sweetness respectively.<\/p>\n\n\n\n<p>The next line contains&nbsp;&nbsp;space-separated integers,&nbsp;.<\/p>\n\n\n\n<p><strong>Constraints<\/strong><\/p>\n\n\n\n<p><strong>Sample Input<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">STDIN               Function\n-----               --------\n6 7                 A[] size n = 6, k = 7\n1 2 3 9 10 12       A = [1, 2, 3, 9, 10, 12]  \n<\/pre>\n\n\n\n<p><strong>Sample Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>2\n<\/code><\/pre>\n\n\n\n<p><strong>Explanation<\/strong><\/p>\n\n\n\n<p>Combine the first two cookies to create a cookie with&nbsp;<em>sweetness<\/em>&nbsp;&nbsp;=&nbsp;<br>After this operation, the cookies are&nbsp;.<br>Then, combine the cookies with sweetness&nbsp;&nbsp;and sweetness&nbsp;, to create a cookie with resulting&nbsp;<em>sweetness<\/em>&nbsp;&nbsp;=&nbsp;<br>Now, the cookies are&nbsp;.<br>All the cookies have a sweetness&nbsp;.<br><br>Thus,&nbsp;&nbsp;operations are required to increase the sweetness.<\/p>\n\n\n\n<p class=\"has-text-align-center\"><strong>Solution<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#!\/bin\/python3\r\n\r\nimport math\r\nimport os\r\nimport random\r\nimport re\r\nimport sys\r\nimport heapq\r\n\r\n#\r\n# Complete the 'cookies' function below.\r\n#\r\n# The function is expected to return an INTEGER.\r\n# The function accepts following parameters:\r\n#  1. INTEGER k\r\n#  2. INTEGER_ARRAY A\r\n#\r\n\r\ndef cookies(k, A):\r\n    # Write your code here\r\n    heapq.heapify(A)\r\n    opc = 0\r\n    while A&#091;0] &lt; k:\r\n        if len(A) &lt; 2:\r\n            return -1\r\n        a = heapq.heappop(A)\r\n        b = heapq.heappop(A)\r\n        heapq.heappush(A, a + 2*b)\r\n        opc += 1\r\n    return (opc if opc &gt;= 0 else -1)\r\n\r\nif __name__ == '__main__':\r\n    fptr = open(os.environ&#091;'OUTPUT_PATH'], 'w')\r\n\r\n    first_multiple_input = input().rstrip().split()\r\n\r\n    n = int(first_multiple_input&#091;0])\r\n\r\n    k = int(first_multiple_input&#091;1])\r\n\r\n    A = list(map(int, input().rstrip().split()))\r\n\r\n    result = cookies(k, A)\r\n\r\n    fptr.write(str(result) + '\\n')\r\n\r\n    fptr.close()\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Problem Jesse loves cookies and wants the sweetness of some cookies to be greater than value&nbsp;. To do this, two cookies with the least sweetness&#8230;<\/p>\n<div class=\"more-link-wrapper\"><a class=\"more-link\" href=\"https:\/\/sites.temple.edu\/vahid\/2022\/03\/05\/hackerrank-solutions-jesse-and-cookies\/\">Continue reading<span class=\"screen-reader-text\">HackerRank Solutions: Jesse and Cookies<\/span><\/a><\/div>\n","protected":false},"author":18759,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[102,104,4,103],"class_list":["post-163","post","type-post","status-publish","format-standard","hentry","category-programming","tag-hackerrank","tag-programming","tag-python","tag-solution","entry"],"_links":{"self":[{"href":"https:\/\/sites.temple.edu\/vahid\/wp-json\/wp\/v2\/posts\/163","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sites.temple.edu\/vahid\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sites.temple.edu\/vahid\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sites.temple.edu\/vahid\/wp-json\/wp\/v2\/users\/18759"}],"replies":[{"embeddable":true,"href":"https:\/\/sites.temple.edu\/vahid\/wp-json\/wp\/v2\/comments?post=163"}],"version-history":[{"count":0,"href":"https:\/\/sites.temple.edu\/vahid\/wp-json\/wp\/v2\/posts\/163\/revisions"}],"wp:attachment":[{"href":"https:\/\/sites.temple.edu\/vahid\/wp-json\/wp\/v2\/media?parent=163"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sites.temple.edu\/vahid\/wp-json\/wp\/v2\/categories?post=163"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sites.temple.edu\/vahid\/wp-json\/wp\/v2\/tags?post=163"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}