zoukankan      html  css  js  c++  java
  • 程序员面试真题--(2)

    题目描述:

    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.

    思路:

    http://blog.csdn.net/taoqick/article/details/18005429

    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********************************************************************************

     1 #include <iostream>
     2 #include <queue>
     3 #include <climits>
     4 #include <algorithm>
     5 #include <memory.h>
     6 #include <stdio.h>
     7 #include <map>
     8 #include <vector>
     9 using namespace std;
    10 
    11 struct person
    12 {
    13     int height;
    14     int higherNum;
    15     bool operator<(const person&other)const
    16     {
    17         return height > other.height;
    18     }
    19 };
    20 
    21 void swap_person(vector<person> &person_arr,int i,int j)
    22 {
    23     int htmp;
    24     int hightmp;
    25 
    26     htmp = person_arr[i].height;
    27     hightmp = person_arr[i].higherNum;
    28     person_arr[i].height = person_arr[j].height;
    29     person_arr[j].height = htmp;
    30 
    31     person_arr[i].higherNum = person_arr[j].higherNum;
    32     person_arr[i].higherNum = hightmp;
    33     return;
    34 }
    35 
    36 int main()
    37 {
    38     vector<person> per_arr;
    39     //height[] = {3,1,2,4}
    40     //pos[] = {0,2,1,0};
    41     person p;
    42     p.height = 3;
    43     p.higherNum = 0;
    44     per_arr.push_back(p);
    45     p.height = 1;
    46     p.higherNum = 2;
    47     per_arr.push_back(p);
    48     p.height = 2;
    49     p.higherNum = 1;
    50     per_arr.push_back(p);
    51     p.height = 4;
    52     p.higherNum = 0;
    53     per_arr.push_back(p);
    54 
    55     sort(per_arr.begin(),per_arr.end());
    56     /*vector<person>::iterator ite = per_arr.begin();
    57     while(ite != per_arr.end())
    58     {
    59         cout<<(*ite).height<<endl;
    60         ++ite;
    61     }*/
    62     int i;
    63     for(i = 1 ; i < per_arr.size() ; ++i)
    64     {
    65         //cout<<per_arr[i].higherNum<<" "<<i<<endl;
    66         if(per_arr[i].higherNum < i)
    67         {
    68             swap_person(per_arr,i,per_arr[i].higherNum);
    69         }
    70     }
    71     ite = per_arr.begin();
    72     while(ite != per_arr.end())
    73     {
    74         cout<<(*ite).height<<endl;
    75         ++ite;
    76     }
    77     return 0;
    78 }
  • 相关阅读:
    Javascript的二进制数据处理学习 ——nodejs环境和浏览器环境分别分析
    ISO日期格式标准,浏览器到服务器到mysql中的时区
    开始学nodejs —— 调试篇
    TCP三次握手的正确使用姿势
    详解Javascript中正则表达式的使用
    浏览器HTTP缓存原理分析
    seajs3.0.0源码分析记录
    用spm2构建seajs项目的过程
    IIS7禁用单个静态文件的缓存配置方法
    jsp之EL表达式
  • 原文地址:https://www.cnblogs.com/cane/p/3841923.html
Copyright © 2011-2022 走看看