Skip to content

Commit 3298f0c

Browse files
committed
Add solution #2320
1 parent 6a179c1 commit 3298f0c

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,882 LeetCode solutions in JavaScript
1+
# 1,883 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1761,6 +1761,7 @@
17611761
2316|[Count Unreachable Pairs of Nodes in an Undirected Graph](./solutions/2316-count-unreachable-pairs-of-nodes-in-an-undirected-graph.js)|Medium|
17621762
2317|[Maximum XOR After Operations](./solutions/2317-maximum-xor-after-operations.js)|Medium|
17631763
2319|[Check if Matrix Is X-Matrix](./solutions/2319-check-if-matrix-is-x-matrix.js)|Easy|
1764+
2320|[Count Number of Ways to Place Houses](./solutions/2320-count-number-of-ways-to-place-houses.js)|Medium|
17641765
2336|[Smallest Number in Infinite Set](./solutions/2336-smallest-number-in-infinite-set.js)|Medium|
17651766
2338|[Count the Number of Ideal Arrays](./solutions/2338-count-the-number-of-ideal-arrays.js)|Hard|
17661767
2342|[Max Sum of a Pair With Equal Sum of Digits](./solutions/2342-max-sum-of-a-pair-with-equal-sum-of-digits.js)|Medium|
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* 2320. Count Number of Ways to Place Houses
3+
* https://leetcode.com/problems/count-number-of-ways-to-place-houses/
4+
* Difficulty: Medium
5+
*
6+
* There is a street with n * 2 plots, where there are n plots on each side of the street. The plots
7+
* on each side are numbered from 1 to n. On each plot, a house can be placed.
8+
*
9+
* Return the number of ways houses can be placed such that no two houses are adjacent to each other
10+
* on the same side of the street. Since the answer may be very large, return it modulo 109 + 7.
11+
*
12+
* Note that if a house is placed on the ith plot on one side of the street, a house can also be
13+
* placed on the ith plot on the other side of the street.
14+
*/
15+
16+
/**
17+
* @param {number} n
18+
* @return {number}
19+
*/
20+
var countHousePlacements = function(n) {
21+
const MOD = 1e9 + 7;
22+
let empty = 1n;
23+
let house = 1n;
24+
25+
for (let i = 2; i <= n; i++) {
26+
const nextEmpty = (empty + house) % BigInt(MOD);
27+
const nextHouse = empty;
28+
empty = nextEmpty;
29+
house = nextHouse;
30+
}
31+
32+
const sideWays = (empty + house) % BigInt(MOD);
33+
return Number((sideWays * sideWays) % BigInt(MOD));
34+
};

0 commit comments

Comments
 (0)