Skip to content

Commit de9cec2

Browse files
authored
feat: add solutions to lc problem: No.983 (#3134)
No.0983.Minimum Cost For Tickets
1 parent 4959c39 commit de9cec2

File tree

34 files changed

+13523
-13165
lines changed

34 files changed

+13523
-13165
lines changed

solution/0100-0199/0191.Number of 1 Bits/README.md

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

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

20-
<p>编写一个函数,输入是一个无符号整数(以二进制串的形式),返回其二进制表达式中 <span data-keyword="set-bit">设置位</span> 的个数(也被称为<a href="https://baike.baidu.com/item/%E6%B1%89%E6%98%8E%E9%87%8D%E9%87%8F" target="_blank">汉明重量</a>)。</p>
20+
<p>编写一个函数,获取一个正整数的二进制形式并返回其二进制表达式中 <span data-keyword="set-bit">设置位</span> 的个数(也被称为<a href="https://baike.baidu.com/item/%E6%B1%89%E6%98%8E%E9%87%8D%E9%87%8F" target="_blank">汉明重量</a>)。</p>
2121

2222
<p>&nbsp;</p>
2323

solution/0400-0499/0419.Battleships in a Board/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@ tags:
1010

1111
<!-- problem:start -->
1212

13-
# [419. 甲板上的战舰](https://leetcode.cn/problems/battleships-in-a-board)
13+
# [419. 棋盘上的战舰](https://leetcode.cn/problems/battleships-in-a-board)
1414

1515
[English Version](/solution/0400-0499/0419.Battleships%20in%20a%20Board/README_EN.md)
1616

1717
## 题目描述
1818

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

21-
<p>给你一个大小为 <code>m x n</code> 的矩阵 <code>board</code> 表示甲板,其中,每个单元格可以是一艘战舰 <code>'X'</code> 或者是一个空位 <code>'.'</code> ,返回在甲板 <code>board</code> 上放置的 <strong>战舰</strong> 的数量。</p>
21+
<p>给你一个大小为 <code>m x n</code> 的矩阵 <code>board</code> 表示棋盘,其中,每个单元格可以是一艘战舰 <code>'X'</code> 或者是一个空位 <code>'.'</code> ,返回在棋盘 <code>board</code> 上放置的 <strong>舰队</strong> 的数量。</p>
2222

23-
<p><strong>战舰</strong> 只能水平或者垂直放置在 <code>board</code> 上。换句话说,战舰只能按 <code>1 x k</code>(<code>1</code> 行,<code>k</code> 列)或 <code>k x 1</code>(<code>k</code> 行,<code>1</code> 列)的形状建造,其中 <code>k</code> 可以是任意大小。两艘战舰之间至少有一个水平或垂直的空位分隔 (即没有相邻的战舰)。</p>
23+
<p><strong>舰队</strong> 只能水平或者垂直放置在 <code>board</code> 上。换句话说,舰队只能按 <code>1 x k</code>(<code>1</code> 行,<code>k</code> 列)或 <code>k x 1</code>(<code>k</code> 行,<code>1</code> 列)的形状放置,其中 <code>k</code> 可以是任意大小。两个舰队之间至少有一个水平或垂直的空格分隔 (即没有相邻的舰队)。</p>
2424

2525
<p>&nbsp;</p>
2626

solution/0500-0599/0558.Logical OR of Two Binary Grids Represented as Quad-Trees/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class Node {
5252

5353
<p><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/0500-0599/0558.Logical%20OR%20of%20Two%20Binary%20Grids%20Represented%20as%20Quad-Trees/images/new_top.png" style="height: 181px; width: 777px;" /></p>
5454

55-
<p>如果你想了解更多关于四叉树的内容,可以参考 <a href="https://en.wikipedia.org/wiki/Quadtree">wiki</a> 。</p>
55+
<p>如果你想了解更多关于四叉树的内容,可以参考 <a href="https://baike.baidu.com/item/%E5%9B%9B%E5%8F%89%E6%A0%91/8557650">百科</a>。</p>
5656

5757
<p><strong>四叉树格式:</strong></p>
5858

solution/0600-0699/0625.Minimum Factorization/README.md

+15-19
Original file line numberDiff line numberDiff line change
@@ -17,37 +17,33 @@ tags:
1717

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

20-
<p>给定一个正整数 <code>a</code>,找出最小的正整数 <code>b</code> 使得 <code>b</code> 的所有数位相乘恰好等于 <code>a</code>。</p>
20+
<p>给定一个正整数 <code>num</code>,找出最小的正整数 <code>x</code> 使得 <code>x</code> 的所有数位相乘恰好等于 <code>num</code>。</p>
2121

22-
<p>如果不存在这样的结果或者结果不是 32 位有符号整数,返回 0。</p>
22+
<p>如果不存在这样的结果或者结果不是 32 位有符号整数,返回 <code>0</code>。</p>
2323

2424
<p>&nbsp;</p>
2525

26-
<p><strong>样例 1</strong></p>
26+
<p><strong class="example">示例 1:</strong></p>
2727

28-
<p>输入:</p>
29-
30-
<pre>48
28+
<pre>
29+
<strong>输入:</strong>num = 48
30+
<strong>输出:</strong>68
3131
</pre>
3232

33-
<p>输出:</p>
34-
35-
<pre>68</pre>
36-
37-
<p>&nbsp;</p>
38-
39-
<p><strong>样例 2</strong></p>
40-
41-
<p>输入:</p>
33+
<p><strong class="example">示例 2:</strong></p>
4234

43-
<pre>15
35+
<pre>
36+
<strong>输入:</strong>num = 15
37+
<strong>输出:</strong>35
4438
</pre>
4539

46-
<p>输出:</p>
40+
<p>&nbsp;</p>
4741

48-
<pre>35</pre>
42+
<p><strong>提示:</strong></p>
4943

50-
<p>&nbsp;</p>
44+
<ul>
45+
<li><code>1 &lt;= num &lt;= 2<sup>31</sup> - 1</code></li>
46+
</ul>
5147

5248
<!-- description:end -->
5349

solution/0700-0799/0776.Split BST/README_EN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ tags:
1919

2020
<!-- description:start -->
2121

22-
<p>Given the <code>root</code> of a binary search tree (BST) and an integer <code>target</code>, split the tree into two subtrees where one subtree has nodes that are all smaller or equal to the target value, while the other subtree has all nodes that are greater than the target value. It Is not necessarily the case that the tree contains a node with the value <code>target</code>.</p>
22+
<p>Given the <code>root</code> of a binary search tree (BST) and an integer <code>target</code>, split the tree into two subtrees where the first subtree has nodes that are all smaller or equal to the target value, while the second subtree has all nodes that are greater than the target value. It is not necessarily the case that the tree contains a node with the value <code>target</code>.</p>
2323

2424
<p>Additionally, most of the structure of the original tree should remain. Formally, for any child <code>c</code> with parent <code>p</code> in the original tree, if they are both in the same subtree after the split, then node <code>c</code> should still have the parent <code>p</code>.</p>
2525

26-
<p>Return <em>an array of the two roots of the two subtrees</em>.</p>
26+
<p>Return <em>an array of the two roots of the two subtrees in order</em>.</p>
2727

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

solution/0900-0999/0979.Distribute Coins in Binary Tree/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ tags:
6161

6262
### 方法一:DFS
6363

64-
我们定义一个函数 $dfs(node)$,表示以 $node$ 为根节点的子树中,金币的超载量,即金币的数量减去节点数。如果 $dfs(node)$ 为正数,表示该子树中金币的数量多于节点数,需要将多余的金币移出该子树;如果 $dfs(node)$ 为负数,表示该子树中金币的数量少于节点数,需要将不足的金币移入该子树。
64+
我们定义一个函数 $\text{dfs(\text{node})}$,表示以 $\text{node}$ 为根节点的子树中,金币的超载量,即金币的数量减去节点数。如果 $\text{dfs(\text{node})}$ 为正数,表示该子树中金币的数量多于节点数,需要将多余的金币移出该子树;如果 $\text{dfs(\text{node})}$ 为负数,表示该子树中金币的数量少于节点数,需要将不足的金币移入该子树。
6565

66-
在函数 $dfs(node)$ 中,我们首先遍历左右子树,获得左右子树的金币超载量 $left$ 和 $right$。那么当前移动的次数需要加上 $|left| + |right|$,即将左右子树中的金币移动到当前节点。然后,我们返回整个子树的金币超载量,即 $left + right + node.val - 1$。
66+
在函数 $\text{dfs(\text{node})}$ 中,我们首先遍历左右子树,获得左右子树的金币超载量 $\text{left}$ 和 $\text{right}$。那么当前移动的次数需要加上 $|\text{left}| + |\text{right}|$,即将左右子树中的金币移动到当前节点。然后,我们返回整个子树的金币超载量,即 $\text{left} + \text{right} + \text{node.val} - 1$。
6767

6868
最后返回移动的次数即可。
6969

solution/0900-0999/0979.Distribute Coins in Binary Tree/README_EN.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,15 @@ tags:
5757

5858
<!-- solution:start -->
5959

60-
### Solution 1
60+
### Solution 1: DFS
61+
62+
We define a function $\text{dfs(node)}$, which represents the coin overload in the subtree rooted at $\text{node}$, i.e., the number of coins minus the number of nodes. If $\text{dfs(node)}$ is positive, it means the subtree has more coins than nodes, and the excess coins need to be moved out of the subtree; if $\text{dfs(node)}$ is negative, it means the subtree has fewer coins than nodes, and the shortfall needs to be moved into the subtree.
63+
64+
In the function $\text{dfs(node)}$, we first traverse the left and right subtrees to obtain the coin overload $\text{left}$ and $\text{right}$ of the left and right subtrees, respectively. Then, the current number of moves needs to be increased by $|\text{left}| + |\text{right}|$, which means moving the coins from the left and right subtrees to the current node. After that, we return the coin overload of the entire subtree, which is $\text{left} + \text{right} + \text{node.val} - 1$.
65+
66+
Finally, we return the number of moves.
67+
68+
The time complexity is $O(n)$, and the space complexity is $O(h)$. Here, $n$ and $h$ respectively represent the number of nodes and the height of the binary tree.
6169

6270
<!-- tabs:start -->
6371

solution/0900-0999/0982.Triples with Bitwise AND Equal To Zero/README_EN.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,15 @@ tags:
7171

7272
<!-- solution:start -->
7373

74-
### Solution 1
74+
### Solution 1: Enumeration + Counting
75+
76+
First, we enumerate any two numbers $x$ and $y$, and use a hash table or array $cnt$ to count the occurrences of their bitwise AND result $x \& y$.
77+
78+
Then, we enumerate the bitwise AND result $xy$, and enumerate $z$. If $xy \& z = 0$, then we add the value of $cnt[xy]$ to the answer.
79+
80+
Finally, we return the answer.
81+
82+
The time complexity is $O(n^2 + n \times M)$, and the space complexity is $O(M)$, where $n$ is the length of the array $nums$; and $M$ is the maximum value in the array $nums$, with $M \leq 2^{16}$ in this problem.
7583

7684
<!-- tabs:start -->
7785

0 commit comments

Comments
 (0)