记录了初步解题思路 以及本地实现代码;并不一定为最优 也希望大家能一起探讨 一起进步
matrix[i]代表i能够到达的点
每段距离耗时相等 需要得到两点间走了多少步
dis[i][0]代表从1到i的最短距离 dis[i][1]代表从1到i的次短距离
因为广搜 步数只增不减 所以更新状态时步数必定非减
只需要考虑是否是第一次更新对于一段时间t 如果mod=t%(2change)>change 说明t经历了一次变灯
需要等待 2change-mod
def secondMinimum(n, edges, time, change):
"""
:type n: int
:type edges: List[List[int]]
:type time: int
:type change: int
:rtype: int
"""
import collections
matrix= [[] for _ in range(n+1)]
for x,y in edges:
matrix[x].append(y)
matrix[y].append(x)
dis = [[float('inf')]*2 for _ in range(n+1)]
dis[1][0]=0
l = collections.deque([(1,0)])
while dis[n][1]==float('inf'):
loc,step = l.popleft()
step +=1
for nxt in matrix[loc]:
if step<dis[nxt][0]:
dis[nxt][0]=step
l.append((nxt,step))
elif dis[nxt][0]<step<dis[nxt][1]:
dis[nxt][1] = step
l.append((nxt,step))
ans = 0
for _ in range(dis[n][1]):
if ans%(2*change)>=change:
ans +=2*change-ans%(2*change)
ans+=time
return ans
1.递归
2.比赛一次淘汰一个人 需要淘汰n-1个人 比赛n-1次
def numberOfMatches(n):
"""
:type n: int
:rtype: int
"""
def recu(n):
if n==1:
return 0
if n%2==0:
return n//2
else:
return n//2+recu(n//2+1)
return recu(n)
def numberOfMatches2(n):
"""
:type n: int
:rtype: int
"""
return n-1
哈希表统计坐标
对于x,y 如果y固定了 找另一个点的tmpx,y
边长为diff=tmpx-x
寻找(x,y) (tmpx,y),(x,y+diff),(tmpx,y+diff)
from collections import defaultdict,Counter
class DetectSquares(object):
def __init__(self):
self.m = defaultdict(Counter)
def add(self, point):
"""
:type point: List[int]
:rtype: None
"""
x,y = point
self.m[x][y]+=1
def count(self, point):
"""
:type point: List[int]
:rtype: int
"""
ans = 0
x,y = point
if x not in self.m:
return 0
othery = self.m[x]
for tmpx,cnt in self.m.items():
if tmpx!=x:
diff = tmpx-x
ans += cnt[y]*othery[y+diff]*cnt[y+diff]
ans += cnt[y]*othery[y-diff]*cnt[y-diff]
return ans
按空格分割成若干小段s
对每个s中的字符依次处理
分为数字;-;标点;英文字符
def countValidWords(sentence):
"""
:type sentence: str
:rtype: int
"""
biao = ["!",".",","]
def check(s):
if len(s)==0:
return False
word = 0
line = 0
has = False
for i,c in enumerate(s):
if "0"<=c<="9":
return False
elif c=="-":
if line>0 or word==0:
return False
line+=1
has = True
elif c in biao:
if i<len(s)-1:
return False
else:
word+=1
has = False
if has:
return False
return True
l = sentence.split(" ")
ans = 0
for s in l:
if check(s):
print(s)
ans+=1
return ans
将攻击力a从大到小排序 同时攻击力相同时防御力b从小到大排序
记录最大防御力maxb 可以保证如果当前防御力b小于最大防御力maxb
则攻击力a必定也小于最大攻击力maxa
def numberOfWeakCharacters(properties):
"""
:type properties: List[List[int]]
:rtype: int
"""
properties.sort(key = lambda x:(-x[0],x[1]))
ans = 0
maxb = 0
for a,b in properties:
if maxb==0:
maxb = b
else:
if maxb>b:
ans+=1
else:
maxb = b
return ans
BFS
初始化水域高度为0 陆地高度为无穷
从水域开始向外扩展 依次增加高度 陆地高度取较小值
def highestPeak(isWater):
"""
:type isWater: List[List[int]]
:rtype: List[List[int]]
"""
n,m = len(isWater),len(isWater[0])
matrix = [[float('inf')]*m for _ in range(n)]
l = []
for i in range(n):
for j in range(m):
if isWater[i][j]==1:
l.append((i,j))
matrix[i][j]=0
steps = [(0,-1),(0,1),(1,0),(-1,0)]
while l:
tmp = set()
for i,j in l:
for x,y in steps:
newi,newj = i+x,j+y
if 0<=newi<n and 0<=newj<m:
if matrix[i][j]+1<matrix[newi][newj]:
matrix[newi][newj] = matrix[i][j]+1
tmp.add((newi,newj))
l = list(tmp)
return matrix
题意可知
将所有单词统计到一起 出现一次的就是不常见单词
def uncommonFromSentences(s1, s2):
"""
:type s1: str
:type s2: str
:rtype: List[str]
"""
ans = []
m = {}
for word in s1.split(" "):
m[word] = m.get(word,0)+1
for word in s2.split(" "):
m[word] = m.get(word,0)+1
for word in m:
if m[word]==1:
ans.append(word)
return ans