Skip to content

Commit 9a46bbc

Browse files
committed
Practise 17-Jul-2020
1 parent 52916b8 commit 9a46bbc

10 files changed

+697
-0
lines changed
+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
#define INF INT_MAX
5+
typedef pair<int, int> iPair;
6+
7+
void adjListGraph(vector<pair<int, int> > adj[], int s, int d, int w){
8+
// undirected graph
9+
adj[s].push_back({d, w});
10+
adj[d].push_back({s, w});
11+
}
12+
13+
void dijkstras(vector<pair<int, int> > adj[], int V, int src){
14+
priority_queue<iPair, vector<iPair>, greater<iPair> > pq; // min heap
15+
vector<int> dist(V, INF); // create a vector for distances and initialize all distances as infinite (INF)
16+
pq.push({0, src});
17+
dist[src]=0;
18+
19+
while(!pq.empty()){
20+
int u=pq.top().second; // pair<distance, vertex >
21+
pq.pop();
22+
for(auto x: adj[u]){
23+
int v=x.first; // pair<vertex, weight >
24+
int w=x.second;
25+
26+
if(dist[v]>dist[u]+w){
27+
dist[v]=dist[u]+w;
28+
pq.push({dist[v], v});
29+
}
30+
}
31+
}
32+
cout<<endl;
33+
cout<<"Vertex Distance"<<endl;
34+
for(int i=0;i<V;i++){
35+
cout<<i<<" "<<dist[i]<<endl;
36+
}
37+
}
38+
39+
int main(){
40+
ios_base::sync_with_stdio(false);
41+
cin.tie(NULL);
42+
int v,e;
43+
cin>>v>>e;
44+
vector<pair<int, int> > adj[v];
45+
while(e--){
46+
int s,d,w;
47+
cin>>s>>d>>w;
48+
adjListGraph(adj,s,d,w);
49+
}
50+
dijkstras(adj, v, 0); // source is 0
51+
}
+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
void adjListGraph(vector<int> adj[], int s, int d){
5+
// directed graph
6+
adj[s].push_back(d);
7+
}
8+
9+
void dfs(vector<int> adj[], int s, bool vis[], stack<int> &st){
10+
vis[s]=true;
11+
for(auto x : adj[s]){
12+
if(!vis[x]){
13+
dfs(adj, x, vis,st);
14+
}
15+
}
16+
st.push(s);
17+
}
18+
19+
void invertGraph(vector<int>iadj[], vector<int>adj[], int v){
20+
21+
for(int i=0;i<v;i++){
22+
for(auto x: adj[i]){
23+
iadj[x].push_back(i);
24+
}
25+
}
26+
}
27+
28+
void reDfs(vector<int>iadj[], int s, bool vis[]){
29+
vis[s]=true;
30+
cout<<s<<" ";
31+
for(auto x: iadj[s]){
32+
if(!vis[x]){
33+
reDfs(iadj, x, vis);
34+
}
35+
}
36+
}
37+
38+
int main(){
39+
ios_base::sync_with_stdio(false);
40+
cin.tie(NULL);
41+
int v,e;
42+
cin>>v>>e;
43+
vector<int> adj[v];
44+
while(e--){
45+
int s,d;
46+
cin>>s>>d;
47+
adjListGraph(adj,s,d);
48+
}
49+
int s=0; // source is node 0
50+
bool vis[v];
51+
memset(vis, false, sizeof(vis));
52+
stack<int> st;
53+
dfs(adj, s, vis, st); // STEP 1
54+
vector<int>iadj[v];
55+
invertGraph(iadj, adj, v); // STEP 2
56+
memset(vis, false, sizeof(vis)); // STEP 3
57+
while(!st.empty()){
58+
int yy=st.top();
59+
st.pop();
60+
if(!vis[yy]){
61+
cout<<"SCC: ";
62+
reDfs(iadj, yy, vis);
63+
cout<<endl;
64+
}
65+
}
66+
}
67+
68+
+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
Steps
3+
1) Initialize keys of all vertices as infinite and
4+
parent of every vertex as -1.
5+
6+
2) Create an empty priority_queue pq. Every item
7+
of pq is a pair (weight, vertex). Weight (or
8+
key) is used used as first item of pair
9+
as first item is by default used to compare
10+
two pairs.
11+
12+
3) Initialize all vertices as not part of MST yet.
13+
We use boolean array inMST[] for this purpose.
14+
This array is required to make sure that an already
15+
considered vertex is not included in pq again. This
16+
is where Ptim's implementation differs from Dijkstra.
17+
In Dijkstr's algorithm, we didn't need this array as
18+
distances always increase. We require this array here
19+
because key value of a processed vertex may decrease
20+
if not checked.
21+
22+
4) Insert source vertex into pq and make its key as 0.
23+
24+
5) While either pq doesn't become empty
25+
a) Extract minimum key vertex from pq.
26+
Let the extracted vertex be u.
27+
28+
b) Include u in MST using inMST[u] = true.
29+
30+
c) Loop through all adjacent of u and do
31+
following for every vertex v.
32+
33+
// If weight of edge (u,v) is smaller than
34+
// key of v and v is not already in MST
35+
If inMST[v] = false && key[v] > weight(u, v)
36+
37+
(i) Update key of v, i.e., do
38+
key[v] = weight(u, v)
39+
(ii) Insert v into the pq
40+
(iv) parent[v] = u
41+
42+
6) Print MST edges using parent array.
43+
*/
44+
45+
46+
47+
48+
49+
50+
#include<bits/stdc++.h>
51+
using namespace std;
52+
53+
#define INF INT_MAX
54+
typedef pair<int, int> iPair;
55+
56+
void adjListGraph(vector<pair<int, int> > adj[], int s, int d, int w){
57+
// undirected graph
58+
adj[s].push_back({d, w});
59+
adj[d].push_back({s, w});
60+
}
61+
62+
void prims(vector<pair<int, int> > adj[], int V){
63+
priority_queue<iPair, vector<iPair>, greater<iPair> > pq; // min heap
64+
int src=0;
65+
vector<int> key(V, INF); // weights
66+
vector<int> parent(V, -1); // u ----> v edge then u is parent of v
67+
vector<bool> inMST(V,false); // keep track of vertices included in MST
68+
69+
pq.push({0, src}); // insert source itself in priority queue
70+
key[src]=0; // and initialize its key as 0
71+
72+
while(!pq.empty()){
73+
int u=pq.top().second; // pait< key, vertex>
74+
pq.pop();
75+
inMST[u]=true;
76+
for(auto x: adj[u]){
77+
int v=x.first; // vector<vertex, weight>
78+
int w=x.second;
79+
80+
// If v is not in MST and weight of (u,v) is smaller than current key of v
81+
if(inMST[v]==false && w<key[v]){
82+
key[v]=w;
83+
pq.push({key[v], v});
84+
parent[v]=u;
85+
}
86+
}
87+
}
88+
cout<<"Edge Weight"<<endl;
89+
for(int i=1;i<V;i++){
90+
cout<<parent[i]<<" - "<<i<<" "<<key[i]<<endl;
91+
}
92+
}
93+
94+
int main(){
95+
ios_base::sync_with_stdio(false);
96+
cin.tie(NULL);
97+
int v,e;
98+
cin>>v>>e;
99+
vector<pair<int, int> > adj[v];
100+
while(e--){
101+
int s,d,w;
102+
cin>>s>>d>>w;
103+
adjListGraph(adj,s,d,w);
104+
}
105+
prims(adj, v);
106+
}
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
typedef pair<int,int> iPair;
5+
6+
int main(){
7+
ios_base::sync_with_stdio(false);
8+
cin.tie(NULL);
9+
cout.tie(NULL);
10+
int t;
11+
cin>>t;
12+
while(t--){
13+
int n, k;
14+
cin>>n>>k;
15+
int a[n];
16+
for(int i=0;i<n;i++) cin>>a[i];
17+
18+
priority_queue<iPair, vector<iPair>, greater<iPair> > minh; // min heap
19+
unordered_map<int, int> mp; // calculating frequencies
20+
for(int i=0;i<n;i++) mp[a[i]]++;
21+
for(auto it=mp.begin(); it!=mp.end(); it++){
22+
minh.push({it->second, it->first});
23+
if(minh.size()> k) minh.pop();
24+
}
25+
while(!minh.empty()){
26+
cout<<minh.top().second<<" ";
27+
minh.pop();
28+
}
29+
cout<<endl;
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
Write a function to check whether an input string is a valid IPv4 address or IPv6 address or neither.
2+
3+
IPv4 addresses are canonically represented in dot-decimal notation, which consists of four decimal numbers, each ranging from 0 to 255, separated by dots ("."), e.g.,172.16.254.1;
4+
5+
Besides, leading zeros in the IPv4 is invalid. For example, the address 172.16.254.01 is invalid.
6+
7+
IPv6 addresses are represented as eight groups of four hexadecimal digits, each group representing 16 bits. The groups are separated by colons (":"). For example, the address 2001:0db8:85a3:0000:0000:8a2e:0370:7334 is a valid one. Also, we could omit some leading zeros among four hexadecimal digits and some low-case characters in the address to upper-case ones, so 2001:db8:85a3:0:0:8A2E:0370:7334 is also a valid IPv6 address(Omit leading zeros and using upper cases).
8+
9+
However, we don't replace a consecutive group of zero value with a single empty group using two consecutive colons (::) to pursue simplicity. For example, 2001:0db8:85a3::8A2E:0370:7334 is an invalid IPv6 address.
10+
11+
Besides, extra leading zeros in the IPv6 is also invalid. For example, the address 02001:0db8:85a3:0000:0000:8a2e:0370:7334 is invalid.
12+
13+
Note: You may assume there is no extra space or special characters in the input string.
14+
15+
Example 1:
16+
Input: "172.16.254.1"
17+
18+
Output: "IPv4"
19+
20+
Explanation: This is a valid IPv4 address, return "IPv4".
21+
Example 2:
22+
Input: "2001:0db8:85a3:0:0:8A2E:0370:7334"
23+
24+
Output: "IPv6"
25+
26+
Explanation: This is a valid IPv6 address, return "IPv6".
27+
Example 3:
28+
Input: "256.256.256.256"
29+
30+
Output: "Neither"
31+
32+
Explanation: This is neither a IPv4 address nor a IPv6 address.
33+
34+
35+
36+
37+
38+
39+
class Solution {
40+
public:
41+
string validIPAddress(string IP) {
42+
if(count(IP.begin(), IP.end(), '.')==3){
43+
if(IP[IP.length()-1]=='.') return "Neither";
44+
stringstream s(IP);
45+
string tmp;
46+
while(getline(s, tmp, '.')){
47+
if(tmp.length()==0 || tmp.length()>3) return "Neither";
48+
if(tmp[0]=='0' && tmp.length()!=1) return "Neither";
49+
for(char c: tmp) if(!isdigit(c)) return "Neither";
50+
if(stoi(tmp)>255) return "Neither";
51+
}
52+
return "IPv4";
53+
} else if(count(IP.begin(), IP.end(), ':')==7){
54+
if(IP[IP.length()-1]==':') return "Neither";
55+
stringstream s(IP);
56+
string tmp;
57+
while(getline(s, tmp, ':')){
58+
if(tmp.length()==0 || tmp.length()>4) return "Neither";
59+
for(char c: tmp) if(!isxdigit(c)) return "Neither";
60+
}
61+
return "IPv6";
62+
63+
} else return "Neither";
64+
}
65+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
Given a non-empty array of integers, return the k most frequent elements.
2+
3+
Example 1:
4+
5+
Input: nums = [1,1,1,2,2,3], k = 2
6+
Output: [1,2]
7+
Example 2:
8+
9+
Input: nums = [1], k = 1
10+
Output: [1]
11+
Note:
12+
13+
You may assume k is always valid, 1 ≤ k ≤ number of unique elements.
14+
Your algorithm's time complexity must be better than O(n log n), where n is the array's size.
15+
It's guaranteed that the answer is unique, in other words the set of the top k frequent elements is unique.
16+
You can return the answer in any order.
17+
18+
19+
20+
21+
22+
class Solution {
23+
public:
24+
vector<int> topKFrequent(vector<int>& nums, int k) {
25+
priority_queue<pair<int,int>, vector<pair<int,int> >, greater<pair<int,int> > > minh;
26+
int n=nums.size();
27+
unordered_map<int, int> mp; // calculating frequencies
28+
for(int i=0;i<n;i++) mp[nums[i]]++;
29+
for(auto it=mp.begin(); it!=mp.end(); it++){
30+
minh.push({it->second, it->first});
31+
if(minh.size()> k) minh.pop();
32+
}
33+
vector<int> res;
34+
while(!minh.empty()){
35+
res.push_back(minh.top().second);
36+
minh.pop();
37+
}
38+
sort(res.begin(), res.end());
39+
return res;
40+
}
41+
};

0 commit comments

Comments
 (0)