Make Lexicographically Smallest Array by Swapping Elements | Leetcode 2948

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

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

  • @freecourseplatformenglish2829
    @freecourseplatformenglish2829 9 дней назад +6

    Seriously it is hard to beat you when its comes to explaining the intution. Thank you

    • @techdose4u
      @techdose4u  9 дней назад +1

      By God’s grace 🙏🏼

  • @abhishekshukla5747
    @abhishekshukla5747 9 дней назад +9

    This problem was tough , not easy to get the intution if solving for first time !
    But your explainantion made it crystal clear sir

  • @az-zm4ji
    @az-zm4ji 9 дней назад +2

    WHAT A GREAT DUDE! I was able to come up wth the DSU approach but didnt code it as it looked too diffcult. This appraoch is just genius.

  • @BCS-IshtiyakAhmadKhan
    @BCS-IshtiyakAhmadKhan 9 дней назад

    Best way to do this problem is to sort the array in decreasing order and maintain a adjacency list of groups .Now the elements in the adjacency list will also be sorted in decreasing fashion and as you travel in original array find the current element group(using hash)and remove the last element from that group in the adjacency list which will take O(1) time .Here's the code
    int n = nums.size();
    vector v(nums);
    sort(v.begin(), v.end(), greater());
    vector dsu(1);
    map mp;
    dsu[0].push_back(v[0]);
    mp[v[0]] = 0;
    int curr = 0;
    for (int i = 1; i < n; i++) {
    if (v[i - 1] - v[i] > limit) {
    dsu.emplace_back();
    curr++;
    }
    dsu[curr].push_back(v[i]);
    mp[v[i]] = curr;
    }
    for (int i = 0; i < n; i++) {
    nums[i] = dsu[mp[nums[i]]].back();
    dsu[mp[nums[i]]].pop_back();
    }
    return nums;

  • @heatedmuscle2329
    @heatedmuscle2329 9 дней назад +1

    You are so smart. How are you so smart. I really really want to know. Thanks for providing this insight. Commented, Liked and Subscribed,

  • @virajmandlik-it9bj
    @virajmandlik-it9bj 9 дней назад

    Insightful Explanation ....Thanks Sir..💯

  • @anantakesharipanda4085
    @anantakesharipanda4085 9 дней назад

    Instead of using two pointers to build the result vector, I was thinking of creating the result vectors of size nums by initializing it with zeroes, then create a map of groups where each Key will be the group index, and value will be vector of sorted indices from original vector. Then just traverse the sorted copy vector and place each element within same group within the index from the map’s values into the result vector initialised with zero.
    I know this not so space efficient, but this approach seemed more easy to visualise to me. 😅

  • @programming6177
    @programming6177 9 дней назад

    nice explanation, thank you bro😇

  • @81_monish_roy74
    @81_monish_roy74 7 дней назад

    Good explanation sir!

  • @UMESAURAVKUMAR
    @UMESAURAVKUMAR 9 дней назад +2

    The selection sort algo will break here:
    {10,3,5,8,2} , limit = 3
    after first iteration: best value for 10 is 8
    {8, 3, 5, 10, 2}
    after second iter: best value for 3
    {8, 2,5,10,3}
    after 3rd iter: best value for 5 is 3
    {8,2,3,10,5}
    after 4th iter: best value for 10 is no one in right
    {8,2,3,10,5}
    but the actual answer for this problem is : {2,3,5,8,10}

    • @techdose4u
      @techdose4u  9 дней назад +1

      Yes.
      One extra loop would be required if elements are in increasing order and we always happen to pick from right.

  • @nabihsara
    @nabihsara 9 дней назад

    Thanks, and i was waiting you yesterday but you have solved late, I wish you solve every day early and thanks again for your effort and explanation

    • @techdose4u
      @techdose4u  9 дней назад +1

      I never solve the same day.
      I solve days before and predict the question to come.
      Yesterday’s prediction dint work out.
      Hence it was late :(

    • @challengemania4447
      @challengemania4447 9 дней назад

      ​@@techdose4u but how sir.. Do you have any ML algo to predict next question ?

    • @rameshgoud2986
      @rameshgoud2986 9 дней назад

      @@challengemania4447 😂😂😂

  • @bhashitmaheshwari5205
    @bhashitmaheshwari5205 9 дней назад +2

    Just a Suggestion try to give Brute Better and Optimal Sol for Problems might Help us in Interviews,Thank You

    • @techdose4u
      @techdose4u  9 дней назад +1

      already explained selection sort right ?

    • @bhashitmaheshwari5205
      @bhashitmaheshwari5205 9 дней назад +2

      @techdose4u yeah sure today you explained it I was talking in general sorry if i offended you in someway.

    • @techdose4u
      @techdose4u  9 дней назад

      @bhashitmaheshwari5205 No problem. I generally expect people to know bruteforce as its just simulation of goal :)

  • @BananaButcher
    @BananaButcher 9 дней назад

    Excellent explanation as always

  • @shivnathkahar8985
    @shivnathkahar8985 9 дней назад +1

    Excellent

  • @Ice-2706
    @Ice-2706 8 дней назад

    the custom selection sort would not work for every case.
    ex - [10, 4, 7], limit = 3
    although the idea of swappable groups is awesome!

    • @techdose4u
      @techdose4u  8 дней назад

      yes it will require an extra loop

  • @sailendrachettri8521
    @sailendrachettri8521 9 дней назад

    Thank you sir :)

  • @vbhv-gupta
    @vbhv-gupta 9 дней назад

    Time complexity of its Brute Force approach will be O(n^3)
    class Solution:
    def lexicographicallySmallestArray(self, nums, limit) :
    copy = None
    while copy != nums: # -> Loop 1
    copy = nums.copy()
    for i in range(len(nums)): # -> Loop 2
    best_idx = i
    for j in range(i+1,len(nums)): #-> Loop 3
    if 0 < nums[i] - nums[j]

    • @techdose4u
      @techdose4u  9 дней назад

      yes

    • @challengemania4447
      @challengemania4447 9 дней назад

      Can we able to do it in O(n^2).. I think we can do as usual comparing one element with each other.
      Will be this approach is brutefoecr ?

    • @rameshgoud2986
      @rameshgoud2986 9 дней назад

      @@challengemania4447 no wont work

  • @sruthinteja4644
    @sruthinteja4644 9 дней назад

    How to do using unionfind

    • @techdose4u
      @techdose4u  9 дней назад +1

      Maintain multiple sets using disjojnt set array. Please follow my disjoint set video for clarity.

  • @kshitijvarma2566
    @kshitijvarma2566 9 дней назад

    This was hard to think of .... 😅

  • @kthamim7552
    @kthamim7552 9 дней назад

    you are sorting a copy array as normal sort but why sir you are saying,you'r done a lexicographically sorting a copy array
    bit confusing in that line
    after finding a logic there is no doubt one is that you are using a normal sorting concept or anyother lexico sorts ?

    • @techdose4u
      @techdose4u  9 дней назад

      I dint understand your problem.
      Can you please mention a little more clearly possibly with example.

  • @hhcdghjjgsdrt235
    @hhcdghjjgsdrt235 9 дней назад +1

    10, 8 were green
    rest were yellow
    the idea clicked