Minimum Distance in One Pass O(n)
Approach\nTo solve the problem, we track the distances between occurrences of each unique element to determine the maximum spread in the array. The rationale is
# Approach\nTo solve the problem, we track the distances between occurrences of each unique element to determine the maximum spread in the array. The rationale is that the spread of an element indicates how quickly it can propagate to make all array elements equal.\n\n# Data Structures Used\n- **Dictionary `distanceMap`:** Maps each unique number in `nums` to a `SetDistance` object which keeps track of various distances concerning the occurrences of the number.\n- **Class `SetDistance`:** Maintains properties such as `Value`, `MaxDistance`, `LeftDistance`, `RightDistance`, and `MaxMidDistance`. These distances help in calculating how quickly a number can "spread" throughout the array.\n\n# Steps\n**1. Initialization:** Create a dictionary `distanceMap` to track distances for each unique number. Also, keep a `minDistance` object to store the number which can spread the fastest.\n\n**2. Iteration Over Array:**\n- For each element in `nums`, check if it is already in `distanceMap`.\n    - If not, initialize a new `SetDistance` object with its index and set the maximum possible distance (i.e., array length minus one).\n    - If the element is already present, update the distance measurements by calling the `Push` method of the `SetDistance` class.\n- Update the `minDistance` if the current element can propagate faster (i.e., has a smaller maximum distance) compared to the previously recorded fastest.\n\n**3. Calculate Result:** The result is derived from `minDistance.MaxDistance`. We determine how many seconds it would take for the element to propagate its value across its maximum distance.\n\n# Efficiency\nThis approach efficiently computes the minimum seconds needed by focusing on the propagation of each number rather than trying to simulate the process. It leverages mathematical properties of distances between indices to ascertain the quickest spread.\n\n# Edge Cases\nIf all elements are initially the same, no operation is needed, and the function immediately returns 0.\n\n# Complexity\n- Time complexity: $$O(n)$$\nDue to the iteration over the entire array `nums` which contains `n` elements.\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n)$$\nDue to the `distanceMap` dictionary that stores a unique entry for each distinct element found in `nums`.\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\npublic class Solution {\n    public int MinimumSeconds(IList<int> nums)\n    {\n        Dictionary<int, SetDistance> distanceMap = [];\n        SetDistance minDistance = new(0, 0, 0);\n        int count = nums.Count;\n\n        for (int i = 0; i < nums.Count; i++)\n        {\n            int maxDistance;\n            if (!distanceMap.ContainsKey(nums[i]))\n            {\n                distanceMap[nums[i]] = new SetDistance(nums[i], i, count);\n                maxDistance = count - 1;\n            }\n            else\n                maxDistance = distanceMap[nums[i]].Push(i);\n\n            if (minDistance.Value == 0 || (minDistance.Value != nums[i] && maxDistance < minDistance.MaxDistance))\n                minDistance = distanceMap[nums[i]];\n        }\n\n        return ((int)(minDistance.MaxDistance - minDistance.MaxDistance % 2) / 2) + minDistance.MaxDistance % 2;\n    }\n\n    public class SetDistance(int val, int currentIndex, int arrayCount)\n    {\n        public int Value { get; } = val; //key\n        private int PreviousIndex { get; set; } = currentIndex;\n        private int LeftDistance { get; set; } = currentIndex; //distance from index 0 to the nearest element\n        private int MaxMidDistance { get; set; } = - 1; //indicate non-distance value \n        private int RightDistance { get; set; } = arrayCount - 1 - currentIndex; //distance from index n-1 to the nearest element\n        public int MaxDistance { get; internal set; } = arrayCount - 1;\n\n        public int Push(int currentIndex)\n        {\n            int distance = currentIndex - PreviousIndex - 1; //-1 to exclude currentIndex\n            RightDistance -= distance + 1; //+1 to regain minus one from distance\n            PreviousIndex = currentIndex;\n            MaxMidDistance = Math.Max(MaxMidDistance, distance);\n            MaxDistance = Math.Max(MaxMidDistance, LeftDistance + RightDistance);\n            return MaxDistance;\n        }\n    }\n}\n```
['C#']