- 2 weeks ago
Transcript
00:00Now, let's talk about the fourth question.
00:01This is a very important question,
00:03which comes to placements, internships,
00:06and if you give online contests,
00:08then there will be some variation in it.
00:12This question is based on stocks.
00:14If you don't know stocks or shares,
00:16then the company sets its value.
00:18That the one share is equal to this one.
00:21With that, every day the value changes.
00:24We can make a graph.
00:26Today the share was 2 rupees,
00:28today it was 1 rupees.
00:30This is 3 rupees.
00:31This is a graph.
00:33If you want to change this problem,
00:36then you will make a candle.
00:37Or you can give another variation,
00:39then there are many variations,
00:41many possible solutions.
00:43The classic name of this problem
00:45is called stock span problem.
00:48There are two major important words.
00:51One is stock and one is span.
00:53Stock is important because
00:54we always give an example
00:56of stock.
00:56Day one,
00:58stock price was 100.
01:01Day two, stock price was 80.
01:03Day three, stock price was 60.
01:064, 75,
01:0960, 85,
01:1080,
01:107, 100.
01:13So in this way,
01:14we have 7 days value
01:16to our stocks.
01:19Span,
01:20stock given,
01:21span is the official definition.
01:25Maximum number of consecutive days
01:27for which price is less than equal to today's price.
01:52Today's price is less than equal to current year.
01:58Today's price is less than equal to current year.
02:00Concurrently.
02:01If you have 80 example,
02:03by default,
02:04first day span
02:05is always 1.
02:06Why is that?
02:07Because 100 before
02:08there is no one.
02:09So, 100 is less than equal to.
02:11So,
02:12how many elements are
02:13which are less than equal to?
02:15Including 100?
02:16It's 1.
02:16So,
02:17span of 0th day,
02:19index is 0.
02:20So,
02:20first day is always 1.
02:22Now,
02:22span of 2nd day,
02:24or 80,
02:25the span of 0th day,
02:25that's not 1.
02:29it's less than equal days.
02:30It's less than 1 times.
02:32And,
02:32if you get to see how much of it?
02:34It's based on the span of 80,
02:36so that's 1.
02:37The span of 1.
02:37We have a way to see that,
02:39if we let the current day of our stock price,
02:42would be 1.
02:43the first stock price was the first bit.
02:46So,
02:46when we let the current day of this,
02:49span.
02:50If we talk about 60, the first one is 80.
02:55Here, the difference between two two will be 1.
02:5870, the first one will be 70.
03:01The difference between 70 and 70 will be 2.
03:03The difference between two will be 2.
03:07The difference between 60 and 70 will be 1.
03:10The difference will be 1.
03:13If we talk about 85, the difference will be 100.
03:18The difference between two is equal to 5.
03:22The difference will be 1, 2, 3, 4 and 5.
03:27If we talk about the difference between two and two,
03:30the difference will be 5.
03:34Here, the span will be 5.
03:38We talk about 7th day.
03:39We talk about 100 and equal to 100.
03:46The span will be 7.
03:51Here, the span will be 7.
03:54The span will be 7.
04:08The span will be 1.
04:09Let's see how the span will be 118.
04:11The span will be 1.
04:14But if we take the span of 80,
04:15we take the span of 90.
04:18Because the span will be 1.
04:26If we take the span of 80,
04:27i will be 0.
04:30yani zero to span kitna ho jayega 80 ke liye 1
04:34ab agar is 60 ka example ne to 60 ka index hai 2
04:383 minus 1
04:39or previous high kya ho jayega 60 ke liye previous high hai 80
04:44to 80 ka index hai humare paas 1
04:46output aega humare paas 1
04:48abis 70 ka example le to 70 ke liye index hai 3
04:52or iska previous high kya hai 70 se pehle wale high ka index
04:56yani 80 ka index 60 ka index nai
04:5880 ka index hai 1
05:00to 1 ko minus karengi answer aega 2
05:03to humare paas span aaga 70 ke liye 2
05:05isi tarhe randomly 85 ke liye nikala
05:08to 85 ka index hai 5 minus
05:12isse previous high ka kya index hai
05:15iska previous high hai hundred
05:16previous high ka index hai zero
05:18to iske liye span humare paas aaga 5
05:20to eek kaafi easy solution hai
05:22jis me span ke liye hame formula doon lete hai
05:24span is equal to i minus previous high
05:27liekin chhoati si problem patay gya hoti hai
05:30is previous high ko calculate karne me
05:39hame hame baas time laga dhe hai maan lije abe eek loop
06:30use karengi apne stack ka pehle eek bar eek approach
06:31one of zero ko to one hume shalik dhe
06:33or iske anndar yani stack ke anndar store kira
06:36dha dhenge zero ko apne is index ko
06:38ab days number ki bajayi yahaan par
06:41likhte hai index abe ajayenge hame 80
06:44par 80 ke case me kya karengi stack
06:46me dhekhenge kya stack me joh index stored
06:50hai us par 80 se bada element hai abe
06:5280 se bade elements jitne bhi
06:55hoonge stack me un sab ko nikal
06:57dhekhenge yahaan par to sirf a ki index
06:59hai zero zero per kaunsa element hai
07:01hundred to hundred ko to nikalne ki
07:03zarurat nahi hai to isse prove hota
07:05ki stack ke anndar joh bhi index stored
07:06hai wo previous high ka index hai
07:08to 80 ke liye minus kar dhenge one minus
07:11hameara ye wala index yani zero to span
07:13aa jayega one aur 80 ke index yani one ko
07:17apne stack ke anndar dhal dhenge ab second step
07:19par kya karengi 60 par a gayi 60 ke
07:22liye dhekhenge kai se chote element stack
07:23ke anndar stored hai stack ke anndar
07:26stored tha one jahaan par hai 80
07:2760 se chota to nahi hai
07:29toh fir 60 ka span kya hua
07:31iska index minus iske previous high
07:33yani is one ka index
07:35joki hai one to 60 ke liye bhi one
07:37liggh diya aur 60 ke index
07:38woh humne add kar dya
07:40abayega turning point turning point
07:42me humayenge 70 per 70 ke liye
07:45kya karengi 70 ke liye check
07:47karengi ki kya 70 se chota
07:48is stack me koji or hai
07:5070 se chota yaha par tha 60
07:5260 tha 70 se chota
07:54toh isko humne stack se baahar nikal diya
07:57phir dhubara check karengi
07:58kya one
07:59par jo element stored hai
08:01yani 80 70 se chota hai
08:02nahi hai
08:02toh one ban gya
08:04humare 70 ka previous high
08:06toh yaha 70 ka index ta 3
08:08or previous high ka index ta 1
08:11toh answer aa gya 2
08:12aur abh humne yaha par
08:143 ko store kera diya
08:15jahaan par hai 70
08:16abh a gaye hum 60 per
08:1860 per jaysse hi aye
08:20toh check karengi
08:21kya 60 se chota koji element hai
08:2260 se chota toh koji element
08:24nahi hai
08:25kyunki top pe 70 hai
08:26toh 60 ke liye span
08:27aa gya
08:284 minus 3
08:29yani 1
08:31aur yaha store kera
08:32dhenge 4 ko
08:33aur 60 ko
08:33abh dobaras eek
08:34naya turning point
08:35abh hum a gaye hai 5
08:37par yani jahaan 85 stored hai
08:39abh 85 compare karega
08:42in sare ke sare elements ke
08:43saath
08:43ye toh index stored hai elements ke
08:46ye un elements ki value hai
08:48toh 85 in ke saath compare ho
08:50raha ho ga
08:5185 jaysse hi compare ho ga
08:5360 ke saath
08:54woh 60 ko toh bahar nikala
08:55dhe ga ki aap merse chhoťe ho
08:56aap kiasse meri span mein aasakte ho
08:59uske baad 85 compare ho ga 70
09:01ke saath 70 ke saath jaysse compare ho
09:03usko bhi bahar nikala dhe ga
09:05phir 85 compare ho ga 80 ke saath
09:07usko bhi bahar nikala dhe ga
09:08phir 85 compare ho ga
09:11finally humare 100 ke saath
09:12jisse woh chhota nai hai
09:14toh 85 dhire dhire karke
09:16in sare elements ko bahar nikala dhe ga
09:18aur stack humara ho jayega
09:20khali
09:21abh 85 ke liye topmost element kiya bana
09:245 minus uska previous high
09:26ban gaya 0
09:26jaha par 100 stored hai
09:29toh iske liye span ban gaya 5
09:3085 yaha par a gaya
09:32uska index yani 5 yaha par a gaya
09:34abh hum aayenge 100 per
09:35100 bhi in sab ke saath compare
09:37kare ga 100 sabse pehle 85 ke saath compare
09:40kare ga usse bada hai toh usko nikala dhe ga
09:41phir 100 ke saath compare kare ga
09:43usse bada hai toh usko nikala dhe ga
09:44is case mein humara jose stack hai
09:47woh khali ho jayega
09:48yani empty ho jayega
09:49jiasse hi stack empty ho jada hai
09:51hume pata chal gaya
09:52humse pehle koji bhi aisa element nai hai
09:54joh humse bada ho
09:55sab humse ya toh chote hai ya equal hai
09:57is case mein humare span ki value ho jayegi
10:00i plus one ke equal
10:02yani muczse sare chote index
10:04i toh hai plus me bhi hou
10:05isliye i plus one
10:07aur yaha par i plus one hooga
10:08six plus one yani seven
10:09toh including this hundred
10:11including this hundred
10:13humare paas seven aise elements hai
10:15joh hundred se ya toh chote hai
10:17ya usse bade hai
10:18toh is tariqe se hum span calculate
10:20karte hai apne stock span problem
10:22me using a stack
10:24abh iska code hum lickkar dhekte hai
10:26code kaffi important hai is problem ka
10:27code ke liye sabse pehle
10:29apne stocks define kar leet hai
10:30stocks ke liye array bana leet hai
10:33hundred, eighty, sixty, seventy
10:37sixty, eighty, five, hundred
10:39aur ek array bana leet hai
10:41apne spans ke liye
10:43isse stock ki length ka hi array
10:45banayenge aur ek function call
10:47lagate hai stock span naam se
10:49jiske under pass kar dhenge
10:50apne stocks aur saat me apna span
10:53and finally print kar dhenge
10:56apne span ko
11:04abh apna function bana te hai public
11:07static void
11:09stock
11:10span naam se
11:12iske under sabse pehle
11:13haemari paas stocks aaree hoongi
11:14uske baad haemari paas span
11:16aa raha hooga
11:18shiruaat me hi kya karenge
11:19hume pata hai ki hume ek stack ki
11:21help se solve karna hai toh
11:22stack declare kar leet hai
11:23stack of
11:26ints of integers s is equal to new stack
11:29ab zero it index ke liye
11:31yani first day ke liye
11:32humeishah humeishah humeari span ki value one hooti hai
11:48toh har day ke liye ek loop lagate hai for int i equal to one
11:52kiunki zero it day ka zero it index ka toh hume already calculate kar chukye
11:57toh i is equal to one se shuru karenge i jab tuk less than stocks dot length rata hai i
12:04plus plus
12:04span calculate karne ka kya process rata hai
12:07sabse pehle current price lakal leet hai
12:10yani aaj ka kya price hai
12:11toh int current price is equal to stocks of i
12:16abis current price ko compare karenge stack ke baaki prices se
12:21toh eq while loop laga leet hai
12:23ye while loop tab tuk chalega
12:25jab tuk hamarah stack ek toh khali nahi hoota
12:28jab tuk isme elements hai
12:29sath ke sath jab tuk hamarah current price
12:33hamarai baaki ke yani pehle wale prices se bada hai
12:37toh pehle wale prices yani stocks of
12:41s dot peak
12:44yaha s dot peak karke
12:46hume sabse pehle index nikaala
12:48uske baad stocks me dal kar
12:50us index per stored value ko hume access kar liya
12:53us value se jab tuk bada hai
12:56humara current price
12:57tab tuk hume previous high nahin mil raha
12:59previous high yani jomujh se badao
13:01muczse bada kamb mil ega
13:03jab mirse chote wale sare element nikal jayenge
13:06toh muczse chote wale jab tuk sare elements nikalte nahi hai
13:09tab tuk hum elements ko karte rehenge pop
13:13taakhi chote wale ko hum nikalte rhenge
13:15uske baad jaisa hi elements pop ho gai
13:17jaisa hi chote wale sare nikal gai
13:18abh humare top per joh hai
13:20jisko hum peak karenge
13:50woh ho ga humara previous high
13:51s.peak per joh bhi value hai
13:53aur span of i ben jayega
13:56i minus previous high
14:00finally hum s me push kar dienge
14:02apne i ko
14:02aur is tariqe se humara loop khatam ho jayega
14:06isko thodha sa recap karke kode ko
14:08semajte hai
14:08humare paa shuruaat mein one tha
14:10jisko hum nne span of zero bana diya
14:12phir hum nne stack me push kar diya zero ko
14:14abe ek loop laga ya hai
14:15har ek dhin par jaa kar chek kar rhen
14:17aaj ka span kya hai
14:18aaj ka span kya hai
14:18aaj ka span kya hai
14:19har din ka current price hum nne nikal dhiya hai
14:21uske baad stack ko tab tuk khali kertte rhenge
14:24jab tuk ya toh woh completely empty nii ho jata
14:26ya phir jab tuk humara current price greater than
14:30humare stack ka top hai
14:32jab tuk current price stack ke top se greater than rhega
14:36tab tuk kya hooga
14:37humi apna previous high nahin mil pahega
14:39previous high tab hooga
14:40jib stack ka top mugh se bhi bada ho jaye
14:42jiasse hi woh point aayega
14:43wiesse hi waha par hum previous high nikal lenge
14:46aur value nikal lenge
14:47nahi toh pop karte rhenge
14:48aur sath ke sath end me hum push kar dienge
14:51apne index ko stack ke under
14:53toh is tariqe se stock span ko
14:55hum nne likh liya completely
14:56end me hum sare ke sare spans ko print karwa dhe
15:00inge isko save kar lietay aur run karte
15:02ha ha ha ha run karke
15:05humare paas output aya hai
15:07one one one two one five and six
15:10aur yye wahi same output hai
15:11jisko abhi abne observe kiya
15:13aapne example ke anndar
15:14toh isi tariqe se stock span problem
15:16ko hum solve karte hai
15:17aapnew