Skip to content

Commit

Permalink
2024-12-17打卡
Browse files Browse the repository at this point in the history
  • Loading branch information
NoyeArk committed Dec 17, 2024
1 parent 64324c4 commit 9c09410
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 62 deletions.
6 changes: 3 additions & 3 deletions leetcode/5-周赛/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@

**竞赛分数**: /

**总结**
**总结**感觉做的还可以,像第 3 题的 LCP 数组之前从来没接触过,困难题从来没做出来过 😂

|序号| 题目名称 | 通过数据点 | 知识点 |
|---| -------------- | --- | --- |
| 1 | [3386. 按下时间最长的按钮](/leetcode/5-周赛/第%20428%20场周赛/3386.%20按下时间最长的按钮.md) | 100% | 模拟 |
| 2 | [3387. 两天自由外汇交易后的最大货币数](/leetcode/5-周赛/第%20428%20场周赛/3387.%20两天自由外汇交易后的最大货币数.md) | 100% | DFS |
| 3 | [3388. 统计数组中的美丽分割](/leetcode/5-周赛/第%20428%20场周赛/3388.%20统计数组中的美丽分割.md) | 99% | LCP 算法 |
| 4 | []() | 0% | |
| 4 | [3389. 使字符频率相等的最少操作次数](/leetcode/5-周赛/第%20428%20场周赛/3389.%20使字符频率相等的最少操作次数.md) | 0% | 枚举、动态规划 |


## 第 427 场周赛
Expand Down Expand Up @@ -118,7 +118,7 @@
|---| -------------- | --- | --- |
| 1 | [3289.数字小镇中的捣蛋鬼](第%20415%20场周赛/3289.%20数字小镇中的捣蛋鬼.md) | 100% | 排序、位运算 |
| 2 | [3290.最高乘法得分](第%20415%20场周赛/3290.%20最高乘法得分.md) | 34% | 动态规划、记忆化搜索 |
| 3 | [3291.形成目标字符串需要的最少字符串数 I](第%20415%20场周赛/3291.%20形成目标字符串需要的最少字符串数%20I.md) | 78% | 贪心 |
| 3 | [3291.形成目标字符串需要的最少字符串数 I](第%20415%20场周赛/3291.%20形成目标字符串需要的最少字符串数%20I.md) | 78% | 字典树、记忆化搜索 |
| 4 | [3292. 形成目标字符串需要的最少字符串数 II](第%20415%20场周赛/3292.%20形成目标字符串需要的最少字符串数%20II.md) | | |


Expand Down
Original file line number Diff line number Diff line change
@@ -1,54 +1,59 @@
# 3291. 形成目标字符串需要的最少字符串数 I

> **作者:** 弘树
> **日期:** 2024-09-15
> **所用时间:** min
## 解题思路
### 1.Trie树

- 时间复杂度:$O()$
- 空间复杂度:$O()$

```C++
class Solution {
public:
int son[5010][26], idx;

void add(string str)
{
int p = 0;
for (int i = 0; str[i]; i ++)
{
int u = str[i] - 'a';
if (!son[p][u]) son[p][u] = ++ idx;
p = son[p][u];
}
}

int query(int i, string str)
{
int p = 0;
while (str[i])
{
int u = str[i] - 'a';
if (!son[p][u]) break;
p = son[p][u];
i ++;
}
return i;
}

int minValidStrings(vector<string>& words, string target) {
for (auto str : words) add(str);
int ans = 0, i = 0;
while (i < target.size())
{
int res = query(i, target);
if (i == res) return -1;
else i = res, ans ++;
}
return ans;
}
};
# [3291. 形成目标字符串需要的最少字符串数 I](https://leetcode.cn/problems/minimum-number-of-valid-strings-to-form-target-i/description/)

> **日期**:2024-09-15
> **所用时间**:25min
## 1. 字典树 + 记忆化搜索

用字典树存储前缀,用一个指针记录字典树当前遍历到的位置,避免反复查询。由于要在遍历到下一个状态的时候用到当前指针,因此这里从左往右遍历,先找出第一个单词,再找下一个。

- 时间复杂度: $O(n^2\times m),n = \text{len}(target), m = \text{height}(Trie)$
- 空间复杂度: $O(nm)$

**Python3**

```python
class Trie:
def __init__(self):
self.son = {}
self.is_end = False

def add(self, word: str) -> None:
p = self
for c in word:
if c not in p.son:
p.son[c] = Trie()
p = p.son[c]
p.is_end = True

def query(self, word):
p = self
for c in word:
if c not in p.son:
return None
p = p.son[c]
return p

class Solution:
def minValidStrings(self, words: List[str], target: str) -> int:
trie = Trie()
for word in words:
trie.add(word)

@cache
def dfs(st: int, ed: int) -> int:
if trie.query(target[st: ed+1]):
return 1

res = inf
p = trie
for k in range(st, ed + 1):
if target[k] not in p.son:
return res if res < inf else -1
n1 = dfs(k + 1, ed)
if n1 != -1:
res = min(res, n1 + 1)
p = p.son[target[k]]

return dfs(0, len(target) - 1)
```
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,4 @@ class Solution:
dfs(k, v, "")

return states[initialCurrency]
```

**C++**

```C++

```
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# [3389. 使字符频率相等的最少操作次数](https://leetcode.cn/problems/minimum-operations-to-make-character-frequencies-equal/description/)

> **日期**:2024-12-17
> **所用时间**:20min
## 1. 枚举 + 动态规划

参考[题解](https://leetcode.cn/problems/minimum-operations-to-make-character-frequencies-equal/solutions/3020622/mei-ju-dp-by-tsreaper-trnh)

- 时间复杂度: $O(|\Sigma|\times n)$ , 其中 $|\Sigma| = 26$ 是字符集大小
- 空间复杂度: $O(|\Sigma|)$

**Python3**

```python
class Solution:
def makeStringGood(self, s: str) -> int:
cnt = [0] * 27
for c in s:
cnt[ord(c) - ord('a') + 1] += 1

def work(y):
f = [[inf] * 2 for _ in range(27)]
f[0][0] = 0

for i in range(1, 27):
f[i][0] = min(f[i - 1][0], f[i - 1][1]) + cnt[i]

if cnt[i] >= y:
f[i][1] = min(f[i - 1][0], f[i - 1][1]) + cnt[i] - y
else:
d = y - cnt[i]
cost1 = f[i - 1][0] + d - min(d, cnt[i - 1])
cost2 = f[i - 1][1] + d - min(d, max(0, cnt[i - 1] - y))
f[i][1] = min(cost1, cost2)
return min(f[26])

return min(work(y) for y in range(1, len(s) + 1))
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# [LCR 072. x 的平方根](https://leetcode.cn/problems/jJ0w9p/description/)

> **日期**:2024-12-17
> **所用时间**:2min
## 1. 二分查找

$l$ 和 $r$ 分别初始化为 $0$ 和 $46341$ 。这里 $46341$ 是因为 $46341^2$ 等于 $2147488281$ ,这在 $32$ 位整数范围内是最大的平方根。因此我们可以安全地将 $r$ 设置为 $46341$ 。

检查 $mid$ 的平方是否大于 $x$ 。如果是,说明平方根在 $mid$ 左侧,因此将 $r$ 更新为 $mid - 1$ 。

如果 $mid^2 \leq x$ ,说明平方根在 $mid$ 右侧或等于 $mid$ ,因此将 $l$ 更新为 $mid$ 。

- 时间复杂度: $O(logx)$
- 空间复杂度: $O(1)$

**Python3**

```python
class Solution:
def mySqrt(self, x: int) -> int:
l, r = 0, 46341
while l < r:
mid = l + r + 1 >> 1
if mid**2 > x: r = mid - 1
else: l = mid
return r
```

0 comments on commit 9c09410

Please sign in to comment.