Advertisement
Guest User

find_max_subArray.py

a guest
Apr 8th, 2020
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.74 KB | None | 0 0
  1. """
  2. Given an array: ['A', 'B', 'C', 'D', 'F', 'D']
  3. find maximum subarray which contain one unique char. You are allowd to change at most K chars to any other char
  4. """
  5. import collections
  6.  
  7. def findMax(Array, K):
  8.     if not Array:
  9.         return 0
  10.  
  11.     res = 0
  12.     majorifytCount = 0
  13.     left = 0
  14.     count = collections.Counter()
  15.  
  16.     for right in range(len(Array)):
  17.         count[Array[right]] += 1
  18.  
  19.         if count[Array[right]] > majorifytCount:
  20.             majorifytCount = count[Array[right]]
  21.  
  22.         if right - left + 1 - majorifytCount <= K:
  23.             res = max(res, right - left + 1)
  24.         else:
  25.             count[Array[left]] -= 1
  26.             left += 1
  27.  
  28.     return res
  29.  
  30. if __name__ == "__main__":
  31.     print()
  32.     A = ['C', 'D', 'C', 'D', 'F', 'D', 'D', 'E', 'D', 'E', 'D']
  33.     K = 3
  34.     print(findMax(A, K))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement