LeetCode 题解工作台
范围中美丽整数的数目
给你正整数 low , high 和 k 。 如果一个数满足以下两个条件,那么它是 美丽的 : 偶数数位的数目与奇数数位的数目相同。 这个整数可以被 k 整除。 请你返回范围 [low, high] 中美丽整数的数目。 示例 1: 输入: low = 10, high = 20, k = 3 输出:…
2
题型
5
代码语言
3
相关题
当前训练重点
困难 · 状态·转移·动态规划
答案摘要
我们注意到,题目求的是区间 $[low, high]$ 内的美丽整数的个数,对于这种区间 的问题,我们通常可以考虑转化为求 $[1, r]$ 和 $[1, l-1]$ 的答案,然后相减即可。另外,题目中只涉及到不同数位之间的关系,而不涉及具体的数值,因此我们可以考虑使用数位 DP 来解决。 我们设计一个函数 $dfs(pos, mod, diff, lead, limit)$,表示当前处理到第 …
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 状态·转移·动态规划 题型思路
题目描述
给你正整数 low ,high 和 k 。
如果一个数满足以下两个条件,那么它是 美丽的 :
- 偶数数位的数目与奇数数位的数目相同。
- 这个整数可以被
k整除。
请你返回范围 [low, high] 中美丽整数的数目。
示例 1:
输入:low = 10, high = 20, k = 3 输出:2 解释:给定范围中有 2 个美丽数字:[12,18] - 12 是美丽整数,因为它有 1 个奇数数位和 1 个偶数数位,而且可以被 k = 3 整除。 - 18 是美丽整数,因为它有 1 个奇数数位和 1 个偶数数位,而且可以被 k = 3 整除。 以下是一些不是美丽整数的例子: - 16 不是美丽整数,因为它不能被 k = 3 整除。 - 15 不是美丽整数,因为它的奇数数位和偶数数位的数目不相等。 给定范围内总共有 2 个美丽整数。
示例 2:
输入:low = 1, high = 10, k = 1 输出:1 解释:给定范围中有 1 个美丽数字:[10] - 10 是美丽整数,因为它有 1 个奇数数位和 1 个偶数数位,而且可以被 k = 1 整除。 给定范围内总共有 1 个美丽整数。
示例 3:
输入:low = 5, high = 5, k = 2 输出:0 解释:给定范围中有 0 个美丽数字。 - 5 不是美丽整数,因为它的奇数数位和偶数数位的数目不相等。
提示:
0 < low <= high <= 1090 < k <= 20
解题思路
方法一:数位 DP
我们注意到,题目求的是区间 内的美丽整数的个数,对于这种区间 的问题,我们通常可以考虑转化为求 和 的答案,然后相减即可。另外,题目中只涉及到不同数位之间的关系,而不涉及具体的数值,因此我们可以考虑使用数位 DP 来解决。
我们设计一个函数 ,表示当前处理到第 位,当前数字模 的结果为 ,当前数字的奇偶数位差为 ,当前数字是否有前导零为 ,当前数字是否达到上界为 时的方案数。
函数 的执行逻辑如下:
如果 超出了 的长度,说明我们已经处理完了所有数位,如果此时 ,并且 ,说明当前数字满足题目要求,我们返回 ,否则返回 。
否则,我们计算得到当前数位的上界 ,然后在 范围内枚举当前数位的数字 :
- 如果 且 为真,说明当前数字只包含前导零,我们递归计算 的值并累加到答案中;
- 否则,我们根据 的奇偶性更新 的值,然后递归计算 的值并累加到答案中。
最终我们返回答案。
在主函数中,我们分别计算 和 的答案 和 ,最终答案为 。
时间复杂度 ,空间复杂度 ,其中 表示 数字的大小,而 表示数字集合。
相似题目:
class Solution:
def numberOfBeautifulIntegers(self, low: int, high: int, k: int) -> int:
@cache
def dfs(pos: int, mod: int, diff: int, lead: int, limit: int) -> int:
if pos >= len(s):
return mod == 0 and diff == 10
up = int(s[pos]) if limit else 9
ans = 0
for i in range(up + 1):
if i == 0 and lead:
ans += dfs(pos + 1, mod, diff, 1, limit and i == up)
else:
nxt = diff + (1 if i % 2 == 1 else -1)
ans += dfs(pos + 1, (mod * 10 + i) % k, nxt, 0, limit and i == up)
return ans
s = str(high)
a = dfs(0, 0, 10, 1, 1)
dfs.cache_clear()
s = str(low - 1)
b = dfs(0, 0, 10, 1, 1)
return a - b
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | and space complexity depend on the number of digits and k. The DP table has dimensions proportional to number of digits, parity difference, and modulo k, resulting in manageable memory and polynomial runtime relative to digit length and k. |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- question_mark
Candidate correctly identifies digit DP as a viable approach.
- question_mark
Shows understanding of state transitions for even/odd counts and modulo k.
- question_mark
Uses memoization effectively to handle large ranges up to 10^9.
常见陷阱
外企场景- error
Confusing digit parity counting or forgetting to track both even and odd digits.
- error
Neglecting the modulo k constraint when updating DP states.
- error
Failing to handle tight constraint correctly, causing overcounting beyond high.
进阶变体
外企场景- arrow_right_alt
Count numbers with more even digits than odd digits divisible by k.
- arrow_right_alt
Find numbers with alternating odd and even digits within a range divisible by k.
- arrow_right_alt
Compute numbers with exact digit sum divisible by k and balanced parity.