Detail explanation - easy solution - beat 100% time
Please upvote\n# Intuition\n- We have (i and j)\nnums[i]==nums[j]==x\nIf We have: i Take 1 second to change\nExample: 2,1,(3),4,(3),6,7\ni=2, j=4 ==> Take 1 sec
# Please upvote\n# Intuition\n- We have (i and j)\nnums[i]==nums[j]==x\nIf We have: i<k<j\nThe number of elements we need to change from nums[k] to X\nExample: 2,1,(3),4,5,(3),6\ni=2, j=5 ==> Take 1 second to change\nExample: 2,1,(3),4,(3),6,7\ni=2, j=4 ==> Take 1 second to change\nExample: 2,1,(3),4,6,5,(3),6,7\ni=2, j=6 ==> Take 2 seconds to change\n- Rule:\nTime = (j-i)/2\n- If we have more than 2 values\nExample:\n2,1,(3),4,5,(3),6,(3),7\nindexs= 2, 5, 7\nWe have another case between i==2 and i==7\n2,1,(3),4,5,3,6,(3),7,[2,1,(3)]\nThe list of ranges = (2,5), (5,7), (7,2) \n==> The number of seconds to change all elements from nums[i] to 2 = Max of all ranges.\n\n- Result of this problem = Min(all of sub-result)\n\n# Approach\n- We will use the Map data structure to store the list of indexes of each value\n<Integer, List<Integer>> map:\n+ Key : the value of each element\n+ Values : list of indexes (i) such that nums[i]==key\n- After that, we will loop all of the keys of this map to calculate based on the idea above.\n\n# Complexity\n- N is the number of elements in the array\n- Time complexity: O(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(N) [there are at most n positions]\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n    public static int minimumSeconds(List<Integer> nums) {\n        HashMap<Integer, List<Integer>> valueToIndexs=new HashMap();\n        int n=nums.size();\n\n        for(int i=0;i<n;i++){\n            List<Integer> listIndexs=valueToIndexs.get(nums.get(i));\n            if(listIndexs==null){\n                listIndexs=new ArrayList();\n            }\n            listIndexs.add(i);\n            valueToIndexs.put(nums.get(i), listIndexs);\n        }\n        int rs=Integer.MAX_VALUE;\n\n        for(int key: valueToIndexs.keySet()){\n            List<Integer> listIndexs=valueToIndexs.get(key);\n            int m=listIndexs.size();\n            int currentRs=0;\n            int prevIndex=listIndexs.get(0);\n            \n            for(int j=1;j<m;j++){\n                currentRs=Math.max(currentRs, (listIndexs.get(j)-prevIndex)/2);\n                prevIndex=listIndexs.get(j);\n            }\n            currentRs=Math.max(currentRs, (listIndexs.get(0)+n-listIndexs.get(m-1))/2);\n            rs=Math.min(rs, currentRs);\n        }\n        return rs;\n    }\n}\n```
['Java']