Pass the Pillow | 2 Approaches | Easy Explanations | Leetcode 2582 | codestorywithMIK
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
Bhaiya meyne kar liya hai 100% beat in java, thoda time laga but kar liya 😊
i was not able to solve, even this easy Q's/
thanks MIK
Bhaiya can you provide solution to today's leetcode-contest hard:
"Find all the subarrays with their Bitwise And equals to K".
Thanks a lot for 2nd approach. Makes sense now
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
Thanks a lot bhaiya ❤❤
Thanks a lot
thanks man for taking out time during weekends also
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...
approach-2 very well explained.
Thanks itna detail me batane k lie guruji 😬
sir please post biweekly contest solutions as well...
done sir
❤❤
sir online assessment ke liye playlist plz
please solve this --> Leetcode 394. Decode String
simulation se bana liya maine khud se.
kaand karte rehenge 🤣🤣🤣🤣
one line code -- > " return time/(n-1)%2==0 ?time%(n-1)+1 :n-time%(n-1); "
Wo 😮
class Solution {
public int passThePillow(int n, int time) {
int ans=1;
if(time==n){
for(int i=1;i
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
Roadmap video already available on the channel!
@@user-ym1nv1pw8i playlist?
@@user-ym1nv1pw8i playlist?
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));
}
};❤❤
bas yahi samjhne aya hu mai 😅
first comment
second comment 😅
Fastest comment award goes to....
@@gui-codesagye ap yhn bhi😂
@@user-ym1nv1pw8ilakshay
Lagta h galti kardi pehla comment karke😅
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;
}
};
|| 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;
}
}
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;
}
}
🎉❤
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;
}
}