Pass the Pillow | 2 Approaches | Easy Explanations | Leetcode 2582 | codestorywithMIK

Sdílet
Vložit
  • čas přidán 5. 07. 2024
  • Whatsapp Community Link : www.whatsapp.com/channel/0029...
    This is the 46th Video of our Playlist "Leetcode Easy : Popular Interview Problems" by codestorywithMIK
    In this video we will try to solve a good practice problem : Pass the Pillow | 2 Approaches | Easy Explanations | Leetcode 2582 | 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 : Pass the Pillow | 2 Approaches | Easy Explanations | Leetcode 2582 | codestorywithMIK
    Company Tags : MICROSOFT
    My solutions on Github(C++ & JAVA) : github.com/MAZHARMIK/Intervie...
    Leetcode Link : leetcode.com/problems/pass-th...
    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
    ╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
    ║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
    ╠╗║╚╝║║╠╗║╚╣║║║║║═╣
    ╚═╩══╩═╩═╩═╩╝╚╩═╩═╝
    Summary :
    Approach 1 (Simple Simulation)
    Time Complexity: O(time)
    Space Complexity: O(1)
    Description: This approach simulates the passing of the pillow in real-time. We maintain two variables, idx for the current position and dir for the direction (either forward or backward). As long as there is remaining time, we update the position and direction based on the current state, decrementing the time with each step until the time runs out. This approach is straightforward but may be inefficient for large time values due to its linear complexity.
    Approach 2 (Simple Maths)
    Time Complexity: O(1)
    Space Complexity: O(1)
    Description: This approach uses mathematical calculations to determine the final position without simulating each step. It calculates the number of full rounds of passing the pillow (totalFullRounds) and the remaining time (timeLeft). Depending on whether the number of full rounds is even or odd, it adjusts the final position accordingly. This method is highly efficient with constant time complexity, making it suitable for larger inputs.
    ✨ 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 • 38

  • @learn_in_shorts
    @learn_in_shorts Před 22 dny +11

    Bhaiya meyne kar liya hai 100% beat in java, thoda time laga but kar liya 😊

  • @bhuppidhamii
    @bhuppidhamii Před 22 dny +4

    i was not able to solve, even this easy Q's/
    thanks MIK

  • @user-ym1nv1pw8i
    @user-ym1nv1pw8i Před 21 dnem +2

    Bhaiya can you provide solution to today's leetcode-contest hard:
    "Find all the subarrays with their Bitwise And equals to K".

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

    Thanks a lot for 2nd approach. Makes sense now

  • @himanshusharma9132
    @himanshusharma9132 Před 22 dny +1

    I was able to solve this question by while loop though, couldnt understand the solution but thanks to you now i understand, beats 100%
    tag came in while loop based solution

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

    Thanks a lot bhaiya ❤❤

  • @tutuimam3381
    @tutuimam3381 Před 22 dny +1

    Thanks a lot

  • @wearevacationuncoverers

    thanks man for taking out time during weekends also

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

    i was trying a similar approach as approach 2 , bas n-1 see reminder and divide nahi kiya tha... and obv output bhi galat usse nikal raha tha...

  • @souravjoshi2293
    @souravjoshi2293 Před 22 dny

    approach-2 very well explained.

  • @aws_handles
    @aws_handles Před 22 dny

    Thanks itna detail me batane k lie guruji 😬

  • @manukesharwani2049
    @manukesharwani2049 Před 21 dnem

    sir please post biweekly contest solutions as well...

  • @EB-ot8uu
    @EB-ot8uu Před 22 dny

    done sir

  • @shabananoor9423
    @shabananoor9423 Před 22 dny

    ❤❤

  • @user-vs5rh1oz3w
    @user-vs5rh1oz3w Před 22 dny

    sir online assessment ke liye playlist plz

  • @bombblaster6945
    @bombblaster6945 Před 22 dny

    please solve this --> Leetcode 394. Decode String

  • @gui-codes
    @gui-codes Před 22 dny +1

    simulation se bana liya maine khud se.

  • @learnwithme8936
    @learnwithme8936 Před 22 dny +3

    kaand karte rehenge 🤣🤣🤣🤣

  • @legendcreatz9783
    @legendcreatz9783 Před 22 dny +9

    one line code -- > " return time/(n-1)%2==0 ?time%(n-1)+1 :n-time%(n-1); "

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

    class Solution {
    public int passThePillow(int n, int time) {
    int ans=1;
    if(time==n){
    for(int i=1;i

  • @apurvwaghmare5974
    @apurvwaghmare5974 Před 22 dny +1

    as always bhaiya nice explanation ❤
    Bhaiya ek help chaiyi thi muhje, pure DSA ke concepts krne h A to Z to aap please bata sakte ho k start kaha se start kru aur ek aur topis list kr sakte ho, ya phir ROADMAP banake ek video ya isi comment ke niche aap ek roadmap reply kr do mein ekdum dedication ke DSA krunga aur koshish karunga k lagbagh 6 months me aachi pakad aa jaye
    @codestorywithMIK

  • @utkarshsahay9908
    @utkarshsahay9908 Před 22 dny +1

    class Solution {
    public:
    int passThePillow(int n, int time) {
    int chunks = time / (n - 1);
    return chunks % 2 == 0 ? (time % (n - 1) + 1) : (n - time % (n - 1));
    }
    };❤❤

    • @gui-codes
      @gui-codes Před 22 dny

      bas yahi samjhne aya hu mai 😅

  • @chetan-eo7lw
    @chetan-eo7lw Před 22 dny +1

    first comment

  • @mdabrarkhan1943
    @mdabrarkhan1943 Před 22 dny

    bhaiya is approach ko rate kariye @codestorywithMIK
    class Solution {
    public:
    int passThePillow(int n, int time) {
    int p=1;
    bool rev = false;
    while(time--){
    if(p == n){
    rev=true;
    }else if(p == 1){
    rev = false;
    }
    if(rev){
    p--;
    }else{
    p++;
    }
    }
    return p;
    }
    };

  • @nikhilhaspe2734
    @nikhilhaspe2734 Před 21 dnem

    || Simple Easy to Understand Java Solution ||
    // Below brute force is way easier to understand
    // 1. Brute Force (Different than the video):
    class Solution {
    public int passThePillow(int n, int time) {
    int index = 1;
    boolean flag = true;
    while(time > 0){
    if(flag){
    index++;
    if(index == n)
    flag = false;
    }else{
    index--;
    if(index == 1)
    flag = true;
    }
    time--;
    }
    return index;
    }
    }
    // 2. Optimised Mathematical Solution:
    class Solution {
    public int passThePillow(int n, int time) {
    int fullRoundCount = time / (n-1);
    int remTimeCount = time % (n-1);
    return fullRoundCount%2 == 0 ? ++remTimeCount: n-remTimeCount;
    }
    }

  • @dayashankarlakhotia4943

    same code of problem leetcode 3178 different here 1base index;
    public int passThePillow(int n,int time){
    n--;
    int noOfRounds=time/n;
    int goTo=time%n;
    if(noOfRounds%2==0){
    return goTo+1;
    }else{
    return (n-goTo)+1;
    }
    }
    🎉❤

  • @rishabhprasad8638
    @rishabhprasad8638 Před 22 dny

    class Solution {
    public int passThePillow(int n, int time) {
    int i=1;
    boolean flag=true;
    while(time-->0){
    if(flag){
    i++;
    if(i==n){
    flag=!flag;
    }
    }else{
    i--;
    if(i==1){
    flag=!flag;
    }
    }
    }
    return i;
    }
    }