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

Example 1:

  1. Input: A = [4,5,0,-2,-3,1], K = 5
  2. Output: 7
  3. Explanation: There are 7 subarrays with a sum divisible by K = 5:
  4. [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)

  1. class Solution {
  2. public int subarraysDivByK(int[] A, int K) {
  3. int[] count = new int[K];
  4. count[0] = 1;
  5. int prefixSum = 0;
  6.  
  7. int result = 0;
  8. for(int a: A) {
  9. prefixSum += a;
  10. prefixSum = (prefixSum % K + K)%K;
  11. result += count[prefixSum];
  12. ++count[prefixSum];
  13. }
  14.  
  15. return result;
  16. }
  17. }

Idea 1.a count pairs

  1. class Solution {
  2. public int subarraysDivByK(int[] A, int K) {
  3. int[] count = new int[K];
  4. count[0] = 1;
  5. int prefixSum = 0;
  6.  
  7. for(int a: A) {
  8. prefixSum += a;
  9. prefixSum = (prefixSum % K + K)%K;
  10. ++count[prefixSum];
  11. }
  12.  
  13. int result = 0;
  14. for(int val: count) {
  15. result += val*(val-1)/2;
  16. }
  17.  
  18. return result;
  19. }
  20. }

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. json 数据在textarea中显示的时候,切换 beauty和ugly模式

    转化为beauty模式 var jsonText = $('#json').val(); $('#json').val(JSON.stringify(JSON.parse(jsonText), nul ...

  2. WIN7 WIN10赋予文件或者文件夹完全访问权限

    WIN7 WIN10赋予文件或者文件夹完全访问权限win7文件夹图标中多了一把小锁打不开文件夹怎么办?解决办法一:右击目录→取得管理员权限!该方法适用于win7旗舰版.解决办法二:添加everyone ...

  3. 获得驱动器信息卷设备&&Ring3得到磁盘文件系统(NTFS WIN10)

    // GetLogicalDriveStrings.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <Windows ...

  4. 杂谈3.py

    bin() --------十转二 hex()------- 十转十六 oct()-------十转八 import math math.floor(数值)返回小于等于数值的整数 math.trunc ...

  5. sqlite--一秒20万数据

    参考博文:https://blog.csdn.net/weixin_35261786/article/details/78222602 #include <iostream> #inclu ...

  6. IDLE的GUI交互模式下完美清屏

    IDLE的GUI交互模式下完美清屏==============================================================================1.首先把 ...

  7. leetcode20

    public class Solution { Stack<char> S = new Stack<char>(); public bool IsValid(string s) ...

  8. Sphinx 安装与使用

    Sphinx 优点 高速索引(接近10M/S) 高速搜索(2-4G文本搜索耗时不到0.1秒) 高可用性(单CPU支持100GB文本,100M文档) 提供相关性排名.分布式搜索.文档摘要(高亮显示) S ...

  9. pytho学习笔记---编码

    编解码 ASCII:1字节,0-255 GBK2313:常用的汉字,2万多个 GBK:对GBK2313的补充,支持藏文,2个字节表示一个汉字 big5:台湾,繁体字 unicode:万国码,2-4字节 ...

  10. nexus的安装和简介(3)

    从私服下载jar包  没有配置nexus之前,如果本地仓库没有,去中央仓库下载,通常在企业中会在局域网内部署一台私服服务器,有了私服本地项目首先去本地仓库找jar,如果没有找到则连接私服从私服下载ja ...