Skip to content

Commit 3581de2

Browse files
authored
Merge pull request #179 from ndk123-web/main
feat: added countinversions and ratInMaze backtrack approach
2 parents cc7c408 + 499581f commit 3581de2

2 files changed

Lines changed: 119 additions & 0 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <iostream>
2+
#include <vector>
3+
using namespace std;
4+
5+
long long mergeAndCount(vector<int>& arr, int l, int m, int r) {
6+
int i = l, j = m + 1;
7+
vector<int> temp;
8+
long long cnt = 0;
9+
10+
while (i <= m && j <= r) {
11+
if (arr[i] <= arr[j]) {
12+
temp.push_back(arr[i++]);
13+
} else {
14+
temp.push_back(arr[j++]);
15+
cnt += (m - i + 1); // inversion count
16+
}
17+
}
18+
19+
while (i <= m) temp.push_back(arr[i++]);
20+
while (j <= r) temp.push_back(arr[j++]);
21+
22+
// copy back
23+
for (int k = l; k <= r; k++) arr[k] = temp[k - l];
24+
25+
return cnt;
26+
}
27+
28+
long long countInversion(vector<int>& arr, int l, int r) {
29+
long long cnt = 0;
30+
if (l < r) {
31+
int m = (l + r) / 2;
32+
cnt += countInversion(arr, l, m);
33+
cnt += countInversion(arr, m + 1, r);
34+
cnt += mergeAndCount(arr, l, m, r);
35+
}
36+
return cnt;
37+
}
38+
39+
int main() {
40+
vector<int> arr = {5, 3, 2, 4, 1};
41+
cout << "Inversions: " << countInversion(arr, 0, arr.size() - 1);
42+
return 0;
43+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Online C++ compiler to run C++ program online
2+
#include <iostream>
3+
#include <vector>
4+
5+
using namespace std;
6+
7+
bool canGo(int row, int col, vector<vector<int>>& matrix) {
8+
int n = matrix.size();
9+
// Check boundaries and validity
10+
if (row < 0 || col < 0 || row >= n || col >= n) return false;
11+
if (matrix[row][col] == 0 || matrix[row][col] == -1) return false;
12+
13+
return true;
14+
}
15+
16+
void solveMaze(int row, int col, vector<vector<int>>& matrix, string& part, int size, vector<string>& ans){
17+
18+
if (row == size-1 && col == size-1){
19+
ans.push_back(part);
20+
return;
21+
}
22+
23+
// mark cell as visited
24+
matrix[row][col] = -1;
25+
26+
// check down
27+
if (canGo(row+1,col,matrix)){
28+
part = part + "D";
29+
solveMaze(row+1,col,matrix,part,size,ans);
30+
part.pop_back();
31+
}
32+
33+
// check up
34+
if (canGo(row-1,col,matrix)){
35+
part = part + "U";
36+
solveMaze(row-1,col,matrix,part,size,ans);
37+
part.pop_back();
38+
}
39+
40+
// check left
41+
if(canGo(row,col-1,matrix)){
42+
part = part + "L";
43+
solveMaze(row,col-1,matrix,part,size,ans);
44+
part.pop_back();
45+
}
46+
47+
// check right
48+
if(canGo(row,col+1,matrix)){
49+
part = part + "R";
50+
solveMaze(row,col+1,matrix,part,size,ans);
51+
part.pop_back();
52+
}
53+
54+
// unmark
55+
matrix[row][col] = 1;
56+
}
57+
58+
int main() {
59+
60+
vector<vector<int>> matrix = {
61+
{1,0,0,0},
62+
{1,1,0,1},
63+
{1,1,0,0},
64+
{0,1,1,1}
65+
};
66+
vector<string> ans;
67+
string part= "";
68+
69+
solveMaze(0,0,matrix,part,matrix.size(),ans);
70+
71+
for (auto i : ans){
72+
cout << i << " ";
73+
}
74+
75+
return 0;
76+
}

0 commit comments

Comments
 (0)