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

【Leetcode笔记】406.根据身高重建队列

文章目录

    • 1. 题目要求
    • 2.解题思路
  • 注意
    • 3.ACM模式代码

1. 题目要求

在这里插入图片描述

2.解题思路

首先,按照每个人的身高属性(即people[i][0])来排队,顺序是从大到小降序排列,如果遇到同身高的,按照另一个属性(即people[i][1])来从小到大升序排列。
使用到C++的sort()函数,第三个参数cmp函数自己定义:

static bool cmp(const vector<int>& a, const vector<int>& b)
{
// 使用const关键字来确保在函数内部不会修改a和bif(a[0] == b[0]) return a[1] < b[1]; // 象形的记,升序return a[0] > b[0];
}

接下来再次遍历people数组,从前到后将每个元素放入到一个二维数组里。
为什么从前到后?
先排身高更高的,后面身高矮的即使因为第二个属性需要插入到前面人的中间去也没关系,反正身高更高的第二个属性不受影响。但是从后到前先排身高矮的可就不行了。

vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {sort (people.begin(), people.end(), cmp);vector<vector<int>> que;for (int i = 0; i < people.size(); i++) {int position = people[i][1];que.insert(que.begin() + position, people[i]);}return que;}

时间复杂度要大于O(nlogn + n ^ 2),首先C++里的sort函数的时间复杂度就是O(nlogn),这个排序函数内部并不是单一的快速排序或者是其他的,而是动态改变的,可能一开始数据量较大时先快速排序对半分,等分到后面则使用插入排序;C++的vector是一个动态数组,插入操作是先考虑原来的数组大小够不够,如果不够那就二倍扩容,然后把原数组拷贝到新数组再插入新的元素,所以时间复杂度要大于O(n^2)。
将数组改成链表

vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {sort (people.begin(), people.end(), cmp);list<vector<int>> que; // list底层是链表实现,插入效率比vector高的多for (int i = 0; i < people.size(); i++) {int position = people[i][1]; // 插入到下标为position的位置std::list<vector<int>>::iterator it = que.begin();while (position--) { // 寻找在插入位置it++;}que.insert(it, people[i]);}return vector<vector<int>>(que.begin(), que.end());

注意

sort函数的第三个参数是cmp,cmp是一个比较函数,想这样使用 sort (people.begin(), people.end(), cmp);那必须得将cmp声明为静态成员函数,这样就不需要将函数实例化了。

3.ACM模式代码

#include <vector>
#include <algorithm>
#include <list>
#include <iostream> // 用于输出调试using namespace std;class Solution {
public:static bool cmp(const vector<int>& a, const vector<int>& b) {// Sort by height descending, and if heights are same, by k ascendingif (a[0] == b[0]) {return a[1] < b[1]; // Sort by k in ascending order} else {return a[0] > b[0]; // Sort by height in descending order}}vector<vector<int>> reconstructQueue(vector<vector<int>>& people) {// Sort people array using custom comparatorsort(people.begin(), people.end(), cmp);// Use list for efficient insertionlist<vector<int>> que;// Insert into list at the specified index (k value)for (int i = 0; i < people.size(); i++) {int position = people[i][1]; // k value tells us the exact index to insertauto it = que.begin();advance(it, position); // Move iterator to the correct positionque.insert(it, people[i]); // Insert person into the list}// Convert list back to vector for returning the resultreturn vector<vector<int>>(que.begin(), que.end());}
};int main() {Solution sol;// Example usage:vector<vector<int>> people = {{7,0}, {4,4}, {7,1}, {5,0}, {6,1}, {5,2}};vector<vector<int>> result = sol.reconstructQueue(people);// Print the resultcout << "[";for (const auto& person : result) {cout << "[" << person[0] << ", " << person[1] << "],";}cout << "]";cout << endl;return 0;
}
http://www.lryc.cn/news/392689.html

相关文章:

  • Linux 安装pdfjam (PDF文件尺寸调整)
  • python+playwright 学习-90 and_ 和 or_ 定位
  • 亲子时光里的打脸高手,贾乃亮与甜馨的父爱如山
  • MySQL篇-SQL优化实战
  • 【MySQL备份】Percona XtraBackup总结篇
  • 【Git 】规范 Git 提交信息的工具 Commitizen
  • ABB PPC902AE1013BHE010751R0101控制器 处理器 模块
  • 大模型AIGC转行记录(一)
  • element-ui Tree之懒加载叶子节点强制设置父级半选效果
  • Java项目:基于SSM框架实现的高校共享单车管理系统分前后台【ssm+B/S架构+源码+数据库+开题报告+任务书+毕业论文】
  • 【Android】自定义换肤框架02之自定义AssetManager和Resource
  • 熵权法、熵值法、熵权TOPSIS三种方法的实用场景及优劣比较
  • 无人机人员搜救
  • 目标检测算法
  • SpringSecurity 三更草堂学习笔记
  • 鸿蒙生态应用开发白皮书V3.0
  • CSS - 深入理解选择器的使用方式
  • 动手学深度学习(Pytorch版)代码实践 -循环神经网络-54~55循环神经网络的从零开始实现和简洁实现
  • Python酷库之旅-第三方库Pandas(006)
  • 智慧矿山:EasyCVR助力矿井视频多业务融合及视频转发服务建设
  • Unix/Linux shell实用小程序1:生字本
  • springboot2.7.6 集成swagger
  • 面试篇-系统设计题总结
  • 如何摆脱反爬虫机制?
  • 68745
  • github仓库的基本使用-创建、上传文件、删除
  • [课程][原创]opencv图像在C#与C++之间交互传递
  • 科研绘图系列:R语言双侧条形图(bar Plot)
  • 计算机未来大方向的选择
  • AndroidKille不能用?更新apktool插件-cnblog