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 f5d9d76

Browse files
✨feat: add 1759、1760、1764
1 parent 08f180c commit f5d9d76

File tree

6 files changed

+300
-36
lines changed

6 files changed

+300
-36
lines changed

‎Index/二分.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
| [1707. 与数组中元素的最大异或值](https://leetcode-cn.com/problems/maximum-xor-with-an-element-from-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximum-xor-with-an-element-from-array/solution/gong-shui-san-xie-jie-zhe-ge-wen-ti-lai-lypqr/) | 困难 | 🤩🤩🤩 |
6767
| [1713. 得到子序列的最少操作次数](https://leetcode-cn.com/problems/minimum-operations-to-make-a-subsequence/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/minimum-operations-to-make-a-subsequence/solution/gong-shui-san-xie-noxiang-xin-ke-xue-xi-oj7yu/) | 困难 | 🤩🤩🤩 |
6868
| [1751. 最多可以参加的会议数目 II](https://leetcode-cn.com/problems/maximum-number-of-events-that-can-be-attended-ii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximum-number-of-events-that-can-be-attended-ii/solution/po-su-dp-er-fen-dp-jie-fa-by-ac_oier-88du/) | 困难 | 🤩🤩🤩 |
69+
| [1760. 袋子里最少数目的球](https://leetcode.cn/problems/minimum-limit-of-balls-in-a-bag/) | [LeetCode 题解链接](https://acoier.com/2022/12/23/1760.%20%E8%A2%8B%E5%AD%90%E9%87%8C%E6%9C%80%E5%B0%91%E6%95%B0%E7%9B%AE%E7%9A%84%E7%90%83%EF%BC%88%E4%B8%AD%E7%AD%89%EF%BC%89/) | 中等 | 🤩🤩🤩🤩🤩 |
6970
| [1818. 绝对差值和](https://leetcode-cn.com/problems/minimum-absolute-sum-difference/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/minimum-absolute-sum-difference/solution/gong-shui-san-xie-tong-guo-er-fen-zhao-z-vrmq/) | 中等 | 🤩🤩🤩🤩🤩 |
7071
| [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/) | 中等 | 🤩🤩🤩 |
7172
| [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/) | 中等 | 🤩🤩🤩🤩 |

‎Index/双指针.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
| [1697. 检查边长度限制的路径是否存在](https://leetcode.cn/problems/checking-existence-of-edge-length-limited-paths/) | [LeetCode 题解链接](https://acoier.com/2022/12/14/1697.%20%E6%A3%80%E6%9F%A5%E8%BE%B9%E9%95%BF%E5%BA%A6%E9%99%90%E5%88%B6%E7%9A%84%E8%B7%AF%E5%BE%84%E6%98%AF%E5%90%A6%E5%AD%98%E5%9C%A8%EF%BC%88%E5%9B%B0%E9%9A%BE%EF%BC%89/) | 困难 | 🤩🤩🤩🤩 |
6969
| [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/) | 中等 | 🤩🤩🤩🤩 |
7070
| [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/) | 简单 | 🤩🤩🤩🤩 |
71+
| [1759. 统计同构子字符串的数目](https://leetcode.cn/problems/count-number-of-homogenous-substrings/) | [LeetCode 题解链接](https://acoier.com/2022/12/26/1759.%20%E7%BB%9F%E8%AE%A1%E5%90%8C%E6%9E%84%E5%AD%90%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%9A%84%E6%95%B0%E7%9B%AE%EF%BC%88%E4%B8%AD%E7%AD%89%EF%BC%89/) | 中等 | 🤩🤩🤩🤩 |
7172
| [1764. 通过连接另一个数组的子数组得到一个数组](https://leetcode-cn.com/problems/form-array-by-concatenating-subarrays-of-another-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/form-array-by-concatenating-subarrays-of-another-array/solution/clean-solutionni-jue-dui-neng-kan-dong-d-l4ts/) | 中等 | 🤩🤩🤩🤩 |
7273
| [2000. 反转单词前缀](https://leetcode-cn.com/problems/reverse-prefix-of-word/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/reverse-prefix-of-word/solution/gong-shui-san-xie-jian-dan-shuang-zhi-zh-dp9u/) | 简单 | 🤩🤩🤩🤩 |
7374
| [2024. 考试的最大困扰度](https://leetcode-cn.com/problems/maximize-the-confusion-of-an-exam/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximize-the-confusion-of-an-exam/solution/by-ac_oier-2rii/) | 中等 | 🤩🤩🤩🤩 |

‎Index/数学.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
| [1720. 解码异或后的数组](https://leetcode-cn.com/problems/decode-xored-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/decode-xored-array/solution/gong-shui-san-xie-li-yong-yi-huo-xing-zh-p1bi/) | 简单 | 🤩🤩🤩 |
8080
| [1734. 解码异或后的排列](https://leetcode-cn.com/problems/decode-xored-permutation/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/decode-xored-permutation/solution/gong-shui-san-xie-note-bie-pian-li-yong-zeh6o/) | 中等 | 🤩🤩🤩🤩 |
8181
| [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/) | 中等 | 🤩🤩🤩 |
82+
| [1759. 统计同构子字符串的数目](https://leetcode.cn/problems/count-number-of-homogenous-substrings/) | [LeetCode 题解链接](https://acoier.com/2022/12/26/1759.%20%E7%BB%9F%E8%AE%A1%E5%90%8C%E6%9E%84%E5%AD%90%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%9A%84%E6%95%B0%E7%9B%AE%EF%BC%88%E4%B8%AD%E7%AD%89%EF%BC%89/) | 中等 | 🤩🤩🤩🤩 |
8283
| [1775. 通过最少操作次数使数组的和相等](https://leetcode.cn/problems/equal-sum-arrays-with-minimum-number-of-operations/) | [LeetCode 题解链接](https://acoier.com/2022/12/09/1775.%20%E9%80%9A%E8%BF%87%E6%9C%80%E5%B0%91%E6%93%8D%E4%BD%9C%E6%AC%A1%E6%95%B0%E4%BD%BF%E6%95%B0%E7%BB%84%E7%9A%84%E5%92%8C%E7%9B%B8%E7%AD%89%EF%BC%88%E4%B8%AD%E7%AD%89%EF%BC%89/) | 中等 | 🤩🤩🤩🤩 |
8384
| [1780. 判断一个数字是否可以表示成三的幂的和](https://leetcode.cn/problems/check-if-number-is-a-sum-of-powers-of-three/) | [LeetCode 题解链接](https://acoier.com/2022/12/12/1780.%20%E5%88%A4%E6%96%AD%E4%B8%80%E4%B8%AA%E6%95%B0%E5%AD%97%E6%98%AF%E5%90%A6%E5%8F%AF%E4%BB%A5%E8%A1%A8%E7%A4%BA%E6%88%90%E4%B8%89%E7%9A%84%E5%B9%82%E7%9A%84%E5%92%8C%EF%BC%88%E4%B8%AD%E7%AD%89%EF%BC%89/) | 中等 | 🤩🤩🤩🤩🤩 |
8485
| [剑指 Offer 44. 数字序列中某一位的数字](https://leetcode.cn/problems/shu-zi-xu-lie-zhong-mou-yi-wei-de-shu-zi-lcof/) | [LeetCode 题解链接](https://leetcode.cn/problems/shu-zi-xu-lie-zhong-mou-yi-wei-de-shu-zi-lcof/solution/by-ac_oier-wgr8/) | 中等 | 🤩🤩🤩🤩 |
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[1759. 统计同构子字符串的数目](https://acoier.com/2022/12/26/1759.%20%E7%BB%9F%E8%AE%A1%E5%90%8C%E6%9E%84%E5%AD%90%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%9A%84%E6%95%B0%E7%9B%AE%EF%BC%88%E4%B8%AD%E7%AD%89%EF%BC%89/)** ,难度为 **中等**
4+
5+
Tag : 「双指针」、「数学」
6+
7+
8+
9+
给你一个字符串 `s`,返回 `s` 中 同构子字符串 的数目。由于答案可能很大,只需返回对 10ドル^9 + 7$ 取余 后的结果。
10+
11+
同构字符串 的定义为:如果一个字符串中的所有字符都相同,那么该字符串就是同构字符串。
12+
13+
子字符串 是字符串中的一个连续字符序列。
14+
15+
示例 1:
16+
```
17+
输入:s = "abbcccaa"
18+
19+
输出:13
20+
21+
解释:同构子字符串如下所列:
22+
"a" 出现 3 次。
23+
"aa" 出现 1 次。
24+
"b" 出现 2 次。
25+
"bb" 出现 1 次。
26+
"c" 出现 3 次。
27+
"cc" 出现 2 次。
28+
"ccc" 出现 1 次。
29+
3 +たす 1 +たす 2 +たす 1 +たす 3 +たす 2 +たす 1 = 13
30+
```
31+
示例 2:
32+
```
33+
输入:s = "xy"
34+
35+
输出:2
36+
37+
解释:同构子字符串是 "x" 和 "y" 。
38+
```
39+
示例 3:
40+
```
41+
输入:s = "zzzzz"
42+
43+
输出:15
44+
```
45+
46+
提示:
47+
* 1ドル <= s.length <= 10^5$
48+
* `s` 由小写字符串组成
49+
50+
51+
---
52+
53+
### 双指针 + 数学
54+
55+
根据题意,我们需要找出 `s` 中所有字符相同的连续段。
56+
57+
假设 $s[i...(j - 1)]$ 为某个连续段,长度为 $m,ドル根据「等差数列求和」可知该连续段所能提供的同构字符串数量为 $\frac{(1 + m) \times m}{2}$。
58+
59+
具体的,我们可以从前往后扫描 `s`,假设当前处理到的位置为 `i`,将其看作连续段的左端点,然后从 `i` 出发找到当前最长连续段的右端点 `j - 1`,统计 $s[i...(j - 1)]$ 所能贡献同构字符串数量,并调整下个发起点为 $i = j$ 以扫描下一个连续段。
60+
61+
Java 代码:
62+
```Java
63+
class Solution {
64+
public int countHomogenous(String s) {
65+
int n = s.length(), MOD = (int)1e9+7;
66+
long ans = 0;
67+
for (int i = 0; i < n; ) {
68+
int j = i;
69+
while (j < n && s.charAt(j) == s.charAt(i)) j++;
70+
long cnt = j - i;
71+
ans += (1 + cnt) * cnt / 2;
72+
ans %= MOD;
73+
i = j;
74+
}
75+
return (int) ans;
76+
}
77+
}
78+
```
79+
TypeScript 代码:
80+
```TypeScript
81+
function countHomogenous(s: string): number {
82+
let n = s.length, mod = 1e9+7, ans = 0
83+
for (let i = 0; i < n; ) {
84+
let j = i
85+
while (j < n && s.charAt(j) == s.charAt(i)) j++
86+
const cnt = j - i
87+
ans += (1 + cnt) * cnt / 2
88+
ans %= mod
89+
i = j
90+
}
91+
return ans
92+
}
93+
```
94+
Python3 代码:
95+
```Python3
96+
class Solution:
97+
def countHomogenous(self, s: str) -> int:
98+
n, mod, i, ans = len(s), 1e9+7, 0, 0
99+
while i < n:
100+
j = i
101+
while j < n and s[j] == s[i]:
102+
j += 1
103+
cnt = j - i
104+
ans += (cnt + 1) * cnt / 2
105+
ans %= mod
106+
i = j
107+
return int(ans)
108+
```
109+
* 时间复杂度:$O(n)$
110+
* 空间复杂度:$O(1)$
111+
112+
---
113+
114+
### 最后
115+
116+
这是我们「刷穿 LeetCode」系列文章的第 `No.1759` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
117+
118+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
119+
120+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
121+
122+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
123+
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[1760. 袋子里最少数目的球](https://acoier.com/2022/12/23/1760.%20%E8%A2%8B%E5%AD%90%E9%87%8C%E6%9C%80%E5%B0%91%E6%95%B0%E7%9B%AE%E7%9A%84%E7%90%83%EF%BC%88%E4%B8%AD%E7%AD%89%EF%BC%89/)** ,难度为 **中等**
4+
5+
Tag : 「二分」
6+
7+
8+
9+
给你一个整数数组 `nums`,其中 `nums[i]` 表示第 `i` 个袋子里球的数目。同时给你一个整数 `maxOperations`
10+
11+
你可以进行如下操作至多 `maxOperations` 次:
12+
13+
* 选择任意一个袋子,并将袋子里的球分到 `2` 个新的袋子中,每个袋子里都有 正整数 个球。
14+
* 比方说,一个袋子里有 `5` 个球,你可以把它们分到两个新袋子里,分别有 `1` 个和 `4` 个球,或者分别有 `2` 个和 `3` 个球。
15+
你的开销是单个袋子里球数目的 最大值 ,你想要 最小化 开销。
16+
17+
请你返回进行上述操作后的最小开销。
18+
19+
示例 1:
20+
```
21+
输入:nums = [9], maxOperations = 2
22+
23+
输出:3
24+
25+
解释:
26+
- 将装有 9 个球的袋子分成装有 6 个和 3 个球的袋子。[9] -> [6,3] 。
27+
- 将装有 6 个球的袋子分成装有 3 个和 3 个球的袋子。[6,3] -> [3,3,3] 。
28+
装有最多球的袋子里装有 3 个球,所以开销为 3 并返回 3 。
29+
```
30+
示例 2:
31+
```
32+
输入:nums = [2,4,8,2], maxOperations = 4
33+
34+
输出:2
35+
36+
解释:
37+
- 将装有 8 个球的袋子分成装有 4 个和 4 个球的袋子。[2,4,8,2] -> [2,4,4,4,2] 。
38+
- 将装有 4 个球的袋子分成装有 2 个和 2 个球的袋子。[2,4,4,4,2] -> [2,2,2,4,4,2] 。
39+
- 将装有 4 个球的袋子分成装有 2 个和 2 个球的袋子。[2,2,2,4,4,2] -> [2,2,2,2,2,4,2] 。
40+
- 将装有 4 个球的袋子分成装有 2 个和 2 个球的袋子。[2,2,2,2,2,4,2] -> [2,2,2,2,2,2,2,2] 。
41+
装有最多球的袋子里装有 2 个球,所以开销为 2 并返回 2 。
42+
```
43+
示例 3:
44+
```
45+
输入:nums = [7,17], maxOperations = 2
46+
47+
输出:7
48+
```
49+
50+
提示:
51+
* 1ドル <= nums.length <= 10^5$
52+
* 1ドル <= maxOperations, nums[i] <= 10^9$
53+
54+
---
55+
56+
### 二分
57+
58+
最小化不超过 `max` 次划分操作后的单个袋子最大值,我们将其称为「划分值」。
59+
60+
**答案具有二段性:若使用 $k \leq \max$ 次划分操作后可达到最小划分值,此时减少划分操作次数,会使得划分值非单调上升。**
61+
62+
因此我们可以二分答案,从而将问题进行等价转换:
63+
64+
**假设当前二分到的值为 $limit,ドル我们需要实现一个线性复杂度为 `check` 函数,判断能否使用不超过 $\max$ 次划分次数,来使得划分值不超过 $limit$**:
65+
66+
* 若能满足,说明 $[limit, +\infty]$ 范围的划分值,均能使用不超过 $\max$ 次的实现,此时让 $r = limit$
67+
* 若不能满足,比 $limit$ 更小的划分值,则更无法在 $\max$ 次操作中满足,说明 $[1, limit]$ 范围划分值均不是答案,此时让 $l = limit + 1$
68+
69+
考虑如何实现 `check` 函数,从前往后处理每个 $nums[i],ドル根据 $nums[i]$ 与当前限制 $limit$ 的大小关系进行分情况讨论:
70+
71+
* 若 $nums[i] \leq limit$:说明当前袋子不会成为瓶颈,无须消耗划分次数
72+
* 若 $nums[i] > limit$:此时需要对当前袋子进行划分,直到满足单个袋子球的数量不超过 $limit$ 为止,由于每次划分相当于增加一个袋子,而将 $nums[i]$ 划分成若干个不超过 $limit$ 个球的袋子,需要 $\left \lceil \frac{nums[i]}{limit} \right \rceil$ 个袋子,减去原本的一个,共需要增加 $\left \lceil \frac{nums[i]}{limit} \right \rceil$ 个新袋子,即划分 $\left \lceil \frac{nums[i]}{limit} \right \rceil$ 次
73+
74+
75+
Java 代码:
76+
```Java
77+
class Solution {
78+
public int minimumSize(int[] nums, int max) {
79+
int l = 1, r = 0x3f3f3f3f;
80+
while (l < r) {
81+
int mid = l + r >> 1;
82+
if (check(nums, mid, max)) r = mid;
83+
else l = mid + 1;
84+
}
85+
return r;
86+
}
87+
boolean check(int[] nums, int limit, int max) {
88+
int cnt = 0;
89+
for (int x : nums) cnt += Math.ceil(x * 1.0 / limit) - 1;
90+
return cnt <= max;
91+
}
92+
}
93+
```
94+
TypeScript 代码:
95+
```TypeScript
96+
function minimumSize(nums: number[], max: number): number {
97+
function check(nums: number[], limit: number, max: number): boolean {
98+
let cnt = 0
99+
for (const x of nums) cnt += Math.ceil(x / limit) - 1
100+
return cnt <= max
101+
}
102+
let l = 1, r = 0x3f3f3f3f
103+
while (l < r) {
104+
const mid = l + r >> 1
105+
if (check(nums, mid, max)) r = mid
106+
else l = mid + 1
107+
}
108+
return r
109+
}
110+
```
111+
Python 代码:
112+
```Python
113+
class Solution:
114+
def minimumSize(self, nums: List[int], maxv: int) -> int:
115+
def check(nums, limit, maxv):
116+
return sum([(x + limit - 1) // limit - 1 for x in nums]) <= maxv
117+
l, r = 1, 0x3f3f3f3f
118+
while l < r:
119+
mid = l + r >> 1
120+
if check(nums, mid, maxv):
121+
r = mid
122+
else:
123+
l = mid + 1
124+
return r
125+
```
126+
* 时间复杂度:$O(n \log{M}),ドル其中 $M = 1e9$ 为值域大小
127+
* 空间复杂度:$O(1)$
128+
129+
---
130+
131+
### 最后
132+
133+
这是我们「刷穿 LeetCode」系列文章的第 `No.1758` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
134+
135+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
136+
137+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
138+
139+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
140+

0 commit comments

Comments
(0)

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