Count Number of Ways to Place Houses LeetCode solution in Java

1
class Solution {
2
int mod = (int) 1e9 + 7;
3

4
public int countHousePlacements(int n) {
5

6
if (n == 1) return 4;
7
if (n == 2) return 9;
8
long a = 2;
9
long b = 3;
10
if (n == 1) return (int) (a % mod);
11
if (n == 2) return (int) (b % mod);
12
long c = 0;
13
for (int i = 3; i <= n; i++) {
14
c = (a + b) % mod;
15
a = b % mod;
16
b = c % mod;
17
}
18

19
return (int) ((c * c) % mod);
20
}
21
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0