Minimize Maximum of Array - (META) | Leetcode-2439 | Explanation ➕ Live Coding 🧑🏻‍💻👩🏻‍💻

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

КОМЕНТАРІ • 86

  • @ajit287
    @ajit287 Рік тому +17

    Great !!! Was not getting intuition behind the use of binary search.

  • @satyasanjay1339
    @satyasanjay1339 Рік тому +2

    Excellent explanation. It was a tricky problem. I have observed that in these types of problems figuring out the operation done on the array. If you can figure out the brute force approach then you can easily solve it using binary search.
    *You can also take the left value as nums[0], because max of nums can not become less than nums[0]*
    *So, the range will be [nums[0], max(nums)]*
    Java Code:
    class Solution {
    boolean maxPossible(int[] nums, int max){
    int n = nums.length;
    long[] arr = new long[n];
    for(int i = 0; i < n; i++){
    arr[i] = nums[i];
    }
    for(int i = 0; i < n-1; i++){
    if(arr[i] > max){
    return false;
    }
    long buffer = max - arr[i];
    arr[i] += buffer;
    arr[i+1] -= buffer;
    }
    if(arr[n-1] > max){
    return false;
    }
    return true;
    }
    public int minimizeArrayValue(int[] nums) {
    int l = nums[0];
    int r = Integer.MIN_VALUE;
    for(int num: nums){
    r = Math.max(r, num);
    }
    int ans = 0;
    while(l

  • @codeandtalk6
    @codeandtalk6 Рік тому +2

    Best explanation

  • @anshumantripathy115
    @anshumantripathy115 Рік тому +3

    The maxL can be taken as nums[0] instead of 1 as nums[0] as it the lowest value of result, as already explained in the video. So the binary search will be between nums[0] and maximum value of nums[ ]. It will make the TC a bit better.
    Thank you for your amazing explanation.

  • @dayashankarlakhotia4943
    @dayashankarlakhotia4943 Рік тому +1

    Class solution {
    Public int minimize Array Value (int[]nums){
    long ans=0 long sum=0;
    for(int i=0;i

  • @146_shashanks4
    @146_shashanks4 Рік тому +4

    Please do vedios for leetcode contest solutions

  • @HealthyOm
    @HealthyOm Рік тому +2

    we learn from u importance of dry run ... which was get frustations also but... its very important we see it in this vedio .... love u bhaiya ur hard works

  • @JJ-tp2dd
    @JJ-tp2dd Рік тому +2

    Little change to avoid creating new array , space O(1)
    class Solution {

    private boolean isValid(int nums[], int mid, int n) {

    if(nums[0] > mid) return false; // nums[0] ko kum toh kr skte ni

    long prev = nums[0];

    for(int i = 1; i < nums.length; i ++) {

    long buffer = mid - prev;

    if(nums[i] - buffer > mid) return false;

    prev = nums[i] - buffer;
    }

    return true;
    }

    public int minimizeArrayValue(int[] nums) {

    int n = nums.length;

    int s = 0;
    int e = 0;

    int result = e;

    for(int i=0; i

    • @codestorywithMIK
      @codestorywithMIK  Рік тому +2

      That’s really good. I am noting this down as an improvement over my code.
      Thanks a lot JJ and yes, today Graph video is coming
      Thank you for your patience and trust

    • @JJ-tp2dd
      @JJ-tp2dd Рік тому +1

      @@codestorywithMIK I have full trust on you bhai and looking forward to learn from you.
      PS: I'll keep bugging for concepts series😛♥

    • @udaykumarchetla6205
      @udaykumarchetla6205 Рік тому

      @@codestorywithMIKdsu?

    • @codestorywithMIK
      @codestorywithMIK  Рік тому +2

      Hi Uday,
      Actually DSU is almost covered. We solved qns too. But more qns will come in Graphs Qns playlist.
      Let me know if I have missed anything in DSU. I will definitely cover

    • @udaykumarchetla6205
      @udaykumarchetla6205 Рік тому +2

      @@codestorywithMIK As u told u'll cover dsu union by size. please cover that and anything else if it's left u can cover that too..

  • @iamnoob7593
    @iamnoob7593 7 місяців тому +2

    superb

  • @U2011-n7w
    @U2011-n7w Рік тому +1

    nice explanation

  • @souravjoshi2293
    @souravjoshi2293 Рік тому +1

    story to code is magic

  • @dayashankarlakhotia4943
    @dayashankarlakhotia4943 Рік тому +1

    Good explanation. 2nd approach code write. Understood code with dryrun

  • @UECAshutoshKumar
    @UECAshutoshKumar 8 місяців тому +1

    Thank you 🙏

  • @iamnoob7593
    @iamnoob7593 Рік тому +2

    Brilliant , I knew the intution behind BS , But was not sure how to detect/logic if that max chosen can be answer or not for each mid.

  • @JJ-tp2dd
    @JJ-tp2dd Рік тому +1

    Java implementation:
    class Solution {

    private boolean isValid(int nums[], int mid, int n) {

    long[] arr = Arrays.stream(nums).asLongStream().toArray();

    for(int i=0; i mid) {

    return false;
    }

    long buffer = mid - arr[i];

    arr[i+1] = arr[i+1] - buffer;

    }

    return arr[n-1]

  • @UmeshBisht-ik4li
    @UmeshBisht-ik4li Рік тому +1

    Story explanation is next level❤
    Thanku bhaiya

  • @rajatrajgautam3224
    @rajatrajgautam3224 4 місяці тому +1

    the solution clicked me at 17:31 .... coded it by myself then thanks

  • @NoneNobe-c3e
    @NoneNobe-c3e 4 місяці тому +1

    Bahut tricky question tha

  • @study-yd6es
    @study-yd6es 3 місяці тому +1

    It was actually a tricky Question!!

  • @wearevacationuncoverers
    @wearevacationuncoverers Рік тому +1

    Was waiting for your video only.

  • @harsh.jain22
    @harsh.jain22 Рік тому

    Java solution : O(1) space complexity
    class Solution {
    boolean isMinMax(int[] nums, int midMax){

    int n = nums.length;
    long lastVal = nums[0];
    long buffer = 0;
    for(int i= 1; i midMax){
    return false;
    }
    lastVal = nums[i]-buffer;
    }
    return true;
    }
    public int minimizeArrayValue(int[] nums) {
    int n = nums.length;

    int s = nums[0]; // minMax can't be less than first elem as it can't be reduced
    int e = Arrays.stream(nums).max().getAsInt(); // max elem of nums arr
    int possibleAns = nums[0];
    while(s

  • @shivamchaudhary3646
    @shivamchaudhary3646 Рік тому +1

    Very nice explanation sir ❤

  • @molyoxide8358
    @molyoxide8358 Рік тому +1

    13:30 was an epic line🤣 🤣 🤣

  • @rounaq_khandelwal
    @rounaq_khandelwal Рік тому +2

    You are the best!

  • @alphadrones23
    @alphadrones23 Рік тому +2

    if we use greedy, we can do it in O(n) but the binary seach intuition was awesome....

  • @codingwave56
    @codingwave56 Рік тому +2

  • @AryanMishra-yh4ic
    @AryanMishra-yh4ic Рік тому +1

    please upload solutions of weekly contest as well. it will help us a lot.

  • @sarthakyadav9950
    @sarthakyadav9950 Рік тому +1

    Great Explanation!!!

  • @mohithadiyal6083
    @mohithadiyal6083 Рік тому +1

    Such a great channel😊

  • @manauwarzulfekaransari4719
    @manauwarzulfekaransari4719 9 місяців тому +1

    Here is the javascript code of above problem
    var isValid = function(nums,expectedMax){
    let arr = [...nums];
    for(let i = 0 ; i < arr.length-1 ; i++){
    if(arr[i] > expectedMax){
    return false;
    }else{
    let buffer = Math.abs(arr[i]-expectedMax);
    arr[i+1] -= buffer;
    }
    }
    return arr[arr.length-1]

  • @prajwalshaw9217
    @prajwalshaw9217 Рік тому

    Sir plz make a video on leetcode 1482 (minimum no.of days to make m bouquets)...which based on binary search on answer concept. I find it really difficult to understand this problem and you have a really unique way of making difficult concepts quite understandable. Thank you

  • @dhairyachauhan6622
    @dhairyachauhan6622 Рік тому +2

    ALTERNATE SOLUTION
    class Solution {
    public:
    bool isPossible(vector&arr,int mid){
    vectornums(arr.begin(),arr.end());
    int n = nums.size();
    long long buffer = 0;
    for(int i = n-1; i>= 1;i--){
    if(nums[i] > mid){
    nums[i-1] += nums[i]-mid;
    nums[i] = mid; // no need to do this just done for visualization.
    }
    }
    return nums[0]

  • @tutuimam3381
    @tutuimam3381 Рік тому +1

    Thanks a lot

  • @dileshchouhan9527
    @dileshchouhan9527 Рік тому

    sir,
    how much time have u taken to solve this perticular problem i mean total time with story to submission.

  • @ashishdhal4614
    @ashishdhal4614 Рік тому

    Can we traverse the array if any value is bigger than exp max make it exp max( we will handle o index by our own) and increase the predecessor by arr[i] - exp if max of array is still larger than exp then return false

  • @prashantb4220
    @prashantb4220 Рік тому

    What if nums[i+1] goes in negative when subtracted with buffer wont that be a problem ? It is given in question nums[i]>0

  • @manishasaini307
    @manishasaini307 Рік тому +3

    bhaiya maxL=nums[0] bhi kr skte hai na

  • @shauncrasta619
    @shauncrasta619 10 місяців тому

    My main question is why do we need to get the 0th element to maximum? Why not change only the elements that are > x. x being the desired max. What is the requirement to do it. Can you please discuss the proof for it bhaiya?

  • @yuvhrajverma9665
    @yuvhrajverma9665 Рік тому

    Sir can u explain O(n) approach mentioned in this question editorial

    • @codestorywithMIK
      @codestorywithMIK  Рік тому +1

      Sure, that comes under Greedy.
      Will create one in Greedy playlist soon.

  • @xiaoshen194
    @xiaoshen194 Рік тому +1

    If(nums[i] > epmax) return false kyu kiya? Nhi samajh aya 😅🥹

  • @the_only_one9415
    @the_only_one9415 11 місяців тому

    SIR isme out of bound kb kaise jaa rha smj nhi aa rha plz ek video banaye kb hme long long use or kaise krna chahiye.

  • @aryanZzz..
    @aryanZzz.. Рік тому

    bhaiya aapke method se new vector create he krna pd rha h long long type ka if isValid function me uski copy pass kre toh test case 29 pr fss rha h class Solution {
    public:
    bool isValidMaxm(vectornums,long long mid){
    int n=nums.size();
    for(long long i=0;imid) return false;
    long long buffer=mid-nums[i];
    nums[i+1]=nums[i+1]-buffer;
    }
    return nums[n-1]

    • @adarshjha5126
      @adarshjha5126 11 місяців тому

      tumhara nums[i] integer me hi hai usko bhi explicitly long long karo

  • @riyakansal3312
    @riyakansal3312 Рік тому +1

    TC is O(nlogn)??

  • @molyoxide8358
    @molyoxide8358 Рік тому

    Bhai jaise aapne 1 line mein ek vector se doosre vector mein jo copy kiya kya vaise vector to map copy ho skta hai kya ek hi line mein.
    Ya vohi for loop lagana padega??

    • @codestorywithMIK
      @codestorywithMIK  Рік тому

      I think not possible. Loop is the only option

    • @molyoxide8358
      @molyoxide8358 Рік тому

      @@codestorywithMIK OK

    • @elakstein
      @elakstein Рік тому

      map me to key and value pair hoga na, to directly copy kaise karna chahte ho ?

    • @molyoxide8358
      @molyoxide8358 Рік тому

      @@elakstein key apne aap store ho jaaye aur uske frequency bhi apne aap increment ho jaaye iss hisab se soch rha tha.

  • @bobo_v
    @bobo_v Рік тому

    bhaiya i requested you to make video on Minimise Maximum Distance between Gas Stations this question is primium in leetcode but in other platform its free if you already make video on this please pin in the comment section

  • @manishaghi2966
    @manishaghi2966 Місяць тому

    This is my java code and it is failing on few test cases:
    class Solution {
    public int minimizeArrayValue(int[] nums) {
    int min = 0, max = Integer.MIN_VALUE;
    int ans = 0;
    for(int x : nums){
    if(x > max) max = x;
    }
    while(min

  • @molyoxide8358
    @molyoxide8358 Рік тому

    bhai -> nums[i+1] = nums[i+1] - buffer;
    kya yeh kabhi neagative mein nhi jaayega??

  • @sumedhaghosh1265
    @sumedhaghosh1265 8 місяців тому

    sir kaise pata ki maximum value 5 hi ayega 4 bhi toh ho sakta hai nah?😭

  • @udaykumarchetla6205
    @udaykumarchetla6205 Рік тому

    Greedy? for this question?

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

    Bro can't you make shorter videos. Concentration 30 minutes ka bohot difficult hai. Videos are too long

  • @parthabhuyan7602
    @parthabhuyan7602 Рік тому +1

    Thanks a lot