Advertisement
asdfg0998

sdfds

Sep 15th, 2024
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.91 KB | None | 0 0
  1. int countMatchingSubarrays(vector<int>& nums, vector<int>& pattern) {
  2.     int m = pattern.size() + 1;  // Length of subarray we need to check
  3.     int n = nums.size();         // Size of nums array
  4.     int ans = 0;                 // Variable to store the count of matching subarrays
  5.  
  6.     for (int i = 0; i <= n - m; i++) {
  7.         vector<int> aux;
  8.  
  9.         for (int j = i; j < i + m; j++) {
  10.             aux.push_back(nums[j]);
  11.         }
  12.  
  13.         int j;
  14.         for (j = 0; j < m - 1; j++) {
  15.             if (pattern[j] == 1 && aux[j + 1] <= aux[j]) {
  16.                 break;
  17.             } else if (pattern[j] == 0 && aux[j + 1] != aux[j]) {
  18.                 break;
  19.             } else if (pattern[j] == -1 && aux[j + 1] >= aux[j]) {
  20.                 break;
  21.             }
  22.         }
  23.  
  24.         if (j == m - 1) {
  25.             ans++;
  26.         }
  27.     }
  28.  
  29.     return ans;  // Return the count of matching subarrays
  30. }
  31.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement