{"id":8708,"date":"2021-11-15T08:57:40","date_gmt":"2021-11-15T16:57:40","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8708"},"modified":"2021-11-15T08:58:32","modified_gmt":"2021-11-15T16:58:32","slug":"leetcode-2073-time-needed-to-buy-tickets","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-2073-time-needed-to-buy-tickets\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2073. Time Needed to Buy Tickets"},"content":{"rendered":"\n<p>There are&nbsp;<code>n<\/code>&nbsp;people in a line queuing to buy tickets, where the&nbsp;<code>0<sup>th<\/sup><\/code>&nbsp;person is at the&nbsp;<strong>front<\/strong>&nbsp;of the line and the&nbsp;<code>(n - 1)<sup>th<\/sup><\/code>&nbsp;person is at the&nbsp;<strong>back<\/strong>&nbsp;of the line.<\/p>\n\n\n\n<p>You are given a&nbsp;<strong>0-indexed<\/strong>&nbsp;integer array&nbsp;<code>tickets<\/code>&nbsp;of length&nbsp;<code>n<\/code>&nbsp;where the number of tickets that the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;person would like to buy is&nbsp;<code>tickets[i]<\/code>.<\/p>\n\n\n\n<p>Each person takes&nbsp;<strong>exactly 1 second<\/strong>&nbsp;to buy a ticket. A person can only buy&nbsp;<strong>1 ticket at a time<\/strong>&nbsp;and has to go back to&nbsp;<strong>the end<\/strong>&nbsp;of the line (which happens&nbsp;<strong>instantaneously<\/strong>) in order to buy more tickets. If a person does not have any tickets left to buy, the person will&nbsp;<strong>leave&nbsp;<\/strong>the line.<\/p>\n\n\n\n<p>Return&nbsp;<em>the&nbsp;<strong>time taken<\/strong>&nbsp;for the person at position&nbsp;<\/em><code>k<\/code><strong><em>(0-indexed)<\/em>&nbsp;<\/strong><em>to finish buying tickets<\/em>.<\/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> tickets = [2,3,2], k = 2\n<strong>Output:<\/strong> 6\n<strong>Explanation:<\/strong> \n- In the first pass, everyone in the line buys a ticket and the line becomes [1, 2, 1].\n- In the second pass, everyone in the line buys a ticket and the line becomes [0, 1, 0].\nThe person at&nbsp;position 2 has successfully bought 2 tickets and it took 3 + 3 = 6 seconds.\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> tickets = [5,1,1,1], k = 0\n<strong>Output:<\/strong> 8\n<strong>Explanation:<\/strong>\n- In the first pass, everyone in the line buys a ticket and the line becomes [4, 0, 0, 0].\n- In the next 4 passes, only the person in position 0 is buying tickets.\nThe person at&nbsp;position 0 has successfully bought 5 tickets and it took 4 + 1 + 1 + 1 + 1 = 8 seconds.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>n == tickets.length<\/code><\/li><li><code>1 &lt;= n &lt;= 100<\/code><\/li><li><code>1 &lt;= tickets[i] &lt;= 100<\/code><\/li><li><code>0 &lt;= k &lt; n<\/code><\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 1: Simulation<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n * tickets[k])<br>Space complexity: O(n) \/ O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre>\n\/\/ Author: huahua\nclass Solution {\npublic:\n  int timeRequiredToBuy(vector<int>& tickets, int k) {\n    const int n = tickets.size();\n    queue<pair<int, int>> q;\n    for (int i = 0; i < n; ++i)\n      q.emplace(i, tickets[i]);\n    int ans = 0;\n    while (!q.empty()) {\n      ++ans;\n      auto [i, t] = q.front(); q.pop();      \n      if (--t == 0 &#038;&#038; k == i) return ans;\n      if (t) q.emplace(i, t);\n    }\n    return -1;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution 2: Math<\/strong><\/h2>\n\n\n\n<p>Each person before k will have tickets[k] rounds, each person after k will have tickets[k] - 1 rounds.<\/p>\n\n\n\n<p>Time complexity: O(n)<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>\n\/\/ Author: huahua\nclass Solution {\npublic:\n  int timeRequiredToBuy(vector<int>& tickets, int k) {\n    int ans = 0;\n    for (int i = 0; i < tickets.size(); ++i)\n      ans += min(tickets[i], tickets[k] - (i > k));\n    return ans;\n  }\n};\n<\/pre>\n<\/div><\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are&nbsp;n&nbsp;people in a line queuing to buy tickets, where the&nbsp;0th&nbsp;person is at the&nbsp;front&nbsp;of the line and the&nbsp;(n &#8211; 1)th&nbsp;person is at the&nbsp;back&nbsp;of the line.&#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":[222,31,213,179],"class_list":["post-8708","post","type-post","status-publish","format-standard","hentry","category-simulation","tag-easy","tag-math","tag-queue","tag-simulation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8708","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=8708"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8708\/revisions"}],"predecessor-version":[{"id":8711,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8708\/revisions\/8711"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8708"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8708"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8708"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}