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
Implementing it yourself makes it much more clearer.
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...🙏🙏🙏
codewithmik bhi hai bro
@@Coder_Buzz07 yup
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
Thank you @striver , for making DSA Easy for us . Hatts of to you .
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
Yes...that also makes sense...but what will be time complexity for getting min sum of n-k array size ?
thanks for so easy and intuitive solution. Lot of solutions for this problem are problem which have made it look really complex
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;
}
```
Thanks striver for this amazing video.
understood!Thank you Striver
u are the best bhaiya🤩 eagerly waiting for your string playlist
Understood...........Thank You So Much for this wonderful video..........🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻
Wow......nice approach......
Hi striver, I knew a better solution that solves in single pass.
thanks vvvvv much sir,i really wanted a playlist like this
Thank you so much for the video bro.
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.....
😌
Thank you very much
Understood bhai. Thank you.
Understood. Thanks
great video 😇
NICE SUPER EXCELLENT MOTIVATED
Thanks Brother❤
Thanks❤
it was helpful
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
Understood ❤
OTHER CREATORS! be like "BHAI SAANS THO LENE DE......".
understood
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
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
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
One more approach is finding min_sum of all windows of size array_length - k , array_total_sum - min_sum will be the ans.
🙌🏻
🙌
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
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 !!!
@@mrinceptionist7038 He has questions but there is no solve videos for them
@striver where can i get the source code solution for content
hey striver ... i'm the one of those follower who always give time for like and comment whenever i watch your videos.....thanku again
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
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..
We want Strings playlist Striver
what about the elements in the middle ? how do we reach them ?
You are only allowed to take
bhaiya aap thora sick ya kaafi stressed lg rhe ho. Wo phle jaisa energy kahi na kahi missing laga.
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
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;
}
You set maxsum to 0 and r_idx-i is wrong
Are bhaiya itni fast fast
JAVA BOLNE WALO K LIYE : --
class Solution {
public int maxScore(int[] arr, int k) {
int sum=0;
for(int i=0;i
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
the condition in the first loop will be i < k or i
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
Subah uthe, aapke darshan hogye. Ab saare contest badia jayenge, sare hard question solve hone lag jayenge 😂😂
bro konse year mein ho??
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;
}
};
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
understood
understood
understood