Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7d85939

Browse files
Add files via upload
can u please check this files ,so u can merge it Signed-off-by: abhijeet147 <117306670+abhijeet147@users.noreply.github.com>
1 parent 6b9b8b0 commit 7d85939

File tree

4 files changed

+267
-0
lines changed

4 files changed

+267
-0
lines changed

‎Duplicate_subtree.cpp‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include <iostream>
2+
#include <unordered_map>
3+
#include <string>
4+
5+
class Node {
6+
public:
7+
int data;
8+
Node* left;
9+
Node* right;
10+
11+
Node(int data) : data(data), left(nullptr), right(nullptr) {}
12+
};
13+
14+
std::string inorder(Node* root, std::unordered_map<std::string, int>& m) {
15+
if (root == nullptr) {
16+
return "";
17+
}
18+
19+
std::string s = "(";
20+
s += inorder(root->left, m);
21+
s += std::to_string(root->data);
22+
s += inorder(root->right, m);
23+
s += ")";
24+
25+
if (m.find(s) != m.end() && m[s] == 1) {
26+
std::cout << root->data << " ";
27+
}
28+
29+
if (m.find(s) != m.end()) {
30+
m[s] += 1;
31+
} else {
32+
m[s] = 1;
33+
}
34+
35+
return s;
36+
}
37+
38+
int main() {
39+
Node* root = new Node(1);
40+
root->left = new Node(2);
41+
root->right = new Node(3);
42+
root->left->left = new Node(4);
43+
root->right->left = new Node(2);
44+
root->right->left->left = new Node(4);
45+
46+
std::unordered_map<std::string, int> m;
47+
inorder(root, m);
48+
49+
// Clean up dynamically allocated memory
50+
delete root->right->left->left;
51+
delete root->right->left;
52+
delete root->left->left;
53+
delete root->left;
54+
delete root;
55+
56+
return 0;
57+
}

‎Pathfinding_algorithm.cpp‎

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#include <iostream>
2+
#include <deque>
3+
#include <vector>
4+
5+
using namespace std;
6+
7+
class Point {
8+
public:
9+
int x, y;
10+
Point(int _x, int _y) : x(_x), y(_y) {}
11+
};
12+
13+
class QueueNode {
14+
public:
15+
Point pt;
16+
int dist;
17+
QueueNode(Point _pt, int _dist) : pt(_pt), dist(_dist) {}
18+
};
19+
20+
bool valid(int row, int col, int ROW, int COL) {
21+
return row >= 0 && row < ROW && col >= 0 && col < COL;
22+
}
23+
24+
int BFS(vector<vector<int>>& matrix, Point source, Point destination) {
25+
int ROW = matrix.size();
26+
int COL = matrix[0].size();
27+
28+
if (matrix[source.x][source.y] != 1 || matrix[destination.x][destination.y] != 1)
29+
return -1;
30+
31+
vector<vector<bool>> visited(ROW, vector<bool>(COL, false));
32+
visited[source.x][source.y] = true;
33+
34+
deque<QueueNode> q;
35+
QueueNode s(source, 0);
36+
q.push_back(s);
37+
38+
int rowNum[] = {-1, 0, 0, 1};
39+
int colNum[] = {0, -1, 1, 0};
40+
41+
while (!q.empty()) {
42+
QueueNode curr = q.front();
43+
Point pt = curr.pt;
44+
q.pop_front();
45+
46+
if (pt.x == destination.x && pt.y == destination.y)
47+
return curr.dist;
48+
49+
for (int i = 0; i < 4; i++) {
50+
int row = pt.x + rowNum[i];
51+
int col = pt.y + colNum[i];
52+
53+
if (valid(row, col, ROW, COL) && matrix[row][col] == 1 && !visited[row][col]) {
54+
visited[row][col] = true;
55+
QueueNode adj_cell(Point(row, col), curr.dist + 1);
56+
q.push_back(adj_cell);
57+
}
58+
}
59+
}
60+
61+
return -1;
62+
}
63+
64+
int main() {
65+
vector<vector<int>> matrix = {
66+
{1, 1, 1, 1, 1, 1, 0, 1, 1, 1},
67+
{0, 0, 1, 0, 1, 1, 1, 0, 1, 1},
68+
{1, 1, 1, 0, 1, 1, 0, 1, 0, 1},
69+
{0, 0, 0, 0, 1, 0, 0, 0, 0, 1},
70+
{1, 1, 1, 0, 1, 1, 1, 0, 1, 0},
71+
{1, 0, 1, 1, 1, 1, 0, 1, 0, 0},
72+
{1, 0, 0, 0, 0, 0, 0, 0, 0, 1},
73+
{1, 0, 1, 1, 1, 1, 0, 1, 1, 1},
74+
{1, 1, 0, 0, 0, 0, 1, 0, 0, 1}
75+
};
76+
77+
Point source(0, 0);
78+
Point destination(5, 5);
79+
80+
int distance = BFS(matrix, source, destination);
81+
82+
if (distance == -1)
83+
cout << "Not possible" << endl;
84+
else
85+
cout << "Shortest distance: " << distance << endl;
86+
87+
return 0;
88+
}

‎Shortes_job_first.cpp‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <queue>
4+
5+
using namespace std;
6+
7+
class Task {
8+
public:
9+
int arrivalTime;
10+
int processingTime;
11+
int index;
12+
13+
Task(int arrTime, int procTime, int idx) : arrivalTime(arrTime), processingTime(procTime), index(idx) {}
14+
15+
bool operator<(const Task& other) const {
16+
return processingTime > other.processingTime; // Min-heap based on processing time
17+
}
18+
};
19+
20+
vector<int> shortestJobFirst(vector<vector<int>>& tasks) {
21+
int n = tasks.size();
22+
23+
// Create Task objects and store them in a priority queue (min-heap) based on processing time
24+
priority_queue<Task> taskQueue;
25+
for (int i = 0; i < n; ++i) {
26+
Task task(tasks[i][0], tasks[i][1], i);
27+
taskQueue.push(task);
28+
}
29+
30+
vector<int> orderedTasks;
31+
int currentTime = 0;
32+
33+
while (!taskQueue.empty()) {
34+
Task currentTask = taskQueue.top();
35+
taskQueue.pop();
36+
37+
// If the arrival time is later than the current time, update the current time
38+
if (currentTask.arrivalTime > currentTime)
39+
currentTime = currentTask.arrivalTime;
40+
41+
// Execute the current task
42+
currentTime += currentTask.processingTime;
43+
orderedTasks.push_back(currentTask.index);
44+
}
45+
46+
return orderedTasks;
47+
}
48+
49+
int main() {
50+
vector<vector<int>> tasks = {{1, 2}, {2, 4}, {3, 2}, {4, 1}};
51+
vector<int> orderedTasks = shortestJobFirst(tasks);
52+
53+
cout << "Order of Tasks: ";
54+
for (int i : orderedTasks)
55+
cout << i << " ";
56+
cout << endl;
57+
58+
return 0;
59+
}

‎SpiralMatrix.cpp‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include <iostream>
2+
#include <vector>
3+
4+
using namespace std;
5+
6+
void spiral_matrix() {
7+
int i = 0; // starting row index
8+
int j = 0; // starting column index
9+
int c = 0;
10+
int no;
11+
12+
cout << "Please enter number of rows of the spiral matrix: ";
13+
cin >> no;
14+
15+
int high = no * no;
16+
int val = 1;
17+
18+
vector<vector<int>> a(no, vector<int>(no, 0));
19+
20+
while (val <= high) {
21+
while (j < no - 1) { // for first row
22+
a[i][j] = val;
23+
val++;
24+
j++;
25+
}
26+
while (i < no - 1) { // for last column
27+
a[i][j] = val;
28+
val++;
29+
i++;
30+
}
31+
while (j > c) { // for last row
32+
a[i][j] = val;
33+
val++;
34+
j--;
35+
}
36+
while (i > c) { // for first column
37+
a[i][j] = val;
38+
val++;
39+
i--;
40+
}
41+
no--;
42+
i++;
43+
j++;
44+
c++;
45+
if (val == high) { // for odd matrix size
46+
a[i][j] = val;
47+
break;
48+
}
49+
}
50+
51+
cout << "Spiral matrix is:" << endl;
52+
for (const auto& row : a) {
53+
for (int element : row) {
54+
cout << element << " ";
55+
}
56+
cout << endl;
57+
}
58+
}
59+
60+
int main() {
61+
spiral_matrix();
62+
return 0;
63+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /