Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9962975

Browse files
✨feat: Add 1012 & Modify 357
1 parent 40cf234 commit 9962975

File tree

4 files changed

+176
-1
lines changed

4 files changed

+176
-1
lines changed

‎Index/容斥原理.md‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
| 题目 | 题解 | 难度 | 推荐指数 |
2+
| ------------------------------------------------------------ | ------------------------------------------------------------ | ---- | -------- |
3+
| [187. 重复的DNA序列](https://leetcode-cn.com/problems/repeated-dna-sequences/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/repeated-dna-sequences/solution/gong-shui-san-xie-yi-ti-shuang-jie-hua-d-30pg/) | 中等 | 🤩🤩🤩🤩 |
4+
| [304. 二维区域和检索 - 矩阵不可变](https://leetcode-cn.com/problems/range-sum-query-2d-immutable/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/range-sum-query-2d-immutable/solution/xia-ci-ru-he-zai-30-miao-nei-zuo-chu-lai-ptlo/) | 中等 | 🤩🤩🤩🤩🤩 |
5+
| [303. 区域和检索 - 数组不可变](https://leetcode-cn.com/problems/range-sum-query-immutable/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/range-sum-query-immutable/solution/sha-shi-qian-zhui-he-ya-tu-jie-qian-zhui-0rla/) | 简单 | 🤩🤩🤩🤩🤩 |
6+
| [307. 区域和检索 - 数组可修改](https://leetcode-cn.com/problems/range-sum-query-mutable/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/range-sum-query-mutable/solution/guan-yu-ge-lei-qu-jian-he-wen-ti-ru-he-x-41hv/) | 中等 | 🤩🤩🤩🤩🤩 |
7+
| [354. 俄罗斯套娃信封问题](https://leetcode-cn.com/problems/russian-doll-envelopes/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/russian-doll-envelopes/solution/zui-chang-shang-sheng-zi-xu-lie-bian-xin-6s8d/) | 困难 | 🤩🤩🤩 |
8+
| [357. 统计各位数字都不同的数字个数](https://leetcode-cn.com/problems/count-numbers-with-unique-digits/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/count-numbers-with-unique-digits/solution/by-ac_oier-6tfl/) | 中等 | 🤩🤩🤩🤩 |
9+
| [363. 矩形区域不超过 K 的最大数值和](https://leetcode-cn.com/problems/max-sum-of-rectangle-no-larger-than-k/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/max-sum-of-rectangle-no-larger-than-k/solution/gong-shui-san-xie-you-hua-mei-ju-de-ji-b-dh8s/) | 困难 | 🤩🤩🤩 |
10+
| [437. 路径总和 III](https://leetcode-cn.com/problems/path-sum-iii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/path-sum-iii/solution/gong-shui-san-xie-yi-ti-shuang-jie-dfs-q-usa7/) | 中等 | 🤩🤩🤩🤩 |
11+
| [523. 连续的子数组和](https://leetcode-cn.com/problems/continuous-subarray-sum/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/continuous-subarray-sum/solution/gong-shui-san-xie-tuo-zhan-wei-qiu-fang-1juse/) | 中等 | 🤩🤩🤩🤩 |
12+
| [525. 连续数组](https://leetcode-cn.com/problems/contiguous-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/contiguous-array/solution/gong-shui-san-xie-qian-zhui-he-ha-xi-bia-q400/) | 中等 | 🤩🤩🤩🤩 |
13+
| [528. 按权重随机选择](https://leetcode-cn.com/problems/random-pick-with-weight/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/random-pick-with-weight/solution/gong-shui-san-xie-yi-ti-shuang-jie-qian-8bx50/) | 中等 | 🤩🤩🤩🤩 |
14+
| [600. 不含连续1的非负整数](https://leetcode-cn.com/problems/non-negative-integers-without-consecutive-ones/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/non-negative-integers-without-consecutive-ones/solution/gong-shui-san-xie-jing-dian-shu-wei-dp-y-mh92/) | 困难 | 🤩🤩🤩🤩 |
15+
| [629. K个逆序对数组](https://leetcode-cn.com/problems/k-inverse-pairs-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/k-inverse-pairs-array/solution/gong-shui-san-xie-yi-dao-xu-lie-dp-zhuan-tm01/) | 中等 | 🤩🤩🤩🤩🤩 |
16+
| [661. 图片平滑器](https://leetcode-cn.com/problems/image-smoother/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/image-smoother/solution/by-ac_oier-nn3v/) | 简单 | 🤩🤩🤩🤩 |
17+
| [673. 最长递增子序列的个数](https://leetcode-cn.com/problems/number-of-longest-increasing-subsequence/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/number-of-longest-increasing-subsequence/solution/gong-shui-san-xie-lis-de-fang-an-shu-wen-obuz/) | 中等 | 🤩🤩🤩🤩 |
18+
| [689. 三个无重叠子数组的最大和](https://leetcode-cn.com/problems/maximum-sum-of-3-non-overlapping-subarrays/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximum-sum-of-3-non-overlapping-subarrays/solution/gong-shui-san-xie-jie-he-qian-zhui-he-de-ancx/) | 困难 | 🤩🤩🤩 |
19+
| [724. 寻找数组的中心下标](https://leetcode-cn.com/problems/find-pivot-index/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/find-pivot-index/solution/shi-yong-shao-bing-ji-qiao-liang-bian-qi-vkju/) | 简单 | 🤩🤩🤩🤩🤩 |
20+
| [825. 适龄的朋友](https://leetcode-cn.com/problems/friends-of-appropriate-ages/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/friends-of-appropriate-ages/solution/gong-shui-san-xie-yi-ti-shuang-jie-pai-x-maa8/) | 中等 | 🤩🤩🤩🤩 |
21+
| [930. 和相同的二元子数组](https://leetcode-cn.com/problems/binary-subarrays-with-sum/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/binary-subarrays-with-sum/solution/gong-shui-san-xie-yi-ti-shuang-jie-qian-hfoc0/) | 中等 | 🤩🤩🤩 |
22+
| [1004. 最大连续1的个数 III](https://leetcode-cn.com/problems/max-consecutive-ones-iii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/max-consecutive-ones-iii/solution/san-chong-jie-fa-cong-dong-tai-gui-hua-d-gxks/) | 中等 | 🤩🤩🤩 |
23+
| [1074. 元素和为目标值的子矩阵数量](https://leetcode-cn.com/problems/number-of-submatrices-that-sum-to-target/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/number-of-submatrices-that-sum-to-target/solution/gong-shui-san-xie-you-hua-mei-ju-de-ji-b-uttw/) | 困难 | 🤩🤩🤩 |
24+
| [1012. 至少有 1 位重复的数字](https://leetcode-cn.com/problems/numbers-with-repeated-digits/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/numbers-with-repeated-digits/solution/by-ac_oier-2szj/) | 困难 | 🤩🤩🤩 |
25+
| [1154. 一年中的第几天](https://leetcode-cn.com/problems/day-of-the-year/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/day-of-the-year/solution/gong-shui-san-xie-jian-dan-qian-zhui-he-lwo2g/) | 简单 | 🤩🤩🤩🤩 |
26+
| [1208. 尽可能使字符串相等](https://leetcode-cn.com/problems/get-equal-substrings-within-budget/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/get-equal-substrings-within-budget/solution/ni-bu-ke-neng-kan-bu-dong-de-qian-zhui-h-u4l1/) | 中等 | 🤩🤩🤩 |
27+
| [1310. 子数组异或查询](https://leetcode-cn.com/problems/xor-queries-of-a-subarray/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/xor-queries-of-a-subarray/solution/gong-shui-san-xie-yi-ti-shuang-jie-shu-z-rcgu/) | 中等 | 🤩🤩🤩🤩 |
28+
| [1442. 形成两个异或相等数组的三元组数目](https://leetcode-cn.com/problems/count-triplets-that-can-form-two-arrays-of-equal-xor/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/count-triplets-that-can-form-two-arrays-of-equal-xor/solution/gong-shui-san-xie-xiang-jie-shi-yong-qia-7gzm/) | 中等 | 🤩🤩🤩 |
29+
| [1480. 一维数组的动态和](https://leetcode-cn.com/problems/running-sum-of-1d-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/running-sum-of-1d-array/solution/gong-shui-san-xie-yi-wei-qian-zhui-he-mo-g8hn/) | 简单 | 🤩🤩🤩🤩🤩 |
30+
| [1588. 所有奇数长度子数组的和](https://leetcode-cn.com/problems/sum-of-all-odd-length-subarrays/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/sum-of-all-odd-length-subarrays/solution/gong-shui-san-xie-yi-ti-shuang-jie-qian-18jq3/) | 简单 | 🤩🤩🤩🤩🤩 |
31+
| [1738. 找出第 K 大的异或坐标值](https://leetcode-cn.com/problems/find-kth-largest-xor-coordinate-value/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/find-kth-largest-xor-coordinate-value/solution/gong-shui-san-xie-xiang-jie-li-yong-er-w-ai0d/) | 中等 | 🤩🤩🤩 |
32+
| [1744. 你能在你最喜欢的那天吃到你最喜欢的糖果吗?](https://leetcode-cn.com/problems/can-you-eat-your-favorite-candy-on-your-favorite-day/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/can-you-eat-your-favorite-candy-on-your-favorite-day/solution/gong-shui-san-xie-qian-zhui-he-qiu-jie-c-b38y/) | 中等 | 🤩🤩🤩🤩🤩 |
33+
| [1749. 任意子数组和的绝对值的最大值](https://leetcode-cn.com/problems/maximum-absolute-sum-of-any-subarray/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximum-absolute-sum-of-any-subarray/solution/xiang-jie-qian-zhui-he-jie-fa-fen-xi-si-yibby/) | 中等 | 🤩🤩🤩 |
34+
| [1838. 最高频元素的频数](https://leetcode-cn.com/problems/frequency-of-the-most-frequent-element/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/frequency-of-the-most-frequent-element/solution/gong-shui-san-xie-cong-mei-ju-dao-pai-xu-kxnk/) | 中等 | 🤩🤩🤩 |
35+
| [1893. 检查是否区域内所有整数都被覆盖](https://leetcode-cn.com/problems/check-if-all-the-integers-in-a-range-are-covered/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/check-if-all-the-integers-in-a-range-are-covered/solution/gong-shui-san-xie-yi-ti-shuang-jie-mo-ni-j83x/) | 简单 | 🤩🤩🤩🤩 |
36+
| [1894. 找到需要补充粉笔的学生编号](https://leetcode-cn.com/problems/find-the-student-that-will-replace-the-chalk/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/find-the-student-that-will-replace-the-chalk/solution/gong-shui-san-xie-yi-ti-shuang-jie-qian-kpqsk/) | 中等 | 🤩🤩🤩🤩 |
37+
| [2055. 蜡烛之间的盘子](https://leetcode-cn.com/problems/plates-between-candles/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/plates-between-candles/solution/gong-shui-san-xie-er-fen-qian-zhui-he-yu-0qt0/) | 中等 | 🤩🤩🤩🤩 |
38+
| [2100. 适合打劫银行的日子](https://leetcode-cn.com/problems/find-good-days-to-rob-the-bank/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/find-good-days-to-rob-the-bank/solution/gong-shui-san-xie-qian-zhui-he-yun-yong-gf604/) | 中等 | 🤩🤩🤩🤩 |
39+

‎Index/数位 DP.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
| 题目 | 题解 | 难度 | 推荐指数 |
22
| ------------------------------------------------------------ | ------------------------------------------------------------ | ---- | -------- |
3+
| [357. 统计各位数字都不同的数字个数](https://leetcode-cn.com/problems/count-numbers-with-unique-digits/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/count-numbers-with-unique-digits/solution/by-ac_oier-6tfl/) | 中等 | 🤩🤩🤩🤩🤩 |
34
| [600. 不含连续1的非负整数](https://leetcode-cn.com/problems/non-negative-integers-without-consecutive-ones/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/non-negative-integers-without-consecutive-ones/solution/gong-shui-san-xie-jing-dian-shu-wei-dp-y-mh92/) | 困难 | 🤩🤩🤩🤩🤩 |
5+
| [1012. 至少有 1 位重复的数字](https://leetcode-cn.com/problems/numbers-with-repeated-digits/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/numbers-with-repeated-digits/solution/by-ac_oier-2szj/) | 困难 | 🤩🤩🤩🤩🤩 |
46

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[1012. 至少有 1 位重复的数字](https://leetcode-cn.com/problems/numbers-with-repeated-digits/solution/by-ac_oier-2szj/)** ,难度为 **困难**
4+
5+
Tag : 「动态规划」、「数位 DP」、「容斥原理」
6+
7+
8+
9+
给定正整数 $n,ドル返回在 $[1, n]$ 范围内具有 至少 1 位 重复数字的正整数的个数。
10+
11+
示例 1:
12+
```
13+
输入:n = 20
14+
15+
输出:1
16+
17+
解释:具有至少 1 位重复数字的正数(<= 20)只有 11 。
18+
```
19+
示例 2:
20+
```
21+
输入:n = 100
22+
23+
输出:10
24+
25+
解释:具有至少 1 位重复数字的正数(<= 100)有 11,22,33,44,55,66,77,88,99 和 100 。
26+
```
27+
示例 3:
28+
```
29+
输入:n = 1000
30+
31+
输出:262
32+
```
33+
34+
提示:
35+
* 1ドル <= n <= 10^9$
36+
37+
---
38+
39+
### 数位 DP
40+
41+
**首先 $[1, n]$ 共有 $n$ 个数,而求解 $[1, n]$ 范围内有多少个「至少有 1ドル$ 位重复数字」的数的个数,等价于「总数 $n$」减去「没有重复数的数的个数」。**
42+
43+
于是问题转换为:**如何求解「没有重复数」的数的个数,同时为了更具一般性,我们将求解 $[1, n]$ 范围内「没有重复数」的数的个数,修改为求解 $[0, n]$ 范围内「没有重复数」的数的个数**
44+
45+
>即将问题转换为 [(题解) 357. 统计各位数字都不同的数字个数](https://leetcode-cn.com/problems/count-numbers-with-unique-digits/solution/by-ac_oier-6tfl/) 中的进阶部分。
46+
47+
假定我们存在函数 `int dp(int x)` 函数,能够返回区间 $[0, x]$ 内合法数的个数,那么配合「容斥原理」我们便能够回答任意区间合法数的查询:
48+
49+
$$
50+
ans_{(l, r)} = dp(r) - dp(l - 1)
51+
$$
52+
53+
然后考虑如何实现 `int dp(int x)` 函数,我们将组成 $[0, x]$ 的合法数分成三类:
54+
* 位数和 $x$ 相同,且最高位比 $x$ 最高位要小的,这部分统计为 `res1`;
55+
* 位数和 $x$ 相同,且最高位与 $x$ 最高位相同的,这部分统计为 `res2`;
56+
* 位数比 $x$ 少,这部分统计为 `res3`
57+
58+
其中 `res1``res3` 求解相对简单,重点落在如何求解 `res2` 上。
59+
60+
**对 $x$ 进行「从高到低」的处理(假定 $x$ 数位为 $n$),对于第 $k$ 位而言($k$ 不为最高位),假设在 $x$ 中第 $k$ 位为 $cur,ドル那么为了满足「大小限制」关系,我们只能在 $[0, cur - 1]$ 范围内取数,同时为了满足「相同数字只能使用一次」的限制,我们需要使用一个 `int` 变量 $s$ 来记录使用情况(用 $s$ 的低十位来代指数字 $[0, 9]$ 是否被使用),统计 $[0, cur - 1]$ 范围内同时符合两个限制条件的数的个数,记为 $cnt$。**
61+
62+
**当第 $k$ 位有 $cnt$ 种合法选择之后,后面的位数可以在满足「相同数字只能使用一次」的限制条件下任意选择(因为大小关系已经由第 $k$ 位保证),为了快速知道剩下的 $n - k$ 位有多少种方案,我们还需要预处理乘积数组,其中 $f[l][r]$ 代表 $l * (l + 1) * ... * (j - 1) * j$ 的乘积之和。**
63+
64+
> 上述讲解若是觉得抽象,我们可以举个 🌰,假设 $x = 678,ドル我们该如何求解 `res2`:由于限定了 `res2` 为「位数和 $x$ 相同,且最高位与 $x$ 最高位相同的」的合法数个数,因此最高位没有选,只能是 6ドル,ドル然后考虑处理次高位,次高位在 $x$ 中为 7ドル,ドル为了满足大小关系,我们只能在 $[0, 6]$ 范围内做限制,同时由于 6ドル$ 已用过,因此次高位实际只有 $[0, 5],ドル共 6ドル$ 种选择,当确定次高位后,后面的位数任意取,由于前面已经填充了 $p = 2$ 位(即消耗了 $p$ 个不同数字),因此从后面的位数开始应该是 $a = 10 - p$ 开始往后自减累乘到 $b = (10 - p) - (n - p) + 1$ 为止,即此时方案数为 $cnt * f[b][a]$(当前位不是最低位)或者 $cnt$(当前位是最低位)。按照此逻辑循环处理所有位数即可,直到遇到重复数值或正常结束。
65+
66+
需要说明的是,上述的举例部分只是为方便大家理解过程,看懂了举例部分不代表理解了数位 DP 做法成立的内在条件,阅读的重点还是要放在前面加粗字体部分,只会使用样例理解算法永远不是科学的做法。
67+
68+
其他细节:乘积数组的预处理与样例无关,我们可以使用 `static` 进行打表优化,同时可以将 `res1``res2` 两种情况进行合并。
69+
70+
代码:
71+
```Java
72+
class Solution {
73+
// f[l][r] 代表 i * (i + 1) * ... * (j - 1) * j
74+
static int[][] f = new int[10][10];
75+
static {
76+
for (int i = 1; i < 10; i++) {
77+
for (int j = i; j < 10; j++) {
78+
int cur = 1;
79+
for (int k = i; k <= j; k++) cur *= k;
80+
f[i][j] = cur;
81+
}
82+
}
83+
}
84+
int dp(int x) {
85+
int t = x;
86+
List<Integer> nums = new ArrayList<>();
87+
while (t != 0) {
88+
nums.add(t % 10);
89+
t /= 10;
90+
}
91+
int n = nums.size();
92+
if (n <= 1) return x + 1; // [0, 9]
93+
// 位数和 x 相同(res1 + res2)
94+
int ans = 0;
95+
for (int i = n - 1, p = 1, s = 0; i >= 0; i--, p++) {
96+
int cur = nums.get(i), cnt = 0;
97+
for (int j = cur - 1; j >= 0; j--) {
98+
if (i == n - 1 && j == 0) continue;
99+
if (((s >> j) & 1) == 0) cnt++;
100+
}
101+
int a = 10 - p, b = a - (n - p) + 1;
102+
ans += b <= a ? cnt * f[b][a] : cnt;
103+
if (((s >> cur) & 1) == 1) break;
104+
s |= (1 << cur);
105+
if (i == 0) ans++;
106+
}
107+
// 位数比 x 少(res3)
108+
ans += 10;
109+
for (int i = 2, last = 9; i < n; i++) {
110+
int cur = last * (10 - i + 1);
111+
ans += cur; last = cur;
112+
}
113+
return ans;
114+
}
115+
public int numDupDigitsAtMostN(int n) {
116+
return (n + 1) - dp(n);
117+
}
118+
}
119+
```
120+
* 时间复杂度:$O(\log{n})$
121+
* 空间复杂度:$O(1)$
122+
123+
---
124+
125+
### 最后
126+
127+
这是我们「刷穿 LeetCode」系列文章的第 `No.1012` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
128+
129+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
130+
131+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
132+
133+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
134+

‎LeetCode/351-360/357. 统计各位数字都不同的数字个数(中等).md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
这是 LeetCode 上的 **[357. 统计各位数字都不同的数字个数](https://leetcode-cn.com/problems/count-numbers-with-unique-digits/solution/by-ac_oier-6tfl/)** ,难度为 **中等**
44

5-
Tag : 「数学」
5+
Tag : 「数学」、「容斥原理」、「数位 DP」
66

77

88

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /