알고리즘/codefights

12>sortByHeight

Diademata 2017. 6. 28. 17:39
반응형

Some people are standing in a row in a park. There are trees between them which cannot be moved. Your task is to rearrange the people by their heights in a non-descending order without moving the trees.


Example


For a = [-1, 150, 190, 170, -1, -1, 160, 180], the output should be

sortByHeight(a) = [-1, 150, 160, 170, -1, -1, 180, 190].


code>>


std::vector<int> sortByHeight(std::vector<int> a) {

std::vector<int> tmp = a;

std::sort(tmp.begin(), tmp.end());

int idx = tmp.size() - 1;

int choose = idx;

while (idx >= 0)

{

if (a[idx] != -1)

{

a[idx] = tmp[choose--];

}

idx--;

}

return a;

}

반응형

'알고리즘 > codefights' 카테고리의 다른 글

14>alternatingSums  (0) 2017.06.28
13>reverseParentheses  (0) 2017.06.28
11>isLucky  (0) 2017.06.28
10>commonCharacterCount  (0) 2017.06.28
9>allLongestStrings  (0) 2017.06.28