@StefanPochmann

Mine first submit is the same as yours, but it exceeds the memory limited, then I try to use TreeMap, (I use java here), but it also exceeds the memory limited. Actually, I'm not sure which is more optimized in memory?

My java:

```
public class Solution {
HashMap<Integer, TreeMap<Integer, Integer>> pos;
public Solution(int[] nums) {
pos = new HashMap<>();
for(int i = 0; i < nums.length; i++) {
if(!pos.containsKey(nums[i])) {
TreeMap<Integer, Integer> tree = new TreeMap<Integer, Integer>();
tree.put(i, 1);
pos.put(nums[i], tree);
} else {
TreeMap<Integer, Integer> tree = pos.get(nums[i]);
Integer floor = tree.floorKey(i);
if(floor != null && i - tree.get(floor) == floor) {
int range = 1 + tree.get(floor);
tree.put(floor, range);
} else {
tree.put(i, 1);
}
}
}
}
public int pick(int target) {
TreeMap<Integer, Integer> tree = pos.get(target);
int sum = 0;
for(Integer i : tree.keySet()) {
sum += tree.get(i);
}
Random r = new Random();
int indx = r.nextInt(sum);
int ans = -1, cur = 0, prev = 0;
for(Integer i : tree.keySet()) {
cur += tree.get(i);
if(cur > indx) {
ans = i + indx - prev;
break;
}
prev = cur;
}
return ans;
}
}
```

for `HashMap<Integer, TreeMap<Integer, Integer>> pos;`

, it means

`num -> start_position -> number of the same successive digit`

compared to `HashMap<Integer, List<Integer>>`

which is more optimized?