Skip to content

Commit

Permalink
2025-01-28打卡
Browse files Browse the repository at this point in the history
  • Loading branch information
NoyeArk committed Jan 28, 2025
1 parent 808d1f2 commit 73a40f2
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
SELECT
customer_id,
COUNT(*) count_no_trans
FROM
Visits v
LEFT JOIN
Transactions t
ON
v.visit_id = t.visit_id
WHERE
transaction_id IS NULL
GROUP BY
customer_id
34 changes: 34 additions & 0 deletions leetcode/4-每日一题/119. 杨辉三角 II.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# [119. 杨辉三角 II](https://leetcode.cn/problems/pascals-triangle-ii/description/)

> **日期**:2025-01-28
> **所用时间**:11min
## 1. 简单模拟

杨辉三角的第 $k$ 行可以通过简单模拟来得到。我们可以用一个二维数组 $f$ 来存储杨辉三角,其中 $f[i][j]$ 表示第 $i$ 行第 $j$ 个数。

为了方便处理边界情况,我们可以在每一行的首尾各加一个 0。这样第 $i$ 行就需要 $i+2$ 个数。

对于每个位置 $f[i][j]$,它等于上一行的相邻两个数之和,即:

$$
f[i][j] = f[i-1][j-1] + f[i-1][j]
$$

最后返回去掉首尾 0 的结果即可。

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

**Python3**

```python
class Solution:
def getRow(self, rowIndex: int) -> List[int]:
f = [[0] * (i + 2) for i in range(1, rowIndex + 2)]
f[0][1] = 1
for i in range(1, rowIndex + 1):
for j in range(1, i + 2):
f[i][j] = f[i - 1][j - 1] + f[i - 1][j]
return f[rowIndex][1:-1]
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# [LCR 090. 打家劫舍 II](https://leetcode.cn/problems/PzWKhm/description/)

> **日期**:2025-01-28
> **所用时间**:7min
## 1. 动态规划

### 状态表示

- $f[i][0]$ 表示不偷第 $1$ 个房子时,前 $i$ 个房子能偷到的最大金额
- $f[i][1]$ 表示偷第 $1$ 个房子时,前 $i$ 个房子能偷到的最大金额

### 状态转移

- 不偷第 $1$ 个房子时,前 $i$ 个房子能偷到的最大金额等于前 $i-1$ 个房子不偷第 $1$ 个房子时能偷到的最大金额和前 $i-2$ 个房子不偷第 $1$ 个房子时能偷到的最大金额加上第 $i$ 个房子的金额中的最大值
- 偷第 $1$ 个房子时,前 $i$ 个房子能偷到的最大金额等于前 $i-1$ 个房子偷第 $1$ 个房子时能偷到的最大金额和前 $i-2$ 个房子偷第 $1$ 个房子时能偷到的最大金额加上第 $i$ 个房子的金额中的最大值

$$
f[i][0] = \max(f[i-1][0], f[i-2][0] + nums[i])
$$

$$
f[i][1] = \max(f[i-1][1], f[i-2][1] + nums[i])
$$

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

**Python3**

```python
class Solution:
def rob(self, nums: List[int]) -> int:
if len(nums) == 1:
return nums[0]

f = [[0, 0] for i in range(len(nums))]
f[0] = [0, nums[0]]
for i in range(1, len(nums)):
f[i][0] = max(f[i - 1][0], f[i - 2][0] + nums[i])
f[i][1] = max(f[i - 1][1], f[i - 2][1] + nums[i])
return max(f[-1][0], f[-2][1])
```

0 comments on commit 73a40f2

Please sign in to comment.