Yeah man, let's support this guy. The efforts he is giving is speechless. One thing that makes him different from others is that he never ever asks for Subscribing or sharing or never promotes any online paid platform. His honesty will win many others like us. Keep it going bro
Is the graph single component or multiple component because if the graph is single component then can we find answer only by iterating through arr and find minimum of arr(i) (2)???
Please continue graph concept videos further. Please bhaiya because maine apka pahle tak I think 20 videos rak complete kar liya tha and beech me apne kuchh reason se break kiya tha so I am requesting ki graph ki playlist series complete kar dijiye because aur ka channel se graph mujhse nhi ho pa raha and apka bahut achhhe se samajh ata hai. Once again Thank you sir❤
Awesome bhaiya, and awesome voice too 😅. Just a suggestion, plz try to upload lc daily early in the morning (like 7:30 AM IST) so that ppl can watch it whenever they r free during the entire day.
Exact Same Java Code (Mapped line by line) : //Java class Solution { int answer = Integer.MAX_VALUE; public void dfs(int u, Map adj, boolean[] visited) { visited[u] = true; for (List edge : adj.get(u)) { int v = edge.get(0); int c = edge.get(1); answer = Math.min(answer, c); if (!visited[v]) { dfs(v, adj, visited); } } } public int minScore(int n, int[][] roads) { Map adj = new HashMap(); for (int[] vec : roads) { int u = vec[0]; int v = vec[1]; int d = vec[2]; adj.computeIfAbsent(u, k -> new ArrayList()).add( Arrays.asList(v, d)); adj.computeIfAbsent(v, k -> new ArrayList()).add( Arrays.asList(u, d)); } boolean[] visited = new boolean[n + 1]; //initially all False dfs(1, adj, visited); return answer; } }
Thanks for your guidance and teaching , MIK Sir. Solved it using BFS too. private int minScoreBfs(int n, int[][] roads) { Map graph = new HashMap(); for (int i = 0; i
@@codestorywithMIK instead you made my day dear Mazar bhaiya. Because leetcode - 1171 ne dimag ka dua kar ke rakha tha. (Ho sake toh iss question ka video bna Dena pls)🙏😔
Sure PrudhviRaj. I have tried to write the code below in Java exact same like C++ (line by line). Hope this helps : //Java class Solution { int answer = Integer.MAX_VALUE; public void dfs(int u, Map adj, boolean[] visited) { visited[u] = true; for (List edge : adj.get(u)) { int v = edge.get(0); int c = edge.get(1); answer = Math.min(answer, c); if (!visited[v]) { dfs(v, adj, visited); } } } public int minScore(int n, int[][] roads) { Map adj = new HashMap(); for (int[] vec : roads) { int u = vec[0]; int v = vec[1]; int d = vec[2]; adj.computeIfAbsent(u, k -> new ArrayList()).add( Arrays.asList(v, d)); adj.computeIfAbsent(v, k -> new ArrayList()).add( Arrays.asList(u, d)); } boolean[] visited = new boolean[n + 1]; //initially all False dfs(1, adj, visited); return answer; } }
Thanks Buddy for again saving me from Ghostly Graph, because in morning I was really frightened by this question but now after watching this video it gave me the confidence to go ahead.
what if in the first example dist bw 2 and 3 is 4 then using this method ans will be 4 . But that is not the correct answer as that edge is not included in the path
i just have one doubt after watching your bfs video , sometimes you put for(int v: adj.get(u)) loop under loop while(n--) and sometimes not what i meant to say is while(!q.isEmpty()) { Pair p=q.peek(); q.poll(); // int u=p.f; int dist=p.s; vis[u]=true; result=Math.min(result,dist); for(Pair np:adj.get(u)) { if(!vis[np.f]) { q.add(np); }// this commented portion is sometimes under while loop i.e while(n--) where n is q size and sometimes not why? }
Both traversal is same in terms of order of processing of elements . But, when you need to process the elements level by level , then inner while loop of (n-) is required. Generally qns, in which you need to find the level, or something related to level, then use inner loop method
Bro, I love your graphs playlist its awesome, but please try to complete graphs concepts playlist, since if we get 1 video a week it's going to take too long...
BHAI EK YE QUESTION HAI ISKA TLE araha hai (not on leetcode): Problem Statement You are given two sorted arrays A and B of size N and M respectively. You have to find the value V, which is the summation of (A[j] - A[i]) for all pairs of i and j such that (j - i) is present in array B. Input The first line contains two space separated integers N and M - size of arrays A and B respectively. The second line contains N integers A1, A2, ... AN. The third line contains M integers B1, B2, ... BM. Constraints: 1 ≤ N ≤ 2×105 1 ≤ M < N 1 ≤ A1 ≤ A2 ≤ ... ≤ AN ≤ 108. 1 ≤ B1 < B2 < ... < BM < N. Output Print a single integer, the value of V. Example Sample Input 1: 4 2 1 2 3 4 1 3 Sample Output 1: 6 Sample Explanation 1: Valid pairs of (i, j) are (1, 2), (2, 3), (3, 4), (1, 4). MY SOLUTION: (CORRECT BUT GIVES TLE) #include // header file includes every Standard library using namespace std; int help(vector&a,unordered_map &m){ int ans=0; for(int i=0;i=0 && jn>>m; vector a,b; int x; for(int i=0;i>x; a.push_back(x); } int ans=0; unordered_map mp; for(int i=0;i>x; b.push_back(x); mp[x]++; } ans=help(a,mp); cout
SIR PLZ TELL ME WHERE AM I WRONG int dfs(int node,vector &adj,vector &visited){ visited[node]=1; int minedge=INT_MAX; for(auto it:adj[node]){ minedge=min(minedge,it.second); if(!visited[it.first]){ int mini=dfs(it.first,adj,visited); minedge=min(minedge,mini); } } return minedge; } int minScore(int n, vector& roads) { int minans=1e8; for(int i=0;i
Guys let's support bhaiya for his efforts 👏👏
Thanks a lot Harsh ❤️❤️❤️
Means a lot to me
Bro we've already started supporting thanks for joining us.
Yeah man, let's support this guy.
The efforts he is giving is speechless. One thing that makes him different from others is that he never ever asks for Subscribing or sharing or never promotes any online paid platform.
His honesty will win many others like us. Keep it going bro
Thanks a ton .
Great Explanation
thx u sir
One of the best channel ever.
It means a lot to me. Thank you so much ❤️
indeed
8k soon!🎉🥳
Thank you for your wishes 😇🙏❤️
bhai yarr jodd banda hai tu bohot videos dekhe tere maja aa gaya
Thanks a lot Aayush ❤️❤️
thanks to your easy explanation videos I was able to solve this question on my own using BFS 😇
Yesss 💪💪💪
Thanks to you i am able to solve graph problems now
Is the graph single component or multiple component because if the graph is single component then can we find answer only by iterating through arr and find minimum of arr(i) (2)???
Please continue graph concept videos further. Please bhaiya because maine apka pahle tak I think 20 videos rak complete kar liya tha and beech me apne kuchh reason se break kiya tha so I am requesting ki graph ki playlist series complete kar dijiye because aur ka channel se graph mujhse nhi ho pa raha and apka bahut achhhe se samajh ata hai. Once again Thank you sir❤
Yes Prakash.
This week, i will be uploading more videos on that.
Great Explanation!, I think this can be solved by using Union by Rank/size in better time.
Indeed Raubinsh. I will definitely plan a video on that too
Thank you for watching ❤️
Awesome bhaiya, and awesome voice too 😅. Just a suggestion, plz try to upload lc daily early in the morning (like 7:30 AM IST) so that ppl can watch it whenever they r free during the entire day.
Thanks a lot Shen.
Actually i was travelling, and hence the delay. Will try to upload early from now again.
Exact Same Java Code (Mapped line by line) :
//Java
class Solution {
int answer = Integer.MAX_VALUE;
public void dfs(int u, Map adj, boolean[] visited) {
visited[u] = true;
for (List edge : adj.get(u)) {
int v = edge.get(0);
int c = edge.get(1);
answer = Math.min(answer, c);
if (!visited[v]) {
dfs(v, adj, visited);
}
}
}
public int minScore(int n, int[][] roads) {
Map adj = new HashMap();
for (int[] vec : roads) {
int u = vec[0];
int v = vec[1];
int d = vec[2];
adj.computeIfAbsent(u, k -> new ArrayList()).add(
Arrays.asList(v, d));
adj.computeIfAbsent(v, k -> new ArrayList()).add(
Arrays.asList(u, d));
}
boolean[] visited = new boolean[n + 1]; //initially all False
dfs(1, adj, visited);
return answer;
}
}
I think JJ is absent in today's lecture🤣 🤣 🤣
Ha ha. Good point Mani 😁
@@codestorywithMIK BTW I've posted BFS approach too.
@@manimanohar_001 Saw the video on phone bhai, bahar tha and laptop ni tha! 😅
@@JJ-tp2dd koi nhi bhai sab koi kabhi kabhi busy ho jaate hai.
BTW are you in LinkedIn?
Thanks a lot
Awesome !!
wow
bhaiya solved using bfs
Thanks for your guidance and teaching , MIK Sir.
Solved it using BFS too.
private int minScoreBfs(int n, int[][] roads) {
Map graph = new HashMap();
for (int i = 0; i
Morning: Unable to solve.
Evening: Solved
The difference between morning and evening is MIK's explanation video.
Wow.
You made my day Mani
Thank you so much
@@codestorywithMIK instead you made my day dear Mazar bhaiya.
Because leetcode - 1171 ne dimag ka dua kar ke rakha tha. (Ho sake toh iss question ka video bna Dena pls)🙏😔
Sure Mani
did this question on my own using bfs ...
class Solution {
public:
int minScore(int n, vector& roads) {
vector adj(n+1);
for(auto it: roads)
{
int u= it[0], v=it[1], d= it[2];
adj[u].push_back({v,d});
adj[v].push_back({u,d});
}
vector vis(n+1,0);
queue q; int mini=INT_MAX;
vis[1]=1;
for(auto it: adj[1])
{
mini=min(mini,it.second);
}
q.push(1);
while(!q.empty())
{
int node= q.front(); q.pop();
for(auto it: adj[node])
{
int adjnode= it.first;
mini=min(mini,it.second);
if(!vis[adjnode])
{
vis[adjnode]=1;
q.push(adjnode);
}
}
}
return mini;
}
};
Sir i am java coder if possible also do same code in java pls
Sure PrudhviRaj.
I have tried to write the code below in Java exact same like C++ (line by line). Hope this helps :
//Java
class Solution {
int answer = Integer.MAX_VALUE;
public void dfs(int u, Map adj, boolean[] visited) {
visited[u] = true;
for (List edge : adj.get(u)) {
int v = edge.get(0);
int c = edge.get(1);
answer = Math.min(answer, c);
if (!visited[v]) {
dfs(v, adj, visited);
}
}
}
public int minScore(int n, int[][] roads) {
Map adj = new HashMap();
for (int[] vec : roads) {
int u = vec[0];
int v = vec[1];
int d = vec[2];
adj.computeIfAbsent(u, k -> new ArrayList()).add(
Arrays.asList(v, d));
adj.computeIfAbsent(v, k -> new ArrayList()).add(
Arrays.asList(u, d));
}
boolean[] visited = new boolean[n + 1]; //initially all False
dfs(1, adj, visited);
return answer;
}
}
Tqns
I understand the importance of visited array, still in this question we are visiting the same nodes multiple time. How will that work!?
If it is a connected graph,then we can simply take the minimum from roads. Don't need to do anythinng else
BFS code :
void bfs(vector adj[], int node, int &result, int n){
queue q;
vector visited(n+1, 0);
q.push(node);
visited[node] = 1;
while(!q.empty()){
int curr = q.front();
q.pop();
for(auto it : adj[curr]){
int v = it.first;
int dis = it.second;
result = min(result, dis);
if(!visited[v]){
visited[v] = 1;
q.push(v);
}
}
}
}
Thanks Buddy for again saving me from Ghostly Graph, because in morning I was really frightened by this question but now after watching this video it gave me the confidence to go ahead.
💪💪 keep it up
what if in the first example dist bw 2 and 3 is 4 then using this method ans will be 4 .
But that is not the correct answer as that edge is not included in the path
i just have one doubt after watching your bfs video , sometimes you put for(int v: adj.get(u)) loop under loop while(n--)
and sometimes not
what i meant to say is
while(!q.isEmpty())
{
Pair p=q.peek();
q.poll();
// int u=p.f;
int dist=p.s;
vis[u]=true;
result=Math.min(result,dist);
for(Pair np:adj.get(u))
{
if(!vis[np.f])
{
q.add(np);
}//
this commented portion is sometimes under while loop i.e while(n--) where n is q size and sometimes not
why?
}
}
Both traversal is same in terms of order of processing of elements .
But, when you need to process the elements level by level , then inner while loop of (n-) is required.
Generally qns, in which you need to find the level, or something related to level, then use inner loop method
Bro, I love your graphs playlist its awesome, but please try to complete graphs concepts playlist, since if we get 1 video a week it's going to take too long...
Thanks a lot .
And yes, this week will be uploading more on Graph Concepts playlist
YE QUESTION DIJIKSTA SE SOLVE NAHI HO SAKTA KYA?
Why this not work??
class Solution {
public:
int minScore(int n, vector& roads) {
int mini=INT_MAX;
for(auto road:roads){
mini=min(mini,road[2]);
}
return mini;
}
};
BHAI EK YE QUESTION HAI ISKA TLE araha hai (not on leetcode):
Problem Statement
You are given two sorted arrays A and B of size N and M respectively.
You have to find the value V, which is the summation of (A[j] - A[i]) for all pairs of i and j such that (j - i) is present in array B.
Input
The first line contains two space separated integers N and M - size of arrays A and B respectively.
The second line contains N integers A1, A2, ... AN.
The third line contains M integers B1, B2, ... BM.
Constraints:
1 ≤ N ≤ 2×105
1 ≤ M < N
1 ≤ A1 ≤ A2 ≤ ... ≤ AN ≤ 108.
1 ≤ B1 < B2 < ... < BM < N.
Output
Print a single integer, the value of V.
Example
Sample Input 1:
4 2
1 2 3 4
1 3
Sample Output 1:
6
Sample Explanation 1:
Valid pairs of (i, j) are (1, 2), (2, 3), (3, 4), (1, 4).
MY SOLUTION: (CORRECT BUT GIVES TLE)
#include // header file includes every Standard library
using namespace std;
int help(vector&a,unordered_map &m){
int ans=0;
for(int i=0;i=0 && jn>>m;
vector a,b;
int x;
for(int i=0;i>x;
a.push_back(x);
}
int ans=0;
unordered_map mp;
for(int i=0;i>x;
b.push_back(x);
mp[x]++;
}
ans=help(a,mp);
cout
Hello guys, can anybody tell me why par wala not working
class Solution {
public:
int minn = INT_MAX;
int N;
void go(int src, int par, vector& graph) {
for(auto it: graph[src]) {
if(it.first != par) {
minn = min(minn, it.second);
go(it.first, src, graph);
}
}
}
int minScore(int n, vector& roads) {
N = n;
vector graph(n+1);
for(auto it: roads) {
graph[it[0]].push_back({it[1], it[2]});
graph[it[1]].push_back({it[0], it[2]});
}
go(1, 0, graph);
return minn;
}
};
SIR PLZ TELL ME WHERE AM I WRONG
int dfs(int node,vector &adj,vector &visited){
visited[node]=1;
int minedge=INT_MAX;
for(auto it:adj[node]){
minedge=min(minedge,it.second);
if(!visited[it.first]){
int mini=dfs(it.first,adj,visited);
minedge=min(minedge,mini);
}
}
return minedge;
}
int minScore(int n, vector& roads) {
int minans=1e8;
for(int i=0;i
Java Implementation:
class Solution {
private int mini;
private void dfs(int u, Map adj, boolean[] visited) {
if(!adj.containsKey(u)) return;
visited[u] = true;
for(Pair edge: adj.get(u)) {
int v = edge.first;
int c = edge.second;
mini = Math.min(mini, c);
if(!visited[v]) {
dfs(v,adj,visited);
}
}
}
public int minScore(int n, int[][] roads) {
mini = Integer.MAX_VALUE;
boolean[] visited = new boolean[n+1];
Map adj = new HashMap();
for(int[] road : roads) {
int u = road[0];
int v = road[1];
int d = road[2];
adj.computeIfAbsent(u, k-> new ArrayList()).add(new Pair(v, d));
adj.computeIfAbsent(v, k-> new ArrayList()).add(new Pair(u,d));
}
dfs(1,adj,visited);
return mini;
}
}
class Pair {
int first;
int second;
Pair(int _first, int _second) {
this.first = _first;
this.second = _second;
}
}
Everyone was waiting for your awesome java code 🙂💪👍🏻
@@codestorywithMIK 😃
class Solution {
int min=Integer.MAX_VALUE;
public int minScore(int n, int[][] roads) {
boolean visited[]=new boolean[n+1];
dfs(0,visited,roads);
return min;
}
void dfs(int go,boolean visited[],int[][] roads){
for(int start=go;start