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 61a5765

Browse files
authored
I added an imp application of DSU, No of Provinces (Py-Contributors#920)
* added nqueens2 * added bfs hard prob * dsu * quick sort * morris added * vertical traversal * modified quick sort
1 parent 79c0a2d commit 61a5765

File tree

4 files changed

+176
-0
lines changed

4 files changed

+176
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
class Solution {
4+
public:
5+
// finding nod1->par equaility with nod2->par;
6+
// to check whether their same component or not;
7+
// if they are same locality then we count both as 1,
8+
// else both are unique
9+
void dsu(int x,int y,vector<int>&par){
10+
int p1=find(x,par),p2=find(y,par);
11+
if(p1==p2) return;
12+
else par[p1]=p2;
13+
}
14+
// checking their nodes vs parents
15+
int find(int v,vector<int>&par){
16+
if(par[v]==-1) return v;
17+
return par[v]=find(par[v],par);
18+
}
19+
int findCircleNum(vector<vector<int>>& mat) {
20+
int n=mat.size();
21+
vector<int>par(n,-1);
22+
for(int i=0;i<n;i++){
23+
for(int j=0;j<n;j++){
24+
if(mat[i][j]==1){
25+
dsu(i,j,par);
26+
}
27+
}
28+
}
29+
// find(); // parent finding
30+
// together();
31+
int c=0;
32+
for(int i=0;i<n;i++){
33+
if(par[i]==-1) c++;
34+
}
35+
return c;
36+
37+
}
38+
};

‎C++/Data Structure/Trees/morris.cpp‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/* Tree Node
2+
struct Node {
3+
int data;
4+
Node* left;
5+
Node* right;
6+
};*/
7+
#include<bits/stdc++.h>
8+
using namespace std;
9+
class Solution {
10+
public:
11+
vector<int> inOrder(Node* root)
12+
{
13+
Node*cur=root;
14+
vector<int>v;
15+
//code here
16+
while(cur){
17+
if(!cur->left) {v.push_back(cur->data); cur=cur->right;}
18+
else{
19+
Node*pred=cur->left;
20+
while(pred->right and pred->right!=cur) pred=pred->right;
21+
if(!pred->right){
22+
pred->right=cur;
23+
cur=cur->left;
24+
}
25+
else{
26+
pred->right=NULL;
27+
v.push_back(cur->data);
28+
cur=cur->right;
29+
}
30+
31+
}
32+
}
33+
return v;
34+
}
35+
};
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
#include<bits/stdc++.h>
13+
using namespace std;
14+
class Solution {
15+
public:
16+
vector<vector<int>> verticalTraversal(TreeNode* root) {
17+
// map<vert,map<level,level vals>>
18+
map<int,map<int,multiset<int>>>mp;
19+
queue<pair<TreeNode*,pair<int,int>>>q; // node val, vert level;
20+
q.push({root,{0,0}});
21+
while(!q.empty()){
22+
auto a=q.front(); // 3,0,0
23+
q.pop();
24+
TreeNode*ptr=a.first;
25+
int vert=a.second.first,lev=a.second.second;
26+
mp[vert][lev].insert(ptr->val);
27+
if(ptr->left) q.push({ptr->left,{vert-1,lev+1}});
28+
if(ptr->right) q.push({ptr->right,{vert+1,lev+1}});
29+
}
30+
vector<vector<int>>ans;
31+
for(auto i:mp){
32+
vector<int>v;
33+
for(auto p:i.second){ // here i.second is int,multiset
34+
// insert(pos,start and begin of another ds)
35+
v.insert(v.end(),p.second.begin(),p.second.end());
36+
}
37+
ans.push_back(v);
38+
}
39+
return ans;
40+
}
41+
};
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
2+
import java.util.*;
3+
class QuickSort
4+
{
5+
// selects last element as pivot, pi using which array is partitioned.
6+
int partition(int intArray[], int low, int high)
7+
{
8+
int pi = intArray[high];
9+
int i = (low - 1); // smaller element index
10+
for (int j = low; j < high; j++)
11+
{
12+
// check if current element is less than or equal to pi
13+
if (intArray[j] <= pi)
14+
{
15+
i++;
16+
// swap intArray[i] and intArray[j]
17+
int temp = intArray[i];
18+
intArray[i] = intArray[j];
19+
intArray[j] = temp;
20+
}
21+
}
22+
// swap intArray[i+1] and intArray[high] (or pi)
23+
int temp = intArray[i + 1];
24+
intArray[i + 1] = intArray[high];
25+
intArray[high] = temp;
26+
return i + 1;
27+
}
28+
// routine to sort the array partitions recursively
29+
void quick_sort(int intArray[], int low, int high)
30+
{
31+
if (low < high)
32+
{
33+
// partition the array around pi=>partitioning index and return pi
34+
int pi = partition(intArray, low, high);
35+
// sort each partition recursively
36+
quick_sort(intArray, low, pi - 1);
37+
quick_sort(intArray, pi + 1, high);
38+
}
39+
}
40+
}
41+
class Quicksort_Recursive
42+
{
43+
public static void main(String args[])
44+
{
45+
Scanner sc = new Scanner(System.in);
46+
System.out.println("enter array size");
47+
int n = sc.nextInt();
48+
int a[] = new int[n];
49+
System.out.println("enter the elements of array ");
50+
for (int i = 0; i < n; i++)
51+
{
52+
a[i] = sc.nextInt();
53+
}
54+
// print the original array
55+
System.out.println("Original Array: " + Arrays.toString(a));
56+
// call quick_sort routine using QuickSort object
57+
QuickSort obj = new QuickSort();
58+
obj.quick_sort(a, 0, n - 1);
59+
// print the sorted array
60+
System.out.println("Sorted Array: " + Arrays.toString(a));
61+
}
62+
}

0 commit comments

Comments
(0)

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