链接: 6148. 矩阵中的局部最大值
定级Easy。
按题意暴力模拟即可。
class Solution:
def largestLocal(self, grid: List[List[int]]) -> List[List[int]]:
m,n = len(grid),len(grid[0])
ans = [[0]* n for _ in range(m)]
for i in range(1,m-1):
for j in range(1,n-1):
ans[i][j] = max(grid[i-1][j-1:j+2]+grid[i][j-1:j+2]+grid[i+1][j-1:j+2])
return [ans[i][1:n-1] for i in range(1,m-1)]
链接: 6149. 边积分最高的节点
一开始看成所有路径指向它的点,其实是直接指向,看邻边累加即可。
class Solution:
def edgeScore(self, edges: List[int]) -> int:
n = len(edges)
f = [0] * n
vis = [0] * n
for u,v in enumerate(edges):
f[v] += u
# print(f)
ans = 0
for i in range(1,n):
if f[i]>f[ans]:
ans = i
return ans
链接: 6150. 根据模式串构造最小数字
定级Medium。
class Solution:
def smallestNumber(self, pattern: str) -> str:
n = len(pattern)+1
from sortedcontainers import SortedList
s = SortedList(range(1,n+1))
r = [0]*n
for i in range(n-2,-1,-1):
if pattern[i] == 'D':
r[i] = r[i+1] +1
else:
r[i] = 0
ans=[0]*n
for i in range(n):
if r[i]<=0:
ans[i] = s.pop(0)
# s.popleft()
else:
ans[i] = s.pop(r[i])
return ''.join(map(str,ans))
链接: 6151. 统计特殊整数
定级Hard。
数位dp,参考力扣 357. 统计各位数字都不同的数字个数.
@cache
def f(i,mask,is_limit,is_num):
数位DP通用模板
class Solution:
def countSpecialNumbers(self, x: int) -> int:
s = str(x)
n = len(s)
@cache
def f(i,mask,is_limit,is_num):
if i == n:
return int(is_num)
ans = 0
if not is_num:
ans += f(i+1,mask,False,False)
r = int(s[i]) if is_limit else 9
l = 0 if is_num else 1
for j in range(l,r+1):
if not mask>>j&1:
ans += f(i+1,mask|(1<<j),is_limit and j == r,True)
return ans
return f(0,0,True,False)
class Solution:
def countSpecialNumbers(self, x: int) -> int:
f = [[0]*10 for _ in range(10)]
for i in range(1,10):
for j in range(1,10):
cur = 1
for k in range(i,j+1):
cur *= k
f[i][j] = cur
t = x
nums = []
while t:
nums.append(t%10)
t //= 10
n = len(nums)
if n<=1:
return x
ans = 0
p = 1
s = 0
for i in range(n-1,-1,-1):
cur = nums[i]
cnt = 0
for j in range(cur-1,-1,-1):
if i == n-1 and j == 0:
continue
if ((s>>j)&1) == 0:
cnt += 1
a = 10-p
b = a-(n-p)+1
ans += (cnt * f[b][a] if b<=a else cnt)
if ((s>>cur)&1) == 1:
break
s |= 1<<cur
if i == 0:
ans += 1
p+=1
ans += 10
last = 9
for i in range(2,n):
cur = last*(10-i+1)
ans += cur
last = cur
return ans-1