2021-12-14:根据身高重建队列。 假设有打乱顺序的一群人站成一个队列,数组 people 表示队列中一些人的属性(不一定按顺序)。每个 people[i] = [hi, ki] 表示第 i 个
2021-12-14:根据身高重建队列。
假设有打乱顺序的一群人站成一个队列,数组 people 表示队列中一些人的属性(不一定按顺序)。每个 people[i] = [hi, ki] 表示第 i 个人的身高为 hi ,前面 正好 有 ki 个身高大于或等于 hi 的人。
请你重新构造并返回输入数组 people 所表示的队列。返回的队列应该格式化为数组 queue ,其中 queue[j] = [hj, kj] 是队列中第 j 个人的属性(queue[0] 是排在队列前面的人)。
力扣406。
答案2021-12-14:
具体见代码。golang代码有点问题,具体可查看java代码。
代码用golang编写。代码如下:
package main
import (
"fmt"
"sort"
)
func main() {
people := [][]int{{6, 0}, {5, 0}, {4, 0}, {3, 2}, {2, 2}, {1, 4}}
ret := reconstructQueue2(people)
fmt.Println(ret)
}
func reconstructQueue1(people [][]int) [][]int {
N := len(people)
units := make([]*Unit, N)
for i := 0; i < N; i++ {
units[i] = NewUnit(people[i][0], people[i][1])
}
sort.Slice(units, func(i, j int) bool {
o1 := units[i]
o2 := units[j]
if o1.h != o2.h {
return o2.h < o1.h
} else {
return o1.k > o2.k
}
})
arrList := make([]*Unit, 0)
for _, unit := range units {
arrListCopy := arrList[0:unit.k]
arrListCopy = append(arrListCopy, unit)
arrListCopy = append(arrListCopy, arrList[unit.k:]...)
arrList = arrListCopy
}
ans := make([][]int, N)
for i := 0; i < N; i++ {
ans[i] = make([]int, 2)
}
index := 0
for _, unit := range arrList {
ans[index][0] = unit.h
ans[index][1] = unit.k
index++
}
return ans
}
func reconstructQueue2(people [][]int) [][]int {
N := len(people)
units := make([]*Unit, N)
for i := 0; i < N; i++ {
units[i] = NewUnit(people[i][0], people[i][1])
}
sort.Slice(units, func(i, j int) bool {
o1 := units[i]
o2 := units[j]
if o1.h != o2.h {
return o2.h < o1.h
} else {
return o1.k > o2.k
}
})
tree := &SBTree{}
for i := 0; i < N; i++ {
tree.insert(units[i].k, i)
}
allIndexes := tree.allIndexes()
ans := make([][]int, N)
for i := 0; i < N; i++ {
ans[i] = make([]int, 2)
}
index := 0
for _, arri := range *allIndexes {
ans[index][0] = units[arri].h
ans[index][1] = units[arri].k
index++
}
return ans
}
type Unit struct {
h int
k int
}
func NewUnit(height, greater int) *Unit {
ret := &Unit{}
ret.h = height
ret.k = greater
return ret
}
type SBTNode struct {
value int
l *SBTNode
r *SBTNode
size int
}
func NewSBTNode(arrIndex int) *SBTNode {
ret := &SBTNode{}
ret.value = arrIndex
ret.size = 1
return ret
}
func twoSelectOne(c bool, a, b int) int {
if c {
return a
} else {
return b
}
}
type SBTree struct {
root *SBTNode
}
func (this *SBTree) rightRotate(cur *SBTNode) *SBTNode {
leftNode := cur.l
cur.l = leftNode.r
leftNode.r = cur
leftNode.size = cur.size
a := 0
if cur.l != nil {
a = cur.l.size
}
b := 0
if cur.r != nil {
b = cur.r.size
}
cur.size = a + b + 1
return leftNode
}
func (this *SBTree) leftRotate(cur *SBTNode) *SBTNode {
rightNode := cur.r
cur.r = rightNode.l
rightNode.l = cur
rightNode.size = cur.size
cur.size = (twoSelectOne(cur.l != nil, cur.l.size, 0)) + (twoSelectOne(cur.r != nil, cur.r.size, 0)) + 1
return rightNode
}
func (this *SBTree) maintain(cur *SBTNode) *SBTNode {
if cur == nil {
return nil
}
leftSize := 0
if cur.l != nil {
leftSize = cur.l.size
}
leftLeftSize := 0
if cur.l != nil && cur.l.l != nil {
leftLeftSize = cur.l.l.size
} else {
leftLeftSize = 0
}
leftRightSize := 0
if cur.l != nil && cur.l.r != nil {
leftRightSize = cur.l.r.size
}
rightSize := 0
if cur.r != nil {
rightSize = cur.r.size
}
rightLeftSize := 0
if cur.r != nil && cur.r.l != nil {
rightLeftSize = cur.r.l.size
}
rightRightSize := 0
if cur.r != nil && cur.r.r != nil {
rightRightSize = cur.r.r.size
}
if leftLeftSize > rightSize {
cur = this.rightRotate(cur)
cur.r = this.maintain(cur.r)
cur = this.maintain(cur)
} else if leftRightSize > rightSize {
cur.l = this.leftRotate(cur.l)
cur = this.rightRotate(cur)
cur.l = this.maintain(cur.l)
cur.r = this.maintain(cur.r)
cur = this.maintain(cur)
} else if rightRightSize > leftSize {
cur = this.leftRotate(cur)
cur.l = this.maintain(cur.l)
cur = this.maintain(cur)
} else if rightLeftSize > leftSize {
cur.r = this.rightRotate(cur.r)
cur = this.leftRotate(cur)
cur.l = this.maintain(cur.l)
cur.r = this.maintain(cur.r)
cur = this.maintain(cur)
}
return cur
}
func (this *SBTree) insert0(root *SBTNode, index int, cur *SBTNode) *SBTNode {
if root == nil {
return cur
}
root.size++
leftAndHeadSize := 0
if root.l != nil {
leftAndHeadSize = root.l.size + 1
} else {
leftAndHeadSize = 1
}
if index < leftAndHeadSize {
root.l = this.insert0(root.l, index, cur)
} else {
root.r = this.insert0(root.r, index-leftAndHeadSize, cur)
}
root = this.maintain(root)
return root
}
func (this *SBTree) get0(root *SBTNode, index int) *SBTNode {
leftSize := twoSelectOne(root.l != nil, root.l.size, 0)
if index < leftSize {
return this.get0(root.l, index)
} else if index == leftSize {
return root
} else {
return this.get0(root.r, index-leftSize-1)
}
}
func (this *SBTree) process(head *SBTNode, indexes *[]int) {
if head == nil {
return
}
this.process(head.l, indexes)
//indexes.addLast(head.value)
*indexes = append(*indexes, head.value)
this.process(head.r, indexes)
}
func (this *SBTree) insert(index, value int) {
cur := NewSBTNode(value)
if this.root == nil {
this.root = cur
} else {
if index <= this.root.size {
this.root = this.insert0(this.root, index, cur)
}
}
}
func (this *SBTree) get(index int) int {
ans := this.get0(this.root, index)
return ans.value
}
func (this *SBTree) allIndexes() *[]int {
//LinkedList<Integer> indexes = new LinkedList<>();
indexes := make([]int, 0)
this.process(this.root, &indexes)
return &indexes
}
执行结果如下:

2021-12-14:根据身高重建队列。 假设有打乱顺序的一群人站成一个队列,数组 people 表示队列中一些人的属性(不一定按顺序)。每个 people[i] = [hi, ki] 表示第 i 个的更多相关文章
- 【LEETCODE】73、根据身高重建队列 第406题
说实话,这道题我没想出来,但是看解题报告题解比较让人觉得眼前一亮,这里记录下来 package y2019.Algorithm.greedy.medium; import java.util.Arra ...
- [Swift]LeetCode406. 根据身高重建队列 | Queue Reconstruction by Height
Suppose you have a random list of people standing in a queue. Each person is described by a pair of ...
- Leetcode:根据身高重建队列
题目 假设有打乱顺序的一群人站成一个队列. 每个人由一个整数对(h, k)表示,其中h是这个人的身高,k是排在这个人前面且身高大于或等于h的人数. 编写一个算法来重建这个队列. 注意: 总人数少于11 ...
- Leetcode 406.根据身高重建队列
根据身高重建队列 假设有打乱顺序的一群人站成一个队列. 每个人由一个整数对(h, k)表示,其中h是这个人的身高,k是排在这个人前面且身高大于或等于h的人数. 编写一个算法来重建这个队列. 注意:总人 ...
- 406 Queue Reconstruction by Height 根据身高重建队列
假设有打乱顺序的一群人站成一个队列. 每个人由一个整数对(h, k)表示,其中h是这个人的身高,k是排在这个人前面且身高大于或等于h的人数. 编写一个算法来重建这个队列.注意:总人数少于1100人.示 ...
- 【LeetCode】406-根据身高重建队列
title: 406-根据身高重建队列 date: 2019-04-15 21:13:06 categories: LeetCode tags: Java容器 比较器 贪心思想 题目描述 假设有打乱顺 ...
- LeetCode 406. 根据身高重建队列(Queue Reconstruction by Height) 46
406. 根据身高重建队列 406. Queue Reconstruction by Height 题目描述 假设有打乱顺序的一群人站成一个队列.每个人由一个整数对 (h, k) 表示,其中 h 是这 ...
- Java实现 LeetCode 406 根据身高重建队列
406. 根据身高重建队列 假设有打乱顺序的一群人站成一个队列. 每个人由一个整数对(h, k)表示,其中h是这个人的身高,k是排在这个人前面且身高大于或等于h的人数. 编写一个算法来重建这个队列. ...
- Noip模拟76 2021.10.14
T1 洛希极限 上来一道大数据结构或者单调队列优化$dp$ 真就没分析出来正解复杂度 正解复杂度$O(q+nm)$,但是据说我的复杂度是假的 考虑一个点转移最优情况是从它上面的一个反$L$形转移过来 ...
- 2021.12.16 eleveni的刷题记录
2021.12.16 eleveni的刷题记录 1. 数论 https://www.luogu.com.cn/problem/P2532 1.1卡特兰数 https://www.luogu.com.c ...
随机推荐
- mysql zip安装步骤
1. 官网下载社区版 https://dev.mysql.com/downloads/mysql/ 版本5.7或者8.0 2. 解压到指定的目录. 3.创建my.ini文件,编辑内容: [mysqld ...
- redis保存数据
转载: https://blog.csdn.net/y510662669/article/details/106677717
- pytorch CrossEntropyLoss() 默认转换one-hot编码
import torchpredict = torch.randn((4,3))predict = torch.nn.functional.softmax(predict,dim = 1)target ...
- python爬虫基础教程
爬虫介绍 爬虫就是程序,是从互联网中,各个网站上爬取数据(能浏览到的网页才可以爬),做数据清洗,入库 爬虫本质: 模拟http请求,获取数据,入库 网站/app > 抓包 我们日常使用的baid ...
- 太坑了,我竟然从RocketMQ源码中扒出了7种导致消息重复消费的原因
大家好,我是三友~~ 在众多关于MQ的面试八股文中有这么一道题,"如何保证MQ消息消费的幂等性". 为什么需要保证幂等性呢?是因为消息会重复消费. 为什么消息会重复消费? 明明已经 ...
- Collection单列集合总结
这篇文章记录了Collection集合,List集合,Set集合 在文章第七点总结了两大系列集合的五种实现类的区别,有需要的小伙伴可以直接去查看 一.什么是集合 集合是Java中存储对象数据的一种容器 ...
- LinkedBlockingQueue出入队实现原理
类图概述 由类图可以看出,L是单向链表实现的,有两个ReentrantLock实例用来控制元素入队和出队的原子性,takeLock用来控制只有一个线程可以从队头获取元素,putLock控制只有一个线程 ...
- 器学习算法(六)基于天气数据集的XGBoost分类预测
1.机器学习算法(六)基于天气数据集的XGBoost分类预测 1.1 XGBoost的介绍与应用 XGBoost是2016年由华盛顿大学陈天奇老师带领开发的一个可扩展机器学习系统.严格意义上讲XGBo ...
- TCP三次握手,四次分手。个人感觉最容易理解的解释
三次握手 名词解释 SYN,ACK,FIN存放在TCP的标志位,一共有6个字符,这里就介绍这三个: SYN:代表请求创建连接,所以在三次握手中前两次要SYN=1,表示这两次用于建立连接,至于第三次什么 ...
- 常用脚本学习手册——Bat脚本
常用脚本学习手册--Bat脚本 我们在日常工作中常常会遇到一些需要重复进行的工作,又或者我们的项目在转交客户时需要去简化配置过程 这时我们就需要使用到一些自动化部署操作,我们常常会采用脚本来完成这部分 ...