看数据范围1 <= nums.length <= 1000 ,直接暴力2行搞定
class Solution:
def findKDistantIndices(self, nums: List[int], key: int, k: int) -> List[int]:
keys = [i for i,v in enumerate(nums) if v == key]
return sorted({j for i in keys for j in range(len(nums)) if abs(i - j) <= k})
看数据范围1 <= n <= 1000 ,每个工件最多只覆盖4个单元格,直接哈希+暴力,2行搞定
class Solution:
def digArtifacts(self, n: int, artifacts: List[List[int]], dig: List[List[int]]) -> int:
map = set([(a, b) for a, b in dig])
return sum(all((i, j) in map for i in range(r1, r2+1) for j in range(c1, c2+1)) for r1, c1, r2, c2 in artifacts)
这题比较吃细节,推荐大家看一下灵茶山艾府大佬的题解,1行就搞定了
class Solution:
def maximumTop(self, nums: List[int], k: int) -> int:
return max(num for i, num in enumerate(nums) if i < k - 1 or i == k) if len(nums) > 1 or k % 2 == 0 else -1
三次dijkstra,可可也是看了题解之后才做出来,15行解法👇
class Solution:
def minimumWeight(self, n: int, edges: List[List[int]], src1: int, src2: int, dest: int) -> int:
g1,g2=defaultdict(lambda:defaultdict(lambda:inf)),defaultdict(lambda:defaultdict(lambda:inf))
for u,v,w in edges:
g1[u][v],g2[v][u]=min(g1[u][v],w),min(g2[v][u],w)
def dijkstra(graph,s):
dist=[inf]*n;dist[s]=0
heap=[];heappush(heap,(0,s))
while heap:
d,u=heappop(heap)
for v in graph[u]:
if graph[u][v]+d<dist[v]:
dist[v]=graph[u][v]+d
heappush(heap,(dist[v],v))
return dist
a,b,c=dijkstra(g1,src1),dijkstra(g1,src2),dijkstra(g2,dest)
return res if (res:=min(a[i]+b[i]+c[i] for i in range(n)))<inf else -1
总结
T4罚坐一小时/(ㄒoㄒ)/~~ ,T1+T2+T3+T4共2+2+1+15=20 行代码,勉强完成【20行完成周赛】的目标!
|