记录了初步解题思路 以及本地实现代码;并不一定为最优 也希望大家能一起探讨 一起进步
目录
1/16 1813. 句子相似性 III
从单次数量少的句子中插入后变成长的句子
短的句子为s1,长度为n1,长的句子为s2,长度为n2
从s1头开始与s2头比较 得到相同个数为l
从s1尾开始与s2尾比较 得到相同个数为r
如果l+r>=n1说明满足
def areSentencesSimilar(sentence1, sentence2):
"""
:type sentence1: str
:type sentence2: str
:rtype: bool
"""
s1,s2 = sentence1.split(" "),sentence2.split(" ")
if len(s1)>len(s2):
s1,s2=s2,s1
n1,n2=len(s1),len(s2)
same = True
for i in range(n1):
if s1[i]!=s2[i]:
same = False
break
l = i
if same:
return True
same = True
for j in range(n1):
if s1[n1-1-j]!=s2[n2-1-j]:
same = False
break
if l+j>=n1:
return True
return same or l+j>=n1
1/17 1814. 统计一个数组中好对子的数目
对于两个数a,b
满足a+rev(b)=b+rev(a)
则满足a-rev(a)=b-rev(b)
对每个数num求num-rev(num)
def countNicePairs(nums):
"""
:type nums: List[int]
:rtype: int
"""
MOD=10**9+7
m = {}
ans = 0
for num in nums:
x = int(str(num)[::-1])
ans += m.get(num-x,0)
m[num-x]+=1
return ans%MOD
1/18 1825. 求出 MK 平均值
队列q保存按次序输入的数值
l,r分别存储k个最小元素 k个最大元素 mid存储中间剩余元素
sum维护mid中数值之和
如果l,r中个数大于k则将最大/最小值 移到mid中
如果l,r中个数小于k则将mid中最小/最大值移入
from sortedcontainers import SortedList
from collections import deque
class MKAverage(object):
def __init__(self, m, k):
"""
:type m: int
:type k: int
"""
self.m = m
self.k = k
self.sum = 0
self.q = deque()
self.l = SortedList()
self.r = SortedList()
self.mid = SortedList()
def addElement(self, num):
"""
:type num: int
:rtype: None
"""
if not self.l or num<=self.l[-1]:
self.l.add(num)
elif not self.r or num>=self.r[0]:
self.r.add(num)
else:
self.mid.add(num)
self.sum+=num
self.q.append(num)
if len(self.q)>self.m:
v = self.q.popleft()
if v in self.l:
self.l.remove(v)
elif v in self.r:
self.r.remove(v)
else:
self.mid.remove(v)
self.sum-=v
while len(self.l)>self.k:
v = self.l.pop()
self.mid.add(v)
self.sum+=v
while len(self.r)>self.k:
v = self.r.pop(0)
self.mid.add(v)
self.sum+=v
while len(self.l)<self.k and self.mid:
v = self.mid.pop(0)
self.l.add(v)
self.sum-=v
while len(self.r)<self.k and self.mid:
v = self.mid.pop()
self.r.add(v)
self.sum-=v
def calculateMKAverage(self):
"""
:rtype: int
"""
if len(self.q)<self.m:
return -1
return self.sum//(self.m-2*self.k)
1/19 2299. 强密码检验器 II
依次判断各项条件
def strongPasswordCheckerII(password):
"""
:type password: str
:rtype: bool
"""
s = "!@#$%^&*()-+"
if len(password)<8:
return False
low,up,dig,sp = False,False,False,False
for i,c in enumerate(password):
if c.islower():
low = True
elif c.isupper():
up = True
elif c.isdigit():
dig = True
elif c in s:
sp = True
if i>0 and c==password[i-1]:
return False
return low and up and dig and sp
1/20 1817. 查找用户活跃分钟数
m[i] 存储用户i活跃的分钟 去除重复时间
遍历m内所有用户 统计其活跃分钟数
def findingUsersActiveMinutes(logs, k):
"""
:type logs: List[List[int]]
:type k: int
:rtype: List[int]
"""
ans = [0]*k
from collections import defaultdict
m = defaultdict(set)
for u,t in logs:
m[u].add(t)
for v in m.values():
ans[len(v)-1]+=1
return ans
1/21 1824. 最少侧跳次数
从头遍历 dp[0/1/2] 记录当前点 从头到达各个赛道最少横条次数
def minSideJumps(obstacles):
"""
:type obstacles: List[int]
:rtype: int
"""
ob = float('inf')
dp = [1,0,1]
for i in obstacles[1:]:
tmp = ob
for j in range(3):
if j == i-1:
dp[j] = ob
else:
tmp = min(tmp,dp[j])
for j in range(3):
if j!=i-1:
dp[j] = min(dp[j],tmp+1)
return min(dp)
1/22 1815. 得到新鲜甜甜圈的最多组数
batchSize<=10
统计每一组取size的余数
如果一组内人数是size的整数倍 余数为0 则这一组能够开心 且下一组也能开心
回溯 遍历每种余数的可能性
mem记忆化搜索 去重
def maxHappyGroups(batchSize, groups):
"""
:type batchSize: int
:type groups: List[int]
:rtype: int
"""
l = [0]*batchSize
for g in groups:
l[g%batchSize]+=1
mem = {}
def dfs(n,cnt):
if (n,cnt) in mem:
return mem[(n,cnt)]
res = 0
cur = 0
if n==0:
cur = 1
cnt = list(cnt)
for s,num in enumerate(cnt):
if num>0:
cnt[s]-=1
res = max(res,cur+dfs((n+s+1)%batchSize,tuple(cnt)))
cnt[s]+=1
mem[(n,tuple(cnt))]=res
return res
return l[0]+dfs(0,tuple(l[1:]))