• 6 days ago
Transcript
00:00Now we have a question that is very beautiful.
00:02Microsoft has asked me to ask, why is it using two stacks?
00:07Now we have two stacks given and two stacks to help us
00:10why do we have to simulate this?
00:12We have linked list, array, array,
00:15and why do we have two stacks?
00:19What are the stacks?
00:20This is an easy question.
00:22It is a last-in, first-out data structure.
00:25But with two last-in, first-out data structures,
00:29we will create a first-in, first-out data structure.
00:32We will create two two ways.
00:35The first one is the push operation,
00:37the add operation.
00:39We will create a remove and pop.
00:43The second one is the pop operation.
00:49The remove operation is the big of n.
00:52This is big of n, then our peak will be big of n.
00:56And our add operation is the big of 1.
01:00So we can implement two stacks of 2 stacks.
01:04Obviously, it will be optimized.
01:07But it will be interesting.
01:09Now, here we will be the approach of following this.
01:12We will have this way.
01:13We will try to do this.
01:14We will try to do all the add operations or add operations.
01:16Then we will try to do the 2 of n.
01:17We will try to do all that.
01:20This is the homework problem.
01:22So we can implement this way.
01:25The problem here is this question.
01:25Now, let's do this.
01:26First of all, here we will make two stacks of 4 stacks.
01:29The first stack is S1 and the second stack is S2.
01:33S2
01:34S1, S2 help us how to make a Q
01:38A Q structure
01:39We will take a example
01:41We will take a name
01:41Q
01:43We will take a 1, 2, 3 elements
01:45We will add
01:461 & 2
01:48We will pop
01:49So we will add
01:51We will remove
01:54If we normally add
01:56Then
01:571
01:582
01:583
01:582
02:00Remove operations
02:01Perform
02:01Output
02:031
02:052
02:062
02:071
02:102
02:101
02:112
02:121
02:132
02:141
02:192
02:202
02:212
02:212
02:212
02:211
02:222
02:222
02:221
02:232
02:231
02:232
02:252
02:261
02:271
02:272
02:281
02:312
02:321
02:322
02:322
02:352
02:362
02:472
02:492
02:492
02:502
02:502
02:532
02:532
02:532
02:532
03:101
03:111
03:162
03:171
03:182
03:181
03:182
03:191
03:192
03:202
03:202
03:202
03:202
03:223
03:222
03:232
03:301
03:321
03:352
03:401
03:411
03:492
03:502
03:511
03:522
03:572
03:582
03:592
04:012
04:012
04:051
04:062
04:082
04:082
04:091
04:122
04:132
04:131
04:182
04:191
04:202
04:222
04:232
04:232
04:232
04:232
04:232
04:233
04:23remove करने के operation की बात करते हैं यानि अगर हमें remove करना होगा तो simply हमें stack s1 से
04:29pop करवाना है
04:30stack s1 से pop करवाएंगे तो हमारे पास सबसे पहले निकलेगा one one यहां से अट गया उसके बाद निकलेगा
04:37two
04:38और ध्यान दे तो one two three यह वही order है जिसमें हमने elements को add करवाए था तो इस
04:44तरीके से हमारा remove में सिर्फ s1 से pop करवाना है
04:47peak में भी सिर्फ s1 से हमें peak करना है लेकिन add में यह three step process हमें follow करना
04:54है जिसकी help से हम पूरक पूरे code को implement कर लेंगे
04:57अब सबसे पहले class बना लेते हैं अपनी static class हमें बनानी है एक queue इस queue के अंदर internally
05:05दो stacks हैं तो stacks को scratch implement नहीं करना है
05:09java collection framework करना है इस stack होगा integer type का इसको नाम देंगे s1 इसे initialize कर लेते हैं
05:17दूसरा static stack बनाएंगे यह भी integer type का इसका नाम होगा s2 और इसको भी हम कर लेते हैं
05:24initialize अब सबसे पहले basic function implement करेंगे is empty वाला तो public static boolean is empty इसमें return कर
05:39देंगे s1.is empty को
05:42तो जब हमारा s1 खाली है यानि हमारी पूरी q खाली है s2 के अंदर क्यूंकि permanently elements को story
05:49नहीं कर रहे तो s2 को देखने की ज़रूत यहां पर नहीं है
05:51add function को implement करते हैं public static void add इसके अंदर कोई data होगा जिसको हमें add करना होगा
05:59अब add करने में सबसे पहला step यह है कि s1 जब तक खाली नहीं होता तब तक s1 के
06:05सारे elements को उठा कर s2 में पटक देना
06:07तो इसके लिए एक while loop लगा लेते हैं while s1.is empty is not true तब तक के लिए
06:14s2 में push कर देते हैं s1 के सारे elements को pop करवा के
06:19I hope यह line clear होगी कि s1 जब तक खाली नहीं होता तब तक s1 के elements को pop
06:25कराएं और s2 में push कर दें यह हो गया first step
06:28दूसरा step होगा s1 में push कर देना अपने data को और तीसरा step होगा वापस से s2 से उठा
06:34कर elements को s1 में डाल देना
06:36यानि जब तक s2.is empty तब तक s1.push s2.pop s2 से pop कर आ रहे हैं और
06:48s1 में push कर वा रहे हैं
06:50इस तरीके से हमारा पूरा add function complete हो गया तीन steps में अब complete करते हैं अपने remove function
06:55को
06:56public static int remove remove करने के लिए हमें simply सबसे पहले चेक कर लेना empty तो नहीं है
07:07अगर empty है तो print करवाते हैं हमारी जो q है that is empty और यहां से कर जाएंगे हम
07:14return
07:15minus 1 के साथ नहीं तो simply हमें return करना है s1.pop s1.pop क्योंकि अब तो हमारा जो
07:24element
07:24front पे आये था यानि सबसे पहले आये था वो तो s1 के top पे तो उसी को pop करा
07:29देंगे तो हमारा
07:30front return भी हो जाएगा और साथ के साथ निकल भी जाएगा
07:34कि सेम वे में पीक को implement करेंगे public static int peak
07:41कि यहां पर similar conditions रख रहे होंगे तो इसी को copy paste कर लेते हैं बस यहां पर pop
07:47को change करके
07:54big of one में constant time में add को implement करने के लिए हमें लगाना पड़ा big of n यानि
08:01linear time
08:02क्योंकि यहां पर while loop चल रहे हैं और दो while loop जब same loop के अंदर एक के बाद
08:06एक चलते हैं तो
08:07उसकी time complexity n plus n हो जाती यानि big of 2n यानि big of n क्योंकि asymptotic notations अब
08:14main function के अंदर
08:15अपनी क्यों बना लेते हैं q is equal to new q यह अंगिलर ब्राकेट्स यहां पर नहीं आ रहे होंगे
08:23क्यों के अंदर
08:24add करते हैं अपने कुछ elements को और देखते हैं कि क्या हमारी क्यों actually implement हुई भी है
08:31add 3 अब जब तक हमारी q से खाली नहीं हो जाती यानि q से elements निकल नहीं जाते हैं
08:41जब तक q
08:41empty नहीं होती तब तक q में peak करके elements को देख लेंगे और q से करा देंगे हम remove
08:49इसको save करके run करें
08:52तो output हमारे पास आना चाहिए 123 और यहां पर output आया है 123 तो सही जरीके से हमने q
08:59को implement किया है
09:00दो stacks की help से time complexity हमें पता चल गई है add की क्या रहती है remove की क्या
09:05रहती है और peak की क्या रहती है