Skip to content

Commit 726d0be

Browse files
authored
feat: add solutions to lc problems: No.3150 (doocs#2802)
No.3150.Invalid Tweets II
1 parent 12cea18 commit 726d0be

File tree

12 files changed

+240
-6
lines changed

12 files changed

+240
-6
lines changed

.prettierignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ node_modules/
2323
/solution/2200-2299/2205.The Number of Users That Are Eligible for Discount/Solution.sql
2424
/solution/2200-2299/2230.The Users That Are Eligible for Discount/Solution.sql
2525
/solution/2200-2299/2252.Dynamic Pivoting of a Table/Solution.sql
26-
/solution/2200-2299/2253.Dynamic Unpivoting of a Table/Solution.sql
26+
/solution/2200-2299/2253.Dynamic Unpivoting of a Table/Solution.sql
27+
/solution/3100-3199/3150.Invalid Tweets II/Solution.sql

solution/2500-2599/2578.Split With Minimum Sum/README_EN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@
3434
<pre>
3535
<strong>Input:</strong> num = 4325
3636
<strong>Output:</strong> 59
37-
<strong>Explanation:</strong> We can split 4325 so that <code>num1 </code>is 24 and num2<code> is </code>35, giving a sum of 59. We can prove that 59 is indeed the minimal possible sum.
37+
<strong>Explanation:</strong> We can split 4325 so that <code>num1</code> is 24 and <code>num2</code> is 35, giving a sum of 59. We can prove that 59 is indeed the minimal possible sum.
3838
</pre>
3939

4040
<p><strong class="example">Example 2:</strong></p>
4141

4242
<pre>
4343
<strong>Input:</strong> num = 687
4444
<strong>Output:</strong> 75
45-
<strong>Explanation:</strong> We can split 687 so that <code>num1</code> is 68 and <code>num2 </code>is 7, which would give an optimal sum of 75.
45+
<strong>Explanation:</strong> We can split 687 so that <code>num1</code> is 68 and <code>num2</code> is 7, which would give an optimal sum of 75.
4646
</pre>
4747

4848
<p>&nbsp;</p>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# [3150. Invalid Tweets II 🔒](https://leetcode.cn/problems/invalid-tweets-ii)
2+
3+
[English Version](/solution/3100-3199/3150.Invalid%20Tweets%20II/README_EN.md)
4+
5+
<!-- tags: -->
6+
7+
## 题目描述
8+
9+
<!-- 这里写题目描述 -->
10+
11+
<p>Table: <code>Tweets</code></p>
12+
13+
<pre>
14+
+----------------+---------+
15+
| Column Name | Type |
16+
+----------------+---------+
17+
| tweet_id | int |
18+
| content | varchar |
19+
+----------------+---------+
20+
tweet_id is the primary key (column with unique values) for this table.
21+
This table contains all the tweets in a social media app.
22+
</pre>
23+
24+
<p>Write a solution to find <strong>invalid tweets</strong>. A tweet is considered invalid if it meets <strong>any</strong> of the following criteria:</p>
25+
26+
<ul>
27+
<li>It exceeds <code>140</code> characters in length.</li>
28+
<li>It has more than <code>3</code> mentions.</li>
29+
<li>It includes more than <code><font face="monospace">3</font></code>&nbsp;hashtags.</li>
30+
</ul>
31+
32+
<p>Return <em>the result table ordered by</em> <code>tweet_id</code> <em>in <strong>ascending</strong> order</em>.</p>
33+
34+
<p>The result format is in the following example.</p>
35+
36+
<p>&nbsp;</p>
37+
<p><strong>Example:</strong></p>
38+
39+
<div class="example-block">
40+
<p><strong>Input:</strong></p>
41+
42+
<p>Tweets table:</p>
43+
44+
<pre class="example-io">
45+
+----------+-----------------------------------------------------------------------------------+
46+
| tweet_id | content |
47+
+----------+-----------------------------------------------------------------------------------+
48+
| 1 | Traveling, exploring, and living my best life @JaneSmith @SaraJohnson @LisaTaylor |
49+
| | @MikeBrown #Foodie #Fitness #Learning |
50+
| 2 | Just had the best dinner with friends! #Foodie #Friends #Fun |
51+
| 4 | Working hard on my new project #Work #Goals #Productivity #Fun |
52+
+----------+-----------------------------------------------------------------------------------+
53+
</pre>
54+
55+
<p><strong>Output:</strong></p>
56+
57+
<pre class="example-io">
58+
+----------+
59+
| tweet_id |
60+
+----------+
61+
| 1 |
62+
| 4 |
63+
+----------+
64+
</pre>
65+
66+
<p><strong>Explanation:</strong></p>
67+
68+
<ul>
69+
<li>tweet_id&nbsp;1 contains 4&nbsp;mentions.</li>
70+
<li>tweet_id 4 contains 4 hashtags.</li>
71+
</ul>
72+
Output table is ordered by tweet_id in ascending order.</div>
73+
74+
## 解法
75+
76+
### 方法一:LENGTH() 函数 + REPLACE() 函数
77+
78+
我们可以使用 `LENGTH()` 函数计算字符串的长度,计算排除掉 `@``#` 之后的长度,然后使用 `OR` 运算符连接这三个条件,筛选出对应的 tweet_id,并按照 tweet_id 升序排序。
79+
80+
<!-- tabs:start -->
81+
82+
```sql
83+
# Write your MySQL query statement below
84+
SELECT tweet_id
85+
FROM Tweets
86+
WHERE LENGTH(content) > 140
87+
OR (LENGTH(content) - LENGTH(REPLACE(content, '@', ''))) > 3
88+
OR (LENGTH(content) - LENGTH(REPLACE(content, '#', ''))) > 3
89+
ORDER BY 1;
90+
```
91+
92+
```python
93+
import pandas as pd
94+
95+
96+
def find_invalid_tweets(tweets: pd.DataFrame) -> pd.DataFrame:
97+
invalid_tweets = tweets[
98+
(tweets["content"].str.len() > 140)
99+
| (tweets["content"].str.count("@") > 3)
100+
| (tweets["content"].str.count("#") > 3)
101+
].sort_values(by="tweet_id")
102+
return invalid_tweets[["tweet_id"]]
103+
```
104+
105+
<!-- tabs:end -->
106+
107+
<!-- end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# [3150. Invalid Tweets II 🔒](https://leetcode.com/problems/invalid-tweets-ii)
2+
3+
[中文文档](/solution/3100-3199/3150.Invalid%20Tweets%20II/README.md)
4+
5+
<!-- tags: -->
6+
7+
## Description
8+
9+
<p>Table: <code>Tweets</code></p>
10+
11+
<pre>
12+
+----------------+---------+
13+
| Column Name | Type |
14+
+----------------+---------+
15+
| tweet_id | int |
16+
| content | varchar |
17+
+----------------+---------+
18+
tweet_id is the primary key (column with unique values) for this table.
19+
This table contains all the tweets in a social media app.
20+
</pre>
21+
22+
<p>Write a solution to find <strong>invalid tweets</strong>. A tweet is considered invalid if it meets <strong>any</strong> of the following criteria:</p>
23+
24+
<ul>
25+
<li>It exceeds <code>140</code> characters in length.</li>
26+
<li>It has more than <code>3</code> mentions.</li>
27+
<li>It includes more than <code><font face="monospace">3</font></code>&nbsp;hashtags.</li>
28+
</ul>
29+
30+
<p>Return <em>the result table ordered by</em> <code>tweet_id</code> <em>in <strong>ascending</strong> order</em>.</p>
31+
32+
<p>The result format is in the following example.</p>
33+
34+
<p>&nbsp;</p>
35+
<p><strong>Example:</strong></p>
36+
37+
<div class="example-block">
38+
<p><strong>Input:</strong></p>
39+
40+
<p>Tweets table:</p>
41+
42+
<pre class="example-io">
43+
+----------+-----------------------------------------------------------------------------------+
44+
| tweet_id | content |
45+
+----------+-----------------------------------------------------------------------------------+
46+
| 1 | Traveling, exploring, and living my best life @JaneSmith @SaraJohnson @LisaTaylor |
47+
| | @MikeBrown #Foodie #Fitness #Learning |
48+
| 2 | Just had the best dinner with friends! #Foodie #Friends #Fun |
49+
| 4 | Working hard on my new project #Work #Goals #Productivity #Fun |
50+
+----------+-----------------------------------------------------------------------------------+
51+
</pre>
52+
53+
<p><strong>Output:</strong></p>
54+
55+
<pre class="example-io">
56+
+----------+
57+
| tweet_id |
58+
+----------+
59+
| 1 |
60+
| 4 |
61+
+----------+
62+
</pre>
63+
64+
<p><strong>Explanation:</strong></p>
65+
66+
<ul>
67+
<li>tweet_id&nbsp;1 contains 4&nbsp;mentions.</li>
68+
<li>tweet_id 4 contains 4 hashtags.</li>
69+
</ul>
70+
Output table is ordered by tweet_id in ascending order.</div>
71+
72+
## Solutions
73+
74+
### Solution 1: LENGTH() Function + REPLACE() Function
75+
76+
We can use the `LENGTH()` function to calculate the length of the string, calculate the length after excluding `@` or `#`, then use the `OR` operator to connect these three conditions, filter out the corresponding tweet_id, and sort by tweet_id in ascending order.
77+
78+
<!-- tabs:start -->
79+
80+
```sql
81+
# Write your MySQL query statement below
82+
SELECT tweet_id
83+
FROM Tweets
84+
WHERE LENGTH(content) > 140
85+
OR (LENGTH(content) - LENGTH(REPLACE(content, '@', ''))) > 3
86+
OR (LENGTH(content) - LENGTH(REPLACE(content, '#', ''))) > 3
87+
ORDER BY 1;
88+
```
89+
90+
```python
91+
import pandas as pd
92+
93+
94+
def find_invalid_tweets(tweets: pd.DataFrame) -> pd.DataFrame:
95+
invalid_tweets = tweets[
96+
(tweets["content"].str.len() > 140)
97+
| (tweets["content"].str.count("@") > 3)
98+
| (tweets["content"].str.count("#") > 3)
99+
].sort_values(by="tweet_id")
100+
return invalid_tweets[["tweet_id"]]
101+
```
102+
103+
<!-- tabs:end -->
104+
105+
<!-- end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import pandas as pd
2+
3+
4+
def find_invalid_tweets(tweets: pd.DataFrame) -> pd.DataFrame:
5+
invalid_tweets = tweets[
6+
(tweets["content"].str.len() > 140)
7+
| (tweets["content"].str.count("@") > 3)
8+
| (tweets["content"].str.count("#") > 3)
9+
].sort_values(by="tweet_id")
10+
return invalid_tweets[["tweet_id"]]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Write your MySQL query statement below
2+
SELECT tweet_id
3+
FROM Tweets
4+
WHERE LENGTH(content) > 140
5+
OR (LENGTH(content) - LENGTH(REPLACE(content, '@', ''))) > 3
6+
OR (LENGTH(content) - LENGTH(REPLACE(content, '#', ''))) > 3
7+
ORDER BY 1;

solution/CONTEST_README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
- [3148. 矩阵中的最大得分](/solution/3100-3199/3148.Maximum%20Difference%20Score%20in%20a%20Grid/README.md)
3030
- [3149. 找出分数最低的排列](/solution/3100-3199/3149.Find%20the%20Minimum%20Cost%20Array%20Permutation/README.md)
3131

32-
#### 第 130 场双周赛(2024-05-11 22:30, 90 分钟) 参赛人数 2603
32+
#### 第 130 场双周赛(2024-05-11 22:30, 90 分钟) 参赛人数 2604
3333

3434
- [3142. 判断矩阵是否满足条件](/solution/3100-3199/3142.Check%20if%20Grid%20Satisfies%20Conditions/README.md)
3535
- [3143. 正方形中的最多点数](/solution/3100-3199/3143.Maximum%20Points%20Inside%20the%20Square/README.md)
@@ -2073,7 +2073,7 @@
20732073
- [1558. 得到目标数组的最少函数调用次数](/solution/1500-1599/1558.Minimum%20Numbers%20of%20Function%20Calls%20to%20Make%20Target%20Array/README.md)
20742074
- [1559. 二维网格图中探测环](/solution/1500-1599/1559.Detect%20Cycles%20in%202D%20Grid/README.md)
20752075

2076-
#### 第 202 场周赛(2020-08-16 10:30, 90 分钟) 参赛人数 4989
2076+
#### 第 202 场周赛(2020-08-16 10:30, 90 分钟) 参赛人数 4990
20772077

20782078
- [1550. 存在连续三个奇数的数组](/solution/1500-1599/1550.Three%20Consecutive%20Odds/README.md)
20792079
- [1551. 使数组中所有元素相等的最小操作数](/solution/1500-1599/1551.Minimum%20Operations%20to%20Make%20Array%20Equal/README.md)

solution/DATABASE_README.md

+1
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@
278278
| 3124 | [查找最长的电话](/solution/3100-3199/3124.Find%20Longest%20Calls/README.md) | `数据库` | 中等 | 🔒 |
279279
| 3126 | [Server Utilization Time](/solution/3100-3199/3126.Server%20Utilization%20Time/README.md) | `数据库` | 中等 | 🔒 |
280280
| 3140 | [连续空余座位 II](/solution/3100-3199/3140.Consecutive%20Available%20Seats%20II/README.md) | `数据库` | 中等 | 🔒 |
281+
| 3150 | [Invalid Tweets II](/solution/3100-3199/3150.Invalid%20Tweets%20II/README.md) | | 简单 | 🔒 |
281282

282283
## 版权
283284

solution/DATABASE_README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ Press <kbd>Control</kbd> + <kbd>F</kbd>(or <kbd>Command</kbd> + <kbd>F</kbd> on
276276
| 3124 | [Find Longest Calls](/solution/3100-3199/3124.Find%20Longest%20Calls/README_EN.md) | `Database` | Medium | 🔒 |
277277
| 3126 | [Server Utilization Time](/solution/3100-3199/3126.Server%20Utilization%20Time/README_EN.md) | `Database` | Medium | 🔒 |
278278
| 3140 | [Consecutive Available Seats II](/solution/3100-3199/3140.Consecutive%20Available%20Seats%20II/README_EN.md) | `Database` | Medium | 🔒 |
279+
| 3150 | [Invalid Tweets II](/solution/3100-3199/3150.Invalid%20Tweets%20II/README_EN.md) | | Easy | 🔒 |
279280

280281
## Copyright
281282

solution/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -3160,6 +3160,7 @@
31603160
| 3147 | [从魔法师身上吸取的最大能量](/solution/3100-3199/3147.Taking%20Maximum%20Energy%20From%20the%20Mystic%20Dungeon/README.md) | | 中等 | 第 397 场周赛 |
31613161
| 3148 | [矩阵中的最大得分](/solution/3100-3199/3148.Maximum%20Difference%20Score%20in%20a%20Grid/README.md) | | 中等 | 第 397 场周赛 |
31623162
| 3149 | [找出分数最低的排列](/solution/3100-3199/3149.Find%20the%20Minimum%20Cost%20Array%20Permutation/README.md) | | 困难 | 第 397 场周赛 |
3163+
| 3150 | [Invalid Tweets II](/solution/3100-3199/3150.Invalid%20Tweets%20II/README.md) | | 简单 | 🔒 |
31633164

31643165
## 版权
31653166

solution/README_EN.md

+1
Original file line numberDiff line numberDiff line change
@@ -3158,6 +3158,7 @@ Press <kbd>Control</kbd> + <kbd>F</kbd>(or <kbd>Command</kbd> + <kbd>F</kbd> on
31583158
| 3147 | [Taking Maximum Energy From the Mystic Dungeon](/solution/3100-3199/3147.Taking%20Maximum%20Energy%20From%20the%20Mystic%20Dungeon/README_EN.md) | | Medium | Weekly Contest 397 |
31593159
| 3148 | [Maximum Difference Score in a Grid](/solution/3100-3199/3148.Maximum%20Difference%20Score%20in%20a%20Grid/README_EN.md) | | Medium | Weekly Contest 397 |
31603160
| 3149 | [Find the Minimum Cost Array Permutation](/solution/3100-3199/3149.Find%20the%20Minimum%20Cost%20Array%20Permutation/README_EN.md) | | Hard | Weekly Contest 397 |
3161+
| 3150 | [Invalid Tweets II](/solution/3100-3199/3150.Invalid%20Tweets%20II/README_EN.md) | | Easy | 🔒 |
31613162

31623163
## Copyright
31633164

solution/contest.json

+1-1
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)