DatabricksOA #24
Subarray Counting
Estimated MediumJudge-ready
Topics (AI-labelled)In-place ArraysStrings
You are given numbers and a pattern containing only -1, 0, and 1. For a contiguous segment of elements, compare each pair of adjacent values: record 1 if the next value is larger, 0 if it is equal, and -1 if it is smaller. Count the contiguous segments whose change sequence exactly equals pattern.
Standard input/output and this site's supplementary ranges follow this problem's description.
Input
The first line contains n m (2≤n≤200000, 1≤m≤200000), the second line contains n integers (−10⁹..10⁹), and the third line contains m values, each −1, 0, or 1. The upper bound on m is a site supplement. A matching segment contains m+1 elements of numbers.
Output
Output the number of matching contiguous subarrays, as one integer.
Example 1
Input:
8 3
4 1 3 4 4 5 5 1
1 0 -1
Output:
1
Example 2
Input:
4 3
1 2 2 1
1 0 -1
Output:
1
Example 3
Input:
2 1
3 3
0
Output:
1
Editorial
Loading solution…