Find the Safest Path in a Grid - Leetcode 2812 - Python

แชร์
ฝัง
  • เผยแพร่เมื่อ 14 ต.ค. 2024

ความคิดเห็น • 79

  • @michael._.
    @michael._. 5 หลายเดือนก่อน +38

    neetcode makes a hard question disguised in medium actually feel like a medium under 30 minutes

  • @nathan_ca
    @nathan_ca 5 หลายเดือนก่อน +22

    Thanks! now I watch you video to do mental exercise rather than wasting time on tiktok or yt shorts for nothing.

  • @mudit7394
    @mudit7394 5 หลายเดือนก่อน +8

    Only simple improvements like removing the dict improves the code massively:
    class Solution:
    def maximumSafenessFactor(self, grid: List[List[int]]) -> int:
    N = len(grid)
    directions = [(0, 1), (0, -1), (1, 0), (-1, 0)]
    q = deque()
    def inside_bounds(r, c):
    return 0

    • @NeetCodeIO
      @NeetCodeIO  5 หลายเดือนก่อน +8

      Yeah as long as modifying the input is allowed this is a great solution, thanks for sharing this variation

  • @abhimanyuambastha2595
    @abhimanyuambastha2595 5 หลายเดือนก่อน

    Solved this problem on my own using the approach from a similar problem 1631. Path with minimum effort. Used a modified dijkstra with max heap. Resulted in TLE but passed 1000/1036 testcases, so pretty happy with my solution. Came here for the neetcode special optimized solution and wasn't disappointed

  • @nirmalgurjar8181
    @nirmalgurjar8181 5 หลายเดือนก่อน +8

    According to me, this is top quality problem which involves multi source bfs, dfs, binary all in one solution.

  • @cc-to2jn
    @cc-to2jn 3 หลายเดือนก่อน +1

    man a year ago i couldnt solve something like this, now it seems easy when u know the patterns. I was surprised that this problem didn't take me longer than 30min to solve on first attempt!

  • @yang5843
    @yang5843 5 หลายเดือนก่อน +7

    Thanks for making this video, there are multiple parts to this problem

  • @business_central
    @business_central 5 หลายเดือนก่อน +1

    Man I did the two other problems you mentioned Walls and gates and swim in rising water, but gosh it didn't came to mind when I was doing today's problem, at least not within the 25 minutes I allow myself. Still long way to go.
    Thanks for all your efforts!

  • @amrutaparab4939
    @amrutaparab4939 17 วันที่ผ่านมา

    Thankyou for specifiying the real difficulty of the problem where needed! Just when my confidence level is about to drop, you say “this one is pretty tricky” and I am like phew😅

  • @SameerRehan-tt4lh
    @SameerRehan-tt4lh 5 หลายเดือนก่อน +1

    Love the way you tackle a problem, I even want to think of approaches like you sir,

  • @black2342
    @black2342 5 หลายเดือนก่อน +1

    Thank you for finding the safest path in LeetCode problems.

  • @kwakukusi4094
    @kwakukusi4094 5 หลายเดือนก่อน

    thanks for the explanation, i could not have solved it without your explanation but once I understood the question I realised why it was a medium problem

  • @meemee417
    @meemee417 5 หลายเดือนก่อน +5

    spent like an hour solving this, half of which was wondering why this was considered a medium

  • @soumyajitganguly2593
    @soumyajitganguly2593 4 หลายเดือนก่อน

    @26:14 Dijkstra / using heap is not the most optimal. You can do this using a dequeue (0/1 BFS)

  • @Ziviani
    @Ziviani 5 หลายเดือนก่อน

    I was "lucky" enough to get a version of this question in my interview. I tried solving it with Dijkstra setting the "unsafe" cells to infinite, so when calculating distances my algorithm would never consider that path. But my implementation sucked just like the feedback I had. 😀

  • @anand_dudi
    @anand_dudi 5 หลายเดือนก่อน

    Whenever any problem goes above my head i straight find neetcode video on youtube 😄

  • @StellasAdi18
    @StellasAdi18 5 หลายเดือนก่อน

    Best explanation! Thanks for wonderful content.

  • @hasibulfor
    @hasibulfor 4 หลายเดือนก่อน

    O my lord, in 1.5x I just saw a thriller movie!!! Keep it up bro...

  • @rahulsihara8946
    @rahulsihara8946 5 หลายเดือนก่อน +7

    Jokes on Leetcode, i didnt event try to solve it. Just came here to understand the problem. :D

  • @DBagg-zz4ip
    @DBagg-zz4ip 5 หลายเดือนก่อน

    Oh man. I thought I had this one but timed out on the last test. Set all the 0 cells to 400 and BFSed from every thief, only adding a neighbor if its value was higher. Thought that would cut down on time complexity but now I visualize a case where thieves cockblock each other back into n^4. Didn't think of just putting them in one deque.

  • @jagrutitiwari2551
    @jagrutitiwari2551 5 หลายเดือนก่อน

    I was searching for your video for this question in the morning. I thought you won't upload it today.

  • @ay5960
    @ay5960 5 หลายเดือนก่อน

    I was reading the recent leak/whistleblower on reddit. Came out today about reverse engineering alien crafts. It was mentioned these crafts are not autonomous but use some object avoidant 3d Dijkstra algorithm and now I come here and see neetcode has a object avoidant Dijkstra question.

  • @thejvenkat3534
    @thejvenkat3534 5 หลายเดือนก่อน

    NeetCode
    If you don't mind can you also analyse the top solutions, because sometimes simple improvements to your solution will bring down the time a lot and can help us understand why it takes more time.

  • @shubhamchouksey9904
    @shubhamchouksey9904 5 หลายเดือนก่อน +1

    thankyou neetcode

  • @rocmewtwo
    @rocmewtwo 5 หลายเดือนก่อน

    Hello neetcode, I found you have two types of dijkstra algorithms.
    some time you add if condition after heap pop, and that is code template in course you teach.
    w1, n1 = heappop(heap)
    if n1 in visit:
    continue
    visit.add(n1)
    But some time you write like as video, make me a little confused.

  • @felipesan774
    @felipesan774 5 หลายเดือนก่อน

    Thanks bro, great video

  • @jayliu5021
    @jayliu5021 5 หลายเดือนก่อน

    Great work! But, I have a question that why sometimes we use Dijkstra BFS to search our neighbor we can't set the visited[neihbor] to true;, but here we can set the visited[neighbor] to true?
    I just pop from the queue and set visited to true once and BFS search not set the visited[neighbor] to true, and it is also work (but may time limit exceed).

  • @johnj171
    @johnj171 2 หลายเดือนก่อน

    hallelujah GodFather hallelujah love you content and your way of expression

  • @priyanshagarwal8490
    @priyanshagarwal8490 5 หลายเดือนก่อน

    Can you explain 166. Fraction to Recurring Decimal next Pls..

  • @Antinormanisto
    @Antinormanisto 5 หลายเดือนก่อน +3

    I want to cry, i tried to solve this for 4 hours(

    • @rohitkumaram
      @rohitkumaram 5 หลายเดือนก่อน +3

      Just a suggestion, don't try more than 20-30 minutes look at the solution , build your knowledge base by looking at the solution, and then put that question with link into an excel sheet to solve after 1 month. Still can't solve it , look at the solution understand it then put in the list to solve in next month. repeat until you can tackle it easily. For example this problem is going in my JUNE month list.
      Try this very effective.

    • @YoungLeaf79
      @YoungLeaf79 5 หลายเดือนก่อน

      You haven't solved it but probably learned a ton so still it's a win imo.

    • @EduarteBDO
      @EduarteBDO 5 หลายเดือนก่อน

      I solved it in 5, Idk what I'm doing with my life.

    • @saumypandey2288
      @saumypandey2288 5 หลายเดือนก่อน +1

      ​​@@EduarteBDO5 what??
      Hrs, minutes ,secs

    • @EduarteBDO
      @EduarteBDO 5 หลายเดือนก่อน

      @@saumypandey2288 hours.

  • @pastori2672
    @pastori2672 5 หลายเดือนก่อน

    i actually believe i did the first part but just ran another regular bfs instead of modified dijkstra's and got tle..

  • @ritikaagrawal769
    @ritikaagrawal769 5 หลายเดือนก่อน

    had to take hint and apply binary search and finally solved it on my owm in 1.5 hrs. But watched your and realised it could be done a bit easily. Complex problems teach you stuff, you did not want to learn. XD

  • @AbhishekSingh-xq4cu
    @AbhishekSingh-xq4cu 5 หลายเดือนก่อน

    How to see locked problems of leetcode, how did you open walls and gates in neetcode?

  • @pastori2672
    @pastori2672 5 หลายเดือนก่อน

    starting to take leetcode less and less seriously tbh it doesnt feel like its making me any better but i think im just coping

  • @anti-tankartur677
    @anti-tankartur677 5 หลายเดือนก่อน

    I tried the bruteforce solution the first time i got this problem. I solved half the test cases and tle on the other half. Feels like shit.

  • @3227998
    @3227998 27 วันที่ผ่านมา

    wow!

  • @HuyLe-zx8ko
    @HuyLe-zx8ko 5 หลายเดือนก่อน

    good job bro 🤘

  • @soumyajitganguly2593
    @soumyajitganguly2593 4 หลายเดือนก่อน

    The free version of Walls and gates is Rotten oranges

  • @BilalShaikh-tn9wu
    @BilalShaikh-tn9wu 5 หลายเดือนก่อน +2

    I'll just let the thieves rob me if it means coming up with this on my own

  • @MohanRam-mq2pk
    @MohanRam-mq2pk 5 หลายเดือนก่อน

    tell me how your accent is so good?

  • @mementomori8856
    @mementomori8856 5 หลายเดือนก่อน

    127 lines in Go lol. I miss Java, it had every data structure under the sun.

  • @ben94_
    @ben94_ 5 หลายเดือนก่อน

    me: i should really start trying more mediums...
    the mediums:

  • @sugaith
    @sugaith หลายเดือนก่อน

    if NeetCode takes 30 min to explain how would I solve this in an assessment of 45 min? this is insane

  • @ronbuchanan5432
    @ronbuchanan5432 5 หลายเดือนก่อน

    You're wrong about the distance metric. The shortest path depends on the metric.
    Manhattan distance (Taxicab distance) means you can only travel on a grid, like cabs in Manhattan.
    Chebyshev distance is similar but you can travel diagonally as well, like the King piece in chess.
    So saying "Manhattan distance is the shortest path" is just wrong and it's important to know that.
    Anyway thanks for the explanation!

    • @NeetCodeIO
      @NeetCodeIO  5 หลายเดือนก่อน +1

      Is it wrong in the context of this problem where we can only move in 4 directions though?

    • @ronbuchanan5432
      @ronbuchanan5432 5 หลายเดือนก่อน

      @@NeetCodeIO okay you're right they have stated above what an "adjacent cell" is, so it a needless complication but at the same I see it as opportunity to educate people.

  • @cheezdog1343
    @cheezdog1343 5 หลายเดือนก่อน

    Can someone please explain to me why after we create the safeness grid using our first BFS, we can't use DP on the grid starting from the bottom right corner to calculate the solution in O(n^2) time? I tried implementing this solution but I'm getting 7 test cases failed, and I don't know if its due to my error or the fact that my approach doesn't work

    • @cheezdog1343
      @cheezdog1343 5 หลายเดือนก่อน +1

      Update: I think at least that DP cannot be used since you can move in all four direction, not just left or right to create a path, so this would make DP solution very hard to implement since you would have to store additional info to make sure you don''t revisit square. Or smthg like that. Who knows. Fuck this.

    • @EduarteBDO
      @EduarteBDO 5 หลายเดือนก่อน

      @@cheezdog1343 I think I did that, I used a new grid to store the values but got tripped by edge cases until I started traveling in 4 directions instead of just right/down

  • @advaitbajaj4241
    @advaitbajaj4241 5 หลายเดือนก่อน +3

    Did you say ladoo? 😂😂

    • @finfan2101
      @finfan2101 5 หลายเดือนก่อน

      when?

  • @bhaskarpilania
    @bhaskarpilania 5 หลายเดือนก่อน +2

    "leetcode laddoo" 🤣

  • @sethomoke-enyi539
    @sethomoke-enyi539 2 หลายเดือนก่อน

    bro was pissed looool, i hear it

  • @JamesBond-mq7pd
    @JamesBond-mq7pd 5 หลายเดือนก่อน

    Still didn't understand. So hard

  • @akash-kumar737
    @akash-kumar737 5 หลายเดือนก่อน

    These type of question is pure cheating and I am not playing this game.

  • @shynalprasad5508
    @shynalprasad5508 4 หลายเดือนก่อน

    Bruh did you said ladoo?

  • @staywithmeforever
    @staywithmeforever 5 หลายเดือนก่อน

    Just to suffer

  • @akshayiithyd
    @akshayiithyd 5 หลายเดือนก่อน

    I solved it using the hints Leetcode provided, but it gives me a TLE

    • @akshayiithyd
      @akshayiithyd 5 หลายเดือนก่อน

      Okay nevermind, should have done a multisource BFS

  • @juanmacias5922
    @juanmacias5922 5 หลายเดือนก่อน

    Yeah, this being a "medium" is suss AF...

  • @MrWuggles
    @MrWuggles 5 หลายเดือนก่อน

    aint no way this is a medium lol

  • @susdoge3767
    @susdoge3767 5 หลายเดือนก่อน

    i'd just leave this shit

  • @pranalidesai5941
    @pranalidesai5941 3 หลายเดือนก่อน

    fake account and you seem to be using the same voice?

    • @NeetCodeIO
      @NeetCodeIO  3 หลายเดือนก่อน

      No it's still me

  • @aanurraj
    @aanurraj 5 หลายเดือนก่อน

  • @MrSkyS-i5v
    @MrSkyS-i5v 5 หลายเดือนก่อน

    Awwwsome video man