LeetCode //C - 406. Queue Reconstruction by Height

406. Queue Reconstruction by Height

You are given an array of people, people, which are the attributes of some people in a queue (not necessarily in order). Each p e o p l e [ i ] = [ h , k i ] people[i] = [h_, k_i] people[i]=[h,ki] represents the i t h i^{th} ith person of height h i h_i hi with exactly k i k_i ki other people in front who have a height greater than or equal to h i h_i hi.

Reconstruct and return the queue that is represented by the input array people. The returned queue should be formatted as an array queue, where q u e u e [ j ] = [ h j , k j ] queue[j] = [h_j, k_j] queue[j]=[hj,kj] is the attributes of the j t h j^{th} jth person in the queue (queue[0] is the person at the front of the queue).
 

Example 1:

Input: people = [[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]
Output: [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]]
Explanation:
Person 0 has height 5 with no other people taller or the same height in front.
Person 1 has height 7 with no other people taller or the same height in front.
Person 2 has height 5 with two persons taller or the same height in front, which is person 0 and 1.
Person 3 has height 6 with one person taller or the same height in front, which is person 1.
Person 4 has height 4 with four people taller or the same height in front, which are people 0, 1, 2, and 3.
Person 5 has height 7 with one person taller or the same height in front, which is person 1.
Hence [[5,0],[7,0],[5,2],[6,1],[4,4],[7,1]] is the reconstructed queue.

Example 2:

Input: people = [[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]
Output: [[4,0],[5,0],[2,2],[3,2],[1,4],[6,0]]

Constraints:
  • 1 <= people.length <= 2000
  • 0 < = h i < = 1 0 6 0 <= hi <= 10^6 0<=hi<=106
  • 0 < = k i < p e o p l e . l e n g t h 0 <= k_i < people.length 0<=ki<people.length
  • It is guaranteed that the queue can be reconstructed.

From: LeetCode
Link: 406. Queue Reconstruction by Height


Solution:

Ideas:

1. Sorting: As before, we sort the people based on height and the number of people in front.

2. Constructing the Queue: We insert each person into the queue at the index specified by their k value. We make sure to shift the queue as needed to maintain the order.

Code:
// Comparator function for sorting the people array
int cmp(const void *a, const void *b) {
    int *personA = *(int **)a;
    int *personB = *(int **)b;
    if (personA[0] == personB[0]) {
        return personA[1] - personB[1]; // sort by k in ascending order
    }
    return personB[0] - personA[0]; // sort by height in descending order
}

/**
 * Return an array of arrays of size *returnSize.
 * The sizes of the arrays are returned as *returnColumnSizes array.
 * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
 */
int** reconstructQueue(int** people, int peopleSize, int* peopleColSize, int* returnSize, int** returnColumnSizes) {
    // Step 1: Sort the people
    qsort(people, peopleSize, sizeof(int *), cmp);
    
    // Step 2: Initialize the queue with a maximum size
    int **queue = (int **)malloc(sizeof(int *) * peopleSize);
    *returnColumnSizes = (int *)malloc(sizeof(int) * peopleSize);
    *returnSize = 0;

    // Step 3: Insert into the queue
    for (int i = 0; i < peopleSize; i++) {
        // Allocate memory for the current person
        int *person = (int *)malloc(sizeof(int) * 2);
        person[0] = people[i][0]; // height
        person[1] = people[i][1]; // k
        
        // Insert the person at the index specified by k
        for (int j = 0; j < *returnSize; j++) {
            // If j is equal to k, insert the person here
            if (j == person[1]) {
                // Shift all elements to the right
                for (int l = *returnSize; l > j; l--) {
                    queue[l] = queue[l - 1];
                }
                queue[j] = person; // Place the person
                (*returnSize)++; // Increase the size of the queue
                break; // Exit the loop after insertion
            }
        }

        // If not inserted, it means it should go to the end
        if (person[1] == *returnSize) {
            queue[*returnSize] = person;
            (*returnSize)++;
        }
    }

    // Set the sizes of the arrays in returnColumnSizes
    for (int i = 0; i < *returnSize; i++) {
        (*returnColumnSizes)[i] = 2; // Each person has two attributes
    }

    return queue;
}

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

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

相关文章

第二十五:IP网络层的数据,IP数据报

在数据链路层传输的数据叫帧&#xff0c;帧是数据链路层的传输单元。 那么在IP网络层的数据也有一个叫法IP数据报。 IP数据报 IP数据报首部 数据。 数据是传输层传递过来的报文&#xff1b;IP数据报首部格式如下&#xff1a; IP 报头的最小长度为 20 字节&#xff0c;上图…

打造爆款店铺:eBay、Temu、亚马逊卖家如何借助测评提升流量转销量?

无论是eBay还是在亚马逊、沃尔玛、Temu、速卖通、敦煌网、shopee、lazada平台上&#xff0c;流量是店铺获得曝光和销售的关键因素之一。提高店铺流量意味着能够吸引更多的买家浏览和关注&#xff0c;从而增加销售机会。那么&#xff0c;在eBay、Temu、亚马逊店铺中如何有效地提…

大模型还能让我们望梅止渴多久?

大模型梦碎的时间点似乎越来越近。过去一周&#xff0c;有关人工智能的消息糟糕多于积极。 周初&#xff0c;诺贝尔物理学奖和化学奖接连砸向时下正热的人工智能领域。这些奖项出人意料且鼓舞人心&#xff0c;意味着人工智能的确已经根本性地改变了我们生活和科学体系的方方面…

这是我见过最全LLM大模型基础知识学习汇总,建议收藏!

关于如何入门LLM&#xff0c;大多数回答都提到了调用API、训练微调和应用。但是大模型更新迭代太快&#xff0c;这个月发布的大模型打榜成功&#xff0c;仅仅过了一个月就被其他模型超越。训练微调也已经不是难事&#xff0c;有大量开源的微调框架&#xff08;llamafactory、fi…

大模型本地部署教程 | 搭建本地AI问答系统

前言 大家好&#xff0c;因为对AI大模型很感兴趣&#xff0c;相信很多兄弟们跟我一样&#xff0c;所以最近花时间了解了一些&#xff0c;有一些总结&#xff0c;分享给大家&#xff0c;希望对各位有所帮助。 本文将讲解如何在本地搭建一个简易的AI问答系统&#xff0c;主要用j…

【网络】【Linux】多路转接技术

多路转接技术 文章目录 1.select1.1select系统调用及参数介绍1.2select基本工作流程1.3select技术实现echo服务器1.4select优缺点1.5select的适用场景 2.poll&#xff08;了解&#xff09;2.1poll系统调用及参数介绍2.2poll技术实现echo服务器2.3poll优缺点 3.epoll3.1epoll系…

探索 ES6 生成器 ( Generator ) 的异步编程应用

一. 前言 在之前的文章中&#xff0c;我们介绍了生成器函数的基本概念和常见应用&#xff0c;包括异步操作的顺序执行、控制异步流程等&#xff0c;同时也了解到 Promise 和生成器结合的应用可以帮助我们更方便地处理异步操作。详细了解请参考之前的文章&#xff1a; 学习 ES…

前端Vue3字体优化三部曲(webFont、font-spider、spa-font-spider-webpack-plugin)

前端Vue字体优化三部曲&#xff08;webFont、font-spider、spa-font-spider-webpack-plugin&#xff09; 引言 最近前端引入了UI给的思源黑体字体文件&#xff0c;但是字体文件过于庞大&#xff0c;会降低页面首次加载的速度&#xff0c;目前我的项目中需要用到如下三个字体文…

Java 8 的内存结构

Java8内存结构图 虚拟机内存与本地内存的区别 Java虚拟机在执行的时候会把管理的内存分配成不同的区域&#xff0c;这些区域被称为虚拟机内存&#xff0c;同时&#xff0c;对于虚拟机没有直接管理的物理内存&#xff0c;也有一定的利用&#xff0c;这些被利用却不在虚拟机内存…

每天3分钟,彻底弄懂神经网络的优化器(十)Nadam

1. Nadam算法的提出 Nadam&#xff08;Nesterov-accelerated Adaptive Moment Estimation&#xff09;算法是由Tim Salimans et al. 在2016年提出的。这一算法结合了Adam算法和Nesterov Accelerated Gradient&#xff08;NAG&#xff09;算法的优点&#xff0c;旨在提高优化算…

[运维]6.github 本地powershell登录及设置ssh连接

当我在本地的git hub 进行修改后&#xff0c;需要推送到远程github仓库。 当我运行了git add . git commit -m "ingress-controller image" 以后&#xff0c;运行git push origin main&#xff0c;发现由于网络原因无法连接到远程github仓库。 此时开始设置ssh连…

MySQL中表的约束

1&#xff0c;概念 表中一定要有各种约束&#xff0c;通过约束&#xff0c;让我们来插入数据库中的数据是符合预期的。 约束本质是通过技术手段&#xff0c;倒逼程序员插入正确的数据&#xff1b;反过来&#xff0c;站在MySQL的角度来单&#xff0c;内部已经插进来的数据&…

即插即用hilo注意力机制,捕获低频高频特征

题目&#xff1a;Fast Vision Transformers with HiLo Attention 论文地址: https://arxiv.org/abs/2205.13213 创新点 HiLo自注意力机制&#xff1a;作者提出了一种新的自注意力机制&#xff0c;称为HiLo注意力&#xff0c;旨在同时捕捉图像中的高频和低频信息。该方法通过…

通信工程学习:什么是SPI串行外设接口

SPI&#xff1a;串行外设接口 SPI&#xff0c;即串行外设接口&#xff08;Serial Peripheral Interface&#xff09;&#xff0c;是一种由Motorola公司首先在其MC68HCXX系列处理器上定义的同步串行接口技术。SPI接口主要用于微控制器&#xff08;MCU&#xff09;与外部设备之间…

1. 到底什么是架构

1. 什么是架构 定义&#xff1a;架构&#xff0c;又名软件架构&#xff0c;是有关软件整体结构与组件的抽象描述&#xff0c;用于指导大型软件系统各个方面的设计优秀架构的特点&#xff1a;优秀的性能、超强的TPS/QPS的承载能力、高可用决定了你能够支撑多少PV的流量 2. 什么…

【Linux修炼进程之权限篇】探讨Linux权限问题

【Linux修炼】——权限问题 目录 一&#xff1a;认识Linux下用户的分类 1.1&#xff1a;如何添加新用户【使用root用户创建添加】 1.2&#xff1a;su指令用法 二&#xff1a;Linux下权限是什么&#xff1f; 2.1&#xff1a;权限所认证的是身份(人身份角色) 2.2&#xff…

【WPF】04 Http消息处理类

这里引入微软官方提供的HttpClient类来实现我们的目的。 首先&#xff0c;介绍一下官方HttpClient类的内容。 HttpClient 类 定义 命名空间: System.Net.Http 程序集: System.Net.Http.dll Source: HttpClient.cs 提供一个类&#xff0c;用于从 URI 标识的资源发送 HTTP 请…

dbt doc 生成文档命令示例应用

DBT提供了强大的命令行工具&#xff0c;它使数据分析师和工程师能够更有效地转换仓库中的数据。dbt的一个关键特性是能够为数据模型生成文档&#xff0c;这就是dbt docs命令发挥作用的地方。本教程将指导您完成使用dbt生成和提供项目文档的过程。 dbt doc 命令 dbt docs命令有…

Gitxray:一款基于GitHub REST API的网络安全工具

关于Gitxray Gitxray是一款基于GitHub REST API的网络安全工具&#xff0c;支持利用公共 GitHub REST API 进行OSINT、信息安全取证和安全检测等任务。 Gitxray&#xff08;Git X-Ray 的缩写&#xff09;是一款多功能安全工具&#xff0c;专为 GitHub 存储库而设计。它可以用于…

STM32CUBEIDE的使用【三】RTC

于正点原子潘多拉开发板&#xff0c;使用stm32官方免费软件进行开发 CubeMx 配置 使用CubeMx 配置RTC 勾选RTC 设置日期和时间 配置LCD的引脚用来显示 STM32CUBEIDE 在usbd_cdc_if.c中重定向printf函数用于打印 #include <stdarg.h>void usb_printf(const char *f…