Skip to content

Commit bdedf6e

Browse files
committed
一刷2320
1 parent f94db06 commit bdedf6e

File tree

6 files changed

+65
-38
lines changed

6 files changed

+65
-38
lines changed

README.adoc

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16266,12 +16266,12 @@ TIP: **公众号的微信号是: `jikerizhi`**。__因为众所周知的原因
1626616266
//|Easy
1626716267
//|
1626816268

16269-
//|{counter:codes}
16270-
//|{leetcode_base_url}/count-number-of-ways-to-place-houses/[2320. Count Number of Ways to Place Houses^]
16271-
//|{source_base_url}/_2320_CountNumberOfWaysToPlaceHouses.java[Java]
16272-
//|{doc_base_url}/2320-count-number-of-ways-to-place-houses.adoc[题解]
16273-
//|Medium
16274-
//|
16269+
|{counter:codes}
16270+
|{leetcode_base_url}/count-number-of-ways-to-place-houses/[2320. Count Number of Ways to Place Houses^]
16271+
|{source_base_url}/_2320_CountNumberOfWaysToPlaceHouses.java[Java]
16272+
|{doc_base_url}/2320-count-number-of-ways-to-place-houses.adoc[题解]
16273+
|Medium
16274+
|
1627516275

1627616276
//|{counter:codes}
1627716277
//|{leetcode_base_url}/maximum-score-of-spliced-array/[2321. Maximum Score Of Spliced Array^]
Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,47 @@
11
[#2320-count-number-of-ways-to-place-houses]
2-
= 2320. Count Number of Ways to Place Houses
2+
= 2320. 统计放置房子的方式数
33

4-
{leetcode}/problems/count-number-of-ways-to-place-houses/[LeetCode - 2320. Count Number of Ways to Place Houses ^]
4+
https://leetcode.cn/problems/count-number-of-ways-to-place-houses/[LeetCode - 2320. 统计放置房子的方式数 ^]
55

6-
There is a street with `n * 2` *plots*, where there are `n` plots on each side of the street. The plots on each side are numbered from `1` to `n`. On each plot, a house can be placed.
6+
一条街道上共有 `n * 2` *地块* ,街道的两侧各有 `n` 个地块。每一边的地块都按从 `1` `n` 编号。每个地块上都可以放置一所房子。
77

8-
Return _the number of ways houses can be placed such that no two houses are adjacent to each other on the same side of the street_. Since the answer may be very large, return it *modulo* `10^9^ + 7`.
8+
现要求街道同一侧不能存在两所房子相邻的情况,请你计算并返回放置房屋的方式数目。由于答案可能很大,需要对 `10^9^ + 7` 取余后再返回。
99

10-
Note that if a house is placed on the `i^th^` plot on one side of the street, a house can also be placed on the `i^th^` plot on the other side of the street.
10+
注意,如果一所房子放置在这条街某一侧上的第 `i` 个地块,不影响在另一侧的第 `i` 个地块放置房子。
1111

12-
13-
*Example 1:*
12+
*示例 1:*
1413

15-
[subs="verbatim,quotes"]
16-
----
17-
*Input:* n = 1
18-
*Output:* 4
19-
*Explanation:*
20-
Possible arrangements:
21-
1. All plots are empty.
22-
2. A house is placed on one side of the street.
23-
3. A house is placed on the other side of the street.
24-
4. Two houses are placed, one on each side of the street.
25-
----
14+
....
15+
输入:n = 1
16+
输出:4
17+
解释:
18+
可能的放置方式:
19+
1. 所有地块都不放置房子。
20+
2. 一所房子放在街道的某一侧。
21+
3. 一所房子放在街道的另一侧。
22+
4. 放置两所房子,街道两侧各放置一所。
23+
....
2624

27-
*Example 2:*
28-
<img alt="" src="https://assets.leetcode.com/uploads/2022/05/12/arrangements.png" style="width: 500px; height: 500px;" />
29-
[subs="verbatim,quotes"]
30-
----
31-
*Input:* n = 2
32-
*Output:* 9
33-
*Explanation:* The 9 possible arrangements are shown in the diagram above.
34-
----
25+
*示例 2:*
3526

36-
37-
*Constraints:*
27+
image::images/2320-01.png[{image_attr}]
3828

29+
....
30+
输入:n = 2
31+
输出:9
32+
解释:如上图所示,共有 9 种可能的放置方式。
33+
....
3934

40-
* `1 <= n <= 10^4^`
41-
35+
*提示:*
4236

37+
* `1 \<= n \<= 10^4^`
4338
4439
4540
== 思路分析
4641

42+
先只考虑一边的情况,再组合两边的情况。只考虑一边的情况,就是一个斐波那契数列: stem:[f(i) = f(i-1) + f(i-2)]。
43+
44+
另外,需要注意处理余数问题。
4745

4846
[[src-2320]]
4947
[tabs]
@@ -70,4 +68,4 @@ include::{sourcedir}/_2320_CountNumberOfWaysToPlaceHouses.java[tag=answer]
7068

7169
== 参考资料
7270

73-
71+
. https://leetcode.cn/problems/count-number-of-ways-to-place-houses/solutions/1625979/d-by-endlesscheng-gybx/[2320. 统计放置房子的方式数 - 线性 DP^]

docs/images/2320-01.png

77.5 KB
Loading

docs/index.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4724,7 +4724,7 @@ include::1997-first-day-where-you-have-been-in-all-the-rooms.adoc[leveloffset=+1
47244724

47254725
// include::2319-check-if-matrix-is-x-matrix.adoc[leveloffset=+1]
47264726

4727-
// include::2320-count-number-of-ways-to-place-houses.adoc[leveloffset=+1]
4727+
include::2320-count-number-of-ways-to-place-houses.adoc[leveloffset=+1]
47284728

47294729
// include::2321-maximum-score-of-spliced-array.adoc[leveloffset=+1]
47304730

logbook/202503.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,11 @@ endif::[]
653653
|{doc_base_url}/2800-shortest-string-that-contains-three-strings.adoc[题解]
654654
|❌ 先求三个字符串的全排列,然后合并(合并时注意处理首尾相同子串,减少合并字符串的长度),在这些合并后的字符串中选择最短,字典排序最小的字符串。
655655

656+
|{counter:codes2503}
657+
|{leetcode_base_url}/count-number-of-ways-to-place-houses/[2320. 统计放置房子的方式数^]
658+
|{doc_base_url}/2320-count-number-of-ways-to-place-houses.adoc[题解]
659+
|❌ 先只考虑一边的情况,再组合两边的情况。只考虑一边的情况,就是一个斐波那契数列: stem:[f(i) = f(i-1) + f(i-2)]。
660+
656661
|===
657662

658663
截止目前,本轮练习一共完成 {codes2503} 道题。
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.diguage.algo.leetcode;
2+
3+
public class _2320_CountNumberOfWaysToPlaceHouses {
4+
// tag::answer[]
5+
/**
6+
* @author D瓜哥 · https://www.diguage.com
7+
* @since 2025-05-11 19:24:20
8+
*/
9+
public int countHousePlacements(int n) {
10+
long a = 1, b = 2;
11+
long cnt = b;
12+
for (int i = 2; i <= n; i++) {
13+
cnt = (a + b) % 1_000_000_007;
14+
a = b;
15+
b = cnt;
16+
}
17+
return (int) ((cnt * cnt) % 1_000_000_007);
18+
}
19+
// end::answer[]
20+
public static void main(String[] args) {
21+
new _2320_CountNumberOfWaysToPlaceHouses()
22+
.countHousePlacements(1000);
23+
}
24+
}

0 commit comments

Comments
 (0)