-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.cpp
More file actions
28 lines (22 loc) · 751 Bytes
/
Copy pathsolution.cpp
File metadata and controls
28 lines (22 loc) · 751 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class Solution {
public:
int countWays(int n, int m) {
// Mod value given in the problem
const int MOD = 1000000007;
// dp[i] = number of ways to tile i x m floor
vector<long long> dp(n + 1, 0);
// Base case: empty floor has one valid way
dp[0] = 1;
// Build answer from smaller floors
for (int i = 1; i <= n; i++) {
// Always possible to place one horizontal tile
dp[i] = dp[i - 1];
// Vertical placement is possible only when at least m rows exist
if (i >= m) {
dp[i] = (dp[i] + dp[i - m]) % MOD;
}
}
// Return final answer
return dp[n];
}
};