Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 85e83d0

Browse files
✨feat: add 198
1 parent f7f6b12 commit 85e83d0

File tree

3 files changed

+155
-0
lines changed

3 files changed

+155
-0
lines changed

‎Index/状态机 DP.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
| 题目 | 题解 | 难度 | 推荐指数 |
22
| ------------------------------------------------------------ | ------------------------------------------------------------ | ---- | -------- |
3+
| [198. 打家劫舍](https://leetcode.cn/problems/house-robber/) | [LeetCode 题解链接](https://leetcode.cn/problems/house-robber/solution/by-ac_oier-7v1g/) | 中等 | 🤩🤩🤩 |
34
| [552. 学生出勤记录 II](https://leetcode-cn.com/problems/student-attendance-record-ii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/student-attendance-record-ii/solution/gong-shui-san-xie-yi-ti-san-jie-ji-yi-hu-fdfx/) | 困难 | 🤩🤩🤩🤩 |
45
| [801. 使序列递增的最小交换次数](https://leetcode.cn/problems/minimum-swaps-to-make-sequences-increasing/) | [LeetCode 题解链接](https://leetcode.cn/problems/minimum-swaps-to-make-sequences-increasing/solution/by-ac_oier-fjhp/) | 困难 | 🤩🤩🤩🤩🤩 |
56
| [1218. 最长定差子序列](https://leetcode-cn.com/problems/longest-arithmetic-subsequence-of-given-difference/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/longest-arithmetic-subsequence-of-given-difference/solution/gong-shui-san-xie-jie-he-tan-xin-de-zhua-dj1k/) | 中等 | 🤩🤩🤩🤩🤩 |

‎Index/线性 DP.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
| [91. 解码方法](https://leetcode-cn.com/problems/decode-ways/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/decode-ways/solution/gong-shui-san-xie-gen-ju-shu-ju-fan-wei-ug3dd/) | 中等 | 🤩🤩🤩 |
77
| [115. 不同的子序列](https://leetcode-cn.com/problems/distinct-subsequences/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/distinct-subsequences/solution/xiang-jie-zi-fu-chuan-pi-pei-wen-ti-de-t-wdtk/) | 困难 | 🤩🤩🤩🤩 |
88
| [119. 杨辉三角 II](https://leetcode-cn.com/problems/pascals-triangle-ii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/pascals-triangle-ii/solution/dong-tai-gui-hua-luo-ti-chang-jian-de-ko-n2xj/) | 简单 | 🤩🤩🤩 |
9+
| [198. 打家劫舍](https://leetcode.cn/problems/house-robber/) | [LeetCode 题解链接](https://leetcode.cn/problems/house-robber/solution/by-ac_oier-7v1g/) | 中等 | 🤩🤩🤩 |
910
| [213. 打家劫舍 II](https://leetcode-cn.com/problems/house-robber-ii/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/house-robber-ii/solution/gong-shui-san-xie-ru-he-jiang-xin-xian-z-zf0w/) | 中等 | 🤩🤩🤩 |
1011
| [338. 比特位计数](https://leetcode-cn.com/problems/counting-bits/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/counting-bits/solution/po-su-jie-fa-dong-tai-gui-hua-jie-fa-by-vvail/) | 简单 | 🤩🤩🤩 |
1112
| [403. 青蛙过河](https://leetcode-cn.com/problems/frog-jump/) | [LeetCode 题解链接](https://leetcode-cn.com/problems/frog-jump/solution/gong-shui-san-xie-yi-ti-duo-jie-jiang-di-74fw/) | 困难 | 🤩🤩🤩 |
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
### 题目描述
2+
3+
这是 LeetCode 上的 **[198. 打家劫舍](https://leetcode.cn/problems/house-robber/solution/by-ac_oier-7v1g/)** ,难度为 **中等**
4+
5+
Tag : 「线性 DP」、「状态机 DP」、「动态规划」
6+
7+
8+
9+
你是一个专业的小偷,计划偷窃沿街的房屋。每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警。
10+
11+
给定一个代表每个房屋存放金额的非负整数数组,计算你 不触动警报装置的情况下 ,一夜之内能够偷窃到的最高金额。
12+
13+
示例 1:
14+
```
15+
输入:[1,2,3,1]
16+
17+
输出:4
18+
19+
解释:偷窃 1 号房屋 (金额 = 1) ,然后偷窃 3 号房屋 (金额 = 3)。
20+
偷窃到的最高金额 = 1 + 3 = 4 。
21+
```
22+
示例 2:
23+
```
24+
输入:[2,7,9,3,1]
25+
26+
输出:12
27+
28+
解释:偷窃 1 号房屋 (金额 = 2), 偷窃 3 号房屋 (金额 = 9),接着偷窃 5 号房屋 (金额 = 1)。
29+
偷窃到的最高金额 = 2 + 9 + 1 = 12 。
30+
```
31+
32+
提示:
33+
* 1ドル <= nums.length <= 100$
34+
* 0ドル <= nums[i] <= 400$
35+
36+
---
37+
38+
### 状态机线性 DP
39+
40+
这是一道入门级的状态机线性 DP 题。
41+
42+
定义 $f[i][j]$ 为考虑前 $i$ 间房子,且第 $i$ 间房子的状态为 $j$ 时所能取得的最大价值(其中 $j = 0$ 代表不偷该房子,$j = 1$ 代表偷该房子)。
43+
44+
再结合题意,因为相邻房子不能同时被偷,可推导出状态转移方程为:
45+
46+
* 当前房子不偷,则对前一间房子的状态无要求,状态值为前一状态的较大值:$f[i][0] = \max(f[i - 1][0], f[i - 1][1])$
47+
* 当前房子偷,此时限定了前一间只能不偷,状态值为前一间房子不偷时的最大价值,加上当前房子的价值: $f[i][1] = f[i - 1][0] + nums[i - 1]$
48+
49+
最终 $\max(f[n][0], f[n][1])$ 即是答案。
50+
51+
Java 代码:
52+
```Java
53+
class Solution {
54+
public int rob(int[] nums) {
55+
int n = nums.length;
56+
int[][] f = new int[n + 10][2];
57+
for (int i = 1; i <= n; i++) {
58+
f[i][0] = Math.max(f[i - 1][0], f[i - 1][1]);
59+
f[i][1] = f[i - 1][0] + nums[i - 1];
60+
}
61+
return Math.max(f[n][0], f[n][1]);
62+
}
63+
}
64+
```
65+
TypeScript 代码:
66+
```TypeScript
67+
function rob(nums: number[]): number {
68+
const n = nums.length
69+
const f = new Array<Array<number>>(n + 10)
70+
f[0] = new Array<number>(2).fill(0)
71+
for (let i = 1; i <= n; i++) {
72+
f[i] = new Array<number>(2).fill(0)
73+
f[i][0] = Math.max(f[i - 1][0], f[i - 1][1])
74+
f[i][1] = f[i - 1][0] + nums[i - 1]
75+
}
76+
return Math.max(f[n][0], f[n][1])
77+
}
78+
```
79+
Python 代码:
80+
```Python
81+
class Solution:
82+
def rob(self, nums: List[int]) -> int:
83+
n = len(nums)
84+
f = [[0] * 2 for _ in range(n + 10)]
85+
for i in range(1, n + 1):
86+
f[i][0] = max(f[i - 1][0], f[i - 1][1])
87+
f[i][1] = f[i - 1][0] + nums[i - 1]
88+
return max(f[n][0], f[n][1])
89+
```
90+
* 时间复杂度:$O(n)$
91+
* 空间复杂度:$O(n)$
92+
93+
---
94+
95+
### 优化
96+
97+
利用 $f[i][X]$ 仅依赖于 $f[i - 1][X],ドル我们可以通过「滚动数组」的方式将空间优化至 $O(1)$。
98+
99+
Java 代码:
100+
```Java
101+
class Solution {
102+
public int rob(int[] nums) {
103+
int n = nums.length;
104+
int[][] f = new int[][]{{0, 0}, {0, 0}};
105+
for (int i = 1; i <= n; i++) {
106+
int a = (i - 1) & 1, b = i & 1;
107+
f[b][0] = Math.max(f[a][0], f[a][1]);
108+
f[b][1] = f[a][0] + nums[i - 1];
109+
}
110+
return Math.max(f[n & 1][0], f[n & 1][1]);
111+
}
112+
}
113+
```
114+
TypeScript 代码:
115+
```TypeScript
116+
function rob(nums: number[]): number {
117+
const n = nums.length
118+
const f = [[0, 0], [0, 0]]
119+
for (let i = 1; i <= n; i++) {
120+
const a = (i - 1) & 1, b = i & 1
121+
f[b][0] = Math.max(f[a][0], f[a][1])
122+
f[b][1] = f[a][0] + nums[i - 1]
123+
}
124+
return Math.max(f[n & 1][0], f[n & 1][1])
125+
}
126+
```
127+
Python 代码:
128+
```Python
129+
class Solution:
130+
def rob(self, nums: List[int]) -> int:
131+
n = len(nums)
132+
f = [[0, 0], [0, 0]]
133+
for i in range(1, n + 1):
134+
a, b = (i - 1) & 1, i & 1
135+
f[b][0] = max(f[a][0], f[a][1])
136+
f[b][1] = f[a][0] + nums[i - 1]
137+
return max(f[n & 1][0], f[n & 1][1])
138+
```
139+
* 时间复杂度:$O(n)$
140+
* 空间复杂度:$O(1)$
141+
142+
---
143+
144+
### 最后
145+
146+
这是我们「刷穿 LeetCode」系列文章的第 `No.198` 篇,系列开始于 2021年01月01日,截止于起始日 LeetCode 上共有 1916 道题目,部分是有锁题,我们将先把所有不带锁的题目刷完。
147+
148+
在这个系列文章里面,除了讲解解题思路以外,还会尽可能给出最为简洁的代码。如果涉及通解还会相应的代码模板。
149+
150+
为了方便各位同学能够电脑上进行调试和提交代码,我建立了相关的仓库:https://github.com/SharingSource/LogicStack-LeetCode
151+
152+
在仓库地址里,你可以看到系列文章的题解链接、系列文章的相应代码、LeetCode 原题链接和其他优选题解。
153+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /