Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GFg POTD 09-10-2024 in cpp #139

Merged
merged 2 commits into from
Oct 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions october_2024/potd_09_10_2024.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*structure of the node of the linked list is as

struct Node
{
int data;
Node* right, *down;

Node(int x){
data = x;
right = NULL;
down = NULL;
}
};
*/

// function must return the pointer to the first element of the in linked list i.e. that
// should be the element at arr[0][0]
class Solution {
public:
Node* constructLinkedMatrix(vector<vector<int>>& mat) {
if (mat.empty() || mat[0].empty()) {
return nullptr;
}

int rows = mat.size();
int cols = mat[0].size();

Node*** node = new Node**[rows];
for (int i = 0; i < rows; i++) {
node[i] = new Node*[cols];
}

for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
node[i][j] = new Node(mat[i][j]);
}
}

for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (j < cols - 1) {
node[i][j]->right = node[i][j + 1];
}
if (i < rows - 1) {
node[i][j]->down = node[i + 1][j];
}
}
}

return node[0][0];


}
};