-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
183 additions
and
0 deletions.
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
striver/prefixSum/Maximum Points You Can Obtain from Cards.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#include<bits/stdc++.h> | ||
using namespace std; | ||
|
||
class Solution { | ||
public: | ||
int maxScore(vector<int>& v, int k) { | ||
int n = v.size(); | ||
int l = 0; | ||
int r = n-k;// l and r, sliding window range | ||
int total = 0;//total points | ||
total = accumulate(v.begin(), v.end(), total);//sum of numbers not taken into acc | ||
int tmp = 0;//we are not taking these eles, just using as a window | ||
for(int i= l; i<r; ++i)tmp+=v[i];//total sum of current window (1st window) | ||
int res = total - tmp;//numbers taken into consideration, all from right | ||
while(r<n){//till right of slide window does not exceeds length | ||
tmp-=v[l++];//removing first one from window | ||
tmp+=v[r++];//adding extra when moving right | ||
res = max(total-tmp,res);//total - temp is what we are taking into consideration | ||
} | ||
return res; | ||
} | ||
}; | ||
|
||
int main(int argc, char const *argv[]) | ||
{ | ||
/* code */ | ||
return 0; | ||
} |
37 changes: 37 additions & 0 deletions
37
striver/prefixSum/Minimum Operations to Reduce X to Zero.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#include<bits/stdc++.h> | ||
using namespace std; | ||
|
||
|
||
class Solution { | ||
public: | ||
int minOperations(vector<int>& v, int x) { | ||
unordered_map<int,int>mp;//prefix sum with index | ||
mp[0]=0;//avoid index out of bound error | ||
int n = v.size(); | ||
int sum = 0;//total sum of arr | ||
for(int i=0; i<n; ++i){ | ||
sum+=v[i]; | ||
mp[sum]=i; | ||
} | ||
if(x>sum)return -1; | ||
int longest = 0;//longest window, that we dont include | ||
sum = sum-x;//longest windows sum should be this | ||
int val=0;//we take all the values, will check at any index we have sum val = rest val | ||
int ans = INT_MIN; | ||
for(int i=0; i<n; ++i){ | ||
val+=v[i];//keeping track of prefix sum | ||
if(mp.find(val-sum)!=mp.end()){//if present | ||
//no elements on left part | ||
if(val-sum==0)longest=max(longest,i-mp[val-sum]+1);//taking it | ||
else longest=max(longest,i-mp[val-sum]);//elements in left part | ||
} | ||
} | ||
return longest==0?(sum==0?n:-1):n-longest; | ||
} | ||
}; | ||
|
||
int main(int argc, char const *argv[]) | ||
{ | ||
/* code */ | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
class Solution { | ||
public: | ||
int minSubArrayLen(int x, vector<int>& v) { | ||
int n = v.size(); | ||
int mi = INT_MAX; | ||
int s = 0;//curr subarray | ||
int i=0, j=0; | ||
while(j<n){ | ||
s+=v[j];//adding curr ele to sum | ||
if(s>=x){ | ||
//till its greater than target we try to compress from left side | ||
while(s>=x){ | ||
s-=v[i]; | ||
i++; | ||
} | ||
//after we compress, we get curr window >= x | ||
mi = min(mi,j-i+1+1);//+1 for getting the previous element | ||
} | ||
j++; | ||
} | ||
return mi==INT_MAX?0:mi; | ||
} | ||
}; | ||
|
||
int main(int argc, char const *argv[]) | ||
{ | ||
/* code */ | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#include<bits/stdc++.h> | ||
using namespace std; | ||
|
||
class Solution { | ||
public: | ||
vector<int> productExceptSelf(vector<int>& v) { | ||
int n = v.size(); | ||
if(n<1)return {}; | ||
vector<int>left; | ||
int pro=1; | ||
for(int i=0; i<n; ++i){ | ||
pro*=v[i]; | ||
left.emplace_back(pro); | ||
} | ||
//o/p array updated with left cumulative product vals | ||
pro=1;//real time cumulative product vals from right side | ||
for(int i=n-1; i>0; --i){//from rev direction | ||
left[i]=left[i-1]*pro;//i-1 because left of that index will have cm of left | ||
pro*=v[i];//updating right cm | ||
} | ||
left[0]=pro;//no eles in left side so cm of right | ||
return left; | ||
} | ||
}; | ||
|
||
|
||
int main(int argc, char const *argv[]) | ||
{ | ||
/* code */ | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#include <bits/stdc++.h> | ||
using namespace std; | ||
|
||
class NumArray { | ||
public: | ||
vector<int> pref; | ||
NumArray(vector<int>& nums) { | ||
int n = nums.size(); | ||
pref.clear(); | ||
for(int i = 0; i < n; i++){ | ||
if(i == 0)pref.push_back(nums[i]); | ||
else pref.push_back(nums[i] + pref[i - 1]); | ||
} | ||
} | ||
int sumRange(int left, int right) { | ||
if(left == 0)return pref[right];//because it's the cum sum till that point | ||
//no ele in left | ||
return pref[right] - pref[left - 1]; | ||
} | ||
}; | ||
|
||
int main(int argc, char const *argv[]) | ||
{ | ||
/* code */ | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#include<bits/stdc++.h> | ||
using namespace std; | ||
|
||
class Solution { | ||
public: | ||
|
||
int subarraySum(vector<int>& a, int k) { | ||
int n = a.size(); | ||
if(n==0)return 0; | ||
unordered_map<int,int>mp;//prefix sum with num of occurence | ||
int curSum = 0;//pre sum | ||
int i = 0;//itr | ||
int cnt = 0; | ||
while(i<n){ | ||
curSum+=a[i]; | ||
if(curSum==k)cnt++;//found a new sub array with sum k | ||
if(mp.find(curSum-k)!=mp.end())cnt+=mp[curSum-k]; | ||
mp[curSum]++; | ||
++i; | ||
} | ||
return cnt; | ||
} | ||
}; | ||
|
||
int main(int argc, char const *argv[]) | ||
{ | ||
/* code */ | ||
return 0; | ||
} |