---
# System prepended metadata

title: LeetCode 932. Beautiful Array

---

# LeetCode 932. Beautiful Array
[LeetCode 932. Beautiful Array]() (<font color=#FFB800>Medium</font> 39.6%)
<!-- 
(<font color=#00AF9B>Easy</font> 53.8%) 
(<font color=#FFB800>Medium</font> 39.6%)
(<font color=#FF375F>Hard</font>)
 -->
    
- 限制 :

    <ul>
    <li><code>1 <= n <= 1000</code></li>
    </ul>

- Solution
    - 時間複雜度: $O(nlogn)$
    - 空間複雜度: $O(n)$

- 程式碼
    ```c++=
    class Solution {
    public:
        vector<int> beautifulArray(int n) {
            vector<int> result = {1};
            while (result.size() < n) {
                vector<int> temp;
                for (int i : result) {
                    if (i * 2 - 1 <= n) temp.push_back(i * 2 - 1); 
                }
                for (int i : result) {
                    if (i * 2 <= n) temp.push_back(i * 2); 
                }
                result = temp; // 更新結果
            }
            return result;

        }
    };
    ```
</details>