博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CarreerCup Sort Height
阅读量:4183 次
发布时间:2019-05-26

本文共 2318 字,大约阅读时间需要 7 分钟。

You are given two array, first array contain integer which represent heights of persons and second array contain how many persons in front of him are standing who are greater than him in term of height and forming a queue. Ex
A: 3 2 1
B: 0 1 1
It means in front of person of height 3 there is no person standing, person of height 2 there is one person in front of him who has greater height then he, similar to person of height 1. Your task to arrange them
Ouput should be.
3 1 2

Here - 3 is at front, 1 has 3 in front ,2 has 1 and 3 in front.

*********************************************Solution**************************************************************************************

Arrange From the highest to the next....

Though I like the idea of sorting using the comparator as mentioned above by amitb2108 but below is the approach that came to my mind first.
lets say height[] = {3,1,2,4}
pos[] = {0,2,1,0}; //no of persons greater height than him
1. create an array of person struct of size n and fill the data from the above two arrays
struct person
{
int height;
int num;
};
2. Sort the person array with height as the key in decreasing order. o(nlgn)
index 0,1,2,3
person[] = {4,3,2,1}
{0,0,1,2} //person.num
3. Remember the index of array represents the no of persons greater in front of the current index. e.g. person with height 3 has array index 1, so 1 person is in front of him with greater height. But we need to have 0 no of person greater than 3, so swap it with index 0.
person[] = {3,4,2,1} //after swapping 3
//2 has only one person in front but index of 2 is 2 currently there are 2 persons
//swap it with index 1
person[] = {3,2,4,1}
//1 has only 2 persons in front but index of 1 is 3, so currently there are 3 persons
//swap it with index 2
person[] = {3,2,1,4}
the idea is, previous index, has a person with greater height than current index. The previous index person's position is already set. Now if we move this previous index person towards right it doesn't impact the position of this person. e.g. person with height 4, if we move this person towards the right, still the no of persons with greater height will be 0.
Total complexity = o(nlogn)
*********************************************Solution**************************************************************************************

转载地址:http://prfoi.baihongyu.com/

你可能感兴趣的文章
4字节 整数哈希 ----------jenkins 32位Hash算法
查看>>
哈希函数的逆向算法
查看>>
1-3 beanstalkd参数
查看>>
git版本控制管理系列-----第四章 GIT基本概念
查看>>
mysql 库级权限、表级权限授权
查看>>
TensorFlow中的单层神经网络
查看>>
在TensorFlow中编程
查看>>
用c实现一个压力测试工具
查看>>
圆周率计算公式
查看>>
排序算法之-选择排序
查看>>
排序算法之-基数排序
查看>>
scikit-learn
查看>>
原生java方法操作SQLite数据库
查看>>
sqlite 数据库驱动框架
查看>>
B树、B+树、B*树 总结
查看>>
kafka常用命令
查看>>
kafka顺序消息
查看>>
kafka 消息服务
查看>>
从零开始玩转JMX(一)——简介和Standard MBean
查看>>
究竟啥才是互联网架构中的高并发!
查看>>