Digit DP: Digit Sum | SPOJ

Поделиться
HTML-код
  • Опубликовано: 5 ноя 2024

Комментарии • 44

  • @sarthaksharma6973
    @sarthaksharma6973 2 года назад +7

    Sir please don't stop making videos. Like other RUclipsrs I know you don't demand subscribers like such but your content is gold and one day people will come to know who is the best just keep moving forward. Educational content grows slowly on yt but it is really what change people's life.

  • @iamcrypticcoder
    @iamcrypticcoder 2 года назад +1

    Nice explanation. Regarding the cnt() method. we can use following simplified imeplementation.
    LL cnt(string num, int n, int tight) {
    if (tight == 0) return (pow(10, n) + 0.1);
    if (n == 0) return 1;
    return stoll(num.substr(num.length()-n)) + 1;
    }

    • @ishmam8643
      @ishmam8643 2 года назад

      We can also recursively count all the valid solutions and multiply them :)
      string s;
      ll dp[20][2], ans;
      ll cdp[20][2];
      ll cnt(int i, int j) {
      if (i == s.size()) return 1;
      ll& ret = cdp[i][j];
      if (~ret) return ret;
      ret = 0;
      int lim = (j ? s[i] - '0' : 9);
      for (int k = 0; k

    • @anuchan-l7y
      @anuchan-l7y 4 месяца назад

      Noicee

  • @mdsourav2073
    @mdsourav2073 2 года назад

    Your Teaching way is Extraordinary vaia,,
    Plz keep making videos.

  • @abhinav4095
    @abhinav4095 Год назад +2

    I messed up all digit dp problems during my campus placements...didn't even know how to solve them without brute force.Thank you for providing explanations!!I'm enjoying your videos a lot

  • @nitindwivedi4074
    @nitindwivedi4074 Год назад

    The Best Digit DP Series, I can say

  • @uptonogood300
    @uptonogood300 2 года назад +3

    Alternate simpler approach we can just keep Sum of digit as parameter ,this way we don't need a separate count function
    ll helper(string &r, int sum=0,int digit=0,bool flag=true)
    {
    if(digit==r.size())
    {
    return sum;
    }
    if(dp[digit][sum][flag]!=-1)
    {
    return dp[digit][sum][flag];
    }
    int tight=(flag)? r[digit]-'0': 9;
    ll k=0;
    for(int i=0;i

    • @abhinavtiwari-7
      @abhinavtiwari-7 Месяц назад

      Thank you

    • @keerthireddy8074
      @keerthireddy8074 Месяц назад

      but how will you initialise the dp table in such a case? Sum variable can go to really high numbers

  • @tonystarc9567
    @tonystarc9567 3 года назад +2

    Thank you brother for everything you are doing... Please one request is do graphs and Union Find problems from Leetcode most frequently asked questions

  • @himalayadebbarma-we4pt
    @himalayadebbarma-we4pt Месяц назад

    Thanks Kartik

  • @neerajpandey8159
    @neerajpandey8159 3 года назад +3

    bhaiya at coder k dp problem set ki series baana do .ache questions h.plzzzzz

  • @piyushlohiya1977
    @piyushlohiya1977 2 года назад

    nice approach

  • @ankitmindewar3710
    @ankitmindewar3710 3 года назад +1

    its great, continue.

  • @vishnuvardhan-md1ux
    @vishnuvardhan-md1ux 3 года назад +7

    Great explanation Bhaiya, but i have one question, while generating cnt(x) if tight ==1 why are you again using recursion instead of taking last n-1 digits? for example our range is 9863
    and we kept our first digit as 9 and since 9 is the maximum we can place in the first digit we need to know how many time 9 gets generated for that i can directly return 863 right if tight==0 then no issues i can return pow(10,n-1)? instead of doing recursion.

    • @AlgosWithKartik
      @AlgosWithKartik  3 года назад +8

      Hey Vishnu, nice observation.
      You are right if tight is 1 and number is 863, we can directly return 863+1(000, 001, 002, ... , 863).
      You can definitely solve it this way too. For me recursion seemed more consistent with the theme of the series digit dp.
      Also imagine if the number was 10000 digit long and you had to return cnt(x) % M.
      Hope that helps :)

    • @vishnuvardhan-md1ux
      @vishnuvardhan-md1ux 3 года назад +3

      @@AlgosWithKartik Thanks Bhaiya.

  • @gunjanbiswas925
    @gunjanbiswas925 3 года назад

    awesome content bhaiya

  • @ananya8740
    @ananya8740 3 года назад +1

    can we memoize the count function ?????

  • @harinath_mishra
    @harinath_mishra 3 года назад +2

    Sir is there way to get editorials of spoj problems just like codeforces.

    • @AlgosWithKartik
      @AlgosWithKartik  3 года назад

      The best I can think of is to google search for "spoj problem code editorial"

  • @technophylic3321
    @technophylic3321 3 года назад +2

    Sir please make a video on merge interval dp problems... ASAP, thank you for contributing it is very very helpful.

    • @AlgosWithKartik
      @AlgosWithKartik  3 года назад +2

      can you share an example problem which uses this technique?

    • @technophylic3321
      @technophylic3321 3 года назад +1

      @@AlgosWithKartik and sir also the blog which you shared regarding dp patterns there are many more examples of these problems, such bust balloon etc.

  • @beingnikhil155
    @beingnikhil155 3 года назад

    got ac first and then watched the video;)

  • @mayankverma3690
    @mayankverma3690 3 года назад +1

    Sir don't worry soon You will have too many subscribers :)

  • @fardeen2158
    @fardeen2158 3 года назад +3

    Thanku so much . Can you please start series for Graphs Algo ,Stl . i saw playist it have one video only for graph .By the way Your doing really amazing job bhaiya/

    • @AlgosWithKartik
      @AlgosWithKartik  3 года назад +1

      Thanks Fardeen!
      Yes I will try to start a series for graph sometime soon

  • @hymnish_you
    @hymnish_you 2 года назад

    Hey Bro, A silly doubt. Can we apply dp to postorder in an case? I meant, if rather than calculating count separately, can we directly send sum in next call postorder?
    Here is an example...
    ```
    private static long helper(long num, int x, boolean isTight, long sum){
    if(x == Long.toString(num).length()) return sum;
    int lb = 0;
    int ub = isTight ? Long.toString(num).charAt(x)-'0' : 9;
    long res = 0;
    for(int i = lb; i

    • @AlgosWithKartik
      @AlgosWithKartik  2 года назад +1

      Try it out :) will need to watch my solution or think of it again before answering your query

    • @hymnish_you
      @hymnish_you 2 года назад +1

      @@AlgosWithKartik No problem, I will try... While I always have hard time at making use of dp in postorder. And its always like we store values in preorder. Anyways thanks!!

  • @qazaqempire2446
    @qazaqempire2446 2 года назад

    what is the level of difficulty here? is this problem hard?

  • @anubhavsethi3416
    @anubhavsethi3416 3 года назад

    which tool u r using for writing on screen ??

  • @kannekantimanoj431
    @kannekantimanoj431 3 года назад

    Is it really a dp problem, i feel like no sub problems will be repeated.

  • @ManishaKumari-yv3nf
    @ManishaKumari-yv3nf 3 года назад +2

    First comment

  • @RajatKumar-yi2cf
    @RajatKumar-yi2cf 3 года назад

    Second comment 🔥

  • @em_nikhil_007
    @em_nikhil_007 3 года назад +1

    7th comment 😂

  • @kuanbibadminton
    @kuanbibadminton 2 года назад

    Can't hear your English:(