LeetCode 974. Subarray Sums Divisible by K–Python解法–数学题–取模求余


LeetCode题解专栏:LeetCode题解
LeetCode 所有题目总结:LeetCode 所有题目总结
大部分题目C++,Python,Java的解法都有。


题目地址:Subarray Sums Divisible by K - LeetCode


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


这道题目最容易想到穷举的方法,肯定会超时。
Python解法如下:

class Solution:def subarraysDivByK(self, A: List[int], K: int) -> int:length = len(A)count = 0for i in range(0, length):for j in range(i, length):if sum(A[i:j+1]) % K == 0:count += 1return count

进行少量优化后还是超时:

class Solution:def subarraysDivByK(self, A: List[int], K: int) -> int:length = len(A)count = 0for i in range(0, length):now = 0for j in range(i, length):now += A[j]if now % K == 0:count += 1return count

但是用动态规划不能解决。

最后看了解法后发现是数学题,无语了。

详细解法参考:Count all sub-arrays having sum divisible by k - GeeksforGeeks

class Solution:def subarraysDivByK(self, A: List[int], K: int) -> int:length = len(A)# create auxiliary hash# array to count frequency# of remaindersmod = [0]*K# Traverse original array# and compute cumulative# sum take remainder of this# current cumulative# sum and increase count by# 1 for this remainder# in mod[] arraycumSum = 0for i in range(length):cumSum = cumSum + A[i]# as the sum can be negative,# taking modulo twicemod[cumSum % K] = mod[cumSum % K] + 1result = 0  # Initialize result# Traverse mod[]for i in range(K):# If there are more than# one prefix subarrays# with a particular mod value.if mod[i] > 1:result = result + (mod[i]*(mod[i]-1))//2# add the elements which# are divisible by k itself# i.e., the elements whose sum = 0return result + mod[0]

LeetCode 974. Subarray Sums Divisible by K--Python解法--数学题--取模求余相关推荐

  1. leetcode 974. Subarray Sums Divisible by K的解法(统计共同余数)

    题目大意:给定一个整数数组 A,返回其中元素之和可被 K 整除的(连续.非空)子数组的数目 大致思路:比如数组A = [1,2,1], K=2,那么1%2 =1,(1+2)%2=1,所以 {2}是符合 ...

  2. leetcode 974 Subarray Sums Divisible by K

    leetcode 974 Subarray Sums Divisible by K 1.题目描述 2.解题思路 3.Python代码 1.题目描述 给定一个整数数组 A,返回其中元素之和可被 K 整除 ...

  3. leetcode 974. Subarray Sums Divisible by K

    974. Subarray Sums Divisible by K 题意:给你一个数组A和一个数K,求改数组有多少连续子序列的和能被K整除. 思路:简单DP.假设sum[i]表示[0-i]的求和,那么 ...

  4. LeetCode:974. Subarray Sums Divisible by K - Python

    974. 和可被 K 整除的子数组 问题描述: 给定一个整数数组 A,返回其中元素之和可被K整除的(连续.非空)子数组的数目. 示例: 输入:A = [4,5,0,-2,-3,1], K = 5 输出 ...

  5. 【leetcode】974. Subarray Sums Divisible by K

    题目如下: Given an array A of integers, return the number of (contiguous, non-empty) subarrays that have ...

  6. 974. Subarray Sums Divisible by K

    Title 给定一个整数数组 A,返回其中元素之和可被 K 整除的(连续.非空)子数组的数目. 示例: 输入:A = [4,5,0,-2,-3,1], K = 5 输出:7 解释: 有 7 个子数组满 ...

  7. 974. Subarray Sums Divisible by K [Medium]

    用了prefix sum的思想,之前没用过,感觉很难 /*** Runtime: 18 ms, faster than 56.05%* Memory Usage: 42.9 MB, less than ...

  8. 取模是什么意思python_原来Python中的取模运算方法竟然是这样的!

    今天小编就为大家分享一篇Python中的取模运算方法,具有很好的参考价值,希望对大家有所帮助.一起跟随小编过来看看吧 所谓取模运算,就是计算两个数相除之后的余数,符号是%.如a % b就是计算a除以b ...

  9. python怎么取模,Python运算符之取模%

    为了可以成为测试,最近在学Python.菜鸟教程中叫%运算为取余,而之前我在学习Java时记得书上是叫%运算为取模.因此想知道这两者到底有啥区别. 一.定义 在matlab中,关于取余和取模是这么定义 ...

最新文章

  1. 有符号类型的最小负数的补码的由来
  2. Python函数之计算规则图形的面积
  3. 大工计算机基础在线作业答案,大工11春《计算机文化基础》在线作业及答案(国外英文资料).doc...
  4. [Linux] nginx的try_files指令实现隐藏index.php的重写
  5. idea中开启Run Dashboard
  6. 学计算机的用哪种笔记本写字,平面设计笔记本电脑,学平面设计用什么电脑好...
  7. 宝峰对讲机16频率表_宝峰888S对讲机的16个信道频率是多少?
  8. 微型计算机主要性能指标是什么,微型计算机的主要性能指标
  9. VVC系列(三)xCompressCTU、xCompressCU和xCheckModeSplit解析
  10. oa处理会签流程图_深入剖析OA办公系统的流程管理方案
  11. 《University Calculus》-chaper8-无穷序列和无穷级数-比值审敛法
  12. 第六章:项目进度管理 - (6.6 控制进度)
  13. 程序员找不到合适工作的原因总结
  14. 传奇3国际版 单机假设.说明和下载地址
  15. VUE activated,deactivated使用
  16. Ceph 分布式存储
  17. Tikz学习笔记(一)
  18. java过滤微信表情符号_微信隐藏彩蛋!表情加符号就能传递“神秘信息”
  19. 高压放大器驱动压电器件工作原理
  20. 【原】Coursera—Andrew Ng机器学习—课程笔记 Lecture 5 Octave Tutorial

热门文章

  1. 【Scikit-Learn 中文文档】34 预处理数据 - 数据集转换 - 用户指南 | ApacheCN
  2. 什么是冲突域?如何解决冲突?
  3. 图片实测:智能鉴黄,哪家强?
  4. 3D打印压铸模具正在悄悄改写制造业布局
  5. AppleScript 介绍
  6. Android SDK简介
  7. 话说 synchronized
  8. 如何提高自己的专注度
  9. 春节后面试别人的经历总结之二,好岗位分享给还在找工作中的软件开发爱好者们...
  10. 嵌入式系统原理及应用课后习题答案