Maximum Swap | 4 techniques | Leetcode 670

Поділитися
Вставка
  • Опубліковано 21 жов 2024
  • This video explains about the maximum swap problem using four techniques. The first technique is using bruteforce then using optimal 2 pass approach then using selection algorithm and finally using greedy algorithm which is the most optimal using only one pass.
    ----------------------------------------------------------------------------------------------------------------------------------------------------------------
    🟣 JOIN our 𝐋𝐈𝐕𝐄 𝐢𝐧𝐭𝐞𝐫𝐯𝐢𝐞𝐰 𝐭𝐫𝐚𝐢𝐧𝐢𝐧𝐠 𝐩𝐫𝐨𝐠𝐫𝐚𝐦 through whatsapp query: +91 8918633037
    🟣 𝐂𝐡𝐞𝐜𝐤𝐨𝐮𝐭 𝐚𝐥𝐥 𝐨𝐮𝐫 𝐂𝐨𝐮𝐫𝐬𝐞𝐬: techdose.co.in/
    🔵 LinkedIn: / surya-pratap-kahar
    🔴 INSTAGRAM: / techdose_official
    🟢 𝐓𝐞𝐜𝐡𝐝𝐨𝐬𝐞-𝟏𝟎𝟎 𝐬𝐡𝐞𝐞𝐭: docs.google.co...
    ---------------------------------------------------------------------------------------------------------------------------------------------------------------
    Largest number formed from an array: • Largest number formed ...
    𝐂𝐎𝐃𝐄 𝐋𝐈𝐍𝐊: gist.github.co...

КОМЕНТАРІ • 28

  • @BananaButcher
    @BananaButcher 4 дні тому

    The best part of this video is when he also says you can watch that video. which makes me easier to relate it with other problems. thanks for your work sir.

    • @techdose4u
      @techdose4u  4 дні тому +1

      Thanks! Glad you found it helpful.

  • @anmolyadav9112
    @anmolyadav9112 4 дні тому

    best YT channel of DSA problem solving

    • @techdose4u
      @techdose4u  4 дні тому

      thanks for your appreciation :)

  • @rishusingh.
    @rishusingh. 4 дні тому +1

    Best solution so far

  • @sailendrachettri8521
    @sailendrachettri8521 4 дні тому +1

    Thank You SirJi :)

  • @akriteesrivastava
    @akriteesrivastava 4 дні тому

    Thankyou so much your video are helping me a lot

  • @tindo0038
    @tindo0038 День тому

    you are the goat

  • @RakeshChanda21
    @RakeshChanda21 3 дні тому

    time complexity is O(N^2) for first 2 approaches 2 parse and selection sort

  • @SaiDharahasReddyIndrakanti
    @SaiDharahasReddyIndrakanti 4 дні тому

    can we use montonic decreasing stack?

    • @techdose4u
      @techdose4u  4 дні тому

      you may have to try some tricks because if you see a lower value in future and pop then its wrong (L to R).
      (R to L) decreasing stack again may not work as highest weight digits may have high value as compared to others. So, I wont use it

  • @Lucifer-xt7un
    @Lucifer-xt7un 4 дні тому

    Will you please make a video about your dsa course so that it will give clarity whether to join or not as im very intrested but no proper guide is there about your course.

    • @techdose4u
      @techdose4u  4 дні тому

      I will make a private video and share on whatsapp. I hope you have queried us on whatsapp.

    • @Lucifer-xt7un
      @Lucifer-xt7un 4 дні тому

      @@techdose4u yes sir

  • @sajan_kharat
    @sajan_kharat 3 дні тому

    I am not able to do any questions or projects. I understand everything when I watch the tutorial. I feel I am stuck in tutorial hell. Please help me

    • @techdose4u
      @techdose4u  3 дні тому

      UA-cam videos are only for you to solve one particular problem but cant help you with everything.
      You can ping us on whatsapp to assist you.

  • @mageshmcc
    @mageshmcc 3 дні тому

    The simplest answer is here, happy to explain if you need help :))
    class Solution:
    def maximumSwap(self, num: int) -> int:
    num_list = [int(i) for i in str(num)]
    max_iter = len(num_list) - 1
    num_dict = {}
    for i, x in enumerate(num_list):
    if x not in num_dict:
    num_dict[x] = [i]
    else:
    num_dict[x].append(i)
    i = 0
    while i < max_iter:
    tmp_list = num_list[i::]
    small = min(tmp_list)
    big = max(tmp_list)
    if num_list[i] == big:
    pass
    else:
    ith_elm = num_list[i]
    num_list[i] = big
    num_list[num_dict[big][-1]] = ith_elm
    break
    i += 1
    output_num = 0
    digit_place = 10**max_iter
    for i in num_list:
    output_num += digit_place * i
    digit_place //= 10
    return output_num

  • @RiteshKumar-IIT_KGP
    @RiteshKumar-IIT_KGP 4 дні тому

    Your 2nd Method fails on test case for num=1993 🤡

    • @techdose4u
      @techdose4u  4 дні тому +1

      I think you can make the changes taking selection technique into consideration and playing with equality :)

    • @RiteshKumar-IIT_KGP
      @RiteshKumar-IIT_KGP 4 дні тому

      @@techdose4u if i would play with equality then another case num=98368 will fail 🤷

    • @spetsnaz_2
      @spetsnaz_2 4 дні тому +1

      Mine got accepted. You might be doing something wrong. Code for your reference :
      class Solution {
      public:
      int maximumSwap(int num) {
      string s = to_string(num);
      int n = s.size();
      vector rightMax(n);
      rightMax[n-1] = n-1;
      for(int i=n-2; i>=0; i--) {
      if(s[i] > s[rightMax[i+1]])
      rightMax[i] = i;
      else
      rightMax[i] = rightMax[i+1];
      }
      for(int i=0; i

    • @RiteshKumar-IIT_KGP
      @RiteshKumar-IIT_KGP 4 дні тому

      @@techdose4u yeah u r right,ur 2nd method also worked.....
      int maximumSwap(int num) {

      vectorno;
      int t=num;
      while(t!=0){
      no.push_back(t%10);
      t=t/10;
      }
      reverse(no.begin(),no.end());
      vectorind(no.size());
      int n=no.size();
      int maxi=INT_MIN;
      int max_ind=n-1;
      for(int i=n-1;i>=0;i--){
      if(no[i]>maxi){
      ind[i]=i;
      max_ind=i;
      maxi=no[i];
      }
      else if(no[i]