Skip to content

Commit 08f180c

Browse files
committed
✨feat: add 301、1785、678
1 parent 7a78781 commit 08f180c

5 files changed

+98
-11
lines changed

Index/模拟.md

+1
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@
218218
| [1773. 统计匹配检索规则的物品数量](https://leetcode.cn/problems/count-items-matching-a-rule/) | [LeetCode 题解链接](https://leetcode.cn/problems/count-items-matching-a-rule/solution/by-ac_oier-qyd6/) | 简单 | 🤩🤩🤩 |
219219
| [1781. 所有子字符串美丽值之和](https://leetcode.cn/problems/sum-of-beauty-of-all-substrings/) | [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/) | 中等 | 🤩🤩🤩🤩 |
220220
| [1784. 检查二进制字符串字段](https://leetcode.cn/problems/check-if-binary-string-has-at-most-one-segment-of-ones/) | [LeetCode 题解链接](https://leetcode.cn/problems/check-if-binary-string-has-at-most-one-segment-of-ones/solution/by-ac_oier-kiu6/) | 简单 | 🤩🤩🤩 |
221+
| [1785. 构成特定和需要添加的最少元素](https://leetcode.cn/problems/minimum-elements-to-add-to-form-a-given-sum/) | [LeetCode 题解链接](https://acoier.com/2022/12/16/1785.%20%E6%9E%84%E6%88%90%E7%89%B9%E5%AE%9A%E5%92%8C%E9%9C%80%E8%A6%81%E6%B7%BB%E5%8A%A0%E7%9A%84%E6%9C%80%E5%B0%91%E5%85%83%E7%B4%A0%EF%BC%88%E4%B8%AD%E7%AD%89%EF%BC%89/) | 中等 | 🤩🤩🤩🤩 |
221222
| [1790. 仅执行一次字符串交换能否使两个字符串相等](https://leetcode.cn/problems/check-if-one-string-swap-can-make-strings-equal/) | [LeetCode 题解链接](https://leetcode.cn/problems/check-if-one-string-swap-can-make-strings-equal/solution/by-ac_oier-qeul/) | 简单 | 🤩🤩🤩🤩🤩 |
222223
| [1791. 找出星型图的中心节点](https://leetcode-cn.com/problems/find-center-of-star-graph/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/find-center-of-star-graph/solution/gong-shui-san-xie-jian-dan-mo-ni-ti-by-a-qoix/) | 简单 | 🤩🤩🤩 |
223224
| [1816. 截断句子](https://leetcode-cn.com/problems/truncate-sentence/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/truncate-sentence/solution/gong-shui-san-xie-jian-dan-zi-fu-chuan-m-l7gu/) | 简单 | 🤩🤩🤩🤩 |

Index/贪心算法.md

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
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/) | 简单 | 🤩🤩🤩🤩🤩 |
4242
| [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/) | 中等 | 🤩🤩🤩🤩 |
43+
| [1785. 构成特定和需要添加的最少元素](https://leetcode.cn/problems/minimum-elements-to-add-to-form-a-given-sum/) | [LeetCode 题解链接](https://acoier.com/2022/12/16/1785.%20%E6%9E%84%E6%88%90%E7%89%B9%E5%AE%9A%E5%92%8C%E9%9C%80%E8%A6%81%E6%B7%BB%E5%8A%A0%E7%9A%84%E6%9C%80%E5%B0%91%E5%85%83%E7%B4%A0%EF%BC%88%E4%B8%AD%E7%AD%89%EF%BC%89/) | 中等 | 🤩🤩🤩🤩 |
4344
| [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/) | 中等 | 🤩🤩🤩🤩🤩 |
4445
| [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/) | 中等 | 🤩🤩🤩🤩🤩 |
4546
| [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/) | 中等 | 🤩🤩🤩🤩🤩 |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[1785. 构成特定和需要添加的最少元素](https://acoier.com/2022/12/16/1785.%20%E6%9E%84%E6%88%90%E7%89%B9%E5%AE%9A%E5%92%8C%E9%9C%80%E8%A6%81%E6%B7%BB%E5%8A%A0%E7%9A%84%E6%9C%80%E5%B0%91%E5%85%83%E7%B4%A0%EF%BC%88%E4%B8%AD%E7%AD%89%EF%BC%89/)** ,难度为 **中等**
4+
5+
Tag : 「贪心」、「模拟」
6+
7+
8+
9+
给你一个整数数组 `nums`,和两个整数 `limit``goal`
10+
11+
数组 `nums` 有一条重要属性:`abs(nums[i]) <= limit`
12+
13+
返回使数组元素总和等于 `goal` 所需要向数组中添加的 最少元素数量 ,添加元素 不应改变 数组中 `abs(nums[i]) <= limit` 这一属性。
14+
15+
注意,如果 `x >= 0`,那么 `abs(x)` 等于 `x` ;否则,等于 `-x`
16+
17+
示例 1:
18+
```
19+
输入:nums = [1,-1,1], limit = 3, goal = -4
20+
21+
输出:2
22+
23+
解释:可以将 -2 和 -3 添加到数组中,数组的元素总和变为 1 - 1 + 1 - 2 - 3 = -4 。
24+
```
25+
示例 2:
26+
```
27+
输入:nums = [1,-10,9,1], limit = 100, goal = 0
28+
29+
输出:1
30+
```
31+
32+
提示:
33+
* $1 <= nums.length <= 10^5$
34+
* $1 <= limit <= 10^6$
35+
* $-limit <= nums[i] <= limit$
36+
* $-10^9 <= goal <= 10^9$
37+
38+
---
39+
40+
### 贪心
41+
42+
对于 `nums` 而言,我们可以先通过 $O(n)$ 的遍历求得其原总和 `sum` 为何值。
43+
44+
`sum``goal` 不等,我们可以按照「贪心」的方式里添加元素。
45+
46+
由于添加的元素需要满足 `abs(x) <= limit` 要求,因此我们添加数的范围在 $[-limit, limit]$ 之间。
47+
48+
为确保添加的元素最小,我们应当优先添加能够有效抵消两者差值的数值(添加 $limit$ 或 $-limit$),添加个数为 $\left \lceil \frac{\left | sum - goal \right |}{limit} \right \rceil$。
49+
50+
Java 代码:
51+
```Java
52+
class Solution {
53+
public int minElements(int[] nums, int limit, int goal) {
54+
long sum = 0;
55+
for (int x : nums) sum += x;
56+
return (int) ((Math.abs(sum - goal) + limit - 1) / limit);
57+
}
58+
}
59+
```
60+
TypeScript 代码:
61+
```TypeScript
62+
function minElements(nums: number[], limit: number, goal: number): number {
63+
let sum = 0
64+
for (const x of nums) sum += x
65+
return Math.ceil(Math.abs(sum - goal) / limit)
66+
}
67+
```
68+
Python 代码:
69+
```Python
70+
class Solution:
71+
def minElements(self, nums: List[int], limit: int, goal: int) -> int:
72+
sumv = sum(nums)
73+
return (abs(sumv - goal) + limit - 1) // limit
74+
```
75+
* 时间复杂度:$O(n)$
76+
* 空间复杂度:$O(1)$
77+
78+
---
79+
80+
### 最后
81+
82+
这是我们「刷穿 LeetCode」系列文章的第 `No.1785` 篇,系列开始于 2021/01/01,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
83+
84+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
85+
86+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
87+
88+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
89+

LeetCode/301-310/301. 删除无效的括号(困难).md

+6-10
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Tag : 「括号问题」、「回溯算法」、「DFS」
66

77

88

9-
给你一个由若干括号和字母组成的字符串 s ,删除最小数量的无效括号,使得输入的字符串有效。
9+
给你一个由若干括号和字母组成的字符串 `s`,删除最小数量的无效括号,使得输入的字符串有效。
1010

1111
返回所有可能的结果。答案可以按 任意顺序 返回。
1212

@@ -28,9 +28,9 @@ Tag : 「括号问题」、「回溯算法」、「DFS」
2828
```
2929

3030
提示:
31-
* 1 <= s.length <= 25
32-
* s 由小写英文字母以及括号 '(' 和 ')' 组成
33-
* s 中至多含 20 个括号
31+
* $1 <= s.length <= 25$
32+
* `s` 由小写英文字母以及括号 `'('``')'` 组成
33+
* `s` 中至多含 `20` 个括号
3434

3535
---
3636

@@ -61,8 +61,6 @@ Tag : 「括号问题」、「回溯算法」、「DFS」
6161

6262
使用 `Set` 进行方案去重,$len$ 记录「爆搜」过程中的最大子串,然后只保留长度等于 $len$ 的子串。
6363

64-
![image.png](https://pic.leetcode-cn.com/1635291631-fxTxUq-image.png)
65-
6664
代码:
6765
```Java
6866
class Solution {
@@ -104,7 +102,7 @@ class Solution {
104102
}
105103
}
106104
```
107-
* 时间复杂度:预处理 $max$ 的复杂度为 $O(n)$;不考虑 $score$ 带来的剪枝效果,最坏情况下,每个位置都有两种选择,搜索所有方案的复杂度为 $O(2^n)$;同时搜索过程中会产生的新字符串(最终递归树中叶子节点的字符串长度最大为 $n$,使用 `StringBuilder` 也是同理),复杂度为 $O(n)$。整体复杂度为 $O(n * 2^n)$
105+
* 时间复杂度:预处理 $max$ 的复杂度为 $O(n)$;不考虑 $score$ 带来的剪枝效果,最坏情况下,每个位置都有两种选择,搜索所有方案的复杂度为 $O(2^n)$;同时搜索过程中会产生的新字符串(最终递归树中叶子节点的字符串长度最大为 $n$,使用 `StringBuilder` 也是同理),复杂度为 $O(n)$。整体复杂度为 $O(n \times 2^n)$
108106
* 空间复杂度:最大合法方案数与字符串长度呈线性关系。复杂度为 $O(n)$
109107

110108
---
@@ -117,8 +115,6 @@ class Solution {
117115

118116
因此在此基础上,我们可以考虑多增加一层剪枝。
119117

120-
![image.png](https://pic.leetcode-cn.com/1635291751-rqYLLd-image.png)
121-
122118
代码:
123119
```Java
124120
class Solution {
@@ -169,7 +165,7 @@ class Solution {
169165
}
170166
}
171167
```
172-
* 时间复杂度:预处理 $max$ 和 $len$ 的复杂度为 $O(n)$;不考虑 $score$ 带来的剪枝效果,最坏情况下,每个位置都有两种选择,搜索所有方案的复杂度为 $O(2^n)$;同时搜索过程中会产生的新字符串(最终递归树中叶子节点的字符串长度最大为 $n$,使用 `StringBuilder` 也是同理),复杂度为 $O(n)$。整体复杂度为 $O(n * 2^n)$
168+
* 时间复杂度:预处理 $max$ 和 $len$ 的复杂度为 $O(n)$;不考虑 $score$ 带来的剪枝效果,最坏情况下,每个位置都有两种选择,搜索所有方案的复杂度为 $O(2^n)$;同时搜索过程中会产生的新字符串(最终递归树中叶子节点的字符串长度最大为 $n$,使用 `StringBuilder` 也是同理),复杂度为 $O(n)$。整体复杂度为 $O(n \times 2^n)$
173169
* 空间复杂度:最大合法方案数与字符串长度呈线性关系。复杂度为 $O(n)$
174170

175171
---

LeetCode/671-680/678. 有效的括号字符串(中等).md

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ Tag : 「有效括号问题」、「动态规划」、「模拟」
3636
```
3737

3838
注意:
39-
* 字符串大小将在 [1,100] 范围内。
39+
* 字符串大小将在 $[1,100]$ 范围内。
4040

4141
---
4242

0 commit comments

Comments
 (0)