当前位置:网站首页>LeetCode 497(C#)
LeetCode 497(C#)
2022-07-07 15:38:00 【有趣就行】
题目
给定一个由非重叠的轴对齐矩形的数组 rects ,其中 rects[i] = [ai, bi, xi, yi] 表示 (ai, bi) 是第 i 个矩形的左下角点,(xi, yi) 是第 i 个矩形的右上角点。设计一个算法来随机挑选一个被某一矩形覆盖的整数点。矩形周长上的点也算做是被矩形覆盖。所有满足要求的点必须等概率被返回。
在给定的矩形覆盖的空间内的任何整数点都有可能被返回。
请注意 ,整数点是具有整数坐标的点。
实现 Solution 类:
Solution(int[][] rects) 用给定的矩形数组 rects 初始化对象。
int[] pick() 返回一个随机的整数点 [u, v] 在给定的矩形所覆盖的空间内。
示例 1:
输入:
[“Solution”, “pick”, “pick”, “pick”, “pick”, “pick”]
[[[[-2, -2, 1, 1], [2, 2, 4, 6]]], [], [], [], [], []]
输出:
[null, [1, -2], [1, -1], [-1, -2], [-2, -2], [0, 0]]
解释:
Solution solution = new Solution([[-2, -2, 1, 1], [2, 2, 4, 6]]);
solution.pick(); // 返回 [1, -2]
solution.pick(); // 返回 [1, -1]
solution.pick(); // 返回 [-1, -2]
solution.pick(); // 返回 [-2, -2]
solution.pick(); // 返回 [0, 0]
代码
二分 + 前缀和
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;
}
}
边栏推荐
- From Devops to mlops: how do it tools evolve to AI tools?
- Process from creation to encapsulation of custom controls in QT to toolbar (I): creation of custom controls
- LeetCode 648(C#)
- NeRF:DeepFake的最终替代者?
- LeetCode 1477. Find two subarrays with sum as the target value and no overlap
- 浅浅理解.net core的路由
- LeetCode 1049. Weight of the last stone II daily question
- With the latest Alibaba P7 technology system, mom doesn't have to worry about me looking for a job anymore
- Shallow understanding Net core routing
- [fan Tan] those stories that seem to be thinking of the company but are actually very selfish (I: building wheels)
猜你喜欢

How to choose the appropriate automated testing tools?

Seaborn data visualization

Skimage learning (3) -- adapt the gray filter to RGB images, separate colors by immunohistochemical staining, and filter the maximum value of the region

浅浅理解.net core的路由

Skimage learning (3) -- gamma and log contrast adjustment, histogram equalization, coloring gray images

管理VDI的几个最佳实践
Share the latest high-frequency Android interview questions, and take you to explore the Android event distribution mechanism

QML beginner

Skimage learning (1)

User defined view essential knowledge, Android R & D post must ask 30+ advanced interview questions
随机推荐
LeetCode 1043. 分隔数组以得到最大和 每日一题
[video / audio data processing] Shanghai daoning brings you elecard download, trial and tutorial
How to mount the original data disk without damage after the reinstallation of proxmox ve?
L1-028 判断素数(Lua)
[source code interpretation] | source code interpretation of livelistenerbus
NeRF:DeepFake的最终替代者?
What is cloud computing?
LeetCode 312. Poke balloon daily
Flash build API Service - generate API documents
[image sensor] correlated double sampling CDs
赋能智慧电力建设 | 麒麟信安高可用集群管理系统,保障用户关键业务连续性
Lex & yacc of Pisa proxy SQL parsing
防火墙系统崩溃、文件丢失的修复方法,材料成本0元
Skimage learning (1)
Repair method of firewall system crash and file loss, material cost 0 yuan
麒麟信安云平台全新升级!
LeetCode 1696. Jumping game VI daily question
邮件服务器被列入黑名单,如何快速解封?
Skimage learning (3) -- gamma and log contrast adjustment, histogram equalization, coloring gray images
Skimage learning (2) -- RGB to grayscale, RGB to HSV, histogram matching