Python CPP速解LLeetcode 2073 Time Needed to Buy Tickets
----
Assume x=tickets[k]. When i≤k, person[i] can only buy x tickets at most; when i bigger than k, person[i] can only buy x-1 tickets at most.
[codes on Leetcode]https://leetcode.com/problems/time-needed-to-buy-tickets/solutions/4995373/loop-with-sum-min-buy-tickets-i-1-liner-0ms-beats-100/
class Solution:
def timeRequiredToBuy(self, t: List[int], k: int) -> int:
x=t[k]
time=sum(min(y,x) for y in t[:k+1])
time+=sum(min(y, x-1) for y in t[k+1:])
return time
Python 1-liner
class Solution: def timeRequiredToBuy(self, t: List[int], k: int) -> int: return sum(min(y,x:=t[k]) for y in t[:k+1])+sum(min(y, x-1) for y in t[k+1:])
沒有留言:
張貼留言