-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path0497-RandomPointInNonOverlappingRectangles.cs
54 lines (47 loc) · 1.69 KB
/
0497-RandomPointInNonOverlappingRectangles.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//-----------------------------------------------------------------------------
// Runtime: 324ms
// Memory Usage: 46.8 MB
// Link: https://leetcode.com/submissions/detail/384877520/
//-----------------------------------------------------------------------------
using System;
namespace LeetCode
{
public class _0497_RandomPointInNonOverlappingRectangles
{
private readonly int[][] rects;
private readonly int totalPointCount;
private readonly int[] points;
private readonly Random random;
public _0497_RandomPointInNonOverlappingRectangles(int[][] rects)
{
this.rects = rects;
points = new int[rects.Length];
for (int i = 0; i < rects.Length; i++)
{
var rect = rects[i];
totalPointCount += (rect[2] - rect[0] + 1) * (rect[3] - rect[1] + 1);
points[i] = totalPointCount;
}
random = new Random();
}
public int[] Pick()
{
var target = random.Next(totalPointCount);
var index = Array.BinarySearch(points, target);
if (index < 0)
index = ~index;
else
index++;
var rect = rects[index];
var width = rect[2] - rect[0] + 1;
var height = rect[3] - rect[1] + 1;
var prevPoints = points[index] - width * height;
return new int[2] { rect[0] + (target - prevPoints) % width, rect[1] + (target - prevPoints) / width };
}
}
/**
* Your Solution object will be instantiated and called as such:
* Solution obj = new Solution(rects);
* int[] param_1 = obj.Pick();
*/
}