BS-15. Capacity to Ship Packages within D Days

Sdílet
Vložit
  • čas přidán 22. 06. 2023
  • Problem Link: bit.ly/43QDpdG
    Notes/C++/Java/Python codes:
    We have solved the problem, and we have gone from brute force and ended with the most optimal solution. Every approach's code has been written in the video itself. Also, we have covered the algorithm with intuition.
    Full Course: bit.ly/tufA2ZYt
    You can follow me across social media, all my handles are below:
    Linkedin/Instagram/Telegram: linktr.ee/takeUforward
    0:00 Introduction of Course

Komentáře • 159

  • @Satyendra_Prakash17
    @Satyendra_Prakash17 Před 28 dny +5

    solved it on my own , woohoo confidence is just sky rocketing !! thank you striver

  • @ArpanChakraborty-do6yz
    @ArpanChakraborty-do6yz Před 5 měsíci +37

    bhaiya i have done the last two problems on my own without watching your solution and now i am gaining a little bit of confidence that i can also do.... it is not that i haven't done any problem on my own but it was after completing your playlist of that topic and then doing other problem of that topic, but it is the first time i am doing this before completing this playlist of binary search,,, thank you bhaiya , you teaching style is just awesome😍😇😇

    • @anonymousanonymous7507
      @anonymousanonymous7507 Před měsícem

      gawd

    • @arujgarg7267
      @arujgarg7267 Před 25 dny

      dude exactly same, i was also able to solve this one and the last one on my own. this happened for the first time!!

    • @luckygarg2294
      @luckygarg2294 Před 24 dny

      @@arujgarg7267 Exactly same... Solved this and previous 2 problems on my own

  • @siddharth892
    @siddharth892 Před 6 měsíci +8

    One more thing that should be added into the question is that the weights need to shipped in order. I think if we are to club the minimum and the maximum weights together then 11 would be the answer for this [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Days = 5

    • @divingdabbler2035
      @divingdabbler2035 Před 3 měsíci +1

      Ikr, right now the question seems more like binary search over series of knapsacks instead of contiguous sums

    • @harshvardhansankpal716
      @harshvardhansankpal716 Před 2 měsíci

      yup, i was literally thinking the same thing, i thought if i only take consecutive days i might get error in leetcode.

  • @sauravchandra10
    @sauravchandra10 Před rokem +15

    The way you explain and write the code so clearly is what is so unique about you. Clearly understood, thanks!

  • @grannymuffins8882
    @grannymuffins8882 Před rokem +11

    bro this is one of the best cheat sheet.
    the way you split each patterns into sub patterns is great
    Would be great if you continue to find sub patterns within existing patterns

  • @samreenimam8608
    @samreenimam8608 Před 6 měsíci +4

    SALUTE HAI BHAI..!! kya smjhaya hai ..itna easy logic!!... blown away! Thank you rahega

  • @elizabethr5161
    @elizabethr5161 Před rokem +1

    Crystal clear explanation striver... Clearly understood everything...Thanks a lot for this awesome series.

  • @lucario404
    @lucario404 Před rokem +7

    aapki videos shuru majboori mei kiye the,
    lekin ab maza aane laga hai :)

  • @adityarajvermaa
    @adityarajvermaa Před 11 měsíci +3

    man..even this one i coded down myself...understanding the previous ones...thanks bhai

  • @rushithagudipudi7742
    @rushithagudipudi7742 Před 3 měsíci +3

    ❤understood, next level lecture , this is what the tier 3 students wish to listen in classroom

  • @tanishkumar5827
    @tanishkumar5827 Před 7 měsíci

    thank u striver bhaiyaa!you made divide and conquer a cakewalk! this ecstatic feeling is so morale boosting as i did this problem by myself in a single go under your guidance ! much love

  • @cinime
    @cinime Před rokem

    Understood! Super excellent explanation as always, thank you very much for your effort!!

  • @vishious14
    @vishious14 Před 6 měsíci +1

    Was able to solve this on my own. This question is similar to minimum days to make m bouquets. Thanks a tonne, Striver !!!!

  • @SumitSingh-dc8pm
    @SumitSingh-dc8pm Před 7 měsíci

    *Completed now, thanks for such lovely content.*

  • @ishangujarathi10
    @ishangujarathi10 Před rokem +1

    superb explanation, 80% coded it myself, tysmm

  • @abhaypandey2668
    @abhaypandey2668 Před 6 měsíci +2

    I watched 5 videos and was trying to understand this for last 3 days, but the way you explained it has become like cutting butter for me now..thanks a lot

    • @kanishkkala16
      @kanishkkala16 Před 5 měsíci

      Can u share where were doing this problem, was it a platform other than leetcode ? Kindly share

  • @sayandey2492
    @sayandey2492 Před 4 měsíci +1

    Wow...the portrait of the ship is so good 😃

  • @Isagi__000
    @Isagi__000 Před 3 měsíci

    wow man, i could able to solve this on my own. i am watching this because i just wanna see your explination, i am following your playlist, learning in a structure and doing similar problem really helps in undertanding the concept, thank man. also after solving koko banana problem, solving prev problem in this playlist find the smallest divisor, really cake walk.
    tha't it, tho i don't comment that much, you are helping many ppl like me. thanks for doing all the work.

  • @ayushagarwal5271
    @ayushagarwal5271 Před měsícem

    Thanks man! you make the code look so simple, Idk why I end up writing a long and complex brute force code even though it works..

  • @user-ti3bd8mp1w
    @user-ti3bd8mp1w Před rokem

    Understood
    Thank You striver for such an amazing explanation

  • @teeyaojha4365
    @teeyaojha4365 Před rokem +9

    mistake at 15:03 it will come to 31 not 23, but its ok. under stood.

  • @manavsingh5919
    @manavsingh5919 Před 11 měsíci +1

    Thank You Striver understood everything🙂

  • @DEVILGAMING-to6bn
    @DEVILGAMING-to6bn Před rokem +42

    sorry to say but studying DSA with you and LOVE sir combined is just better than best

  • @Codro09
    @Codro09 Před 3 měsíci +1

    I was able to code in less than 7 mins (thala for a reason....naah, obv u the reason).....thanks a lot.

  • @AnmolGupta-oj4lm
    @AnmolGupta-oj4lm Před 11 měsíci +1

    Understood Very Well!

  • @chinmaygambhirrao9774
    @chinmaygambhirrao9774 Před 10 měsíci

    Man, you're legend🤓. Thanks

  • @TON-108
    @TON-108 Před 8 měsíci +1

    Understood! Thanks :)

  • @IAmIndraTheGod
    @IAmIndraTheGod Před 9 měsíci +1

    slight improvement, the max can also be sum(weights)-days+1. This is because in worst case you can ship the item with max weight on day 1, then remaining items combination on each days

  • @MYMIND252
    @MYMIND252 Před 9 měsíci +3

    - [00:17](czcams.com/video/Mde2q7GFCrw/video.html) The problem is about finding the minimum capacity needed to ship packages within a given number of days.
    - [01:49](czcams.com/video/Mde2q7GFCrw/video.htmlm49s) An example is given where if the ship's capacity is 100, all packages can be shipped in one day.
    - [03:22](czcams.com/video/Mde2q7GFCrw/video.htmlm22s) It is demonstrated that a capacity of 10 leads to shipping taking more than the given number of days.
    - [04:20](czcams.com/video/Mde2q7GFCrw/video.htmlm20s) Increasing the capacity to 15 allows shipping all packages within the given days.
    - [06:11](czcams.com/video/Mde2q7GFCrw/video.htmlm11s) The answer must be between the maximum weight of a product and the sum of all weights.
    - [08:01](czcams.com/video/Mde2q7GFCrw/video.htmlm1s) A function is described to find the number of days required to ship with a given capacity.
    - [12:32](czcams.com/video/Mde2q7GFCrw/video.html2m32s) The initial solution has a time complexity of O(N^2) due to linear search.
    - [13:54](czcams.com/video/Mde2q7GFCrw/video.html3m54s) Binary search is introduced as an optimization technique to find the minimum capacity needed.
    - [16:49](czcams.com/video/Mde2q7GFCrw/video.html6m49s) The binary search approach eliminates capacities that are not possible and updates the answer with the lowest possible capacity.
    - [18:25](czcams.com/video/Mde2q7GFCrw/video.html8m25s) The time complexity of the binary search solution is O(N * log(Sum - Max)), where N is the number of items and Sum and Max are the sum of weights and the maximum weight, respectively.
    - [19:50](czcams.com/video/Mde2q7GFCrw/video.html9m50s) The C++ code for the binary search solution is demonstrated, and viewers are encouraged to find code for other programming languages in the video description.

  • @92AkshaySharma
    @92AkshaySharma Před 7 měsíci

    concise and helpful explaination

  • @user-js1rx8rs9p
    @user-js1rx8rs9p Před 3 měsíci

    you are artist too bro, multitalented striver

  • @sujalgupta6100
    @sujalgupta6100 Před 2 měsíci

    solved by myself, All thanks to you.

  • @saswatrath4646
    @saswatrath4646 Před 3 měsíci

    Understood and solved by myself

  • @user-or5oz1pk2x
    @user-or5oz1pk2x Před 2 měsíci

    Thanks a lot Bhaiya. You make it easy peasy problem 😀

  • @prabhatece0488
    @prabhatece0488 Před 6 měsíci +9

    At 15:11 bhaiya you said high=23, but actually mid was=32 before, so new high=mid-1=32-1=31

    • @kanhiyasharma3648
      @kanhiyasharma3648 Před měsícem

      hnn bhaii mee bhi yhi soch rhaa thaa tbbhi comment dhundh rha tha ki kisi nee to kia hogaa

  • @U2011-n7w
    @U2011-n7w Před 10 měsíci +1

    nice explanation

  • @NazeerBashaShaik
    @NazeerBashaShaik Před 3 měsíci

    Understood, thank you.

  • @aliakbaransaria3-925
    @aliakbaransaria3-925 Před 11 měsíci

    Nice explanation 👌

  • @K_EN_VisheshSaini
    @K_EN_VisheshSaini Před 9 měsíci +1

    Another code for same approach:-
    bool f(int mid, vector &weights, int d){
    int day = 1, temp = mid;
    for(int i=0;i

  • @CodeMode9313
    @CodeMode9313 Před 11 měsíci +1

    mast habibi

  • @md.imrankhan6912
    @md.imrankhan6912 Před 10 měsíci

    Living Legend!

  • @ganeshjaggineni4097
    @ganeshjaggineni4097 Před měsícem

    NICE SUPER EXCELLENT MOTIVATED

  • @user-is6ky7pp2n
    @user-is6ky7pp2n Před měsícem

    Understood !! 😍😍

  • @dhruv2014
    @dhruv2014 Před 6 měsíci

    Nice explanation

  • @diptamoymitra7486
    @diptamoymitra7486 Před rokem

    Atlast got it❤❤❤

  • @kiranmoura2974
    @kiranmoura2974 Před rokem

    Superb sir ❤

  • @rushidesai2836
    @rushidesai2836 Před 2 měsíci

    Amazing question

  • @YourCodeVerse
    @YourCodeVerse Před 7 měsíci

    Understood✅🔥🔥

  • @MJBZG
    @MJBZG Před 24 dny

    solved it on my own, yay!

  • @utsavseth6573
    @utsavseth6573 Před 11 měsíci

    Well done

  • @CrazyHunk14
    @CrazyHunk14 Před 5 měsíci +1

    well well well now i am solving leetcode medium question without any help in the optimal way thank you striver!

  • @senseiAree
    @senseiAree Před 9 měsíci

    Understood ❤

  • @gouravbhatt479
    @gouravbhatt479 Před měsícem +1

    i was able to do it by myself...........thanks to striver;
    return happy😆;

  • @her_soulmate
    @her_soulmate Před 6 měsíci

    Understood 🎉

  • @thenriquevicentini
    @thenriquevicentini Před 2 měsíci

    Understood!

  • @pihus3498
    @pihus3498 Před rokem

    understooood :)

  • @joeljacob4685
    @joeljacob4685 Před 9 měsíci

    Understood!!

  • @GuruPrasadShukla
    @GuruPrasadShukla Před rokem

    going smoothly

  • @gunjjoshi5687
    @gunjjoshi5687 Před 9 měsíci

    Thank You

  • @srinathv1412
    @srinathv1412 Před 5 měsíci

    Understood !!!!!

  • @ambaradhikari7425
    @ambaradhikari7425 Před rokem +3

    Bro by when your whole dsa sheet will be covered, placement coming up?any eta approx?

  • @user-tk2vg5jt3l
    @user-tk2vg5jt3l Před 5 měsíci

    Thank you Bhaiya

  • @vipu18
    @vipu18 Před 5 měsíci +1

    Understood

  • @Manishgupta200
    @Manishgupta200 Před rokem

    Possible function takes time to understand but clear now
    bool isPossible(vector &weights, int d, int mid){
    int dayCount = 1;
    int sum = 0;
    for(int i = 0; i < weights.size(); i++){
    sum += weights[i];
    if(sum d){
    return false;
    }
    sum = weights[i];
    }
    }
    return true;
    }

  • @humanity7880
    @humanity7880 Před rokem

    understood!

  • @shyamkumar712
    @shyamkumar712 Před rokem +1

    if weights are 1 2 8 9 and d=2 will it give correct answer??

  • @kushagramishra5638
    @kushagramishra5638 Před rokem +1

    understood

  • @cyanideyt9579
    @cyanideyt9579 Před 5 měsíci +2

    what's the overall time complexity of binary search algorithm in this question
    Because first loop to find summation and maximum element takes O(n) and then the binary search algo which takes (log2(sum-maxi)), after the algo we call the findDays function which take O(n) time complexity.
    so, what's the time complexity of the code
    Thanks You!!

    • @manavpatnaik222
      @manavpatnaik222 Před 4 měsíci

      It would be O(n*log(sum-max)), but if you want the exact time complexity taking into account the summation and maximum, then O(2n + n*log(sum-max)).

  • @user-dq4gq2sd5j
    @user-dq4gq2sd5j Před 3 měsíci +1

    It was just an awesome video... but pls tell me... why int days is initialised with 1 .... why not int days=0;

  • @sattwikmanna7851
    @sattwikmanna7851 Před 11 měsíci +2

    bhaiya timestamp 15:06
    here mid-1=high=point 31 ,not point 23.

  • @VishalGupta-xw2rp
    @VishalGupta-xw2rp Před 9 měsíci

    17:00 opposite polarity

  • @YASHRAJ-ex6wq
    @YASHRAJ-ex6wq Před 6 dny

    I am thinking about taking the arr[i] + arr[n-i-1] elements together in the ship , then capacity would be 11? right?

  • @yugal8627
    @yugal8627 Před rokem +1

    hi @striver , when to use while(high - low >1) condition

    • @takeUforward
      @takeUforward  Před rokem +5

      I don't use it anywhere, all problems can be solved without that, if your base is clear

  • @shreshtasingh2384
    @shreshtasingh2384 Před 4 měsíci

    🔥🔥🔥🔥🔥🔥🔥🔥

  • @BiswajitDas-fj5gp
    @BiswajitDas-fj5gp Před rokem

    ❤❤❤

  • @codeman3828
    @codeman3828 Před 6 měsíci

    Undertsood

  • @girikgarg8
    @girikgarg8 Před 10 měsíci

    Done

  • @VinayQ-
    @VinayQ- Před 9 měsíci

    I think in linear search return the capacity not daysRquired

  • @amittiwari360
    @amittiwari360 Před 3 měsíci

    Why the weights are not sorted then also it works, please let me know the function which is inside while loop

  • @saqlainkadiri
    @saqlainkadiri Před rokem

    Isn't this same as painter partition problem?

  • @drizzle01
    @drizzle01 Před 11 měsíci

  • @user-vs4ng8ul9x
    @user-vs4ng8ul9x Před 28 dny

    can anyone tell, while filling the boat why least weights possible are taken first??

  • @rohit.mnnita
    @rohit.mnnita Před rokem

    17:59

  • @raghavkansal3765
    @raghavkansal3765 Před rokem

    "understood"

  • @globalcuber9816
    @globalcuber9816 Před rokem +3

    I have doubt in first example the answer must be 11 as you can take 1st and last element sum everytime so it will be 11

    • @takeUforward
      @takeUforward  Před rokem +2

      You need to take consecutive elements :)

  • @piyushgarg41
    @piyushgarg41 Před rokem +1

    Can anyone look up at my code and find mistake . I got wrong answer of this ques i.e capacity to ship packages within d days .
    class Solution {
    public:
    int caldays(vector& weights, int cap){
    int sum=0, cnt=0;
    for(auto it: weights){
    sum+=it;
    if(sum

    • @sayakghosh5104
      @sayakghosh5104 Před rokem

      Low has to start from the maxElement in the array, other then that I see no error...

    • @swapnil243
      @swapnil243 Před rokem +1

      min days will be 1 not 0, just change days = 0 to days = 1 in the caldays function, also start low from the max element in the weights array

    • @ayushmukhopadhyay7470
      @ayushmukhopadhyay7470 Před rokem

      @@swapnil243 Correct

    • @piyushgarg41
      @piyushgarg41 Před rokem

      @@swapnil243 but it will. Without changing days in cladays

  • @Omneeshkushwah9695
    @Omneeshkushwah9695 Před rokem +6

    @striver please make full dsa paid or free course for competitive programming. I am watching your videos from past 5 months but still I am unable to solve new problems or codeforces ,codechef problems
    Can u make paid live full course .
    I am highly requested to u please launch your course from basic to advanced competitive course. programming course

    • @takeUforward
      @takeUforward  Před rokem +7

      join tle-eliminators

    • @iWontFakeIt
      @iWontFakeIt Před rokem +2

      @@takeUforward please launch paid cp course we want to learn from you, you explain very well

  • @footybanter1
    @footybanter1 Před 6 měsíci

    Day 2 till here

  • @bingereviewer7752
    @bingereviewer7752 Před rokem +1

    hello bhaiya , as a tier 3 student should i focus more on DSA or web development. Learning web d. only that much to make 2 to 3 project is enough or should i go in depth in both dsa and web

    • @sauravchandra10
      @sauravchandra10 Před rokem +4

      I think the focus should be on DSA. Projects can be sorted out later, but DSA is something which will get you an interview call.

    • @thelostguy1212
      @thelostguy1212 Před rokem +6

      Web development projects will land you up in cheap companies. Strong DSA will land you up in Maang or good product companies. Choice is yours.

    • @MohanC-tv5kk
      @MohanC-tv5kk Před rokem

      @@thelostguy1212 #FAANGM

    • @mukulkhanna26
      @mukulkhanna26 Před rokem +1

      bro i have passed out from cse branch from tier 3 without any job 3 star on codechef and good rating on leetcode and 500 problems solved but everyone else with other dev skills got a package except me rest u decide becoz at the end it's your call always but try to balance both dev+dsa

    • @davendrabedwal830
      @davendrabedwal830 Před 11 měsíci

      do web dev and master the skill to that level that each company wants you for your skill. dsa is not a thing to do its just problem solving kind of thing, never solve it for the numbers, just make sure whatever you do , u should be the best in it!! if you are in 1nd or 2nd year explore development field more

  • @RahulKumar-nd2sp
    @RahulKumar-nd2sp Před rokem +2

    15:14 high should be 31 na?

  • @mustafa-zl9ox
    @mustafa-zl9ox Před 11 měsíci

    class Solution {
    public:
    bool func(vector& v , int cap ,int day){
    int days=1 , load=0;
    for(int i=0 ; i< v.size() ; i++){
    if(load + v[i] > cap){
    days++;
    load = v[i];
    }
    else{
    load += v[i];
    }
    if( days > day){
    return true;
    }
    }
    return false;
    }
    int shipWithinDays(vector& weights, int days) {
    int low = INT_MIN , high = 0 ,ans=-1;
    for(int i = 0; i < weights.size() ; i++){
    low = max( low , weights[i]);
    high += weights[i];
    }
    while( low

  • @ritikverma3206
    @ritikverma3206 Před rokem +5

    First comment

  • @poornima8206
    @poornima8206 Před 5 měsíci

    why we are initializing days with 1

  • @pravalikavemula6952
    @pravalikavemula6952 Před měsícem

    This problem is same as BOOK ALLOCATION PROBLEM

  • @siddhantgureja7803
    @siddhantgureja7803 Před rokem

    Striver, please upload the solution of k closest elements.
    It's Leetcode problem no. 658
    It's a binary search question.

  • @user-td9uu2om9f
    @user-td9uu2om9f Před 4 dny

    all the question are similar to previous one

  • @rahulhembram4519
    @rahulhembram4519 Před 10 měsíci

    UnderStood

  • @ishangupta803
    @ishangupta803 Před rokem

    this question can also be solved using previous video idea
    #include
    int possible(vector&weights,int posans,int days)
    {
    int n=weights.size();
    int count=0,posday=1;
    for(int i=0;iposans)
    return false;
    if(count+weights[i]>posans)
    {
    count=0;
    posday++;
    }
    count+=weights[i];
    }
    if(posday

  • @kshitijmishra2716
    @kshitijmishra2716 Před rokem

    hehe solved by self

  • @cenacr007
    @cenacr007 Před 9 měsíci

    us