-1

In a circular list of length n, where a1 is adjacent to a2, a2 is adjacent to a3, and an is adjacent to a1. Each operation can delete one number, after each deletion, if there exists adjacent equal numbers, one of them will be automaticlly deleted until there are no adjacent equal numbers in the list.

For example, consider a circular list [1, 2, 3, 2]. If we delete the number 3, one of the adjacent 2's will be automatically deleted, resulting in [1, 2]. If we then delete the number 2, the list becomes [1]. Finally 1 is deleted, and the operation number is 3, because automatically deletions are not included.

I would like to know the maximum and minimum number of operations required to delete all the elements in the circular list. What are the approaches or algorithms that can be used to solve this problem efficiently? Java code is better.

Yu Xing
  • 19
  • 3

1 Answers1

0

My approach is to use a map to record the occurrence count of each number. If there is a number that appears only once or there are at least three different numbers in total, I will return the length of the array. Otherwise, if the scenario is similar to "1 2 1 2 1 2 1 2", I will return the length of the array divided by 2 plus 1 (each deletion triggers an automatic removal, reducing the length by 2; when the length reaches 2, two deletions can be performed). But I wonder if it is right and how to prove it.

Yu Xing
  • 19
  • 3