100095. 上一个遍历的整数
按题意模拟即可。
class Solution:
def lastVisitedIntegers(self, words: List[str]) -> List[int]:
ans = []
nums = []
k=0
for v in words:
if v[0].isdigit():
nums.append(int(v))
k = 0
else:
k += 1
if k > len(nums):
ans.append(-1)
else:
ans.append(nums[-k])
return ans
100078. 最长相邻不相等子序列 I
方法1
方法2
class Solution:
def getWordsInLongestSubsequence(self, n: int, words: List[str], groups: List[int]) -> List[str]:
def f(s):
ans = [0,[]]
for w, g in zip(words,groups):
if g == s:
ans[0]+=1
ans[1].append(w)
s ^= 1
return ans
return max(f(0),f(1))[1]
100077. 最长相邻不相等子序列 II
def han(s, t):
return sum(x!=y for x,y in zip(s,t))
class Solution:
def getWordsInLongestSubsequence(self, n: int, words: List[str], groups: List[int]) -> List[str]:
f = [1]*n
from_index = [-1]*n
for i, (w, g) in enumerate(zip(words,groups)):
for j in range(i):
if g != groups[j] and len(w) == len(words[j]) and han(words[j], w) == 1 and f[j] >= f[i]:
f[i] = f[j] + 1
from_index[i] = j
i = f.index(max(f))
ans = []
while i != -1:
ans.append(words[i])
i = from_index[i]
return ans[::-1]
100029. 和带限制的子多重集合的数目
MOD = 10**9 + 7
class Solution:
def countSubMultisets(self, nums: List[int], l: int, r: int) -> int:
cnt = Counter(nums)
f = [cnt[0]+1] + [0] * r
s = 0
for k, c in cnt.items():
if k == 0:continue
s += k * c
pre = [0]*k
for v in f:
pre.append((v+pre[-k])%MOD)
for j in range(min(s, r), 0, -1):
# f[j] += f[j-k]+..f[j-k*c]
f[j] += pre[j] - pre[max(j-k*c, 0)]
f[j] %= MOD
return sum(f[l:]) %MOD