from collections import deque 100% test Case passed Updated def stabilize_array(A, N): # Step 1: Initialize the queue for indices needing update queue = deque() for i in range(N - 1): if A[i] < A[i + 1]: queue.append(i) seconds = 0 # Step 2: Simulate the process of stabilization while queue: seconds += 1 # Process all the changes in the current second for _ in range(len(queue)): i = queue.popleft() if A[i] < A[i + 1]: A[i] = A[i + 1] if i > 0 and A[i - 1] < A[i]: # If the previous element needs to be updated queue.append(i - 1) return seconds # Input handling T = int(input()) # Number of test cases for _ in range(T): N = int(input()) # Length of the array A = list(map(int, input().split())) # The array A print(stabilize_array(A, N))
from collections import deque
100% test Case passed Updated
def stabilize_array(A, N):
# Step 1: Initialize the queue for indices needing update
queue = deque()
for i in range(N - 1):
if A[i] < A[i + 1]:
queue.append(i)
seconds = 0
# Step 2: Simulate the process of stabilization
while queue:
seconds += 1
# Process all the changes in the current second
for _ in range(len(queue)):
i = queue.popleft()
if A[i] < A[i + 1]:
A[i] = A[i + 1]
if i > 0 and A[i - 1] < A[i]: # If the previous element needs to be updated
queue.append(i - 1)
return seconds
# Input handling
T = int(input()) # Number of test cases
for _ in range(T):
N = int(input()) # Length of the array
A = list(map(int, input().split())) # The array A
print(stabilize_array(A, N))
tq
🤝