Try to Explain with pen ,paper...
Similar Question - https://leetcode.com/problems/random-pick-with-weight/description/\n# Approach\n Describe your approach to solving the problem. \n###  Look c
Similar Question - https://leetcode.com/problems/random-pick-with-weight/description/\n# Approach\n<!-- Describe your approach to solving the problem. -->\n###  Look code after each step of Explanation \n\n![1.jpg](https://assets.leetcode.com/users/images/a58fd37d-ec90-4385-a08c-26ea4b7452a8_1704805343.4923408.jpeg)\n![2.jpg](https://assets.leetcode.com/users/images/00f35b02-c43e-4139-8ebb-ca27656d002b_1704805399.2797208.jpeg)\n![3.jpg](https://assets.leetcode.com/users/images/6f623d10-b88b-488f-a8ea-35e4a660660c_1704805411.2538877.jpeg)\n![4.jpg](https://assets.leetcode.com/users/images/fb1454bf-b27a-4549-a59f-6e7b54ac9968_1704805422.9484098.jpeg)\n\n\n# Code\n```\nclass Solution {\n    Random rand;\n    TreeMap<Integer, Integer> map;\n    int area;\n    int rect[][];\n\n    public Solution(int[][] rects) {\n        this.rect = rects;\n        rand = new Random();\n        area =0;\n        map = new TreeMap<>();\n\n        for(int i =0;i<rect.length;i++){\n            area += (rect[i][2] - rect[i][0]+1) * (rect[i][3] - rect[i][1]+1);\n            map.put(area,i);\n        }\n    }\n\n    public int[] pick() {\n        int randVal = rand.nextInt(area);\n        int key = map.higherKey(randVal);\n        int rects[] = rect[map.get(key)];\n\n        int offset = key - randVal - 1;\n        int width = rects[2] - rects[0] + 1; \n\n        //  The modulo operation ensures that the result is within the range [0, width-1].\n        int x = offset % (width) + rects[0];\n       \n        // The divide operation ensures that the result is within the range [0, width-1].\n        int y = offset / (width) + rects[1];\n\n        return new int[] { x, y};\n    }\n}\n```
['Ordered Map', 'Java']