Skip to content

Commit

Permalink
Day 70 by admin
Browse files Browse the repository at this point in the history
  • Loading branch information
Harsh971 authored May 9, 2023
1 parent c803996 commit 145cc50
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
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);
}
}
30 changes: 30 additions & 0 deletions Day 70/Spiral Matrix_Leetcode/Spiral_Matrix.cpp
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;
}
};

0 comments on commit 145cc50

Please sign in to comment.