Team Day Photo I

EPI 14.8


Description:

Taking photo of two team players. front row team player must be shorter than the back row player. If ok return true, else return false.

Given a row of numbers, each number represent the height of player.

Example:

Note

Idea:

Sort according to height. Check if the front row number is smaller than the back row one by one from back to front.

time O(n)

Code:

#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

bool teamPhoto(vector<int> frontRow, vector<int> backRow){
    if(frontRow.size()>backRow.size())
        return false;
    if(frontRow.empty())
        return true;

    sort(frontRow.begin(), frontRow.end());
    sort(backRow.begin(), backRow.end());

    int i=frontRow.size()-1;
    int j=backRow.size()-1;

    while(i>=0 && j>=0){
        if(frontRow[i]>=backRow[j]){
            return false;
        }
        --i;
        --j;
    }

    return true;
}


int main(){
    vector<int> backRow={1,2,3,4,5};
    vector<int> frontRow={2,2,4};

    if(teamPhoto(frontRow, backRow)){
        cout<<"Success";
    }
    else{
        cout<<"Fail";
    }
}

results matching ""

    No results matching ""