aboutsummaryrefslogtreecommitdiff
path: root/beautiful_array.py
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-07-10 23:14:36 -0600
committerJack Sangdahl <[email protected]>2025-07-10 23:14:36 -0600
commit19c811f19b9732c8166e632535d1262093ae5006 (patch)
tree3d8e7a9e6c783188561ec961699a50b290296664 /beautiful_array.py
parentb56ca2e2ad30474103c9826147ab8c7fe1f0751a (diff)
downloadleetcode-19c811f19b9732c8166e632535d1262093ae5006.tar.gz
leetcode-19c811f19b9732c8166e632535d1262093ae5006.zip
932. beautiful array
Diffstat (limited to 'beautiful_array.py')
-rw-r--r--beautiful_array.py27
1 files changed, 27 insertions, 0 deletions
diff --git a/beautiful_array.py b/beautiful_array.py
new file mode 100644
index 0000000..1cbbfeb
--- /dev/null
+++ b/beautiful_array.py
@@ -0,0 +1,27 @@
+# 932. Beautiful Array
+
+"""
+an array 'nums' of length 'n' is beautiful if 'nums' is a permutation of the
+integers in the range '[1, n]', and for every '0 <= i < j < n', there is no
+index 'k' with 'i < k < j' where '2 nums[k] == nums[i] + nums[j]'. given
+the integer 'n', return any beautiful array 'nums' of length 'n'. there will
+be at least one valid answer for the given 'n'.
+"""
+
+
+class Solution(object):
+ def beautifulArray(self, n):
+ """
+ :type n: int
+ :rtype: List[int]
+ """
+ ans = [1]
+ while len(ans) < n:
+ ans = [i * 2 - 1 for i in ans] + [i * 2 for i in ans]
+ return [i for i in ans if i <= n]
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.beautifulArray(n=4))
+ print(obj.beautifulArray(n=5))