Average Waiting Time | Simple Simulation | Leetcode 1701 | codestorywithMIK

Sdílet
Vložit
  • čas přidán 7. 07. 2024
  • Whatsapp Community Link : www.whatsapp.com/channel/0029...
    This is the 96th Video of our Playlist "Arrays - 1D & 2D : Popular Interview Problems" by codestorywithMIK
    In this video we will try to solve a good simulation problem : Average Waiting Time | Simple Simulation | Leetcode 1701 | codestorywithMIK
    I will explain the intuition so easily that you will never forget and start seeing this as cakewalk EASYYY.
    We will do live coding after explanation and see if we are able to pass all the test cases.
    Also, please note that my Github solution link below contains both C++ as well as JAVA code.
    Problem Name : Average Waiting Time | Simple Simulation | Leetcode 1701 | codestorywithMIK
    Company Tags : Will update soon
    My solutions on Github(C++ & JAVA) : github.com/MAZHARMIK/Intervie...
    Leetcode Link : leetcode.com/problems/average...
    My DP Concepts Playlist : • Roadmap for DP | How t...
    My Graph Concepts Playlist : • Graph Concepts & Qns -...
    My Recursion Concepts Playlist : • Introduction | Recursi...
    My GitHub Repo for interview preparation : github.com/MAZHARMIK/Intervie...
    Instagram : / codestorywithmik
    Facebook : / 100090524295846
    Twitter : / cswithmik
    Subscribe to my channel : / @codestorywithmik
    ╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
    ║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
    ╠╗║╚╝║║╠╗║╚╣║║║║║═╣
    ╚═╩══╩═╩═╩═╩╝╚╩═╩═╝
    Approach Summary :
    The approach to calculate the average waiting time for customers is as follows:
    Initialization:
    totalWaitTime is initialized to accumulate the total waiting time for all customers.
    currTime is initialized to keep track of the current time as the customers are processed.
    Iterate through Customers:
    Loop through each customer in the given list/array.
    For each customer, extract arrivalTime and cookTime.
    Update Current Time:
    If the currTime is less than the customer's arrivalTime, update currTime to the customer's arrivalTime because the chef will wait for the customer to arrive.
    Calculate Waiting Time:
    Calculate the waiting time for the current customer as currTime + cookTime - arrivalTime.
    Add the waiting time to totalWaitTime.
    Update Current Time After Cooking:
    Increment currTime by cookTime to reflect the time taken to cook the current customer's order.
    Calculate Average Waiting Time:
    Finally, calculate the average waiting time by dividing totalWaitTime by the number of customers n.
    This approach ensures that we account for both the waiting time due to early arrival and the cooking time for each customer, ultimately providing the average waiting time across all customers.
    ✨ Timelines✨
    00:00 - Introduction
    #coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge#leetcodequestions #leetcodechallenge #hindi #india #hindiexplanation #hindiexplained #easyexplaination #interview#interviewtips #interviewpreparation #interview_ds_algo #hinglish #github #design #data #google #video #instagram #facebook #leetcode #computerscience #leetcodesolutions #leetcodequestionandanswers #code #learning #dsalgo #dsa #newyear2024

Komentáře • 40

  • @AbhijeetMuneshwar
    @AbhijeetMuneshwar Před 19 dny +18

    Respected MIK Sir,
    I’m learning to be consistent from you 🙏🏼

  • @IronmanDon-my6fw
    @IronmanDon-my6fw Před 19 dny +20

    It is first come first serve algorithm of operating system

  • @darshitgajjar5199
    @darshitgajjar5199 Před 18 dny +1

    You're Really helping the CP community. Big Kudos from my side!!
    .....
    Please make this kind of Video. you motivated us to fight for our Goal

  • @gui-codes
    @gui-codes Před 19 dny +5

    I was able to solve on my own. After doing some mistakes, it got accepted.
    I am here to thank you for helping me improve my problem solving skills. Thanks a lot MIK

  • @sonakshibajpai6445
    @sonakshibajpai6445 Před 19 dny +4

    i used to be scared of doing POTDs when i started dsa , now the first thing i do is watch your video in the morning thank you!

  • @manasdeora4601
    @manasdeora4601 Před 19 dny +1

    I made the exact same logic and on my own. Though this is easy one, but I am glad that the thought process matched.

  • @star_Cedric
    @star_Cedric Před 19 dny +5

    Hey Mik, your explanations are too good.
    Btw, this was my approach -
    class Solution {
    public:
    double averageWaitingTime(vector& customers) {
    int n = customers.size();
    double customersWaitingTime = 0;
    int cookingStartTime = 0;
    int servingTime = 0;

    int i = 0;
    while(i != n){
    cookingStartTime = max(customers[i][0], servingTime);
    servingTime = cookingStartTime + customers[i][1];
    customersWaitingTime += (servingTime - customers[i][0]);
    i++;
    }
    return (customersWaitingTime / n);
    }
    };

  • @rahulkumar-wz9jc
    @rahulkumar-wz9jc Před 19 dny +1

    Bhaiya yr aap kamaal ho daily morning uthta hu toh dekhta hu ki aapne leetcode ka potd solve krke daal diye toh esse khaafi motivation milti h ki ha bhaiya daale toh krna toh bnta h thnq bhaiya chizon ko etna aasan bnane ke liye

  • @kumarsaurabh6207
    @kumarsaurabh6207 Před 19 dny +2

    it is based on FCFS algorithm, we can take a separate array for completion time of orders and then can use the formula waiting time = completion time - arrival time; at last add all waiting times and divide by number of orders or customers.

  • @froglighthouse1223
    @froglighthouse1223 Před 19 dny +9

    Because of you I have started to get better at coding…. If I get placed in the next year you will be the reason for it. Thank you sir

    • @codestorywithMIK
      @codestorywithMIK  Před 19 dny +11

      Means a lot ❤️🙏
      I am sure you will crack the interviews.
      Keep up the consistency and hard work, and no one will stop you

  • @ReshuSharma-jc7yu
    @ReshuSharma-jc7yu Před 19 dny

    Thank You @codestorywithMIK because of you i have completely lost my fear of DSA looking forward for your new playlist of backtracking and heap by the way i have completed this at 5:30 am because of you looking for more guidance with you

  • @user-hn9ke6ty2g
    @user-hn9ke6ty2g Před 19 dny +1

    you r a gem sir

  • @syedhamid-ky3su
    @syedhamid-ky3su Před 19 dny +1

    Based on FCFS algorithm which we studied in operating system

  • @user-ym1nv1pw8i
    @user-ym1nv1pw8i Před 19 dny +3

    My approach :
    class Solution {
    public:
    double averageWaitingTime(vector& customers) {
    int n = customers.size();
    long long currentTime = customers[0][0];
    long long waitingTime = 0;
    for(auto &vec : customers) {
    if(vec[0] > currentTime){
    currentTime = vec[0];
    }
    currentTime += vec[1];
    waitingTime += (currentTime - vec[0]);
    }
    return (double)(waitingTime)/(n);
    }
    };

  • @aizad786iqbal
    @aizad786iqbal Před 19 dny +1

    aaj ka question khud se kar liya... pehle mai itne bade question padh ke ghabra jaata tha..
    your videos help ..
    bas ab series waali playlist start karni ... starting with recurison...
    my code intially failed beacause maine total time etc int mai le liya tha..kuch range ka issue tha I think
    class Solution {
    public double averageWaitingTime(int[][] customers) {
    int n = customers.length;
    double total_time = 0;
    int arrival_time = 0;
    int prepare_time = 0;
    double avg_time= 0;
    for(int[] x : customers){
    arrival_time = x[0];
    prepare_time = x[1];
    if(arrival_time > total_time){
    total_time += (arrival_time - total_time);
    }
    total_time += prepare_time;
    double waiting_time = total_time - arrival_time;
    avg_time += waiting_time;
    }
    return avg_time / n;
    }
    }

  • @ugcwithaddi
    @ugcwithaddi Před 19 dny

    Thank you 😊
    I could do it on my own

  • @user-ub2is4rs4x
    @user-ub2is4rs4x Před 19 dny

    It was an easy medium. Solved 😍

  • @gauravbanerjee2898
    @gauravbanerjee2898 Před 19 dny +1

    Thanks a lot bhaiya ❤❤ Congrats for 56k subs 🥳🥳

  • @xyzhacker00999
    @xyzhacker00999 Před 18 dny +1

    good

  • @pain9569
    @pain9569 Před 19 dny +2

    Today I completed my 100 Days of Streak
    To be honest earlier I was so bad, I used to take hours and was still not able to solve the problems, in the last 20 days, I am able to solve most of the POTDs on my own, all thanks to you. Because the time I got stuck on a question, you helped me thinking of its approaches from base. Thank you so much Mazhar Bhaiya 😌❤

    • @TheRandomPerson49
      @TheRandomPerson49 Před 18 dny +2

      I just started making streaks. Hope I will achieve this milestone. BTW, Keep it up man 🫡

    • @pain9569
      @pain9569 Před 18 dny +1

      @@TheRandomPerson49 Thanks, best of luck
      Still a lot to achieve 😄

  • @noname-te8nn
    @noname-te8nn Před 19 dny

    sir please solve count subarray with and value k, it would be very helpful

  • @shabananoor9423
    @shabananoor9423 Před 19 dny

    ❤❤

  • @asimshah1824
    @asimshah1824 Před 19 dny +1

    Funfact this question is the same as FCFS of operating system

  • @Karnn_Bose
    @Karnn_Bose Před 18 dny +1

    sir which writing software do you use??

  • @main_karan
    @main_karan Před 19 dny

    ❤❤❤

  • @ShouryaGupta-nn5ev
    @ShouryaGupta-nn5ev Před 19 dny

    plzz upload the soln 🙏number of subarray with AND equal to K

  • @rakeshprajapati9916
    @rakeshprajapati9916 Před 19 dny

    isn't there any good approach for this that takes less time?

  • @factinsaan4333
    @factinsaan4333 Před 19 dny +1

    class Solution {
    public:
    double averageWaitingTime(vector& c) {
    double ans=0.0;
    int n=c.size();
    int start_time=c[0][0];
    int end_time=c[0][1]+start_time;
    long long total=c[0][1];
    for(int i=1;i

  • @Shivamsingh-ry6bz
    @Shivamsingh-ry6bz Před 18 dny +2

    my approach
    class Solution {
    public double averageWaitingTime(int[][] arr) {
    int n=arr.length;
    double sum=arr[0][1];
    int p=arr[0][0]+arr[0][1];
    for(int i=1;i

  • @tarunsingh2480
    @tarunsingh2480 Před 18 dny

    class Solution {
    public:
    double averageWaitingTime(vector& customers) {
    int n = customers.size();
    double wait=0;
    double time = customers[0][0];
    for(auto &it: customers){
    if(time>=it[0]){
    time+= it[1];
    int x =time-it[0];
    wait+=x;
    }
    else{
    time =it[0];
    time+= it[1];
    int x =time-it[0];
    wait+=x;
    }
    }
    return (wait/n);
    }
    };

  • @03_utpallucky40
    @03_utpallucky40 Před 19 dny +3

    class Solution {
    public:
    double averageWaitingTime(vector& customers) {
    double ans=0;
    long long n=customers.size();
    long long waitingTime=customers[0][1];
    int totalTime=customers[0][0]+customers[0][1];
    for(int i=1;i

  • @anupthakur489
    @anupthakur489 Před 19 dny

    my approach
    class Solution {
    public:
    double averageWaitingTime(vector& customers) {
    int finishtime =0;
    int waitingtime=0;
    double sum = 0;
    for(int i = 0;i

  • @dayashankarlakhotia4943
    @dayashankarlakhotia4943 Před 19 dny +1

    public double averageWaitingTime(int[][]customers){
    double wait=0;
    int time=-1;
    for(int[]vec:customers){
    time=(time