-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathsolution.java
More file actions
39 lines (30 loc) · 1.06 KB
/
solution.java
File metadata and controls
39 lines (30 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import java.util.*;
class Solution {
public int minSubarray(int[] nums, int p) {
long total = 0;
for (int x : nums) {
total = (total + x) % p; // keep modulo to avoid overflow
}
int need = (int) total;
if (need == 0)
return 0; // already divisible
int n = nums.length;
Map<Integer, Integer> lastIndex = new HashMap<>();
lastIndex.put(0, -1); // prefix before any element
int ans = n;
long prefix = 0;
for (int i = 0; i < n; i++) {
prefix = (prefix + nums[i]) % p;
int prefMod = (int) prefix;
int target = prefMod - need;
if (target < 0)
target += p; // (prefMod - need + p) % p
if (lastIndex.containsKey(target)) {
ans = Math.min(ans, i - lastIndex.get(target));
}
// store latest index for this prefix remainder
lastIndex.put(prefMod, i);
}
return ans == n ? -1 : ans;
}
}