Description
Submission
class Solution {
public:
int minSubarray(vector<int>& nums, int p) {
unordered_map<int, int> Map;
Map[0] = -1;
int r0 = 0;
int n = nums.size();
for(int num: nums) {
r0 = (r0 + num) % p;
}
if(r0 == 0) return r0;
int presum = 0;
int ret = INT_MAX;
for(int i = 0; i < n; ++i) {
presum = (presum + nums[i]) % p;
int diff = (p + presum - r0) % p;
if(Map.find(diff) != Map.end()) {
ret = min(i - Map[diff], ret);
}
Map[presum] = i;
}
if(ret < nums.size()) {
return ret;
}
return -1;
}
};