Skip to content

feat: update lc problems #3591

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 1, 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
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ id 是该表的主键(具有唯一值的列)。
<p><strong>示例 1:</strong></p>

<pre>
<strong>输入:</strong>
<strong>输入:</strong>
Employee 表:
+----+-------+--------+-----------+
| id | name | salary | managerId |
Expand All @@ -54,7 +54,7 @@ Employee 表:
| 3 | Sam | 60000 | Null |
| 4 | Max | 90000 | Null |
+----+-------+--------+-----------+
<strong>输出:</strong>
<strong>输出:</strong>
+----------+
| Employee |
+----------+
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Each row of this table indicates the ID of an employee, their name, salary, and
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong>
<strong>Input:</strong>
Employee table:
+----+-------+--------+-----------+
| id | name | salary | managerId |
Expand All @@ -53,7 +53,7 @@ Employee table:
| 3 | Sam | 60000 | Null |
| 4 | Max | 90000 | Null |
+----+-------+--------+-----------+
<strong>Output:</strong>
<strong>Output:</strong>
+----------+
| Employee |
+----------+
Expand Down
31 changes: 20 additions & 11 deletions solution/0700-0799/0727.Minimum Window Subsequence/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,36 @@ tags:

<!-- description:start -->

<p>给定字符串 <code>S</code> and <code>T</code>,找出 <code>S</code> 中最短的(连续)<strong>子串</strong> <code>W</code> ,使得 <code>T</code> 是 <code>W</code> 的 <strong>子序列</strong> 。</p>
<p>给定字符串 <code>s1</code> 和&nbsp;<code>s2</code>,找出 <code>s1</code> 中最短的连续&nbsp;<strong>子串</strong>,使得 <code>s2</code> 是该子串的 <strong>子序列</strong> 。</p>

<p>如果 <code>S</code> 中没有窗口可以包含 <code>T</code> 中的所有字符,返回空字符串 <code>&quot;&quot;</code>。如果有不止一个最短长度的窗口,返回开始位置最靠左的那个。</p>
<p>如果 <code>s1</code> 中没有窗口可以包含 <code>s2</code> 中的所有字符,返回空字符串 <code>""</code>。如果有不止一个最短长度的窗口,返回 <strong>开始位置最靠左</strong> 的那个。</p>

<p><strong>示例 1:</strong></p>

<pre><strong>输入:</strong>
S = &quot;abcdebdde&quot;, T = &quot;bde&quot;
<strong>输出:</strong>&quot;bcde&quot;
<pre>
<strong>输入:</strong>
s1 = "abcdebdde", s2 = "bde"
<strong>输出:</strong>"bcde"
<strong>解释:</strong>
&quot;bcde&quot; 是答案,因为它在相同长度的字符串 &quot;bdde&quot; 出现之前。
&quot;deb&quot; 不是一个更短的答案,因为在窗口中必须按顺序出现 T 中的元素。</pre>
"bcde" 是答案,因为它在相同长度的字符串 "bdde" 出现之前。
"deb" 不是一个更短的答案,因为在窗口中必须按顺序出现 T 中的元素。
</pre>

<p><strong class="example">示例 2:</strong></p>

<pre>
<strong>输入:</strong>s1 = "jmeqksfrsdcmsiwvaovztaqenprpvnbstl", s2 = "u"
<b>输出:</b>""
</pre>

<p>&nbsp;</p>

<p><strong>:</strong></p>
<p><strong>提示:</strong></p>

<ul>
<li>所有输入的字符串都只包含小写字母。All the strings in the input will only contain lowercase letters.</li>
<li><code>S</code>&nbsp;长度的范围为&nbsp;<code>[1, 20000]</code></li>
<li><code>T</code>&nbsp;长度的范围为&nbsp;<code>[1, 100]</code>。</li>
<li><code>1 &lt;= s1.length &lt;= 2 * 10<sup>4</sup></code></li>
<li><code>1 &lt;= s2.length &lt;= 100</code></li>
<li><code>s1</code>&nbsp;&nbsp;<code>s2</code>&nbsp;只包含小写英文字母。</li>
</ul>

<p>&nbsp;</p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,13 @@ Other valid solutions are to put the green box in room 2 or to put the orange bo

<!-- solution:start -->

### Solution 1
### Solution 1: Preprocessing + Sorting + Greedy

First, we preprocess the warehouse to get the maximum height of each room. Then, we sort both the boxes and the warehouse. Starting with the smallest box and the smallest room, if the current room's height is greater than or equal to the current box's height, we can place the current box in the current room; otherwise, we continue to the next room.

Finally, we return the number of boxes that can be placed.

The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the warehouse.

<!-- tabs:start -->

Expand Down
146 changes: 73 additions & 73 deletions solution/1500-1599/1582.Special Positions in a Binary Matrix/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,15 @@ tags:

<!-- solution:start -->

### 方法一:模拟
### 方法一:计数

遍历矩阵 `mat`,先统计每一行,每一列中 `1` 的个数,分别记录在 `r` 和 `c` 数组中
我们可以用两个数组 $\textit{rows}$ 和 $\textit{cols}$ 分别记录每一行和每一列的 $1$ 的个数

然后再遍历矩阵 `mat`,如果 `mat[i][j] == 1` 且 `row[i] == 1` 且 `col[j] == 1`,则 $(i, j)$ 是特殊位置
然后遍历矩阵,对于每一个 $1$,检查其所在的行和列是否只有一个 $1$,如果是则答案加一

时间复杂度 $O(m\times n)$,空间复杂度 $O(m+n)$。其中 $m$, $n$ 分别是矩阵 `mat` 的行数和列数。
遍历结束后,返回答案即可。

时间复杂度 $O(m \times n)$,空间复杂度 $O(m + n)$。其中 $m$ 和 $n$ 分别是矩阵 $\textit{mat}$ 的行数和列数。

<!-- tabs:start -->

Expand All @@ -73,18 +75,16 @@ tags:
```python
class Solution:
def numSpecial(self, mat: List[List[int]]) -> int:
m, n = len(mat), len(mat[0])
r = [0] * m
c = [0] * n
rows = [0] * len(mat)
cols = [0] * len(mat[0])
for i, row in enumerate(mat):
for j, v in enumerate(row):
r[i] += v
c[j] += v
for j, x in enumerate(row):
rows[i] += x
cols[j] += x
ans = 0
for i in range(m):
for j in range(n):
if mat[i][j] == 1 and r[i] == 1 and c[j] == 1:
ans += 1
for i, row in enumerate(mat):
for j, x in enumerate(row):
ans += x == 1 and rows[i] == 1 and cols[j] == 1
return ans
```

Expand All @@ -94,22 +94,25 @@ class Solution:
class Solution {
public int numSpecial(int[][] mat) {
int m = mat.length, n = mat[0].length;
int[] r = new int[m];
int[] c = new int[n];
int[] rows = new int[m];
int[] cols = new int[n];

for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
r[i] += mat[i][j];
c[j] += mat[i][j];
rows[i] += mat[i][j];
cols[j] += mat[i][j];
}
}

int ans = 0;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (mat[i][j] == 1 && r[i] == 1 && c[j] == 1) {
++ans;
if (mat[i][j] == 1 && rows[i] == 1 && cols[j] == 1) {
ans++;
}
}
}

return ans;
}
}
Expand All @@ -122,21 +125,25 @@ class Solution {
public:
int numSpecial(vector<vector<int>>& mat) {
int m = mat.size(), n = mat[0].size();
vector<int> r(m), c(n);
vector<int> rows(m);
vector<int> cols(n);

for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
r[i] += mat[i][j];
c[j] += mat[i][j];
rows[i] += mat[i][j];
cols[j] += mat[i][j];
}
}

int ans = 0;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (mat[i][j] == 1 && r[i] == 1 && c[j] == 1) {
++ans;
if (mat[i][j] == 1 && rows[i] == 1 && cols[j] == 1) {
ans++;
}
}
}

return ans;
}
};
Expand All @@ -145,24 +152,23 @@ public:
#### Go

```go
func numSpecial(mat [][]int) int {
m, n := len(mat), len(mat[0])
r, c := make([]int, m), make([]int, n)
func numSpecial(mat [][]int) (ans int) {
rows := make([]int, len(mat))
cols := make([]int, len(mat[0]))
for i, row := range mat {
for j, v := range row {
r[i] += v
c[j] += v
for j, x := range row {
rows[i] += x
cols[j] += x
}
}
ans := 0
for i, x := range r {
for j, y := range c {
if mat[i][j] == 1 && x == 1 && y == 1 {
for i, row := range mat {
for j, x := range row {
if x == 1 && rows[i] == 1 && cols[j] == 1 {
ans++
}
}
}
return ans
return
}
```

Expand All @@ -172,27 +178,23 @@ func numSpecial(mat [][]int) int {
function numSpecial(mat: number[][]): number {
const m = mat.length;
const n = mat[0].length;
const rows = new Array(m).fill(0);
const cols = new Array(n).fill(0);
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
if (mat[i][j] === 1) {
rows[i]++;
cols[j]++;
}
const rows: number[] = Array(m).fill(0);
const cols: number[] = Array(n).fill(0);
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; ++j) {
rows[i] += mat[i][j];
cols[j] += mat[i][j];
}
}

let res = 0;
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
let ans = 0;
for (let i = 0; i < m; ++i) {
for (let j = 0; j < n; ++j) {
if (mat[i][j] === 1 && rows[i] === 1 && cols[j] === 1) {
res++;
++ans;
}
}
}

return res;
return ans;
}
```

Expand All @@ -212,15 +214,15 @@ impl Solution {
}
}

let mut res = 0;
let mut ans = 0;
for i in 0..m {
for j in 0..n {
if mat[i][j] == 1 && rows[i] == 1 && cols[j] == 1 {
res += 1;
ans += 1;
}
}
}
res
ans
}
}
```
Expand All @@ -229,31 +231,29 @@ impl Solution {

```c
int numSpecial(int** mat, int matSize, int* matColSize) {
int m = matSize;
int n = *matColSize;
int* rows = (int*) malloc(sizeof(int) * m);
int* cols = (int*) malloc(sizeof(int) * n);
memset(rows, 0, sizeof(int) * m);
memset(cols, 0, sizeof(int) * n);
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (mat[i][j] == 1) {
rows[i]++;
cols[j]++;
}
int m = matSize, n = matColSize[0];
int rows[m];
int cols[n];
memset(rows, 0, sizeof(rows));
memset(cols, 0, sizeof(cols));

for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
rows[i] += mat[i][j];
cols[j] += mat[i][j];
}
}
int res = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {

int ans = 0;
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (mat[i][j] == 1 && rows[i] == 1 && cols[j] == 1) {
res++;
ans++;
}
}
}
free(rows);
free(cols);
return res;

return ans;
}
```

Expand Down
Loading
Loading