Minimum Increment to Make Array Unique - Leetcode 945 - Python

Поділитися
Вставка
  • Опубліковано 7 січ 2025

КОМЕНТАРІ • 41

  • @varunpalsingh3822
    @varunpalsingh3822 6 місяців тому +6

    Neet can you post a video solution on "Leetcode 987. Vertical Order Traversal of a Binary Tree" question

  • @SayanChowdhury-f1h
    @SayanChowdhury-f1h 6 місяців тому

    This is the second solution
    int minIncrementForUnique(vector& nums) {
    mapm;
    for(auto it:nums)
    m[it]++;
    int n=nums.size(),count=0;
    while(m.size()1)
    {
    int key=it.first;
    int temp=key;
    m[key]--;
    temp++;
    m[temp]++;
    count++;
    break;
    }
    }
    }
    return count;

    }

  • @PrakashLight-vs1nj
    @PrakashLight-vs1nj 6 місяців тому +1

    For the max value we can just add (n*(n-1) /2 in the result where n is the count of the max value

  • @swastiktiwari7066
    @swastiktiwari7066 6 місяців тому +1

    In second solution : range(min(nums),max(nums)+len(nums)) instead of range(0,max(nums)+len(nums)).....min and max can be calculated simultaneously in single pass first

  • @saki-ch3rt
    @saki-ch3rt 6 місяців тому

    You can just iterate to the max(nums)
    Let carry = count[max(nums)]
    if carry > 1, then add carry*(carry -1)/2 to the result
    Here is my C++ solution
    class Solution {
    public:
    int minIncrementForUnique(vector& nums) {
    vector counter(1e5 + 1);
    for (const auto& x : nums) {
    ++counter[x];
    }
    int carry = 0;
    int res = 0;
    for (int x = 0; x < counter.size(); ++x) {
    counter[x] += carry;
    carry = max(counter[x] - 1, 0);
    res += carry;
    }
    res += max(carry - 1, 0) * carry / 2;
    return res;
    }
    };

  • @imPriyansh77
    @imPriyansh77 6 місяців тому

    Initially, I thought of using Next Greater to Left & Next Greater to Right concepts, & did dry run as per below written code for the test cases, [1,2,2], [3,2,1,2,1,7] & [4,4,4,4,4]. According to the dry run, I got the correct answers, but when I run the test cases on leetcode, I get correct output for [1,2,2], but for [3,2,1,2,1,7] & [4,4,4,4,4], it shows output of 4, which is wrong. My dry run (as per the same code) yielded correct answer, but on running on platform, it fails. Can anyone help me address this issue ? The code is :
    class Solution {
    private:
    vector NGR(vector& arr, int n){
    stack st;
    vector result(n, -1);
    for (int i = n - 1; i >= 0; i--) {
    while (!st.empty() && st.top()

  • @pat777b
    @pat777b 6 місяців тому

    To get an O(n) solution, you can also do count sort instead of python's built in sort method in your first solution. Imo, this is easier to understand than your second solution even if it may take more lines of code.
    this is my python solution.
    class Solution:
    def minIncrementForUnique(self, nums: List[int]) -> int:
    large = max(nums)
    small = min(nums)
    counts = Counter(nums)
    nums = []
    for i in range(small, large +1):
    for j in range(counts[i]):
    nums.append(i)
    ans = 0
    minimum = nums[0]
    for i in range(len(nums)):
    if nums[i] < minimum:
    ans += minimum - nums[i]
    minimum += 1
    else:
    minimum = nums[i] + 1
    return ans

    • @turskaah
      @turskaah 6 місяців тому

      using a counter makes the complexity O(n log n) though

    • @pat777b
      @pat777b 6 місяців тому

      @@turskaah what, neetcode also uses a counter in his O(n) solution.

  • @freecourseplatformenglish2829
    @freecourseplatformenglish2829 6 місяців тому +17

    I take NlogN solution. 2nd solution is what discourage beginners because it is really hard to come up with 2nd solution.

    • @pastori2672
      @pastori2672 6 місяців тому +4

      it really isnt especially because of the context of the previous problems that were all solvable using counting sort

    • @xenomorphisisdilage472
      @xenomorphisisdilage472 6 місяців тому +1

      It's not that hard. I had first tried to use a similar approach but had gotten stuck on where to go next from defining dicts, etc. Probably because I was doing it all in my head, should keep a pen and paper next time.

  • @dmitriytereshchenko2032
    @dmitriytereshchenko2032 6 місяців тому

    if we take the first solution, but use radix sort, will the solution improve to O(n + d), where n - length array, d - max number

  • @nirmalgurjar8181
    @nirmalgurjar8181 6 місяців тому +1

    Only catch of this problem is how you prove if value at i is less than value i - 1 means there was a duplicate.

    • @yajasmalhotra9011
      @yajasmalhotra9011 5 місяців тому

      For a sorted array, by default any element to the left would be less than an element to the right. If an element to the left is greater, that means it's original value was already in the array so it had to be incremented to the point where it is either equal or greater than the element to the right.

  • @aashishbathe
    @aashishbathe 6 місяців тому +1

    That is a brilliant solution..

  • @adama7752
    @adama7752 6 місяців тому +1

    Isn't just iterating !empty() over a hash map while you remove any with counts of 1 better? Then its O(n) and not O(N+max)

  • @vijethkashyap151
    @vijethkashyap151 6 місяців тому +1

    BEST EVER!!!!!!!!!!!!!!!!

  • @atg878
    @atg878 6 місяців тому

    why hash set is failed here?

  • @vigneshs1852
    @vigneshs1852 6 місяців тому +2

    class Solution {
    public int minIncrementForUnique(int[] nums) {
    Arrays.sort(nums);
    int res=0;
    int length=nums.length;
    int i=0;
    int j=1;
    while(j < length)
    {
    if(nums[i] >= nums[j])
    {
    int before=nums[j];
    nums[j]=nums[i]+1;
    res=res+nums[j]-before;

    }
    ++i;
    ++j;
    }
    return res;
    }
    }
    1st solution with different approach

  • @stephan24297
    @stephan24297 6 місяців тому

    It takes a lot of skill to not only conjur the idea but to bring it into code. I thought of the second solution as in how to fill in the gap but was stuck on how to iterate from key to key in a map.

    • @Vancha112
      @Vancha112 6 місяців тому

      I came up with the second solution when my naive solution timed out. I managed to finally pass the time constraint, but looking at the code in the video i see that i solved a buch of problems with my code very inefficiently:
      Neetcode: 8 lines of code
      Me: me 28 lines of code -.-
      Same basic algorithm

  • @gokuanisantiogan
    @gokuanisantiogan 6 місяців тому

    Why do we have to iterate n+max times? Can't we just iterate until the max number? Knowing that there's no more elements bigger than this one we can just calculate the triangle number of the remaining duplicates on one operation.
    Something like this:
    class Solution:
    def minIncrementForUnique(self, nums: List[int]) -> int:
    count = Counter(nums)
    res = 0
    for i in range(max(nums)):
    if count[i] > 1:
    extra = count[i] - 1
    count[i + 1] += extra
    res += extra
    n = count[max(nums)]-1
    res+= n * (n +1)/2
    return res

  • @AhmedAmer-se1mr
    @AhmedAmer-se1mr 6 місяців тому +1

    I came with the optimal solution easily, but I still must watch your videos

  • @melikpetit
    @melikpetit 6 місяців тому

    how do u know all of this

  • @mikehan47
    @mikehan47 6 місяців тому

    So clear thx bro

  • @sidazhong2019
    @sidazhong2019 5 місяців тому

    Interesting, The clever way is less efficient.

  • @ivan.legostin
    @ivan.legostin 6 місяців тому +3

    Can someone explain to me the time complexity for the first solution ?
    My guess is it takes N*logN from sorting and N when we iterate the array

    • @PalakKalsi
      @PalakKalsi 6 місяців тому +1

      That's correct.

    • @TalhaTabrez7
      @TalhaTabrez7 6 місяців тому

      Yes

    • @imPriyansh77
      @imPriyansh77 6 місяців тому

      You're right.

    • @fieworjohn5697
      @fieworjohn5697 6 місяців тому

      nlogn is the most significant so you can ignore the other n from iteration and just say the time complexity is nlogn

  • @chuckle_pugz96
    @chuckle_pugz96 6 місяців тому

    In the second solution, I added this line inside the for loop, which makes it a bit faster:
    if i>max(nums) and count[i]==0:
    break

  • @shantipriya370
    @shantipriya370 5 місяців тому

    thanks

  • @chien-yuyeh9386
    @chien-yuyeh9386 6 місяців тому

    First!!🎉

  • @TechieClasher
    @TechieClasher 6 місяців тому +2

    did both by myself 🤧🤧

  • @aashishbathe
    @aashishbathe 6 місяців тому

    9:54 exrta 🤣🤣🤣🤣

  • @evalyly9313
    @evalyly9313 6 місяців тому

    Finally I am first comment!❤🎉🎉

  • @NursultanBegaliev
    @NursultanBegaliev 6 місяців тому