Skip to content

Commit

Permalink
Create beautiful-array.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
kamyu104 authored Oct 28, 2018
1 parent ab4a502 commit 1ba8dd3
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions C++/beautiful-array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Time: O(nlogn)
// Space: O(n)

class Solution {
public:
vector<int> beautifulArray(int N) {
vector<int> result{1};
while (result.size() < N) {
vector<int> tmp;
for (const auto& i : result) {
tmp.emplace_back(i * 2 - 1);
}
for (const auto& i : result) {
tmp.emplace_back(i * 2);
}
result = move(tmp);
}
vector<int> tmp = move(result);
for (const auto& i : tmp) {
if (i <= N) {
result.emplace_back(i);
}
}
return result;
}
};

0 comments on commit 1ba8dd3

Please sign in to comment.