题解 | 数独数组
数独数组
https://www.nowcoder.com/practice/12e6adfa05f5417dbf5a0d85ff5fb93c
import sys
from collections import Counter
def func(n,x):
min = n // 9
extra = n % 9
for i in range(1,10):
if x.count(i) < min:
return 0
if x.count(i) > min + 1:
return 0
return 1
data = sys.stdin.read().strip().split()
n = int(data[0])
a = list(map(int, data[1:1+n]))
if func(n,a) == 1:
print('YES')
else:
print('NO')
数度数组的本质:一个周期为9的循环排列的变形
需要考虑两种情况:
数组长度 = n
1、n是9的倍数——n=9*x
2、n不是9的倍数——n=9*x+y ,此时0<y<9
第一种情况正常讨论:每一个数字个数都相等即可
第二种情况需要注意,每个数字个数之差不能超过1,不然无法通过滑动窗口构建数独数组
查看11道真题和解析