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

Idea 1.  prefix sums + HashMap + modular rules, note: count[0] = 1, (X + X%K)%K for negative values

Time complexity: O(n)

Space complexity: O(K)

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

Idea 1.a count pairs

 class Solution {
public int subarraysDivByK(int[] A, int K) {
int[] count = new int[K];
count[0] = 1;
int prefixSum = 0; for(int a: A) {
prefixSum += a;
prefixSum = (prefixSum % K + K)%K;
++count[prefixSum];
} int result = 0;
for(int val: count) {
result += val*(val-1)/2;
} return result;
}
}

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

  1. [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 ...

  2. 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 解题报告(C++)

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

  4. 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 ...

  5. 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 ...

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

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

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

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

  8. 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 ...

  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. centos 7中监控mysql 数据库脚本(监控端口)

    centos 7中监控mysql 数据库脚本(监控端口) 监控mysql数据库的方法如下: 1.监控端口 netstat -nltp |grep 3306 2.监控进程 ps -ef |grep 33 ...

  2. js延迟

    function sleep(numberMillis) { var now = new Date(); var exitTime = now.getTime() + numberMillis; wh ...

  3. 理解OpenShift(2):网络之 DNS(域名服务)

    理解OpenShift(1):网络之 Router 和 Route 理解OpenShift(2):网络之 DNS(域名服务) 理解OpenShift(3):网络之 SDN 理解OpenShift(4) ...

  4. gogs 源码阅读笔记 001

    gogs 源码阅读笔记 001 gogs项目相当不错,本笔记实际是基于gogs fork版本 git-122a66f. gitea (gitea版本由来)[https://blog.gitea.io/ ...

  5. Quartz的API简介及Jobs和Trigger介绍

    Quartz的API: 主要api: The key interfaces of the Quartz API are: Scheduler - the main API for interactin ...

  6. 笔记本 原来win10系统改装win7系统遇到 invaid signature detected.check secure boot policy setup问题

    这次操作的笔记本电脑是   华硕R414U 大家如果遇到类似问题的话也可以参考这个方法,但是必须搞清楚电脑的型号,型号不同操作起来有差别的 我这里选择的重装系统的方法是最简单粗暴的硬盘安装方法,怎么硬 ...

  7. 【学习】数据处理基础知识(汇总和计算描述统计)【pandas】

    pd对象拥有一组常用的数学和统计方法.大部分都属于约简和汇总统计,用于从Series中单个值,如sum 和 mean 或从DF的行或列中提取一个Series. 1. 描述和汇总统计方法 #汇总和计算描 ...

  8. input输入限制

    1:只能输入两位小数点:function keepTwoPointNum(that){ var val=that.value; if(isNaN(val)){ $(that).val(''); ret ...

  9. kaggle首秀之intel癌症预测(续篇)

    之前写了这篇文章.现在把他搬到知乎live上了.书非借不能读也,因此搞了点小费用,如果你觉得贵,加我微信我给你发红包返回给你. 最近的空余时间拿去搞kaggle了, 好久没更新文章了.今天写写kagg ...

  10. sublime3支持es6语法和vue彩色显示

    支持ES6语法设置: 首先安装nodejs 当然你可以使用其它诸如jsc之类的环境来运行js, 本文使用的是nodejs. 首先确保你的电脑已经安装好nodejs, 并已将其添加到环境变量中 (一般安 ...