class Solution { MapstartIndex; Map lengths; Random random; public Solution(int[] nums) { random = new Random(); startIndex = new HashMap<>(); lengths = new HashMap<>(); int length = 1; int start = nums[0]; startIndex.put(nums[0], 0); for (int i = 1; i < nums.length; i++) { if (start != nums[i]) { lengths.put(start, length); length = 1; start = nums[i]; startIndex.put(nums[i], i); } else{ length++; } } lengths.put(start, length); } public int pick(int target) { if (!startIndex.containsKey(target)) { return 0; } return startIndex.get(target) + random.nextInt(lengths.get(target)); }}/** * Your Solution object will be instantiated and called as such: * Solution obj = new Solution(nums); * int param_1 = obj.pick(target); */