class Solution:
def timeRequiredToBuy(self, tickets: List[int], k: int) -> int:
n = len(tickets)
q = deque()
for i in range(n):
q.append(i)
time = 0
while q:
time += 1
cur = q.popleft()
tickets[cur] -= 1
if tickets[cur] == 0:
if cur == k:
return time
else:
q.append(cur)
return timeWhere is the size of the input array and is the maximum value in the input array.
class Solution:
def timeRequiredToBuy(self, tickets: List[int], k: int) -> int:
n = len(tickets)
idx = 0
time = 0
while True:
time += 1
tickets[idx] -= 1
if tickets[idx] == 0:
if idx == k:
return time
idx = (idx + 1) % n
while tickets[idx] == 0:
idx = (idx + 1) % n
return timeWhere is the size of the input array and is the maximum value in the input array.
class Solution:
def timeRequiredToBuy(self, tickets: List[int], k: int) -> int:
res = 0
for i in range(len(tickets)):
if i <= k:
res += min(tickets[i], tickets[k])
else:
res += min(tickets[i], tickets[k] - 1)
return res