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 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
用了dp,如果某一个数能被K整除,那么以该位结尾的这样的连续子数组的个数就是前一位dp值+1(新的1是它本身)。
如果不能被K整除,那么就让j从i开始往前遍历,碰到第一个从j到i能被K整除的子数组,那么该位上面的长度就是第j位的dp值加上1,新的1指的是从j到i的子数组。
class Solution {
public:
int subarraysDivByK(vector<int>& A, int K) {
vector<int> dp(A.size(),);
vector<int> Bsum(A.size()+, );
for(int i=; i<A.size(); i++){
Bsum[i+] = Bsum[i] + A[i];
}
int ret = ;
for(int i=; i<Bsum.size(); i++){
if(A[i-] % K == ){
if(i- == ) dp[i-] = ;
else dp[i-] = dp[i-] + ;
continue;
}
int newcnt = ;
for(int j=i-; j>=; j--){
//if(Bsum[i] - Bsum[j] == 0) continue;
if( (Bsum[i] - Bsum[j]) % K == ) {
//cout << i << " " << j << endl;
newcnt += dp[j-] + ;
break;
}
}
dp[i-] = newcnt;
}
for(auto x : dp) ret += x;
return ret;
}
};
a better solution
count the remainder.
class Solution {
public int subarraysDivByK(int[] A, int K) {
Map<Integer,Integer> mp = new HashMap<>();
mp.put(0,1);
int prefix = 0, ret = 0;
for(int a : A){
prefix += a;
prefix = (prefix%K+K)%K;
ret += mp.getOrDefault(prefix, 0);
mp.put(prefix, mp.getOrDefault(prefix,0)+1);
}
return ret;
}
}
LC 974. Subarray Sums Divisible by K的更多相关文章
- 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:/ ...
- 【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 ...
随机推荐
- golang整数与小数间的加减乘除
我们假设你需要 整数与小数一起进行运算,或者 整数除以整数 得到小数这种运算 如果你使用了decimal, 那么之后所有的运算你都必须使用decimal, 因为通过它计算出来的结果的类型统统为deci ...
- python基础:流程控制案例:
1,简述编译型与解释型的语言,且分别列出你知道的哪些语言属于编译型,哪些属于解释型. 答:简单理解编译型语言类似谷歌翻译,整篇读入整篇翻译,代表语言有C语言,解释型语言类似同 声传译,读入一行翻译 ...
- Shell 语法报错记录
sh: missing ] if 条件语句 “或”多个条件并行时 执行then命令 变量a等于aa且变量b等于bb 或者 变量c等于cc且变量d等于dd 这样的条件成立的话,输出success if ...
- ubuntu下log4cxx安装使用
需要安装log4cxx,安装的过程中可是充满了坎坷...最大的问题是在make log4cxx时,总是报undefined XML什么什么的错误,查了一下也没解决了,然后把apr-utils删了重新装 ...
- C# 时间戳转换为时间格式
// 时间戳转为格式 public DateTime StampToDateTime(string timeStamp) { DateTime dateTimeStart = TimeZone.Cur ...
- CF732F Tourist Reform[边双缩点]
题意:给无向图每一条边定向,使得每个点可达点数$R_i$最小值尽可能大,求方案. 条件反射想到二分答案,然后看怎么检验,发现要让所有点$R_i$大于等于某一个值,首先我们关注某些特殊的子图:如果有环的 ...
- springboot中使用spring security,登录url就出现403错误
参考链接:https://segmentfault.com/q/1010000012743613 有两个controller,一个是所有用户可以访问的@RequestMapping("use ...
- 关于pe结构
每一种操作系统它最重要的格式就是它的可执行文件格式, 因为操作系统就是为了支持这些文件而生成的,内核里面有很多机制,也是配合这种文件格式设计的. 换句话说,这种文件格式也是适合操作系统设计的. 比如: ...
- HDU 5486 Difference of Clustering 暴力模拟
Difference of Clustering HDU - 5486 题意:有n个实体,新旧两种聚类算法,每种算法有很多聚类,在同一算法里,一个实体只属于一个聚类,然后有以下三种模式. 第一种分散, ...
- shell脚本中${...}函数的用法总结
${...}在字符串中有非常多的用法: 1.${var} 限定变量. 如果一个变量名A是另一个变量名AB的前部分,那么,如果要在AB相连时,要得到A的值,就必须使用${var}限定. 如果变量名不会产 ...