Skip to content

Commit

Permalink
Create 1983.Widest-Pair-of-Indices-With-Equal-Range-Sum.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
wisdompeak authored Aug 29, 2021
1 parent 153e267 commit 3721983
Showing 1 changed file with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
class Solution {
public:
int widestPairOfIndices(vector<int>& nums1, vector<int>& nums2)
{
int sum = 0;
int n = nums1.size();
vector<int>pre1(n+1);
vector<int>pre2(n+1);

for (int i=1; i<=n; i++)
pre1[i] = pre1[i-1]+nums1[i-1];
for (int i=1; i<=n; i++)
pre2[i] = pre2[i-1]+nums2[i-1];

vector<int>diff(n+1);
for (int i=0; i<=n; i++)
diff[i] = pre1[i]-pre2[i];

unordered_map<int,int>Map;
Map[0]=0;
int ret = 0;
for (int i=1; i<=n; i++)
{
if (Map.find(diff[i])!=Map.end())
ret = max(ret, i-Map[diff[i]]);
else
Map[diff[i]] = i;
}
return ret;

}
};

0 comments on commit 3721983

Please sign in to comment.