974. Subarray Sums Divisible by K
Given an array
A
of integers, return the number of (contiguous, non-empty) subarrays that have a sum divisible byK
.
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
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:
974. Subarray Sums Divisible by K的更多相关文章
- 【LeetCode】974. Subarray Sums Divisible by K 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 前缀和求余 日期 题目地址:https:/ ...
- 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 ...
- [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 ...
- Subarray Sums Divisible by K LT974
Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have a sum ...
- 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 ...
- [LeetCode] Subarray Product Less Than K 子数组乘积小于K
Your are given an array of positive integers nums. Count and print the number of (contiguous) subarr ...
随机推荐
- js-移动端android浏览器中input框被软键盘遮住的问题解决方案
我遇到的问题:在一个页面里有一个弹出层之前我给我的最外层加了固定定位 用了下面的方法也不好使:没有办法我将之改为绝对定位层级变高在加上一个顶部标签通过js计算顶部高度来实现满屏遮挡: <sect ...
- 轮播图jQuery
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- OpenSSL 结构体
X509_STORE 头文件:x509_vfy.h 定义 typedef struct x509_store_st X509_STORE; struct x509_store_st { /* The ...
- db2 批处理
db2在Windows下执行批处理,需要使用两个.bat文件 1)把以下命令保存为first_do.bat@echo off@@ECHO ------------------------------- ...
- process_创建进程
import multiprocessingimport time#方式一def worker(interval): n = 5 while n > 0: print("The tim ...
- js 和 css 中 不能使用 jsp 页面中一些 标签 和 java 代码等,应注意
js 和 css 中 不能使用 jsp 页面中一些 标签 和 java 代码等,应注意 如 ${ } <%%> 等
- 修改python ide的主题,颜色
1.找到这个名叫config-highlight.cfg文件后接下来就需要编辑它了 2. 贴上: [Obsidian] definition-foreground = #678CB1 error-fo ...
- 2018.07.17 CQOI2017 余数求和(整除分块)
洛谷传送门 bzoj传送门 这道题要用到学习莫比乌斯反演时掌握的整除分块算法,也就是对于一个数n" role="presentation" style="pos ...
- Vim配置(转)
1.按F5可以直接编译并执行C.C++.java代码以及执行shell脚本,按“F8”可进行C.C++代码的调试 2.自动插入文件头 ,新建C.C++源文件时自动插入表头:包括文件名.作者.联系方式. ...
- IntelliJ IDEA 2017版 spring-boot基础补充,原理详解
一.Spring发展史 1.Spring1.x 版本一时代主要是通过XML文件配置bean,在java和xml中不断切换,在学习java web 初期的时候经常使用 2.Spring2 ...