From 145cc50bd41b67b3d87da444a80fcc301d27b4f9 Mon Sep 17 00:00:00 2001 From: Harsh Thakkar <65072835+Harsh971@users.noreply.github.com> Date: Tue, 9 May 2023 19:31:21 +0530 Subject: [PATCH] Day 70 by admin --- ..._Binary_Strings_With_No_Consecutive_1s.cpp | 40 +++++++++++++++++++ .../Spiral Matrix_Leetcode/Spiral_Matrix.cpp | 30 ++++++++++++++ 2 files changed, 70 insertions(+) create mode 100644 Day 70/Count Binary Strings With No Consecutive 1s_GFG/Count_Binary_Strings_With_No_Consecutive_1s.cpp create mode 100644 Day 70/Spiral Matrix_Leetcode/Spiral_Matrix.cpp diff --git a/Day 70/Count Binary Strings With No Consecutive 1s_GFG/Count_Binary_Strings_With_No_Consecutive_1s.cpp b/Day 70/Count Binary Strings With No Consecutive 1s_GFG/Count_Binary_Strings_With_No_Consecutive_1s.cpp new file mode 100644 index 0000000..a8f9469 --- /dev/null +++ b/Day 70/Count Binary Strings With No Consecutive 1s_GFG/Count_Binary_Strings_With_No_Consecutive_1s.cpp @@ -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); + } +} \ No newline at end of file diff --git a/Day 70/Spiral Matrix_Leetcode/Spiral_Matrix.cpp b/Day 70/Spiral Matrix_Leetcode/Spiral_Matrix.cpp new file mode 100644 index 0000000..0f51164 --- /dev/null +++ b/Day 70/Spiral Matrix_Leetcode/Spiral_Matrix.cpp @@ -0,0 +1,30 @@ +class Solution { + public: + vector spiralOrder(vector>& matrix) { + if (matrix.empty()) + return {}; + + const int m = matrix.size(); + const int n = matrix[0].size(); + vector 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; + } +};