herrDeng網內搜尋

自訂搜尋

Ads

2024年4月9日 星期二

Python C++速解Leetcode 2073 Time Needed to Buy Tickets


Python CPP速解LLeetcode 2073  Time Needed to Buy Tickets
假設x=tickets[k]。 當i≤k時,person[i]最多只能買x張票; 當 i大於k時,person[i]最多只能購買 x-1 票。python code請進
----
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.
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:])

沒有留言:

Related Posts Plugin for WordPress, Blogger...

熱門文章