g2801_2900.s2845_count_of_interesting_subarrays.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java Show documentation
Show all versions of leetcode-in-java Show documentation
Java-based LeetCode algorithm problem solutions, regularly updated
The newest version!
package g2801_2900.s2845_count_of_interesting_subarrays;
// #Medium #Array #Hash_Table #Prefix_Sum #2023_12_13_Time_27_ms_(97.76%)_Space_62.7_MB_(26.12%)
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Solution {
public long countInterestingSubarrays(List nums, int modulo, int k) {
int prefixCnt = 0;
Map freq = new HashMap<>();
freq.put(0, 1);
long interestingSubarrays = 0;
for (Integer num : nums) {
if (num % modulo == k) {
prefixCnt++;
}
int expectedPrefix = (prefixCnt - k + modulo) % modulo;
interestingSubarrays += freq.getOrDefault(expectedPrefix, 0);
freq.put(prefixCnt % modulo, freq.getOrDefault(prefixCnt % modulo, 0) + 1);
}
return interestingSubarrays;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy