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

Approach #1: HashMap. [Java]

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

  

Approach #2: Optimize. [Java]

class Solution {
public int subarraysDivByK(int[] A, int K) {
int[] map = new int[K];
int sum = 0, count = 0;
map[0] = 1;
for (int a : A) {
sum = (sum + a) % K;
if (sum < 0) sum += K;
count += map[sum];
map[sum]++;
}
return count;
}
}

  

Analysis:

About this problems - sum of contigous subarray, prefix sum is a common techinque.

Another thisng is if sum[0, i] % K == sum[0, j] % K, sum[i+1, j] is divisible by K. So for current index j, we need to find out how many index i (i < j) exit that has the same mod for K. Now it easy to come up with HashMap <mod, frequency>

Time complexity: O(N)

Space complexity: O (N)

Reference:

https://leetcode.com/problems/subarray-sums-divisible-by-k/discuss/217980/Java-O(N)-with-HashMap-and-prefix-Sum

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

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

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

  2. LC 974. Subarray Sums Divisible by K

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

  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. apt-get clean 清除 apt 的缓存

    #apt-get clean 再 df -h 看看,是不是可用空间增加了几个 G ?

  2. 2018.09.15 vijos1053Easy sssp(最短路)

    传送门 貌似可以最短路时同时判定负环啊. 但我不想这样做. 于是写了一个dfs版的判环,bfs版的求最短路. 代码: #include<iostream> #include<ccty ...

  3. [operator]Ubuntu server 18 设置静态IP

    root@ubuntu-MesosMaster-Marathon:~# cat /etc/netplan/-cloud-init.yaml # This file is generated from ...

  4. BZOJ 1011 [HNOI2008]遥远的行星 (误差分析)

    1011: [HNOI2008]遥远的行星 Time Limit: 10 Sec  Memory Limit: 162 MBSec  Special JudgeSubmit: 4974  Solved ...

  5. day03(接口,多态)

    接口:            概念:是功能的集合,可以当做引用数据类型的一种.比抽象类更加抽象. 接口的成员:               成员变量:必须使用final修饰 默认被 public &a ...

  6. 在 JNI 编程中避免内存泄漏与崩溃

    JNI 编程简介 JNI,Java Native Interface,是 native code 的编程接口.JNI 使 Java 代码程序可以与 native code 交互——在 Java 程序中 ...

  7. B样条参数曲线学习(1)

    B样条参数曲线学习 Bezier曲线有许多优越性,但有两点不足: (1) 特征多边形的顶点个数决定了Bezier曲线的阶次,并且在阶次较大时,特征多边形对曲线的控制将会减弱: (2) Bezier曲线 ...

  8. hdu 5039 线段树+dfs序

    http://acm.hdu.edu.cn/showproblem.php?pid=5039 给定一棵树,边权为0/1.m个操作支持翻转一条边的权值或者询问树上有多少条路径的边权和为奇数. 用树形df ...

  9. Create a site by Google Site - All Free

    Follow this link :  https://www.google.com/sites/help/intl/en/overview.html

  10. string 和String的区别

    string 是 System.String 的别名,习惯上,我们把字符串当作对象时(有值的对象实体),我们用string.而我们把它当类时(需要字符串类中定义的方法),我们用String,比如: s ...