-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
...Binary Strings With No Consecutive 1s_GFG/Count_Binary_Strings_With_No_Consecutive_1s.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Java Solution | ||
|
||
class Solution { | ||
public int mod = 1000000007; | ||
public void multiply(int[][] a, int[][] b) { | ||
int mul[][] = new int[3][3]; | ||
for (int i = 0; i < 3; i++) { | ||
for (int j = 0; j < 3; j++) { | ||
for (int k = 0; k < 3; k++) { | ||
long temp = ((long)a[i][k] * b[k][j]) % mod; | ||
mul[i][j] += temp; | ||
mul[i][j] %= mod; | ||
} | ||
} | ||
} | ||
for (int i = 0; i < 3; i++) { | ||
for (int j = 0; j < 3; j++) { | ||
a[i][j] = mul[i][j]; | ||
} | ||
} | ||
} | ||
public int power(int[][] mat, long N) { | ||
int M[][] = {{1, 1, 0}, {1, 0, 0}, {0, 1, 0}}; | ||
if (N == 1) { | ||
return (mat[0][0] + mat[0][1]) % mod; | ||
} | ||
power(mat, N / 2); | ||
multiply(mat, mat); | ||
if (N % 2 != 0) { | ||
multiply(mat, M); | ||
} | ||
return (mat[0][0] + mat[0][1]) % mod; | ||
} | ||
int countStrings(long N) { | ||
int[][] mat = {{1, 1, 0}, {1, 0, 0}, {0, 1, 0}}; | ||
if (N == 2) return 3; | ||
if (N == 1) return 2; | ||
return power(mat, N); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
class Solution { | ||
public: | ||
vector<int> spiralOrder(vector<vector<int>>& matrix) { | ||
if (matrix.empty()) | ||
return {}; | ||
|
||
const int m = matrix.size(); | ||
const int n = matrix[0].size(); | ||
vector<int> ans; | ||
int r1 = 0; | ||
int c1 = 0; | ||
int r2 = m - 1; | ||
int c2 = n - 1; | ||
|
||
// Repeatedly add matrix[r1..r2][c1..c2] to ans | ||
while (ans.size() < m * n) { | ||
for (int j = c1; j <= c2 && ans.size() < m * n; ++j) | ||
ans.push_back(matrix[r1][j]); | ||
for (int i = r1 + 1; i <= r2 - 1 && ans.size() < m * n; ++i) | ||
ans.push_back(matrix[i][c2]); | ||
for (int j = c2; j >= c1 && ans.size() < m * n; --j) | ||
ans.push_back(matrix[r2][j]); | ||
for (int i = r2 - 1; i >= r1 + 1 && ans.size() < m * n; --i) | ||
ans.push_back(matrix[i][c1]); | ||
++r1, ++c1, --r2, --c2; | ||
} | ||
|
||
return ans; | ||
} | ||
}; |