Minimum Score of a Path Between Two Cities | Leetcode - 2492 | GOOGLE | Explanation ➕ Live Coding

Поділитися
Вставка
  • Опубліковано 7 січ 2025

КОМЕНТАРІ • 71

  • @shivsakthi37
    @shivsakthi37 Рік тому +20

    Guys let's support bhaiya for his efforts 👏👏

    • @codestorywithMIK
      @codestorywithMIK  Рік тому +2

      Thanks a lot Harsh ❤️❤️❤️
      Means a lot to me

    • @manimanohar_001
      @manimanohar_001 Рік тому +2

      Bro we've already started supporting thanks for joining us.

    • @souravjoshi2293
      @souravjoshi2293 Рік тому +1

      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

  • @iamnoob7593
    @iamnoob7593 3 місяці тому +1

    Thanks a ton .

  • @anuppatankar4294
    @anuppatankar4294 Рік тому +6

    Great Explanation

  • @shikharpandya4927
    @shikharpandya4927 6 місяців тому +1

    thx u sir

  • @krishnaneeldey8722
    @krishnaneeldey8722 Рік тому +3

    One of the best channel ever.

  • @humanity7880
    @humanity7880 Рік тому +1

    8k soon!🎉🥳

  • @aayushtaparia2676
    @aayushtaparia2676 Рік тому +2

    bhai yarr jodd banda hai tu bohot videos dekhe tere maja aa gaya

  • @r.beditz3674
    @r.beditz3674 Рік тому +3

    thanks to your easy explanation videos I was able to solve this question on my own using BFS 😇

  • @souravjoshi2293
    @souravjoshi2293 Рік тому +3

    Thanks to you i am able to solve graph problems now

  • @opgamers9660
    @opgamers9660 5 місяців тому +2

    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)???

  • @Prakash_8873
    @Prakash_8873 Рік тому +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❤

    • @codestorywithMIK
      @codestorywithMIK  Рік тому

      Yes Prakash.
      This week, i will be uploading more videos on that.

  • @RaubinshArya
    @RaubinshArya Рік тому +3

    Great Explanation!, I think this can be solved by using Union by Rank/size in better time.

    • @codestorywithMIK
      @codestorywithMIK  Рік тому

      Indeed Raubinsh. I will definitely plan a video on that too
      Thank you for watching ❤️

  • @xiaoshen194
    @xiaoshen194 Рік тому +2

    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.

    • @codestorywithMIK
      @codestorywithMIK  Рік тому

      Thanks a lot Shen.
      Actually i was travelling, and hence the delay. Will try to upload early from now again.

  • @codestorywithMIK
    @codestorywithMIK  Рік тому +4

    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;
    }
    }

    • @manimanohar_001
      @manimanohar_001 Рік тому +2

      I think JJ is absent in today's lecture🤣 🤣 🤣

    • @codestorywithMIK
      @codestorywithMIK  Рік тому +1

      Ha ha. Good point Mani 😁

    • @manimanohar_001
      @manimanohar_001 Рік тому

      @@codestorywithMIK BTW I've posted BFS approach too.

    • @JJ-tp2dd
      @JJ-tp2dd Рік тому +2

      @@manimanohar_001 Saw the video on phone bhai, bahar tha and laptop ni tha! 😅

    • @manimanohar_001
      @manimanohar_001 Рік тому +2

      @@JJ-tp2dd koi nhi bhai sab koi kabhi kabhi busy ho jaate hai.
      BTW are you in LinkedIn?

  • @tutuimam3381
    @tutuimam3381 Рік тому +1

    Thanks a lot

  • @dojoPojo
    @dojoPojo Рік тому +1

    Awesome !!

  • @aryanbhardwaj7519
    @aryanbhardwaj7519 2 місяці тому +1

    wow

  • @OmjiKesharwani-b5t
    @OmjiKesharwani-b5t 7 місяців тому +1

    bhaiya solved using bfs

  • @AbhijeetMuneshwar
    @AbhijeetMuneshwar 6 місяців тому +1

    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

  • @manimanohar_001
    @manimanohar_001 Рік тому +3

    Morning: Unable to solve.
    Evening: Solved
    The difference between morning and evening is MIK's explanation video.

    • @codestorywithMIK
      @codestorywithMIK  Рік тому

      Wow.
      You made my day Mani
      Thank you so much

    • @manimanohar_001
      @manimanohar_001 Рік тому +1

      @@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)🙏😔

    • @codestorywithMIK
      @codestorywithMIK  Рік тому

      Sure Mani

  • @unknown47896
    @unknown47896 5 місяців тому

    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;
    }
    };

  • @prudhvirajmacherla9854
    @prudhvirajmacherla9854 Рік тому +1

    Sir i am java coder if possible also do same code in java pls

    • @codestorywithMIK
      @codestorywithMIK  Рік тому +2

      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;
      }
      }

    • @prudhvirajmacherla9854
      @prudhvirajmacherla9854 Рік тому +1

      Tqns

  • @KishanSingh-vc3re
    @KishanSingh-vc3re 10 місяців тому +1

    I understand the importance of visited array, still in this question we are visiting the same nodes multiple time. How will that work!?

  • @yogeshagarwal403
    @yogeshagarwal403 11 місяців тому +1

    If it is a connected graph,then we can simply take the minimum from roads. Don't need to do anythinng else

  • @kamranwarsi12b22
    @kamranwarsi12b22 Рік тому +1

    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);
    }
    }
    }
    }

  • @molyoxide8358
    @molyoxide8358 Рік тому +6

    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.

  • @RajMishra-bk2by
    @RajMishra-bk2by 5 місяців тому

    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

  • @satyamkumaryadav1560
    @satyamkumaryadav1560 Рік тому +1

    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?
    }


    }

    • @codestorywithMIK
      @codestorywithMIK  Рік тому

      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

  • @mewaconite
    @mewaconite Рік тому +1

    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...

    • @codestorywithMIK
      @codestorywithMIK  Рік тому +1

      Thanks a lot .
      And yes, this week will be uploading more on Graph Concepts playlist

  • @nish0798
    @nish0798 10 місяців тому

    YE QUESTION DIJIKSTA SE SOLVE NAHI HO SAKTA KYA?

  • @nikhil_squats
    @nikhil_squats 17 днів тому

    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;
    }
    };

  • @kartikkk4583
    @kartikkk4583 Рік тому

    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

  • @dojoPojo
    @dojoPojo Рік тому

    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;
    }
    };

  • @ANIKETSINGH-sy2hg
    @ANIKETSINGH-sy2hg 11 місяців тому

    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

  • @JJ-tp2dd
    @JJ-tp2dd Рік тому +2

    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;
    }
    }

    • @codestorywithMIK
      @codestorywithMIK  Рік тому +1

      Everyone was waiting for your awesome java code 🙂💪👍🏻

    • @JJ-tp2dd
      @JJ-tp2dd Рік тому

      @@codestorywithMIK 😃

  • @yatri6329
    @yatri6329 Рік тому

    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