Skip to content

Commit 378cf49

Browse files
committed
2 parents ddf7899 + 6deece8 commit 378cf49

15 files changed

+917
-28
lines changed

DSA 450 GFG/max_sum_bottom_up.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#https://practice.geeksforgeeks.org/problems/maximum-sum-problem2211/1/
2+
3+
4+
#Approach 2 - Bottom Up approach
5+
6+
7+
def maxSum(self, n):
8+
# code here
9+
dp = [0]*(n+1)
10+
dp[0] = 0
11+
if(n >= 1):
12+
dp[1] = 1
13+
for i in range(2 , n+1):
14+
dp[i] = max((dp[int(i/2)] + dp[int(i/3)] + dp[int(i/4)]) , i)
15+
16+
return dp[n]
17+
18+
19+
#{
20+
# Driver Code Starts
21+
#Initial Template for Python 3
22+
23+
if __name__ == '__main__':
24+
t = int(input())
25+
for _ in range(t):
26+
n = int(input())
27+
ob = Solution()
28+
print(ob.maxSum(n))
29+
# } Driver Code Ends

DSA 450 GFG/max_sum_memoization.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
#https://practice.geeksforgeeks.org/problems/maximum-sum-problem2211/1/
3+
4+
# Approach 1 - Memoization
5+
6+
from functools import lru_cache
7+
8+
class Solution:
9+
@lru_cache(maxsize = 1000)
10+
def maxSum(self, n):
11+
# code here
12+
if(n == 0 or n == 1):
13+
return n
14+
else:
15+
return max( (self.maxSum(n//2) + self.maxSum(n//3) + self.maxSum(n//4)) , n )
16+
17+
18+
19+
#{
20+
# Driver Code Starts
21+
#Initial Template for Python 3
22+
23+
if __name__ == '__main__':
24+
t = int(input())
25+
for _ in range(t):
26+
n = int(input())
27+
ob = Solution()
28+
print(ob.maxSum(n))
29+
# } Driver Code Ends
30+
31+
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/* There is an undirected star graph consisting of n nodes labeled from 1 to n.
2+
A star graph is an undirected graph that has n nodes in which one is center node and has exactly n - 1 edges.
3+
4+
Given a 2D integer array edges where each edges[i] = [ui, vi] indicates that there is an edge between the nodes ui and vi.
5+
Return the center of the given star graph.
6+
4
7+
|
8+
2
9+
/ \
10+
1 3
11+
12+
Here 2 is the answer.
13+
14+
Approach: Store the nodes in vector that are connected to partciular node, if size of that vector equals to nodes-1 then
15+
print it is the centre. */
16+
17+
#include <bits/stdc++.h>
18+
using namespace std;
19+
20+
int findCenter(vector<vector<int>>& edges)
21+
{
22+
int n=edges.size()+1;
23+
24+
vector<int>l[n+1];
25+
26+
// Store the destintaion edges into source node vector
27+
for(int i=0;i<edges.size();i++)
28+
{
29+
l[edges[i][0]].push_back(edges[i][1]);
30+
l[edges[i][1]].push_back(edges[i][0]);
31+
}
32+
33+
// Check if vector size is equal to nodes-1, to find the center
34+
int center;
35+
for(int i=0;i<=n;i++)
36+
{
37+
if(l[i].size()==n-1)
38+
center=i;
39+
}
40+
return center;
41+
}
42+
43+
int main()
44+
{
45+
int src,dest,no_of_edges;
46+
cout<<"Enter number of edges"<<endl;
47+
cin>>no_of_edges;
48+
vector<vector<int>>edges;
49+
50+
for(int i=0;i<no_of_edges;i++)
51+
{
52+
vector<int>temp;
53+
54+
cout<<"Enter source of edge"<<endl;
55+
cin>>src;
56+
temp.push_back(src);
57+
cout<<"Enter destination of edge"<<endl;
58+
cin>>dest;
59+
temp.push_back(dest);
60+
61+
edges.push_back(temp);
62+
}
63+
cout<<"Center Of Star Graph : ";
64+
cout<<(findCenter(edges));
65+
return 0;
66+
}
67+
68+
69+
/*
70+
INPUT:
71+
Enter number of edges
72+
3
73+
Enter source of edge
74+
1
75+
Enter destination of edge
76+
2
77+
Enter source of edge
78+
2
79+
Enter destination of edge
80+
3
81+
Enter source of edge
82+
4
83+
Enter destination of edge
84+
2
85+
OUTPUT:
86+
Center Of Star Graph : 2
87+
88+
Time Complexity: O(no_of_edges)
89+
Space Complexity: O(no_of_edges)
90+
*/

Data Structures/Graphs/Khan_algo.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
'''
2+
author :saurabh jain
3+
to find whether a graph has a cycle or not not
4+
if not than print the topological sorting for them
5+
'''
6+
from collections import defaultdict
7+
8+
9+
class solution:
10+
11+
def __init__(self, vrt):
12+
self.vrt = vrt
13+
self.graph = defaultdict(list)
14+
15+
def edges(self, u, v):
16+
global vertex,indegree
17+
self.graph[u].append(v)
18+
if u not in indegree.keys():
19+
indegree[u]=0
20+
if v not in indegree.keys():
21+
indegree[v]=1
22+
23+
else:
24+
indegree[v]+=1
25+
vertex.add(u)
26+
vertex.add(v)
27+
28+
def topological(self):
29+
global vertex,indegree
30+
q=[]
31+
count=0
32+
for i in vertex:
33+
if indegree[i]==0:
34+
q.append(i)
35+
36+
result=[]
37+
38+
while q:
39+
40+
current=q.pop(0)
41+
count+=1
42+
result.append(current)
43+
44+
45+
46+
for i in self.graph[current]:
47+
indegree[i]-=1
48+
if indegree[i]==0:
49+
q.append(i)
50+
51+
if count!=len(vertex):
52+
print("there is no topolical string ")
53+
else:
54+
print(result)
55+
56+
57+
58+
return
59+
60+
61+
62+
63+
64+
indegree=defaultdict()
65+
vertex=set()
66+
67+
g =solution(6)
68+
g.edges(5, 2);
69+
g.edges(5, 0);
70+
g.edges(4, 0);
71+
g.edges(4, 1);
72+
g.edges(2, 3);
73+
g.edges(3, 1)
74+
print(indegree)
75+
print(vertex)
76+
77+
g.topological()
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
#define f(i,a,b) for(int i = a;i<b;i++)
5+
class Solution
6+
{
7+
public:
8+
//Function to find minimum time required to rot all oranges.
9+
void solve(int r, int c, queue<pair<int,int>>&q, vector<vector<int>>&grid){
10+
int rr[4] = {-1,1,0,0};
11+
int cc[4] = {0,0,-1,1};
12+
for(int i = 0;i<4;i++){
13+
int nr = r + rr[i];
14+
int nc = c + cc[i];
15+
16+
if(nr<0 || nc<0 || nr>=grid.size()|| nc>=grid[0].size())continue;
17+
if(grid[nr][nc] == 1){
18+
grid[nr][nc] = 2;
19+
q.push({nr,nc});
20+
}
21+
22+
}
23+
}
24+
int orangesRotting(vector<vector<int>>& grid) {
25+
queue<pair<int,int>>q;
26+
f(i,0,grid.size()){
27+
f(j,0,grid[0].size()){
28+
if(grid[i][j] == 2){
29+
q.push({i,j});
30+
}
31+
}
32+
}
33+
int timer = -1;
34+
while(!q.empty()){
35+
int k = q.size();
36+
while(k--){
37+
int r = q.front().first;
38+
int c = q.front().second;
39+
q.pop();
40+
solve(r,c,q,grid);
41+
}
42+
timer++;
43+
}
44+
for(auto i: grid){
45+
for(auto j :i){
46+
if(j == 1)return -1;
47+
}
48+
49+
}
50+
return timer;
51+
52+
53+
}
54+
};
55+
56+
int main(){
57+
int tc;
58+
cin >> tc;
59+
while(tc--){
60+
int n, m;
61+
cin >> n >> m;
62+
vector<vector<int>>grid(n, vector<int>(m, -1));
63+
for(int i = 0; i < n; i++){
64+
for(int j = 0; j < m; j++){
65+
cin >> grid[i][j];
66+
}
67+
}
68+
Solution obj;
69+
int ans = obj.orangesRotting(grid);
70+
cout << ans << "\n";
71+
}
72+
return 0;
73+
} //

0 commit comments

Comments
 (0)