Skip to content

Commit 5d8c2f9

Browse files
authored
feat: add solutions to lc problems: No.3319,3322 (doocs#3639)
1 parent 6dfa077 commit 5d8c2f9

File tree

25 files changed

+969
-33
lines changed

25 files changed

+969
-33
lines changed

solution/0100-0199/0112.Path Sum/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ tags:
3737
<pre>
3838
<strong>Input:</strong> root = [1,2,3], targetSum = 5
3939
<strong>Output:</strong> false
40-
<strong>Explanation:</strong> There two root-to-leaf paths in the tree:
40+
<strong>Explanation:</strong> There are two root-to-leaf paths in the tree:
4141
(1 --&gt; 2): The sum is 3.
4242
(1 --&gt; 3): The sum is 4.
4343
There is no root-to-leaf path with sum = 5.

solution/0400-0499/0442.Find All Duplicates in an Array/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ tags:
1717

1818
<!-- description:start -->
1919

20-
<p>Given an integer array <code>nums</code> of length <code>n</code> where all the integers of <code>nums</code> are in the range <code>[1, n]</code> and each integer appears <strong>once</strong> or <strong>twice</strong>, return <em>an array of all the integers that appears <strong>twice</strong></em>.</p>
20+
<p>Given an integer array <code>nums</code> of length <code>n</code> where all the integers of <code>nums</code> are in the range <code>[1, n]</code> and each integer appears <strong>at most</strong> <strong>twice</strong>, return <em>an array of all the integers that appears <strong>twice</strong></em>.</p>
2121

2222
<p>You must write an algorithm that runs in <code>O(n)</code> time and uses only <em>constant</em> auxiliary space, excluding the space needed to store the output</p>
2323

solution/0800-0899/0887.Super Egg Drop/README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public:
158158
int superEggDrop(int k, int n) {
159159
int f[n + 1][k + 1];
160160
memset(f, 0, sizeof(f));
161-
function<int(int, int)> dfs = [&](int i, int j) -> int {
161+
auto dfs = [&](auto&& dfs, int i, int j) -> int {
162162
if (i < 1) {
163163
return 0;
164164
}
@@ -171,17 +171,17 @@ public:
171171
int l = 1, r = i;
172172
while (l < r) {
173173
int mid = (l + r + 1) >> 1;
174-
int a = dfs(mid - 1, j - 1);
175-
int b = dfs(i - mid, j);
174+
int a = dfs(dfs, mid - 1, j - 1);
175+
int b = dfs(dfs, i - mid, j);
176176
if (a <= b) {
177177
l = mid;
178178
} else {
179179
r = mid - 1;
180180
}
181181
}
182-
return f[i][j] = max(dfs(l - 1, j - 1), dfs(i - l, j)) + 1;
182+
return f[i][j] = max(dfs(dfs, l - 1, j - 1), dfs(dfs, i - l, j)) + 1;
183183
};
184-
return dfs(n, k);
184+
return dfs(dfs, n, k);
185185
}
186186
};
187187
```

solution/0800-0899/0887.Super Egg Drop/README_EN.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public:
141141
int superEggDrop(int k, int n) {
142142
int f[n + 1][k + 1];
143143
memset(f, 0, sizeof(f));
144-
function<int(int, int)> dfs = [&](int i, int j) -> int {
144+
auto dfs = [&](auto&& dfs, int i, int j) -> int {
145145
if (i < 1) {
146146
return 0;
147147
}
@@ -154,17 +154,17 @@ public:
154154
int l = 1, r = i;
155155
while (l < r) {
156156
int mid = (l + r + 1) >> 1;
157-
int a = dfs(mid - 1, j - 1);
158-
int b = dfs(i - mid, j);
157+
int a = dfs(dfs, mid - 1, j - 1);
158+
int b = dfs(dfs, i - mid, j);
159159
if (a <= b) {
160160
l = mid;
161161
} else {
162162
r = mid - 1;
163163
}
164164
}
165-
return f[i][j] = max(dfs(l - 1, j - 1), dfs(i - l, j)) + 1;
165+
return f[i][j] = max(dfs(dfs, l - 1, j - 1), dfs(dfs, i - l, j)) + 1;
166166
};
167-
return dfs(n, k);
167+
return dfs(dfs, n, k);
168168
}
169169
};
170170
```

solution/0800-0899/0887.Super Egg Drop/Solution.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ class Solution {
33
int superEggDrop(int k, int n) {
44
int f[n + 1][k + 1];
55
memset(f, 0, sizeof(f));
6-
function<int(int, int)> dfs = [&](int i, int j) -> int {
6+
auto dfs = [&](auto&& dfs, int i, int j) -> int {
77
if (i < 1) {
88
return 0;
99
}
@@ -16,16 +16,16 @@ class Solution {
1616
int l = 1, r = i;
1717
while (l < r) {
1818
int mid = (l + r + 1) >> 1;
19-
int a = dfs(mid - 1, j - 1);
20-
int b = dfs(i - mid, j);
19+
int a = dfs(dfs, mid - 1, j - 1);
20+
int b = dfs(dfs, i - mid, j);
2121
if (a <= b) {
2222
l = mid;
2323
} else {
2424
r = mid - 1;
2525
}
2626
}
27-
return f[i][j] = max(dfs(l - 1, j - 1), dfs(i - l, j)) + 1;
27+
return f[i][j] = max(dfs(dfs, l - 1, j - 1), dfs(dfs, i - l, j)) + 1;
2828
};
29-
return dfs(n, k);
29+
return dfs(dfs, n, k);
3030
}
31-
};
31+
};

solution/3000-3099/3016.Minimum Number of Pushes to Type Word II/README_EN.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ It can be shown that no other mapping can provide a lower cost.
4949
</pre>
5050

5151
<p><strong class="example">Example 2:</strong></p>
52-
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3000-3099/3016.Minimum%20Number%20of%20Pushes%20to%20Type%20Word%20II/images/keypadv2e2.png" style="width: 329px; height: 313px;" />
52+
<img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3000-3099/3016.Minimum%20Number%20of%20Pushes%20to%20Type%20Word%20II/images/edited.png" style="width: 329px; height: 313px;" />
5353
<pre>
5454
<strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot;
5555
<strong>Output:</strong> 12

solution/3000-3099/3095.Shortest Subarray With OR at Least K I/README_EN.md

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ tags:
3737
<p><strong>Explanation:</strong></p>
3838

3939
<p>The subarray <code>[3]</code> has <code>OR</code> value of <code>3</code>. Hence, we return <code>1</code>.</p>
40+
41+
<p>Note that <code>[2]</code> is also a special subarray.</p>
4042
</div>
4143

4244
<p><strong class="example">Example 2:</strong></p>

solution/3300-3399/3314.Construct the Minimum Bitwise Array I/README_EN.md

-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3314.Co
2222

2323
<p>If it is <em>not possible</em> to find such a value for <code>ans[i]</code> that satisfies the <strong>condition</strong>, then set <code>ans[i] = -1</code>.</p>
2424

25-
<p>A <strong>prime number</strong> is a natural number greater than 1 with only two factors, 1 and itself.</p>
26-
2725
<p>&nbsp;</p>
2826
<p><strong class="example">Example 1:</strong></p>
2927

solution/3300-3399/3315.Construct the Minimum Bitwise Array II/README_EN.md

-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3315.Co
2222

2323
<p>If it is <em>not possible</em> to find such a value for <code>ans[i]</code> that satisfies the <strong>condition</strong>, then set <code>ans[i] = -1</code>.</p>
2424

25-
<p>A <strong>prime number</strong> is a natural number greater than 1 with only two factors, 1 and itself.</p>
26-
2725
<p>&nbsp;</p>
2826
<p><strong class="example">Example 1:</strong></p>
2927

solution/3300-3399/3316.Find Maximum Removals From Source String/README_EN.md

-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3316.Fi
2727

2828
<p>Return the <strong>maximum</strong> number of <em>operations</em> that can be performed.</p>
2929

30-
<p>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.</p>
31-
3230
<p>&nbsp;</p>
3331
<p><strong class="example">Example 1:</strong></p>
3432

solution/3300-3399/3319.K-th Largest Perfect Subtree Size in Binary Tree/README.md

+185-4
Original file line numberDiff line numberDiff line change
@@ -83,32 +83,213 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3319.K-
8383

8484
<!-- solution:start -->
8585

86-
### 方法一
86+
### 方法一:DFS + 排序
87+
88+
我们定义一个函数 $\textit{dfs}$,用于计算以当前节点为根节点的完美二叉子树的大小,用一个数组 $\textit{nums}$ 记录所有完美二叉子树的大小。如果以当前节点为根节点的子树不是完美二叉子树,则返回 $-1$。
89+
90+
函数 $\textit{dfs}$ 的执行过程如下:
91+
92+
1. 如果当前节点为空,则返回 $0$;
93+
2. 递归计算左子树和右子树的完美二叉子树的大小,分别记为 $l$ 和 $r$;
94+
3. 如果左子树和右子树的大小不相等,或者左子树和右子树的大小小于 $0$,则返回 $-1$;
95+
4. 计算当前节点的完美二叉子树的大小 $\textit{cnt} = l + r + 1$,并将 $\textit{cnt}$ 添加到数组 $\textit{nums}$ 中;
96+
5. 返回 $\textit{cnt}$。
97+
98+
我们调用 $\textit{dfs}$ 函数计算出所有完美二叉子树的大小,如果数组 $\textit{nums}$ 的长度小于 $k$,则返回 $-1$,否则对数组 $\textit{nums}$ 进行降序排序,返回第 $k$ 大的完美二叉子树的大小。
99+
100+
时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 是二叉树的节点数。
87101

88102
<!-- tabs:start -->
89103

90104
#### Python3
91105

92106
```python
93-
107+
# Definition for a binary tree node.
108+
# class TreeNode:
109+
# def __init__(self, val=0, left=None, right=None):
110+
# self.val = val
111+
# self.left = left
112+
# self.right = right
113+
class Solution:
114+
def kthLargestPerfectSubtree(self, root: Optional[TreeNode], k: int) -> int:
115+
def dfs(root: Optional[TreeNode]) -> int:
116+
if root is None:
117+
return 0
118+
l, r = dfs(root.left), dfs(root.right)
119+
if l < 0 or l != r:
120+
return -1
121+
cnt = l + r + 1
122+
nums.append(cnt)
123+
return cnt
124+
125+
nums = []
126+
dfs(root)
127+
if len(nums) < k:
128+
return -1
129+
nums.sort(reverse=True)
130+
return nums[k - 1]
94131
```
95132

96133
#### Java
97134

98135
```java
99-
136+
/**
137+
* Definition for a binary tree node.
138+
* public class TreeNode {
139+
* int val;
140+
* TreeNode left;
141+
* TreeNode right;
142+
* TreeNode() {}
143+
* TreeNode(int val) { this.val = val; }
144+
* TreeNode(int val, TreeNode left, TreeNode right) {
145+
* this.val = val;
146+
* this.left = left;
147+
* this.right = right;
148+
* }
149+
* }
150+
*/
151+
class Solution {
152+
private List<Integer> nums = new ArrayList<>();
153+
154+
public int kthLargestPerfectSubtree(TreeNode root, int k) {
155+
dfs(root);
156+
if (nums.size() < k) {
157+
return -1;
158+
}
159+
nums.sort(Comparator.reverseOrder());
160+
return nums.get(k - 1);
161+
}
162+
163+
private int dfs(TreeNode root) {
164+
if (root == null) {
165+
return 0;
166+
}
167+
int l = dfs(root.left);
168+
int r = dfs(root.right);
169+
if (l < 0 || l != r) {
170+
return -1;
171+
}
172+
int cnt = l + r + 1;
173+
nums.add(cnt);
174+
return cnt;
175+
}
176+
}
100177
```
101178

102179
#### C++
103180

104181
```cpp
105-
182+
/**
183+
* Definition for a binary tree node.
184+
* struct TreeNode {
185+
* int val;
186+
* TreeNode *left;
187+
* TreeNode *right;
188+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
189+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
190+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
191+
* };
192+
*/
193+
class Solution {
194+
public:
195+
int kthLargestPerfectSubtree(TreeNode* root, int k) {
196+
vector<int> nums;
197+
auto dfs = [&](auto&& dfs, TreeNode* root) -> int {
198+
if (!root) {
199+
return 0;
200+
}
201+
int l = dfs(dfs, root->left);
202+
int r = dfs(dfs, root->right);
203+
if (l < 0 || l != r) {
204+
return -1;
205+
}
206+
int cnt = l + r + 1;
207+
nums.push_back(cnt);
208+
return cnt;
209+
};
210+
dfs(dfs, root);
211+
if (nums.size() < k) {
212+
return -1;
213+
}
214+
ranges::sort(nums, greater<int>());
215+
return nums[k - 1];
216+
}
217+
};
106218
```
107219
108220
#### Go
109221
110222
```go
223+
/**
224+
* Definition for a binary tree node.
225+
* type TreeNode struct {
226+
* Val int
227+
* Left *TreeNode
228+
* Right *TreeNode
229+
* }
230+
*/
231+
func kthLargestPerfectSubtree(root *TreeNode, k int) int {
232+
nums := []int{}
233+
var dfs func(*TreeNode) int
234+
dfs = func(root *TreeNode) int {
235+
if root == nil {
236+
return 0
237+
}
238+
l, r := dfs(root.Left), dfs(root.Right)
239+
if l < 0 || l != r {
240+
return -1
241+
}
242+
cnt := l + r + 1
243+
nums = append(nums, cnt)
244+
return cnt
245+
}
246+
dfs(root)
247+
if len(nums) < k {
248+
return -1
249+
}
250+
sort.Sort(sort.Reverse(sort.IntSlice(nums)))
251+
return nums[k-1]
252+
}
253+
```
111254

255+
#### TypeScript
256+
257+
```ts
258+
/**
259+
* Definition for a binary tree node.
260+
* class TreeNode {
261+
* val: number
262+
* left: TreeNode | null
263+
* right: TreeNode | null
264+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
265+
* this.val = (val===undefined ? 0 : val)
266+
* this.left = (left===undefined ? null : left)
267+
* this.right = (right===undefined ? null : right)
268+
* }
269+
* }
270+
*/
271+
272+
function kthLargestPerfectSubtree(root: TreeNode | null, k: number): number {
273+
const nums: number[] = [];
274+
const dfs = (root: TreeNode | null): number => {
275+
if (!root) {
276+
return 0;
277+
}
278+
const l = dfs(root.left);
279+
const r = dfs(root.right);
280+
if (l < 0 || l !== r) {
281+
return -1;
282+
}
283+
const cnt = l + r + 1;
284+
nums.push(cnt);
285+
return cnt;
286+
};
287+
dfs(root);
288+
if (nums.length < k) {
289+
return -1;
290+
}
291+
return nums.sort((a, b) => b - a)[k - 1];
292+
}
112293
```
113294

114295
<!-- tabs:end -->

0 commit comments

Comments
 (0)