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. 为何一线城市的企业更愿意选择 Spring Cloud?

    最近公司正在搭建微服务框架,处于小白的我,赶紧借书,上网,实操的学习了一下,下面是一些自己的入门的总结: 目录: 一.怎么理解Spring Cloud? 一.Spring Cloud 的优势在哪? 一 ...

  2. SpringMVC【二、项目搭建】

    HelloWorld搭建 1.用Maven WebApp框架创建一个项目 红框中的是后添加的 2.添加pom引用(此处因为要引用多个spring包,建议把版本号提出来放到Properties) 会导入 ...

  3. (6)python基础数据类型

    python的六大标准数据类型 (一)Number   数字类型(int float bool complex) (二)String 字符串类型 (三)List 列表类型 (四)Tuple 元组类型 ...

  4. zencart价格筛选插件

    1.首先,新建文件includes\modules\sideboxes\price_range.php <?php function zen_count_products_in_price($p ...

  5. 【小知识】证明 $1$ 到 $n$ 的立方和公式

    scb 发明了小学奥数(确信) Formula \(\sum\limits_{i=1}^n i^3 = (\sum\limits_{i=1}^n i)^2\) Provement 构造一个矩阵 \(a ...

  6. HAL UART DMA 数据收发

    UART使用DMA进行数据收发,实现功能,串口2发送指令到上位机,上位机返回数据给串口2,串口2收到数据后由串口1进行转发,该功能为实验功能 1.UART与DMA通道进行绑定 void HAL_UAR ...

  7. 部署ELK

    1.搭建客户机Nginx ls[root@nginx ~]# hostname nginx [root@nginx ~]# cat /etc/redhat-release CentOS release ...

  8. 01 salt平台,软件架构图

    1.前期调研 1.别人家的 https://www.cnblogs.com/ssyfj/p/9060367.html#top https://www.cnblogs.com/evilliu/artic ...

  9. 【leetcode】1288. Remove Covered Intervals

    题目如下: Given a list of intervals, remove all intervals that are covered by another interval in the li ...

  10. SpringBoot+Mybatis-Plus两种分页方法

    用到的依赖: <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus ...