Skip to content
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

feat: update lc problems #2811

Merged
merged 1 commit into from
May 14, 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
4 changes: 2 additions & 2 deletions lcci/17.16.The Masseuse/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,14 +124,14 @@ class Solution {
func massage(_ nums: [Int]) -> Int {
var f = 0
var g = 0

for x in nums {
let ff = g + x
let gg = max(f, g)
f = ff
g = gg
}

return max(f, g)
}
}
Expand Down
4 changes: 2 additions & 2 deletions lcci/17.16.The Masseuse/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,14 @@ class Solution {
func massage(_ nums: [Int]) -> Int {
var f = 0
var g = 0

for x in nums {
let ff = g + x
let gg = max(f, g)
f = ff
g = gg
}

return max(f, g)
}
}
Expand Down
10 changes: 5 additions & 5 deletions lcci/17.18.Shortest Supersequence/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,11 +194,11 @@ class Solution {
var need = [Int: Int]()
var window = [Int: Int]()
small.forEach { need[$0, default: 0] += 1 }

var count = needCount
var minLength = Int.max
var result = (-1, -1)

var left = 0
for right in 0..<big.count {
let element = big[right]
Expand All @@ -208,13 +208,13 @@ class Solution {
count -= 1
}
}

while count == 0 {
if right - left + 1 < minLength {
minLength = right - left + 1
result = (left, right)
}

let leftElement = big[left]
if need[leftElement] != nil {
window[leftElement]! -= 1
Expand All @@ -225,7 +225,7 @@ class Solution {
left += 1
}
}

return result.0 == -1 ? [] : [result.0, result.1]
}
}
Expand Down
10 changes: 5 additions & 5 deletions lcci/17.18.Shortest Supersequence/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,11 +198,11 @@ class Solution {
var need = [Int: Int]()
var window = [Int: Int]()
small.forEach { need[$0, default: 0] += 1 }

var count = needCount
var minLength = Int.max
var result = (-1, -1)

var left = 0
for right in 0..<big.count {
let element = big[right]
Expand All @@ -212,13 +212,13 @@ class Solution {
count -= 1
}
}

while count == 0 {
if right - left + 1 < minLength {
minLength = right - left + 1
result = (left, right)
}

let leftElement = big[left]
if need[leftElement] != nil {
window[leftElement]! -= 1
Expand All @@ -229,7 +229,7 @@ class Solution {
left += 1
}
}

return result.0 == -1 ? [] : [result.0, result.1]
}
}
Expand Down
14 changes: 7 additions & 7 deletions lcci/17.19.Missing Two/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,31 +147,31 @@ class Solution {
func missingTwo(_ nums: [Int]) -> [Int] {
let n = nums.count + 2
var xor = 0

for num in nums {
xor ^= num
}

for i in 1...n {
xor ^= i
}

let diff = xor & (-xor)

var a = 0

for num in nums {
if (num & diff) != 0 {
a ^= num
}
}

for i in 1...n {
if (i & diff) != 0 {
a ^= i
}
}

let b = xor ^ a
return [a, b]
}
Expand Down
14 changes: 7 additions & 7 deletions lcci/17.19.Missing Two/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,31 +140,31 @@ class Solution {
func missingTwo(_ nums: [Int]) -> [Int] {
let n = nums.count + 2
var xor = 0

for num in nums {
xor ^= num
}

for i in 1...n {
xor ^= i
}

let diff = xor & (-xor)

var a = 0

for num in nums {
if (num & diff) != 0 {
a ^= num
}
}

for i in 1...n {
if (i & diff) != 0 {
a ^= i
}
}

let b = xor ^ a
return [a, b]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@

函数 $\text{dfs}(\text{mask}, \text{pre})$ 的计算过程如下:

- 如果 $\text{mask}$ 的二进制表示中 $1$ 的个数为 $n$,即 $\text{mask} = 2^n - 1$,表示所有数字都已经被选取,此时返回 $\text{abs}(\text{pre} - \text{nums}[0])$;
- 如果 $\text{mask}$ 的二进制表示中 $1$ 的个数为 $n$,即 $\text{mask} = 2^n - 1$,表示所有数字都已经被选取,此时返回 $|\text{pre} - \text{nums}[0]|$;
- 否则,我们枚举下一个选取的数字 $\text{cur}$,如果数字 $\text{cur}$ 还未被选取,那么我们可以将数字 $\text{cur}$ 加入到排列中,此时排列的分数为 $|\text{pre} - \text{nums}[\text{cur}]| + \text{dfs}(\text{mask} \, | \, 1 << \text{cur}, \text{cur})$,我们需要取所有 $\text{cur}$ 中分数的最小值。

最后,我们利用一个函数 $\text{g}(\text{mask}, \text{pre})$ 来构造得到最小分数的排列。我们首先将数字 $\text{pre}$ 加入到排列中,然后枚举下一个选取的数字 $\text{cur}$,如果数字 $\text{cur}$ 还未被选取,且满足 $|\text{pre} - \text{nums}[\text{cur}]| + \text{dfs}(\text{mask} \, | \, 1 << \text{cur}, \text{cur})$ 的值等于 $\text{dfs}(\text{mask}, \text{pre})$,那么我们就可以将数字 $\text{cur}$ 加入到排列中。
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ We design a function $\text{dfs}(\text{mask}, \text{pre})$, which represents the

The calculation process of the function $\text{dfs}(\text{mask}, \text{pre})$ is as follows:

- If the number of $1$s in the binary representation of $\text{mask}$ is $n$, that is, $\text{mask} = 2^n - 1$, it means that all numbers have been selected, then return $\text{abs}(\text{pre} - \text{nums}[0])$;
- If the number of $1$s in the binary representation of $\text{mask}$ is $n$, that is, $\text{mask} = 2^n - 1$, it means that all numbers have been selected, then return $|\text{pre} - \text{nums}[0]|$;
- Otherwise, we enumerate the next selected number $\text{cur}$. If the number $\text{cur}$ has not been selected yet, then we can add the number $\text{cur}$ to the permutation. At this time, the score of the permutation is $|\text{pre} - \text{nums}[\text{cur}]| + \text{dfs}(\text{mask} \, | \, 1 << \text{cur}, \text{cur})$. We need to take the minimum score among all $\text{cur}$.

Finally, we use a function $\text{g}(\text{mask}, \text{pre})$ to construct the permutation that gets the minimum score. We first add the number $\text{pre}$ to the permutation, and then enumerate the next selected number $\text{cur}$. If the number $\text{cur}$ has not been selected yet, and it satisfies that the value of $|\text{pre} - \text{nums}[\text{cur}]| + \text{dfs}(\text{mask} \, | \, 1 << \text{cur}, \text{cur})$ is equal to $\text{dfs}(\text{mask}, \text{pre})$, then we can add the number $\text{cur}$ to the permutation.
Expand Down
38 changes: 19 additions & 19 deletions solution/3100-3199/3150.Invalid Tweets II/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# [3150. Invalid Tweets II 🔒](https://leetcode.cn/problems/invalid-tweets-ii)
# [3150. 无效的推文 II 🔒](https://leetcode.cn/problems/invalid-tweets-ii)

[English Version](/solution/3100-3199/3150.Invalid%20Tweets%20II/README_EN.md)

Expand All @@ -8,7 +8,7 @@

<!-- 这里写题目描述 -->

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

<pre>
+----------------+---------+
Expand All @@ -17,29 +17,29 @@
| tweet_id | int |
| content | varchar |
+----------------+---------+
tweet_id is the primary key (column with unique values) for this table.
This table contains all the tweets in a social media app.
</pre>
tweet_id 是这个表的主键(有不同值的列)。
这个表包含某社交媒体 App 中所有的推文。</pre>

<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>
<p>编写一个解决方案来找到 <strong>无效的推文</strong>。如果一条推文满足下面 <strong>任一</strong>&nbsp;条件会被认为无效:</p>

<ul>
<li>It exceeds <code>140</code> characters in length.</li>
<li>It has more than <code>3</code> mentions.</li>
<li>It includes more than <code><font face="monospace">3</font></code>&nbsp;hashtags.</li>
<li>长度超过&nbsp;<code>140</code>&nbsp;个字符。</li>
<li>有超过&nbsp;<code>3</code>&nbsp;次提及。</li>
<li>有超过&nbsp;<code><font face="monospace">3</font></code>&nbsp;个标签。</li>
</ul>

<p>Return <em>the result table ordered by</em> <code>tweet_id</code> <em>in <strong>ascending</strong> order</em>.</p>
<p>以&nbsp;<code>tweet_id</code> <em><strong>升序</strong>&nbsp;</em>返回结果表。</p>

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

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

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

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

<p>Tweets table:</p>
<p>Tweets 表:</p>

<pre class="example-io">
+----------+-----------------------------------------------------------------------------------+
Expand All @@ -52,7 +52,7 @@ This table contains all the tweets in a social media app.
+----------+-----------------------------------------------------------------------------------+
</pre>

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

<pre class="example-io">
+----------+
Expand All @@ -63,13 +63,13 @@ This table contains all the tweets in a social media app.
+----------+
</pre>

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

<ul>
<li>tweet_id&nbsp;1 contains 4&nbsp;mentions.</li>
<li>tweet_id 4 contains 4 hashtags.</li>
<li>tweet_id&nbsp;1 包含 4&nbsp;次提及。</li>
<li>tweet_id 4 包含 4 个标签。</li>
</ul>
Output table is ordered by tweet_id in ascending order.</div>
输出表以 tweet_id 升序排序。</div>

## 解法

Expand Down
2 changes: 1 addition & 1 deletion solution/DATABASE_README.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@
| 3124 | [查找最长的电话](/solution/3100-3199/3124.Find%20Longest%20Calls/README.md) | `数据库` | 中等 | 🔒 |
| 3126 | [Server Utilization Time](/solution/3100-3199/3126.Server%20Utilization%20Time/README.md) | `数据库` | 中等 | 🔒 |
| 3140 | [连续空余座位 II](/solution/3100-3199/3140.Consecutive%20Available%20Seats%20II/README.md) | `数据库` | 中等 | 🔒 |
| 3150 | [Invalid Tweets II](/solution/3100-3199/3150.Invalid%20Tweets%20II/README.md) | | 简单 | 🔒 |
| 3150 | [无效的推文 II](/solution/3100-3199/3150.Invalid%20Tweets%20II/README.md) | | 简单 | 🔒 |

## 版权

Expand Down
2 changes: 1 addition & 1 deletion solution/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3160,7 +3160,7 @@
| 3147 | [从魔法师身上吸取的最大能量](/solution/3100-3199/3147.Taking%20Maximum%20Energy%20From%20the%20Mystic%20Dungeon/README.md) | | 中等 | 第 397 场周赛 |
| 3148 | [矩阵中的最大得分](/solution/3100-3199/3148.Maximum%20Difference%20Score%20in%20a%20Grid/README.md) | | 中等 | 第 397 场周赛 |
| 3149 | [找出分数最低的排列](/solution/3100-3199/3149.Find%20the%20Minimum%20Cost%20Array%20Permutation/README.md) | | 困难 | 第 397 场周赛 |
| 3150 | [Invalid Tweets II](/solution/3100-3199/3150.Invalid%20Tweets%20II/README.md) | | 简单 | 🔒 |
| 3150 | [无效的推文 II](/solution/3100-3199/3150.Invalid%20Tweets%20II/README.md) | | 简单 | 🔒 |

## 版权

Expand Down
Loading