当前位置:网站首页>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;
}
}
边栏推荐
- [fan Tan] those stories that seem to be thinking of the company but are actually very selfish (I: building wheels)
- [source code interpretation] | source code interpretation of livelistenerbus
- 赋能智慧电力建设 | 麒麟信安高可用集群管理系统,保障用户关键业务连续性
- Seaborn数据可视化
- 智慧物流平台:让海外仓更聪明
- Sator推出Web3游戏“Satorspace” ,并上线Huobi
- LeetCode 120. 三角形最小路径和 每日一题
- Matplotlib绘图界面设置
- Proxmox VE重装后,如何无损挂载原有的数据盘?
- LeetCode 1981. 最小化目标值与所选元素的差 每日一题
猜你喜欢

How to add aplayer music player in blog

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

What is cloud computing?

AI来搞财富分配比人更公平?来自DeepMind的多人博弈游戏研究

User defined view essential knowledge, Android R & D post must ask 30+ advanced interview questions

Siggraph 2022 best technical paper award comes out! Chen Baoquan team of Peking University was nominated for honorary nomination

skimage学习(1)

【视频/音频数据处理】上海道宁为您带来Elecard下载、试用、教程

鲲鹏开发者峰会2022 | 麒麟信安携手鲲鹏共筑计算产业新生态

QML初学
随机推荐
Jenkins发布uniapp开发的H5遇到的问题
【Seaborn】组合图表:PairPlot和JointPlot
The process of creating custom controls in QT to encapsulating them into toolbars (II): encapsulating custom controls into toolbars
蓝桥杯 决赛 异或变换 100分
数值 - number(Lua)
Rpcms method of obtaining articles under the specified classification
NeRF:DeepFake的最终替代者?
Test case management tool recommendation
PLC:自动纠正数据集噪声,来洗洗数据集吧 | ICLR 2021 Spotlight
Matplotlib绘图界面设置
LeetCode 648(C#)
LeetCode 1031. Maximum sum of two non overlapping subarrays
[Huang ah code] Why do I suggest you choose go instead of PHP?
浅谈 Apache Doris FE 处理查询 SQL 源码解析
LeetCode 1155. N ways to roll dice one question per day
Share the latest high-frequency Android interview questions, and take you to explore the Android event distribution mechanism
[fan Tan] those stories that seem to be thinking of the company but are actually very selfish (I: building wheels)
Flask搭建api服务-SQL配置文件
LeetCode 312. Poke balloon daily
On Apache Doris Fe processing query SQL source code analysis