L2. Maximum Points You Can Obtain from Cards | 2 Pointers and Sliding Window Playlist

Поделиться
HTML-код
  • Опубликовано: 25 мар 2024
  • Notes/Codes/Problem links under step 10 of A2Z DSA Course: takeuforward.org/strivers-a2z...
    Entire playlist: • Two Pointer and Slidin...
    Follow us on our other social media handles: linktr.ee/takeuforward

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

  • @muhammadfarhaan6951
    @muhammadfarhaan6951 3 месяца назад +15

    Implementing it yourself makes it much more clearer.

  • @4444-c4s
    @4444-c4s 3 месяца назад +50

    East or West, Striver bhaiya sabse Best
    RUclips pe esa koi bhi nahi hoga jo job ke saath saath itna content banata ho aur apni company bhi chalata ho...🙏🙏🙏

    • @Coder_Buzz07
      @Coder_Buzz07 3 месяца назад +7

      codewithmik bhi hai bro

    • @Amanh729
      @Amanh729 2 месяца назад +2

      @@Coder_Buzz07 yup

  • @unknown2698
    @unknown2698 18 дней назад +3

    public static int maxScore(int[] cardPoints, int k) {
    int lsum =0, rsum =0, max =0,sum =0;
    int n = cardPoints.length;
    for(int i=0;imax){
    max = sum;
    }
    }
    return max;
    }
    same approach but easier to understand

  • @KashishIsOn786
    @KashishIsOn786 2 месяца назад +1

    Thank you @striver , for making DSA Easy for us . Hatts of to you .

  • @aakanshavishwakarma8235
    @aakanshavishwakarma8235 Месяц назад +9

    another approach? we can take a consecutive window of size (n-k) and find the minimum sum window , that yields us the maximum sum of the remaining 4 elements from the first or last

    • @akshaysharmaBSG
      @akshaysharmaBSG 6 дней назад

      Yes...that also makes sense...but what will be time complexity for getting min sum of n-k array size ?

  • @mayanksaurabhmayanksaurabh9271
    @mayanksaurabhmayanksaurabh9271 Месяц назад +1

    thanks for so easy and intuitive solution. Lot of solutions for this problem are problem which have made it look really complex

  • @rishabsharma5307
    @rishabsharma5307 11 дней назад

    Started from the end and circularly rotated sliding window
    ```
    int maxScore(vector& cardPoints, int k) {
    int i, j, maxSum, sum, n = cardPoints.size();
    bool flag = false;
    i = j = n - k;
    maxSum = sum = 0;
    while(true)
    {
    sum += cardPoints[j];
    if(j-i+1 == k || flag)
    {
    flag = true;
    maxSum = max(maxSum, sum);
    if(i == 0)
    break;
    sum -= cardPoints[i];
    i = (i + 1) % n;
    }
    j = (j + 1) % n;
    }
    return maxSum;
    }
    ```

  • @hareshnayak7302
    @hareshnayak7302 2 месяца назад

    Thanks striver for this amazing video.

  • @LinhHoang-ml1qo
    @LinhHoang-ml1qo 2 месяца назад

    understood!Thank you Striver

  • @109_debjitacharjee9
    @109_debjitacharjee9 2 месяца назад +1

    u are the best bhaiya🤩 eagerly waiting for your string playlist

  • @stith_pragya
    @stith_pragya Месяц назад +1

    Understood...........Thank You So Much for this wonderful video..........🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻

  • @sarangkumarsingh7901
    @sarangkumarsingh7901 2 месяца назад

    Wow......nice approach......

  • @ishanaggarwal7265
    @ishanaggarwal7265 Месяц назад +2

    Hi striver, I knew a better solution that solves in single pass.

  • @ShauryaPundir
    @ShauryaPundir 3 месяца назад

    thanks vvvvv much sir,i really wanted a playlist like this

  • @lavanyaan2158
    @lavanyaan2158 3 месяца назад

    Thank you so much for the video bro.

  • @Codingforugeek
    @Codingforugeek 3 месяца назад +4

    class Solution {
    public:
    int maxScore(vector& nums, int k) {
    int leftSum=0,rightSum=0,maxSum=0;
    for(int i=0;i=0;i--){
    leftSum-=nums[i];
    rightSum+=nums[rightIndex];
    rightIndex--;
    maxSum=max(maxSum,leftSum+rightSum);
    }
    return maxSum;
    }
    };
    here's the working code.....
    😌

  • @subee128
    @subee128 3 месяца назад

    Thank you very much

  • @torishi82
    @torishi82 Месяц назад

    Understood bhai. Thank you.

  • @codeman3828
    @codeman3828 2 месяца назад

    Understood. Thanks

  • @kshitijraj1320
    @kshitijraj1320 3 месяца назад

    great video 😇

  • @ganeshjaggineni4097
    @ganeshjaggineni4097 Месяц назад

    NICE SUPER EXCELLENT MOTIVATED

  • @mr_weird3680
    @mr_weird3680 3 месяца назад

    Thanks Brother❤

  • @ok-jg9jb
    @ok-jg9jb 3 месяца назад

    Thanks❤

  • @Pushpraj-sf1nz
    @Pushpraj-sf1nz 2 месяца назад +1

    it was helpful

  • @vaibhavmurarka5179
    @vaibhavmurarka5179 3 месяца назад +2

    my approach is similar to urs what i did is take k last elements followed by k first then i took sum of last k and then kept removing the front and adding the latest element in the sum and took max of these steps: int n=arr.size();
    int ans=0;
    int sum=0;
    int ret=0;
    for(int i=n-k;i

  • @naveenau143
    @naveenau143 28 дней назад

    Understood ❤

  • @parth_3856
    @parth_3856 3 месяца назад +9

    OTHER CREATORS! be like "BHAI SAANS THO LENE DE......".

  • @shototodoroki4719
    @shototodoroki4719 3 месяца назад

    understood

  • @AmanPandey-bd1sj
    @AmanPandey-bd1sj 26 дней назад

    thanks you bhaiya, Understood😊
    import java.util.*;
    public class main{
    public static void main(String args[]){
    int a[] = {6, 2, 3, 4, 7, 2, 1, 7, 1};
    int k = 4;
    System.out.println("MAX SUM IS: "+ findMaxPoints(a, k));
    }
    public static int findMaxPoints(int a[], int k){
    int lsum = 0;
    int rsum = 0;
    int maxsum = 0;

    for(int i=0;i

  • @sandeeppp9040
    @sandeeppp9040 Месяц назад +1

    class Solution {
    public int maxScore(int[] cardPoints, int k) {

    int lsum=0,rsum=0,maxSum=0;
    for(int i=0;i=0;i--){
    lsum-=cardPoints[i];
    rsum+=cardPoints[rindex];
    rindex--;
    maxSum=Math.max(maxSum,(lsum+rsum));
    }
    return maxSum;



    }
    }
    i was here and anyone can have this java code !!!! and also initial loop will go to end k because of correct calculation of lsum

  • @bharath3387
    @bharath3387 2 месяца назад +1

    Do we need seperate variables for right and left sum, can we not just maintain a single variable and 2 pointers and remove left pointer value and increase right bponter value

  • @subhasishdas3011
    @subhasishdas3011 3 месяца назад

    One more approach is finding min_sum of all windows of size array_length - k , array_total_sum - min_sum will be the ans.

  • @angeldeveloper
    @angeldeveloper 3 месяца назад

    🙌🏻

  • @jitghosh3923
    @jitghosh3923 3 месяца назад

    🙌

  • @IstiakGametube
    @IstiakGametube 3 месяца назад +14

    I have been following your a2z DSA course. I want to do strings but there is no videos and problems in your sheet. Please make videos on them and upload the problems

    • @mrinceptionist7038
      @mrinceptionist7038 3 месяца назад

      bro what are you talking about.....the a2z dsa sheet has 2 dedicated section for strings....step-5 and step-18, which covers all basics,medium and hard string questions !!!

    • @IstiakGametube
      @IstiakGametube 3 месяца назад

      @@mrinceptionist7038 He has questions but there is no solve videos for them

  • @sandeepgmore1483
    @sandeepgmore1483 25 дней назад

    @striver where can i get the source code solution for content

  • @raghavmanish24
    @raghavmanish24 Месяц назад +2

    hey striver ... i'm the one of those follower who always give time for like and comment whenever i watch your videos.....thanku again

  • @8daudio672
    @8daudio672 3 месяца назад +2

    class Solution {
    public int maxScore(int[] nums, int k) {
    //int left=0,right=0,maxi=0,sum=0;
    int n=nums.length,leftsum=0,rightsum=0,maxi=0;
    for(int i=0;i=0;i--){
    leftsum-=nums[i];//contract
    //if(j>n-k-1)
    rightsum+=nums[j];//expand
    j--;
    maxi=Math.max(maxi,leftsum+rightsum);
    }
    return maxi;
    }
    } whats wong with it

    • @kisnagoyal9694
      @kisnagoyal9694 3 месяца назад +3

      I think you are ignoring value of leftsum from (idx = 0 to idx=k-1) in maxi... So it doesn't take value of leftsum for first kth elements.
      So after the first loop try maxi = leftsum before going for second loop in which you are decreasing value of leftsum....try it...may help..

  • @thoughtsofkrishna8963
    @thoughtsofkrishna8963 19 дней назад

    We want Strings playlist Striver

  • @prasannasahoo0806
    @prasannasahoo0806 2 месяца назад +1

    what about the elements in the middle ? how do we reach them ?

  • @DeorajMaharaj
    @DeorajMaharaj 3 месяца назад

    bhaiya aap thora sick ya kaafi stressed lg rhe ho. Wo phle jaisa energy kahi na kahi missing laga.

  • @sanukyadav
    @sanukyadav Месяц назад

    Understood, implementing with pseudocode is not able to pass leetcode test cases! did i miss something!
    def (nums, k):
    lsum = 0
    maxsum = 0
    n = len(nums)
    for i in range(k-1):
    lsum = lsum + nums[i]
    maxsum = lsum
    r_idx = n-1
    maxsum = 0
    rsum = 0
    for i in range(k-1, -1, -1):
    lsum = lsum - nums[i]
    rsum = rsum + nums[r_idx-i]
    r_idx = r_idx - 1
    maxsum = max(maxsum, lsum+rsum)
    return maxsum

    • @vatsalpoddar6660
      @vatsalpoddar6660 Месяц назад

      int maxScore(vector& cardPoints, int k) {
      int n = cardPoints.size();
      int left = k-1;
      int right = n-1;
      int maxSum = INT_MIN;
      int sum = 0;
      for(int i = 0; i < k; i++){
      sum += cardPoints[i];
      }
      maxSum = max(maxSum, sum);
      while(left >= 0){
      sum = sum - cardPoints[left];
      sum = sum + cardPoints[right];
      maxSum = max(maxSum, sum);
      left--;
      right--;
      }
      return maxSum;
      }

    • @Josuke217
      @Josuke217 5 дней назад

      You set maxsum to 0 and r_idx-i is wrong

  • @manasandmohit
    @manasandmohit 3 месяца назад

    Are bhaiya itni fast fast

  • @ManishKumar-dk8hl
    @ManishKumar-dk8hl 3 месяца назад +3

    JAVA BOLNE WALO K LIYE : --
    class Solution {
    public int maxScore(int[] arr, int k) {
    int sum=0;
    for(int i=0;i

  • @user-dr6zr2uo7c
    @user-dr6zr2uo7c 2 месяца назад

    class Solution {
    public:
    int maxScore(vector& nums, int k) {
    int lsum=0;
    int rsum = 0;
    int maxSum = 0;
    int n = nums.size();
    for(int i=0;i=0;i--){
    lsum=lsum-nums[i];
    rsum=rsum+nums[rindex];
    rindex=rindex-1;
    maxSum= max(maxSum,lsum+rsum);
    }
    return maxSum;
    }
    };this code not passing testcases in leetcode

    • @ManishLakkavatri
      @ManishLakkavatri Месяц назад

      the condition in the first loop will be i < k or i

  • @kuldeeprawat-zp7od
    @kuldeeprawat-zp7od 3 месяца назад +1

    Hello everyone, another approach we can think of is
    We can take sum of all the elements and as according to example we want sum of 4 maximum elements with given conditions and the size of array is 9 so actually we can calculate the sum of 5 consecutive elements which is minimum among all and then we can subtract it from the total sum of all the elements of the array

  • @adityaroychowdhury3709
    @adityaroychowdhury3709 3 месяца назад +2

    Subah uthe, aapke darshan hogye. Ab saare contest badia jayenge, sare hard question solve hone lag jayenge 😂😂

  • @ShahNawaz-nk3po
    @ShahNawaz-nk3po Месяц назад

    Another approach with is exactly same as one of the very standard Sliding window question.
    class Solution {
    public:
    int maxScore(vector& cardPoints, int k) {
    // we need to find substring of size (n-k) and minimum sum
    // final answer = is total sum of length n - minimum sum of substring of length (n-k)
    // = maximum sum of length k taken from extreme left/right
    int n = cardPoints.size();
    int l = 0;
    int r = 0;
    int ans = INT_MAX;
    int sum = 0;
    int val = 0;
    for(auto it:cardPoints)
    val+=it;
    while(r(n-k)){
    sum-=cardPoints[l];
    l++;
    }
    if(r-l+1==(n-k))
    ans = min(ans, sum);
    r++;
    }
    return val-ans;

    }
    };

  • @saketjaiswal9381
    @saketjaiswal9381 3 месяца назад

    class Solution {
    public:
    int maxScore(vector& a, int k) {
    int n=a.size();
    int s1=0; int s2=0; int i=0; int j=n-k;
    while(j k. tk loop chala k times o(k)
    {
    s2=s2+a[j];
    j++;
    }
    int maxi = s2;

    j=n-k;
    while(i

  • @ramakrishnakcr4417
    @ramakrishnakcr4417 3 месяца назад

    understood

  • @user-zy6vo1fj3y
    @user-zy6vo1fj3y 9 дней назад

    understood

  • @raghavmanish24
    @raghavmanish24 Месяц назад

    understood