{"id":8373,"date":"2021-04-18T09:44:25","date_gmt":"2021-04-18T16:44:25","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8373"},"modified":"2021-04-18T09:44:33","modified_gmt":"2021-04-18T16:44:33","slug":"leetcode-1833-maximum-ice-cream-bars","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/greedy\/leetcode-1833-maximum-ice-cream-bars\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1833. Maximum Ice Cream Bars"},"content":{"rendered":"\n<p>It is a sweltering summer day, and a boy wants to buy some ice cream bars.<\/p>\n\n\n\n<p>At the store, there are&nbsp;<code>n<\/code>&nbsp;ice cream bars. You are given an array&nbsp;<code>costs<\/code>&nbsp;of length&nbsp;<code>n<\/code>, where&nbsp;<code>costs[i]<\/code>&nbsp;is the price of the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;ice cream bar in coins. The boy initially has&nbsp;<code>coins<\/code>&nbsp;coins to spend, and he wants to buy as many ice cream bars as possible.&nbsp;<\/p>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>maximum<\/strong>&nbsp;number of ice cream bars the boy can buy with&nbsp;<\/em><code>coins<\/code><em>&nbsp;coins.<\/em><\/p>\n\n\n\n<p><strong>Note:<\/strong>&nbsp;The boy can buy the ice cream bars in any order.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> costs = [1,3,2,4,1], coins = 7\n<strong>Output:<\/strong> 4\n<strong>Explanation: <\/strong>The boy can buy ice cream bars at indices 0,1,2,4 for a total price of 1 + 3 + 2 + 1 = 7.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> costs = [10,6,8,7,7,8], coins = 5\n<strong>Output:<\/strong> 0\n<strong>Explanation: <\/strong>The boy cannot afford any of the ice cream bars.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> costs = [1,6,3,1,2,5], coins = 20\n<strong>Output:<\/strong> 6\n<strong>Explanation: <\/strong>The boy can buy all the ice cream bars for a total price of 1 + 6 + 3 + 1 + 2 + 5 = 18.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>costs.length == n<\/code><\/li><li><code>1 &lt;= n &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= costs[i] &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>1 &lt;= coins &lt;= 10<sup>8<\/sup><\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Greedy<\/strong><\/h2>\n\n\n\n<p>Sort by price in ascending order, buy from the lowest price to the highest price.<\/p>\n\n\n\n<p>Time complexity: O(nlogn)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\nclass Solution {\npublic:\n  int maxIceCream(vector<int>& costs, int coins) {\n    sort(begin(costs), end(costs));\n    int ans = 0;\n    for (int c : costs) {\n      if (c > coins) break;\n      coins -= c;\n      ++ans;\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>It is a sweltering summer day, and a boy wants to buy some ice cream bars. At the store, there are&nbsp;n&nbsp;ice cream bars. You are&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[51],"tags":[88,177,23],"class_list":["post-8373","post","type-post","status-publish","format-standard","hentry","category-greedy","tag-greedy","tag-medium","tag-sort","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8373","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=8373"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8373\/revisions"}],"predecessor-version":[{"id":8375,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8373\/revisions\/8375"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8373"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8373"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8373"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}