Rust/Python linear solution with detailed explanation
Intuition\nLets look at two of the same values and some random values between them X . . . . . X where in between we have n different values (none of them are X
# Intuition\nLets look at two of the same values and some random values between them `X . . . . . X` where in between we have `n` different values (none of them are X). How many steps do you need to convert them all to X? It will be `(p2 - p1) / 2` where `p1` and `p2` are positions of `X`.\n\nNow what if we have multiple values of `X`. For example: `. . X . . . X . X . .`. To do this we can find the values between each two X (not forgetting the gap between last X and first) and take the maximum\n\nThis gives us the correct solution. Create the dictionary which maps the value to positions where it is seen. Then use the above algorithm to get the number of seconds to convert all array to this value. Then find the minimum.\n\n\n# Complexity\n- Time complexity: $O(n)$\n- Space complexity: $O(n)$\n\n# Code\n```Rust []\nuse std::collections::HashMap;\n\nimpl Solution {\n  pub fn minimum_seconds(nums: Vec<i32>) -> i32 {\n    let mut data: HashMap<i32, Vec<usize>> = HashMap::new();\n    for i in 0 .. nums.len() {\n      data.entry(nums[i]).or_insert(vec![]).push(i);\n    }\n\n    let mut res = nums.len();\n    for (_, v) in data {\n      let mut curr_max = (v[0] + nums.len() - v[v.len() - 1]) / 2;\n      for i in 1 .. v.len() {\n        curr_max = curr_max.max((v[i] - v[i - 1]) / 2);\n      }\n      res = res.min(curr_max);\n    }\n\n    return res as i32;\n  }\n}\n```\n```python []\nclass Solution:\n  def minimumSeconds(self, nums: List[int]) -> int:\n    data = defaultdict(list)\n    for i, v in enumerate(nums):\n      data[v].append(i)\n\n    res = len(nums) + 1\n    for k, v in data.items():\n      curr_max = (v[0] + len(nums) - v[-1]) // 2;\n      for i in range(1, len(v)):\n        curr_max = max(curr_max, (v[i] - v[i - 1]) // 2)\n      res = min(res, curr_max)\n      \n    return res\n```\n
['Python', 'Rust']