Minimize Maximum of Array - Leetcode 2439 - Python

SdĂ­let
VloĆŸit
  • čas pƙidĂĄn 23. 07. 2024
  • 🚀 neetcode.io/ - A better way to prepare for Coding Interviews
    Solving Leetcode 2439 - Minimize Maximum of Array, today's daily leetcode problem on April 4th.
    đŸ„· Discord: / discord
    🐩 Twitter: / neetcode1
    🐼 Support the channel: / neetcode
    ⭐ BLIND-75 PLAYLIST: ‱ Two Sum - Leetcode 1 -...
    💡 DYNAMIC PROGRAMMING PLAYLIST: ‱ House Robber - Leetco...
    Problem Link: leetcode.com/problems/minimiz...
    0:00 - Read the problem
    0:30 - Drawing Explanation
    7:55 - Coding Explanation
    leetcode 2439
    #neetcode #leetcode #python

Komentáƙe • 49

  • @abhayphanse9509
    @abhayphanse9509 Pƙed rokem +62

    I have almost 400 questions on leetcode now and still got stuck at this , it is in my opinion a one off problem (it is what it is). Dont be discouraged if not able to solve this , keep grinding . We always have neetcode to help us out

    • @16avikasgupta70
      @16avikasgupta70 Pƙed rokem +5

      Same I have also solved around 300 and was stuck how to approach andin mind i was stuck with max heap approach

    • @Cygx
      @Cygx Pƙed rokem

      Brilliant solution and explanation!

    • @annoyingorange90
      @annoyingorange90 Pƙed rokem

      im on 600 and still shit bro lol

    • @minhdang5132
      @minhdang5132 Pƙed rokem

      > 500 here, and still cannot solve it

    • @tarnished3571
      @tarnished3571 Pƙed rokem +1

      550 and also got stuck at this problem

  • @NitinSingh-yc7rg
    @NitinSingh-yc7rg Pƙed rokem +11

    he came when we needed the most

  • @akashsardar495
    @akashsardar495 Pƙed rokem +4

    I got upset when I could not come up with a Binary search approach as mentioned in the Hint section of the problem but after watching the first 3-4 mins of your video I was able to solve it. You are really great at breaking down complicated problem statements in their lucidest form đŸ™đŸ»đŸ™đŸ»Thank u NeetcodeIO.

  • @MP-ny3ep
    @MP-ny3ep Pƙed rokem +4

    Thank you so much for the daily leetcode problems ! Helps a lot !!! Keep it up

  • @scresat
    @scresat Pƙed rokem +5

    with every new problem, I'm getting intuition if I will be able to solve it on my own or not.
    For this one, I came straight to you without writing a single thing on paper.

  • @akifahmed9610
    @akifahmed9610 Pƙed rokem +1

    You have explained the solution so simply. Keep up the good work

  • @netanelkomm5636
    @netanelkomm5636 Pƙed rokem

    Thank you so much, I actually thought of this idea but couldn't verify it for some reason myself. I watched your video and realized it was the same idea, so I implemented it and it worked.
    New sub!

  • @yoonkeunkoh9162
    @yoonkeunkoh9162 Pƙed rokem

    You are amazing.
    You explain this problem as if explaining to 7 years old student.
    Great Video

  • @ShivangiSingh-wc3gk
    @ShivangiSingh-wc3gk Pƙed měsĂ­cem

    Great solution I was thinking the heap way and driving myself crazy. This is so elegant

  • @ayushdhiman9378
    @ayushdhiman9378 Pƙed rokem

    Only video that helped me understand the problem fully thank you !!❀❀

  • @effyzhang1281
    @effyzhang1281 Pƙed rokem +2

    I don't even understand the question to begin with, thanks so much for the video!!!

  • @mirkelor
    @mirkelor Pƙed rokem

    After listening 2 min of your video, suddenly the solution occurred in my mind and I continued. It was precisely the same as yours. Thank you for clarifying as best as every time

  • @mrmcpherson2722
    @mrmcpherson2722 Pƙed rokem

    Right on time! Thank you!

  • @krateskim4169
    @krateskim4169 Pƙed rokem

    how did you even come with this type of intuition, its amazing

  • @HoanNguyen-fc8vb
    @HoanNguyen-fc8vb Pƙed rokem

    Very nice! Thanks !

  • @bobbajahnavi7540
    @bobbajahnavi7540 Pƙed rokem

    thank you so much for the vedio it helps me a lot........

  • @mostafaahmed-fj8ji
    @mostafaahmed-fj8ji Pƙed rokem

    good work , thank you

  • @adityamangla4794
    @adityamangla4794 Pƙed 6 dny

    How are you dividing the total by the length of the array? We can't just equally distribute the values right?

  • @user-xu4qv9mt7e
    @user-xu4qv9mt7e Pƙed rokem

    Really good video

  • @nisaacdz
    @nisaacdz Pƙed rokem

    thanks

  • @fane1159
    @fane1159 Pƙed rokem

    please also solve this using binary search ,else this is also a fantastic way

  • @VIVEKSINGH-pc4lb
    @VIVEKSINGH-pc4lb Pƙed rokem

    you are genious

  • @Aabid_02
    @Aabid_02 Pƙed rokem

    I had compilation error @line 7 because there is to +i add in total ( total + i ) / ( i + 1 )) not sure how it running right there. Please check

    • @infiniteloop5449
      @infiniteloop5449 Pƙed rokem +1

      why are you adding i to total? it should be total/(i+1)

  • @harshitbisen8924
    @harshitbisen8924 Pƙed rokem +3

    why a similar logic in c++ fails a test case?
    Code:
    class Solution {
    public:
    int minimizeArrayValue(vector& nums) {
    int total = nums[0];
    int res = nums[0];
    int n = nums.size();
    for(int i = 1;i < n;i++){
    total += nums[i];
    int t = ceil(total/(i+1));
    res = max(res,t);
    }
    return res;
    }
    };
    Test case:
    nums =
    [13,13,20,0,8,9,9]
    Output
    15
    Expected
    16

    • @NeetCodeIO
      @NeetCodeIO  Pƙed rokem +5

      My guess is that your ceil() method call is not working, since in C++, dividing integers results in an integer rather than a float. You prob want to cast total as a float or double before dividing

    • @yashgupta6797
      @yashgupta6797 Pƙed rokem +3

      use
      int t = ceil (double(total)/double(i+1));
      bcz you used int/int which will always give u int only no advantage of ceil

    • @yashgupta6797
      @yashgupta6797 Pƙed rokem +2

      also used long long for total else overflow may occur

    • @scresat
      @scresat Pƙed rokem +5

      Correct way is (int) ceil((double) total/(i+1))
      Casting value inside the ceil to double, then casting the return of ceil to int since ceil returns a double value.

  • @amandubey5287
    @amandubey5287 Pƙed rokem +1

    class Solution:
    def minimizeArrayValue(self, nums: List[int]) -> int:
    sum_val = nums[0]
    count = 1
    max_val = nums[0]
    for r in range(1,len(nums)):
    sum_val+=nums[r]
    count+=1
    avg_val = math.ceil(sum_val/count)
    max_val = max(max_val,avg_val)

    return max_val

  • @naive-fleek7420
    @naive-fleek7420 Pƙed rokem

    dayummmm love u

  • @foobar69
    @foobar69 Pƙed 6 měsĂ­ci

    why is average working here? why isn't median being used? i thought of something along these lines then went ahead with median and got WA.

  • @shubhamraj25
    @shubhamraj25 Pƙed rokem

    Tricky one, yeah

  • @twonwon3595
    @twonwon3595 Pƙed rokem

    I one question about this in the explanation that they have provided what if we do this operation one more time and select i=3 then the array will become
    [5,5,3,4]
    So wouldn't this 4 be the minimum value ?
    I think I am missing something here

    • @sandagolcea4966
      @sandagolcea4966 Pƙed rokem

      Stumbled upon the same, but: No, because we always get the overall array max, so overall max in the array is still 5 (see [5,5,3,4] ) even if we modify some other part to have a smaller max, it will make no difference.

    • @twonwon3595
      @twonwon3595 Pƙed rokem

      @@sandagolcea4966 yes when you put it this way it makes sense. Thanks for replying !

  • @brm266
    @brm266 Pƙed 8 měsĂ­ci

    I came up with a solution in 35 minutes

  • @sandagolcea4966
    @sandagolcea4966 Pƙed rokem +2

    Don't understand what you mean by "we can't move values to the left" / right. đŸ€”

    • @yashjoon3889
      @yashjoon3889 Pƙed rokem

      moving a value to left means decreasing the current value and increasing the left value. Imagine it like a number flew towards the left hence reduced the current value by 1 and increased the left value by 1.

  • @cc-to2jn
    @cc-to2jn Pƙed 6 měsĂ­ci

    man i tried heap sol, but couldnt get it to pass. Not sure where the error is:
    class Node:
    def __init__(self,val,left=None,right=None):
    self.val=val
    self.left=left
    self.right=right
    def __lt__(self, other):
    return self.val int:

    heap=[]
    curr=h=Node(float('inf'))

    for j in range(len(nums)):
    node=Node(-nums[j])
    curr.right,node.left=node,curr
    heappush(heap,node)
    curr=curr.right

    out=heap[0].val

    while True:
    if heap[0].left==h or heap[0].val+1==0: #cant make smaller
    return -heap[0].val
    node=heappop(heap)
    node.val+=1
    node.left.val-=1
    heappush(heap,node)
    if heap[0].val>=out:
    out=heap[0].val
    else:
    break

    return -out
    Testcase: [13,13,20,0,8,9,9]
    output: 15
    Expected: 16