当前位置: 首页 > news >正文

LeetCode //C - 436. Find Right Interval

436. Find Right Interval

You are given an array of intervals, where i n t e r v a l s [ i ] = [ s t a r t i , e n d i ] intervals[i] = [start_i, end_i] intervals[i]=[starti,endi] and each starti is unique.

The right interval for an interval i is an interval j such that s t a r t j > = e n d i start_j >= end_i startj>=endi and startj is minimized. Note that i may equal j.

Return an array of right interval indices for each interval i. If no right interval exists for interval i, then put -1 at index i.
 

Example 1:

Input: intervals = [[1,2]]
Output: [-1]
Explanation: There is only one interval in the collection, so it outputs -1.

Example 2:

Input: intervals = [[3,4],[2,3],[1,2]]
Output: [-1,0,1]
Explanation: There is no right interval for [3,4].
The right interval for [2,3] is [3,4] since start0 = 3 is the smallest start that is >= end1 = 3.
The right interval for [1,2] is [2,3] since start1 = 2 is the smallest start that is >= end2 = 2.

Example 3:

Input: intervals = [[1,4],[2,3],[3,4]]
Output: [-1,2,-1]
Explanation: There is no right interval for [1,4] and [3,4].
The right interval for [2,3] is [3,4] since start2 = 3 is the smallest start that is >= end1 = 3.

Constraints:
  • 1 < = i n t e r v a l s . l e n g t h < = 2 ∗ 1 0 4 1 <= intervals.length <= 2 * 10^4 1<=intervals.length<=2104
  • intervals[i].length == 2
  • − 1 0 6 < = s t a r t i < = e n d i < = 1 0 6 -10^6 <= starti <= endi <= 10^6 106<=starti<=endi<=106
  • The start point of each interval is unique.

From: LeetCode
Link: 436. Find Right Interval


Solution:

Ideas:
  1. Create an array to store the original indices of the intervals since we’ll sort the intervals based on their start times but still need to return the indices based on the original input order.
  2. Sort the intervals based on their start times while keeping track of their original indices.
  3. For each interval, use binary search to find the smallest interval whose start time is greater than or equal to the current interval’s end time.
  4. Populate the result array with the indices found in step 3. If no such interval is found, put -1 for that interval.
  5. Return the result array.
Code:
int compare(const void* a, const void* b) {int* intervalA = *(int**)a;int* intervalB = *(int**)b;return intervalA[0] - intervalB[0];
}/*** Note: The returned array must be malloced, assume caller calls free().*/
int* findRightInterval(int** intervals, int intervalsSize, int* intervalsColSize, int* returnSize) {// Create an array to store the original index of each intervalint** intervalsWithIndex = (int**)malloc(intervalsSize * sizeof(int*));for (int i = 0; i < intervalsSize; i++) {intervalsWithIndex[i] = (int*)malloc(3 * sizeof(int)); // Increase size to store original indexintervalsWithIndex[i][0] = intervals[i][0]; // startintervalsWithIndex[i][1] = intervals[i][1]; // endintervalsWithIndex[i][2] = i; // original index}// Sort the intervals by their start timeqsort(intervalsWithIndex, intervalsSize, sizeof(int*), compare);// Allocate memory for the result array*returnSize = intervalsSize;int* result = (int*)malloc(intervalsSize * sizeof(int));// Binary search to find the right interval for each intervalfor (int i = 0; i < intervalsSize; i++) {int left = 0, right = intervalsSize - 1;int target = intervals[i][1];int found = -1;while (left <= right) {int mid = left + (right - left) / 2;if (intervalsWithIndex[mid][0] >= target) {found = intervalsWithIndex[mid][2];right = mid - 1;} else {left = mid + 1;}}result[i] = found;}// Free the allocated memoryfor (int i = 0; i < intervalsSize; i++) {free(intervalsWithIndex[i]);}free(intervalsWithIndex);return result;
}

相关文章:

  • MySQL进阶-----索引的语法与SQL性能分析
  • 【Python百日进阶-Web开发-Peewee】Day290 - Peewee 的扩展(十)架构迁移(下)/ 映射
  • Unity 学习日记 12.小球撞击冰块游戏
  • RabbitMQ介绍
  • 【WPF应用16】WPF如何让Canvas上的元素响应鼠标点击事件?
  • 企业产品网络安全建设日志0328
  • 单源最短路径
  • Qlib-Server:量化库数据服务器
  • Apache HBase(二)
  • 康耐视visionpro-CogBlobTool工具详细说明
  • 指标监控和归因分析——数据异常波动
  • ssm网上订餐管理系统开发mysql数据库web结构java编程计算机网页源码eclipse项目采用线性算法
  • 安装paddle detection心得
  • FFmpeg开发笔记(十五)详解MediaMTX的推拉流
  • 计算机专业学习单片机有什么意义吗?
  • canvas实际项目操作,包含:线条,圆形,扇形,图片绘制,图片圆角遮罩,矩形,弧形文字...
  • iOS帅气加载动画、通知视图、红包助手、引导页、导航栏、朋友圈、小游戏等效果源码...
  • Javascript 原型链
  • JS变量作用域
  • Linux各目录及每个目录的详细介绍
  • Linux中的硬链接与软链接
  • markdown编辑器简评
  • Node + FFmpeg 实现Canvas动画导出视频
  • October CMS - 快速入门 9 Images And Galleries
  • Vue实战(四)登录/注册页的实现
  • 关于Flux,Vuex,Redux的思考
  • 回顾 Swift 多平台移植进度 #2
  • 使用docker-compose进行多节点部署
  •  一套莫尔斯电报听写、翻译系统
  • 移动端唤起键盘时取消position:fixed定位
  • 追踪解析 FutureTask 源码
  • No resource identifier found for attribute,RxJava之zip操作符
  • 400多位云计算专家和开发者,加入了同一个组织 ...
  • postgresql行列转换函数
  • 如何在 Intellij IDEA 更高效地将应用部署到容器服务 Kubernetes ...
  • ​总结MySQL 的一些知识点:MySQL 选择数据库​
  • (16)Reactor的测试——响应式Spring的道法术器
  • (8)Linux使用C语言读取proc/stat等cpu使用数据
  • (rabbitmq的高级特性)消息可靠性
  • (分享)自己整理的一些简单awk实用语句
  • (三分钟了解debug)SLAM研究方向-Debug总结
  • (十)T检验-第一部分
  • (一一四)第九章编程练习
  • (转)socket Aio demo
  • (轉)JSON.stringify 语法实例讲解
  • *(长期更新)软考网络工程师学习笔记——Section 22 无线局域网
  • ./include/caffe/util/cudnn.hpp: In function ‘const char* cudnnGetErrorString(cudnnStatus_t)’: ./incl
  • .bat批处理(二):%0 %1——给批处理脚本传递参数
  • .cfg\.dat\.mak(持续补充)
  • .Net - 类的介绍
  • .NET Standard 的管理策略
  • .Net 垃圾回收机制原理(二)
  • .NET 设计一套高性能的弱事件机制
  • .net 怎么循环得到数组里的值_关于js数组
  • .NET/C# 检测电脑上安装的 .NET Framework 的版本