Skip to content

Commit

Permalink
2024-11-30打卡
Browse files Browse the repository at this point in the history
  • Loading branch information
NoyeArk committed Nov 30, 2024
1 parent aed49ad commit 226f5ff
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 1 deletion.
2 changes: 1 addition & 1 deletion algorithm/滑动窗口.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ return ans

## 3.1 定长滑动窗口


1. [1456. 定长子串中元音的最大数目](/leetcode/其他/1456.%20定长子串中元音的最大数目.md)

## 3.2 不定长滑动窗口

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# [1220. 统计元音字母序列的数目](https://leetcode.cn/problems/count-vowels-permutation/description/)

> **作者**:弘树
> **日期**:2024-11-30
> **所用时间**:11min
## 1. 多维动态规划

### 状态表示

- $f[i][0]$ 表示第 $i$ 个字符为 'a',$s[i:n]$ 可以构成满足要求的字符串的个数
- $f[i][1]$ 表示第 $i$ 个字符为 'e',$s[i:n]$ 可以构成满足要求的字符串的个数
- $f[i][2]$ 表示第 $i$ 个字符为 'i',$s[i:n]$ 可以构成满足要求的字符串的个数
- $f[i][3]$ 表示第 $i$ 个字符为 'o',$s[i:n]$ 可以构成满足要求的字符串的个数
- $f[i][4]$ 表示第 $i$ 个字符为 'u',$s[i:n]$ 可以构成满足要求的字符串的个数

### 状态计算

对于 $f[i][0]$ 来说,由于第 $i$ 个字符为 'a',按照题目要求,'a' 后面只能跟着 'e',所以状态计算如下:

$$
f[i][0] = f[i + 1][1]
$$

同理可得其他状态的计算:

$$
f[i][1] = f[i + 1][0] + f[i + 1][2]
$$

$$
f[i][2] = f[i + 1][0] + f[i + 1][1] + f[i + 1][3] + f[i + 1][4]
$$

$$
f[i][3] = f[i + 1][2] + f[i + 1][4]
$$

$$
f[i][4] = f[i + 1][0]
$$

最后答案为 $\text{sum}(f[0][j]), j \in [0, 4]$

状态初始化,考虑字符串最后一个位置,此时没有任何限制,所以初始化为 $1$ 。

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

**Python3**

```python
class Solution:
def countVowelPermutation(self, n: int) -> int:
M = 1_000_000_007
f = [[1] * 5 for _ in range(n)]
for i in range(n - 2, -1, -1):
f[i][0] = f[i + 1][1] % M
f[i][1] = (f[i + 1][0] + f[i + 1][2]) % M
f[i][2] = (f[i + 1][0] + f[i + 1][1] + f[i + 1][3] + f[i + 1][4]) % M
f[i][3] = (f[i + 1][2] + f[i + 1][4]) % M
f[i][4] = f[i + 1][0] % M
return sum(f[0]) % 1_000_000_007
```

## 2. 滚动数组优化

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

**Python3**

```python
class Solution:
def countVowelPermutation(self, n: int) -> int:
M = 1_000_000_007
f = [1] * 5
for i in range(n - 2, -1, -1):
t = f.copy()
f = [t[1] % M, (t[0] + t[2]) % M, (sum(t) - t[2]) % M, (t[2] + t[4]) % M, t[0] % M]
return sum(f) % M
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# [3232. 判断是否可以赢得数字游戏](https://leetcode.cn/problems/find-if-digit-game-can-be-won/description/)

> **作者**:弘树
> **日期**:2024-11-30
> **所用时间**:3min
## 1. 简单模拟

由于 $nums[i]$ 的范围是 $[1, 99]$ ,所以要么是个位数,要么是两位数。所以分别统计个位数和两位数的和即可,然后判断个位数的和是否大于两位数,或者两位数的和是否大于个位数的和。

换句话说,只要个位数的和和两位数的和不相等,Alice 就可以获胜,否则 Bob 获胜。

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

**Python3**

```python
class Solution:
def canAliceWin(self, nums: List[int]) -> bool:
single_s = s = 0
for x in nums:
s += x
if 1 <= x <= 9:
single_s += x
return True if 2 * single_s != s else False
```

- 简化写法

**Python3**

```python
class Solution:
def canAliceWin(self, nums: List[int]) -> bool:
return True if 2 * sum(x for x in nums if 1 <= x <= 9) != sum(nums) else False
```
49 changes: 49 additions & 0 deletions leetcode/其他/1456. 定长子串中元音的最大数目.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# [1456. 定长子串中元音的最大数目](https://leetcode.cn/problems/maximum-number-of-vowels-in-a-substring-of-given-length/description/)

> **日期**:2024-11-30
> **所用时间**:5min
## 1. 定长滑动窗口

使用 $cnt$ 维护滑动窗口内元音字母的个数,如果当前窗口大小等于 $k$ ,则更新答案,并将窗口内最左边的元素移出窗口,同时更新 $cnt$ 。

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

**Python3**

```python
class Solution:
def maxVowels(self, s: str, k: int) -> int:
ans = l = cnt = 0
for r, c in enumerate(s):
cnt += 1 if c in 'aeiou' else 0
if r - l + 1 == k:
ans = max(ans, cnt)
cnt -= 1 if s[l] in 'aeiou' else 0
l += 1
return ans
```

**C++**

```C++
class Solution {
public:
bool check(char c)
{
return c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u';
}

int maxVowels(string s, int k) {
int l = 0, ans = 0, cnt = 0;
for (int r = 0; r < s.size(); r ++)
{
if (check(s[r])) cnt ++;
if (r - l + 1 == k)
ans = max(ans, cnt), cnt = check(s[l ++]) ? cnt - 1 : cnt;
}
return ans;
}
};
```

0 comments on commit 226f5ff

Please sign in to comment.