Тёмный

G-37. Path With Minimum Effort 

take U forward
Подписаться 682 тыс.
Просмотров 138 тыс.
50% 1

GfG-Problem Link: bit.ly/3dMBvq6
C++/Java/Codes and Notes Link: takeuforward.o...
DP Series: • Striver's Dynamic Prog...
SDE Sheet: takeuforward.o...
Check out our Website for curated resources:
Our Second Channel: / @striver_79
In case you are thinking to buy courses, please check below:
Code "takeuforward" for 15% off at GFG: practice.geeks...
Code "takeuforward" for 20% off on sys-design: get.interviewr...?_aff=takeuforward
Crypto, I use the Wazirx app: wazirx.com/inv...
Take 750 rs free Amazon Stock from me: indmoney.oneli...
Earn 100 rs by making a Grow Account for investing: app.groww.in/v...
Linkedin/Instagram/Telegram: linktr.ee/take...
---------------------------------------------------------------------------------------------------------------------------

Опубликовано:

 

1 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 244   
@takeUforward
@takeUforward 2 года назад
Let's continue the habit of commenting “understood” if you got the entire video. Please give it a like too, you don't 😞 Do follow me on Instagram: striver_79
@b-techcodewala3030
@b-techcodewala3030 Год назад
best teacher
@ashishkumaryadav5252
@ashishkumaryadav5252 Год назад
Outstanding teaching skills.
@sudhanshushekhar4222
@sudhanshushekhar4222 Год назад
understood
@patchasrinivas337
@patchasrinivas337 2 месяца назад
hi striver, can we solve these problem using dp as like as maximum path sum in a grid
@codingwithanonymous890
@codingwithanonymous890 2 года назад
i always try this approach fitting algorithms if its asked minimum think of binary search, dijkstra algorithm or dp
@satvikdixit
@satvikdixit Год назад
Whenever there is an optimisation problem, i.e finding maximum or minimum. You generally have 3 options. DP ,Greedy , Binary search. Djiktra is kind of a Greedy+DP thing where the states of dp ( or nodes in the graph) can have greedy transitions. That is how I think. Very similar to you
@techbucks7339
@techbucks7339 Год назад
Wait you guys did dp before graph? Am i doing something wrong f
@codingwithanonymous890
@codingwithanonymous890 Год назад
@@techbucks7339 theres not any specific order bro , like recursion is used in both dp and graph traversals so recursion is pre requisite other things you can cover in the series
@techbucks7339
@techbucks7339 Год назад
@@codingwithanonymous890 thanks man i plan to do dp after this .
@sahilbani7020
@sahilbani7020 Месяц назад
I would have a hard time thinking this as a graph problem rather than a DP problem.
@alexwhitewood6480
@alexwhitewood6480 Год назад
The hiker analogy is terrible for this question...
@highsociety7677
@highsociety7677 Год назад
Watched 4 videos on this question, yours is the only one that made sense! Great teaching, thanks so much!
@PiyushBajaj05
@PiyushBajaj05 Год назад
Hi Striver, One basic question: We can brainstorm such questions or solve if we draw such graph like matrix and queue with pen and paper or using digital pen in the draw tool. But how to solve the same in google doc or any notepad, because in the real interview we have to solve in that?
@shaikfahim1232
@shaikfahim1232 Год назад
You can ask him to dry run it using pen and paper having with you and come with this approach
@242deepak
@242deepak Год назад
why are you returning diff when top element in priority queue has row and col as n-1? what if all the paths reaching that cell have not been traversed yet?
@mayuksarkar5077
@mayuksarkar5077 Год назад
Same question
@guptashashwat
@guptashashwat Год назад
Let me explain: You can also do without early returning ie at the end simply dist[n-1][m-1]. Priority queue/set gives smallest value on top, if that smallest value is destination then that has to be the ans because, you cannot further reduce the value by using other bigger values present in the DS.
@coderhumai
@coderhumai Год назад
​@@guptashashwat so is it impossible to return -1? We would get atleast a difference possible?
@krishnananddubey2870
@krishnananddubey2870 Год назад
@@coderhumai Yes bro there always exist a path from src to dest. You don't need to return -1.
@zephyr_8
@zephyr_8 Год назад
@@coderhumai for this question, you can. actually, there always exists certain paths from (0,0) to (row-1,col-1), so your return will always occur in the queue.
@rahulkhandelwal7034
@rahulkhandelwal7034 2 года назад
Thanks striver for all videos you have created so far .Learnt a hell lot of things from you . 🙏🙏
@ashishkumaryadav5252
@ashishkumaryadav5252 Год назад
Supreme quality content, exceptional teaching skills, thanks a lot sir.
@keertilata20
@keertilata20 Год назад
GOD LEVEL PLAYLIST ON GRAPHS🙇‍♀
@NavyasriThalluri
@NavyasriThalluri 18 дней назад
why we returning the answer value early , instead of queue becoming empty and returning dist[m-1][n-1] where m=no. of rows and n= no. of cols
@tanujaSangwan
@tanujaSangwan 29 дней назад
Solved this question also without any help. With dijkstra. Are these questions really easy or am I improving?
@soubhikghosh6564
@soubhikghosh6564 Год назад
Hi Striver there is a lot of concern in many questions on some questions on grids cant be solved by using dp? please give a general explanation over that. many are stuck on this problem including me
@rickk3300
@rickk3300 Год назад
We can use DP to solve only those problems which can be decomposed into such smaller subproblems which are not dependent on each other, like if there is a subproblem A and a subproblem B of the main problem, then if A depends on the result of B, then B should not depend on the result of A and vice versa, that is, the unidirectional dependency among subproblems should be maintained. But here, in this problem, if you look carefully, two subproblems depend on each other, hence we cannot apply dp here.
@tanujaSangwan
@tanujaSangwan 29 дней назад
@@rickk3300 Movement Restriction (Right and Down): DP works effectively because you can break down the problem into simpler subproblems where each subproblem (cell) only depends on a fixed set of previous subproblems (the cells directly above or to the left). The problem structure is simple and doesn’t involve revisiting cells or complex path updates. Movement in All Directions: DP is less straightforward due to the possibility of revisiting cells and the need to handle complex dependencies between different paths. Algorithms designed for shortest path problems in graphs (like Dijkstra’s or A*) are better suited to handle these cases, as they are designed to efficiently manage dynamic path updates and complex dependencies.
@rickk3300
@rickk3300 29 дней назад
@@tanujaSangwan exactly, that's what I said! If there is a need to revisit cells or there are complex dependencies, we can never apply dp.
@tanujaSangwan
@tanujaSangwan 29 дней назад
@@rickk3300 Right. Thanks for the clarification
@rushidesai2836
@rushidesai2836 5 месяцев назад
class Solution { public int minimumEffortPath(int[][] heights) { int n = heights.length; int m = heights[0].length; PriorityQueue pq = new PriorityQueue(Comparator.comparingInt(t -> t.d)); int[][] dist = new int[n][m]; for(int i = 0; i < n; i++){ for(int j = 0; j < m; j++){ dist[i][j] = (int)(1e9); } } dist[0][0] = 0; pq.add(new Tuple(0,0,0)); int[] delRow = {-1,0,1,0}; int[] delCol = {0,1,0,-1}; while(pq.isEmpty() == false){ Tuple t = pq.poll(); int distance = t.d; int row = t.row; int col = t.col; for(int i = 0; i < 4; i++){ int nRow = row + delRow[i]; int nCol = col + delCol[i]; if(nRow >= 0 && nRow < n && nCol >= 0 && nCol < m){ int newEffort = Math.max(distance, Math.abs(heights[row][col] - heights[nRow][nCol])); if(newEffort < dist[nRow][nCol]){ dist[nRow][nCol] = newEffort; pq.add(new Tuple(newEffort,nRow,nCol)); } } } } return dist[n-1][m-1]; } } Return statement should be at the end, not while taking out of PriorityQueue. Tested on LeetCode.
@Nirala_414
@Nirala_414 Год назад
After 3- time watch, i was able to understood it. I got stuck in some cross question, Now happy 😊 Thank you bhaiya ❤
@rajatraj4297
@rajatraj4297 9 месяцев назад
Best feeling when you can solve a problem completely based on the the understanding from previous videos.
@tasneemayham974
@tasneemayham974 4 месяца назад
AND FROM ONE GOO!!!
@udaykumarpg3874
@udaykumarpg3874 Год назад
Why can't we solve this problem using DP?
@somsk56
@somsk56 Месяц назад
cause u r gay
@nehatanganiya5491
@nehatanganiya5491 Год назад
How we get this types of approaches... I can't imagine this approach... I don't know why...I know this can solved using bfs but
@meetverma4812
@meetverma4812 22 дня назад
can't we do this with simple BFS?
@tasneemayham974
@tasneemayham974 4 месяца назад
BEST BEST BEST TEACHEERRR!!! It really shows when you are able to solve all these problems by your own!!! THANK YOU BRO!!!
@Prateek_Mantry
@Prateek_Mantry 3 месяца назад
Why are we stopping at {2, {2, 2}} ? In future operations we might have found a lesser difference path like {1, {2,2}} ?
@FooBar-lb5wf
@FooBar-lb5wf 2 месяца назад
The greedy approach of selecting the element with the minimum dist value from the priority queue (that is selecting the unvisited node which is closest to the source) ensures that by the time a node 'u' is extracted from the priority queue, dist[u] is already set to the shortest path distance from the source. This is a property of Dijkstra's algorithm (independent of this problem)
@akshanshsharma6025
@akshanshsharma6025 Год назад
at the time stamp 12:54 I didn't get it when the previous difference is 6 and the new difference is 5 then why we update it because initially we take the maximum of the difference
@kamranwarsi12b22
@kamranwarsi12b22 Год назад
same doubt
@037_abhinavkumar3
@037_abhinavkumar3 7 месяцев назад
same doubt ):
@muskanagrawal7428
@muskanagrawal7428 2 месяца назад
we update effort with maximum and dis array with minimum, earlier we updated dis[0][2] with 1 and not 0 because effort is max(0,1)=1 and updatd dis with min(effort, 1e9)=1, similarly we updated dis[1][1] with min(effort(viz 5),dis[1][1](viz 6)) = 5.
@Tiyasa-d8f
@Tiyasa-d8f Месяц назад
Can this also be thought of like the Painter's partition problem using Binary search? The maximum effort can be the sum of differences and minimum is 0. I thought about it because here we need to find MINIMUM OF THE MAXIMUM effort, just like in the painter's partition problem.
@muskangupta5873
@muskangupta5873 4 месяца назад
why writing condition like this gives TLE if(nrow >= 0 && nrow < rows && ncol >= 0 && ncol < columns && abs(heights[nrow][ncol] - heights[x][y]) < ans[nrow][ncol]) { int new_diff = max(diff, abs(heights[nrow][ncol] - heights[x][y])); ans[nrow][ncol] = new_diff; q.push({new_diff, {nrow, ncol}}); }
@OnstreamGaming
@OnstreamGaming 21 день назад
why we have created absolute diff array[][] when the absolute diff will going to be different in every different path
@amansingh.h716
@amansingh.h716 27 дней назад
First i try bruteforce approach normal BFS go to all direction --GETS TLE Dikastra algo goes in 4 direction which ever is small it goes there and generate shortest path
@shivamgupta3520
@shivamgupta3520 Год назад
can u please explain the line of priority queue how u write this in this question and also in dijkstra algorithm i didn,t understand it
@rishabhgupta9846
@rishabhgupta9846 Год назад
that is syntax for min priority queue ,we need to take pair as data type for PQ
@amansinghal4663
@amansinghal4663 Год назад
see the c++ STL video of striver, jump to the priority queue part, there you will see that for MAX HEAP priority queue, the syntax is simple, but for MIN HEAP, the syntax is larger and different. So in that syntax, just replace ''int'' from everywhere with pair because in this question, the data type we want to store in the priority queue is not simple 'int', it's a 'pair'
@shivamgupta3520
@shivamgupta3520 Год назад
@@amansinghal4663Thanks I have learnt the heap now that time i don't know about the heap DS.
@dhawalpatil7779
@dhawalpatil7779 2 месяца назад
He straight away told use dijkstra, but why ? any proof
@development5298
@development5298 Месяц назад
why will a normal bfs not work for this ? i have written the same code but its just for bfs why doesnt this work then ?
@nikhil_squats
@nikhil_squats Месяц назад
DP came to my mind, as it asked to explore all paths
@amansingh.h716
@amansingh.h716 27 дней назад
so dikastra algo is all about find shortest path to its neighbour and ultimately we get shortest path to destination
@banothutharun2743
@banothutharun2743 2 месяца назад
understood brother 🤩
@lakshya3324
@lakshya3324 Год назад
Im not able to understand the loop break condition ( why we will not find a better solution in future) at 15:10
@coder_boy
@coder_boy 4 месяца назад
Because of the PriorityQueue, the mindiff is covered so the upcoming diff will be equal to or greater than mindiff. That's why.
@mbm.editzz
@mbm.editzz 9 месяцев назад
can someone pls explain why {2,{2,2}} will be final answer and there wouldnt be any lesser distnce
@tasneemayham974
@tasneemayham974 4 месяца назад
Because we are taking the priorityQueue which takes least distances. So, every other distance is greater than what I currently have. And since this is the last node, so there is no chance of different paths. It's over. Greedy on the last node works. Get me?
@iamnoob7593
@iamnoob7593 11 дней назад
Man what a problem, Thank striver
@anubhavpabby6856
@anubhavpabby6856 2 года назад
Striver bhiaya can we also use the concept of dynamic programming to calculate minimum effort? BTW I am getting confused like when to apply dp and when to use dijsktra to calculate min effort?
@anubhavpabby6856
@anubhavpabby6856 2 года назад
Bhaiya I am geting confused like why we cannot use dfs or dp here to calculate the minimum answer.
@takeUforward
@takeUforward 2 года назад
You can try, I always say to write code and submit. And then see which test cass it is failing. Then write the test case, in most of the cases you get to know why
@SatyamEdits
@SatyamEdits 2 года назад
@@anubhavpabby6856 with dfs you will get correct answer but it will fail the time limit.... because you may have to visit some nodes alot of times.....(same problem arises with using Queue also..instead of priority queue)...... Before learning a new algorithm try to find out what are the problems you are facing with your current knowledge of algorithm....attempt the question with your knowledge...then find out the problems you faced...and then finally see how striver bhaiya has solved those problems with new algorithm......
@krishanpratap3286
@krishanpratap3286 2 года назад
@@SatyamEdits instead of just simple recursion agar dp use kare toh work kar jayega shayd
@b28venkatasivakalyankaliki81
@boomsi69 Thank you for the explanation !!!❤❤❤❤❤❤
@harshit_nimesh
@harshit_nimesh 6 месяцев назад
I was able to solve this without watching the video. Thanks to your explanation skills. nice work sir!
@tasneemayham974
@tasneemayham974 4 месяца назад
MEE TOOO!!! Striver is the BESTTT!!!!
@cinime
@cinime 2 года назад
Understood! Super amazing explanation as always, thank you very much!!
@Dishant-nn7uk
@Dishant-nn7uk 3 месяца назад
can we do this instead of dr and dc for(int i=-1;i
@puspendrayadav8931
@puspendrayadav8931 Год назад
This problem can also be solved with binary search on answer + simple BFS.
@deviprasad_bal
@deviprasad_bal Год назад
how?
@muditkhanna8164
@muditkhanna8164 Год назад
So the main point to remember other than dijktra is when looking for nodes in all 4 directions if we found a node which is row=n-1 and col=n-1 we just push it into the pq and we declare the answer only when the top element of pq is the node at row=n-1 and col=n-1 then only we declare the answer ?
@Mercer80
@Mercer80 Год назад
yes
@adityanigam8373
@adityanigam8373 Год назад
Thank you striver bhaiya. beautiful explanation .wish to meet you someday
@djpsn7094
@djpsn7094 Год назад
22:27 got heart attack to me at this time 😄
@pusarlaaishwarya5035
@pusarlaaishwarya5035 2 года назад
Understood 👍👍 can you please put like this video's eveyday 👍 I will surely watch it👍
@kaichang8186
@kaichang8186 21 день назад
understood, thanks for the great video
@ayushranjan1265
@ayushranjan1265 4 месяца назад
In cell configuration (1,1) I think the diff should be 7. Please correct me if I am wrong..
@jayantgahlot2653
@jayantgahlot2653 Год назад
doubt: why we need to break the loop we can get the answer at the end also by -: return dist[n-1][m-1]
@ithcaironsthlotron5546
@ithcaironsthlotron5546 2 года назад
if I use simple DFS with backtrack(of vis array), what will be its TIME COMPLEXITY?
@yashbankar4207
@yashbankar4207 11 месяцев назад
ig 4^n
@yaswanthtogarapu8458
@yaswanthtogarapu8458 11 месяцев назад
Hi guys, have a small doubt that dijkstra's algorithm seems to use the same key idea from what dynamic programming does (don't think for the overlapping sub problems rather the key idea of optimization i'm saying) , because every time it optimizes the vertex locally but from all possible directions, ?
@mrlord8519
@mrlord8519 6 месяцев назад
Same doubt bro....any lead now
@tasneemayham974
@tasneemayham974 4 месяца назад
Yes, it does. The nice thing in Dijkstra is that it relies on Greedy more than on exploring all paths. Like in DP, you don't care about which one comes first. You are choosing randomly, but in Dijkstra we are first prioritizing the least distances and once we reach our destination we have the answer. Meanwhile DP, cannot do that. Even if you reach the final destination, since you are moving randomly, there might be a path that is yet to be discovered. When you say it optimizes vertices locally, yes both optimize locally, but the difference is in the process. In DP, we explore the entire path to the end of the base case / destination, and THEN return and explore the next path and optimize the vertex. In Dijkstra, WHILE we are exploring the path, we instantly choose "I am the lesser vertex. Why are you here?" So the vertex is optimized. So when we reach the end we have the answer. I hope I made it clear. Is there anything vague?
@Prateek_Mantry
@Prateek_Mantry 3 месяца назад
@@tasneemayham974 In this question why we are stopping at {2, {2, 2}} ? In future operations we might have found a lesser difference path like {1, {2,2}} ?
@tasneemayham974
@tasneemayham974 3 месяца назад
@@Prateek_Mantry Impossible. If we are using a PQ that ensures the elements are sorted according to least distance. Then we are certain, that when we reach {2, {2,2}} there is NO way a lesser path could've been found. Because if there was a path indeed it would've been popped before {2,{2,2}}. Got me?
@aadilkhan4867
@aadilkhan4867 Год назад
bhaiya sorry i have a doubt in this solution you are returning the ans first time whenever we reach the destination this can lead a wrong ans as you have explain in but not following in code i guess i think we should return the ans after our pq is empty abs will dist[n-1][m-1] i guess can you confirm please
@BruceWayne-mf6ps
@BruceWayne-mf6ps 7 месяцев назад
16:28 do not conclude, it should have been better if you have simulated more until all the elements are empty. However, I loved your video.
@justanuhere
@justanuhere 5 месяцев назад
what is the point of using distance array here? i tried doing this without a distance array it gives tle why?
@tasneemayham974
@tasneemayham974 4 месяца назад
Because won't you go back and forth? In your code ask yourself what prevents my algorithm from going back to the cell it came from?
@vibhorbhatt3076
@vibhorbhatt3076 4 месяца назад
I can't believe the level of simplicity you follow to teach us these complex topics with ease. I can never imagine understanding this stuff with minimal efforts without Striver bhaiya ❤
@krisify
@krisify 3 месяца назад
Can this be solved using bs?
@karthikavijayannv5319
@karthikavijayannv5319 Год назад
understood
@SameerAnand-YearBTechElectrica
@SameerAnand-YearBTechElectrica 4 месяца назад
You didnt create a min heap in code
@abhishekranjan8741
@abhishekranjan8741 2 года назад
dp on trees striver plz
@amanasrani6405
@amanasrani6405 3 месяца назад
Amazing sir , bestttttttttt wayyyyyyyy, make that tough question so easyyyyyyy thank you sir, 🙏🏻🙏🏻❤
@shaddyrogue9430
@shaddyrogue9430 2 года назад
There is similar issue with this question as well. Your Code's output is: 30080 It's Correct output is: 30080 Output Difference 30080
@swapnilhajare5557
@swapnilhajare5557 2 года назад
Same issue
@SatyamEdits
@SatyamEdits 2 года назад
yesss......
@nishantsah6981
@nishantsah6981 2 года назад
This Problem is also on Leetcode Problem 1631
@tejasc7409
@tejasc7409 2 года назад
@@nishantsah6981 No It works fine in Leetcode
@princenagar1686
@princenagar1686 2 месяца назад
Solved this question without watching video , thanks Striver.
@shivammaurya9630
@shivammaurya9630 8 месяцев назад
you w8 might find someone better...:)
@gamerglobe4839
@gamerglobe4839 Год назад
why we are using distance array here even without this,we can solve this and previous problem too by just using priority queue min heap because it always pops up min value and we proceed with it and at the end of the day we can return our answer...
@amansinghal4663
@amansinghal4663 Год назад
yes i have the same doubt, by doing this, i was getting wrong answer in previous question for some test cases and getting TLE in this question for all the test cases.
@gamerglobe4839
@gamerglobe4839 Год назад
@@amansinghal4663 bro since we are learning to apply dijkstra that's why he used distance array and also in some questions without distance array ,finding answer could be a complicating stuff.
@amansinghal4663
@amansinghal4663 Год назад
​@@gamerglobe4839 I see....
@pratikshadhole6694
@pratikshadhole6694 Год назад
understood
@ShivamYadav-lz5gx
@ShivamYadav-lz5gx Месяц назад
Understood
@SHOURYASINGHAI-h8v
@SHOURYASINGHAI-h8v 2 месяца назад
understood !
@UECAshutoshKumar
@UECAshutoshKumar 11 месяцев назад
Thank you sir 😊
@lucy2003-d8p
@lucy2003-d8p 2 месяца назад
nice brother
@Sakuna172
@Sakuna172 Год назад
I first commented as not understood but by end of video i edited the comment as understood 😂
@HARSHITCHOPRA2K21_CO_18
@HARSHITCHOPRA2K21_CO_18 2 месяца назад
Understood
@ujjwalagrawalvideos
@ujjwalagrawalvideos 2 месяца назад
understood
@KRRISHKUMAR-st1yn
@KRRISHKUMAR-st1yn 3 месяца назад
Understood
@ADI.scussion
@ADI.scussion Год назад
Bhai yaar kya hi samjhaya, ekdum garda . Mazza aa gya
@abhinanda7049
@abhinanda7049 4 месяца назад
understood
@technologybaba192
@technologybaba192 4 месяца назад
Understood
@vikrantyadav3073
@vikrantyadav3073 4 месяца назад
Understood
@harshalgarg1676
@harshalgarg1676 Месяц назад
US
@aniketlal1657
@aniketlal1657 Год назад
Could this be solved with DP?
@harshitjaiswal9439
@harshitjaiswal9439 6 месяцев назад
understood
@Professor-du2pf
@Professor-du2pf 6 месяцев назад
Understood
@raghavmanish24
@raghavmanish24 4 месяца назад
thanku striver , it's crystall clear
@chiragbansod8252
@chiragbansod8252 7 месяцев назад
understood
@EBKCS_VARUN_BHUTADA
@EBKCS_VARUN_BHUTADA 7 месяцев назад
understood
@ishitarakchhit8272
@ishitarakchhit8272 11 месяцев назад
Understood
@parallax8916
@parallax8916 11 месяцев назад
Understood
@paragroy5359
@paragroy5359 8 месяцев назад
Thanks a lot for the video. Nice explanation keep on making such videos.
@anshumanbehera3706
@anshumanbehera3706 Год назад
i got time limited exceed in thr case of java code so can you plz optimized the code of java I am not able to submit the solution in gfg
@CodeMode9313
@CodeMode9313 Год назад
habibi video me bas ye clear nhi ho rha ki jab 2 destination cell me store hua then usske baad hi kyu return kardia answer why didnt we explore the remaining iteration of the queues
@optimus_prime01
@optimus_prime01 Год назад
do a dry run
@AyushSharma-ux4fk
@AyushSharma-ux4fk Год назад
should we not use dp in this problem?
@saichandu8178
@saichandu8178 Год назад
this is one of confucious problems
@subhashpatel5861
@subhashpatel5861 Год назад
this question can be done using dp as well as binary search on answers too :)
@parthmadan671
@parthmadan671 Год назад
how
@mrlord8519
@mrlord8519 6 месяцев назад
Dp ok but how on binary search on answers?
@pratyakshhhhhhhhhhhhhhhhhhhhh
@pratyakshhhhhhhhhhhhhhhhhhhhh 9 месяцев назад
🎉
@siddharthmishra8233
@siddharthmishra8233 Год назад
i have a confusion we got the destination at with effort of 3 already so why didn't it returned 3?
@Mohit_Q
@Mohit_Q Год назад
bcoz we are prioriy queue (min heap) , to uski call he nahi hogi and us se phela fir {2,{2,2}} call hogi and we will get the answer
@aryanbarnwal5645
@aryanbarnwal5645 9 месяцев назад
why is dfs giving tle?
@dank7044
@dank7044 3 месяца назад
did this on my own
@souravjoshi2293
@souravjoshi2293 Год назад
22:42 confusing here
@amansinghal4663
@amansinghal4663 Год назад
I think this question can be solved without the distance matrix also, i tried it using priority queue without distance matrix but it's giving TLE. I defined the priority queue as pair of 2 pairs. Like this: {{a,b},{c,d}} where, a = absolute difference b = current value in the given matrix c = row d = column
@gauravkabdwal309
@gauravkabdwal309 Год назад
Understood
@aryankumar3018
@aryankumar3018 Год назад
Understood
@akshatgosain3831
@akshatgosain3831 Год назад
efficient c++ code: int n=heights.size(); int m=heights[0].size(); priority_queue q; q.push({0,{0,0}}); vector dist(n,vector (m,1e9)); dist[0][0]=0; while(!q.empty()){ int dis=q.top().first; int row=q.top().second.first; int col=q.top().second.second; q.pop(); for(int k=-1;k=0 && row+kmax(dis,abs(heights[row][col]-heights[row+k][col]))){ dist[row+k][col]=max(dis,abs(heights[row][col]-heights[row+k][col])); q.push({dist[row+k][col],{row+k,col}}); } } if(col+k>=0 && col+kmax(dis,abs(heights[row][col]-heights[row][col+k]))){ dist[row][col+k]=max(dis,abs(heights[row][col]-heights[row][col+k])); q.push({dist[row][col+k],{row,col+k}}); } } } } return dist[n-1][m-1];
@rishabhagarwal8049
@rishabhagarwal8049 2 года назад
Understood Sir, Thank you very much
Далее
G-38. Cheapest Flights Within K Stops
23:56
Просмотров 160 тыс.
G-36. Shortest Distance in a Binary Maze
23:42
Просмотров 145 тыс.
G-40. Number of Ways to Arrive at Destination
24:06
Просмотров 116 тыс.
G-39. Minimum Multiplications to Reach End
19:31
Просмотров 94 тыс.
Path with Minimum Effort - Leetcode 1631 - Python
14:35
Path with Maximum Probability - Leetcode 1514 - Python
13:11
How to speak like the 1% elite
10:30
Просмотров 7 тыс.
G-35. Print Shortest Path - Dijkstra's Algorithm
19:20
Просмотров 200 тыс.
G-41. Bellman Ford Algorithm
27:43
Просмотров 214 тыс.