Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.3368
Browse files Browse the repository at this point in the history
No.3368.First Letter Capitalization
  • Loading branch information
yanglbme committed Nov 25, 2024
1 parent 78e0f6b commit a14c0f2
Show file tree
Hide file tree
Showing 9 changed files with 205 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,14 @@ tags:
<p>对于每个查询&nbsp;<code>queries[i]</code>:</p>

<ul>
<li>在&nbsp;<code>nums</code>&nbsp;的下标范围&nbsp;<code>[l<sub>i</sub>, r<sub>i</sub>]</code>&nbsp;内选择一个下标子集。</li>
<li>在&nbsp;<code>nums</code>&nbsp;的下标范围&nbsp;<code>[l<sub>i</sub>, r<sub>i</sub>]</code>&nbsp;内选择一个下标 <span data-keyword="subset">子集</span>。</li>
<li>将选中的每个下标对应的元素值减 1。</li>
</ul>

<p><strong>零数组&nbsp;</strong>是指所有元素都等于 0 的数组。</p>

<p>如果在按顺序处理所有查询后,可以将 <code>nums</code> 转换为&nbsp;<strong>零数组&nbsp;</strong>,则返回 <code>true</code>,否则返回 <code>false</code>。</p>

<p>数组的&nbsp;<strong>子集&nbsp;</strong>是对数组元素的选择(可能为空)。</p>

<p>&nbsp;</p>

<p><strong class="example">示例 1:</strong></p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,39 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3359.Fi

<!-- problem:start -->

# [3359. Find Sorted Submatrices With Maximum Element at Most K 🔒](https://leetcode.cn/problems/find-sorted-submatrices-with-maximum-element-at-most-k)
# [3359. 查找最大元素不超过 K 的有序子矩阵 🔒](https://leetcode.cn/problems/find-sorted-submatrices-with-maximum-element-at-most-k)

[English Version](/solution/3300-3399/3359.Find%20Sorted%20Submatrices%20With%20Maximum%20Element%20at%20Most%20K/README_EN.md)

## 题目描述

<!-- description:start -->

<p>You are given a 2D matrix <code>grid</code> of size <code>m x n</code>. You are also given a <strong>non-negative</strong> integer <code>k</code>.</p>
<p>给定一个大小为&nbsp;<code>m x n</code>&nbsp;的二维矩阵&nbsp;<code>grid</code>。同时给定一个 <strong>非负整数</strong>&nbsp;<code>k</code></p>

<p>Return the number of <strong>submatrices</strong> of <code>grid</code> that satisfy the following conditions:</p>
<p>返回满足下列条件的&nbsp;<code>grid</code>&nbsp;的子矩阵:</p>

<ul>
<li>The maximum element in the submatrix <strong>less than or equal to</strong> <code>k</code>.</li>
<li>Each row in the submatrix is sorted in <strong>non-increasing</strong> order.</li>
<li>子矩阵中最大的元素 <b>小于等于</b>&nbsp;<code>k</code></li>
<li>子矩阵的每一行都以 <strong>非递增</strong> 顺序排序。</li>
</ul>

<p>A submatrix <code>(x1, y1, x2, y2)</code> is a matrix that forms by choosing all cells <code>grid[x][y]</code> where <code>x1 &lt;= x &lt;= x2</code> and <code>y1 &lt;= y &lt;= y2</code>.</p>
<p>矩阵的子矩阵&nbsp;<code>(x1, y1, x2, y2)</code>&nbsp;是通过选择所有满足&nbsp;<code>x1 &lt;= x &lt;= x2</code>&nbsp;&nbsp;<code>y1 &lt;= y &lt;= y2</code>&nbsp;&nbsp;<code>grid[x][y]</code> 元素组成的矩阵。</p>

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

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

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[4,3,2,1],[8,7,6,1]], k = 3</span></p>
<p><span class="example-io"><b>输入:</b>grid = [[4,3,2,1],[8,7,6,1]], k = 3</span></p>

<p><strong>Output:</strong> <span class="example-io">8</span></p>
<p><strong>输出:</strong><span class="example-io">8</span></p>

<p><strong>Explanation:</strong></p>
<p><strong>解释:</strong></p>

<p><strong><img alt="" src="https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/3300-3399/3359.Find%20Sorted%20Submatrices%20With%20Maximum%20Element%20at%20Most%20K/images/mine.png" style="width: 360px; height: 200px;" /></strong></p>

<p>The 8 submatrices are:</p>
<p>8 个子矩阵分别是:</p>

<ul>
<li><code>[[1]]</code></li>
Expand All @@ -51,28 +52,29 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3300-3399/3359.Fi
</ul>
</div>

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

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[1,1,1],[1,1,1],[1,1,1]], k = 1</span></p>
<p><span class="example-io"><b>输入:</b>grid = [[1,1,1],[1,1,1],[1,1,1]], k = 1</span></p>

<p><strong>Output:</strong> <span class="example-io">36</span></p>
<p><span class="example-io"><b>输出:</b>36</span></p>

<p><strong>Explanation:</strong></p>
<p><strong>解释:</strong></p>

<p>There are 36 submatrices of grid. All submatrices have their maximum element equal to 1.</p>
<p>矩阵中有 36 个子矩阵。所有子矩阵的最大元素都等于 1。</p>
</div>

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

<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">grid = [[1]], k = 1</span></p>
<p><span class="example-io"><b>输入:</b>grid = [[1]], k = 1</span></p>

<p><strong>Output:</strong> <span class="example-io">1</span></p>
<p><span class="example-io"><b>输出:</b>1</span></p>
</div>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

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

<ul>
<li><code>1 &lt;= m == grid.length &lt;= 10<sup>3</sup></code></li>
Expand Down
103 changes: 78 additions & 25 deletions solution/3300-3399/3368.First Letter Capitalization/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ tags:

<!-- problem:start -->

# [3368. First Letter Capitalization 🔒](https://leetcode.cn/problems/first-letter-capitalization)
# [3368. 首字母大写 🔒](https://leetcode.cn/problems/first-letter-capitalization)

[English Version](/solution/3300-3399/3368.First%20Letter%20Capitalization/README_EN.md)

## 题目描述

<!-- description:start -->

<p>Table: <code>user_content</code></p>
<p>表:<code>user_content</code></p>

<pre>
+-------------+---------+
Expand All @@ -25,31 +25,32 @@ tags:
| content_id | int |
| content_text| varchar |
+-------------+---------+
content_id is the unique key for this table.
Each row contains a unique ID and the corresponding text content.
content_id 是这张表的唯一主键。
每一行包含一个不同的 ID 以及对应的文本内容。
</pre>

<p>Write a solution to transform the text in the <code>content_text</code> column by applying the following rules:</p>
<p>编写一个解决方案来通过应用以下规则来转换&nbsp;<code>content_text</code>&nbsp;列中的文本:</p>

<ul>
<li>Convert the first letter of each word to uppercase</li>
<li>Keep all other letters in lowercase</li>
<li>Preserve all existing spaces</li>
<li>把每个单词的首字母变成大写</li>
<li>其它字母保持小写</li>
<li>保留所有现有空格</li>
</ul>

<p><strong>Note</strong>: There will be no special character in <code>content_text</code>.</p>
<p><b>注意:</b><code>content_text</code>&nbsp;中没有特殊字符。</p>

<p>Return <em>the result table that includes both the original <code>content_text</code> and the modified text where each word starts with a capital letter</em>.</p>
<p>返回结果表,同时包含原来的<em>&nbsp;<code>content_text</code>&nbsp;</em>以及将所有单词首字母变成大写的修改后文本。</p>

<p>The result format is in the following example.</p>
<p>结果格式如下所示。</p>

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

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

<div class="example-block">
<p><strong>Input:</strong></p>
<p><strong>输入:</strong></p>

<p>user_content table:</p>
<p>user_content 表:</p>

<pre class="example-io">
+------------+-----------------------------------+
Expand All @@ -62,7 +63,7 @@ Each row contains a unique ID and the corresponding text content.
+------------+-----------------------------------+
</pre>

<p><strong>Output:</strong></p>
<p><strong>输出:</strong></p>

<pre class="example-io">
+------------+-----------------------------------+-----------------------------------+
Expand All @@ -75,28 +76,28 @@ Each row contains a unique ID and the corresponding text content.
+------------+-----------------------------------+-----------------------------------+
</pre>

<p><strong>Explanation:</strong></p>
<p><strong>解释:</strong></p>

<ul>
<li>For content_id = 1:
<li>对于 content_id = 1:
<ul>
<li>Each word&#39;s first letter is capitalized: Hello World Of SQL</li>
<li>每个单词的首字母都已经大写:Hello World Of SQL</li>
</ul>
</li>
<li>For content_id = 2:
<li>对于 content_id = 2:
<ul>
<li>Original mixed-case text is transformed to title case: The Quick Brown Fox</li>
<li>原来混合大小写的文本变为首字母大写:The Quick Brown Fox</li>
</ul>
</li>
<li>For content_id = 3:
<li>对于 content_id = 3:
<ul>
<li>The word AND&nbsp;is converted to &quot;And&quot;: &quot;Data Science And Machine Learning&quot;</li>
<li>单词 AND 被转换为 "And":"Data Science And Machine Learning"</li>
</ul>
</li>
<li>For content_id = 4:
<li>对于 content_id = 4:
<ul>
<li>Handles&nbsp;word TOP rated&nbsp;correctly: Top Rated</li>
<li>Converts BOOKS&nbsp;from all caps to title case: Books</li>
<li>正确处理单词 TOP ratedTop Rated</li>
<li> BOOKS 从全大写改为首字母大写:Books</li>
</ul>
</li>
</ul>
Expand All @@ -115,7 +116,59 @@ Each row contains a unique ID and the corresponding text content.
#### MySQL

```sql
WITH RECURSIVE
capitalized_words AS (
SELECT
content_id,
content_text,
SUBSTRING_INDEX(content_text, ' ', 1) AS word,
SUBSTRING(
content_text,
LENGTH(SUBSTRING_INDEX(content_text, ' ', 1)) + 2
) AS remaining_text,
CONCAT(
UPPER(LEFT(SUBSTRING_INDEX(content_text, ' ', 1), 1)),
LOWER(SUBSTRING(SUBSTRING_INDEX(content_text, ' ', 1), 2))
) AS processed_word
FROM user_content
UNION ALL
SELECT
c.content_id,
c.content_text,
SUBSTRING_INDEX(c.remaining_text, ' ', 1),
SUBSTRING(c.remaining_text, LENGTH(SUBSTRING_INDEX(c.remaining_text, ' ', 1)) + 2),
CONCAT(
c.processed_word,
' ',
CONCAT(
UPPER(LEFT(SUBSTRING_INDEX(c.remaining_text, ' ', 1), 1)),
LOWER(SUBSTRING(SUBSTRING_INDEX(c.remaining_text, ' ', 1), 2))
)
)
FROM capitalized_words c
WHERE c.remaining_text != ''
)
SELECT
content_id,
content_text AS original_text,
MAX(processed_word) AS converted_text
FROM capitalized_words
GROUP BY 1, 2;
```

#### Pandas

```python
import pandas as pd


def process_text(user_content: pd.DataFrame) -> pd.DataFrame:
user_content["converted_text"] = user_content["content_text"].apply(
lambda text: " ".join(word.capitalize() for word in text.split(" "))
)
return user_content[["content_id", "content_text", "converted_text"]].rename(
columns={"content_text": "original_text"}
)
```

<!-- tabs:end -->
Expand Down
52 changes: 52 additions & 0 deletions solution/3300-3399/3368.First Letter Capitalization/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,59 @@ Each row contains a unique ID and the corresponding text content.
#### MySQL

```sql
WITH RECURSIVE
capitalized_words AS (
SELECT
content_id,
content_text,
SUBSTRING_INDEX(content_text, ' ', 1) AS word,
SUBSTRING(
content_text,
LENGTH(SUBSTRING_INDEX(content_text, ' ', 1)) + 2
) AS remaining_text,
CONCAT(
UPPER(LEFT(SUBSTRING_INDEX(content_text, ' ', 1), 1)),
LOWER(SUBSTRING(SUBSTRING_INDEX(content_text, ' ', 1), 2))
) AS processed_word
FROM user_content
UNION ALL
SELECT
c.content_id,
c.content_text,
SUBSTRING_INDEX(c.remaining_text, ' ', 1),
SUBSTRING(c.remaining_text, LENGTH(SUBSTRING_INDEX(c.remaining_text, ' ', 1)) + 2),
CONCAT(
c.processed_word,
' ',
CONCAT(
UPPER(LEFT(SUBSTRING_INDEX(c.remaining_text, ' ', 1), 1)),
LOWER(SUBSTRING(SUBSTRING_INDEX(c.remaining_text, ' ', 1), 2))
)
)
FROM capitalized_words c
WHERE c.remaining_text != ''
)
SELECT
content_id,
content_text AS original_text,
MAX(processed_word) AS converted_text
FROM capitalized_words
GROUP BY 1, 2;
```

#### Pandas

```python
import pandas as pd


def process_text(user_content: pd.DataFrame) -> pd.DataFrame:
user_content["converted_text"] = user_content["content_text"].apply(
lambda text: " ".join(word.capitalize() for word in text.split(" "))
)
return user_content[["content_id", "content_text", "converted_text"]].rename(
columns={"content_text": "original_text"}
)
```

<!-- tabs:end -->
Expand Down
10 changes: 10 additions & 0 deletions solution/3300-3399/3368.First Letter Capitalization/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import pandas as pd


def process_text(user_content: pd.DataFrame) -> pd.DataFrame:
user_content["converted_text"] = user_content["content_text"].apply(
lambda text: " ".join(word.capitalize() for word in text.split(" "))
)
return user_content[["content_id", "content_text", "converted_text"]].rename(
columns={"content_text": "original_text"}
)
38 changes: 38 additions & 0 deletions solution/3300-3399/3368.First Letter Capitalization/Solution.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
WITH RECURSIVE
capitalized_words AS (
SELECT
content_id,
content_text,
SUBSTRING_INDEX(content_text, ' ', 1) AS word,
SUBSTRING(
content_text,
LENGTH(SUBSTRING_INDEX(content_text, ' ', 1)) + 2
) AS remaining_text,
CONCAT(
UPPER(LEFT(SUBSTRING_INDEX(content_text, ' ', 1), 1)),
LOWER(SUBSTRING(SUBSTRING_INDEX(content_text, ' ', 1), 2))
) AS processed_word
FROM user_content
UNION ALL
SELECT
c.content_id,
c.content_text,
SUBSTRING_INDEX(c.remaining_text, ' ', 1),
SUBSTRING(c.remaining_text, LENGTH(SUBSTRING_INDEX(c.remaining_text, ' ', 1)) + 2),
CONCAT(
c.processed_word,
' ',
CONCAT(
UPPER(LEFT(SUBSTRING_INDEX(c.remaining_text, ' ', 1), 1)),
LOWER(SUBSTRING(SUBSTRING_INDEX(c.remaining_text, ' ', 1), 2))
)
)
FROM capitalized_words c
WHERE c.remaining_text != ''
)
SELECT
content_id,
content_text AS original_text,
MAX(processed_word) AS converted_text
FROM capitalized_words
GROUP BY 1, 2;
2 changes: 1 addition & 1 deletion solution/DATABASE_README.md
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@
| 3328 | [查找每个州的城市 II](/solution/3300-3399/3328.Find%20Cities%20in%20Each%20State%20II/README.md) | `数据库` | 中等 | 🔒 |
| 3338 | [第二高的薪水 II](/solution/3300-3399/3338.Second%20Highest%20Salary%20II/README.md) | `数据库` | 中等 | 🔒 |
| 3358 | [评分为 NULL 的图书](/solution/3300-3399/3358.Books%20with%20NULL%20Ratings/README.md) | `数据库` | 简单 | 🔒 |
| 3368 | [First Letter Capitalization](/solution/3300-3399/3368.First%20Letter%20Capitalization/README.md) | | 困难 | 🔒 |
| 3368 | [首字母大写](/solution/3300-3399/3368.First%20Letter%20Capitalization/README.md) | | 困难 | 🔒 |

## 版权

Expand Down
Loading

0 comments on commit a14c0f2

Please sign in to comment.