CarreerCup Sort Height
来源:互联网 发布:steam数据 编辑:程序博客网 时间:2024/06/18 15:19
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
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**************************************************************************************
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....
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**************************************************************************************
0 0
- CarreerCup Sort Height
- height
- height和line-height
- height与line-height
- height vs line-height
- sort
- Sort
- sort
- Sort
- sort
- Sort
- sort
- sort
- Sort
- Sort
- sort
- sort
- sort
- 百度LBS 常用问题总结
- [Leetcode] N-Queens II (Java)
- Spring中Quartz的配置
- MIPI DIsplay Panel And Linux Driver Model .
- 关于openfire+spark连不上同一局域网的其他机子
- CarreerCup Sort Height
- mysql分表的3种方法
- WEB前端开发工程师前期准备阶段
- LR性能测试经验总结(持续跟新中)
- linux u盘不识别解决办法
- 关于c c++ 混编包含的头文件出现的问题
- 全局变量 局部变量 栈 堆
- POJ 1511 Invitation Cards 求来回最短路(spfa)
- ionicframework学习和使用