{"id":7422,"date":"2020-09-27T00:50:47","date_gmt":"2020-09-27T07:50:47","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7422"},"modified":"2020-09-27T00:52:36","modified_gmt":"2020-09-27T07:52:36","slug":"leetcode-1599-maximum-profit-of-operating-a-centennial-wheel","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-1599-maximum-profit-of-operating-a-centennial-wheel\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1599. Maximum Profit of Operating a Centennial Wheel"},"content":{"rendered":"\n<p>You are the operator of a Centennial Wheel that has&nbsp;<strong>four gondolas<\/strong>, and each gondola has room for&nbsp;<strong>up<\/strong>&nbsp;<strong>to<\/strong>&nbsp;<strong>four people<\/strong>. You have the ability to rotate the gondolas&nbsp;<strong>counterclockwise<\/strong>, which costs you&nbsp;<code>runningCost<\/code>&nbsp;dollars.<\/p>\n\n\n\n<p>You are given an array&nbsp;<code>customers<\/code>&nbsp;of length&nbsp;<code>n<\/code>&nbsp;where&nbsp;<code>customers[i]<\/code>&nbsp;is the number of new customers arriving just before the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;rotation (0-indexed). This means you&nbsp;<strong>must rotate<\/strong>&nbsp;the wheel&nbsp;<code>i<\/code>&nbsp;times before&nbsp;<code>customers[i]<\/code>&nbsp;arrive. Each customer pays&nbsp;<code>boardingCost<\/code>&nbsp;dollars when they board on the gondola closest to the ground and will exit once that gondola reaches the ground again.<\/p>\n\n\n\n<p>You can stop the wheel at any time, including&nbsp;<strong>before<\/strong>&nbsp;<strong>serving<\/strong>&nbsp;<strong>all<\/strong>&nbsp;<strong>customers<\/strong>. If you decide to stop serving customers,&nbsp;<strong>all subsequent rotations are free<\/strong>&nbsp;in order to get all the customers down safely. Note that if there are currently more than four customers waiting at the wheel, only four will board the gondola, and the rest will wait&nbsp;<strong>for the next rotation<\/strong>.<\/p>\n\n\n\n<p>Return<em>&nbsp;the minimum number of rotations you need to perform&nbsp;to maximize your profit.<\/em>&nbsp;If there is&nbsp;<strong>no scenario<\/strong>&nbsp;where the profit is positive, return&nbsp;<code>-1<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/09\/09\/wheeldiagram12.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> customers = [8,3], boardingCost = 5, runningCost = 6\n<strong>Output:<\/strong> 3\n<strong>Explanation:<\/strong> The numbers written on the gondolas are the number of people currently there.\n1. 8 customers arrive, 4 board and 4 wait for the next gondola, the wheel rotates. Current profit is 4 * $5 - 1 * $6 = $14.\n2. 3 customers arrive, the 4 waiting board the wheel and the other 3 wait, the wheel rotates. Current profit is 8 * $5 - 2 * $6 = $28.\n3. The final 3 customers board the gondola, the wheel rotates. Current profit is 11 * $5 - 3 * $6 = $37.\nThe highest profit was $37 after rotating the wheel 3 times.<\/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> customers = [10,9,6], boardingCost = 6, runningCost = 4\n<strong>Output:<\/strong> 7\n<strong>Explanation:<\/strong>\n1. 10 customers arrive, 4 board and 6 wait for the next gondola, the wheel rotates. Current profit is 4 * $6 - 1 * $4 = $20.\n2. 9 customers arrive, 4 board and 11 wait (2 originally waiting, 9 newly waiting), the wheel rotates. Current profit is 8 * $6 - 2 * $4 = $40.\n3. The final 6 customers arrive, 4 board and 13 wait, the wheel rotates. Current profit is 12 * $6 - 3 * $4 = $60.\n4. 4 board and 9 wait, the wheel rotates. Current profit is 16 * $6 - 4 * $4 = $80.\n5. 4 board and 5 wait, the wheel rotates. Current profit is 20 * $6 - 5 * $4 = $100.\n6. 4 board and 1 waits, the wheel rotates. Current profit is 24 * $6 - 6 * $4 = $120.\n7. 1 boards, the wheel rotates. Current profit is 25 * $6 - 7 * $4 = $122.\nThe highest profit was $122 after rotating the wheel 7 times.\n\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> customers = [3,4,0,5,1], boardingCost = 1, runningCost = 92\n<strong>Output:<\/strong> -1\n<strong>Explanation:<\/strong>\n1. 3 customers arrive, 3 board and 0 wait, the wheel rotates. Current profit is 3 * $1 - 1 * $92 = -$89.\n2. 4 customers arrive, 4 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 2 * $92 = -$177.\n3. 0 customers arrive, 0 board and 0 wait, the wheel rotates. Current profit is 7 * $1 - 3 * $92 = -$269.\n4. 5 customers arrive, 4 board and 1 waits, the wheel rotates. Current profit is 12 * $1 - 4 * $92 = -$356.\n5. 1 customer arrives, 2 board and 0 wait, the wheel rotates. Current profit is 13 * $1 - 5 * $92 = -$447.\nThe profit was never positive, so return -1.\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> customers = [10,10,6,4,7], boardingCost = 3, runningCost = 8\n<strong>Output:<\/strong> 9\n<strong>Explanation:<\/strong>\n1. 10 customers arrive, 4 board and 6 wait, the wheel rotates. Current profit is 4 * $3 - 1 * $8 = $4.\n2. 10 customers arrive, 4 board and 12 wait, the wheel rotates. Current profit is 8 * $3 - 2 * $8 = $8.\n3. 6 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 12 * $3 - 3 * $8 = $12.\n4. 4 customers arrive, 4 board and 14 wait, the wheel rotates. Current profit is 16 * $3 - 4 * $8 = $16.\n5. 7 customers arrive, 4 board and 17 wait, the wheel rotates. Current profit is 20 * $3 - 5 * $8 = $20.\n6. 4 board and 13 wait, the wheel rotates. Current profit is 24 * $3 - 6 * $8 = $24.\n7. 4 board and 9 wait, the wheel rotates. Current profit is 28 * $3 - 7 * $8 = $28.\n8. 4 board and 5 wait, the wheel rotates. Current profit is 32 * $3 - 8 * $8 = $32.\n9. 4 board and 1 waits, the wheel rotates. Current profit is 36 * $3 - 9 * $8 = $36.\n10. 1 board and 0 wait, the wheel rotates. Current profit is 37 * $3 - 10 * $8 = $31.\nThe highest profit was $36 after rotating the wheel 9 times.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>n == customers.length<\/code><\/li><li><code>1 &lt;= n &lt;= 10<sup>5<\/sup><\/code><\/li><li><code>0 &lt;= customers[i] &lt;= 50<\/code><\/li><li><code>1 &lt;= boardingCost, runningCost &lt;= 100<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Simulation<\/strong><\/h2>\n\n\n\n<p>Process if waiting customers &gt; 0 or i &lt; n.<\/p>\n\n\n\n<p>Pruning, if runningCost &gt; 4 * boardingCost (max revenue), there is no way to make profit.<\/p>\n\n\n\n<p>Time complexity: sum(consumers) \/ 4<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 minOperationsMaxProfit(vector<int>& customers, int boardingCost, int runningCost) {\n    const int n = customers.size();\n    constexpr int kMaxC = 4;\n    if (runningCost > kMaxC * boardingCost) return -1;\n    int c = 0;\n    int ans = -1;\n    int p = 0;\n    int max_p = 0;    \n    for (int r = 0; r < n || c > 0; ++r) {\n      c += (r < n ? customers[r] : 0);      \n      p += min(c, kMaxC) * boardingCost - runningCost;      \n      c -= min(c, kMaxC);\n      if (p > max_p) {\n        max_p = p;\n        ans = r + 1; \/\/ 1-based\n      }\n    }\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are the operator of a Centennial Wheel that has&nbsp;four gondolas, and each gondola has room for&nbsp;up&nbsp;to&nbsp;four people. You have the ability to rotate the&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[177,179],"class_list":["post-7422","post","type-post","status-publish","format-standard","hentry","category-simulation","tag-medium","tag-simulation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7422","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=7422"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7422\/revisions"}],"predecessor-version":[{"id":7424,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7422\/revisions\/7424"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7422"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7422"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7422"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}