2660. 保龄球游戏的获胜者
def calc(a):
ans = 0
for i,v in enumerate(a):
if i and a[i-1] == 10 or i-1>0 and a[i-2] == 10:
v *= 2
ans += v
return ans
class Solution:
def isWinner(self, p1: List[int], p2: List[int]) -> int:
a,b = calc(p1),calc(p2)
if a == b:
return 0
if a > b:
return 1
return 2
2661. 找出叠涂元素
class Solution:
def firstCompleteIndex(self, arr: List[int], mat: List[List[int]]) -> int:
m,n = len(mat),len(mat[0])
p = {}
for i,row in enumerate(mat):
for j,v in enumerate(row):
p[v] = (i,j)
rows = [n]*m
cols = [m] * n
for i,v in enumerate(arr):
x,y = p[v]
rows[x] -= 1
cols[y] -= 1
if not rows[x] or not cols[y]:
return i
2662. 前往目标的最小代价
class Solution:
def minimumCost(self, start: List[int], target: List[int], specialRoads: List[List[int]]) -> int:
g = defaultdict(dict)
def get(x,y):
return x * 1000000+y
def add_edge(u,v,w):
if v in g[u]:
g[u][v] = min(g[u][v],w)
else:
g[u][v] = w
def manhadun(x1,y1,x2,y2):
return abs(x1-x2) + abs(y1-y2)
sx,sy = start
tx,ty = target
s,t = get(sx,sy),get(tx,ty)
add_edge(s,t,manhadun(tx,ty,sx,sy))
ps = {(sx,sy),(tx,ty)}
def add_p(x,y):
ps.add((x,y))
for x1,y1,x2,y2,w in specialRoads:
add_p(x1,y1)
add_p(x2,y2)
u,v = get(x1,y1),get(x2,y2)
add_edge(u,v,w)
add_edge(s,u,manhadun(sx,sy,x1,y1))
add_edge(v,t,manhadun(x2,y2,tx,ty))
for x1,y1 in ps:
u = get(x1,y1)
for x2,y2 in ps:
v = get(x2,y2)
add_edge(u,v,manhadun(x2,y2,x1,y1))
dis = defaultdict(lambda : inf)
q = [(0,s)]
while q:
c,u = heappop(q)
if u == t :
return c
if c > dis[u]:continue
for v, w in g[u].items():
d = c + w
if d < dis[v]:
dis[v] = d
heappush(q,(d,v))
return -1
2663. 字典序最小的美丽字符串
贪心
class Solution:
def smallestBeautifulString(self, s: str, k: int) -> str:
n = len(s)
s = list(s)
ans = list(s)
up = chr(ord('a') + k - 1)
i = n - 1
ans[n-1] = chr(ord(ans[i]) + 1)
while i < n:
if ans[i] > up:
if i == 0 :
return ''
ans[i] = 'a'
i -= 1
ans[i] = chr(ord(ans[i]) + 1)
elif i and ans[i] == ans[i-1] or i-1>0 and ans[i] == ans[i-2]:
ans[i] = chr(ord(ans[i]) + 1)
else:
i += 1
return ''.join(ans)