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 e96ba5e

Browse files
Merge pull request SharingSource#759 from SharingSource/ac_oier
✨feat: add 93、95、1775
2 parents 169bc75 + be35c5c commit e96ba5e

10 files changed

+451
-12
lines changed

‎Index/DFS.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
| [37. 解数独](https://leetcode-cn.com/problems/sudoku-solver/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/sudoku-solver/solution/he-n-huang-hou-yi-yang-shi-yi-dao-hui-su-lfpd/) | 困难 | 🤩🤩🤩🤩 |
66
| [39. 组合总和](https://leetcode-cn.com/problems/combination-sum/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/combination-sum/solution/dfs-hui-su-suan-fa-yi-ji-ru-he-que-ding-wpbo5/) | 中等 | 🤩🤩🤩🤩 |
77
| [40. 组合总和 II](https://leetcode-cn.com/problems/combination-sum-ii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/combination-sum-ii/solution/dfs-hui-su-jie-fa-yi-ji-ru-he-pan-duan-s-xlwy/) | 中等 | 🤩🤩🤩🤩 |
8+
| [93. 复原 IP 地址](https://leetcode.cn/problems/restore-ip-addresses/) | [LeetCode 题解链接](https://acoier.com/2022/12/09/93.%20%E5%A4%8D%E5%8E%9F%20IP%20%E5%9C%B0%E5%9D%80%EF%BC%88%E4%B8%AD%E7%AD%89%EF%BC%89/) | 中等 | 🤩🤩🤩🤩 |
9+
| [95. 不同的二叉搜索树 II](https://leetcode.cn/problems/unique-binary-search-trees-ii/) | [LeetCode 题解链接](https://www.acoier.com/2022/12/09/95.%20%E4%B8%8D%E5%90%8C%E7%9A%84%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91%20II%EF%BC%88%E4%B8%AD%E7%AD%89%EF%BC%89/) | 中等 | 🤩🤩🤩🤩🤩 |
810
| [211. 添加与搜索单词 - 数据结构设计](https://leetcode-cn.com/problems/design-add-and-search-words-data-structure/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/design-add-and-search-words-data-structure/solution/gong-shui-san-xie-yi-ti-shuang-jie-er-we-un94/) | 中等 | 🤩🤩🤩🤩🤩 |
911
| [241. 为运算表达式设计优先级](https://leetcode.cn/problems/different-ways-to-add-parentheses/) | [LeetCode 题解链接](https://leetcode.cn/problems/different-ways-to-add-parentheses/solution/by-ac_oier-z07i/) | 中等 | 🤩🤩🤩🤩🤩 |
1012
| [282. 给表达式添加运算符](https://leetcode-cn.com/problems/expression-add-operators/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/expression-add-operators/solution/gong-shui-san-xie-hui-su-suan-fa-yun-yon-nl9z/) | 困难 | 🤩🤩🤩🤩 |

‎Index/二叉树.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
| 题目 | 题解 | 难度 | 推荐指数 |
22
| ------------------------------------------------------------ | ------------------------------------------------------------ | ---- | -------- |
3+
| [95. 不同的二叉搜索树 II](https://leetcode.cn/problems/unique-binary-search-trees-ii/) | [LeetCode 题解链接](https://www.acoier.com/2022/12/09/95.%20%E4%B8%8D%E5%90%8C%E7%9A%84%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91%20II%EF%BC%88%E4%B8%AD%E7%AD%89%EF%BC%89/) | 中等 | 🤩🤩🤩🤩 |
34
| [230. 二叉搜索树中第K小的元素](https://leetcode-cn.com/problems/kth-smallest-element-in-a-bst/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/kth-smallest-element-in-a-bst/solution/gong-shui-san-xie-yi-ti-san-jie-pai-xu-y-8uah/) | 中等 | 🤩🤩🤩🤩 |
45
| [240. 搜索二维矩阵 II](https://leetcode-cn.com/problems/search-a-2d-matrix-ii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/search-a-2d-matrix-ii/solution/gong-shui-san-xie-yi-ti-shuang-jie-er-fe-y1ns/) | 中等 | 🤩🤩🤩🤩🤩 |
56
| [297. 二叉树的序列化与反序列化](https://leetcode-cn.com/problems/serialize-and-deserialize-binary-tree/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/xu-lie-hua-er-cha-shu-lcof/solution/gong-shui-san-xie-er-cha-shu-de-xu-lie-h-n89a/) | 困难 | 🤩🤩🤩🤩🤩 |

‎Index/回溯算法.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
| [39. 组合总和](https://leetcode-cn.com/problems/combination-sum/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/combination-sum/solution/dfs-hui-su-suan-fa-yi-ji-ru-he-que-ding-wpbo5/) | 中等 | 🤩🤩🤩🤩 |
66
| [40. 组合总和 II](https://leetcode-cn.com/problems/combination-sum-ii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/combination-sum-ii/solution/dfs-hui-su-jie-fa-yi-ji-ru-he-pan-duan-s-xlwy/) | 中等 | 🤩🤩🤩🤩 |
77
| [90. 子集 II](https://leetcode-cn.com/problems/subsets-ii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/subsets-ii/solution/gong-shui-san-xie-yi-ti-shuang-jie-hui-s-g77q/) | 中等 | 🤩🤩🤩🤩 |
8+
| [93. 复原 IP 地址](https://leetcode.cn/problems/restore-ip-addresses/) | [LeetCode 题解链接](https://acoier.com/2022/12/09/93.%20%E5%A4%8D%E5%8E%9F%20IP%20%E5%9C%B0%E5%9D%80%EF%BC%88%E4%B8%AD%E7%AD%89%EF%BC%89/) | 中等 | 🤩🤩🤩🤩 |
9+
| [95. 不同的二叉搜索树 II](https://leetcode.cn/problems/unique-binary-search-trees-ii/) | [LeetCode 题解链接](https://www.acoier.com/2022/12/09/95.%20%E4%B8%8D%E5%90%8C%E7%9A%84%E4%BA%8C%E5%8F%89%E6%90%9C%E7%B4%A2%E6%A0%91%20II%EF%BC%88%E4%B8%AD%E7%AD%89%EF%BC%89/) | 中等 | 🤩🤩🤩🤩🤩 |
810
| [131. 分割回文串](https://leetcode-cn.com/problems/palindrome-partitioning/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/palindrome-partitioning/solution/wei-sha-yao-zhe-yang-bao-sou-ya-shi-ru-h-41gf/) | 中等 | 🤩🤩🤩🤩 |
911
| [212. 单词搜索 II](https://leetcode-cn.com/problems/word-search-ii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/word-search-ii/solution/gong-shui-san-xie-yi-ti-shuang-jie-hui-s-am8f/) | 困难 | 🤩🤩🤩 |
1012
| [301. 删除无效的括号](https://leetcode-cn.com/problems/remove-invalid-parentheses/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/remove-invalid-parentheses/solution/yi-fen-zhong-nei-kan-dong-jiang-gua-hao-aya6k/) | 困难 | 🤩🤩🤩🤩 |

‎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+
| [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/) | 中等 | 🤩🤩🤩🤩 |
8283
| [剑指 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/) | 中等 | 🤩🤩🤩🤩 |
8384
| [面试题 10.02. 变位词组](https://leetcode-cn.com/problems/group-anagrams-lcci/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/group-anagrams-lcci/solution/gong-shui-san-xie-tong-ji-bian-wei-ci-de-0iqe/) | 中等 | 🤩🤩🤩🤩 |
8485
| [面试题 17.19. 消失的两个数字](https://leetcode.cn/problems/missing-two-lcci/) | [LeetCode 题解链接](https://leetcode.cn/problems/missing-two-lcci/solution/by-ac_oier-pgeh/) | 困难 | 🤩🤩🤩🤩 |

‎Index/贪心算法.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
| [1710. 卡车上的最大单元数](https://leetcode.cn/problems/maximum-units-on-a-truck/) | [LeetCode 题解链接](https://leetcode.cn/problems/maximum-units-on-a-truck/solution/by-ac_oier-5wlo/) | 简单 | 🤩🤩🤩🤩🤩 |
4040
| [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/) | 困难 | 🤩🤩🤩🤩🤩 |
4141
| [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/) | 简单 | 🤩🤩🤩🤩🤩 |
42+
| [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/) | 中等 | 🤩🤩🤩🤩 |
4243
| [1833. 雪糕的最大数量](https://leetcode-cn.com/problems/maximum-ice-cream-bars/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximum-ice-cream-bars/solution/gong-shui-san-xie-noxiang-xin-ke-xue-xi-yrhjx/) | 中等 | 🤩🤩🤩🤩🤩 |
4344
| [1846. 减小和重新排列数组后的最大元素](https://leetcode-cn.com/problems/maximum-element-after-decreasing-and-rearranging/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/maximum-element-after-decreasing-and-rearranging/solution/gong-shui-san-xie-noxiang-xin-ke-xue-xi-yh9qt/) | 中等 | 🤩🤩🤩🤩🤩 |
4445
| [1877. 数组中最大数对和的最小值](https://leetcode-cn.com/problems/minimize-maximum-pair-sum-in-array/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/minimize-maximum-pair-sum-in-array/solution/gong-shui-san-xie-noxiang-xin-ke-xue-xi-ru29y/) | 中等 | 🤩🤩🤩🤩🤩 |

‎LeetCode/1741-1750/1743. 从相邻元素对还原数组(中等).md‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ Tag : 「哈希表」、「双指针」、「模拟」
66

77

88

9-
存在一个由 n 个不同元素组成的整数数组 nums ,但你已经记不清具体内容。好在你还记得 nums 中的每一对相邻元素。
9+
存在一个由 `n` 个不同元素组成的整数数组 `nums` ,但你已经记不清具体内容。好在你还记得 `nums` 中的每一对相邻元素。
1010

11-
给你一个二维整数数组 adjacentPairs ,大小为 n - 1 ,其中每个 adjacentPairs[i] = [ui, vi] 表示元素 uivi 在 nums 中相邻。
11+
给你一个二维整数数组 `adjacentPairs` ,大小为 `n - 1` ,其中每个 $adjacentPairs[i] = [u_i, v_i]$ 表示元素 $u_i$$v_i$`nums` 中相邻。
1212

13-
题目数据保证所有由元素 nums[i] 和 nums[i+1] 组成的相邻元素对都存在于 adjacentPairs 中,存在形式可能是 [nums[i], nums[i+1]] ,也可能是 [nums[i+1], nums[i]] 。这些相邻元素对可以 按任意顺序 出现。
13+
题目数据保证所有由元素 `nums[i]``nums[i+1]` 组成的相邻元素对都存在于 `adjacentPairs` 中,存在形式可能是 `[nums[i], nums[i+1]]` ,也可能是 `[nums[i+1], nums[i]]` 。这些相邻元素对可以 按任意顺序 出现。
1414

15-
返回 原始数组 nums 。如果存在多种解答,返回 其中任意一个 即可。
15+
返回 原始数组 `nums` 。如果存在多种解答,返回 其中任意一个 即可。
1616

1717
示例 1:
1818
```
@@ -40,12 +40,12 @@ Tag : 「哈希表」、「双指针」、「模拟」
4040
```
4141

4242
提示:
43-
* nums.length == n
44-
* adjacentPairs.length == n - 1
45-
* adjacentPairs[i].length == 2
46-
* 2 <= n <= $10^5$
47-
* -$10^5$ <= nums[i], ui, vi <= $10^5$
48-
* 题目数据保证存在一些以 adjacentPairs 作为元素对的数组
43+
* $nums.length == n$
44+
* $adjacentPairs.length == n - 1$
45+
* $adjacentPairs[i].length == 2$
46+
* $2 <= n <= 10^5$
47+
* $-10^5 <= nums[i], u_i, v_i <= 10^5$
48+
* 题目数据保证存在一些以 `adjacentPairs` 作为元素对的数组
4949

5050
---
5151

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[1775. 通过最少操作次数使数组的和相等量](https://www.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/)** ,难度为 **中等**
4+
5+
Tag : 「枚举」、「贪心」、「数学」
6+
7+
8+
9+
给你两个长度可能不等的整数数组 `nums1``nums2` 。两个数组中的所有值都在 `1``6` 之间(包含 `1``6`)。
10+
11+
每次操作中,你可以选择 任意 数组中的任意一个整数,将它变成 `1``6` 之间 任意 的值(包含 `1``6`)。
12+
13+
请你返回使 `nums1` 中所有数的和与 `nums2` 中所有数的和相等的最少操作次数。如果无法使两个数组的和相等,请返回 `-1`
14+
15+
示例 1:
16+
```
17+
输入:nums1 = [1,2,3,4,5,6], nums2 = [1,1,2,2,2,2]
18+
19+
输出:3
20+
21+
解释:你可以通过 3 次操作使 nums1 中所有数的和与 nums2 中所有数的和相等。以下数组下标都从 0 开始。
22+
- 将 nums2[0] 变为 6 。 nums1 = [1,2,3,4,5,6], nums2 = [6,1,2,2,2,2] 。
23+
- 将 nums1[5] 变为 1 。 nums1 = [1,2,3,4,5,1], nums2 = [6,1,2,2,2,2] 。
24+
- 将 nums1[2] 变为 2 。 nums1 = [1,2,2,4,5,1], nums2 = [6,1,2,2,2,2] 。
25+
```
26+
示例 2:
27+
```
28+
输入:nums1 = [1,1,1,1,1,1,1], nums2 = [6]
29+
30+
输出:-1
31+
32+
解释:没有办法减少 nums1 的和或者增加 nums2 的和使二者相等。
33+
```
34+
示例 3:
35+
```
36+
输入:nums1 = [6,6], nums2 = [1]
37+
38+
输出:3
39+
40+
解释:你可以通过 3 次操作使 nums1 中所有数的和与 nums2 中所有数的和相等。以下数组下标都从 0 开始。
41+
- 将 nums1[0] 变为 2 。 nums1 = [2,6], nums2 = [1] 。
42+
- 将 nums1[1] 变为 2 。 nums1 = [2,2], nums2 = [1] 。
43+
- 将 nums2[0] 变为 4 。 nums1 = [2,2], nums2 = [4] 。
44+
```
45+
46+
提示:
47+
* 1ドル <= nums1.length, nums2.length <= 10^5$
48+
* 1ドル <= nums1[i], nums2[i] <= 6$
49+
50+
---
51+
52+
### 枚举 + 贪心 + 数学
53+
54+
`nums1` 的长度为 `n`,`nums2` 的长度为 `m`,根据题意两数组的值域分别为 $[n, 6n]$ 和 $[m, 6m],ドル可分别视为数轴上的两条线段。
55+
56+
为了方便,我们人为固定 $n\leq m,ドル若不满足则交换两数组,返回 `minOperations(nums2, nums1)` 即可。
57+
58+
先来考虑无解的情况:当 6ドルn < m$ 时,说明两线段不重合,必然无法通过变换使得总和相等,直接返回 `-1`
59+
60+
由于 $\max(n, m)$ 的范围为 1ドルe5,ドル且 $nums[i]$ 的值域大小 $C = 6,ドル因此我们可以通过枚举最终目标和 `x`(两线段的重合部分)来做,枚举范围不超过 6ドル \times 1e5$。
61+
62+
于是问题转换为:**对于一个原总和为 `sum` 的数组 `nums` 而言,按照题目的变换规则,至少经过多少次变换,才能将其总和变为 `x`**
63+
64+
根据原总和 `sum` 和目标结果 `x` 的大小关系进行分情况讨论(将两者差值绝对值记为 `d`):
65+
66+
* 当 $sum < x$ 时,对于原数为 $nums[i]$ 的数而言,其能变为不超过 $nums[i] - 1$ 的任意数。
67+
68+
例如 6ドル$ 能够变化为 $[1, 5]$ 中的任意数,即单个数值 6ドル$ 最多能够抵消 6ドル - 1$ 个差值,不失一般性的可概括为原数为 $nums[i]$ 所能抵消的差值为 $nums[i] - 1$。
69+
70+
因此,我们贪心的使用较大数进行变换(从 6ドル$ 往 2ドル$ 枚举 `i`),对于每个数值 `i` 而言,其所能提供的个数为 $\min(\left \lceil \frac{d}{i - 1} \right \rceil, cnst[i])$。
71+
72+
* 当 $sum > x$ 时,同理,原数为 $nums[i]$ 所能提供的最大抵消数为 6ドル - nums[i],ドル因此我们贪心使用较小数进行变换(从 1ドル$ 往 5ドル$ 枚举 `i`),对于每个数值 `i` 而言,其所能提供的个数为 $\min(\left \lceil \frac{d}{6 - i} \right \rceil, cnst[i])$。
73+
74+
如此一来,我们通过枚举两线段重合点 `x`,复杂度为 $O(C \times \max(n, m)),ドル并通过复杂度为 $O(C)$ 的数学方法来得知将两原数组总和变为 `x` 所需要的操作次数 `cnt`,在所有的 `cnt` 取最小值即是答案。整体计算量为 3ドル.6 \times 10^6,ドル可以过。
75+
76+
77+
Java 代码:
78+
```Java
79+
class Solution {
80+
int[] c1 = new int[10], c2 = new int[10];
81+
int s1, s2;
82+
public int minOperations(int[] nums1, int[] nums2) {
83+
int n = nums1.length, m = nums2.length;
84+
if (n > m) return minOperations(nums2, nums1);
85+
if (m > 6 * n) return -1;
86+
for (int x : nums1) {
87+
c1[x]++; s1 += x;
88+
}
89+
for (int x : nums2) {
90+
c2[x]++; s2 += x;
91+
}
92+
int ans = n + m;
93+
for (int i = m; i <= 6 * n; i++) ans = Math.min(ans, getCnt(c1, s1, i) + getCnt(c2, s2, i));
94+
return ans;
95+
}
96+
int getCnt(int[] cnts, int sum, int x) {
97+
int ans = 0;
98+
if (sum > x) {
99+
for (int i = 6, d = sum - x; i >= 2 && d > 0; i--) {
100+
int c = Math.min((int) Math.ceil(d * 1.0 / (i - 1)), cnts[i]);
101+
ans += c; d -= c * (i - 1);
102+
}
103+
} else if (sum < x) {
104+
for (int i = 1, d = x - sum; i <= 5 && d > 0; i++) {
105+
int c = Math.min((int) Math.ceil(d * 1.0 / (6 - i)), cnts[i]);
106+
ans += c; d -= c * (6 - i);
107+
}
108+
}
109+
return ans;
110+
}
111+
}
112+
```
113+
Python 代码:
114+
```Python
115+
class Solution:
116+
def minOperations(self, nums1: List[int], nums2: List[int]) -> int:
117+
n, m = len(nums1), len(nums2)
118+
if n > m:
119+
return self.minOperations(nums2, nums1)
120+
if m > 6 * n:
121+
return -1
122+
c1, c2 = Counter(nums1), Counter(nums2)
123+
s1, s2 = sum(nums1), sum(nums2)
124+
def getCnt(cnts, tot, x):
125+
ans = 0
126+
if tot > x:
127+
d = tot - x
128+
for i in range(6, 1, -1):
129+
if d <= 0:
130+
break
131+
c = min(math.ceil(d / (i - 1)), cnts[i])
132+
ans, d = ans + c, d - c * (i - 1)
133+
elif tot < x:
134+
d = x - tot
135+
for i in range(1, 6):
136+
if d <= 0:
137+
break
138+
c = min(math.ceil(d / (6 - i)), cnts[i])
139+
ans, d = ans + c, d - c * (6 - i)
140+
return ans
141+
ans = n + m
142+
for i in range(m, 6 * n + 1):
143+
ans = min(ans, getCnt(c1, s1, i) + getCnt(c2, s2, i))
144+
return ans
145+
```
146+
* 时间复杂度:$O(C \times \max(n, m) \times C),ドル其中 $C = 6$ 为 $nums[i]$ 的值域大小
147+
* 空间复杂度:$O(C)$
148+
149+
---
150+
151+
### 最后
152+
153+
这是我们「刷穿 LeetCode」系列文章的第 `No.1775` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
154+
155+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
156+
157+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
158+
159+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
160+

‎LeetCode/411-420/413. 等差数列划分(中等).md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ Tag : 「双指针」、「模拟」、「数学」
3131
```
3232

3333
提示:
34-
* 1 <= nums.length <= 5000
35-
* -1000 <= nums[i] <= 1000
34+
* $1 <= nums.length <= 5000$
35+
* $-1000 <= nums[i] <= 1000$
3636

3737
---
3838

0 commit comments

Comments
(0)

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