44 Egg Dropping Problem Memoization

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

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

  • @aryansinha1818
    @aryansinha1818 2 ปีที่แล้ว +20

    The one who droped directly at video 43,
    *When comes why memoization* : Ohh WOW!! nice explaination what a nice guy, no one teaches like that,THE LEGEND!
    *The guy watching from video 1*
    When comes why memoization : Not again.. ahhh. NO.. Ohh lets hear him maybe we could learn something new..... NO NOT AGAIN!!!..

  • @amanbhadauria2881
    @amanbhadauria2881 4 ปีที่แล้ว +152

    those who know why memoization needed start at 12:32

  • @navaugustt
    @navaugustt 2 ปีที่แล้ว +15

    Thanks buddy ❤️ finally i have some command over Dp, Completed the playlist with full enthusiasm and knowledge gathering. #Dpwithadityaverma ❤️

  • @anuragshakya511
    @anuragshakya511 4 ปีที่แล้ว +53

    No Dislikes, I think Tushar Roy has not watched this video yet.

    • @0anant0
      @0anant0 4 ปีที่แล้ว +6

      But I am surprised why the viewership is low (7K vs 20k for most other videos). Maybe ppl already have become experts by watching prev videos :-)

    • @Unknown-Stranger
      @Unknown-Stranger 3 ปีที่แล้ว +6

      kali zuban, ab dislike aagya

    • @tekbssync5727
      @tekbssync5727 3 ปีที่แล้ว +1

      @@0anant0 memoized dhekne ki zarurat nhi if you know recursive 😁

    • @raviashwin1157
      @raviashwin1157 3 ปีที่แล้ว +5

      @@Unknown-Stranger tushar roy ne matlab dekh li video

    • @kumarivandana1554
      @kumarivandana1554 3 ปีที่แล้ว +2

      has watched now and from two ids 😁

  • @0anant0
    @0anant0 4 ปีที่แล้ว +13

    43 of 50 (86%) done! Nice revision of memoization.

  • @adityajain4125
    @adityajain4125 4 ปีที่แล้ว +12

    Can we get the pdf copy of all the pages you write an explanation on? Will be of great help!

  • @ankoor
    @ankoor 4 ปีที่แล้ว +6

    Python: Top Down
    eggs = 3
    floors = 5
    T = [[-1 for _ in range(floors+1)] for _ in range(eggs+1)]
    def Solve(eggs, floors):
    if floors == 0 or floors == 1:
    return floors
    if eggs == 1:
    return floors
    if T[eggs][floors] != -1:
    return T[eggs][floors]
    count = float('inf')
    for i in range(1, floors+1):
    temp = 1 + max(Solve(eggs, floors-i), Solve(eggs-1, i-1))
    count = min(count, temp)
    T[eggs][floors] = count
    return count
    attempts = Solve(eggs, floors)
    print('# of attempts: ', attempts)

  • @voiceRUNdown
    @voiceRUNdown 3 ปีที่แล้ว +1

    Explanation tgda tha bhai 🤝

  • @rituraj6056
    @rituraj6056 3 ปีที่แล้ว +6

    sir,when r u uploading the videoes fibonaci and their 7 variation,grid(14),knade's(6).....?

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

      did you find about these questions ?

  • @princesaini9580
    @princesaini9580 4 ปีที่แล้ว +4

    Bhai Its called Top down apporach. Although Its brilliant the way you explain.

  • @KishankumarPatel
    @KishankumarPatel 2 ปีที่แล้ว

    Even this memorized solution is giving TLE on Leet Code -> Better Solution can be figuring out the max no. of floor that cn be checked with a given no of egg.

  • @dibyanshujaiswal4507
    @dibyanshujaiswal4507 2 ปีที่แล้ว +1

    you should have returned 1 + mn as we are making an attempt to check for future conditions.

  • @madhurbhargava
    @madhurbhargava 4 ปีที่แล้ว +15

    Bro, you need to use binary search in place of your for loop, otherwise this will give TLE on lc despite memoization.

    • @RathourShubham
      @RathourShubham 4 ปีที่แล้ว

      No you should not try binary in egg dropping. It is an another problem, think it over

    • @madhurbhargava
      @madhurbhargava 4 ปีที่แล้ว +4

      @@RathourShubham Bro, say good bye to logic - Did you even tried submitting it on lc?

    • @aayush5474
      @aayush5474 4 ปีที่แล้ว

      it worked on gfg

    • @madhurbhargava
      @madhurbhargava 4 ปีที่แล้ว +1

      @@aayush5474 gfg needs stricter test cases then. Also, if the interviewer asks to optimize this then you are out of options if you CHOOSE not to apply BS.

    • @anjana1700
      @anjana1700 4 ปีที่แล้ว

      Can you please share your code pls?

  • @devashisgupta9853
    @devashisgupta9853 3 ปีที่แล้ว

    why can't we use hashmap like previous videos for memoization

  • @rahulnagwanshi2348
    @rahulnagwanshi2348 3 ปีที่แล้ว +1

    Those two dislikes are from Tushar and Jenny

  • @tanmaysahare2353
    @tanmaysahare2353 2 ปีที่แล้ว +1

    Can anybody tell he is live or not just asking for in some previous videos comments someone says he is not live

  • @ankoor
    @ankoor 4 ปีที่แล้ว +1

    Python: Bottom Up, Time: O(E N^2), Space: O(EN)
    def Solve(eggs, floors):
    # Initialize T with 0's
    T = [[0 for _ in range(floors+1)] for _ in range(eggs+1)]
    # If only 1 egg was given
    for j in range(floors+1):
    T[1][j] = j
    for i in range(2, eggs+1): # From 2 because we do not want to overwrite row index 1 values
    for j in range(1, floors+1):
    T[i][j] = float('inf')
    for k in range(1, j+1):
    count = 1 + max(T[i-1][k-1], T[i][j-k])
    T[i][j] = min(T[i][j], count)
    return T[i][j]
    eggs = 2
    floors = 100
    attempts = Solve(eggs, floors)
    print('Min # of attempts: ', attempts)

    • @thevijayraj34
      @thevijayraj34 4 ปีที่แล้ว

      Are you able to pass the leet code question with this approach?
      leetcode.com/problems/super-egg-drop/submissions/

    • @ankoor
      @ankoor 4 ปีที่แล้ว +1

      @@thevijayraj34 Time Limit Exceeded (TLE)...I modified the solution but still I get TLE...there might be some optimization needed... TLE when 4 eggs and 5000 floors
      class Solution(object):
      def superEggDrop(self, K, N):
      """
      :type K: int
      :type N: int
      :rtype: int
      """
      # Initialize table with infinity
      T = [[float('inf') for _ in range(N+1)] for _ in range(K+1)]
      # Base cases: 0 floor or 1 floor
      for i in range(K+1):
      T[i][0] = 0 # 0 floor -> 0 attempt
      T[i][1] = 1 # 1 floor -> 1 attempt
      # Base cases: 0 egg or 1 egg
      for j in range(N+1):
      T[0][j] = 0 # 0 egg -> 0 attempt
      T[1][j] = j # 1 egg -> check each floor
      for i in range(2, K+1): # Rows: 0 and 1 are base cases
      for j in range(2, N+1): # Cols: 0 and 1 are base cases
      for f in range(1, j+1): # Check 1 to j floors
      count = 1 + max(T[i-1][f-1], T[i][j-f])
      T[i][j] = min(T[i][j], count)
      return T[K][N]

    • @thevijayraj34
      @thevijayraj34 4 ปีที่แล้ว

      @@ankoor There must be a different story for this to solve. Lemme dig it, then we'll discuss about this further.

    • @thevijayraj34
      @thevijayraj34 4 ปีที่แล้ว +1

      @@ankoor Hey bruh.. Just found the efficient way to solve this. Right now we're trying to find no. of attempts using (Floor and Eggs). But we've to do it in other way around. We should look for No. of Floors using (No. of Attempts and Eggs). This goes from min. numbers to big numbers and it resolves the TLE. This is one of the fine problem to solve.

    • @akashtyagi7182
      @akashtyagi7182 4 ปีที่แล้ว

      Code is correct.
      In leetcode same solution in Java get accepted but TLE for python.

  • @arjunkhanna1803
    @arjunkhanna1803 2 ปีที่แล้ว

    Can we use the concept of binary search on answer?

  • @codekro6060
    @codekro6060 3 ปีที่แล้ว +2

  • @TW-uk1xi
    @TW-uk1xi 4 ปีที่แล้ว +2

    What's the time complexity of this optimized code?

    • @LegitGamer2345
      @LegitGamer2345 3 ปีที่แล้ว +4

      N^3 since its a variation of MCM but it can be further optimized to N^2logN by using binary search instead of linear search for k loop , some people have commented the binary search version down below

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

      @@LegitGamer2345 Hey. No.. This is not n^3. The recursive solution is tradition 2^n. (2 choices each). But the memoized code is n*k^2 (n*k is the dp matrix, k is the for loop)

  • @deepanshubhatia1240
    @deepanshubhatia1240 4 ปีที่แล้ว +4

    Bro, I used Recursive and Memoization, However, it is exceeding time limit on LeetCode for Test Case (Egg -> 3, Floors -> 25). I have also memoized inner loop but still getting same issue. Any Help?

    • @Amitkumar-nw5mt
      @Amitkumar-nw5mt 4 ปีที่แล้ว

      you must use dp-optimization to reduce the time(o) ie use tabulation

    • @amiteshanand5157
      @amiteshanand5157 4 ปีที่แล้ว

      try same loop with binary search

    • @adityamahajan1238
      @adityamahajan1238 4 ปีที่แล้ว +4

      Go through this link
      leetcode.com/problems/super-egg-drop/discuss/792736/CPP-Explained-Recursive-greatermemoization-greateroptimization-greaterDP-oror-Well-Explained-oror-Easy-to-unserstand

  • @rkalpeshk
    @rkalpeshk 3 ปีที่แล้ว +1

    JAVA CODE :
    static int funMemo(int egg, int floor, int[][] memo) {
    if (floor == 0 || floor == 1)
    return floor;
    if (egg == 1)
    return floor;
    if (memo[egg][floor] != -1)
    return memo[egg][floor];
    int min = Integer.MAX_VALUE;
    for (int k = 1; k temp)
    min = temp;
    }
    memo[egg][floor] = min;
    return min;
    }

  • @prashuchaudhry
    @prashuchaudhry 2 ปีที่แล้ว

    8:13 why are we using max PLEASE HELP

  • @ppr6429
    @ppr6429 4 ปีที่แล้ว +5

    Can u please add English subtitles to this I am not able to find the English subtitles for this video..I am poor at Hindi

    • @ajitzote6103
      @ajitzote6103 4 ปีที่แล้ว +4

      the real magic is in his hindi though.

  • @Lol.therealdhrxv
    @Lol.therealdhrxv 10 หลายเดือนก่อน

    Code for the same in Java (leetcode) -
    class Solution {
    private int[][] dp = new int[101][10001];
    public Solution() {
    for (int[] row: dp) Arrays.fill(row, -1);
    }
    public int superEggDrop(int k, int n) {
    if (n == 0) return 0;
    if (k == 1) return n;
    if (dp[k][n] != -1) return dp[k][n];
    int ans = Integer.MAX_VALUE;
    for (int i = 1; i

  • @ankitsoni3388
    @ankitsoni3388 4 ปีที่แล้ว +6

    Use binary search instead of linear loop to avoid tle like this,
    code--
    private int helper(int K, int N, int[][] memo) {
    if (N 0) {
    return memo[K][N];
    }

    int low = 1, high = N, result = N;
    while (low < high) {
    int mid = low + (high - low) / 2;
    int left = helper(K - 1, mid - 1, memo);
    int right = helper(K, N - mid, memo);
    result = Math.min(result, Math.max(left, right) + 1);
    if (left == right) {
    break;
    } else if (left < right) {
    low = mid + 1;
    } else {
    high = mid;
    }
    }
    memo[K][N] = result;
    return result;
    }

    • @kumarakash5219
      @kumarakash5219 3 ปีที่แล้ว +2

      I was also thinking of binary search bro

  • @NitishKumar-xt6hc
    @NitishKumar-xt6hc 2 ปีที่แล้ว

    #include
    using namespace std;
    int static t[11][51];
    int solve(int e, int f)
    {
    if (f == 0 || f == 1)
    return f;
    if (e == 1)
    return f;
    if (t[e][f] != -1)
    return t[e][f];
    int mn = INT_MAX;
    for (int k = 1; k as we have to find minimum Number of attempt
    // basically hame worst case ka minimum number of attempt nikalna hai
    mn = min(mn, temp);
    }
    // pahle store and then return
    return t[e][f] = mn;
    }
    int main()
    {
    memset(t, -1, sizeof(t));
    int e = 2;
    int f = 4;
    // op should be 3
    int minTrail = solve(e, f);
    cout

  • @anshulsingh1265
    @anshulsingh1265 2 ปีที่แล้ว +1

    JAVA OPTIMIZE --
    ```
    class Solution
    {
    //Function to find minimum number of attempts needed in
    //order to find the critical floor.
    static HashMap hm = new HashMap();
    static int eggDrop(int n, int k)
    {
    if(k==0 || k==1) return k;
    if(n == 1) return k;
    if(hm.containsKey(n+" "+k)) return hm.get(n+" "+k);
    int min=Integer.MAX_VALUE;
    for(int i=1;i