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 interval
    int** 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 index
        intervalsWithIndex[i][0] = intervals[i][0]; // start
        intervalsWithIndex[i][1] = intervals[i][1]; // end
        intervalsWithIndex[i][2] = i; // original index
    }
    
    // Sort the intervals by their start time
    qsort(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 interval
    for (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 memory
    for (int i = 0; i < intervalsSize; i++) {
        free(intervalsWithIndex[i]);
    }
    free(intervalsWithIndex);
    
    return result;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/498209.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

踏上机器学习之路:探索数据科学的奥秘与魅力

✨✨ 欢迎大家来访Srlua的博文&#xff08;づ&#xffe3;3&#xffe3;&#xff09;づ╭❤&#xff5e;✨✨ &#x1f31f;&#x1f31f; 欢迎各位亲爱的读者&#xff0c;感谢你们抽出宝贵的时间来阅读我的文章。 我是Srlua小谢&#xff0c;在这里我会分享我的知识和经验。&am…

hxp CTF 2021 - A New Novel LFI(新颖的解法)

一、环境 unbentu&#xff0c;docker https://2021.ctf.link/assets/files/includers%20revenge-25377e1ebb23d014.tar.xz 二、解析 PHP Filter 当中有一种 convert.iconv 的 Filter &#xff0c;可以用来将数据从字符集 A 转换为字符集 B &#xff0c;其中这两个字符集可以…

记录pycharm配置Anaconda环境时没有反应的问题

记录pycharm配置Anaconda环境时没有反应的问题 背景 下载最新pycharm后在设置中配置add interpreter Anaconda环境时&#xff0c;x选中conda.ba文件点击Load Enviroments后&#xff0c;没有反应&#xff0c;就闪了一下&#xff0c;也有添加成功 探索路程 试过了重启&#x…

NineData与StarRocks商业化运营公司镜舟科技完成产品兼容认证

近日&#xff0c;镜舟科技与NineData完成产品兼容测试。在经过联合测试后&#xff0c;镜舟科技旗下产品与NineData云原生智能数据管理平台完全兼容&#xff0c;整体运行高效稳定。 镜舟科技致力于帮助中国企业构建卓越的数据分析系统&#xff0c;打造独具竞争力的“数据护城河”…

量化交易入门(二十五)什么是RSI,原理和炒股实操

前面我们了解了KDJ&#xff0c;MACD&#xff0c;MTM三个技术指标&#xff0c;也进行了回测&#xff0c;结果有好有坏&#xff0c;今天我们来学习第四个指标RSI。RSI指标全称是相对强弱指标(Relative Strength Index),是通过比较一段时期内的平均收盘涨数和平均收盘跌数来分析市…

leetcode热题100.柱状图中最大的矩形

Problem: 84. 柱状图中最大的矩形 文章目录 题目思路复杂度Code 题目 给定 n 个非负整数&#xff0c;用来表示柱状图中各个柱子的高度。每个柱子彼此相邻&#xff0c;且宽度为 1 。 求在该柱状图中&#xff0c;能够勾勒出来的矩形的最大面积。 示例 1: 输入&#xff1a;hei…

RAM IP核

1.原理 数据使能信号充当掩码的作用。1表示1字节就是8个位有效。

答题小程序功能细节揭秘:如何提升用户体验和满足用户需求?

答题小程序功能细节体现 随着移动互联网的快速发展&#xff0c;答题小程序成为了用户获取知识、娱乐休闲的重要平台。一款优秀的答题小程序不仅应该具备简洁易用的界面设计&#xff0c;更应该在功能细节上做到极致&#xff0c;以提升用户体验和满足用户需求。本文将从题库随机…

八大技术趋势案例(虚拟现实增强现实)

科技巨变,未来已来,八大技术趋势引领数字化时代。信息技术的迅猛发展,深刻改变了我们的生活、工作和生产方式。人工智能、物联网、云计算、大数据、虚拟现实、增强现实、区块链、量子计算等新兴技术在各行各业得到广泛应用,为各个领域带来了新的活力和变革。 为了更好地了解…

day56 动态规划part13

300. 最长递增子序列 中等 给你一个整数数组 nums &#xff0c;找到其中最长严格递增子序列的长度。 子序列 是由数组派生而来的序列&#xff0c;删除&#xff08;或不删除&#xff09;数组中的元素而不改变其余元素的顺序。例如&#xff0c;[3,6,2,7] 是数组 [0,3,1,6,2,2,…

【FedCoin: A Peer-to-Peer Payment System for Federated Learning】

在这篇论文中&#xff0c;我们提出了FedCoin&#xff0c;一个基于区块链的点对点支付系统&#xff0c;专为联邦学习设计&#xff0c;以实现基于Shapley值的实际利润分配。在FedCoin系统中&#xff0c;区块链共识实体负责计算SV&#xff0c;并且新的区块是基于“Shapley证明”&a…

Linux 入门及其基本指令(上)

目录 0 .引言 1. XShell 远程登录 Linux 1.1 云服务器 1.2. XShell 远程登陆 Linux 2. 详解 Linux 基本指令 2.1 ls 指令 2.2 pwd 指令 2.3 cd 指令 2.4 touch 指令 2.5 mkdir指令 2.6 rmdir指令 && rm 指令 0 .引言 如今&#xff0c;Linux 在服务器…

公众号的AI聊天机器人已修复!谷歌Gemini Pro 10大使用场景解析

大家好&#xff0c;我是木易&#xff0c;一个持续关注AI领域的互联网技术产品经理&#xff0c;国内Top2本科&#xff0c;美国Top10 CS研究生&#xff0c;MBA。我坚信AI是普通人变强的“外挂”&#xff0c;所以创建了“AI信息Gap”这个公众号&#xff0c;专注于分享AI全维度知识…

Kafka重要配置参数全面解读(重要)

欢迎来到我的博客&#xff0c;代码的世界里&#xff0c;每一行都是一个故事 Kafka重要配置参数全面解读(重要 前言auto.create.topics.enableauto.leader.rebalance.enablelog.retention.{hour|minutes|ms}offsets.topic.num.partitions 和 offsets.topic.replication.factorlo…

Long long类型比较大小

long 与 Long long类型和Long类型是不一样&#xff0c;long类型属于基本的数据类型&#xff0c;而Long是long类型的包装类。 结论 long是基本数据类型&#xff0c;判断是否相等时使用 &#xff0c;即可判断值是否相等。&#xff08;基本数据类型没有equals()方法&#xff0…

JVM之EhCache缓存

EhCache缓存 一、EhCache介绍 在查询数据的时候&#xff0c;数据大多来自数据库&#xff0c;咱们会基于SQL语句的方式与数据库交互&#xff0c;数据库一般会基于本地磁盘IO的形式将数据读取到内存&#xff0c;返回给Java服务端&#xff0c;Java服务端再将数据响应给客户端&am…

Ubuntu下使用vscode进行C/C++开发:进阶篇

在vscode上进行C/C++开发的进阶需求: 1) 编写及调试源码时,可进行断点调试、可跨文件及文件夹进行函数调用。 2) 可生成库及自动提取对应的头文件和库文件。 3) 可基于当前工程资源一键点击验证所提取的库文件的正确性。 4) 可结合find_package实现方便的调用。 对于第一…

LLM之RAG实战(三十五)| 使用LangChain的3种query扩展来优化RAG

RAG有时无法从矢量数据库中检索到正确的文档。比如我们问如下问题&#xff1a; 从1980年到1990年&#xff0c;国际象棋的规则是什么&#xff1f; RAG在矢量数据库中进行相似性搜索&#xff0c;来查询与国际象棋规则问题相关的相关文档。然而&#xff0c;在某些情况下&#xff0…

mysql修改用户权限

https://blog.csdn.net/anzhen0429/article/details/78296814

Elasticsearch 和 Kibana 8.13:简化 kNN 和改进查询并行化

作者&#xff1a;Gilad Gal, Tyler Perkins, Srikanth Manvi, Aris Papadopoulos, Trevor Blackford 在 8.13 版本中&#xff0c;Elastic 引入了向量搜索的重大增强&#xff0c;并将 Cohere 嵌入集成到其统一 inference API 中。这些更新简化了将大型语言模型&#xff08;LLM&a…
最新文章