当前位置:网站首页>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;
}
}
边栏推荐
- The highest level of anonymity in C language
- [software test] from the direct employment of the boss of the enterprise version, looking at the resume, there is a reason why you are not covered
- Golang client server login
- Creative changes brought about by the yuan universe
- Redis集群与扩展
- Charles+Postern的APP抓包
- nest. Database for getting started with JS
- String type, constant type and container type of go language
- idea彻底卸载安装及配置笔记
- Continuous test (CT) practical experience sharing
猜你喜欢
高温火烧浑不怕,钟薛高想留清白在人间
Realize payment function in applet
链式二叉树的基本操作(C语言实现)
Redis cluster and expansion
Review of network attack and defense
Basic concepts and properties of binary tree
RISCV64
50亿,福建又诞生一只母基金
Multimodal point cloud fusion and visual location based on image and laser
Differences between rip and OSPF and configuration commands
随机推荐
How to choose the appropriate automated testing tools?
RISCV64
Comparison and selection of kubernetes Devops CD Tools
Continuous test (CT) practical experience sharing
数据验证框架 Apache BVal 再使用
Complete e-commerce system
初识缓存以及ehcache初体验「建议收藏」
CVPR 2022 - learning non target knowledge for semantic segmentation of small samples
[sword finger offer] 59 - I. maximum value of sliding window
How much does it cost to develop a small program mall?
二叉树的基本概念和性质
[Tawang methodology] Tawang 3W consumption strategy - U & a research method
Tsinghua, Cambridge and UIC jointly launched the first Chinese fact verification data set: evidence-based, covering many fields such as medical society
RIP和OSPF的区别和配置命令
【MIME笔记】
Scientists have observed for the first time that the "electron vortex" helps to design more efficient electronic products
Antisamy: a solution against XSS attack tutorial
AntiSamy:防 XSS 攻击的一种解决方案使用教程
【Base64笔记】「建议收藏」
Cloud security daily 220707: Cisco Expressway series and telepresence video communication server have found remote attack vulnerabilities and need to be upgraded as soon as possible