Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum divisible by K.

Example 1:

Input: A = [4,5,0,-2,-3,1], K = 5
Output: 7
Explanation: There are 7 subarrays with a sum divisible by K = 5:
[4, 5, 0, -2, -3, 1], [5], [5, 0], [5, 0, -2, -3], [0], [0, -2, -3], [-2, -3]

Note:

  1. 1 <= A.length <= 30000
  2. -10000 <= A[i] <= 10000
  3. 2 <= K <= 10000

用了dp,如果某一个数能被K整除,那么以该位结尾的这样的连续子数组的个数就是前一位dp值+1(新的1是它本身)。

如果不能被K整除,那么就让j从i开始往前遍历,碰到第一个从j到i能被K整除的子数组,那么该位上面的长度就是第j位的dp值加上1,新的1指的是从j到i的子数组。

class Solution {
public:
int subarraysDivByK(vector<int>& A, int K) {
vector<int> dp(A.size(),);
vector<int> Bsum(A.size()+, );
for(int i=; i<A.size(); i++){
Bsum[i+] = Bsum[i] + A[i];
}
int ret = ;
for(int i=; i<Bsum.size(); i++){
if(A[i-] % K == ){
if(i- == ) dp[i-] = ;
else dp[i-] = dp[i-] + ;
continue;
}
int newcnt = ;
for(int j=i-; j>=; j--){
//if(Bsum[i] - Bsum[j] == 0) continue;
if( (Bsum[i] - Bsum[j]) % K == ) {
//cout << i << " " << j << endl;
newcnt += dp[j-] + ;
break;
}
}
dp[i-] = newcnt;
}
for(auto x : dp) ret += x;
return ret;
}
};

a better solution

count the remainder.

class Solution {
public int subarraysDivByK(int[] A, int K) {
Map<Integer,Integer> mp = new HashMap<>();
mp.put(0,1);
int prefix = 0, ret = 0;
for(int a : A){
prefix += a;
prefix = (prefix%K+K)%K;
ret += mp.getOrDefault(prefix, 0);
mp.put(prefix, mp.getOrDefault(prefix,0)+1);
}
return ret;
}
}

LC 974. Subarray Sums Divisible by K的更多相关文章

  1. 974. Subarray Sums Divisible by K

    Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum ...

  2. 【LeetCode】974. Subarray Sums Divisible by K 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 前缀和求余 日期 题目地址:https:/ ...

  3. 【leetcode】974. Subarray Sums Divisible by K

    题目如下: Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have ...

  4. 「Leetcode」974. Subarray Sums Divisible by K(Java)

    分析 这题场上前缀和都想出来了,然后就没有然后了...哭惹.jpg 前缀和相减能够得到任意一段连续区间的和,然后他们取余\(K\)看余数是否为0就能得到.这是朴素的遍历算法.那么反过来说,如果两个前缀 ...

  5. Leetcode 974. Subarray Sums Divisible by K

    前缀和(prefix sum/cumulative sum)的应用. 还用了一个知识点: a≡b(mod d) 则 a-b被d整除. 即:a与b对d同余,则a-b被d整除. class Solutio ...

  6. [Swift]LeetCode974. 和可被 K 整除的子数组 | Subarray Sums Divisible by K

    Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum ...

  7. Subarray Sums Divisible by K LT974

    Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum ...

  8. 119th LeetCode Weekly Contest Subarray Sums Divisible by K

    Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum ...

  9. [LeetCode] Subarray Product Less Than K 子数组乘积小于K

    Your are given an array of positive integers nums. Count and print the number of (contiguous) subarr ...

随机推荐

  1. WaitType:ASYNC

    项目组有一个数据库备份的Job运行异常,该Job将备份数据存储到remote server上,平时5个小时就能完成的备份操作,现在运行19个小时还没有完成,backup命令的Wait type是 AS ...

  2. OpenCV视觉处理核心课程

    OpenCV视觉处理核心课程 观看链接:https://www.bilibili.com/video/av29500928?from=search&seid=47008639320014639 ...

  3. AGC刷题记

    已经刷不了几天了... AGC001 A-BBQ Easy 排个序就过了 B-Mysterious Light 手膜一下,你会发现魔改一下\(gcd\)就行了 C-Shorten Diameter 刚 ...

  4. docker: Error response from daemon: invalid mount config for type "bind": bind source path does not exist: /tmp/tfserving/

    注意要是当前的完整路径 pwd查看到完整路径,再加入到source里面即可

  5. C# LINQ(10)

    LINQ 查询 var query = from r in Formula1.GetChampions() where r.Country == "Brazil" orderby ...

  6. Codeforces Round #454 Div. 1 [ 906A A. Shockers ] [ 906B B. Seating of Students ] [ 906C C. Party ]

    PROBLEM A. Shockers 题 http://codeforces.com/contest/906/problem/A 906A 907C 解 水题,按照题意模拟一下就行了 如果是 ‘ ! ...

  7. String类的format方法的用法

    public class Test { public static void main(String[] args) { String url = "https://api.weixin.q ...

  8. 3 Servlet、Filter使用

    1 使用Servlet获取数据 使用Servlet获取前端的数据,在后端从控制台中打印出前端的数据,前端页面如下图 前端页面程序:需要注意的是form的提交,以及input的不同类型对应的显示不同 & ...

  9. [引用]MATLAB中的fft后为何要用fftshift

    原文地址:MATLAB中的fft后为何要用fftshift fft是一维傅里叶变换,即将时域信号转换为频域. fftshift是针对频域的,将FFT的DC分量移到频谱中心,重新排列fft,fft1和… ...

  10. CSS实现太极效果

    这个伪元素的位置对齐还妹搞明白 需要再研究研究   <html> <head> <title>taiji</title> <style> b ...