Skip to content

Commit e038d33

Browse files
committed
✨feat: add 878 * 808
1 parent 34d7d91 commit e038d33

File tree

6 files changed

+156
-2
lines changed

6 files changed

+156
-2
lines changed

Index/二分.md

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
| [793. 阶乘函数后 K 个零](https://leetcode.cn/problems/preimage-size-of-factorial-zeroes-function/) | [LeetCode 题解链接](https://leetcode.cn/problems/preimage-size-of-factorial-zeroes-function/solution/by-ac_oier-pk9g/) | 困难 | 🤩🤩🤩🤩 |
4848
| [852. 山脉数组的峰顶索引](https://leetcode-cn.com/problems/peak-index-in-a-mountain-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/peak-index-in-a-mountain-array/solution/gong-shui-san-xie-er-fen-san-fen-cha-zhi-5gfv/) | 简单 | 🤩🤩🤩🤩🤩 |
4949
| [875. 爱吃香蕉的珂珂](https://leetcode.cn/problems/koko-eating-bananas/) | [LeetCode 题解链接](https://leetcode.cn/problems/koko-eating-bananas/solution/by-ac_oier-4z7i/) | 中等 | 🤩🤩🤩🤩 |
50+
| [878. 第 N 个神奇数字](https://leetcode.cn/problems/nth-magical-number/) | [LeetCode 题解链接](https://leetcode.cn/problems/nth-magical-number/solution/by-ac_oier-ln3b/) | 困难 | 🤩🤩🤩🤩🤩 |
5051
| [911. 在线选举](https://leetcode-cn.com/problems/online-election/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/online-election/solution/gong-shui-san-xie-er-fen-yun-yong-ti-by-5y3hi/) | 中等 | 🤩🤩🤩🤩🤩 |
5152
| [926. 将字符串翻转到单调递增](https://leetcode.cn/problems/flip-string-to-monotone-increasing/) | [LeetCode 题解链接](https://leetcode.cn/problems/flip-string-to-monotone-increasing/solution/by-ac_oier-h0we/) | 中等 | 🤩🤩🤩 |
5253
| [981. 基于时间的键值存储](https://leetcode-cn.com/problems/time-based-key-value-store/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/time-based-key-value-store/solution/gong-shui-san-xie-yi-ti-shuang-jie-ha-xi-h5et/) | 中等 | 🤩🤩🤩🤩 |

Index/容斥原理.md

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
| [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/) | 简单 | 🤩🤩🤩🤩🤩 |
2020
| [793. 阶乘函数后 K 个零](https://leetcode.cn/problems/preimage-size-of-factorial-zeroes-function/) | [LeetCode 题解链接](https://leetcode.cn/problems/preimage-size-of-factorial-zeroes-function/solution/by-ac_oier-pk9g/) | 困难 | 🤩🤩🤩🤩 |
2121
| [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/) | 中等 | 🤩🤩🤩🤩 |
22+
| [878. 第 N 个神奇数字](https://leetcode.cn/problems/nth-magical-number/) | [LeetCode 题解链接](https://leetcode.cn/problems/nth-magical-number/solution/by-ac_oier-ln3b/) | 困难 | 🤩🤩🤩🤩🤩 |
2223
| [926. 将字符串翻转到单调递增](https://leetcode.cn/problems/flip-string-to-monotone-increasing/) | [LeetCode 题解链接](https://leetcode.cn/problems/flip-string-to-monotone-increasing/solution/by-ac_oier-h0we/) | 中等 | 🤩🤩🤩🤩 |
2324
| [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/) | 中等 | 🤩🤩🤩 |
2425
| [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/) | 中等 | 🤩🤩🤩 |

Index/数学.md

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
| [828. 统计子串中的唯一字符](https://leetcode.cn/problems/count-unique-characters-of-all-substrings-of-a-given-string/) | [LeetCode 题解链接](https://leetcode.cn/problems/count-unique-characters-of-all-substrings-of-a-given-string/solution/by-ac_oier-922k/) | 困难 | 🤩🤩🤩🤩 |
5757
| [829. 连续整数求和](https://leetcode.cn/problems/consecutive-numbers-sum/) | [LeetCode 题解链接](https://leetcode.cn/problems/consecutive-numbers-sum/solution/by-ac_oier-220q/) | 困难 | 🤩🤩🤩🤩 |
5858
| [869. 重新排序得到 2 的幂](https://leetcode-cn.com/problems/reordered-power-of-2/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/reordered-power-of-2/solution/gong-shui-san-xie-yi-ti-shuang-jie-dfs-c-3s1e/) | 中等 | 🤩🤩🤩🤩 |
59+
| [878. 第 N 个神奇数字](https://leetcode.cn/problems/nth-magical-number/) | [LeetCode 题解链接](https://leetcode.cn/problems/nth-magical-number/solution/by-ac_oier-ln3b/) | 困难 | 🤩🤩🤩🤩 |
5960
| [879. 盈利计划](https://leetcode-cn.com/problems/profitable-schemes/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/profitable-schemes/solution/gong-shui-san-xie-te-shu-duo-wei-fei-yon-7su9/) | 困难 | 🤩🤩🤩🤩🤩 |
6061
| [891. 子序列宽度之和](https://leetcode.cn/problems/sum-of-subsequence-widths/) | [LeetCode 题解链接](https://leetcode.cn/problems/sum-of-subsequence-widths/solution/by-ac_oier-6tyk/) | 困难 | 🤩🤩🤩🤩 |
6162
| [907. 子数组的最小值之和](https://leetcode.cn/problems/sum-of-subarray-minimums/) | [LeetCode 题解链接](https://leetcode.cn/problems/sum-of-subarray-minimums/solution/by-ac_oier-h9cd/) | 中等 | 🤩🤩🤩 |

LeetCode/471-480/472. 连接词(困难).md

-1
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,3 @@ class Solution {
118118
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
119119

120120
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
121-

LeetCode/801-810/808. 分汤(中等).md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
这是 LeetCode 上的 **[808. 分汤](https://leetcode.cn/problems/soup-servings/solution/by-ac_oier-3n1h/)** ,难度为 **中等**
44

5-
Tag : 「数学」、「动态规划」
5+
Tag : 「数学」、「动态规划」、「线性 DP」
66

77

88

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[878. 第 N 个神奇数字](https://leetcode.cn/problems/nth-magical-number/solution/by-ac_oier-ln3b/)** ,难度为 **困难**
4+
5+
Tag : 「数学」、「容斥原理」、「二分」、「gcd」、「lcm」
6+
7+
8+
9+
一个正整数如果能被 `a``b` 整除,那么它是神奇的。
10+
11+
给定三个整数 `n``a` , `b` ,返回第 `n` 个神奇的数字。因为答案可能很大,所以返回答案 对 $10^9 + 7$ 取模 后的值。
12+
13+
示例 1:
14+
```
15+
输入:n = 1, a = 2, b = 3
16+
17+
输出:2
18+
```
19+
示例 2:
20+
```
21+
输入:n = 4, a = 2, b = 3
22+
23+
输出:6
24+
```
25+
26+
提示:
27+
* $1 <= n <= 10^9$
28+
* $2 <= a, b <= 4 \times 10^4$
29+
30+
---
31+
32+
### 数学
33+
34+
#### 提示一 : 从题面分析常见做法,从常见做法复杂度出发考虑其他做法
35+
36+
若不看数据范围,只看题面,容易想到的做法是「多路归并」:起始使用两个指针指向 `[a, 2a, 3a, ... ]``[b, 2b, 3b, ...]` 的开头,不断比较两指针所指向的数值大小,从而决定将谁后移,并不断更新顺位计数。
37+
38+
该做法常见,但其复杂度为 $O(n)$,对于本题 $n = 1e9$ 来说并不可行。
39+
40+
确定线性复杂度的做法不可行后,我们考虑是否存在对数复杂度的做法。
41+
42+
#### 提示二 : 如何考虑常见的对数复杂度做法,如何定义二段性
43+
44+
题目要我们求第 $n$ 个符合要求的数,假设我们想要通过「二分」来找该数值,那么我们需要分析其是否存在「二段性」。
45+
46+
假设在所有「能够被 `a``b` 整除的数」形成的数轴上,我们要找的分割点是 `k`,我们期望通过「二分」来找到 `k` 值,那么需要定义某种性质,使得 `k` 左边的数均满足该性质,`k` 右边的数均不满足该性质。
47+
48+
不难想到可根据题意来设定该性质:小于 `k` 的任意数字 `x` 满足在 $[0, x]$ 范围数的个数不足 `k` 个,而大于等于 `k` 的任意数字 `x` 则不满足该性质。
49+
50+
#### 提示三 : 如何实现高效的 `check` 函数
51+
52+
当确定使用「二分」来做时,剩下问题转化为:**如何快速得知某个 $[0, n]$ 中满足要求的数的个数。**
53+
54+
容易联想到「容斥原理」:**能被 `a``b` 整除的数的个数 = 能够被 `a` 整除的数的个数 + 能够被 `b` 整除的数的个数 - 既能被 `a` 又能被 `b` 整除的数的个数**
55+
56+
$$
57+
\left \lfloor \frac{n}{a} \right \rfloor + \left \lfloor \frac{n}{b} \right \rfloor - \left \lfloor \frac{n}{c} \right \rfloor
58+
$$
59+
60+
其中 `c``a``b` 的最小公倍数。
61+
62+
求解最小公倍数 `lcm` 需要实现最大公约数 `gcd`,两者模板分别为:
63+
64+
```Java []
65+
int gcd(int a, int b) {
66+
return b == 0 ? a : gcd(b, a % b);
67+
}
68+
int lcm(int a, int b) {
69+
return a * b / gcd(a, b);
70+
}
71+
```
72+
73+
#### 提示四 : 如何确定值域
74+
75+
一个合格的值域只需要确定答案在值域范围即可,因此我们可以直接定值域大小为 $1e20$。
76+
77+
或是根据 `a``b` 的取值来大致确定:假设两者中的较大值为 $m$,此时第 $n$ 个符合要求的数最大不会超过 $n \times m$,因此也可以设定值域大小为 $[0, 40000n]$。
78+
79+
Java 代码:
80+
```Java
81+
class Solution {
82+
int n, a, b, c;
83+
int gcd(int a, int b) {
84+
return b == 0 ? a : gcd(b, a % b);
85+
}
86+
public int nthMagicalNumber(int _n, int _a, int _b) {
87+
n = _n; a = _a; b = _b; c = a * b / gcd(a, b);
88+
long l = 0, r = (long)1e20;
89+
while (l < r) {
90+
long mid = l + r >> 1;
91+
if (check(mid) >= n) r = mid;
92+
else l = mid + 1;
93+
}
94+
return (int)(r % 1000000007);
95+
}
96+
long check(long x) {
97+
return x / a + x / b - x / c;
98+
}
99+
}
100+
```
101+
TypeScript 代码:
102+
```TypeScript
103+
function nthMagicalNumber(n: number, a: number, b: number): number {
104+
function gcd(a: number, b: number): number {
105+
return b == 0 ? a : gcd(b, a % b)
106+
}
107+
function check(x: number): number {
108+
return Math.floor(x / a) + Math.floor(x / b) - Math.floor(x / c)
109+
}
110+
const c = Math.floor(a * b / gcd(a, b))
111+
let l = 0, r = 1e19
112+
while (l < r) {
113+
const mid = Math.floor((l + r) / 2)
114+
if (check(mid) >= n) r = mid
115+
else l = mid + 1
116+
}
117+
return r % 1000000007
118+
}
119+
```
120+
Python3 代码:
121+
```Python
122+
class Solution:
123+
def nthMagicalNumber(self, n: int, a: int, b: int) -> int:
124+
def gcd(a, b):
125+
return a if b == 0 else gcd(b, a % b)
126+
def check(x):
127+
return x // a + x // b - x // c
128+
c = a * b // gcd(a, b)
129+
l, r = 0, 1e20
130+
while l < r:
131+
mid = (l + r) // 2
132+
if check(mid) >= n:
133+
r = mid
134+
else:
135+
l = mid + 1
136+
return int(r % 1000000007)
137+
```
138+
* 时间复杂度:$O(\log{N})$,其中 $N = 1e20$ 为值域大小
139+
* 空间复杂度:$O(1)$
140+
141+
---
142+
143+
### 最后
144+
145+
这是我们「刷穿 LeetCode」系列文章的第 `No.878` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
146+
147+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
148+
149+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
150+
151+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
152+

0 commit comments

Comments
 (0)