-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
187 changed files
with
6,214 additions
and
210 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
''' This DFS Algorithm will find out all the possible paths from all nodes in the Graph''' | ||
|
||
from collections import defaultdict | ||
|
||
|
||
class Graph: | ||
|
||
def __init__(self): | ||
self.graph = defaultdict(list) # create from existing library | ||
|
||
def addEdge(self, i, j): | ||
self.graph[i].append(j) # add a new edge | ||
|
||
# Actual algorithm of DFS in python | ||
def DFSUtil(self, j, isvisited): | ||
isvisited[j] = True | ||
print(j) | ||
for i in self.graph[j]: | ||
if isvisited[i] == False: | ||
self.DFSUtil(i, isvisited) | ||
|
||
def DFS(self, j): | ||
visited = [False] * (len(self.graph)) | ||
self.DFSUtil(j, visited) | ||
|
||
|
||
graph = Graph() | ||
graph.addEdge(0, 1) | ||
graph.addEdge(0, 2) | ||
graph.addEdge(1, 2) | ||
graph.addEdge(2, 0) | ||
graph.addEdge(2, 3) | ||
graph.addEdge(3, 3) | ||
|
||
for i in range(4): | ||
print("DFS from vertex " + str(i) + ": ") | ||
graph.DFS(i) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
#include<bits/stdc++.h> | ||
#include<cstdio> | ||
#include<cstdlib> | ||
#include<string> | ||
#define ll long long int | ||
#define loop(i,n) for(i=0;i<n;i++) | ||
#define loop1(i,s,e) for(ll i=s;i<=e;i++) | ||
#define io std::ios::sync_with_stdio(false);cin.tie(NULL); | ||
#define mod 1000000007 | ||
#define rep(i,c) for(typeof(c.begin())i=c.begin();i!=c.end();++i) | ||
#define pb push_back | ||
#define pii pair<ll,ll> | ||
using namespace std; | ||
#define MAX 109 | ||
#define INF 1000000000000000 | ||
ll dis[MAX]; | ||
bool visit[MAX]; | ||
vector<pii> edge[MAX]; | ||
void dijkstra(ll src,ll dest) | ||
{ | ||
priority_queue<pii,vector<pii>,greater<pii> > pq; | ||
while(!pq.empty()) | ||
pq.pop(); | ||
pq.push(make_pair(0,src)); | ||
dis[src]=0; | ||
while(!pq.empty()) | ||
{ | ||
ll w=pq.top().first; | ||
ll u=pq.top().second; | ||
pq.pop(); | ||
if(!visit[u]) | ||
{ | ||
visit[u]=1; | ||
if(dis[u]<w) | ||
continue; | ||
rep(i,edge[u]) | ||
{ | ||
ll v=(*i).first; | ||
ll wt=(*i).second; | ||
if(visit[v]) | ||
continue; | ||
if(dis[v]>dis[u]+wt) | ||
{ | ||
dis[v]=dis[u]+wt; | ||
pq.push(make_pair(dis[v],v)); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
int main() | ||
{ | ||
io | ||
ll n,m,a,b,c,i,src; | ||
cin>>n>>m; | ||
loop1(i,1,m) | ||
{ | ||
cin>>a>>b>>c; | ||
edge[a].pb(make_pair(b,c)); | ||
edge[b].pb(make_pair(a,c)); | ||
} | ||
loop(i,MAX) | ||
{ | ||
dis[i]=INF; | ||
visit[i]=false; | ||
} | ||
cin>>src; | ||
dijkstra(src,n); | ||
loop1(i,1,n) | ||
cout<<dis[i]<<" "; | ||
cout<<endl; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// Design and implement a data structure for LRU (Least Recently Used) cache. It should support the following operations: get and set. | ||
// | ||
// get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1. | ||
// set(key, value) - Set or insert the value if the key is not already present. When the cache reaches its capacity, it should invalidate the least recently used item before inserting the new item. | ||
// The LRU Cache will be initialized with an integer corresponding to its capacity. Capacity indicates the maximum number of unique keys it can hold at a time. | ||
// | ||
// Definition of “least recently used” : An access to an item is defined as a get or a set operation of the item. “Least recently used” item is the one with the oldest access time | ||
class f implements Comparable<f> { | ||
int priority; | ||
int key; | ||
int value; | ||
f(int a,int b,int prio) | ||
{ | ||
priority=prio; | ||
key=a; | ||
value=b; | ||
} | ||
|
||
@Override | ||
public int compareTo(f o) { | ||
// TODO Auto-generated method stub | ||
if(o.priority==this.priority) | ||
{ | ||
return o.key-this.key; | ||
} | ||
return this.priority-o.priority; | ||
|
||
} | ||
} | ||
public class Solution { | ||
TreeSet<f> ne; | ||
int pr; | ||
int max; | ||
HashMap<Integer,f> hm; | ||
public Solution(int capacity) { | ||
ne=new TreeSet<f>(); | ||
pr=0; | ||
max=capacity; | ||
hm=new HashMap<Integer,f>(); | ||
} | ||
|
||
public int get(int key) { | ||
if(hm.containsKey(key)) | ||
{ | ||
f g=hm.get(key); | ||
ne.remove(g); | ||
g.priority=pr++; | ||
ne.add(g); | ||
return g.value; | ||
// f g=hm.get(key); | ||
// g.priority=pr++; | ||
// return g.value; | ||
} | ||
else | ||
{ | ||
return -1; | ||
} | ||
} | ||
|
||
public void set(int key, int value) { | ||
//System.out.println(ne.size()+"> "+hm.size()+" "+key+" "+value); | ||
if(ne.size()==max && !hm.containsKey(key)) | ||
{ | ||
int ke=ne.pollFirst().key; | ||
// System.out.println(ke+"~"); | ||
hm.remove(ke); | ||
|
||
} | ||
{ | ||
if(hm.containsKey(key)) | ||
{ | ||
|
||
f g=hm.get(key); | ||
ne.remove(g); | ||
g.priority=pr++; | ||
g.value=value; | ||
ne.add(g); | ||
} | ||
else | ||
{ | ||
f vall=new f(key,value,pr++); | ||
hm.put(key, vall); | ||
|
||
ne.add(vall); | ||
|
||
|
||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
G4G HEAPS | ||
Given K sorted arrays of size K arranged in a form of a matrix, your task is to merge them. | ||
The output will be the sorted merged array. | ||
*/ | ||
|
||
|
||
// Space Complexity O(K) & Time Complexity O(n*log(k)) | ||
int ans[1000003]; | ||
int j; | ||
|
||
int *mergeKArrays(int arr[][N], int k) | ||
{ | ||
priority_queue <pair <int, pair<int, int> >, vector <pair <int, pair<int, int> > >, greater <pair <int, pair<int, int> > > > pq; | ||
for (int i = 0; i < k; i++) { | ||
pq.push(make_pair(arr[i][0], make_pair(i, 0))); | ||
} | ||
|
||
j = 0; | ||
for (int i = 0; i < k*k; i++) { | ||
ans[j++] = pq.top().first; | ||
int row = pq.top().second.first; | ||
int col = pq.top().second.second; | ||
// debug | ||
cout << ans[j-1] << " " << row << " " << col << endl; | ||
pq.pop(); | ||
|
||
if (col < k-1) { | ||
pq.push(make_pair(arr[row][col+1], make_pair(row, col+1))); | ||
} | ||
else { | ||
pq.push(make_pair(INT_MAX, make_pair(row, col+1))); | ||
} | ||
} | ||
|
||
return ans; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#include<stdio.h> | ||
void main(){ | ||
printf("Hello World"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
// MANCHESTER UNITED >>>>> BARCELONA | ||
#include <iostream> | ||
|
||
using namespace std; | ||
|
||
class node | ||
{ | ||
int info; | ||
node* next; | ||
public: | ||
node* enqueue(node*); | ||
node* deque(node*); | ||
node* display(node*); | ||
}; | ||
node* node::enqueue(node* start) | ||
{ | ||
node* temp; | ||
cout<<"enqueue...\n"; | ||
if(start==NULL) | ||
{ | ||
cout<<"Queue was empty..Entering first element..\n"; | ||
temp = new node; | ||
cout<<"Enter info..\n"; | ||
cin>>temp->info; | ||
temp->next = NULL; | ||
start = temp; | ||
temp = NULL; | ||
} | ||
else | ||
{ | ||
node* temp1; | ||
temp = start; | ||
while(temp->next) | ||
{ | ||
temp = temp -> next; | ||
} | ||
temp1 = new node; | ||
temp1 -> next = NULL; | ||
cout<<"Enter information..\n"; | ||
cin>>temp1->info; | ||
temp -> next = temp1; | ||
temp=temp1=NULL; | ||
} | ||
return start; | ||
} | ||
node* node::deque(node* start) | ||
{ | ||
node* temp; | ||
cout<<"Deleting..\n"; | ||
if(start==NULL) | ||
{ | ||
cout<<"Queue empty..\n"; | ||
return start; | ||
} | ||
else if(start->next == NULL) | ||
{ | ||
cout<<"Single element present..\n"; | ||
cout<<"Data deleted is.."<<start->info<<"\n"; | ||
delete start; | ||
start = NULL; | ||
return start; | ||
} | ||
else | ||
{ | ||
temp = start; | ||
start = start -> next; | ||
cout<<"Element deleted is.."<<temp->info<<" "<<"\n"; | ||
delete temp; | ||
return start; | ||
} | ||
} | ||
node* node::display(node* start) | ||
{ | ||
node* temp; | ||
if(start==NULL) | ||
{ | ||
cout<<"Queue empty..\n"; | ||
return start; | ||
} | ||
else | ||
{ | ||
temp = start; | ||
while(temp) | ||
{ | ||
cout<<" "<<temp->info<<" "; | ||
temp = temp -> next; | ||
} | ||
cout<<"\n"; | ||
temp = NULL; | ||
return start; | ||
} | ||
} | ||
int main() | ||
{ | ||
node* start = NULL; | ||
node n; | ||
int ch=1,ch1; | ||
while(ch) | ||
{ | ||
cout<<"\n\nEnter 1.enqueue\n\t2.dequeue\n\t3.display\n\t4.Exit\n\n"; | ||
cin>>ch1; | ||
switch(ch1) | ||
{ | ||
case 1: cout<<"enqueue..\n"; | ||
start = n.enqueue(start); | ||
break; | ||
case 2: cout<<"deque..\n"; | ||
start = n.deque(start); | ||
break; | ||
case 3: cout<<"Displaying..\n"; | ||
start = n.display(start); | ||
break; | ||
case 4: cout<<"Exit..\n"; | ||
ch=0; | ||
break; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import java.util.Scanner; | ||
|
||
public class MaiorPosicao { | ||
|
||
public static void main(String[] args) { | ||
int N, maior = 0, posicao = 0; | ||
Scanner sc = new Scanner(System.in); | ||
for(int i = 1; i <= 100; i++){ | ||
N = sc.nextInt(); | ||
if(N > maior){ | ||
maior = N; | ||
posicao = i; | ||
} | ||
} | ||
System.out.println(maior); | ||
System.out.println(posicao); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,8 @@ | ||
# python3 | ||
# Made By: Keshav Gupta | ||
# 7:35 PM | ||
# main method | ||
|
||
print("Enter the string to check") | ||
t = input("Please enter a string") | ||
|
||
if(t == t[::-1]): | ||
print("The string entered is palindrome") | ||
else: | ||
print("Not a palindrome") | ||
print("Not a palindrome") |
Oops, something went wrong.