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. 1 <= A.length <= 30000
  2. -10000 <= A[i] <= 10000
  3. 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(),0);
        vector<int> Bsum(A.size()+1, 0);
        for(int i=0; i<A.size(); i++){
            Bsum[i+1] = Bsum[i] + A[i];
        }
        int ret = 0;
        for(int i=1; i<Bsum.size(); i++){
            if(A[i-1] % K == 0){
                if(i-1 == 0) dp[i-1] = 1;
                else dp[i-1] = dp[i-2] + 1;
                continue;
            }
            int newcnt = 0;
            for(int j=i-1; j>=0; j--){
                //if(Bsum[i] - Bsum[j] == 0) continue;
                if( (Bsum[i] - Bsum[j]) % K == 0) {
                    //cout << i << " " << j << endl;
                  newcnt += dp[j-1] + 1;
                  break;
                }
            }
            dp[i-1] = newcnt;
        }
        for(auto x : dp) ret += x;
        return ret;
    }
};

原文地址:https://www.cnblogs.com/ethanhong/p/10262354.html

时间: 2024-11-11 11:20:42

LC 974. Subarray Sums Divisible by K的相关文章

【leetcode】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,

[Swift Weekly Contest 118]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 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

[Swift]LeetCode713. 乘积小于K的子数组 | Subarray Product Less Than K

Your are given an array of positive integers nums. Count and print the number of (contiguous) subarrays where the product of all the elements in the subarray is less than k. Example 1: Input: nums = [10, 5, 2, 6], k = 100 Output: 8 Explanation: The 8

LeetCode 713. Subarray Product Less Than K

Problem Description: Your are given an array of positive integers nums. Count and print the number of (contiguous) subarrays where the product of all the elements in the subarray is less than k. 题解: 很快想到了two pointer的解法,但是一直被corner case困住. 两个典型的case [

Leetcode 713 Subarray Product Less Than K (子数组乘积大于K的个数) (双指针)

目录 问题描述 例子 方法 Leetcode 713 问题描述 Your are given an array of positive integers nums. Count and print the number of (contiguous) subarrays where the product of all the elements in the subarray is less than k. 例子 Example 1: Input: nums = [10, 5, 2, 6], k

Leetcode 1015. Smallest Integer Divisible by K

思路显然是暴力枚举. 但是两个问题: 1.当1的位数非常大时,模运算很费时间,会超时. 其实每次不用完全用'11111...'来%K,上一次的余数*10+1后再%K就行. 证明: 令f(n)=111111...(n个1); g(n)=f(n)%K 因为f(n)=f(n-1)*10+1 所以f(n)%K=(f(n-1)*10+1)%K 即g(n)=g(n-1)*10+1 2.枚举何时停止? 一种方法是可以设置一个大数,比如10的6次方,可以Accepted. 更精确的方法是:从1个1到K个1,如果

[LC] 1099. Two Sum Less Than K

Given an array A of integers and integer K, return the maximum S such that there exists i < j with A[i] + A[j] = S and S < K. If no i, j exist satisfying this equation, return -1. Example 1: Input: A = [34,23,1,24,75,33,54,8], K = 60 Output: 58 Expl

Weekly Contest 119

第一题: 973. K Closest Points to Origin We have a list of points on the plane.  Find the K closest points to the origin (0, 0). (Here, the distance between two points on a plane is the Euclidean distance.) You may return the answer in any order.  The an

【暑假】[实用数据结构]UVa11997 K Smallest Sums

UVa11997 K Smallest Sums  题目: K Smallest Sums You're given k arrays, each array has k integers. There are kk ways to pick exactly one element in each array and calculate the sum of the integers. Your task is to find the k smallest sums among them. In