首页 > 其他分享 >2073. Time Needed to Buy Tickets

2073. Time Needed to Buy Tickets

时间:2024-06-05 11:58:01浏览次数:25  
标签:tickets Buy person int currentPerson Needed buy 2073 line

There are n people in a line queuing to buy tickets, where the 0th person is at the front of the line and the (n - 1)th person is at the back of the line.

You are given a 0-indexed integer array tickets of length n where the number of tickets that the ith person would like to buy is tickets[i].

Each person takes exactly 1 second to buy a ticket. A person can only buy 1 ticket at a time and has to go back to the end of the line (which happens instantaneously) in order to buy more tickets. If a person does not have any tickets left to buy, the person will leave the line.

Return the time taken for the person at position k (0-indexed) to finish buying tickets.

Example 1:

Input: tickets = [2,3,2], k = 2
Output: 6
Explanation: 
- In the first pass, everyone in the line buys a ticket and the line becomes [1, 2, 1].
- In the second pass, everyone in the line buys a ticket and the line becomes [0, 1, 0].
The person at position 2 has successfully bought 2 tickets and it took 3 + 3 = 6 seconds.

Example 2:

Input: tickets = [5,1,1,1], k = 0
Output: 8
Explanation:
- In the first pass, everyone in the line buys a ticket and the line becomes [4, 0, 0, 0].
- In the next 4 passes, only the person in position 0 is buying tickets.
The person at position 0 has successfully bought 5 tickets and it took 4 + 1 + 1 + 1 + 1 = 8 seconds.

Constraints:

  • n == tickets.length
  • 1 <= n <= 100
  • 1 <= tickets[i] <= 100
  • 0 <= k < n
class Solution {
public:
    int timeRequiredToBuy(vector<int>& tickets, int k) {
        int n=tickets.size();
        int totalTime=0;
        queue<int>q;
        for(int i=0;i<n;i++){
            q.push(i);
        }
        while(!q.empty() && tickets[q.front()]>0){
            int currentPerson=q.front();
            q.pop();
            tickets[currentPerson]--;
            totalTime++;
            if(tickets[currentPerson]){
                q.push(currentPerson);
            }
            if(currentPerson==k && tickets[currentPerson]==0){
                return totalTime;
            }
        }
        return totalTime;
    }
};

标签:tickets,Buy,person,int,currentPerson,Needed,buy,2073,line
From: https://blog.csdn.net/2301_80161204/article/details/139448677

相关文章

  • 反向海淘代购系统|pandabuy系统方案|系统流程讲解:引领全球购物新潮流
    随着全球化的深入发展和互联网技术的不断进步,人们的购物方式也在发生着翻天覆地的变化。反向海淘代购系统作为这一变革的杰出代表,正逐渐走进大众视野,为消费者带来前所未有的全球购物体验。一、反向海淘代购系统的定义传统的海淘模式主要是中国消费者通过跨境电商平台购买国......
  • 反海淘商业模式案例分析 :Pandabuy淘宝代购集运系统解析丨1688代采集运系统
    反海淘商业模式是指通过代购、代采集运等方式,帮助海外消费者购买并运输国内商品的一种商业模式。这种模式可以帮助海外消费者解决购买国内商品的困难,同时也为国内商家提供了一个新的销售渠道。下面以Pandabuy淘宝代购集运系统和1688代采集运系统为例进行解析。Pandabuy淘宝代......
  • 逆向海淘商业模式案例分析 :hagobuy淘宝代购集运系统丨淘宝代购集运系统搭建
    淘宝代购集运系统是一个电子商务平台,它允许消费者购买来自不同在线零售商(如淘宝、天猫、京东等)的商品,并将这些商品统一运送到消费者的地址。下面将探讨淘宝代购集运系统的工作机制和优势:多平台API接口的集成实时数据同步:通过API接口,系统能够实时获取并同步电商平台上商品......
  • Codeforces Round 946 (Div. 3) G Money Buys Less Happiness Now(反悔贪心)
    MoneyBuysLessHappinessNow1.题目大意:有n天,每天可以赚x块钱,然后每天可以通过花\(C_{i}\)块钱购买1点快乐值,然后每天赚的钱至少要在下一天才能用,问最多能获得多少快乐值。2.解题思路:我们发现天数变得很多,不能像e题那样dp了,所以要用贪心。具体来讲,我们碰到当前能买的就直接......
  • E. Money Buys Happiness
    原题链接题解观察到h不大于1e5,于是拿h做文章如果想要在第\(i\)个月的幸福值达到\(j\)那么第\(i-1\)个月的幸福值一定能达到\(j-h_i\)而且\(cost_{[i-1][j-h_i]}+c_i\leqx·(i-1)\)记得用滚动数组优化,因为这里\(i\)只和\(i-1\)的小幸福值有关code#include<bit......
  • G. Money Buys Less Happiness Now
    原题链接题解假如最后有\(k\)个月购买过幸福,那么这\(k\)个月的价格一定是前\(k\)小的code#include<bits/stdc++.h>#definelllonglongusingnamespacestd;intmain(){ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);intt;cin>>t;whil......
  • Codeforces 1974G Money Buys Less Happiness Now
    考虑到有一种贪心的思路就是能选就选。显然这是错的,因为可能存在后面更优的情况,即当\(c_i>c_j(i<j)\)时,选\(j\)肯定比选\(i\)更优,因为后面剩下的更多且中间也留下了一些。于是考虑反悔贪心。还是一样的,如果能选就一定选上。否则来说,考虑对于当前已经选了的中的最大......
  • 122- Best Time to Buy and Sell Stock II 卖股票II
    题目描述链接:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/description/Youaregivenanintegerarraypriceswhereprices[i]isthepriceofagivenstockontheithday.Oneachday,youmaydecidetobuyand/orsellthestock.Youcanon......
  • CF938D Buy a Ticket
    题目链接:https://www.luogu.com.cn/problem/CF938D虚拟源点+最短路首先因为所要求的权值由往返的路费和目的地需要的票价两部分构成,所以我们先对每座城市之间的道路建边,边权直接设为输入的两倍。之后我们建立一个虚拟源点,对所有城市链接一条单向边,边权就是城市的票价,即把点权转......
  • The "TypeScript Vue Plugin (Volar)" extension is no longer needed since v2. Plea
    这个报错信息表明你正在使用的是VisualStudioCode或者其他支持Volar的编辑器,而Volar是一个为Vue3应用提供TypeScript支持的工具。这个报错指出自从Volar版本2开始,"TypeScriptVue插件(Volar)"这个扩展就不再需要了。解决方法:如果你在使用的是VisualStudioCode编辑器,并且安装......