第一题 拿硬币
class Solution:
def minCount(self, coins) -> int:
cnt = 0
for x in coins:
cnt += x >> 1
cnt += x & 1
return cnt
第二题 传递信息
class Solution:
def numWays(self, n: int, relation: List[List[int]], k: int) -> int:
x = [0]
m = {}
for r in relation:
if r[0] not in m:
m[r[0]] = [r[1]]
else:
m[r[0]].append(r[1])
for i in range(k):
xx = []
for c in x:
if c in m:
xx.extend(m[c])
# print(xx)
x = xx
cnt = 0
for xxx in x:
if xxx == n-1:
cnt += 1
return cnt
第三题 LCP 08. 剧情触发时间
import bisect
class Solution:
def getTriggerTime(self, increase: List[List[int]], requirements: List[List[int]]) -> List[int]:
c = [0]
r = [0]
h = [0]
for cc, rr, hh in increase:
c.append(c[-1] + cc)
r.append(r[-1] + rr)
h.append(h[-1] + hh)
# print(c)
# print(r)
# print(h)
a = []
for cc, rr, hh in requirements:
ct = bisect.bisect_left(c, cc)
rt = bisect.bisect_left(r, rr)
ht = bisect.bisect_left(h, hh)
xx = max([ct, rt, ht])
if xx == len(c): xx = -1
a.append(xx)
return a
第四题 最小跳跃次数
class Solution:
def minJump(self, jump: List[int]) -> int:
l = len(jump)
outi = []
to = {}
fr = {}
ddd = [-1] * l
for i in range(l):
d = i + jump[i]
if d >= l:
outi.append(i)
else:
to[i] = d
if d in fr:
fr[d].append(i)
else:
fr[d] = [i]
mi = min(outi)
import queue
q = queue.PriorityQueue()
for oi in outi:
ddd[oi] = 0
q.put((0, oi))
# print(q.queue)
# print(fr)
cm = l
while True:
i, n = q.get()
# print(i, n)
if n == 0:
break
for j in range(n+1, cm):
if ddd[j] == -1 or ddd[j] > i+1:
# print('%d -> %d' %(n, j))
q.put((i+1, j))
ddd[j] = i+1
cm = min(cm, n)
if n in fr:
for f in fr[n]:
# print(f,'f')
if ddd[f] == -1 or ddd[f] > i+1:
ddd[f] = i+1
# print('%d -> %d' %(n, f))
q.put((i+1, f))
return i+1
第五题 二叉树任务调度
LCP 10. 二叉树任务调度
我自己还没做出来,参考了两个题解 littledva的题解 和 zqy1018的题解
两篇都是 dfs,但思路不太一样,第一篇是把每棵树的时间分为单核时间(就是不能并行的时间)和双核时间,答案是二者的和;第二篇是分别计算每棵子树最短时间和所有任务的合计时间。
欢迎来我的博客: https://codeplot.top/
我的博客刷题分类:https://codeplot.top/categories/%E5%88%B7%E9%A2%98/
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。