question_slug stringlengths 3 77 | title stringlengths 1 183 | slug stringlengths 12 45 | summary stringlengths 1 160 ⌀ | author stringlengths 2 30 | certification stringclasses 2
values | created_at stringdate 2013-10-25 17:32:12 2025-04-12 09:38:24 | updated_at stringdate 2013-10-25 17:32:12 2025-04-12 09:38:24 | hit_count int64 0 10.6M | has_video bool 2
classes | content stringlengths 4 576k | upvotes int64 0 11.5k | downvotes int64 0 358 | tags stringlengths 2 193 | comments int64 0 2.56k |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
find-the-k-sum-of-an-array | C++ solution | c-solution-by-hujinxin0607-i3hz | \nclass Solution {\npublic:\n long long kSum(vector<int>& nums, int k) {\n long sum = 0;\n vector<int> q;\n \n for (auto x: nums) | hujinxin0607 | NORMAL | 2022-08-21T05:04:12.882543+00:00 | 2022-08-21T05:04:33.652348+00:00 | 110 | false | ```\nclass Solution {\npublic:\n long long kSum(vector<int>& nums, int k) {\n long sum = 0;\n vector<int> q;\n \n for (auto x: nums) {\n if (x >= 0){\n sum += x;\n q.push_back(-x);\n } else q.push_back(x);\n }\n \n s... | 0 | 1 | [] | 0 |
find-the-k-sum-of-an-array | Python solution | python-solution-by-andrewpeng-185v | ```\nimport heapq\n\n\nclass Solution:\n def kSum(self, nums: List[int], k: int) -> int:\n heap = []\n \n # get maximal subsequence sum\ | andrewpeng | NORMAL | 2022-08-21T04:51:23.357962+00:00 | 2022-08-21T04:51:36.444787+00:00 | 222 | false | ```\nimport heapq\n\n\nclass Solution:\n def kSum(self, nums: List[int], k: int) -> int:\n heap = []\n \n # get maximal subsequence sum\n max_subset = sum(n for n in nums if n > 0)\n removal_candidates = list(sorted(nums, key=lambda x: abs(x)))\n \n # keep max_heap of... | 0 | 0 | [] | 0 |
find-the-k-sum-of-an-array | O(n * klogk) solution + clever tricks | on-klogk-solution-clever-tricks-by-chuan-glao | Starting point: given a multiset of subsequence sums sl, adding an element x in nums results in a new multiset of subsequence sums sl.extend([n + x for n in sl] | chuan-chih | NORMAL | 2022-08-21T04:26:13.469954+00:00 | 2022-08-21T04:36:04.200966+00:00 | 335 | false | Starting point: given a multiset of subsequence sums `sl`, adding an element `x` in `nums` results in a new multiset of subsequence sums `sl.extend([n + x for n in sl])`. We want to keep the top `k` of them. However, straight implementation of this is too slow.\n\nIntuition: if `x` is negative, we probably don\'t have ... | 0 | 0 | ['Python'] | 0 |
find-the-k-sum-of-an-array | [Java] Priority Queue | java-priority-queue-by-xuanzhang98-6pfa | \n\nclass Solution {\n public long kSum(int[] nums, int k) {\n long sum = 0;\n for(int i=0;i<nums.length;i++){\n if(nums[i] > 0) sum | Xuanzhang98 | NORMAL | 2022-08-21T04:26:00.489438+00:00 | 2022-08-21T04:26:00.489468+00:00 | 267 | false | \n```\nclass Solution {\n public long kSum(int[] nums, int k) {\n long sum = 0;\n for(int i=0;i<nums.length;i++){\n if(nums[i] > 0) sum += nums[i];\n else nums[i] = -nums[i];\n }\n PriorityQueue<long[]> pq = new PriorityQueue(new Comparator<long[]>(){\n @O... | 0 | 0 | ['Heap (Priority Queue)', 'Java'] | 0 |
find-the-k-sum-of-an-array | Java Solution with explanation O(N log(N) + k log(k)) | java-solution-with-explanation-on-logn-k-srh4 | from: https://stackoverflow.com/questions/72114300/how-to-generate-k-largest-subset-sums-for-a-given-array-contains-positive-and-n/72117947#72117947\n\nFirst, i | binglelove | NORMAL | 2022-08-21T04:19:52.838736+00:00 | 2022-08-21T04:26:06.624956+00:00 | 307 | false | **from:** https://stackoverflow.com/questions/72114300/how-to-generate-k-largest-subset-sums-for-a-given-array-contains-positive-and-n/72117947#72117947\n\nFirst, in a single pass, we find the sum of the positive numbers. This is the maximum sum. We initialize our answer array with [maximum_sum].\n\nNext, we create an ... | 0 | 0 | [] | 0 |
find-the-k-sum-of-an-array | [Java] greedy : sort + max heap | java-greedy-sort-max-heap-by-caoxz0815-5o14 | ```\n public long kSum(int[] nums, int k) {\n int len = nums.length;\n long maxSum = 0;\n for (int temp : nums) {\n if (temp > 0 | caoxz0815 | NORMAL | 2022-08-21T04:09:56.136075+00:00 | 2022-08-21T04:09:56.136112+00:00 | 357 | false | ```\n public long kSum(int[] nums, int k) {\n int len = nums.length;\n long maxSum = 0;\n for (int temp : nums) {\n if (temp > 0) maxSum += temp;\n }\n if (k==1) return maxSum;\n\n int[] absArray = IntStream.of(nums).map(Math::abs).sorted().toArray();\n\n Prio... | 0 | 0 | ['Java'] | 0 |
random-point-in-non-overlapping-rectangles | Trying to explain why the intuitive solution wont work | trying-to-explain-why-the-intuitive-solu-i3vv | Problem\nThe intuitive solution is randomly pick one rectangle from the rects and then create a random point within it. But this approach wont work. It took me | murushierago | NORMAL | 2019-06-22T03:06:54.272642+00:00 | 2021-05-12T03:21:51.208418+00:00 | 7,705 | false | ### Problem\nThe intuitive solution is randomly pick one rectangle from the `rects` and then create a random point within it. But this approach wont work. It took me a while to understand, I am trying to explain it:\n\n\n\nAs shown in the picture abov... | 192 | 1 | [] | 13 |
random-point-in-non-overlapping-rectangles | [Python] Short solution with binary search, explained | python-short-solution-with-binary-search-hjnb | Basically, this problem is extention of problem 528. Random Pick with Weight, let me explain why. Here we have several rectangles and we need to choose point fr | dbabichev | NORMAL | 2020-08-22T08:31:13.442681+00:00 | 2020-08-22T08:31:13.442711+00:00 | 5,382 | false | Basically, this problem is extention of problem **528. Random Pick with Weight**, let me explain why. Here we have several rectangles and we need to choose point from these rectangles. We can do in in two steps:\n\n1. Choose rectangle. Note, that the bigger number of points in these rectangle the more should be our cha... | 100 | 5 | ['Binary Tree'] | 12 |
random-point-in-non-overlapping-rectangles | Java Solution. Randomly pick a rectangle then pick a point inside. | java-solution-randomly-pick-a-rectangle-1s3l2 | \nclass Solution {\n TreeMap<Integer, Integer> map;\n int[][] arrays;\n int sum;\n Random rnd= new Random();\n \n public Solution(int[][] rect | uynait | NORMAL | 2018-07-27T10:16:53.570404+00:00 | 2018-10-25T06:31:10.024100+00:00 | 12,704 | false | ```\nclass Solution {\n TreeMap<Integer, Integer> map;\n int[][] arrays;\n int sum;\n Random rnd= new Random();\n \n public Solution(int[][] rects) {\n arrays = rects;\n map = new TreeMap<>();\n sum = 0;\n \n for(int i = 0; i < rects.length; i++) {\n int[]... | 69 | 2 | [] | 14 |
random-point-in-non-overlapping-rectangles | Java TreeMap solution only one random per pick | java-treemap-solution-only-one-random-pe-jzhj | \nclass Solution {\n private int[][] rects;\n private Random r;\n private TreeMap<Integer, Integer> map;\n private int area;\n\n public Solution( | wangzi6147 | NORMAL | 2018-07-31T19:20:10.623383+00:00 | 2018-09-09T03:34:23.268190+00:00 | 4,533 | false | ```\nclass Solution {\n private int[][] rects;\n private Random r;\n private TreeMap<Integer, Integer> map;\n private int area;\n\n public Solution(int[][] rects) {\n this.rects = rects;\n r = new Random();\n map = new TreeMap<>();\n area = 0;\n for (int i = 0; i < rect... | 50 | 2 | [] | 8 |
random-point-in-non-overlapping-rectangles | C++ concise solution using binary search (pick with a weight) | c-concise-solution-using-binary-search-p-umf1 | First pick a random rectangle with a weight of their areas.\nThen pick a random point inside the rectangle.\n\n\nclass Solution {\npublic:\n vector<int> v;\n | zhoubowei | NORMAL | 2018-07-29T15:30:40.220539+00:00 | 2018-09-09T03:36:48.337001+00:00 | 2,767 | false | First pick a random rectangle with a weight of their areas.\nThen pick a random point inside the rectangle.\n\n```\nclass Solution {\npublic:\n vector<int> v;\n vector<vector<int>> rects;\n \n int area(vector<int>& r) {\n return (r[2] - r[0] + 1) * (r[3] - r[1] + 1);\n }\n \n Solution(vector... | 37 | 2 | [] | 6 |
random-point-in-non-overlapping-rectangles | Python weighted probability solution | python-weighted-probability-solution-by-smx1b | \nclass Solution:\n\n def __init__(self, rects):\n self.rects, self.ranges, sm = rects, [], 0\n for x1, y1, x2, y2 in rects:\n sm += | cenkay | NORMAL | 2018-07-27T11:32:06.048473+00:00 | 2018-10-25T06:31:05.731286+00:00 | 5,374 | false | ```\nclass Solution:\n\n def __init__(self, rects):\n self.rects, self.ranges, sm = rects, [], 0\n for x1, y1, x2, y2 in rects:\n sm += (x2 - x1 + 1) * (y2 - y1 + 1)\n self.ranges.append(sm)\n\n def pick(self):\n x1, y1, x2, y2 = self.rects[bisect.bisect_left(self.ranges... | 36 | 3 | [] | 7 |
random-point-in-non-overlapping-rectangles | C++ solution using reservoir sampling with explanation - concise and easy to understand | c-solution-using-reservoir-sampling-with-f285 | I found no other guys solved this problem using reservoir sampling method. Take a look at my solution.\n\n\nclass Solution {\npublic:\n vector<vector<int>> r | heesub | NORMAL | 2018-09-15T14:11:20.093182+00:00 | 2018-09-15T14:11:20.093227+00:00 | 3,407 | false | I found no other guys solved this problem using reservoir sampling method. Take a look at my solution.\n\n```\nclass Solution {\npublic:\n vector<vector<int>> rects;\n \n Solution(vector<vector<int>> rects) : rects(rects) {\n }\n \n vector<int> pick() {\n int sum_area = 0;\n vector<int> ... | 28 | 1 | [] | 3 |
random-point-in-non-overlapping-rectangles | java Accepted. Clean and Concise. !! Commented ! | java-accepted-clean-and-concise-commente-7o67 | Please upvote if helpful\n\n\nclass Solution {\n \n Random random;\n TreeMap<Integer,int[]> map;\n int areaSum = 0;\n \n public Solution(int[] | kunal3322 | NORMAL | 2020-08-22T13:37:51.346533+00:00 | 2020-08-22T14:30:18.660345+00:00 | 2,131 | false | * **Please upvote if helpful**\n\n```\nclass Solution {\n \n Random random;\n TreeMap<Integer,int[]> map;\n int areaSum = 0;\n \n public Solution(int[][] rects) {\n this.random = new Random();\n this.map = new TreeMap<>();\n \n for(int i = 0; i < rects.length; i++){\n ... | 25 | 2 | ['Java'] | 1 |
random-point-in-non-overlapping-rectangles | [C++] Simple Solution | c-simple-solution-by-sahilgoyals-47du | \nclass Solution {\npublic:\n vector<int> v;\n vector<vector<int>> rects;\n // I add the +1 here because in some inputs they contain lines also like \n | sahilgoyals | NORMAL | 2020-08-22T08:04:07.891938+00:00 | 2020-08-22T18:48:25.203047+00:00 | 3,361 | false | ```\nclass Solution {\npublic:\n vector<int> v;\n vector<vector<int>> rects;\n // I add the +1 here because in some inputs they contain lines also like \n\t// [ 1,2,1,3 ] ( rectangle with height 0 or width 0 but this also contains 2 points )\n\t// to also add these points I add +1.\n int area(vector<int>& r... | 25 | 1 | ['C', 'C++'] | 5 |
random-point-in-non-overlapping-rectangles | Python [Probability/Monte Carlo] | python-probabilitymonte-carlo-by-gsan-kcb8 | This is a straightforward idea from probability theory. Say you have two rectangles, the first one contains 7 points inside and the second one contains 3. A ran | gsan | NORMAL | 2020-08-22T07:41:15.250544+00:00 | 2020-08-22T09:51:03.537977+00:00 | 1,372 | false | This is a straightforward idea from probability theory. Say you have two rectangles, the first one contains 7 points inside and the second one contains 3. A randomly drawn point has a probability of coming from the first rectangle equal to 0.7. So you calculate the number of points in each rectangle, and then use the i... | 13 | 0 | [] | 2 |
random-point-in-non-overlapping-rectangles | [C++] Easy solution with explanation | c-easy-solution-with-explanation-by-_ris-rlro | If you are unable to solve this question, I would highly suggest you try this one first:\nhttps://leetcode.com/problems/random-pick-with-weight/\n\nIf you had s | _rishabharora | NORMAL | 2020-08-22T12:11:18.330334+00:00 | 2020-08-22T12:11:18.330371+00:00 | 707 | false | If you are unable to solve this question, I would highly suggest you try this one first:\nhttps://leetcode.com/problems/random-pick-with-weight/\n\nIf you had solved the above question, you would understand that here the probablity of picking up a random rectangle is proportional to the number if points enclosed in the... | 9 | 0 | [] | 2 |
random-point-in-non-overlapping-rectangles | C++, easy and slow, beats 100% | c-easy-and-slow-beats-100-by-chrisys-ey9i | ```\nclass Solution {\npublic:\n vector> rect;\n vector r_area;\n int total_area;\n Solution(vector> rects) {\n rect = rects;\n int to | chrisys | NORMAL | 2019-01-29T16:01:45.291709+00:00 | 2019-01-29T16:01:45.291777+00:00 | 1,483 | false | ```\nclass Solution {\npublic:\n vector<vector<int>> rect;\n vector<int> r_area;\n int total_area;\n Solution(vector<vector<int>> rects) {\n rect = rects;\n int total = 0;\n for (int i = 0; i < rects.size(); i++) {\n total += (rects[i][2] - rects[i][0]+1)*(rects[i][3] - rects... | 9 | 0 | [] | 4 |
random-point-in-non-overlapping-rectangles | Random Point testcase 32/35 fault | random-point-testcase-3235-fault-by-user-6onp | I cannot understand why the testcase 32 fails.\nAll points inside rectangles. Whats wrong ?\n\n\nclass Solution {\n int[][] rects;\n\n public Solution(int | user0181c | NORMAL | 2020-08-22T11:36:44.291315+00:00 | 2020-08-22T11:39:01.261361+00:00 | 815 | false | I cannot understand why the testcase 32 fails.\nAll points inside rectangles. Whats wrong ?\n\n```\nclass Solution {\n int[][] rects;\n\n public Solution(int[][] rects) {\n this.rects = rects;\n }\n\n public int[] pick() {\n int [] res = new int[2];\n Random random = new Random();\n\n ... | 8 | 0 | ['Java'] | 10 |
random-point-in-non-overlapping-rectangles | JAVA 10+ lines TreeMap Sorted With Area | java-10-lines-treemap-sorted-with-area-b-2qcb | I borrow the idea from 880. Random Pick with Weight\nBut this time we use area as key.\n\nclass Solution {\n TreeMap<Integer, int[]> map= new TreeMap<>();\n | caraxin | NORMAL | 2018-07-27T05:24:06.059570+00:00 | 2018-09-09T03:36:24.263796+00:00 | 1,588 | false | I borrow the idea from [880. Random Pick with Weight\n](https://leetcode.com/problems/random-pick-with-weight/discuss/154024/JAVA-8-lines-TreeMap)But this time we use area as key.\n```\nclass Solution {\n TreeMap<Integer, int[]> map= new TreeMap<>();\n Random rnd= new Random();\n int area= 0;\n public Solut... | 8 | 1 | [] | 1 |
random-point-in-non-overlapping-rectangles | Java solution with just call one Random() for each Pick()!!!666 | java-solution-with-just-call-one-random-xubm4 | So for the first Random, we can get a Point which is already random and uniform, then We just need to represent it based on its Index in the corresponding Rect. | yunwei_qiu | NORMAL | 2018-07-31T19:53:46.278549+00:00 | 2018-10-25T06:31:18.712561+00:00 | 1,123 | false | So for the first Random, we can get a Point which is already random and uniform, then We just need to represent it based on its Index in the corresponding Rect. \n```\n int[][] rects;\n TreeMap<Integer, Integer> map;\n Random random;\n int sum = 0;\n public Solution(int[][] rects) {\n this.rects =... | 7 | 2 | [] | 3 |
random-point-in-non-overlapping-rectangles | Python O(n) with approach | python-on-with-approach-by-obose-20dk | Intuition\n Describe your first thoughts on how to solve this problem. \nThe problem is asking us to pick a random point within a given list of rectangles. The | Obose | NORMAL | 2023-01-13T15:27:05.107948+00:00 | 2023-01-13T15:27:05.108003+00:00 | 714 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe problem is asking us to pick a random point within a given list of rectangles. The key to solving this problem is to understand the concept of weighting. We can assign a weight to each rectangle based on the number of points it contai... | 6 | 0 | ['Python3'] | 1 |
random-point-in-non-overlapping-rectangles | C++ solutions | c-solutions-by-infox_92-2467 | \nclass Solution {\npublic:\n vector<vector<int>> rects;\n \n Solution(vector<vector<int>> rects) : rects(rects) {\n }\n \n vector<int> pick() | Infox_92 | NORMAL | 2022-12-01T15:28:12.381860+00:00 | 2022-12-01T15:28:12.381898+00:00 | 1,275 | false | ```\nclass Solution {\npublic:\n vector<vector<int>> rects;\n \n Solution(vector<vector<int>> rects) : rects(rects) {\n }\n \n vector<int> pick() {\n int sum_area = 0;\n vector<int> selected;\n \n /* Step 1 - select a random rectangle considering the area of it. */\n ... | 6 | 0 | ['C', 'C++'] | 1 |
random-point-in-non-overlapping-rectangles | Python readable, commented solution, without using bisect | python-readable-commented-solution-witho-ra58 | The idea is simple;\n\n1. Choose a rect, then choose a point inside it.\n2. The bigger the rectangle, the higher the probability of it getting chosen\n\n\nimpor | rainsaremighty | NORMAL | 2020-08-22T10:00:34.415946+00:00 | 2020-08-22T11:59:20.202749+00:00 | 426 | false | The idea is simple;\n\n1. Choose a rect, then choose a point inside it.\n2. The bigger the rectangle, the higher the probability of it getting chosen\n\n```\nimport random\n\nclass Solution:\n\n def __init__(self, rects: List[List[int]]):\n self.rects = rects\n\n \n # I am more of a list compreh... | 6 | 0 | ['Python'] | 2 |
random-point-in-non-overlapping-rectangles | Python O(n) by pool sampling. [w/ Visualization] | python-on-by-pool-sampling-w-visualizati-9ldd | Hint:\n\nThink of pool sampling.\n\nTotal n pools, and total P points\n\n\n\nEach rectangle acts as pool_i with points p_i by itself,\nwhere p_i = ( x_i_2 - x_ | brianchiang_tw | NORMAL | 2020-08-22T09:44:40.997786+00:00 | 2020-08-22T09:45:09.812317+00:00 | 1,171 | false | **Hint**:\n\nThink of **pool sampling**.\n\nTotal **n** pools, and total **P** points\n\n\n\nEach **rectangle** acts as **pool_i** with points **p_i** by itself,\nwhere p_i = ( x_i_2 - x_i_1 + 1) * ( y_i_2 - y... | 6 | 0 | ['Math', 'Python', 'Python3'] | 1 |
random-point-in-non-overlapping-rectangles | No TreeMap, Use Reservoir Sampling Java Solution, One Pass | no-treemap-use-reservoir-sampling-java-s-2is4 | \nclass Solution {\n Random random;\n int[][] rects;\n public Solution(int[][] rects) {\n random = new Random();\n this.rects = rects;\n | jkone | NORMAL | 2019-07-25T06:16:48.057001+00:00 | 2019-07-25T06:16:48.057035+00:00 | 330 | false | ```\nclass Solution {\n Random random;\n int[][] rects;\n public Solution(int[][] rects) {\n random = new Random();\n this.rects = rects;\n }\n \n public int[] pick() {\n int sum = 0;\n // the idx of rect that will be selected\n int idx = 0;\n for (int i = 0; ... | 6 | 0 | [] | 2 |
random-point-in-non-overlapping-rectangles | Is [1,0,3,0] a valid rectangle? | is-1030-a-valid-rectangle-by-branzhang-kpde | From Description:\n\nExample 2:\n\nInput: \n["Solution","pick","pick","pick","pick","pick"]\n[[[[-2,-2,-1,-1],[1,0,3,0]]],[],[],[],[],[]]\nOutput: \n[null,[-1,- | branzhang | NORMAL | 2018-08-29T09:38:32.156629+00:00 | 2018-08-29T09:38:32.156675+00:00 | 717 | false | From Description:\n\nExample 2:\n```\nInput: \n["Solution","pick","pick","pick","pick","pick"]\n[[[[-2,-2,-1,-1],[1,0,3,0]]],[],[],[],[],[]]\nOutput: \n[null,[-1,-2],[2,0],[-2,-1],[3,0],[-2,-2]]\n```\n\nIs [1,0,3,0] a valid rectangle? | 6 | 0 | [] | 1 |
random-point-in-non-overlapping-rectangles | Easy Code, Intuition😇 & Explanation 🎯 | easy-code-intuition-explanation-by-iamor-bwcc | IntuitionRead comments inside the code, easy to understandApproachBinary search on points of rectangle, rand() will always bring unique valueComplexity
Time co | Iamorphouz | NORMAL | 2025-01-05T06:30:16.989610+00:00 | 2025-01-05T06:30:16.989610+00:00 | 384 | false | # Intuition
Read comments inside the code, easy to understand
# Approach
Binary search on points of rectangle, rand() will always bring unique value
# Complexity
- Time complexity:
$$O(log(N))$$ : for each pick(binary Search)
- Space complexity:
$$O(N)$$ : store points count
# Code
```cpp []
class Solution {
public... | 5 | 0 | ['Array', 'Math', 'Binary Search', 'Prefix Sum', 'C++'] | 0 |
random-point-in-non-overlapping-rectangles | 497: Space 95.52%, Solution with step by step explanation | 497-space-9552-solution-with-step-by-ste-xyrw | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n1. Initialize the Solution class with a list of non-overlapping axis-alig | Marlen09 | NORMAL | 2023-03-11T18:09:52.458547+00:00 | 2023-03-11T18:09:52.458589+00:00 | 815 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Initialize the Solution class with a list of non-overlapping axis-aligned rectangles rects.\n\n2. Inside the init function, calculate the total area covered by all the rectangles and store it in a variable called total_ar... | 4 | 0 | ['Array', 'Math', 'Binary Search', 'Python', 'Python3'] | 1 |
random-point-in-non-overlapping-rectangles | [Javascript] AREA is NOT an Appropriate Weight! | javascript-area-is-not-an-appropriate-we-a98c | Here\'s an intuitive thought:\n\n> If rect A is bigger than rect B, it should have a larger weight to get selected.\n> While its # of points are also more, each | lynn19950915 | NORMAL | 2022-05-25T06:15:48.601547+00:00 | 2023-05-27T05:02:05.613938+00:00 | 292 | false | Here\'s an intuitive thought:\n\n> If `rect A` is bigger than `rect B`, it should have a larger weight to get selected.\n> While its `# of points` are also more, **each point** within it has a smaller chance for picked then.\n\nSounds fair, but that\'s not completely right.\n.\n\nTaking `rect A`=2x1, `rect B`=1x1 for e... | 4 | 0 | ['JavaScript'] | 1 |
random-point-in-non-overlapping-rectangles | Minimum Cost For Tickets Solution | C++ | Easy | minimum-cost-for-tickets-solution-c-easy-14rm | Cost[i] -> total amount spent till day i to travell at all the days before it.\n\n\nclass Solution {\npublic:\n int mincostTickets(vector<int>& days, vector< | prisonmike | NORMAL | 2020-08-25T07:34:17.862620+00:00 | 2020-08-25T07:34:56.428859+00:00 | 413 | false | Cost[i] -> total amount spent till day i to travell at all the days before it.\n\n```\nclass Solution {\npublic:\n int mincostTickets(vector<int>& days, vector<int>& costs) {\n set<int> dd(days.begin(),days.end());\n int cost[366];\n memset(cost,0,sizeof(cost));\n int one=costs[0],seven=c... | 4 | 2 | ['Dynamic Programming', 'C'] | 1 |
random-point-in-non-overlapping-rectangles | C# easy solution | c-easy-solution-by-quico14-jni3 | \npublic class Solution {\n public int[][] SolutionPoints { get; set; }\n public SortedDictionary<int, int> RectangleByArea = new SortedDictionary<int, in | quico14 | NORMAL | 2020-08-22T10:23:05.556632+00:00 | 2020-08-22T10:23:05.556664+00:00 | 241 | false | ```\npublic class Solution {\n public int[][] SolutionPoints { get; set; }\n public SortedDictionary<int, int> RectangleByArea = new SortedDictionary<int, int>();\n public int NumberOfSolutions { get; set; }\n public Random Random = new Random();\n \n public Solution(int[][] rects)\n {\n Sol... | 4 | 0 | [] | 0 |
random-point-in-non-overlapping-rectangles | C++ solution using std::discrete_distribution | c-solution-using-stddiscrete_distributio-e9gd | discrete_distribution can be used to select a random rectangle with weighted probabilities.\n- uniform_int_distribution can be used to select random coordinates | yessenamanov | NORMAL | 2019-12-08T18:19:08.986973+00:00 | 2020-02-12T17:43:06.082238+00:00 | 254 | false | - [discrete_distribution](https://en.cppreference.com/w/cpp/numeric/random/discrete_distribution) can be used to select a random rectangle with weighted probabilities.\n- [uniform_int_distribution](https://en.cppreference.com/w/cpp/numeric/random/uniform_int_distribution) can be used to select random coordinates of a p... | 4 | 0 | [] | 0 |
random-point-in-non-overlapping-rectangles | Python 3 Weighting by area | python-3-weighting-by-area-by-thaeliosra-nd87 | I am a bit baffled by this problem in particular and how leetcode judges problems involving randomness in general. The code below selects a rectangle at random | thaeliosraedkin1 | NORMAL | 2020-08-22T17:52:22.892116+00:00 | 2020-08-22T17:52:55.166030+00:00 | 169 | false | I am a bit baffled by this problem in particular and how leetcode judges problems involving randomness in general. The code below selects a rectangle at random weighted by its area. This is necessary because to select some point with uniform probability means the selected point is more likely to land in a rectangle wit... | 3 | 0 | [] | 0 |
random-point-in-non-overlapping-rectangles | Explained Javascript Solution, Using binary search | explained-javascript-solution-using-bina-2v8m | \n/**\n /**\n * @param {number[][]} rects\n */\nvar Solution = function(rects) {\n this.rects = rects;\n this.map = {};\n this.sum = 0;\n // we put | thebigbadwolf | NORMAL | 2020-08-22T10:00:18.052349+00:00 | 2020-08-22T10:02:44.735583+00:00 | 498 | false | ```\n/**\n /**\n * @param {number[][]} rects\n */\nvar Solution = function(rects) {\n this.rects = rects;\n this.map = {};\n this.sum = 0;\n // we put in the map the number of points that belong to each rect\n for(let i in rects) {\n const rect = rects[i];\n // the number of points can be p... | 3 | 0 | ['Binary Tree', 'JavaScript'] | 1 |
random-point-in-non-overlapping-rectangles | Python 1-liner | python-1-liner-by-ekovalyov-c2ye | The solution uses library function choice from package random.\n\nFinal version after refactoring:\n\nclass Solution:\n def __init__(self, rects: List[List[i | ekovalyov | NORMAL | 2020-08-22T09:38:55.852633+00:00 | 2020-08-22T11:17:35.803998+00:00 | 234 | false | The solution uses library function choice from package random.\n\nFinal version after refactoring:\n```\nclass Solution:\n def __init__(self, rects: List[List[int]]):\n self.r=[*zip(*[[r,(r[2]-r[0]+1)*(r[3]-r[1]+1)]for r in rects])]\n def pick(self) -> List[int]:\n return [[randint(r[0],r[2]), randi... | 3 | 0 | [] | 0 |
random-point-in-non-overlapping-rectangles | Python Random choices with weights | python-random-choices-with-weights-by-fo-asq1 | \n(x2-x1+1)*(y2-y1+1) stands for the number of points in rectangle [x1,y1,x2,y2]. So with the weights, we can ensure for every point the chance of being picked | formatmemory | NORMAL | 2019-02-09T01:16:17.490317+00:00 | 2019-02-09T01:16:17.490360+00:00 | 426 | false | \n(x2-x1+1)*(y2-y1+1) stands for the number of points in rectangle [x1,y1,x2,y2]. So with the weights, we can ensure for every point the chance of being picked is evenly.\n\n```\nclass Solution:\n\n def __init__(self, rects: \'List[List[int]]\'):\n self.rects_weight = []\n self.rects = rects\n ... | 3 | 0 | [] | 1 |
random-point-in-non-overlapping-rectangles | Microsoft⭐ || Easy Solution🔥 | microsoft-easy-solution-by-priyanshi_gan-e9mt | #ReviseWithArsh #6Companies30Days challenge 2k24\nCompany 2 :- Microsoft\n\n\n# Code\n\nclass Solution {\npublic:\n Solution(std::vector<std::vector<int>>& r | Priyanshi_gangrade | NORMAL | 2024-01-09T11:18:07.917758+00:00 | 2024-01-09T11:18:07.917786+00:00 | 632 | false | ***#ReviseWithArsh #6Companies30Days challenge 2k24\nCompany 2 :- Microsoft***\n\n\n# Code\n```\nclass Solution {\npublic:\n Solution(std::vector<std::vector<int>>& rects)\n : rects(rects), x(rects[0][0] - 1), y(rects[0][1]), i(0) {}\n\n std::vector<int> pick() {\n // Increment x until reaching the ... | 2 | 0 | ['C++'] | 1 |
random-point-in-non-overlapping-rectangles | Simple Solution using binary search and math || C++ | simple-solution-using-binary-search-and-obwql | Intuition\n Describe your first thoughts on how to solve this problem. \nCount number of points in every rectangle and push till number points in su vector to s | rkkumar421 | NORMAL | 2022-11-12T12:19:40.271031+00:00 | 2022-11-12T12:19:40.271063+00:00 | 526 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nCount number of points in every rectangle and push till number points in su vector to search in which rectangle random point will lie .\n# Approach\n<!-- Describe your approach to solving the problem. -->\nBinary Search\n# Complexity\n- T... | 2 | 0 | ['Math', 'Binary Search', 'C++'] | 1 |
random-point-in-non-overlapping-rectangles | Python3 Solution Explained | Easy to understand | python3-solution-explained-easy-to-under-mmeo | Solution:\n1. First find the sum of the area of all the given rectangles\n2. Create a probability list to determine which rectangle should be selected. A rectan | abrahamshimekt | NORMAL | 2022-10-27T19:56:27.633975+00:00 | 2022-10-27T19:56:27.634013+00:00 | 397 | false | **Solution:**\n1. First find the sum of the area of all the given rectangles\n2. Create a probability list to determine which rectangle should be selected. A rectangle with higher probability will be selected. The ith element of the probability list is the area of the ith rectangle devided by the sum of the area of all... | 2 | 0 | ['Prefix Sum', 'Probability and Statistics', 'Python', 'Python3'] | 0 |
random-point-in-non-overlapping-rectangles | C++ beat 99% | c-beat-99-by-huimingzhou-2kf4 | cpp\nclass Solution {\npublic:\n vector<vector<int>> data;\n int area = 0;\n \n Solution(vector<vector<int>>& rects) {\n for (auto& x : rects | huimingzhou | NORMAL | 2020-10-07T23:25:17.965052+00:00 | 2020-10-07T23:25:17.965087+00:00 | 204 | false | ```cpp\nclass Solution {\npublic:\n vector<vector<int>> data;\n int area = 0;\n \n Solution(vector<vector<int>>& rects) {\n for (auto& x : rects) {\n area += (x[2] - x[0] + 1) * (x[3] - x[1] + 1);\n data.push_back({area, x[0], x[2] - x[0] + 1, x[1], x[3] - x[1] + 1});\n }... | 2 | 0 | [] | 0 |
random-point-in-non-overlapping-rectangles | [C++] Random Points | Simple | c-random-points-simple-by-jmbryan10-ewuh | Build a weighted list of rectangles where the weight is equal to each rectangle\'s area. When picking a random point, first pick a rect from the weighted list a | jmbryan10 | NORMAL | 2020-08-22T18:14:51.853886+00:00 | 2020-08-22T18:15:29.629980+00:00 | 219 | false | Build a weighted list of rectangles where the weight is equal to each rectangle\'s area. When picking a random point, first pick a rect from the weighted list and then pick a random point from within that rect.\n```\nclass Solution {\npublic:\n vector<pair<int, vector<int>>> weightedRects;\n long long totalWeight... | 2 | 0 | [] | 0 |
random-point-in-non-overlapping-rectangles | Short and easy solution but something's wrong! Cannot pass 3 testcases - used random.choice() | short-and-easy-solution-but-somethings-w-2dm7 | Hi, I tried solving it using random.choice(). Firstly, I made an array that had X and Y ranges in a list for each rectangle. Later, I simply used random.choice | sanya638 | NORMAL | 2020-08-22T09:58:52.996713+00:00 | 2020-08-22T09:58:52.996765+00:00 | 257 | false | Hi, I tried solving it using random.choice(). Firstly, I made an array that had X and Y ranges in a list for each rectangle. Later, I simply used random.choice in selecting the rectangle and later on points by applying choice on selecting x_coordinate and y_coordinate.\n\nI don\'t know how to explain the approach exact... | 2 | 0 | [] | 4 |
random-point-in-non-overlapping-rectangles | [Java] Distribute The Points via TreeMap | java-distribute-the-points-via-treemap-b-2x83 | \nclass Solution {\n private TreeMap<Integer, Integer> map;\n private int[][] minmax;\n private int len;\n private int sum;\n \n public Soluti | blackspinner | NORMAL | 2020-08-22T05:12:21.318425+00:00 | 2020-08-22T05:12:21.318577+00:00 | 271 | false | ```\nclass Solution {\n private TreeMap<Integer, Integer> map;\n private int[][] minmax;\n private int len;\n private int sum;\n \n public Solution(int[][] rects) {\n map = new TreeMap<>();\n len = rects.length;\n minmax = new int[len][4];\n sum = 0;\n for (int i = 0... | 2 | 1 | [] | 0 |
random-point-in-non-overlapping-rectangles | C# Solution | c-solution-by-leonhard_euler-4ab7 | \npublic class Solution \n{\n private int[][] rects;\n private int[] sums;\n private Random random;\n\n public Solution(int[][] rects) \n {\n | Leonhard_Euler | NORMAL | 2019-07-27T04:20:07.983033+00:00 | 2019-07-27T04:20:07.983063+00:00 | 178 | false | ```\npublic class Solution \n{\n private int[][] rects;\n private int[] sums;\n private Random random;\n\n public Solution(int[][] rects) \n {\n this.rects = rects;\n sums = new int[rects.Length];\n for(int i = 0; i <rects.Length; i++)\n {\n var area = RectAreaPoint... | 2 | 0 | [] | 1 |
random-point-in-non-overlapping-rectangles | Python 3 Solution (using random.randint and bisect.bisect_left) | python-3-solution-using-randomrandint-an-b35d | \nimport bisect\nimport random\n\n\nclass Solution:\n\n def __init__(self, rects: List[List[int]]):\n self.rects = rects\n # number of points i | jinjiren | NORMAL | 2019-02-28T11:30:40.036695+00:00 | 2019-02-28T11:30:40.036740+00:00 | 514 | false | ```\nimport bisect\nimport random\n\n\nclass Solution:\n\n def __init__(self, rects: List[List[int]]):\n self.rects = rects\n # number of points in each rectangle\n self.counts = [(x2 - x1 + 1) * (y2 - y1 + 1) \n for x1, y1, x2, y2 in rects]\n self.total = sum(self.c... | 2 | 0 | [] | 0 |
random-point-in-non-overlapping-rectangles | [Rust] Solution using BTreeMap | rust-solution-using-btreemap-by-talalsha-a7rl | Check @remember8964 for explanation\n\n# Code\nrust []\nuse rand::Rng;\nuse std::collections::BTreeMap;\n\nstruct Solution {\n total_sum: i32,\n mp: BTree | talalshafei | NORMAL | 2024-11-27T20:01:05.299227+00:00 | 2024-11-27T20:01:05.299260+00:00 | 20 | false | Check @[remember8964](https://leetcode.com/problems/random-point-in-non-overlapping-rectangles/solutions/316890/trying-to-explain-why-the-intuitive-solution-wont-work) for explanation\n\n# Code\n```rust []\nuse rand::Rng;\nuse std::collections::BTreeMap;\n\nstruct Solution {\n total_sum: i32,\n mp: BTreeMap<i32, ... | 1 | 0 | ['Binary Search', 'Rust'] | 0 |
random-point-in-non-overlapping-rectangles | Based on the area | based-on-the-area-by-gorums-n9jy | Intuition\nUsing copilot\n\n# Approach\n1.\tInitialization (Solution constructor): The algorithm starts by calculating the area of each rectangle. The area is c | gorums | NORMAL | 2024-03-05T16:47:17.501673+00:00 | 2024-03-05T16:47:17.501701+00:00 | 80 | false | # Intuition\nUsing copilot\n\n# Approach\n1.\tInitialization (Solution constructor): The algorithm starts by calculating the area of each rectangle. The area is calculated as (x2 - x1 + 1) * (y2 - y1 + 1), where (x1, y1) are the coordinates of the bottom-left corner and (x2, y2) are the coordinates of the top-right cor... | 1 | 0 | ['Randomized', 'C#'] | 0 |
random-point-in-non-overlapping-rectangles | Try to Explain with pen ,paper... | try-to-explain-with-pen-paper-by-ankit14-a0wu | Similar Question - https://leetcode.com/problems/random-pick-with-weight/description/\n# Approach\n Describe your approach to solving the problem. \n### Look c | ankit1478 | NORMAL | 2024-01-09T13:06:12.200036+00:00 | 2024-01-09T13:24:27.104526+00:00 | 287 | false | 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\n!... | 1 | 0 | ['Ordered Map', 'Java'] | 1 |
random-point-in-non-overlapping-rectangles | EAZY || 90% || 30DAYS 6COMPAINES | eazy-90-30days-6compaines-by-bhav1729-oajo | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time | bhav1729 | NORMAL | 2024-01-06T19:40:28.060781+00:00 | 2024-01-06T19:40:28.060815+00:00 | 20 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --... | 1 | 0 | ['Array', 'Math', 'Binary Search', 'Reservoir Sampling', 'Prefix Sum', 'Ordered Set', 'Randomized', 'C++'] | 0 |
random-point-in-non-overlapping-rectangles | Java solution with comment explanation | java-solution-with-comment-explanation-b-l190 | \n\n# Code\n\nclass Solution {\n \n int[][] rects;\n TreeMap<Integer, Integer> weightedRectIndex = new TreeMap<>();\n int nPoints = 0;\n \n Ra | safo-samson | NORMAL | 2023-05-12T09:54:10.140279+00:00 | 2023-05-12T09:54:10.140305+00:00 | 788 | false | \n\n# Code\n```\nclass Solution {\n \n int[][] rects;\n TreeMap<Integer, Integer> weightedRectIndex = new TreeMap<>();\n int nPoints = 0;\n \n Random rng = new Random();\n\n public Solution(int[][] rects) {\n this.rects = rects;\n int index = 0;\n for (int[] rect : rects) {\n\t... | 1 | 0 | ['Array', 'Math', 'Binary Search', 'Reservoir Sampling', 'Java'] | 0 |
random-point-in-non-overlapping-rectangles | Solution | solution-by-deleted_user-pgif | C++ []\nclass Solution {\npublic:\n vector<vector<int>> r;\n vector<int> acc;\n Solution(vector<vector<int>>& rects){\n\n std::ios_base::sync_wi | deleted_user | NORMAL | 2023-05-10T08:10:26.165510+00:00 | 2023-05-10T08:58:05.372264+00:00 | 340 | false | ```C++ []\nclass Solution {\npublic:\n vector<vector<int>> r;\n vector<int> acc;\n Solution(vector<vector<int>>& rects){\n\n std::ios_base::sync_with_stdio(false);\n cin.tie(0);\n\n r = rects;\n int len = rects.size();\n acc = vector<int>(len, 0);\n\n for(int i = 0; i ... | 1 | 0 | ['C++', 'Java', 'Python3'] | 1 |
random-point-in-non-overlapping-rectangles | Python | Binary Search | O(logn) per Pick | python-binary-search-ologn-per-pick-by-a-13fw | ```\nfrom itertools import accumulate\nfrom random import randint\nfrom bisect import bisect_left\nclass Solution:\n\n def init(self, rects: List[List[int]]) | aryonbe | NORMAL | 2022-08-25T03:02:59.296710+00:00 | 2022-08-25T03:02:59.296746+00:00 | 122 | false | ```\nfrom itertools import accumulate\nfrom random import randint\nfrom bisect import bisect_left\nclass Solution:\n\n def __init__(self, rects: List[List[int]]):\n self.rects = rects\n self.acc = list(accumulate([ (x2-x1+1)*(y2-y1+1) for x1, y1, x2, y2 in rects ], initial = 0))\n\n def pick(self) -... | 1 | 0 | ['Python'] | 0 |
random-point-in-non-overlapping-rectangles | Help! Why no working for the last 3 case? (Resolved: count the total pts inside each rec not area) | help-why-no-working-for-the-last-3-case-7kz1v | Now I understand. In order to solve this problem, we assume that the proportion is on the number of total points each rectangle not the area.\nThe difference is | vonnan | NORMAL | 2022-08-13T05:22:07.909102+00:00 | 2023-04-14T17:09:43.539008+00:00 | 336 | false | Now I understand. In order to solve this problem, we assume that the proportion is on the number of total points each rectangle not the area.\nThe difference is as follows\nAssume the rectangle is [x1, y1, x2, y2] where (x1,y1) the bottom left corner and (x2,y2) the top right corner\nThe area of the rectangle is (x2 - ... | 1 | 0 | ['Python3'] | 1 |
random-point-in-non-overlapping-rectangles | [Java] 100% Solution | java-100-solution-by-a_love_r-ycfl | ~~~java\n\nclass Solution {\n // 1. pick a rect\n // 2. pick a point inside this rect\n int[][] rects;\n int[] prefixWeights;\n Random rd;\n\n | a_love_r | NORMAL | 2022-01-30T19:32:23.921130+00:00 | 2022-01-30T19:32:23.921173+00:00 | 136 | false | ~~~java\n\nclass Solution {\n // 1. pick a rect\n // 2. pick a point inside this rect\n int[][] rects;\n int[] prefixWeights;\n Random rd;\n\n public Solution(int[][] rects) {\n this.rects = rects;\n this.prefixWeights = new int[rects.length];\n this.rd = new Random();\n \n... | 1 | 0 | [] | 0 |
random-point-in-non-overlapping-rectangles | Why using one Rand() failed 3 out of 35 test cases, while using 3 Rand() passed all? | why-using-one-rand-failed-3-out-of-35-te-hsx5 | Following is the version of using one rand() per pick, and it failed 3 test cases.\n\nclass Solution {\npublic:\n vector<int> prefix_sum;\n vector<vector< | Peak_2250_MID_in_King-of-Glory | NORMAL | 2022-01-08T01:21:20.631226+00:00 | 2022-01-08T01:21:20.631273+00:00 | 153 | false | Following is the version of using one rand() per pick, and it failed 3 test cases.\n```\nclass Solution {\npublic:\n vector<int> prefix_sum;\n vector<vector<int>> rects_vec;\n int total = 0;\n \n Solution(vector<vector<int>>& rects) {\n int cur = 0;\n for (int i = 0; i < rects.size(); ++i) ... | 1 | 0 | ['C'] | 0 |
random-point-in-non-overlapping-rectangles | Python Binary Search | python-binary-search-by-ypatel38-iy4v | \nclass Solution:\n def __init__(self, rects: List[List[int]]):\n self.rects = rects\n self.search_space = []\n\n for i, rect in enumera | ypatel38 | NORMAL | 2021-09-09T02:06:02.911312+00:00 | 2021-09-09T02:07:05.961048+00:00 | 310 | false | ```\nclass Solution:\n def __init__(self, rects: List[List[int]]):\n self.rects = rects\n self.search_space = []\n\n for i, rect in enumerate(rects):\n a, b, c, d = rect\n self.search_space.append((d - b + 1) * (c - a + 1))\n if i != 0:\n self.sear... | 1 | 0 | ['Binary Tree', 'Python', 'Python3'] | 0 |
random-point-in-non-overlapping-rectangles | Python using choices with weight | python-using-choices-with-weight-by-iori-t9uf | \nimport random\nclass Solution:\n def __init__(self, rects: List[List[int]]):\n self.rects = rects\n self.weights = []\n for i, r in en | iorilan | NORMAL | 2021-06-30T13:28:56.733917+00:00 | 2021-06-30T13:28:56.733964+00:00 | 128 | false | ```\nimport random\nclass Solution:\n def __init__(self, rects: List[List[int]]):\n self.rects = rects\n self.weights = []\n for i, r in enumerate(self.rects):\n x1, y1, x2, y2 = r\n self.weights.append((x2-x1+1)*(y2-y1+1))\n def pick(self) -> List[int]:\n points ... | 1 | 0 | [] | 0 |
random-point-in-non-overlapping-rectangles | C++ Solution using Binary search with explanation | c-solution-using-binary-search-with-expl-ab92 | \nclass Solution {\n //concept is simple\n //we need to pick the coordinates from the rectangle space\n //for that we will use rand() function to find | jyotsnamunjal9 | NORMAL | 2021-01-02T06:09:12.772212+00:00 | 2021-01-02T06:09:12.772256+00:00 | 249 | false | ```\nclass Solution {\n //concept is simple\n //we need to pick the coordinates from the rectangle space\n //for that we will use rand() function to find out randomly any rectangle\n //but to which we will apply rand()\n //that should be cumulative sum of areas why?? because we can\'t use area of any rec... | 1 | 0 | [] | 0 |
random-point-in-non-overlapping-rectangles | Java solution explained using array | java-solution-explained-using-array-by-r-a9ms | class Solution {\n\n int arr[];\n Random rand;\n int cumulativeSum;\n int rectangles[][];\n public Solution(int[][] rects) {\n rectangles | rocx96 | NORMAL | 2020-09-25T05:03:30.826380+00:00 | 2020-09-25T05:11:14.659029+00:00 | 230 | false | class Solution {\n\n int arr[];\n Random rand;\n int cumulativeSum;\n int rectangles[][];\n public Solution(int[][] rects) {\n rectangles = rects;\n rand = new Random();\n int n = rects.length;\n arr = new int[n];\n cumulativeSum = 0;\n for(int i=0;i<n;i++) {\n ... | 1 | 0 | ['Binary Tree'] | 0 |
random-point-in-non-overlapping-rectangles | Go | go-by-dynasty919-na6b | \ntype Solution struct {\n list []rec\n sum int\n}\n\ntype rec struct {\n bottomleft [2]int\n l int\n w int\n prefixPoints int\n}\n\nfunc Cons | dynasty919 | NORMAL | 2020-09-20T08:12:10.018763+00:00 | 2020-09-20T08:12:10.018806+00:00 | 58 | false | ```\ntype Solution struct {\n list []rec\n sum int\n}\n\ntype rec struct {\n bottomleft [2]int\n l int\n w int\n prefixPoints int\n}\n\nfunc Constructor(rects [][]int) Solution {\n list := make([]rec, len(rects))\n sum := 0\n for i, v := range rects {\n a, b := v[2] - v[0] + 1, v[3] - ... | 1 | 0 | [] | 0 |
random-point-in-non-overlapping-rectangles | Python 3 + Prefix Sum & Binary Search Explanation | python-3-prefix-sum-binary-search-explan-3hqs | Since the question asks us to sample a random point in the available space marked out by the rectangles with a uniform probability, we need to weight our choice | amronagdy | NORMAL | 2020-09-13T18:53:05.582712+00:00 | 2020-09-13T18:53:05.582757+00:00 | 116 | false | * Since the question asks us to sample a random point in the available space marked out by the rectangles **with a uniform probability**, we need to weight our choice of which rectangle to pick a random point from by its area.\n\t* Therefore this question can be reduced to a weighted probability selection question.\n\t... | 1 | 0 | [] | 0 |
random-point-in-non-overlapping-rectangles | Largest Component Size by Common Factor in C++ | largest-component-size-by-common-factor-rfj2f | An understandable approach using C++ : map all the elements with its factors, so mapping process would take o(n*sqrt(n)) for all the elements. Then iterate thro | chandms | NORMAL | 2020-08-30T13:38:51.628988+00:00 | 2020-08-30T13:38:51.629025+00:00 | 497 | false | An understandable approach using C++ : map all the elements with its factors, so mapping process would take o(n*sqrt(n)) for all the elements. Then iterate through map and connect consecutive elements under a key, except key=1.Then apply dfs for rest of the process.\n```\nclass Solution {\n void fac(map <int,vector... | 1 | 1 | [] | 0 |
random-point-in-non-overlapping-rectangles | simplest pancake C++ soution using vector , reverse function | simplest-pancake-c-soution-using-vector-i70l0 | \nclass Solution {\npublic:\n void flip(vector<int> &v , int k){\n reverse(v.begin() , v.begin() + k+1);\n }\n vector<int> pancakeSort(vector<in | holmes_36 | NORMAL | 2020-08-29T15:27:44.641504+00:00 | 2020-08-29T15:29:36.333655+00:00 | 120 | false | ```\nclass Solution {\npublic:\n void flip(vector<int> &v , int k){\n reverse(v.begin() , v.begin() + k+1);\n }\n vector<int> pancakeSort(vector<int>& A) {\n vector<int>ans;\n int n = A.size();\n //int mx = INT_MIN , imx ;\n for(int sz = n;sz>1;sz--){\n int mx = IN... | 1 | 1 | [] | 0 |
random-point-in-non-overlapping-rectangles | Minimum Cost For Tickets | minimum-cost-for-tickets-by-shin_crayon-ikw8 | Let OPT(i, j) is the minimum cost you need to spend to travel from day i-th to day j-th.\nWe have 3 options to buy ticket at a day:\n Buy 1-day pass with the pr | shin_crayon | NORMAL | 2020-08-27T10:24:37.713789+00:00 | 2020-08-27T10:24:37.713834+00:00 | 127 | false | Let OPT(i, j) is the minimum cost you need to spend to travel from day i-th to day j-th.\nWe have 3 options to buy ticket at a day:\n* Buy 1-day pass with the price costs[0]\n* Buy 7-day pass with the price costs[1]\n* Buy 30-day pass with the price costs[2]\n\nTherefore, we can set a target function as follow:\n```\nO... | 1 | 0 | ['Dynamic Programming', 'Python3'] | 1 |
random-point-in-non-overlapping-rectangles | Minimum Cost For Tickets : Dynamic Programming: C++ | minimum-cost-for-tickets-dynamic-program-1nse | \nclass Solution {\npublic:\n int mincostTickets(vector<int>& days, vector<int>& costs) {\n set<int> dd(days.begin(),days.end());\n int cost[36 | isimran18 | NORMAL | 2020-08-25T10:43:00.870537+00:00 | 2020-08-25T10:43:00.870596+00:00 | 107 | false | ```\nclass Solution {\npublic:\n int mincostTickets(vector<int>& days, vector<int>& costs) {\n set<int> dd(days.begin(),days.end());\n int cost[366];\n memset(cost,0,sizeof(cost));\n int one=costs[0], seven=costs[1], thirty=costs[2];\n for(int i=1;i<=365;i++){\n cost[i] ... | 1 | 0 | [] | 0 |
random-point-in-non-overlapping-rectangles | Java Solution with Explanation | java-solution-with-explanation-by-uddant-5d8x | \nclass Solution {\n Random random;\n TreeMap<Integer,int[]> map;\n int areaSum = 0;\n public Solution(int[][] rects) {\n random = new Random | uddantisaketh2001 | NORMAL | 2020-08-23T07:33:04.310148+00:00 | 2020-08-23T07:33:54.424650+00:00 | 103 | false | ```\nclass Solution {\n Random random;\n TreeMap<Integer,int[]> map;\n int areaSum = 0;\n public Solution(int[][] rects) {\n random = new Random();\n map = new TreeMap<>();\n \n for(int i = 0; i < rects.length; i++){\n int[] rectangleCoordinates = rects[i];\n ... | 1 | 0 | [] | 0 |
random-point-in-non-overlapping-rectangles | C# solution (prefixSum + binary search) | c-solution-prefixsum-binary-search-by-ne-0eeb | ```\npublic class Solution {\n\n public Random random;\n public int[][] rects;\n \n public int[] areasPrefixSum;\n public int areaSum;\n \n | newbiecoder1 | NORMAL | 2020-08-23T06:14:36.100470+00:00 | 2020-08-23T06:14:36.100508+00:00 | 51 | false | ```\npublic class Solution {\n\n public Random random;\n public int[][] rects;\n \n public int[] areasPrefixSum;\n public int areaSum;\n \n public Solution(int[][] rects) {\n \n random = new Random();\n this.rects = rects; \n areasPrefixSum = new int[rects.Length];\... | 1 | 0 | [] | 0 |
random-point-in-non-overlapping-rectangles | Swift Explanation | swift-explanation-by-jtbergman-bzrp | A First Approach\nAn initial idea may be to select a rectangle at random then select a random point in that rectangle. However, we want to select a point unifor | jtbergman | NORMAL | 2020-08-23T02:37:45.019523+00:00 | 2020-08-23T02:37:45.019582+00:00 | 59 | false | **A First Approach**\nAn initial idea may be to select a rectangle at random then select a random point in that rectangle. However, we want to select a point uniformly at random from the entire space. Selecting a rectangle first, then selecting a point from that rectangle would give rectangles with small areas the same... | 1 | 0 | [] | 0 |
random-point-in-non-overlapping-rectangles | Python Simple Solution Explained (video + code) | python-simple-solution-explained-video-c-pyu5 | https://www.youtube.com/watch?v=mED_K_EaZIo\n\n\nimport random\nclass Solution:\n\n def __init__(self, rects: List[List[int]]):\n self.rects = rects\n | spec_he123 | NORMAL | 2020-08-22T22:07:12.938148+00:00 | 2020-08-22T22:07:12.938181+00:00 | 206 | false | https://www.youtube.com/watch?v=mED_K_EaZIo\n[](https://www.youtube.com/watch?v=mED_K_EaZIo)\n```\nimport random\nclass Solution:\n\n def __init__(self, rects: List[List[int]]):\n self.rects = rects\n self.weights = []\n self.sum_ = 0\n \n for x1, y1, x2, y2 in rects:\n ... | 1 | 0 | ['Python', 'Python3'] | 0 |
random-point-in-non-overlapping-rectangles | Most unreadable 1-Liner - just for fun! | most-unreadable-1-liner-just-for-fun-by-fvgei | class Solution:\n\n def __init__(self, rects: List[List[int]]):\n self.W = list(itertools.accumulate((x[2]-x[0]+1)*(x[3]-x[1]+1) for x in rects))\n | user0571rf | NORMAL | 2020-08-22T20:51:21.816974+00:00 | 2020-08-22T20:51:21.817023+00:00 | 171 | false | ```class Solution:\n\n def __init__(self, rects: List[List[int]]):\n self.W = list(itertools.accumulate((x[2]-x[0]+1)*(x[3]-x[1]+1) for x in rects))\n self.rects = rects\n\n def pick(self) -> List[int]:\n return [(xr:=random.randint)((c := self.rects[(ans := bisect.bisect_left(self.W,xr(0,sel... | 1 | 0 | ['Python3'] | 0 |
random-point-in-non-overlapping-rectangles | Clean, straight forward Java solution with step-wise explanation | clean-straight-forward-java-solution-wit-m3gi | Process the input co-ordinates and transform into rectangle objects. Calculate the area of the rectangle and keep a total sum of areas. Use TreeMap to simulate | shivkumar | NORMAL | 2020-08-22T20:42:35.114259+00:00 | 2020-08-22T20:42:35.114308+00:00 | 134 | false | 1. Process the input co-ordinates and transform into rectangle objects. Calculate the area of the rectangle and keep a total sum of areas. Use TreeMap to simulate a ranged map where the rectangle occupies a range block proportional to its area. \n2. The pick would only be uniform if the solution ensures weighted bias ... | 1 | 0 | [] | 0 |
random-point-in-non-overlapping-rectangles | [Python] Readable & Clean Solution with Binary Search | python-readable-clean-solution-with-bina-s0eg | \nimport random\nclass Solution:\n\n def numberOfPoints(self, rect):\n x1, y1, x2, y2 = rect\n return ((abs(x1-x2)+1) * (abs(y1-y2)+1)) \n \ | dschradick | NORMAL | 2020-08-22T19:21:04.711487+00:00 | 2020-08-22T19:26:36.315126+00:00 | 110 | false | ```\nimport random\nclass Solution:\n\n def numberOfPoints(self, rect):\n x1, y1, x2, y2 = rect\n return ((abs(x1-x2)+1) * (abs(y1-y2)+1)) \n \n def sampleFromRect(self, rect):\n x1, y1, x2, y2 = rect\n x = random.randint(x1, x2) \n y = random.randint(y1, y2) \n return... | 1 | 0 | [] | 0 |
random-point-in-non-overlapping-rectangles | C++ Beats 100% on space and time. O(1), a simple goto , to represent a for | c-beats-100-on-space-and-time-o1-a-simpl-k0zn | \nclass Solution {\n int x1, y1, x2, y2;\n int RectPos;\n int Reset;\n vector<vector<int>> *R;\npublic:\n Solution(vector<vector<int>>& rects) {\ | riou23 | NORMAL | 2020-08-22T18:46:27.398231+00:00 | 2020-08-22T18:46:27.398287+00:00 | 81 | false | ```\nclass Solution {\n int x1, y1, x2, y2;\n int RectPos;\n int Reset;\n vector<vector<int>> *R;\npublic:\n Solution(vector<vector<int>>& rects) {\n RectPos = 0;\n Reset = rects[0][2];\n x1 = rects[0][0];\n y1 = rects[0][1];\n x2 = rects[0][2];\n y2 = rects[0][3... | 1 | 1 | [] | 2 |
random-point-in-non-overlapping-rectangles | Someone explain why the simple approach fails here. Thanks...!!! | someone-explain-why-the-simple-approach-xwtmw | Can anyone explain why the below approach is failing...\nI am trying to pick a random rectangle first, then within that rect. , trying to generate x and y coord | shubhamu68 | NORMAL | 2020-08-22T17:18:33.791148+00:00 | 2020-08-22T17:19:32.387770+00:00 | 56 | false | Can anyone explain why the below approach is failing...\nI am trying to pick a random rectangle first, then within that rect. , trying to generate x and y coordinates within range.\nFails for a very large TC->32.\n\n```\nint rects[][];\n\tint len = 0;\n public Solution(int[][] rects) {\n \tthis.rects = rects;\n ... | 1 | 1 | [] | 1 |
random-point-in-non-overlapping-rectangles | What's wrong with my solution of using random numbers | whats-wrong-with-my-solution-of-using-ra-l59z | I am saving all the rectagles and then picking one rectangle from among them and then I am picking a x that is between the min and max x of that rectangle and t | in10se | NORMAL | 2020-08-22T14:39:57.542125+00:00 | 2020-08-22T17:38:12.097485+00:00 | 98 | false | I am saving all the rectagles and then picking one rectangle from among them and then I am picking a x that is between the min and max x of that rectangle and then picking a y that is between the min and max of that rectangle. Could anyone point out to me what I am doing wrong here?\n\n```\nclass Solution {\n int re... | 1 | 0 | [] | 1 |
random-point-in-non-overlapping-rectangles | JavaScript HashMap + Binary Search. ES6, approach included with time and space. comments welcome. | javascript-hashmap-binary-search-es6-app-m2y1 | \n/*\neach rectangle will have to have a number in a map {cumulative area : [Xrange, yrange]}\nfind the range of x and y per rectangle\nuse random number * leng | alexanderywang | NORMAL | 2020-08-22T14:19:14.230707+00:00 | 2020-08-22T15:58:30.351963+00:00 | 133 | false | ```\n/*\neach rectangle will have to have a number in a map {cumulative area : [Xrange, yrange]}\nfind the range of x and y per rectangle\nuse random number * length of range + low of range\n\n** need to pick randomly from sum of all areas. picking random rect and then random point inside rect won\'t do that. if one ar... | 1 | 0 | [] | 0 |
random-point-in-non-overlapping-rectangles | Solution without binary search | solution-without-binary-search-by-bhadr3-rfx2 | \nclass Solution {\n int[][] rects;\n int[] range;\n int totArea = 0;\n Random r = new Random();\n\n public Solution(int[][] rects) {\n th | bhadr3 | NORMAL | 2020-08-22T13:35:10.004522+00:00 | 2020-08-22T14:40:49.102543+00:00 | 194 | false | ```\nclass Solution {\n int[][] rects;\n int[] range;\n int totArea = 0;\n Random r = new Random();\n\n public Solution(int[][] rects) {\n this.rects = rects;\n range = new int[rects.length];\n int id = 0; \n for(int[] rect : rects){\n int area = Math.abs((rect[2]-r... | 1 | 0 | ['Java'] | 0 |
random-point-in-non-overlapping-rectangles | Javascript O(n) initiation O(1) picking using Alias Method | javascript-on-initiation-o1-picking-usin-ecd7 | \nvar Solution = function(rects) {\n // Imagine we put the boxes into a 2D pool and pick one. Then the bigger box will more likely to be seen and picked\n // | hillisanoob | NORMAL | 2020-08-22T11:57:28.573938+00:00 | 2020-08-22T11:57:42.269003+00:00 | 263 | false | ```\nvar Solution = function(rects) {\n // Imagine we put the boxes into a 2D pool and pick one. Then the bigger box will more likely to be seen and picked\n // So first we initialize the pool with the area of the boxes\n let totalArea = 0;\n this.boxes = rects.map(([x1, y1, x2, y2], index, area) => (\n totalAre... | 1 | 1 | ['JavaScript'] | 0 |
random-point-in-non-overlapping-rectangles | Python O(n) time/space solution w/ explanation | python-on-timespace-solution-w-explanati-rjoe | Thought process:\nThis problem asks us to find a way to generate a point inside n rectangles with uniform probability, we have a few options:\nOption 1: a trivi | algorithm_cat | NORMAL | 2020-08-22T10:25:22.733139+00:00 | 2020-08-22T10:45:49.975852+00:00 | 112 | false | **Thought process:**\nThis problem asks us to find a way to generate a point inside n rectangles with uniform probability, we have a few options:\nOption 1: a trivial solution is to keep track of all the valid integer points, when `pick()` is called just generate a random index and return the integer point at that inde... | 1 | 0 | [] | 0 |
random-point-in-non-overlapping-rectangles | C++ Easy Implementation | c-easy-implementation-by-alpha98-abzi | \nclass Solution {\npublic:\n vector<int> v;\n vector<vector<int>> rect;\n Solution(vector<vector<int>>& rects) \n {\n rect=rects;\n i | alpha98 | NORMAL | 2020-08-22T09:08:23.825800+00:00 | 2020-08-22T09:08:23.825854+00:00 | 104 | false | ```\nclass Solution {\npublic:\n vector<int> v;\n vector<vector<int>> rect;\n Solution(vector<vector<int>>& rects) \n {\n rect=rects;\n int area=0;\n for(auto rec:rect)\n {\n int a= (rec[2]- rec[0]+1)*(rec[3]-rec[1]+1);\n area+=a;\n v.push_back(ar... | 1 | 1 | [] | 1 |
random-point-in-non-overlapping-rectangles | Represent points using offsets - FULL explanation with example | represent-points-using-offsets-full-expl-v1kk | Idea:\n\n\n\n Consider above mentioned 3 rectangles. As you can see, rectangles contains 12, 9, 15 integer coordinates respectively.\n So, in total, we have 36 | parin2 | NORMAL | 2020-08-22T08:27:20.034650+00:00 | 2020-08-22T08:28:09.542459+00:00 | 93 | false | **Idea:**\n\n\n\n* Consider above mentioned 3 rectangles. As you can see, rectangles contains `12, 9, 15` integer coordinates respectively.\n* So, in total, we have 36 points to represent. We can represent them ... | 1 | 0 | [] | 0 |
random-point-in-non-overlapping-rectangles | simple, short and clean java solution | simple-short-and-clean-java-solution-by-71j46 | we are using tree map to increase the chance of selecting a rectangle with larger area, then selecting a point in its area randomly.\n\nclass Solution {\n in | frustrated_employee | NORMAL | 2020-08-22T08:18:59.445995+00:00 | 2020-08-22T08:20:11.371259+00:00 | 216 | false | we are using tree map to increase the chance of selecting a rectangle with larger area, then selecting a point in its area randomly.\n```\nclass Solution {\n int[][] rect;\n TreeMap<Integer,Integer> tm=new TreeMap<>();\n int sum=0;\n Random ran=new Random();\n public Solution(int[][] rects) {\n re... | 1 | 0 | [] | 1 |
random-point-in-non-overlapping-rectangles | Java TreeMap | java-treemap-by-hobiter-hz2v | \nclass Solution {\n TreeMap<Integer, Integer> map;\n int[][] rs;\n int sum = 0;\n Random rand = new Random();\n public Solution(int[][] rects) { | hobiter | NORMAL | 2020-07-28T02:34:10.949996+00:00 | 2020-07-28T02:34:10.950041+00:00 | 166 | false | ```\nclass Solution {\n TreeMap<Integer, Integer> map;\n int[][] rs;\n int sum = 0;\n Random rand = new Random();\n public Solution(int[][] rects) {\n map = new TreeMap<>();\n rs = rects;\n for (int i = 0; i < rs.length; i++) {\n map.put(sum, i);\n sum += (rs[i]... | 1 | 0 | [] | 0 |
random-point-in-non-overlapping-rectangles | Easy to understand Python 3 Solution | easy-to-understand-python-3-solution-by-ne413 | The crux of the problem is understanding that the solution is also evaluated on the kind of random distribution your solution offers. A random distribution, uni | subwayharearmy | NORMAL | 2020-07-26T08:42:39.952623+00:00 | 2020-07-26T08:42:39.952666+00:00 | 122 | false | The crux of the problem is understanding that the solution is also evaluated on the kind of random distribution your solution offers. A random distribution, uniform over the areas of the rectangles is expected. \n\nUses Python3\'s random.choices() function that allows weighted sampling from a specified population. The ... | 1 | 0 | [] | 0 |
random-point-in-non-overlapping-rectangles | C++: Sweet&Simple solution with explanation | c-sweetsimple-solution-with-explanation-raj2v | It\'s similar to question#528. My Explanation is here:\n\nhttps://leetcode.com/problems/random-pick-with-weight/discuss/672072/C%2B%2B%3A-Sweet-and-simple-solut | bingabid | NORMAL | 2020-06-06T12:56:17.553676+00:00 | 2020-06-06T12:58:56.255477+00:00 | 156 | false | It\'s similar to question#528. My Explanation is here:\n```\nhttps://leetcode.com/problems/random-pick-with-weight/discuss/672072/C%2B%2B%3A-Sweet-and-simple-solution\n```\nHere is my implementation. For any doubt feel free to comment. \n```\nclass Solution {\n vector<vector<int>> rect;\n vector<int> area;\n i... | 1 | 0 | [] | 0 |
random-point-in-non-overlapping-rectangles | Python, readable, simple and short. | python-readable-simple-and-short-by-g2co-k2lh | python\nfrom random import randint\n\nclass Solution:\n\n def __init__(self, rects: List[List[int]]):\n self.rects = rects\n self.cellSum = [0] | g2codes | NORMAL | 2020-03-19T05:24:35.497394+00:00 | 2020-03-19T05:24:35.497427+00:00 | 214 | false | ```python\nfrom random import randint\n\nclass Solution:\n\n def __init__(self, rects: List[List[int]]):\n self.rects = rects\n self.cellSum = [0]\n \n # Count the number of cells in each rectangle and add to cellSum as a\n # cumulative sum.\n # cellSum is initialized to 0, ... | 1 | 0 | [] | 1 |
random-point-in-non-overlapping-rectangles | c++ simple solution by transfering all 2D rectangles to a 1D line | c-simple-solution-by-transfering-all-2d-5aean | My idea is to transfer all rectangles to a 1D line, then random pick up one point from the line. Finally convert the point in the line back to its\' real positi | hanzhoutang | NORMAL | 2019-09-19T04:44:55.367146+00:00 | 2019-09-20T02:50:45.968027+00:00 | 247 | false | My idea is to transfer all rectangles to a 1D line, then random pick up one point from the line. Finally convert the point in the line back to its\' real position. \n```\nclass Solution {\npublic:\n map<int,int> toRect;\n vector<vector<int>> rs;\n int total = 0;\n Solution(vector<vector<int>>& rects) : rs(r... | 1 | 0 | [] | 0 |
random-point-in-non-overlapping-rectangles | Python intuitive solution | python-intuitive-solution-by-mikeyliu-ft71 | i\'m sure this can be optimized in some ways.\n\nfrom random import randint,choices\nclass Solution:\n\n def __init__(self, R: List[List[int]]):\n sel | mikeyliu | NORMAL | 2019-08-28T06:47:40.736856+00:00 | 2019-08-28T06:47:40.736890+00:00 | 145 | false | i\'m sure this can be optimized in some ways.\n```\nfrom random import randint,choices\nclass Solution:\n\n def __init__(self, R: List[List[int]]):\n self.R=R\n self.A=[(c-a+1)*(d-b+1) for a,b,c,d in R]\n\n def pick(self) -> List[int]:\n a,b,c,d=choices(population=self.R,weights=self.A,k=1)[0... | 1 | 1 | [] | 0 |
random-point-in-non-overlapping-rectangles | Anyone Know what it wrong with this? (C# Easy) | anyone-know-what-it-wrong-with-this-c-ea-tjsj | Failing 3 testcases from 35. I did not know what is wrong here.\n\n private static int[][] arrs;\n private static int arrsIndex = 0;\n priv | daviti | NORMAL | 2019-06-05T07:39:36.412937+00:00 | 2019-06-05T07:39:36.412975+00:00 | 127 | false | Failing 3 testcases from 35. I did not know what is wrong here.\n```\n private static int[][] arrs;\n private static int arrsIndex = 0;\n private static Random rnd = new Random();\n\n public static void Solution(int[][] rects)\n {\n arrs = rects.Where(r => r.Length != 0).To... | 1 | 0 | [] | 1 |
random-point-in-non-overlapping-rectangles | For help ! I don't know why I was wrong | for-help-i-dont-know-why-i-was-wrong-by-s1qhv | here is my answer. \nit pass 32/35 testcase,\nbut when use \n[[[82918473, -57180867, 82918476, -57180863],\n[83793579, 18088559, 83793580, 18088560],\n[66574245 | leeqh | NORMAL | 2018-09-04T06:17:39.075962+00:00 | 2018-09-04T06:17:39.076005+00:00 | 331 | false | here is my answer. \nit pass 32/35 testcase,\nbut when use \n**[[[82918473, -57180867, 82918476, -57180863],\n[83793579, 18088559, 83793580, 18088560],\n[66574245, 26243152, 66574246, 26243153],\n[72983930, 11921716, 72983934, 11921720]]]**\nit wiil be wrong.\n\n```\nfrom random import *\nclass Solution(object):\n \... | 1 | 0 | [] | 1 |
random-point-in-non-overlapping-rectangles | Easy Java with a List | easy-java-with-a-list-by-bianhit-cotx | \nclass Solution {\n int area = 0;\n List<Integer> list = new ArrayList<>();\n int[][] rects;\n public Solution(int[][] rects) {\n this.rects | bianhit | NORMAL | 2018-08-01T14:53:16.691975+00:00 | 2018-09-09T03:36:33.319374+00:00 | 177 | false | ```\nclass Solution {\n int area = 0;\n List<Integer> list = new ArrayList<>();\n int[][] rects;\n public Solution(int[][] rects) {\n this.rects = rects;\n for (int[] rect : rects) {\n area += (rect[2]-rect[0] + 1)*(rect[3] - rect[1] + 1);\n list.add(area);\n }\n ... | 1 | 1 | [] | 0 |
random-point-in-non-overlapping-rectangles | Python3 O(log(n)) solution using statistical ideas | python3-ologn-solution-using-statistical-nldu | I am currently studying statistics, so thought it would be a nice practice to apply a bit of that here.\n\nWhat we want here, per requirement, is that every int | de_mexico | NORMAL | 2018-07-30T21:10:40.983817+00:00 | 2018-07-30T21:10:40.983817+00:00 | 281 | false | I am currently studying statistics, so thought it would be a nice practice to apply a bit of that here.\n\nWhat we want here, per requirement, is that every integer point has the same probability of being picked. Now, since points are distributed among rectangles we could use a sampling technique where we pick first a ... | 1 | 0 | [] | 0 |
random-point-in-non-overlapping-rectangles | C++ single rand() call | c-single-rand-call-by-stkoso-gms8 | Revised from Random Pick with weight, use area as weight\n\nclass Solution {\n random_device rd;\n vector<int> areas;\n vector<vector<int>> rects;\npub | stkoso | NORMAL | 2018-07-30T06:37:39.730564+00:00 | 2018-09-09T19:54:59.327901+00:00 | 567 | false | Revised from [Random Pick with weight](https://leetcode.com/problems/random-pick-with-weight), use area as weight\n```\nclass Solution {\n random_device rd;\n vector<int> areas;\n vector<vector<int>> rects;\npublic:\n Solution(vector<vector<int>> input_rects) {\n int total = 0;\n rects = move(... | 1 | 1 | [] | 1 |
random-point-in-non-overlapping-rectangles | c++ wighted random, O(logn) for pick() | c-wighted-random-ologn-for-pick-by-gemhu-7a5j | \nclass Solution {\npublic:\n Solution(vector<vector<int>> rects):rects(rects) {\n\n sum = 0;\n \n for(auto& e : rects){\n \n | gemhung | NORMAL | 2018-07-29T07:14:23.421569+00:00 | 2018-10-25T06:31:43.025849+00:00 | 255 | false | ```\nclass Solution {\npublic:\n Solution(vector<vector<int>> rects):rects(rects) {\n\n sum = 0;\n \n for(auto& e : rects){\n \n const auto row = e[2]-e[0]+1;\n const auto col = e[3]-e[1]+1;\n \n sum += row*col;\n \n dp... | 1 | 1 | [] | 0 |
random-point-in-non-overlapping-rectangles | Python Easy Solution | python-easy-solution-by-crazyphotonzz-fo66 | This is simply a Python implementation of the solution at: https://leetcode.com/problems/random-point-in-non-overlapping-rectangles/discuss/154130/Java-randomly | crazyphotonzz | NORMAL | 2018-07-27T20:51:01.029244+00:00 | 2018-07-27T20:51:01.029244+00:00 | 442 | false | This is simply a Python implementation of the solution at: https://leetcode.com/problems/random-point-in-non-overlapping-rectangles/discuss/154130/Java-randomly-pick-a-rectangle-then-pick-a-point-inside\n\nThe idea is simply to pick a rectangle and pick points in it randomly. \nA dictionary is created to store the sum ... | 1 | 0 | [] | 0 |
random-point-in-non-overlapping-rectangles | Easy Solution with math | easy-solution-with-math-by-skh37744-bh8x | Code | skh37744 | NORMAL | 2025-03-16T04:18:43.885704+00:00 | 2025-03-16T04:18:43.885704+00:00 | 5 | false | # Code
```python3 []
import random
class Solution:
def __init__(self, rects: List[List[int]]):
self.tot = 0
self.S, self.X, self.Y = [], [], []
self.rects = []
# For each rectangle, there are (x_i - a_i + 1) * (y_i - b_i + 1) integer points
for rect in rects:
... | 0 | 0 | ['Python3'] | 0 |
random-point-in-non-overlapping-rectangles | Help me figure out why do 3 test cases get WA | help-me-figure-out-why-do-3-test-cases-g-ij15 | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | harry331 | NORMAL | 2025-03-14T11:59:50.039214+00:00 | 2025-03-14T11:59:50.039214+00:00 | 2 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
`... | 0 | 0 | ['C++'] | 0 |
random-point-in-non-overlapping-rectangles | Point in non-overlapping rectangles [C] (Not recommended in C) | point-in-non-overlapping-rectangles-c-no-l0ix | IntuitionMy first idea was to parse all the points from each rectangle and put them in the Solution struct such that each "pick" call is effectively just O(1) w | isjoltz | NORMAL | 2025-03-13T22:18:25.411557+00:00 | 2025-03-13T22:18:25.411557+00:00 | 4 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
My first idea was to parse all the points from each rectangle and put them in the Solution struct such that each "pick" call is effectively just O(1) with the random number selection on size; the problem with that is the worst case could ma... | 0 | 0 | ['C'] | 0 |
random-point-in-non-overlapping-rectangles | C++ 100% beat | c-100-beat-by-aviadblumen-91qd | IntuitionApproachComplexity
Time complexity:
Space complexity:
Code | aviadblumen | NORMAL | 2025-02-26T18:22:52.845137+00:00 | 2025-02-26T18:22:52.845137+00:00 | 4 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
`... | 0 | 0 | ['C++'] | 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.