PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#PS0000140. Đếm đoạn nhị phân có tổng cho trước (Binary Subarrays With Sum)
HỌC THEO LỘ TRÌNH
1 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Đếm đoạn nhị phân có tổng cho trước (Binary Subarrays With Sum)
Binary Subarrays With Sum
Source: LeetCode
Version: Phuoc Hung OJ Extended
Problem Statement
Given a binary array and an integer , count its nonempty contiguous subarrays whose element sum equals . Subarrays with different endpoint pairs are counted separately, even if their contents are identical.
Input
- Line 1: integer , the array length and integer , the target sum.
- Line 2: integers in order.
Output
Print the number of qualifying intervals, or 0 if there are none.
Subtasks
- Subtask 1 (20 points): ; ; ; .
- Subtask 2 (30 points): ; ; ; ; .
- Subtask 3 (50 points): ; ; ; .
Example 1
Input
5 2
1 0 1 0 1
Output
4
Explanation
The four intervals , , , and each contain exactly two ones; no other interval does.
Example 2
Input
5 0
0 0 0 0 0
Output
15
Explanation
All five entries are zero. Lengths 1 through 5 give 5, 4, 3, 2 and 1 intervals, totaling 15.