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 ...
随机推荐
- json 数据在textarea中显示的时候,切换 beauty和ugly模式
转化为beauty模式 var jsonText = $('#json').val(); $('#json').val(JSON.stringify(JSON.parse(jsonText), nul ...
- WIN7 WIN10赋予文件或者文件夹完全访问权限
WIN7 WIN10赋予文件或者文件夹完全访问权限win7文件夹图标中多了一把小锁打不开文件夹怎么办?解决办法一:右击目录→取得管理员权限!该方法适用于win7旗舰版.解决办法二:添加everyone ...
- 获得驱动器信息卷设备&&Ring3得到磁盘文件系统(NTFS WIN10)
// GetLogicalDriveStrings.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <Windows ...
- 杂谈3.py
bin() --------十转二 hex()------- 十转十六 oct()-------十转八 import math math.floor(数值)返回小于等于数值的整数 math.trunc ...
- sqlite--一秒20万数据
参考博文:https://blog.csdn.net/weixin_35261786/article/details/78222602 #include <iostream> #inclu ...
- IDLE的GUI交互模式下完美清屏
IDLE的GUI交互模式下完美清屏==============================================================================1.首先把 ...
- leetcode20
public class Solution { Stack<char> S = new Stack<char>(); public bool IsValid(string s) ...
- Sphinx 安装与使用
Sphinx 优点 高速索引(接近10M/S) 高速搜索(2-4G文本搜索耗时不到0.1秒) 高可用性(单CPU支持100GB文本,100M文档) 提供相关性排名.分布式搜索.文档摘要(高亮显示) S ...
- pytho学习笔记---编码
编解码 ASCII:1字节,0-255 GBK2313:常用的汉字,2万多个 GBK:对GBK2313的补充,支持藏文,2个字节表示一个汉字 big5:台湾,繁体字 unicode:万国码,2-4字节 ...
- nexus的安装和简介(3)
从私服下载jar包 没有配置nexus之前,如果本地仓库没有,去中央仓库下载,通常在企业中会在局域网内部署一台私服服务器,有了私服本地项目首先去本地仓库找jar,如果没有找到则连接私服从私服下载ja ...