class Solution {
public:
int subarraySum(vector<int>& nums, int k) {
unordered_map<int, int> mp;
mp[0] = 1;
int count = 0;
int n = nums.size();
vector<int> pre(n+1);
for(int i{};i<n;++i){
pre[i+1] = pre[i] + nums[i];
if (mp.find(pre[i+1]- k) != mp.end()) {
count += mp[pre[i+1]- k];
}
mp[pre[i+1]]++;
}
return count;
}
};
class Solution {
public:
int numberOfSubarrays(vector<int>& nums, int k) {
unordered_map<int, int> mp;
mp[0] = 1;
int count = 0;
int n = nums.size();
vector<int> pre(n+1);
for(int i{};i<n;++i){
pre[i+1] = pre[i] + (nums[i]&1);
if (mp.find(pre[i+1]- k) != mp.end()) {
count += mp[pre[i+1]- k];
}
mp[pre[i+1]]++;
}
return count;
}
};
class Solution {
public:
int findMaxLength(vector<int>& nums) {
unordered_map<int, int> mp;
mp[0] = -1;
int result= 0;
int n = nums.size();
vector<int> pre(n+1);
for(int i{};i<n;++i){
pre[i+1] = pre[i] + (nums[i] == 0 ? -1 : nums[i]);
if (mp.find(pre[i+1]) != mp.end()) {
int j = mp[pre[i+1]];
result = max(result,i-j);
}else mp[pre[i+1]] = i;
}
return result;
}
};
class Solution {
public:
int longestWPI(vector<int>& hours) {
unordered_map<int, int> mp;
int result= 0;
int n = hours.size();
vector<int> pre(n+1);
for(int i{};i<n;++i){
pre[i+1] = pre[i] + (hours[i] > 8 ? 1 : -1);
if(!mp.count(pre[i+1]))
mp[pre[i+1]] = i;
if(pre[i+1] > 0){
result = i+1;
continue;
}
if(mp.count(pre[i+1]-1)){
result = max(result,i-mp[pre[i+1]-1]);
}
}
return result;
}
};
class Solution {
public:
vector<string> findLongestSubarray(vector<string>& array) {
unordered_map<int, int> mp;
mp[0] = -1;
int result= 0;
int n = array.size();
vector<int> pre(n+1);
vector<pair<int,int>> vp;
for(int i{};i<n;++i){
pre[i+1] = pre[i] + (isdigit(array[i][0])? -1 : 1);
if (mp.find(pre[i+1]) != mp.end()) {
int j = mp[pre[i+1]];
if(i-j > result){
result = i-j;
vp.clear();
vp.push_back({j,i});
}else if(i-j == result){
vp.push_back({j,i});
}
}else mp[pre[i+1]] = i;
}
if(vp.size() == 0) return {};
sort(vp.begin(),vp.end());
return vector<string>(array.begin()+vp[0].first + 1,array.begin()+vp[0].second+1);
}
};
class Solution {
public:
int subarraySum(vector<int>& nums, int k) {
unordered_map<int, int> mp;
mp[0] = 1;
int n = nums.size();
int result{};
vector<int> pre(n+1);
for(int i{};i<n;++i){
pre[i+1] = pre[i] + nums[i];
int preMod = (pre[i+1]%k + k)%k;
if (mp.find(preMod) != mp.end()) {
result += mp[preMod];
}
mp[preMod]++;
}
return result ;
}
};
class Solution {
public:
int subarraySum(vector<int>& nums, int k) {
unordered_map<int, int> mp;
mp[0] = -1;
int n = nums.size();
int result{};
vector<int> pre(n+1);
for(int i{};i<n;++i){
pre[i+1] = pre[i] + nums[i];
int preMod = ((long long)pre[i+1]%k + k)%k;
if (mp.find(preMod) != mp.end()) {
int left = mp[preMod];
if(i-left >= 2) return true;
}else{
mp[preMod] = i;
}
}
return false;
}
};
class Solution {
public:
int numOfSubarrays(vector<int>& arr) {
const int MODULO = 1000000007;
int odd{};
int even{1};
int result{};
int n = arr.size();
vector<int> pre(n+1);
for(int i{};i < n;++i){
pre[i+1] = pre[i] + arr[i];
int preMod = pre[i+1]%2;
result = (result + (preMod == 0 ?odd : even))%MODULO;
if(preMod == 0) even++;
else odd++;
}
return result;
}
};
class Solution {
public:
int minSubarray(vector<int>& nums, int p) {
unordered_map<int, int> mp;
mp[0] = -1;
int n = nums.size();
int result{n};
long long sums = accumulate(nums.begin(),nums.end(),0.0);
if(sums < p) return -1;
int mod = sums % p;
if(0 == mod) return 0;
vector<long long> pre(n+1);
for(int i{};i<n;++i){
pre[i+1] = pre[i] + nums[i];
int curMod = pre[i+1] % p;
int target = (curMod - mod + p) % p;
if (mp.find(target) != mp.end()) {
int j = mp[target];
result = min(i-j,result);
}
mp[curMod] = i;
}
return result == n ? -1 : result;
}
};
|