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. 13.MySQL锁机制

    锁的分类 从对数据的类型 (读\写)分: 1.读锁(共享锁):针对同一份数据,多个读操作可以同时进行而不会互相影响 2.写锁(排它锁):当前写操作没有完成前,它会阻断其他写锁和读锁 从对数据操作的粒度 ...

  2. LINUX 使用grep命令查看某个指定时间段的日志

    今天查看订单重复的问题,由于订单生成已经有一段时间了,所以我必须精准进行日志查询.开始用的是sed 命令查询法,后来改成了grep查询,很方便. 命令: grep '时间' '日志文件名 ' 例如:我 ...

  3. WebLogic 12c Linux 命令行 静默安装

    CentOS 6.3安装配置Weblogic 10  http://www.linuxidc.com/Linux/2014-02/96918.htm Oracle WebLogic 11g 安装部署文 ...

  4. linux 下安装 jdk1.7

    1.官网 下载jdk7版本 地址: http://www.oracle.com/technetwork/java/javase/downloads/java-archive-downloads-jav ...

  5. Hadoop_20_MapReduce程序的运行模式

    1.MapReduce程序的运行模式 1. Windows中运行MapReduce程序 (1)mapreduce程序是被提交给LocalJobRunner在本地以单进程的形式运行 (2)而处理的数据及 ...

  6. [shell] shell echo打印换行的方法

    echo要支持同C语言一样的\转义功能,只需要加上参数-e,如下所示: echo -e hello \n echo \n

  7. sql 拼接字符串单条拆分多条

    SELECT * FROM ( SELECT A.WS_ID , B.NEXT_OPERATOR FROM ( SELECT WS_ID , [NEXT_OPERATOR] = CONVERT(XML ...

  8. IDEA常见问题和设置

    1.查看方法的文档:快捷键 Ctrl-Q(Ctrl-J(mac)) 2.历史记录 3.IDEA控制台输出中文乱码问题 4.修改Idea默认的maven等全局设置 5.idea 启动时报错javax.i ...

  9. set调用add报错:

    Map<String, String> goodsStandMap = new HashMap<>();goodsStandMap.put("key1", ...

  10. Linux之df磁盘信息

    df命令用于查看磁盘的分区,磁盘已使用的空间,剩余的空间 1.用法 df [选项] [文件..] 2.命令选项 -a,--all 全部文件系统-h,--human-readable 以以合适的单位来显 ...