Re: [閒聊] 每日leetcode
看板Marginalman作者DJYOSHITAKA (franchouchouISBEST)時間1年前 (2024/10/15 08:35)推噓3(3推 0噓 2→)留言5則, 5人參與討論串987/1550 (看更多)
今天的
就從後面滑過來
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


--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 193.148.16.52 (日本)
※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1728952523.A.FC9.html
推
10/15 08:42,
1年前
, 1F
10/15 08:42, 1F
→
10/15 08:42,
1年前
, 2F
10/15 08:42, 2F
推
10/15 08:44,
1年前
, 3F
10/15 08:44, 3F
推
10/15 08:44,
1年前
, 4F
10/15 08:44, 4F
→
10/15 09:39,
1年前
, 5F
10/15 09:39, 5F
討論串 (同標題文章)
完整討論串 (本文為第 987 之 1550 篇):