264. Ugly Number II | Dynamic Programming | Prime factorization | Leetcode POTD Explained

Поділитися
Вставка
  • Опубліковано 29 жов 2024

КОМЕНТАРІ • 13

  • @codeby_naruto
    @codeby_naruto  2 місяці тому

    Code :-
    class Solution {
    public:
    int nthUglyNumber(int n) {
    vector results (1,1);
    int i = 0, j = 0, k = 0;
    while (results.size() < n)
    {
    results.push_back(min(results[i] * 2, min(results[j] * 3, results[k] * 5)));
    if (results.back() == results[i] * 2) ++i;
    if (results.back() == results[j] * 3) ++j;
    if (results.back() == results[k] * 5) ++k;
    }
    return results.back();
    }
    };

  • @shreyasyaduvanshi
    @shreyasyaduvanshi 2 місяці тому +1

    cool approach , thanks for sharing. Gd explanation.

  • @shubhamjaiswal7645
    @shubhamjaiswal7645 2 місяці тому +1

    bhai ...ek number ....soo good solution

  • @rightledgeai
    @rightledgeai 2 місяці тому +1

    Please do this everyday for at least 3 months I'll watch everyday

  • @chad._life
    @chad._life 2 місяці тому +1

    good explaination

  • @motivationalshorts393
    @motivationalshorts393 2 місяці тому +2

    can you help to provide the solution of ugly number III ?
    why this approach failed in III

    • @codeby_naruto
      @codeby_naruto  2 місяці тому

      Yes, I will provide a video with a brief explanation and upload it .
      This approach does not work for Ill because in II 2, 3, and 5 are prime numbers and not divisible by any other numbers.
      However, in Ill, a, b, and c can be any numbers.
      For example, consider 10 with a = 2, b = 11, and c = 13. 10 is divisible by 2, but pow(2,x) != 10, so we cannot represent 10 in the form 2^a *11^b* 13^c.
      Therefore, 10 is considered an "ugly" number because it is divisible by 2.

  • @ankit8269
    @ankit8269 2 місяці тому +1

    i did this question after 2.5 hours hard analysis 😓😓

    • @codeby_naruto
      @codeby_naruto  2 місяці тому

      No problem. Learn it, and remember this concept next time. ✌️