作者DJYOSHITAKA (franchouchouISBEST)
看板Marginalman
標題Re: [閒聊] 每日leetcode
時間Tue Oct 15 08:35:20 2024
今天的
就從後面滑過來
def minimumSteps(self, s: str) -> int:
cost, ans = 0, 0
for i in range(len(s)-1, -1, -1):
if s[i] == "1":
ans += cost
elif s[i] == "0":
cost += 1
return ans
不知道哪一天的
我直接用兩個pq搞==
好麻煩
def smallestChair(self, times: List[List[int]], targetFriend: int) -> int:
times = [(time, idx) for idx, time in enumerate(times)]
times.sort()
empty_pq = []
occupy_pq = []
for time, idx in times:
while len(occupy_pq)>0 and occupy_pq[0][0]<=time[0]:
_, chair_idx = heappop(occupy_pq)
heappush(empty_pq, chair_idx)
if len(empty_pq)==0:
cur_chair_idx = len(occupy_pq)
else:
cur_chair_idx = heappop(empty_pq)
if idx==targetFriend:
return cur_chair_idx
heappush(occupy_pq, (time[1], cur_chair_idx))
return -1
不知道哪天的
直接用meeting room的方式
找重疊區間最多的地方
def minGroups(self, intervals: List[List[int]]) -> int:
q = []
for interv in intervals:
# -1 for left, 1 for right
q.append((interv[0], -1))
q.append((interv[1], 1))
q.sort()
ans, cur_cnt = 0, 0
for num, flag in q:
if flag==-1:
cur_cnt += 1
else:
cur_cnt -= 1
ans = max(ans, cur_cnt)
return ans
不知道哪天的
直接硬幹
def maxKelements(self, nums: List[int], k: int) -> int:
pq = []
for num in nums:
heappush(pq, -num)
ans = 0
for _ in range(k):
cur = heappop(pq) * -1
ans += cur
heappush(pq, -(ceil(cur/3)))
return ans
--
https://i.imgur.com/QaQrl0t.jpeg
https://i.imgur.com/yXpuYNA.jpeg
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 193.148.16.52 (日本)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1728952523.A.FC9.html
推 oin1104: 大師 你一天捲我好幾天 10/15 08:42
→ rainkaras: 大師 10/15 08:42
推 dont: 大師 10/15 08:44
推 sustainer123: 別卷了 10/15 08:44
→ sixB: 誰捲得過你 10/15 09:39