Skip to content

Commit c08b1af

Browse files
authored
Create beautiful-arrangement-ii.cpp
1 parent 9ab2677 commit c08b1af

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

C++/beautiful-arrangement-ii.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
class Solution {
5+
public:
6+
vector<int> constructArray(int n, int k) {
7+
vector<int> result;
8+
int left = 1, right = n;
9+
while (left <= right) {
10+
if (k > 1) {
11+
result.emplace_back(k-- % 2 ? left++ : right--);
12+
}
13+
else {
14+
result.emplace_back(k % 2 ? left++ : right--);
15+
}
16+
}
17+
return result;
18+
}
19+
};

0 commit comments

Comments
 (0)