Text Justification Dynamic Programming

  Рет қаралды 143,048

Tushar Roy - Coding Made Simple

Tushar Roy - Coding Made Simple

9 жыл бұрын

Given a sequence of words, and a limit on the number of characters that can be put in one line (line width). Put line breaks in the given sequence such that the lines are printed neatly
github.com/mission-peace/inte...
github.com/mission-peace/inte...

Пікірлер: 136
@RicardoBuquet
@RicardoBuquet 4 жыл бұрын
Dont really understand the explanation from minute 7:20 onwards. :(
@becharam1
@becharam1 9 жыл бұрын
I was traversing so many places to learn dynamic program but at last these video lectures made my job easy and understandable...awesome, thanks a lot
@robinganpat250
@robinganpat250 8 жыл бұрын
Hey, Filling the first 2D array which contains the values of the squared left-over length of a line is N^2. This is for large texts with like 30.000 words a problem. Wouldn't it be possible to save it in N*L? If so, how?
@Iwebguru
@Iwebguru 5 жыл бұрын
How the hell would anyone think of this in an interview?
@PrathamMantri
@PrathamMantri 5 жыл бұрын
Thats what I was thinking while watching the video, then decided to go with greedy algorithm :P
@08himanshu
@08himanshu 5 жыл бұрын
It has a simple solution also. You can solve it like matrix chain multiplication. Problem - 3
@sahibjotsinghbedi292
@sahibjotsinghbedi292 5 жыл бұрын
@@PrathamMantri I did the same
@CodeCampaign
@CodeCampaign 5 жыл бұрын
You can solve this problem in more simple manner, like we do all other partitioning problems. Check the problem on word partitioning and then try to apply the same logic here. This solution is over complicated.
@kipa_chu
@kipa_chu 5 жыл бұрын
thats why not everyone is a google, apple engineer
@srktheking1
@srktheking1 8 жыл бұрын
awesome job tushar!! have to say yours is the best tool for learning dynammic programming I have across simple and efficient at the same time! keep up the good work man! you are helping out so many people like me, thanks for that!
@sejalmandhan5635
@sejalmandhan5635 Жыл бұрын
Can you tell me how he store 5 in result array?
@WeeZy1534
@WeeZy1534 4 жыл бұрын
I LOVE YOU !
@justapolitecommenter351
@justapolitecommenter351 8 жыл бұрын
You rock man. You rock !
@amanpreetsinghbawa1600
@amanpreetsinghbawa1600 8 жыл бұрын
Tushar sir your programming videos have helped me a lot. Just one point to mention is that please give the recursive relation so that we can get completely connected with the solution. Thanks!
@vorasagar7
@vorasagar7 6 жыл бұрын
agree
@diptiprakashsinha
@diptiprakashsinha 3 жыл бұрын
Agreed
@shubham_chime
@shubham_chime 8 жыл бұрын
+Tushar, Thanks a lot for this video. I understood the solution but I did not get how you came up with thought of using dynamic programming in first place. What was your thought process ?
@TechCluesByAbsek
@TechCluesByAbsek 2 жыл бұрын
This is the only satisfactory video in KZbin till now thanks a lot for that
@varuntaneja7073
@varuntaneja7073 2 жыл бұрын
I wasn't able to wrap this solution around my head . Thanks for the explanation!
@sejalmandhan5635
@sejalmandhan5635 Жыл бұрын
Can you tell me how he store 5 in result array?
@maxwellward7533
@maxwellward7533 3 жыл бұрын
I didn't fully understand at first, but after re-watching and writing out the algorithm along with the video, it all makes sense now!
@joeystechpluscode
@joeystechpluscode 3 жыл бұрын
Hey Maxwell, This problem has been explained clearly in my channel and students are really complementing the explanation. Just check it out kzbin.info/www/bejne/eX6vpJ2rZq6DhKs
@ashishmadaan9488
@ashishmadaan9488 6 жыл бұрын
very great explanation of the question. Thank you a lot.
@user-vm5pz5gz9c
@user-vm5pz5gz9c 5 жыл бұрын
Thank you, Tushar! This is awesome
@MOHDSALMAN-sj2zu
@MOHDSALMAN-sj2zu 8 жыл бұрын
U explained it in very good manner. Thanks a lot.
@deepsaxena2768
@deepsaxena2768 6 жыл бұрын
Please try to give logical reasoning for taking any decision, like you started from left or can we club both the matrix into one but sol will become O n4 like that, will be really helpful
@anandgupta8529
@anandgupta8529 3 жыл бұрын
Acc to Tushar-->Dynamic programing ==filling the table. No intutuion ,no logic, just fill the table
@swethav5122
@swethav5122 6 жыл бұрын
Thank you so much, Tushar. Your videos are really helpful. I understood the procedure you have explained in this video. Can you tell me where exactly you have an explanation for the code for the Text Justification Dynamic Programming Problem?
@sandeepsr5300
@sandeepsr5300 8 жыл бұрын
Thanks a lot. As an extension of this i have one question. Given width and number of rows, how many times you can fit in that sentence? Next sentence can start after a space from last word. It will be helpful if you share the technique. Once again thank you.
@sandipbeed
@sandipbeed 9 жыл бұрын
at 7:56 .. can anyone please explain why 5 is assinged at last index in second array ??starting from 4 till 5 .. i dont get it where is 5th index
@depinderpreetsingh3554
@depinderpreetsingh3554 8 жыл бұрын
+Sandip M. Waghmare : Actually it just means that if there is 5th indexed word then it would be in next line. You can easily understand it's concept by looking at other indexes as shown in the video. Hope that helps a bit.
@dragnet232
@dragnet232 8 жыл бұрын
Great work, thx for your help.
@JamesBond-jp2ep
@JamesBond-jp2ep 7 жыл бұрын
Hello, thanks for the nice video. I have the task to output a .txt file in c++ compiler with a justified width of 25 inputted by the user. I have to use ofstream and ifstream to read character by character and output each line so that the maximum number of character in a line is not more than 25. (not more than 25 characters) I need to solve this task urgently... :-( Could you please help me out?
@tavvayaswanth
@tavvayaswanth 4 жыл бұрын
Thank you Tushar, your videos are really helpful :)
@madhuramjajoo8967
@madhuramjajoo8967 6 жыл бұрын
Tushar Sir there is a O(nlogn) method for longest increasing susequence . Can this question be done in O(nlogn) also?
@rogerbosman2126
@rogerbosman2126 6 жыл бұрын
Thanks for the explanation, but when looking at the code I don't get it. In the video, you start doing the "where to break" analysis once you hit infinity between i and j. However, in the code, you just 'continue;'. How does this work? Where does this analysis take place in the code? Thanks!
@ararbi90
@ararbi90 6 жыл бұрын
Can you please write the actual recursion!!!
@angadrajsingh4311
@angadrajsingh4311 2 жыл бұрын
Amazing explanation for this question on youtube so far. Thankyou..
@depinderpreetsingh3554
@depinderpreetsingh3554 8 жыл бұрын
Thanks for such a nice video :)
@s1337m
@s1337m 8 жыл бұрын
why do you square the number of white spaces?
7 жыл бұрын
So that the big individual spaces get more value than several smaller spaces. The same is done in TeX (en.wikipedia.org/wiki/Line_wrap_and_word_wrap#Minimum_raggedness).
@ahmxtb
@ahmxtb 7 жыл бұрын
Yes, TeX actually uses (width - used)^3. The ^3 is to amplify discouraging unused spaces.
@cvryn7
@cvryn7 9 жыл бұрын
This was little complicated!!! but finally understood :P Thanks!
@geniusmridul007
@geniusmridul007 9 жыл бұрын
In you video you missed out the traversal from 2 to 3 (excluding 3). I get that we have used the word at index 2 but is there a way to fix that such that we don't get this kind of repetition?
@dk109k2dask9
@dk109k2dask9 7 жыл бұрын
Great video Tushar!
@nileshagrawal9520
@nileshagrawal9520 8 жыл бұрын
Excellent Explanation of the most of the problems. Helped a lot in understanding dynamic programming. Can you recommend any good book for reading and subtle explanation of dynamic programming problems
@RiRiDingetjes
@RiRiDingetjes 7 жыл бұрын
You could do it in O(nm) where m is the max length of one line. Make a matrix sized [n][m/2]. Now every [i][j] stands for the words i-j to i.
@SachinSharma-vd5fr
@SachinSharma-vd5fr 8 жыл бұрын
Thank you Tushar, nice explanation... :)
@rajeshkumarjha007raj
@rajeshkumarjha007raj 5 жыл бұрын
Yes, we'll use Dynamic Programming.
@AbhijeetAelkatwad
@AbhijeetAelkatwad 4 жыл бұрын
:D :D
@yingyinghu4906
@yingyinghu4906 3 жыл бұрын
lamo
@sudhirghandikota3360
@sudhirghandikota3360 8 жыл бұрын
Thanks tushar...good job!!!
@asifbilla4924
@asifbilla4924 7 жыл бұрын
Nice explanation.
@mo3osman
@mo3osman 4 жыл бұрын
at 11:05 it should be 49 + 9 (best you can do from j = 2 till end), or am I missing something?
@sejalmandhan5635
@sejalmandhan5635 Жыл бұрын
Can you tell me how he store 5 in result array?
@ektabhalwara9542
@ektabhalwara9542 8 жыл бұрын
ohk tushar , i found box stacking problem too. thanks ur vedios help a ton.and u look good in specs
@alia2122
@alia2122 8 жыл бұрын
Tushar, how would we solve if we got question saying, do not consider last row penalisation, that is wheneve is in last row at as long as it fits size lets sayy 10, it wont get penalised.
@widmanm
@widmanm 6 жыл бұрын
how do we implement this?
@prashantsahu2959
@prashantsahu2959 3 жыл бұрын
Why are we using sum of squares(spaces) for cost determination?
@kevinnicaise4379
@kevinnicaise4379 8 жыл бұрын
Thank you ! :)
@deepsaxena2768
@deepsaxena2768 6 жыл бұрын
How the recursion tree looks like?
@betamoo
@betamoo 7 жыл бұрын
I think we can get the solution using 1d arrays instead of 2d array. The 1d array F[i] will still represent the cost of fitting all prev words including i We will use another 1D array Acc[i] to store the accumulated sum of word lengths up till i Acc[i] = sum(F[i] ... F[0]) which will help get the sum of any interval in constant time. Now the recursive formula will be F[i]= minimum of previous F[j-1] + the error introduced of putting words j..i in a new line F[i] = min( F[j-1] + (width - (Acc[i]-Acc[j-1]) + (i-j))^2) for j= i to 0 or until (Acc[i]-Acc[j-1]) + (i-j))^2 becomes larger then width
@davidoh6342
@davidoh6342 4 жыл бұрын
I see many people explaining this question with minimizing badness. But how is it any better than just iterating through the list of words, and reinitializing the every time I find the last word based on word length count and separating sentences?
@arjunkhanna1803
@arjunkhanna1803 4 жыл бұрын
3:30 how can you tell that arrangement 2 is better then 1 only because the sqaure of empty spaces is greate? and why we should count the square, because after all number of empty spaces matter not square
@joeystechpluscode
@joeystechpluscode 3 жыл бұрын
This problem has been explained clearly in my channel and students are really complementing the explanation. Just check it out kzbin.info/www/bejne/eX6vpJ2rZq6DhKs
@AbhijeetSachdev
@AbhijeetSachdev 9 жыл бұрын
Thanks a ton Tushar :)))))
@MaheshGaonkar22
@MaheshGaonkar22 9 жыл бұрын
How J2 = 49 + 34 ? why not J2 = 49 + 9 ? I didn't get at 10:57 .. can anyone explain it?
@KirubaEbenezer
@KirubaEbenezer 8 жыл бұрын
Thanks Bro.
@HAAH999
@HAAH999 4 жыл бұрын
Super work, but you could have used the same cost matrix since its simpler. You set infinity immediately while you could have chosen to check the minimum of infinity and all other possibilities. T[i][j] = min ( "infinity" if total length > width otherwise "computed cost", min(T[i][k]+T[k+1][j])) I tried it and got the same result. We can set the start of new lines by just adding the index in the matrix as you did in previous videos to find the arrangement.
@yernartalgatuly4252
@yernartalgatuly4252 8 жыл бұрын
In 11:09. Why did you write 49+34? The best of 2 to the end of line is 9. Or I didn't understand
@deltechdiaries5907
@deltechdiaries5907 2 жыл бұрын
how will we solve covid crisis? Roy : Yes, we will use dynamic programming for this
@user-no5oi3eh8v
@user-no5oi3eh8v 8 жыл бұрын
Спасибо за видео
@dawnoin
@dawnoin 8 жыл бұрын
It would help if you could also prove correctness of the algorithm. And, why should one choose a particular algorithm over another, inorder to solve a given problem.
@CyberMew
@CyberMew 2 жыл бұрын
What is that 5x5 matrix about? What’s the value of x and y axis? I don’t get it, it’s not explained.
@vrn11
@vrn11 5 жыл бұрын
Why square? What is the justification?
@tttuberc
@tttuberc 3 жыл бұрын
How is this method called "dynamic programming"? thanks
@joeystechpluscode
@joeystechpluscode 3 жыл бұрын
It starts from the lowest subproblem and moves to the highest problem, that's why. This problem has been explained beautifully in my channel and students are really complementing the explanation. Just check it out kzbin.info/www/bejne/eX6vpJ2rZq6DhKs
@ektabhalwara9542
@ektabhalwara9542 8 жыл бұрын
Hey Tushar please upload vedio for Box Stacking problem too. please
@syedabdullahzobair4490
@syedabdullahzobair4490 8 жыл бұрын
kindly Explain RNA secondary Structure as well please !!
@kumarshubhamshivhare
@kumarshubhamshivhare 9 жыл бұрын
Good job
@komalanand3507
@komalanand3507 3 жыл бұрын
can you please share git repo link again
@shashankbajpai5659
@shashankbajpai5659 3 жыл бұрын
but what if one line can store more than 2 words?
@nileshmare9662
@nileshmare9662 3 жыл бұрын
You have given a very efficient solution :bestofluck
@bashaieralyousefi4038
@bashaieralyousefi4038 9 жыл бұрын
that is a great video , helped me a lot ! thank u
@Official-tk3nc
@Official-tk3nc 3 жыл бұрын
ola hu uber
@ShivangiSingh-wc3gk
@ShivangiSingh-wc3gk 2 жыл бұрын
Ugh I dont even feel like wrapping my head around this.
@elissneia6027
@elissneia6027 3 жыл бұрын
could anyone explain why 0-3 is INF? 0 Tushar 3 to 6+3+1 = 10 isn't supposed it be 0 ?
@shriram5444
@shriram5444 2 жыл бұрын
For those trying to use recurrence relation mentioned at the end to solve the problem, m[n] or m[len] or m[5](in this particular example) is 0
@The_Promised_Neverland...
@The_Promised_Neverland... 2 жыл бұрын
Here I come again after 9 months for this same question, History connects us lol
@Lassar25
@Lassar25 5 жыл бұрын
For 4 to 4 the penalty should be zero, because it's the last line. If you don't believe me check out wikipedia on line wrap, and word wrap.
@hulk94smash
@hulk94smash 4 жыл бұрын
couldn't understand it clearly :(
@Ankitbomb1
@Ankitbomb1 5 жыл бұрын
NO explanation at all on why we are doing what we are doing. Really disappointed.
@user-vr9ok5uw1x
@user-vr9ok5uw1x 4 жыл бұрын
True, It is diappointing but it is one of the best explanation we have so far. Tushar, It would be much better if transition between the statements would be smoother. It saves a lot time on preparation as you cannot remember everything but the basic concepts.
@augiegardner76
@augiegardner76 6 жыл бұрын
You have some great videos but this one didn't do a great job of explaining the reason behind the solution as much as it explained the methodology behind arriving at said solution. The matrix and the two arrays could have been better elaborated upon
@kvsrkrishnan
@kvsrkrishnan 6 жыл бұрын
kzbin.info/www/bejne/e3_coKttoLN-m7s
@deathbombs
@deathbombs 2 жыл бұрын
I don't like how you jumped right into approaches before explaining the problem fully, like what's a space? (We learn afterward that it's whatever is left over by words)
@vishalmishra9549
@vishalmishra9549 4 жыл бұрын
salute
@DarkLordAli95
@DarkLordAli95 7 жыл бұрын
6:21 "10 - 2 is 4" you heard it here boys.
@hemantsethi8467
@hemantsethi8467 4 жыл бұрын
Stop nitpicking. Making videos ain't a piece of cake. It was 10 - 8 = 2 extra spaces. Cost = 2^2 = 4.
@nileshmare9662
@nileshmare9662 3 жыл бұрын
Sir, your code for this question is wrong..try using following case. "aaa","12345","1234567","1234","123","1234", "1", "12345","12345678","12345". take width as 12.
@mohammadkarami8984
@mohammadkarami8984 4 жыл бұрын
Thanks for the note. but it was not clear
@joeystechpluscode
@joeystechpluscode 3 жыл бұрын
Hi, no worries - this problem has been explained clearly in my channel and students are really complementing the explanation. Just check it out kzbin.info/www/bejne/eX6vpJ2rZq6DhKs
@AbhijeetNayak-connect
@AbhijeetNayak-connect 6 жыл бұрын
No body should be asking these types of questions during a Interview and expect this answer. You won't be able to judge a person by this question.
@karthikkottugumada8930
@karthikkottugumada8930 4 жыл бұрын
For anyone interested in why Tushar went for square: kzbin.info/www/bejne/e3_coKttoLN-m7s
@lakhveerkaur2886
@lakhveerkaur2886 4 жыл бұрын
At 6:17, Am I the only one who laughed when he corrected himself, and be like 10 - 2 is 4...oops no 10 - 2 is 2 and 2 squared is 4. watching at 1.5x makes it more funny! ;)
@adhishmalviya2408
@adhishmalviya2408 3 жыл бұрын
hahaha...I was going to write a similar comment
@markgraham2312
@markgraham2312 3 жыл бұрын
Here's the coded solution: long solveBalancedLineBreaks(std::vector words, int limit) { std::vector cost(words.size(), std::vector(words.size(), std::numeric_limits::max())); for (size_t i = 0; i < words.size(); i++) { int sum = 0; for (size_t j = i; j < words.size(); j++) { // calculate: sum{i, j} |word]i]| sum += words[j].size(); if (j != i && sum limit) break; else { int value = (limit - sum); cost[i][j] = value * value; } } } std::vector mincost(words.size(), std::numeric_limits::max()); int i = words.size() - 1, j; while (i >= 0) { j = words.size() - 1; while (j >= i) { if (cost[i][j] < std::numeric_limits::max()) { int subcost = cost[i][j]; if (j + 1
@johannstraburg194
@johannstraburg194 3 жыл бұрын
Pet peeve: Wrong pronunciation of "infinite".
@anandchowdhury9262
@anandchowdhury9262 3 жыл бұрын
For people starting out in DP, please dont follow tushar's tutorials. His videos jumps straight into a well known solution. HIS VIDEOS DONT TEACH YOU HOW TO THINK. For dp it is very imp to come up with a recurance relation and identity the optimal substructure and overlapping subproblems. That is the base of your solution. Once you reach that, you can convert your soln to a top up approach. Watch the MIT lectures instead.
@markgraham2312
@markgraham2312 3 жыл бұрын
Your explanation of the dp is terrible. Very poorly explained. Your explanation of how to calculate the 2d array is clear, but I have no idea what you are doing to calculate the 2 1d arrays. Okay, I understand. The results array is the index of the last word + 1!
@rohitjoshi6335
@rohitjoshi6335 4 жыл бұрын
lame
@harshitsaluja3493
@harshitsaluja3493 2 жыл бұрын
Not at all helpful you are just telling how to solve not telling why we go ahead with Dp with this solution
@gilbertwiosna7434
@gilbertwiosna7434 3 жыл бұрын
his language is poor and sad
@geniusmridul007
@geniusmridul007 9 жыл бұрын
In you video you missed out the traversal from 2 to 3 (excluding 3). I get that we have used the word at index 2 but is there a way to fix that such that we don't get this kind of repetition?
@XAngelsLifeX
@XAngelsLifeX 8 жыл бұрын
Спасибо за видео
Text Justification Algorithm (LeetCode)
30:45
AlgosWithMichael
Рет қаралды 31 М.
Burst Balloon Dynamic Programming[Leetcode]
27:22
Tushar Roy - Coding Made Simple
Рет қаралды 102 М.
MISS CIRCLE STUDENTS BULLY ME!
00:12
Andreas Eskander
Рет қаралды 8 МЛН
Stay on your way 🛤️✨
00:34
A4
Рет қаралды 4,3 МЛН
Regular Expression Dynamic Programming
18:34
Tushar Roy - Coding Made Simple
Рет қаралды 253 М.
Maximum Sum Rectangular Submatrix in Matrix dynamic programming/2D kadane
13:54
Tushar Roy - Coding Made Simple
Рет қаралды 202 М.
Maximum Size Rectangle of All 1's Dynamic Programming
6:55
Tushar Roy - Coding Made Simple
Рет қаралды 180 М.
Worst leetcode hard problem - Text Justification : 68
13:06
Destination FAANG
Рет қаралды 1,2 М.
Palindrome Partition Dynamic Programming
11:46
Tushar Roy - Coding Made Simple
Рет қаралды 104 М.
Sum Query in 2D Immutable Array Dynamic Programming
18:34
Tushar Roy - Coding Made Simple
Рет қаралды 50 М.
Minimum Cost Path Dynamic Programming
4:38
Tushar Roy - Coding Made Simple
Рет қаралды 132 М.
Binary Search : Median of two sorted arrays of different sizes.
24:48
Tushar Roy - Coding Made Simple
Рет қаралды 541 М.