diff --git a/.prettierignore b/.prettierignore index f76480ca0d15f..98948b2cd2533 100644 --- a/.prettierignore +++ b/.prettierignore @@ -24,4 +24,5 @@ node_modules/ /solution/2200-2299/2230.The Users That Are Eligible for Discount/Solution.sql /solution/2200-2299/2252.Dynamic Pivoting of a Table/Solution.sql /solution/2200-2299/2253.Dynamic Unpivoting of a Table/Solution.sql -/solution/3100-3199/3150.Invalid Tweets II/Solution.sql \ No newline at end of file +/solution/3100-3199/3150.Invalid Tweets II/Solution.sql +/solution/3100-3199/3198.Find Cities in Each State/Solution.sql \ No newline at end of file diff --git a/images/starcharts.svg b/images/starcharts.svg index 6baa47dab4e3a..339acfc0af351 100644 --- a/images/starcharts.svg +++ b/images/starcharts.svg @@ -1,4 +1,4 @@ - + \ No newline at end of file +L 950 14 +L 950 14 +L 950 14 +L 950 14 +L 950 14 +L 950 14 +L 950 14 +L 950 14 +L 950 14 +L 950 14 +L 950 14 +L 950 14 +L 950 14 +L 950 14 +L 950 14 +L 950 14 +L 950 14 +L 950 14 +L 950 14 +L 950 14 +L 950 14 +L 950 14 +L 950 14 +L 950 14 +L 950 14 +L 950 14 +L 950 14 +L 950 14 +L 950 14 +L 950 14" style="stroke-width:2;stroke:rgba(129,199,239,1.0);fill:none"/> \ No newline at end of file diff --git a/solution/0100-0199/0127.Word Ladder/README.md b/solution/0100-0199/0127.Word Ladder/README.md index ef4ace621e8a5..28d751002c5f0 100644 --- a/solution/0100-0199/0127.Word Ladder/README.md +++ b/solution/0100-0199/0127.Word Ladder/README.md @@ -18,7 +18,7 @@ tags: -
字典 wordList
中从单词 beginWord
和 endWord
的 转换序列 是一个按下述规格形成的序列 beginWord -> s1 -> s2 -> ... -> sk
:
字典 wordList
中从单词 beginWord
到 endWord
的 转换序列 是一个按下述规格形成的序列 beginWord -> s1 -> s2 -> ... -> sk
:
给定一个二叉搜索树的根节点 root
,和一个整数 k
,请你设计一个算法查找其中第 k
个最小元素(从 1 开始计数)。
给定一个二叉搜索树的根节点 root
,和一个整数 k
,请你设计一个算法查找其中第 k
小的元素(从 1 开始计数)。
+
示例 1:
+
-
+
提示:
n
。1 <= k <= n <= 104
0 <= Node.val <= 104
1 <= k <= n <= 104
0 <= Node.val <= 104
+
进阶:如果二叉搜索树经常被修改(插入/删除操作)并且你需要频繁地查找第 k
小的值,你将如何优化算法?
更正式的,如果选出来的行子集大小(即行的数量)为 k,那么每一列的和至多为 floor(k / 2)
。
请你返回一个整数数组,它包含好子集的行下标,请你将子集中的元素 升序 返回。
+请你返回一个整数数组,它包含好子集的行下标,请你将其 升序 返回。
如果有多个好子集,你可以返回任意一个。如果没有好子集,请你返回一个空数组。
@@ -89,7 +89,7 @@ tags: 我们可以从小到大考虑答案选择的行数 $k$。 - 如果 $k = 1$,每一列的和最大为 $0$,那么必须满足有一行的所有元素都是 $0$,否则无法满足条件。 -- 如果 $k = 2$,每一列的和最大为 $1$,那么必须存在有两行,且这两行的元素按位或之后的结果是 $0$,否则无法满足条件。 +- 如果 $k = 2$,每一列的和最大为 $1$,那么必须存在有两行,且这两行的元素按位与之后的结果是 $0$,否则无法满足条件。 - 如果 $k = 3$,每一列的和最大也是 $1$。如果 $k = 2$ 不满足条件,那么 $k = 3$ 也一定不满足条件,所以我们不需要考虑所有 $k \gt 2$ 且 $k$ 为奇数的情况。 - 如果 $k = 4$,每一列的和最大为 $2$,此时一定是 $k = 2$ 不满足条件,也就是说,任意选取两行,都存在至少一个列的和为 $2$。我们在 $4$ 行中任意选取 $2$ 行,一共有 $C_4^2 = 6$ 种选法,那么存在至少 $6$ 个 $2$ 的列。由于列数 $n \le 5$,所以一定存在至少一列的和大于 $2$,所以 $k = 4$ 也不满足条件。 - 对于 $k \gt 4$ 且 $k$ 为偶数的情况,我们可以得出同样的结论,即 $k$ 一定不满足条件。 diff --git a/solution/2700-2799/2732.Find a Good Subset of the Matrix/README_EN.md b/solution/2700-2799/2732.Find a Good Subset of the Matrix/README_EN.md index ea281d6093215..ef1d2c319dc45 100644 --- a/solution/2700-2799/2732.Find a Good Subset of the Matrix/README_EN.md +++ b/solution/2700-2799/2732.Find a Good Subset of the Matrix/README_EN.md @@ -87,7 +87,7 @@ The length of the chosen subset is 1. We can consider the number of rows $k$ chosen for the answer from smallest to largest. - If $k = 1$, the maximum sum of each column is $0$. Therefore, there must be a row where all elements are $0$, otherwise, the condition cannot be met. -- If $k = 2$, the maximum sum of each column is $1$. There must exist two rows, and the bitwise OR result of these two rows' elements is $0$, otherwise, the condition cannot be met. +- If $k = 2$, the maximum sum of each column is $1$. There must exist two rows, and the bitwise AND result of these two rows' elements is $0$, otherwise, the condition cannot be met. - If $k = 3$, the maximum sum of each column is also $1$. If the condition for $k = 2$ is not met, then the condition for $k = 3$ will definitely not be met either. Therefore, we do not need to consider any case where $k > 2$ and $k$ is odd. - If $k = 4$, the maximum sum of each column is $2$. This situation definitely occurs when the condition for $k = 2$ is not met, meaning that for any two selected rows, there exists at least one column with a sum of $2$. When choosing any 2 rows out of 4, there are a total of $C_4^2 = 6$ ways to choose, so there are at least $6$ columns with a sum of $2$. Since the number of columns $n \le 5$, there must be at least one column with a sum greater than $2$, so the condition for $k = 4$ is also not met. - For $k > 4$ and $k$ being even, we can draw the same conclusion, that $k$ definitely does not meet the condition. diff --git a/solution/2700-2799/2741.Special Permutations/README.md b/solution/2700-2799/2741.Special Permutations/README.md index f0e7b4cc7c07b..0ece2b8547030 100644 --- a/solution/2700-2799/2741.Special Permutations/README.md +++ b/solution/2700-2799/2741.Special Permutations/README.md @@ -62,7 +62,7 @@ tags: ### 方法一:状态压缩动态规划 -我们注意到题目中数组的长度最大不超过 $14$,因此,我们可以用一个整数来表示当前的状态,其中第 $i$ 位为 $1$ 表示数组中的第 $i$ 个数已经被选取,为 $0$ 表示数组中的第 $i$ 个数还未被选取。 +我们注意到题目中数组的长度最大不超过 $14$,因此,我们可以用一个二进制整数来表示当前的状态,其中第 $i$ 位为 $1$ 表示数组中的第 $i$ 个数已经被选取,为 $0$ 表示数组中的第 $i$ 个数还未被选取。 我们定义 $f[i][j]$ 表示当前选取的整数状态为 $i$,且最后一个选取的整数下标为 $j$ 的方案数。初始时 $f[0][0]=0$,答案为 $\sum_{j=0}^{n-1}f[2^n-1][j]$。 @@ -208,6 +208,73 @@ func specialPerm(nums []int) (ans int) { } ``` +#### TypeScript + +```ts +function specialPerm(nums: number[]): number { + const mod = 1e9 + 7; + const n = nums.length; + const m = 1 << n; + const f = Array.from({ length: m }, () => Array(n).fill(0)); + + for (let i = 1; i < m; ++i) { + for (let j = 0; j < n; ++j) { + if (((i >> j) & 1) === 1) { + const ii = i ^ (1 << j); + if (ii === 0) { + f[i][j] = 1; + continue; + } + for (let k = 0; k < n; ++k) { + if (nums[j] % nums[k] === 0 || nums[k] % nums[j] === 0) { + f[i][j] = (f[i][j] + f[ii][k]) % mod; + } + } + } + } + } + + return f[m - 1].reduce((acc, x) => (acc + x) % mod); +} +``` + +#### Rust + +```rust +impl Solution { + pub fn special_perm(nums: VecYou are given an integer array nums
of size n
and a positive integer k
.
You are given an integer array nums
of size n
where n
is a multiple of 3 and a positive integer k
.
Divide the array into one or more arrays of size 3
satisfying the following conditions:
Divide the array nums
into n / 3
arrays of size 3 satisfying the following condition:
nums
should be in exactly one array.k
.k
.Return a 2D array containing all the arrays. If it is impossible to satisfy the conditions, return an empty array. And if there are multiple answers, return any of them.
+Return a 2D array containing the arrays. If it is impossible to satisfy the conditions, return an empty array. And if there are multiple answers, return any of them.
Example 1:
--Input: nums = [1,3,4,8,7,9,3,5,1], k = 2 -Output: [[1,1,3],[3,4,5],[7,8,9]] -Explanation: We can divide the array into the following arrays: [1,1,3], [3,4,5] and [7,8,9]. -The difference between any two elements in each array is less than or equal to 2. -Note that the order of elements is not important. -+
Input: nums = [1,3,4,8,7,9,3,5,1], k = 2
+ +Output: [[1,1,3],[3,4,5],[7,8,9]]
+ +Explanation:
+ +The difference between any two elements in each array is less than or equal to 2.
+Example 2:
--Input: nums = [1,3,3,2,7,3], k = 3 -Output: [] -Explanation: It is not possible to divide the array satisfying all the conditions. -+
Input: nums = [2,4,2,2,5,2], k = 2
+ +Output: []
+ +Explanation:
+ +Different ways to divide nums
into 2 arrays of size 3 are:
Because there are four 2s there will be an array with the elements 2 and 5 no matter how we divide it. since 5 - 2 = 3 > k
, the condition is not satisfied and so there is no valid division.
Example 3:
+ +Input: nums = [4,2,9,8,2,12,7,12,10,5,8,5,5,7,9,2,5,11], k = 14
+ +Output: [[2,2,12],[4,8,5],[5,9,7],[7,8,5],[5,9,10],[11,12,2]]
+ +Explanation:
+ +The difference between any two elements in each array is less than or equal to 14.
+
Constraints:
@@ -56,7 +80,7 @@ Note that the order of elements is not important.n == nums.length
1 <= n <= 105
n
is a multiple of 3
.n
is a multiple of 31 <= nums[i] <= 105
1 <= k <= 105
Given a 2D array rooks
of length n
, where rooks[i] = [xi, yi]
indicates the position of a rook on an n x n
chess board. Your task is to move the rooks 1 cell at a time vertically or horizontally (to an adjacent cell) such that the board becomes peaceful.
给定一个长度为 n
的二维数组 rooks
,其中 rooks[i] = [xi, yi]
表示 n x n
棋盘上一个车的位置。你的任务是每次在垂直或水平方向上移动 1 格 车(到一个相邻的格子)使得棋盘变得 和平。
A board is peaceful if there is exactly one rook in each row and each column.
+如果每行每列都 只有 一个车,那么这块棋盘就是和平的。
-Return the minimum number of moves required to get a peaceful board.
+返回获得和平棋盘所需的 最少 步数。
-Note that at no point can there be two rooks in the same cell.
+注意 任何时刻 两个车都不能在同一个格子。
-
Example 1:
+ +示例 1:
Input: rooks = [[0,0],[1,0],[1,1]]
+输入:rooks = [[0,0],[1,0],[1,1]]
-Output: 3
+输出:3
-Explanation:
-解释:
+Example 2:
+示例 2:
Input: rooks = [[0,0],[0,1],[0,2],[0,3]]
+输入:rooks = [[0,0],[0,1],[0,2],[0,3]]
-Output: 6
+输出:6
-Explanation:
-解释:
+-
Constraints:
+ +提示:
1 <= n == rooks.length <= 500
0 <= xi, yi <= n - 1
Constraints:
diff --git a/solution/3100-3199/3194.Minimum Average of Smallest and Largest Elements/README.md b/solution/3100-3199/3194.Minimum Average of Smallest and Largest Elements/README.md index 11ed3314cbcd3..b013c5378d84d 100644 --- a/solution/3100-3199/3194.Minimum Average of Smallest and Largest Elements/README.md +++ b/solution/3100-3199/3194.Minimum Average of Smallest and Largest Elements/README.md @@ -2,6 +2,10 @@ comments: true difficulty: 简单 edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3194.Minimum%20Average%20of%20Smallest%20and%20Largest%20Elements/README.md +tags: + - 数组 + - 双指针 + - 排序 --- diff --git a/solution/3100-3199/3194.Minimum Average of Smallest and Largest Elements/README_EN.md b/solution/3100-3199/3194.Minimum Average of Smallest and Largest Elements/README_EN.md index 3bd07c1e4c890..80e412dd5c12f 100644 --- a/solution/3100-3199/3194.Minimum Average of Smallest and Largest Elements/README_EN.md +++ b/solution/3100-3199/3194.Minimum Average of Smallest and Largest Elements/README_EN.md @@ -2,6 +2,10 @@ comments: true difficulty: Easy edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3194.Minimum%20Average%20of%20Smallest%20and%20Largest%20Elements/README_EN.md +tags: + - Array + - Two Pointers + - Sorting --- diff --git a/solution/3100-3199/3195.Find the Minimum Area to Cover All Ones I/README_EN.md b/solution/3100-3199/3195.Find the Minimum Area to Cover All Ones I/README_EN.md index 791bf0115bf14..ee14207784ff8 100644 --- a/solution/3100-3199/3195.Find the Minimum Area to Cover All Ones I/README_EN.md +++ b/solution/3100-3199/3195.Find the Minimum Area to Cover All Ones I/README_EN.md @@ -36,7 +36,7 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3195.FiExample 2:
Input: grid = [[0,0],[1,0]]
+Input: grid = [[1,0],[0,0]]
Output: 1
diff --git a/solution/3100-3199/3198.Find Cities in Each State/README.md b/solution/3100-3199/3198.Find Cities in Each State/README.md new file mode 100644 index 0000000000000..d340f3d27c5d0 --- /dev/null +++ b/solution/3100-3199/3198.Find Cities in Each State/README.md @@ -0,0 +1,128 @@ +--- +comments: true +difficulty: 简单 +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3100-3199/3198.Find%20Cities%20in%20Each%20State/README.md +--- + + + +# [3198. 查找每个州的城市 🔒](https://leetcode.cn/problems/find-cities-in-each-state) + +[English Version](/solution/3100-3199/3198.Find%20Cities%20in%20Each%20State/README_EN.md) + +## 题目描述 + + + +表:cities
++-------------+---------+ +| Column Name | Type | ++-------------+---------+ +| state | varchar | +| city | varchar | ++-------------+---------+ +(state, city) 是这张表的主键(有不同值的列的组合)。 +这张表的每一行包含州名和其中的城市名。 ++ +
编写一个解决方案来 查找每个州的所有城市,并将它们组合成 一个逗号分隔 的字符串。
+ +返回结果表以 state
升序 排序。
结果格式如下所示。
+ ++ +
示例:
+ +输入:
+ +cities 表:
+ +++-------------+---------------+ +| state | city | ++-------------+---------------+ +| California | Los Angeles | +| California | San Francisco | +| California | San Diego | +| Texas | Houston | +| Texas | Austin | +| Texas | Dallas | +| New York | New York City | +| New York | Buffalo | +| New York | Rochester | ++-------------+---------------+ ++ +
输出:
+ +++-------------+---------------------------------------+ +| state | cities | ++-------------+---------------------------------------+ +| California | Los Angeles, San Diego, San Francisco | +| New York | Buffalo, New York City, Rochester | +| Texas | Austin, Dallas, Houston | ++-------------+---------------------------------------+ ++ +
解释:
+ +注意:输出表以州名升序排序。
+Table: cities
++-------------+---------+ +| Column Name | Type | ++-------------+---------+ +| state | varchar | +| city | varchar | ++-------------+---------+ +(state, city) is the primary key (combination of columns with unique values) for this table. +Each row of this table contains the state name and the city name within that state. ++ +
Write a solution to find all the cities in each state and combine them into a single comma-separated string.
+ +Return the result table ordered by state
in ascending order.
The result format is in the following example.
+ ++
Example:
+ +Input:
+ +cities table:
+ +++-------------+---------------+ +| state | city | ++-------------+---------------+ +| California | Los Angeles | +| California | San Francisco | +| California | San Diego | +| Texas | Houston | +| Texas | Austin | +| Texas | Dallas | +| New York | New York City | +| New York | Buffalo | +| New York | Rochester | ++-------------+---------------+ ++ +
Output:
+ +++-------------+---------------------------------------+ +| state | cities | ++-------------+---------------------------------------+ +| California | Los Angeles, San Diego, San Francisco | +| New York | Buffalo, New York City, Rochester | +| Texas | Austin, Dallas, Houston | ++-------------+---------------------------------------+ ++ +
Explanation:
+ +Note: The output table is ordered by the state name in ascending order.
+