Subarray Sums Divisible by K LT974
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 <= A.length <= 30000
-10000 <= A[i] <= 10000
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的更多相关文章
- [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 ...
- 974. Subarray Sums Divisible by K
Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum ...
- 【LeetCode】974. Subarray Sums Divisible by K 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 前缀和求余 日期 题目地址:https:/ ...
- 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 ...
- 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 ...
- 【leetcode】974. Subarray Sums Divisible by K
题目如下: Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have ...
- 「Leetcode」974. Subarray Sums Divisible by K(Java)
分析 这题场上前缀和都想出来了,然后就没有然后了...哭惹.jpg 前缀和相减能够得到任意一段连续区间的和,然后他们取余\(K\)看余数是否为0就能得到.这是朴素的遍历算法.那么反过来说,如果两个前缀 ...
- 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 ...
- [LeetCode] Subarray Product Less Than K 子数组乘积小于K
Your are given an array of positive integers nums. Count and print the number of (contiguous) subarr ...
随机推荐
- oracle12建立非C##用户并且导入数据
由于要导入dmp文件,所以想建立和oracle11一样的用户,折腾了半天,记录一下过程: 1.进入sqlplus,建立用户和分配权限 cmd>sqlplus /nolog SQL>conn ...
- # 20175227 2018-2019-2 《Java程序设计》第一周学习总结
20175227 2018-2019-2 <Java程序设计>第一周学习总结 教材学习内容总结 1.安装VB,Ubuntu,Git,JDK,并自行配置. 2.写"Hello Wo ...
- 软件-集成开发环境:IDEA(Java 语言开发的集成环境)
ylbtech-软件-集成开发环境:IDEA(Java 语言开发的集成环境) IDEA 全称IntelliJ IDEA,是用于java语言开发的集成环境(也可用于其他开发语言),IntelliJ在业界 ...
- npm安装与使用
NPM 使用介绍 摘自:http://www.runoob.com/nodejs/nodejs-npm.html NPM是随同NodeJS一起安装的包管理工具,能解决NodeJS代码部署上的很多问题, ...
- The Weather
后台: <?php //接受查询的城市 $city = $_GET['city']; //连接redis $redis = new redis(); $redis->connect(&qu ...
- 小程序打开pdf
wx.downloadFile({ url:"https://xxxxxx.pdf", success(res){ console.log(res) let data = res. ...
- js中的数值转换
js中有3个函数可以把非数值转换为数值:Number().parseInt().parseFloat().其中Number()可以用于任何数据类型.parseInt()及parseFloat()用于将 ...
- 谈谈线上CPU100%排查套路
知识点总结 ---------------------------------------------------------------------------------------------- ...
- Django Forms 表单
环境 python 3.7 服务端 views.py from django import forms # 引入 froms 模块 from django.forms import widgets ...
- 关于spring的一些注解