当前位置:网站首页>LeetCode 497(C#)
LeetCode 497(C#)
2022-07-07 19:04:00 【Just be interesting】
subject
Given an array of rectangles aligned by non overlapping axes rects , among rects[i] = [ai, bi, xi, yi] Express (ai, bi) It's No i Bottom left corner of rectangle ,(xi, yi) It's No i The upper right corner of a rectangle . Design an algorithm to randomly select an integer point covered by a rectangle . Points on the perimeter of a rectangle are also considered to be covered by the rectangle . All points that meet the requirements must be returned with equal probability .
Any integer point in the space covered by a given rectangle can be returned .
Please note that , An integer point is a point with integer coordinates .
Realization Solution class :
Solution(int[][] rects) With the given rectangular array rects Initialize object .
int[] pick() Returns a random integer point [u, v] In the space covered by a given rectangle .
Example 1:
Input :
[“Solution”, “pick”, “pick”, “pick”, “pick”, “pick”]
[[[[-2, -2, 1, 1], [2, 2, 4, 6]]], [], [], [], [], []]
Output :
[null, [1, -2], [1, -1], [-1, -2], [-2, -2], [0, 0]]
explain :
Solution solution = new Solution([[-2, -2, 1, 1], [2, 2, 4, 6]]);
solution.pick(); // return [1, -2]
solution.pick(); // return [1, -1]
solution.pick(); // return [-1, -2]
solution.pick(); // return [-2, -2]
solution.pick(); // return [0, 0]
Code
Two points + The prefix and
public class Solution
{
Random _random;
int[] _sum;
int[][] _rects;
public Solution(int[][] rects)
{
_rects = rects;
_sum = new int[rects.Length + 1];
for (int i = 1; i <= rects.Length; i++)
_sum[i] = _sum[i - 1] + (rects[i - 1][2] - rects[i - 1][0] + 1) * (rects[i - 1][3] - rects[i - 1][1] + 1);
_random = new Random();
}
public int[] Pick()
{
int k = _random.Next(_sum[_sum.Length - 1]);
int index = BinarySearch(k + 1) - 1;
k -= _sum[index];
int[] cnt = _rects[index];
int col = cnt[3] - cnt[1] + 1;
return new int[]{
cnt[0] + k / col, cnt[1] + k % col};
}
private int BinarySearch(int k)
{
var (low, high) = (0, _sum.Length - 1);
while (low < high)
{
int mid = low + (high - low) / 2;
if (_sum[mid] >= k) high = mid;
else low = mid + 1;
}
return high;
}
}
边栏推荐
- A hodgepodge of ICER knowledge points (attached with a large number of topics, which are constantly being updated)
- The moveposition function of rigidbody2d of unity2d solves the problem of people or screen jitter when moving
- The highest level of anonymity in C language
- 50亿,福建又诞生一只母基金
- UVALive – 4621 Cav 贪心 + 分析「建议收藏」
- 二叉树的基本概念和性质
- Tips for short-term operation of spot silver that cannot be ignored
- Kubernetes DevOps CD工具对比选型
- 6.关于jwt
- 嵌入式C语言程序调试和宏使用的技巧
猜你喜欢
能同时做三个分割任务的模型,性能和效率优于MaskFormer!Meta&UIUC提出通用分割模型,性能优于任务特定模型!开源!...
Scientists have observed for the first time that the "electron vortex" helps to design more efficient electronic products
DataSimba推出微信小程序,DataNuza接受全场景考验? | StartDT Hackathon
面试唯品会实习测试岗、抖音实习测试岗【真实投稿】
99% of people don't know that privatized deployment is also a permanently free instant messaging software!
Desci: is decentralized science the new trend of Web3.0?
6.关于jwt
单臂路由和三层交换的简单配置
CVPR 2022 - learning non target knowledge for semantic segmentation of small samples
Multimodal point cloud fusion and visual location based on image and laser
随机推荐
A hodgepodge of ICER knowledge points (attached with a large number of topics, which are constantly being updated)
2022-07-04 matlab reads video frames and saves them
Will low code help enterprises' digital transformation make programmers unemployed?
低代码助力企业数字化转型会让程序员失业?
标准ACL与扩展ACL
unity2d的Rigidbody2D的MovePosition函数移动时人物或屏幕抖动问题解决
强化学习-学习笔记8 | Q-learning
Calculation of torque target value (ftorque) in servo torque control mode
Do you know all four common cache modes?
Redis集群与扩展
nest. Database for getting started with JS
Reinforcement learning - learning notes 8 | Q-learning
Realize payment function in applet
Reject policy of thread pool
Kirk Borne的本周学习资源精选【点击标题直接下载】
String type, constant type and container type of go language
初识缓存以及ehcache初体验「建议收藏」
Do you really understand sticky bag and half bag? 3 minutes to understand it
Redis
How much does it cost to develop a small program mall?