Number of Longest Increasing Subsequence - Dynamic Programming - Leetcode 673 - Python

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

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

  • @fxrcode7923
    @fxrcode7923 2 ปีที่แล้ว +11

    Whenever I can't follow official editorial, I go for Neetcode. You're superb in explain algorithm thought flow, and more importantly, you code according!

  • @avltrees1558
    @avltrees1558 2 ปีที่แล้ว +29

    How is this a medium? It kills me

  • @shenzheng2116
    @shenzheng2116 2 ปีที่แล้ว +19

    This should definitely be a hard question! 😂

  • @johns3641
    @johns3641 3 ปีที่แล้ว +8

    OMG YES NEETCODE!! Thank you for doing solution! Really appreciate it!

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

    I dont do a leetcode question if there is no NeetCode video for it LOL

  • @andrepinto7895
    @andrepinto7895 2 ปีที่แล้ว +5

    There is a O(n logn) solution for this using patience sort but it is considerably trickier to code. Also, you can store a tuple, instead of a list as dp's value.

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

      But i dont think you can get the number of LIS from that, it would be useful for getting the length or to retrieve any one of the LIS

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

    I get so happy when I see your knew vids 😊

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

    Bro, you're the best, and funny 😂 1000 bucks 😂 Keep it up 💪🏻

  • @ankurbhambri9199
    @ankurbhambri9199 3 ปีที่แล้ว +9

    Excellent Explanation Sir Please Keep Going

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

      Thanks and for sure!!

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

      @@NeetCode The explanation was more or less like a 2 pass approach but you did it in one
      pass. Had to think through carefully and walk through the example with the code before getting
      the exact concept of the code.
      Thank you very much.
      You are helping a lot.

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

    Java code for this -
    class Solution {
    public int findNumberOfLIS(int[] nums) {
    int n = nums.length;
    int dp_len[] = new int[n]; //store length of LIS starting at each index
    int dp_count[] = new int[n]; //store count of such LIS of length starting and each index.
    Arrays.fill(dp_len, 1);
    Arrays.fill(dp_count, 1);
    int lenLIS = 0; //length of longest LIS
    int totalCount = 0; //total no. of longest LIS
    for(int i = n - 1; i >= 0; i --) { //start from right to left
    int curMax = 1;
    int curCount = 1;
    for(int j = i + 1; j < n; j ++) { //for each ele, check if LIS exists on right
    if(nums[j] > nums[i]) { //increasing order
    int len = dp_len[j];
    int count = dp_count[j];
    if(len + 1 > curMax) {
    curMax = len + 1;
    curCount = count;
    } else if(len + 1 == curMax) {
    curCount += count;
    }
    }
    }
    if(curMax > lenLIS) {
    lenLIS = curMax;
    totalCount = curCount;
    } else if(curMax == lenLIS) {
    totalCount += curCount;
    }
    dp_len[i] = curMax;
    dp_count[i] = curCount;
    }
    return totalCount;

    }
    }

    • @sadekjn
      @sadekjn ปีที่แล้ว

      nice, but no need to fill the arrays with 1 since we already do that by default if we need to when setting curMax = curCount = 1 and there are no numbers greater than the number after

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

      Thanks, another version:
      class Solution {
      public int findNumberOfLIS(int[] nums) {
      int n = nums.length;
      int[] LIS = new int[n];
      int[] count = new int[n];
      Arrays.fill(LIS, 1);
      int maxLIS = Integer.MIN_VALUE;
      for (int i = n-1; i >= 0; --i) {
      int curMax = 0;
      int curCount = 1;
      for (int j = i+1; j < n; ++j) {
      if (nums[j] > nums[i]) {
      if (LIS[j] > curMax) {
      curMax = LIS[j];
      curCount = count[j];
      } else if (LIS[j] == curMax) {
      //dups produce many LISequences
      //sum up all counters of dups
      curCount+=count[j];
      }
      }
      }
      //dp
      LIS[i] = 1 + curMax;
      count[i] = Math.max(curCount, 1);
      maxLIS = Math.max(maxLIS, LIS[i]);
      }
      int numberOfLIS = 0;
      for (int i = 0; i < count.length; ++i) {
      if (LIS[i] == maxLIS) {
      numberOfLIS+=count[i];
      }
      }
      return numberOfLIS;
      }
      }

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

    Excellent job!

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

    nice explaination and and way you update maxLen and maxCnt in 1 loop, never though of that because I afraid something wrong and not clear, so I do 2 seperated loops.

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

    I hope you’ll be doing Advent of Code solutions in a few days!

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

    Best explanation!! Thank you so much for this!

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

    Woah!!!
    Best explanation!

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

    wow this problem ruined my life

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

    I was doing the same ques !
    but I solved it :) but your explanation makes it crystal clear !

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

    Wonderful explanation. I’m kind of new to problem solving. What’s a cache?

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

    How will look the solution using BIT or segment tree?

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

    Thanks 🙏 I needed this

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

    You github link is not working anymore

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

    couldn’t we solve this in O(n) time? by using the closing window approach and keeping track of the max length and count in a map, then increment that count in the map every time another LIS is found (while we are iterating the array)

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

      nevermind, apparently increasing subsequence does not have to be contiguous in this problem

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

    I love the energy

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

    I love your videos and they are super super helpful!!! However, sometimes I do find your explanations just a little on the wordy side.. the best explanation still though!

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

    can u make video for 332 Reconstruct Itinerary

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

    Appreciated

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

    so I think this is a bottom up dp?

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

    please make a telegram group where we can ask doubt