-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
128 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,10 +48,4 @@ class Solution: | |
dfs(k, v, "") | ||
|
||
return states[initialCurrency] | ||
``` | ||
|
||
**C++** | ||
|
||
```C++ | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
``` |