链接: 6249. 分割圆的最少切割次数
class Solution:
def numberOfCuts(self, n: int) -> int:
if n == 1:
return 0
if n &1:
return n
return n//2
按题意模拟即可。
class Solution:
def onesMinusZeros(self, grid: List[List[int]]) -> List[List[int]]:
m,n = len(grid),len(grid[0])
cols = [0]*n
rows = [sum(r) for r in grid]
for i in range(m):
for j in range(n):
cols[j] += grid[i][j]
diff = [[0]*n for _ in range(m)]
for i in range(m):
for j in range(n):
diff[i][j] = rows[i]*2-m + cols[j]*2-n
return diff
链接: 6250. 商店的最少代价
class Solution:
def bestClosingTime(self, customers: str) -> int:
n = len(customers)
Y = customers.count('Y')
N = n - Y
a =b=0
ans = mn = inf
for i in range(n):
c = b+Y-a
if c < mn:
ans = i
mn = c
a+=customers[i]=='Y'
b+=customers[i]=='N'
c = b+Y-a
if c < mn:
ans = n
mn = c
return ans
链接: 6251. 统计回文子序列数目
MOD = 10**9+7
class Solution:
def countPalindromes(self, s: str) -> int:
s = list(map(int,s))
suf1 = [0]*10
suf2 = [0]*100
for v in s:
for d,c in enumerate(suf1):
suf2[d*10+v] += c
suf1[v] += 1
pre1 = [0]*10
pre2 = [0]*100
ans = 0
for v in s:
suf1[v] -= 1
for d,c in enumerate(suf1):
suf2[v*10+d] -= c
for i in range(10):
for j in range(10):
ans += suf2[i*10+j] * pre2[j*10+i]
ans %= MOD
for d,c in enumerate(pre1):
pre2[d*10+v] += c
pre1[v]+=1
return ans