Skip to content

Commit 106a4ec

Browse files
committed
一刷213
1 parent 85e678a commit 106a4ec

File tree

5 files changed

+101
-30
lines changed

5 files changed

+101
-30
lines changed

README.adoc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1516,14 +1516,14 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
15161516
//|{doc_base_url}/0212-word-search-ii.adoc[题解]
15171517
//|Hard
15181518
//|
1519-
//
1520-
//|{counter:codes}
1521-
//|{leetcode_base_url}/house-robber-ii/[213. House Robber II^]
1522-
//|{source_base_url}/_0213_HouseRobberII.java[Java]
1523-
//|{doc_base_url}/0213-house-robber-ii.adoc[题解]
1524-
//|Medium
1525-
//|
1526-
//
1519+
1520+
|{counter:codes}
1521+
|{leetcode_base_url}/house-robber-ii/[213. House Robber II^]
1522+
|{source_base_url}/_0213_HouseRobberII.java[Java]
1523+
|{doc_base_url}/0213-house-robber-ii.adoc[题解]
1524+
|Medium
1525+
|
1526+
15271527
//|{counter:codes}
15281528
//|{leetcode_base_url}/shortest-palindrome/[214. Shortest Palindrome^]
15291529
//|{source_base_url}/_0214_ShortestPalindrome.java[Java]

docs/0213-house-robber-ii.adoc

Lines changed: 53 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,70 @@
11
[#0213-house-robber-ii]
2-
= 213. House Robber II
2+
= 213. 打家劫舍 II
33

4-
{leetcode}/problems/house-robber-ii/[LeetCode - House Robber II^]
4+
https://leetcode.cn/problems/house-robber-ii/[LeetCode - 213. 打家劫舍 II ^]
55

6-
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are *arranged in a circle.* That means the first house is the neighbor of the last one. Meanwhile, adjacent houses have security system connected and *it will automatically contact the police if two adjacent houses were broken into on the same night*.
6+
你是一个专业的小偷,计划偷窃沿街的房屋,每间房内都藏有一定的现金。这个地方所有的房屋都 *围成一圈*,这意味着第一个房屋和最后一个房屋是紧挨着的。同时,相邻的房屋装有相互连通的防盗系统,*如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警*
77

8-
Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight *without alerting the police*.
8+
给定一个代表每个房屋存放金额的非负整数数组,计算你 *在不触动警报装置的情况下* ,今晚能够偷窃到的最高金额。
99

10-
*Example 1:*
10+
*示例 1:*
1111

12-
[subs="verbatim,quotes,macros"]
13-
----
14-
*Input:* [2,3,2]
15-
*Output:* 3
16-
*Explanation:* You cannot rob house 1 (money = 2) and then rob house 3 (money = 2),
17-
because they are adjacent houses.
12+
....
13+
输入:nums = [2,3,2]
14+
输出:3
15+
解释:你不能先偷窃 1 号房屋(金额 = 2),然后偷窃 3 号房屋(金额 = 2), 因为他们是相邻的。
16+
....
1817

19-
----
18+
*示例 2:*
2019

21-
*Example 2:*
20+
....
21+
输入:nums = [1,2,3,1]
22+
输出:4
23+
解释:你可以先偷窃 1 号房屋(金额 = 1),然后偷窃 3 号房屋(金额 = 3)。
24+
偷窃到的最高金额 = 1 + 3 = 4 。
25+
....
2226

23-
[subs="verbatim,quotes,macros"]
24-
----
25-
*Input:* [1,2,3,1]
26-
*Output:* 4
27-
*Explanation:* Rob house 1 (money = 1) and then rob house 3 (money = 3).
28-
Total amount you can rob = 1 + 3 = 4.
29-
----
27+
*示例 3:*
28+
29+
....
30+
输入:nums = [1,2,3]
31+
输出:3
32+
....
33+
34+
*提示:*
35+
36+
* `+1 <= nums.length <= 100+`
37+
* `+0 <= nums[i] <= 1000+`
3038
3139
40+
== 思路分析
41+
42+
相比 xref:0198-house-robber.adoc[198. House Robber],这里的房屋是首尾相连,那么首和尾只能选一个。这里采取“从头到尾”和“从尾到头”遍历两遍的策略,在遍历时都排除最后一个元素不考虑。然后取这两遍遍历中的最大值为结果。
43+
3244

3345
[[src-0213]]
46+
[tabs]
47+
====
48+
一刷::
49+
+
50+
--
3451
[{java_src_attr}]
3552
----
36-
include::{sourcedir}/_0213_HouseRobberII.java[tag=answer]
53+
include::{sourcedir}/_0213_HouseRobberIi.java[tag=answer]
3754
----
55+
--
56+
57+
// 二刷::
58+
// +
59+
// --
60+
// [{java_src_attr}]
61+
// ----
62+
// include::{sourcedir}/_0213_HouseRobberIi_2.java[tag=answer]
63+
// ----
64+
// --
65+
====
66+
67+
68+
== 参考资料
69+
3870

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ include::0210-course-schedule-ii.adoc[leveloffset=+1]
508508

509509
// include::0212-word-search-ii.adoc[leveloffset=+1]
510510

511-
// include::0213-house-robber-ii.adoc[leveloffset=+1]
511+
include::0213-house-robber-ii.adoc[leveloffset=+1]
512512

513513
// include::0214-shortest-palindrome.adoc[leveloffset=+1]
514514

logbook/202503.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,11 @@ endif::[]
280280
|{doc_base_url}/0198-house-robber.adoc[题解]
281281
|✅ 动态规划
282282

283+
|{counter:codes2503}
284+
|{leetcode_base_url}/house-robber-ii/[213. 打家劫舍 II^]
285+
|{doc_base_url}/0213-house-robber-ii.adoc[题解]
286+
|✅ 动态规划
287+
283288

284289
|===
285290
˚
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.diguage.algo.leetcode;
2+
3+
import java.util.Arrays;
4+
5+
public class _0213_HouseRobberIi {
6+
// tag::answer[]
7+
/**
8+
* @author D瓜哥 · https://www.diguage.com
9+
* @since 2025-04-17 14:52:14
10+
*/
11+
public int rob(int[] nums) {
12+
if (nums.length == 1) {
13+
return nums[0];
14+
}
15+
if (nums.length == 2) {
16+
return Math.max(nums[0], nums[1]);
17+
}
18+
int[] dp = new int[nums.length];
19+
dp[0] = nums[0];
20+
dp[1] = Math.max(nums[0], nums[1]);
21+
for (int i = 2; i < nums.length - 1; i++) {
22+
dp[i] = Math.max(dp[i - 2] + nums[i], dp[i - 1]);
23+
}
24+
int inOne = dp[nums.length - 2];
25+
Arrays.fill(dp, 0);
26+
dp[nums.length - 1] = nums[nums.length - 1];
27+
dp[nums.length - 2] = Math.max(nums[nums.length - 1], nums[nums.length - 2]);
28+
for (int i = nums.length - 3; i > 0; i--) {
29+
dp[i] = Math.max(dp[i + 2] + nums[i], dp[i + 1]);
30+
}
31+
return Math.max(inOne, dp[1]);
32+
}
33+
// end::answer[]
34+
}

0 commit comments

Comments
 (0)