Advertisement
tensedtorch

Q2

May 18th, 2025 (edited)
502
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.58 KB | None | 0 0
  1. import bisect
  2.  
  3. def findMinimumTime(password: str, attackOrder: list[int], m: int) -> int:
  4.     N = len(password)
  5.  
  6.     if N == 0:
  7.         return 0
  8.  
  9.     if m == 0:
  10.         return 1
  11.  
  12.     def count_substrings_from_length(k: int) -> int:
  13.         if k <= 0:
  14.             return 0
  15.         return k * (k + 1) // 2
  16.  
  17.     malicious_sites_sorted = [-1, N]
  18.  
  19.     current_total_clean_substrings = count_substrings_from_length(N)
  20.     total_possible_substrings = current_total_clean_substrings
  21.  
  22.     for t, attack_pos_1_indexed in enumerate(attackOrder):
  23.         time_step = t + 1
  24.         attack_idx_0_based = attack_pos_1_indexed - 1
  25.  
  26.         insertion_idx = bisect.bisect_left(malicious_sites_sorted, attack_idx_0_based)
  27.        
  28.         prev_mal_idx = malicious_sites_sorted[insertion_idx - 1]
  29.         next_mal_idx = malicious_sites_sorted[insertion_idx]
  30.  
  31.         len_old_segment = next_mal_idx - prev_mal_idx - 1
  32.         current_total_clean_substrings -= count_substrings_from_length(len_old_segment)
  33.  
  34.         len_left_segment = attack_idx_0_based - prev_mal_idx - 1
  35.         current_total_clean_substrings += count_substrings_from_length(len_left_segment)
  36.  
  37.         len_right_segment = next_mal_idx - attack_idx_0_based - 1
  38.         current_total_clean_substrings += count_substrings_from_length(len_right_segment)
  39.        
  40.         malicious_sites_sorted.insert(insertion_idx, attack_idx_0_based)
  41.  
  42.         num_malicious_substrings = total_possible_substrings - current_total_clean_substrings
  43.         if num_malicious_substrings >= m:
  44.             return time_step
  45.            
  46.     return -1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement