Advertisement
tensedtorch

Q1

May 18th, 2025 (edited)
410
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.00 KB | None | 0 0
  1. def findMinimumMachinesSize(machineCapacity: list[int]) -> int:
  2.     n = len(machineCapacity)
  3.  
  4.     if n == 0:
  5.         return 0
  6.     if n == 1:
  7.         return 1
  8.  
  9.     deduplicated_list = [machineCapacity[0]]
  10.     for i in range(1, n):
  11.         if machineCapacity[i] != deduplicated_list[-1]:
  12.             deduplicated_list.append(machineCapacity[i])
  13.  
  14.     len_dl = len(deduplicated_list)
  15.  
  16.     if len_dl <= 2:
  17.         return len_dl
  18.  
  19.     final_list = [deduplicated_list[0]]
  20.     for i in range(1, len_dl - 1):
  21.         prev_kept_val = final_list[-1]
  22.         current_val = deduplicated_list[i]
  23.         next_potential_val = deduplicated_list[i+1]
  24.  
  25.         is_collinear = False
  26.         if (prev_kept_val <= current_val <= next_potential_val) or \
  27.            (prev_kept_val >= current_val >= next_potential_val):
  28.             is_collinear = True
  29.        
  30.         if not is_collinear:
  31.             final_list.append(current_val)
  32.  
  33.     final_list.append(deduplicated_list[-1])
  34.    
  35.     return len(final_list)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement