- 6 days ago
Transcript
00:00So in the last question we have learned how to do push in big of n. This time we will
00:04solve this question.
00:06We will solve freshness within the question and solve this question.
00:11We will keep this problem as a homework problem which you can do at home.
00:16In the last question we will be similar to the code.
00:19If we talk about implementation, we will have two cues here.
00:27We will have two cues here.
00:31We will implement a stack.
00:35If we have a normal stack, we will have one, two, three, three elements.
00:40We will push one, two, three, three.
00:43If we go back, three, two, one, two, one.
00:53We will leave this question to the question mark.
00:58The question mark has identified when it comes to the question mark?
01:01However, the question marks us and how we have to push in the question mark.
01:05So we will click on the question mark.
01:16After this question mark, we can Nurman below THOMPSHGvist.
01:23Q के अंदर यानि उस Q के अंदर हमेशा push करना है जिसके अंदर पहले से
01:26elements है जो non empty है non empty Q को पहचानना बहुत ज़रूरी है सिर्फ is empty function
01:32चलाना है constant time लगेगा तो हमारा जो add function होगा stack का push हो गया
01:39और Q में add हो गया तो जो add function होगा वो हमेशा big of one में होगा
01:43क्योंकि हम सिर्फ यह देख रहे है क्योंकि anonymity सीधा उसमें add कर दो
01:47तो यह बन गया हमारा पूरा push function यह बन गया हमारा पूरा push function
01:53अब pop function की बात करें तो pop function थोड़ा सा लंबा होने वाला है
01:58pop function में क्या करेंगे सारे के सारे elements को एक Q से दूसरी Q में shift कर देंगे
02:04जब तक हमारे पास आखरी element नहीं आ जाता तो Q1 Q2 के अंदर हमने elements को डाल रखा है
02:10अब pop करने के लिए हम सबसे पहले Q से elements को remove करेंगे Q1 से remove करेंगे Q2 में
02:15add करेंगे
02:16Q1 से remove हमेशा होता है front से तो front से remove होगा तो 1 यहां से हट जाएगा
02:21और 1 Q2 के अंदर add हो जाएगा यहां पर उसके बाद 2 को remove करेंगे
02:262 यहां से add जाएगा और 2 add हो जाएगा Q2 में उसके बाद 3 को remove करेंगे
02:313 यहां से remove हो जाएगा और 3 जो है वो एक तरीके से हमारा top है यानि last element
02:37है
02:37Now, just last element, we will add to Q2 and return to Q2, we will return to Q2 and return
02:43to Q2.
02:45The next question is, if Q2 and Q1 will transfer to Q1? Why?
02:51Because here is the front element, the one will return to Q2.
02:57The two will return to Q2.
02:59This will be copy.
03:07The two will return to Q2.
03:11The two will return to Q2.
03:12This will return to Q2 and return to Q2.
03:13In this case, we will return to Q2.
03:14One will return to Q2.
03:14first split will pass less than Q2.
03:15Let's see, in this case, the two elements will return to Q2.
03:30to move to the place.
03:312 from here.
03:322 is the last element.
03:36Top.
03:37Now, Q1 is the best.
03:42What will be?
03:44Q2 is the most common and Q1 is the best.
03:47Q1 is the front,
03:49the top will be 1.
03:54If we see the order,
03:55then 3 is the 2,
03:57then 1 is the 1.
03:58If this is simple Q,
03:59then 1 is the first one,
04:01then 2 is the second one,
04:01then 3 is the second one.
04:023, 2, 1 is the second one.
04:05If this is a stack,
04:07then 1 is the first class.
04:14Static class,
04:15stack,
04:16stack,
04:17and internally 2 Qs.
04:22Q of integer,
04:24Q1,
04:25this is the Static type.
04:26This is the new link list.
04:32which will be the new link list.
04:34We can run it with Java collection framework.
04:36We can run it with scratch.
04:38We can run it with scratch.
04:39We can run it with Q1.
04:40we can run it with empty.
04:43So,
04:45public static boolean is empty.
04:51Here,
04:52return
04:53Q1.is empty
04:55and
04:57Q2.is empty.
04:59Now,
05:01we can run it with 2 stacks.
05:03We can run it with S1.
05:04We can run it with Q1 and Q2.
05:06We can run it with 2 blocks.
05:07Because we cannot run it with permanently.
05:09We have to run it with the elements.
05:12And we have to run it with Q1 and Q2.
05:15So,
05:16we need to check the two elements.
05:18is empty हमारे पास हो गया अब यहां पर अपना add function जिसको पुश function नाम देंगे public static void
05:27push
05:28push के अंदर कोई data हमें push करना होगा अपने stack के अंदर तो data को push करने का process
05:34क्या रहेगा process यह रहेगा कि जो भी
05:36Q non empty है उसी में आप add कर दो if not of q1.is empty तो उस case में
05:44q1 के अंदर add कर देंगे अपने data को नहीं तो q2 के अंदर
05:52add कर देंगे अपने data को अब implement करते हैं अपना pop function तो public static int pop
06:03check कर लेंगे अगर empty है हमारा stack तो उस case में एक तो print करवा लेते हैं empty stack
06:12और साथ के साथ return कर देते हैं minus one अब एक top variable ले ले लेते हैं जिसको minus
06:18one से initialize करेंगे
06:22pop करने का क्या process था? pop करने के लिए हम elements तब तक निकालते जाएंगे q1 से जब तक
06:27हम last element तक नहीं पहुंच जाते
06:29और उन सारे elements को हम q2 में डाल रहे होंगे तो while हमारा q1 जो है
06:37वो empty नहीं हो जाता
06:41सबसे पहले if condition चेक कर लेते हैं if हमारा q1 जो है वो non empty है यानि q1 के
06:47अंदर अगर elements stored हैं तो एक case चल रहा होगा
06:50अगर q2 के अंदर stored हैं तो दूसरा case चल रहा होगा तो यह if है हमारा case 1 इसके
06:56अलावा एक else भी हो सकता है यानि q2 के अंदर सारे elements हैं वो बन जाएगा हमारा case 2
07:03case 1 की पहले बात करेंगे जब तक हमारे q1 के अंदर elements है q1 जब तक empty नहीं हो
07:09जाता तब तक एक अपने top के अंदर q1 से elements को निकालेंगे remove करेंगे
07:20और जैसे ही हमारा q1 जो है वो empty हो जाए इस loop से कर जाएंगे हम break
07:28अगर q1 empty हो जाएगा तो top के अंदर हमारा last element stored है q1 का और break करते ही
07:35हम उस last element को यहां से कर देंगे return
07:37यानि यहां से top return हो जाएगा लेकिन अगर last element नहीं है हमारे पास तो उस case में क्या
07:42करेंगे q2 के अंदर add कर देंगे अपने top को
07:47same चीज यहां पर करेंगे case 2 में और इसमें सारी conditions उल्टी हो जाएगी पहले q1 जब तक empty
07:54नहीं होता अब q2 जब तक empty नहीं होता
07:56q2 जब तक empty नहीं होता तब तक top बन जाएगा हमारा q2.remove और जैसे ही q2 हमारा empty
08:06हो जाए
08:09वैसे हम यहां से कर जाएंगे break नहीं तो q1 में add कर देंगे अपने top को
08:17तो यह वो case है जिसमें q1 के अंदर सारे elements है यह वो case है जिसमें q2 के अंदर
08:22सारे elements है यह दोनों case एक दूसरे के
08:24symmetrical हैं बिल्कुल similar conditions हमने लिखी हुई है last में हमने top को return करवा दिया है अब इसी
08:31सेम तरीके से हम implement करते हैं
08:33public static int peak को in fact इन ही conditions को copy कर लेते हैं paste यहाँ पर बिस एक
08:50चीज़ हमें नहीं करनी
08:51इस if condition को यहां से हटा देते हैं और इस if condition को यहां से हटा देते हैं यानि
08:58इनको निकालते जाएंगे और add करते जाएंगे end में हमारे top में हमें पता ही है क्या value stored रहेगी
09:08तो इसको सेव कर लेते हैं अपने main function के अंदर एक stack बनाते हैं s नाम से new
09:15stack
09:18s के अंदर push करते हैं पहले one को फिर s.push2
09:27s.push3 और अब एक लूप चलाते हैं while s.is empty
09:37s से peak करते रहेंगे elements को और फिर s से कर देंगे pop इसको सेव करके run करते हैं
09:49run करने के बाद हमारे पास जो output आया that is three two one हमने insert किया था पहले one
09:55फिर किया two फिर किया three अब यह last and first out fashion में वापस आए last में three गया
10:01तो तो पहले three आया
10:02उससे पहले two गया था तो second पे two आया और सब उससे पहले one गया था तो last में
10:07one आया
10:08तो last and first out हो गया और इस तरीके से दो क्यूज की help से हम एक stack को
10:12implement करते हैं
10:13यहां time complexity की बात करें तो हमारे जो push function है उसमें constant time लगा big of one लेकिन
10:19pop function में
10:20हमने loop चलाएं while loop जिसके वज़े से हमें linear time लगा यानि big of one और same for peak
10:26peak के अंदर भी
10:27हमने loop चलाएं तो हमें यहां पर भी big of one time लगा तो इस तरीके से दो क्यूज की
10:32help से हम stack को implement तो कर सकते हैं
10:34पर वो थोड़ा सा time complexity wise इतना अच्छा decision नहीं होता तो interview और पूछे तभी दो क्यूज यूज
10:40करनी है खुद से scratch से दो क्यूज यूज नहीं करनी है