Skip to content

feat: update solutions to lc problems: No.3319,3322 #3639

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion solution/0100-0199/0112.Path Sum/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ tags:
<pre>
<strong>Input:</strong> root = [1,2,3], targetSum = 5
<strong>Output:</strong> false
<strong>Explanation:</strong> There two root-to-leaf paths in the tree:
<strong>Explanation:</strong> There are two root-to-leaf paths in the tree:
(1 --&gt; 2): The sum is 3.
(1 --&gt; 3): The sum is 4.
There is no root-to-leaf path with sum = 5.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ tags:

<!-- description:start -->

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

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

Expand Down
10 changes: 5 additions & 5 deletions solution/0800-0899/0887.Super Egg Drop/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public:
int superEggDrop(int k, int n) {
int f[n + 1][k + 1];
memset(f, 0, sizeof(f));
function<int(int, int)> dfs = [&](int i, int j) -> int {
auto dfs = [&](auto&& dfs, int i, int j) -> int {
if (i < 1) {
return 0;
}
Expand All @@ -171,17 +171,17 @@ public:
int l = 1, r = i;
while (l < r) {
int mid = (l + r + 1) >> 1;
int a = dfs(mid - 1, j - 1);
int b = dfs(i - mid, j);
int a = dfs(dfs, mid - 1, j - 1);
int b = dfs(dfs, i - mid, j);
if (a <= b) {
l = mid;
} else {
r = mid - 1;
}
}
return f[i][j] = max(dfs(l - 1, j - 1), dfs(i - l, j)) + 1;
return f[i][j] = max(dfs(dfs, l - 1, j - 1), dfs(dfs, i - l, j)) + 1;
};
return dfs(n, k);
return dfs(dfs, n, k);
}
};
```
Expand Down
10 changes: 5 additions & 5 deletions solution/0800-0899/0887.Super Egg Drop/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public:
int superEggDrop(int k, int n) {
int f[n + 1][k + 1];
memset(f, 0, sizeof(f));
function<int(int, int)> dfs = [&](int i, int j) -> int {
auto dfs = [&](auto&& dfs, int i, int j) -> int {
if (i < 1) {
return 0;
}
Expand All @@ -154,17 +154,17 @@ public:
int l = 1, r = i;
while (l < r) {
int mid = (l + r + 1) >> 1;
int a = dfs(mid - 1, j - 1);
int b = dfs(i - mid, j);
int a = dfs(dfs, mid - 1, j - 1);
int b = dfs(dfs, i - mid, j);
if (a <= b) {
l = mid;
} else {
r = mid - 1;
}
}
return f[i][j] = max(dfs(l - 1, j - 1), dfs(i - l, j)) + 1;
return f[i][j] = max(dfs(dfs, l - 1, j - 1), dfs(dfs, i - l, j)) + 1;
};
return dfs(n, k);
return dfs(dfs, n, k);
}
};
```
Expand Down
12 changes: 6 additions & 6 deletions solution/0800-0899/0887.Super Egg Drop/Solution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class Solution {
int superEggDrop(int k, int n) {
int f[n + 1][k + 1];
memset(f, 0, sizeof(f));
function<int(int, int)> dfs = [&](int i, int j) -> int {
auto dfs = [&](auto&& dfs, int i, int j) -> int {
if (i < 1) {
return 0;
}
Expand All @@ -16,16 +16,16 @@ class Solution {
int l = 1, r = i;
while (l < r) {
int mid = (l + r + 1) >> 1;
int a = dfs(mid - 1, j - 1);
int b = dfs(i - mid, j);
int a = dfs(dfs, mid - 1, j - 1);
int b = dfs(dfs, i - mid, j);
if (a <= b) {
l = mid;
} else {
r = mid - 1;
}
}
return f[i][j] = max(dfs(l - 1, j - 1), dfs(i - l, j)) + 1;
return f[i][j] = max(dfs(dfs, l - 1, j - 1), dfs(dfs, i - l, j)) + 1;
};
return dfs(n, k);
return dfs(dfs, n, k);
}
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ It can be shown that no other mapping can provide a lower cost.
</pre>

<p><strong class="example">Example 2:</strong></p>
<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;" />
<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;" />
<pre>
<strong>Input:</strong> word = &quot;xyzxyzxyzxyz&quot;
<strong>Output:</strong> 12
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ tags:
<p><strong>Explanation:</strong></p>

<p>The subarray <code>[3]</code> has <code>OR</code> value of <code>3</code>. Hence, we return <code>1</code>.</p>

<p>Note that <code>[2]</code> is also a special subarray.</p>
</div>

<p><strong class="example">Example 2:</strong></p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3314.Co

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

<p>A <strong>prime number</strong> is a natural number greater than 1 with only two factors, 1 and itself.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3315.Co

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

<p>A <strong>prime number</strong> is a natural number greater than 1 with only two factors, 1 and itself.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3316.Fi

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

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

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,32 +83,213 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3319.K-

<!-- solution:start -->

### 方法一
### 方法一:DFS + 排序

我们定义一个函数 $\textit{dfs}$,用于计算以当前节点为根节点的完美二叉子树的大小,用一个数组 $\textit{nums}$ 记录所有完美二叉子树的大小。如果以当前节点为根节点的子树不是完美二叉子树,则返回 $-1$。

函数 $\textit{dfs}$ 的执行过程如下:

1. 如果当前节点为空,则返回 $0$;
2. 递归计算左子树和右子树的完美二叉子树的大小,分别记为 $l$ 和 $r$;
3. 如果左子树和右子树的大小不相等,或者左子树和右子树的大小小于 $0$,则返回 $-1$;
4. 计算当前节点的完美二叉子树的大小 $\textit{cnt} = l + r + 1$,并将 $\textit{cnt}$ 添加到数组 $\textit{nums}$ 中;
5. 返回 $\textit{cnt}$。

我们调用 $\textit{dfs}$ 函数计算出所有完美二叉子树的大小,如果数组 $\textit{nums}$ 的长度小于 $k$,则返回 $-1$,否则对数组 $\textit{nums}$ 进行降序排序,返回第 $k$ 大的完美二叉子树的大小。

时间复杂度 $O(n \times \log n)$,空间复杂度 $O(n)$。其中 $n$ 是二叉树的节点数。

<!-- tabs:start -->

#### Python3

```python

# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def kthLargestPerfectSubtree(self, root: Optional[TreeNode], k: int) -> int:
def dfs(root: Optional[TreeNode]) -> int:
if root is None:
return 0
l, r = dfs(root.left), dfs(root.right)
if l < 0 or l != r:
return -1
cnt = l + r + 1
nums.append(cnt)
return cnt

nums = []
dfs(root)
if len(nums) < k:
return -1
nums.sort(reverse=True)
return nums[k - 1]
```

#### Java

```java

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
private List<Integer> nums = new ArrayList<>();

public int kthLargestPerfectSubtree(TreeNode root, int k) {
dfs(root);
if (nums.size() < k) {
return -1;
}
nums.sort(Comparator.reverseOrder());
return nums.get(k - 1);
}

private int dfs(TreeNode root) {
if (root == null) {
return 0;
}
int l = dfs(root.left);
int r = dfs(root.right);
if (l < 0 || l != r) {
return -1;
}
int cnt = l + r + 1;
nums.add(cnt);
return cnt;
}
}
```

#### C++

```cpp

/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int kthLargestPerfectSubtree(TreeNode* root, int k) {
vector<int> nums;
auto dfs = [&](auto&& dfs, TreeNode* root) -> int {
if (!root) {
return 0;
}
int l = dfs(dfs, root->left);
int r = dfs(dfs, root->right);
if (l < 0 || l != r) {
return -1;
}
int cnt = l + r + 1;
nums.push_back(cnt);
return cnt;
};
dfs(dfs, root);
if (nums.size() < k) {
return -1;
}
ranges::sort(nums, greater<int>());
return nums[k - 1];
}
};
```

#### Go

```go
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func kthLargestPerfectSubtree(root *TreeNode, k int) int {
nums := []int{}
var dfs func(*TreeNode) int
dfs = func(root *TreeNode) int {
if root == nil {
return 0
}
l, r := dfs(root.Left), dfs(root.Right)
if l < 0 || l != r {
return -1
}
cnt := l + r + 1
nums = append(nums, cnt)
return cnt
}
dfs(root)
if len(nums) < k {
return -1
}
sort.Sort(sort.Reverse(sort.IntSlice(nums)))
return nums[k-1]
}
```

#### TypeScript

```ts
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/

function kthLargestPerfectSubtree(root: TreeNode | null, k: number): number {
const nums: number[] = [];
const dfs = (root: TreeNode | null): number => {
if (!root) {
return 0;
}
const l = dfs(root.left);
const r = dfs(root.right);
if (l < 0 || l !== r) {
return -1;
}
const cnt = l + r + 1;
nums.push(cnt);
return cnt;
};
dfs(root);
if (nums.length < k) {
return -1;
}
return nums.sort((a, b) => b - a)[k - 1];
}
```

<!-- tabs:end -->
Expand Down
Loading
Loading