6031. 找出数组中的所有 K 近邻下标
看数据范围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})
5203. 统计可以提取的工件
看数据范围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)
5227. K 次操作后最大化顶端元素
这题比较吃细节,推荐大家看一下灵茶山艾府大佬的题解,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
6032. 得到要求路径的最小带权子图
三次dijkstra,可可也是看了题解之后才做出来,15行解法👇文章来源:https://www.toymoban.com/news/detail-535209.html
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行完成周赛】的目标!文章来源地址https://www.toymoban.com/news/detail-535209.html
到了这里,关于【算法】力扣第 284 场周赛(最短代码)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!