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 93169b2

Browse files
✨feat: add 878、1742
1 parent e038d33 commit 93169b2

File tree

4 files changed

+126
-5
lines changed

4 files changed

+126
-5
lines changed

‎Index/哈希表.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
| [1640. 能否连接形成数组](https://leetcode.cn/problems/check-array-formation-through-concatenation/) | [LeetCode 题解链接](https://leetcode.cn/problems/check-array-formation-through-concatenation/solution/by-ac_oier-3jqf/) | 简单 | 🤩🤩🤩🤩🤩 |
8282
| [1700. 无法吃午餐的学生数量](https://leetcode.cn/problems/number-of-students-unable-to-eat-lunch/) | [LeetCode 题解链接](https://leetcode.cn/problems/number-of-students-unable-to-eat-lunch/solution/by-ac_oier-rvc3/) | 简单 | 🤩🤩🤩🤩🤩 |
8383
| [1711. 大餐计数](https://leetcode-cn.com/problems/count-good-meals/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/count-good-meals/solution/gong-shui-san-xie-xiang-jie-san-chong-gu-nn4f/) | 中等 | 🤩🤩🤩 |
84+
| [1742. 盒子中小球的最大数量](https://leetcode.cn/problems/maximum-number-of-balls-in-a-box/) | [LeetCode 题解链接](https://leetcode.cn/problems/maximum-number-of-balls-in-a-box/solution/by-ac_oier-3mxf/) | 简单 | 🤩🤩🤩 |
8485
| [1743. 从相邻元素对还原数组](https://leetcode-cn.com/problems/restore-the-array-from-adjacent-pairs/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/restore-the-array-from-adjacent-pairs/solution/gong-shui-san-xie-yi-ti-shuang-jie-dan-x-elpx/) | 中等 | 🤩🤩🤩🤩 |
8586
| [1748. 唯一元素的和](https://leetcode-cn.com/problems/sum-of-unique-elements/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/sum-of-unique-elements/solution/gong-shui-san-xie-yi-ti-shuang-jie-pai-x-atnd/) | 简单 | 🤩🤩🤩🤩 |
8687
| [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/) | 中等 | 🤩🤩🤩 |

‎Index/模拟.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@
206206
| [1732. 找到最高海拔](https://leetcode.cn/problems/find-the-highest-altitude/) | [LeetCode 题解链接](https://leetcode.cn/problems/find-the-highest-altitude/solution/by-ac_oier-a0j8/) | 简单 | 🤩🤩🤩🤩 |
207207
| [1736. 替换隐藏数字得到的最晚时间](https://leetcode-cn.com/problems/latest-time-by-replacing-hidden-digits/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/latest-time-by-replacing-hidden-digits/solution/gong-shui-san-xie-ti-huan-yin-cang-shu-z-2l1h/) | 简单 | 🤩🤩🤩🤩 |
208208
| [1737. 满足三条件之一需改变的最少字符数](https://leetcode.cn/problems/change-minimum-characters-to-satisfy-one-of-three-conditions/) | [LeetCode 题解链接](https://leetcode.cn/problems/change-minimum-characters-to-satisfy-one-of-three-conditions/solution/by-ac_oier-vs5u/) | 中等 | 🤩🤩🤩🤩 |
209+
| [1742. 盒子中小球的最大数量](https://leetcode.cn/problems/maximum-number-of-balls-in-a-box/) | [LeetCode 题解链接](https://leetcode.cn/problems/maximum-number-of-balls-in-a-box/solution/by-ac_oier-3mxf/) | 简单 | 🤩🤩🤩 |
209210
| [1743. 从相邻元素对还原数组](https://leetcode-cn.com/problems/restore-the-array-from-adjacent-pairs/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/restore-the-array-from-adjacent-pairs/solution/gong-shui-san-xie-yi-ti-shuang-jie-dan-x-elpx/) | 中等 | 🤩🤩🤩🤩 |
210211
| [1748. 唯一元素的和](https://leetcode-cn.com/problems/sum-of-unique-elements/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/sum-of-unique-elements/solution/gong-shui-san-xie-yi-ti-shuang-jie-pai-x-atnd/) | 简单 | 🤩🤩🤩🤩 |
211212
| [1763. 最长的美好子字符串](https://leetcode-cn.com/problems/longest-nice-substring/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/longest-nice-substring/solution/cong-shu-ju-fan-wei-xuan-ze-he-gua-suan-n3y2a/) | 简单 | 🤩🤩🤩 |
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[1742. 盒子中小球的最大数量](https://leetcode.cn/problems/maximum-number-of-balls-in-a-box/solution/by-ac_oier-3mxf/)** ,难度为 **简单**
4+
5+
Tag : 「哈希表」、「模拟」
6+
7+
8+
9+
你在一家生产小球的玩具厂工作,有 `n` 个小球,编号从 `lowLimit` 开始,到 `highLimit` 结束(包括 `lowLimit``highLimit` ,即 `n == highLimit - lowLimit + 1`)。另有无限数量的盒子,编号从 `1``infinity`
10+
11+
你的工作是将每个小球放入盒子中,其中盒子的编号应当等于小球编号上每位数字的和。例如,编号 `321` 的小球应当放入编号 `3 + 2 + 1 = 6` 的盒子,而编号 `10` 的小球应当放入编号 `1 + 0 = 1` 的盒子。
12+
13+
给你两个整数 `lowLimit``highLimit` ,返回放有最多小球的盒子中的小球数量。如果有多个盒子都满足放有最多小球,只需返回其中任一盒子的小球数量。
14+
15+
示例 1:
16+
```
17+
输入:lowLimit = 1, highLimit = 10
18+
输出:2
19+
20+
解释:
21+
盒子编号:1 2 3 4 5 6 7 8 9 10 11 ...
22+
小球数量:2 1 1 1 1 1 1 1 1 0 0 ...
23+
编号 1 的盒子放有最多小球,小球数量为 2 。
24+
```
25+
示例 2:
26+
```
27+
输入:lowLimit = 5, highLimit = 15
28+
29+
输出:2
30+
31+
解释:
32+
盒子编号:1 2 3 4 5 6 7 8 9 10 11 ...
33+
小球数量:1 1 1 1 2 2 1 1 1 0 0 ...
34+
编号 5 和 6 的盒子放有最多小球,每个盒子中的小球数量都是 2 。
35+
```
36+
示例 3:
37+
```
38+
输入:lowLimit = 19, highLimit = 28
39+
40+
输出:2
41+
42+
解释:
43+
盒子编号:1 2 3 4 5 6 7 8 9 10 11 12 ...
44+
小球数量:0 1 1 1 1 1 1 1 1 2 0 0 ...
45+
编号 10 的盒子放有最多小球,小球数量为 2 。
46+
```
47+
48+
提示:
49+
* 1ドル <= lowLimit <= highLimit <= 10^5$
50+
51+
---
52+
53+
### 模拟
54+
55+
数据范围 $n = 1e5,ドル因此最大盒子编号 `99999 = 5 * 9 = 45`,我们可以用一个大小为 50ドル$ 的数组 `cnts` 来统计每个编号盒子中小球的数量,$cnts[idx] = x$ 含义为编号为 $idx$ 的盒子有 $x$ 个小球。
56+
57+
Java 代码:
58+
```Java
59+
class Solution {
60+
public int countBalls(int l, int r) {
61+
int ans = 0;
62+
int[] cnts = new int[50];
63+
for (int i = l; i <= r; i++) {
64+
int j = i, cur = 0;
65+
while (j != 0) {
66+
cur += j % 10; j /= 10;
67+
}
68+
if (++cnts[cur] > ans) ans = cnts[cur];
69+
}
70+
return ans;
71+
}
72+
}
73+
```
74+
TypeScript 代码:
75+
```TypeScript
76+
function countBalls(l: number, r: number): number {
77+
let ans = 0
78+
const cnts = new Array<number>(50).fill(0)
79+
for (let i = l; i <= r; i++) {
80+
let j = i, cur = 0
81+
while (j != 0) {
82+
cur += j % 10
83+
j = Math.floor(j / 10)
84+
}
85+
if (++cnts[cur] > ans) ans = cnts[cur]
86+
}
87+
return ans
88+
}
89+
```
90+
Python 代码:
91+
```Python
92+
class Solution:
93+
def countBalls(self, l: int, r: int) -> int:
94+
ans = 0
95+
cnts = [0] * 50
96+
for i in range(l, r + 1):
97+
j, cur = i, 0
98+
while j != 0:
99+
j, cur = j // 10, cur + j % 10
100+
cnts[cur] += 1
101+
if cnts[cur] > ans:
102+
ans = cnts[cur]
103+
return ans
104+
```
105+
* 时间复杂度:$O(n\log{r})$
106+
* 空间复杂度:$O(C),ドル其中 $C = 50$ 为最大盒子编号
107+
108+
---
109+
110+
### 最后
111+
112+
这是我们「刷穿 LeetCode」系列文章的第 `No.1742` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
113+
114+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
115+
116+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
117+
118+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
119+

‎LeetCode/871-880/878. 第 N 个神奇数字(困难).md‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ int lcm(int a, int b) {
7272

7373
#### 提示四 : 如何确定值域
7474

75-
一个合格的值域只需要确定答案在值域范围即可,因此我们可以直接定值域大小为 $1e20$。
75+
一个合格的值域只需要确定答案在值域范围即可,因此我们可以直接定值域大小为 $1e18$。
7676

7777
或是根据 `a``b` 的取值来大致确定:假设两者中的较大值为 $m,ドル此时第 $n$ 个符合要求的数最大不会超过 $n \times m,ドル因此也可以设定值域大小为 $[0, 40000n]$。
7878

@@ -85,7 +85,7 @@ class Solution {
8585
}
8686
public int nthMagicalNumber(int _n, int _a, int _b) {
8787
n = _n; a = _a; b = _b; c = a * b / gcd(a, b);
88-
long l = 0, r = (long)1e20;
88+
long l = 0, r = (long)1e18;
8989
while (l < r) {
9090
long mid = l + r >> 1;
9191
if (check(mid) >= n) r = mid;
@@ -108,7 +108,7 @@ function nthMagicalNumber(n: number, a: number, b: number): number {
108108
return Math.floor(x / a) + Math.floor(x / b) - Math.floor(x / c)
109109
}
110110
const c = Math.floor(a * b / gcd(a, b))
111-
let l = 0, r = 1e19
111+
let l = 0, r = 1e18
112112
while (l < r) {
113113
const mid = Math.floor((l + r) / 2)
114114
if (check(mid) >= n) r = mid
@@ -126,7 +126,7 @@ class Solution:
126126
def check(x):
127127
return x // a + x // b - x // c
128128
c = a * b // gcd(a, b)
129-
l, r = 0, 1e20
129+
l, r = 0, 1e18
130130
while l < r:
131131
mid = (l + r) // 2
132132
if check(mid) >= n:
@@ -135,7 +135,7 @@ class Solution:
135135
l = mid + 1
136136
return int(r % 1000000007)
137137
```
138-
* 时间复杂度:$O(\log{N}),ドル其中 $N = 1e20$ 为值域大小
138+
* 时间复杂度:$O(\log{N}),ドル其中 $N = 1e18$ 为值域大小
139139
* 空间复杂度:$O(1)$
140140

141141
---

0 commit comments

Comments
(0)

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