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. 5.1 Request 获取请求数据的几种方法

    //获取请求头和请求数据 //请求数据(1.通过超链接 2.通过表单) //获取请求数据的时候一般来说 都要先检查 再使用 public class RequestDemo2 extends Http ...

  2. 【Java并发】基础

    一.概述 1.1 线程与进程区别 1.2 多线程引发的性能问题 二.多线程创建方式 2.1 第一种-继承Thread类 2.2 第二种-实现Runnable接口 2.3 第三种-实现Callable接 ...

  3. Win7系统不能拖动文件夹的问题怎么解决?

    一般情况下,如果我们想要在电脑中移动文件夹,可以采用剪切复制粘贴或者按住鼠标左键拖动的方式来实现,但有些Win7系统用户反映文件夹会出现不能移动的情况,这是怎么回事呢?下面好系统U盘启动就为大家介绍一 ...

  4. RT-Thread代码启动过程与$Sub$ $main、$Super$ $main

    文章转载自:https://blog.csdn.net/yang1111111112/article/details/80913001 我们找到系统复位的地方,可以往下单步跟踪. ①从系统初始化开始执 ...

  5. npm 安装指定版本的包

    使用 包名@版本号 指定, 例如,安装 Express 3.21.2, $ npm

  6. 洛谷P1462 通往奥格瑞玛的道路(SPFA+二分答案)

    题目背景 在艾泽拉斯大陆上有一位名叫歪嘴哦的神奇术士,他是部落的中坚力量 有一天他醒来后发现自己居然到了联盟的主城暴风城 在被众多联盟的士兵攻击后,他决定逃回自己的家乡奥格瑞玛 题目描述 在艾泽拉斯, ...

  7. tsung报告中Transactions Statistics缺失问题

    长时间没有做性能测试,最近在使用tsung进行性能测试时,修改tsung自带的范例脚本后,运行查看结果时,发现测试报告中transactions  statistics缺失,刚开始一直以为是监控中的配 ...

  8. tensorflow 更新出现HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out.

    pip install --upgrade tensorflow --default-timeout=1000

  9. [ES2015] Number.isNaN instead of isNaN

    const result = Number('55px'); // isNaN "NaN" console.log(isNaN(result)); // true console. ...

  10. [React] Create a Persistent Reference to a Value Using React useRef Hook

    The useRef is a hook for creating values that persist across renders. In this lesson we'll learn how ...