首页 > 其他分享 >406. 根据身高重建队列c

406. 根据身高重建队列c

时间:2024-03-17 14:56:24浏览次数:12  
标签:index 队列 406 int num students array 身高 peopleSize

/**
 * 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().
 */
typedef struct node{
    int h;
    int n;
}students;

void insert(int** array,int n,int index,students num){
    for(int i=n;i>index;i--){
        array[i][0]=array[i-1][0];
        array[i][1]=array[i-1][1];
    }
    array[index][0]=num.h;
    array[index][1]=num.n;
}

int cmp(const void* a,const void* b){
    students* x=(students*)a;
    students* y=(students*)b;
    if(x->h==y->h){
        return x->n-y->n;
    }
    return y->h-x->h;
}

int** reconstructQueue(int** people, int peopleSize, int* peopleColSize, int* returnSize, int** returnColumnSizes) {
    *returnSize=peopleSize;
    *peopleColSize=2;
    int* column=(int*)malloc(sizeof(int)*peopleSize);
    for(int i=0;i<peopleSize;i++) column[i]=2;
    students* s=(students*)malloc(sizeof(students)*peopleSize);
    for(int i=0;i<peopleSize;i++){
        s[i].h=people[i][0];
        s[i].n=people[i][1];    
    }
    qsort(s,peopleSize,sizeof(students),cmp);
    int** array=(int**)malloc(sizeof(int*)*peopleSize);
    for(int i=0;i<peopleSize;i++) array[i]=(int*)malloc(sizeof(int)*2);
    for(int i=0;i<peopleSize;i++){
        printf("%d",i);
        insert(array,i,s[i].n,s[i]);
    }
    *returnColumnSizes=column;
    return array;
}

标签:index,队列,406,int,num,students,array,身高,peopleSize
From: https://www.cnblogs.com/llllmz/p/18078584

相关文章

  • 【洛谷 P8661】[蓝桥杯 2018 省 B] 日志统计 题解(滑动窗口+优先队列+双端队列+集合)
    [蓝桥杯2018省B]日志统计题目描述小明维护着一个程序员论坛。现在他收集了一份“点赞”日志,日志共有NNN行。其中每一行的格式是tsid,表示在......
  • 代码随想录: 栈和队列
    cpp中stack和queue都是一种适配器。三个最为普遍的STL版本:HPSTL其他版本的C++STL,一般是以HPSTL为蓝本实现出来的,HPSTL是C++STL的第一个实现版本,而且开放源代码。P.J.PlaugerSTL由P.J.Plauger参照HPSTL实现出来的,被VisualC++编译器所采用,不是开源的。SGISTL由Sili......
  • 代码随想录算法训练营第十天(栈和队列I)| 232. 用栈实现队列、225. 用队列实现栈(JAVA)
    文章目录栈和队列理论基础概念方法栈队列232.用栈实现队列解题思路源码225.用队列实现栈解题思路源码总结栈和队列理论基础概念栈:后进先出队列:先进先出方法栈方法名作用Stackstack=newStack<>();构造栈stack.push(Ee)将e入栈,并返回estack.pop()将栈......
  • [bzoj2120]数颜色/维护队列 (分块)
    数颜色/维护队列[做题笔记]此生第一道不贺题解\(AC\)的分块蓝题!!!题目描述墨墨@hs_mo购买了一套\(N\)支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问。墨墨会向你发布如下指令:\(Q\L\R\)代表询问你从第\(L\)支画笔到第\(R\)支画笔中共有几种不同颜色的......
  • 【linux system V 消息队列】
    #简介消息队列就是一些消息的列表,或者说是一些消息组成的队列。消息队列与管道有些类似,消息队列可以认为是管道的改进版。相较于管道的先进先出准则,消息队列在读取时可以按照消息的类型进行读取,这也是消息队列的特点,它可以实现消息随机查询。消息发送时,需要将消息封装,然......
  • 单调队列
    题目1点击查看代码#include<bits/stdc++.h>usingnamespacestd;#defineintlonglong#definepiipair<int,int>#defineinf0x3f3f3f3fsignedmain(){ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);intn,m;cin>>n>>m;......
  • leedcode-用队列实现栈
    利用内置的listclassMyStack:def__init__(self):#初始化一个空列表用于存储栈的元素self.li=list()defpush(self,x:int)->None:#向栈中压入元素xself.li.append(x)defpop(self)->int:#从栈顶弹......
  • 代码随想录算法训练营第十天| 232. 用栈实现队列 225. 用队列实现栈
    232.用栈实现队列https://leetcode.cn/problems/implement-queue-using-stacks/description/classMyQueue{Stack<Integer>stackIn;Stack<Integer>stackOut;publicMyQueue(){stackIn=newStack<>();stackOut=new......
  • Python使用RocketMQ(消息队列)
    消息队列在日常开发中比较常用的开发中间件,每家大厂一般都会具有自己的消息队列服务器。本文主要讲述Python中如何使用RocketMQ的相关SDK。希望大家在阅读本文前可以先了解一下RocketMQ的基本知识。使用 pipinstallrocketmq-ihttps://pypi.tuna.tsinghua.edu.cn/sim......
  • LeetCode232.栈实现队列
    ques:用两个栈实现队列功能ans:与225一样的思路,看完225大佬们的题解之后能很轻松的想出思路,用s1来实现真正模拟队列中的元素顺序,借助s2辅助完成这一排序代码实现#include<iostream>#include<stack>usingnamespacestd;classMyQueue{private:stack<int>s1;/......