930. Binary Subarrays With Sum Medium

@problem@discussion
#Array#Hash Table#Sliding Window#Prefix Sum



1/**
2 * [930] Binary Subarrays With Sum
3 *
4 * Given a binary array nums and an integer goal, return the number of non-empty subarrays with a sum goal.
5 * 
6 * A subarray is a contiguous part of the array.
7 * 
8 *  
9 * Example 1:
10 * 
11 * 
12 * Input: nums = [1,0,1,0,1], goal = 2
13 * Output: 4
14 * Explanation: The 4 subarrays are bolded and underlined below:
15 * [<u>1,0,1</u>,0,1]
16 * [<u>1,0,1,0</u>,1]
17 * [1,<u>0,1,0,1</u>]
18 * [1,0,<u>1,0,1</u>]
19 * 
20 * 
21 * Example 2:
22 * 
23 * 
24 * Input: nums = [0,0,0,0,0], goal = 0
25 * Output: 15
26 * 
27 * 
28 *  
29 * Constraints:
30 * 
31 * 
32 * 	1 <= nums.length <= 3 * 10^4
33 * 	nums[i] is either 0 or 1.
34 * 	0 <= goal <= nums.length
35 * 
36 */
37pub struct Solution {}
38
39// problem: https://leetcode.com/problems/binary-subarrays-with-sum/
40// discuss: https://leetcode.com/problems/binary-subarrays-with-sum/discuss/?currentPage=1&orderBy=most_votes&query=
41
42// submission codes start here
43
44impl Solution {
45    pub fn num_subarrays_with_sum(nums: Vec<i32>, goal: i32) -> i32 {
46        
47    }
48}
49
50// submission codes end
51
52#[cfg(test)]
53mod tests {
54    use super::*;
55
56    #[test]
57    fn test_930() {
58    }
59}
60


Back
© 2025 bowen.ge All Rights Reserved.